From 1170b0798bbd90af2418dce090f9bb99b08e1625 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 12 May 2014 13:57:24 +0300 Subject: [PATCH] make condition brackets optional --- lib/parser/control.rb | 6 ++++-- test/parser/test_conditional.rb | 15 +++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/parser/control.rb b/lib/parser/control.rb index e264eda1..40d6a94d 100644 --- a/lib/parser/control.rb +++ b/lib/parser/control.rb @@ -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 diff --git a/test/parser/test_conditional.rb b/test/parser/test_conditional.rb index 98735783..09931549 100644 --- a/test/parser/test_conditional.rb +++ b/test/parser/test_conditional.rb @@ -3,16 +3,23 @@ require_relative "helper" class TestConditional < MiniTest::Test # include the magic (setup and parse -> test method translation), see there include ParserHelper + + def test_conditional_brackets + check("(0)") + end + def test_conditional_no_brackets + check("0") + end - def test_conditional - @string_input = <"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}