Browse files

terminate return statement with line break

  • Loading branch information...
1 parent e29e618 commit e2bad48d93db605e3ecbb123beeeb12753441b9b @mental mental committed May 14, 2011
Showing with 17 additions and 1 deletion.
  1. +12 −1 lib/razorpit/parser.rb
  2. +5 −0 spec/parser_spec.rb
View
13 lib/razorpit/parser.rb
@@ -284,7 +284,11 @@ def expression_statement
def try_control_statement
return nil unless try_consume_token(Tokens::RETURN)
- ast = try_expression(MIN_BINDING_POWER)
+ ast = if consume_line_break
+ nil
+ else
+ try_expression(MIN_BINDING_POWER)
+ end
Nodes::Return[ast]
end
@@ -366,6 +370,13 @@ def program
statement_list(Tokens::EOF) { |s| Nodes::Program[*s] }
end
+ def consume_line_break
+ lookahead_token
+ result = @line_break
+ @line_break = false
+ result
+ end
+
def lookahead_token
token = @lexer.peek
case token
View
5 spec/parser_spec.rb
@@ -26,6 +26,11 @@
ast.should == N::Program[N::Return[N::Number[3]]]
end
+ it "terminates return statements with newlines" do
+ ast = RazorPit::Parser.parse("return\n3")
+ ast.should == N::Program[N::Return[], N::Number[3]]
+ end
+
it "assumes semicolon before } and EOF" do
ast = RazorPit::Parser.parse("1; 2")
ast.should == N::Program[N::Number[1], N::Number[2]]

0 comments on commit e2bad48

Please sign in to comment.