From 3061ddfed959c4dfdac5b005c36cd05c7db0c83c Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 19 Oct 2015 15:21:11 +0300 Subject: [PATCH] same syntax change for while_xxx xxx can be any string for the parser the language will restrict if somewhat later --- lib/parser/keywords.rb | 3 +-- lib/parser/statement.rb | 6 +++++- lib/parser/transform.rb | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/parser/keywords.rb b/lib/parser/keywords.rb index 872d39a..3e64087 100644 --- a/lib/parser/keywords.rb +++ b/lib/parser/keywords.rb @@ -11,11 +11,10 @@ module Parser rule(:keyword_return) { str('return').as(:return) >> space?} rule(:keyword_true) { str('true').as(:true) } rule(:keyword_nil) { str('nil').as(:nil) } - rule(:keyword_while) { str('while').as(:while) } # this rule is just to make sure identifiers can't be keywords. Kind of duplication here, but we need the # space in above rules, so just make sure to add any here too. - rule(:keyword){ str('if_') | str('else') | str('end') | str('while') | + rule(:keyword){ str('if_') | str('else') | str('end') | str('while_') | str('false') | str('true')| str('nil') | str("class") | str('return')| str('int')| str('field')} end diff --git a/lib/parser/statement.rb b/lib/parser/statement.rb index 2851213..52615f7 100644 --- a/lib/parser/statement.rb +++ b/lib/parser/statement.rb @@ -2,8 +2,12 @@ module Parser module Statement include Parslet + rule( :keyword_while) do + str("while_") >> alpha.repeat.as(:condition) >> space + end + rule(:while_statement) do - keyword_while >> left_parenthesis >> r_value.as(:condition) >> + keyword_while >> left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >> statements_end.as(:body) end diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index 8ba4c00..8bf10b1 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -63,10 +63,10 @@ module Parser s(:field_access , s(:receiver , receiver) , s(:field , field) ) end - rule(:while => simple(:while), - :condition => simple(:condition) , + rule(:condition => simple(:condition) , + :conditional => simple(:conditional), :body => {:statements => sequence(:body) , :end => simple(:e) }) do - s(:while_statement , s(:condition , condition), s(:statements , *body)) + s(:while_statement,condition.to_s.to_sym, s(:conditional , conditional), s(:statements , *body)) end rule(:condition => simple(:condition), :conditional => simple(:conditional), @@ -80,7 +80,7 @@ module Parser rule(:condition => simple(:condition), :conditional => simple(:conditional), :true_statements => {:statements => sequence(:true_statements) , :end => simple(:e) }) do - s(:if_statement, condition.to_s.to_sym, s(:condition, conditional), + s(:if_statement, condition.to_s.to_sym, s(:condition, conditional), s(:true_statements, *true_statements), s(:false_statements , nil) ) end