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 module Control
include Parslet include Parslet
rule(:conditional) do rule(:conditional) do
keyword_if >> left_parenthesis >> (simple_expression|operator_expression).as(:conditional) >> right_parenthesis >> newline >> keyword_if >>
expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false) (( (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 end
rule(:while_do) do rule(:while_do) do

View File

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