Browse Source

Allow calling the result of a function call

master
Dylan Baker 4 years ago
parent
commit
2088c2ed1a
2 changed files with 22 additions and 6 deletions
  1. 8
    6
      lib/ahem/parser.rb
  2. 14
    0
      spec/parser_spec.rb

+ 8
- 6
lib/ahem/parser.rb View File

@@ -284,17 +284,19 @@ class Parser
284 284
         raise "Unexpected token #{token.type}"
285 285
       end
286 286
 
287
-    if @current_token.type == TokenKinds::LPAREN
287
+    while @current_token.type == TokenKinds::LPAREN
288 288
       args = arguments
289
-      AST::FunctionCall.new(expr, args)
290
-    elsif @current_token.type == TokenKinds::LBRACKET
289
+      expr = AST::FunctionCall.new(expr, args)
290
+    end
291
+
292
+    while @current_token.type == TokenKinds::LBRACKET
291 293
       eat(TokenKinds::LBRACKET)
292 294
       key = expression
293 295
       eat(TokenKinds::RBRACKET)
294
-      AST::Index.new(expr, key)
295
-    else
296
-      expr
296
+      expr = AST::Index.new(expr, key)
297 297
     end
298
+
299
+    expr
298 300
   end
299 301
 
300 302
   def function_expression

+ 14
- 0
spec/parser_spec.rb View File

@@ -454,4 +454,18 @@ RSpec.describe Parser do
454 454
       [AST::Assignment.new(AST::Identifier.new('x'), AST::Number.new(5.0))]
455 455
     )
456 456
   end
457
+
458
+  it 'allows calling the result of a function call as a function' do
459
+    expect(parse('func()();')).to eq(
460
+      [
461
+        AST::FunctionCall.new(
462
+          AST::FunctionCall.new(
463
+            AST::Identifier.new('func'),
464
+            [],
465
+          ),
466
+          []
467
+        )        
468
+      ]
469
+    )
470
+  end
457 471
 end

Loading…
Cancel
Save