Browse Source

Allow calling the result of a function call

master
Dylan Baker 5 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
         raise "Unexpected token #{token.type}"
284
         raise "Unexpected token #{token.type}"
285
       end
285
       end
286
 
286
 
287
-    if @current_token.type == TokenKinds::LPAREN
287
+    while @current_token.type == TokenKinds::LPAREN
288
       args = arguments
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
       eat(TokenKinds::LBRACKET)
293
       eat(TokenKinds::LBRACKET)
292
       key = expression
294
       key = expression
293
       eat(TokenKinds::RBRACKET)
295
       eat(TokenKinds::RBRACKET)
294
-      AST::Index.new(expr, key)
295
-    else
296
-      expr
296
+      expr = AST::Index.new(expr, key)
297
     end
297
     end
298
+
299
+    expr
298
   end
300
   end
299
 
301
 
300
   def function_expression
302
   def function_expression

+ 14
- 0
spec/parser_spec.rb View File

454
       [AST::Assignment.new(AST::Identifier.new('x'), AST::Number.new(5.0))]
454
       [AST::Assignment.new(AST::Identifier.new('x'), AST::Number.new(5.0))]
455
     )
455
     )
456
   end
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
 end
471
 end

Loading…
Cancel
Save