make condition brackets optional

This commit is contained in:
Torsten Ruger 2014-05-12 13:57:24 +03:00
parent 956d1eb135
commit 1170b0798b
2 changed files with 15 additions and 6 deletions

View File

@ -2,8 +2,10 @@ module Parser
module Control
include Parslet
rule(:conditional) do
keyword_if >> left_parenthesis >> (simple_expression|operator_expression).as(:conditional) >> right_parenthesis >> newline >>
expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
keyword_if >>
(( (simple_expression|operator_expression).as(:conditional) ) |
left_parenthesis >> (simple_expression|operator_expression).as(:conditional) >> right_parenthesis) >>
newline >> expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
end
rule(:while_do) do

View File

@ -4,15 +4,22 @@ class TestConditional < MiniTest::Test
# include the magic (setup and parse -> test method translation), see there
include ParserHelper
def test_conditional
@string_input = <<HERE
if(0)
def test_conditional_brackets
check("(0)")
end
def test_conditional_no_brackets
check("0")
end
def check cond
input = <<HERE
42
else
667
end
HERE
@string_input.chop!
@string_input = "if #{cond} " + input.chop!
@parse_output = {:if=>"if", :conditional=>{:integer=>"0"},
:if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"},
:if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}