still trying that while loop. Check as everything else works

This commit is contained in:
Torsten Ruger
2014-05-12 12:14:04 +03:00
parent 4a27314533
commit 7940efc64d
7 changed files with 56 additions and 38 deletions

View File

@ -27,7 +27,7 @@ module Parser
rule(:exponent) { (str('e')| str('E')) }
# identifier must start with lower case
rule(:name) { (match['a-z'] >> match['a-zA-Z0-9'].repeat).as(:name) >> space? }
rule(:name) { keyword.absent? >> (match['a-z'] >> match['a-zA-Z0-9'].repeat).as(:name) >> space? }
rule(:escape) { str('\\') >> any.as(:esc) }
rule(:string) { quote >> (

View File

@ -1,15 +1,13 @@
module Parser
module Conditional
include Parslet
rule(:conditional) {
rule(:conditional) do
keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >> newline >>
delimited_expressions(keyword_else).as(:if_true) >>
delimited_expressions(keyword_end).as(:if_false)
}
expressions_else.as(:if_true) >> expressions_end.as(:if_false)
end
rule(:while) {
keyword_while >> expression.as(:while_cond) >> keyword_do >> newline >>
delimited_expressions(keyword_end).as(:body)
}
rule(:while_do) do
keyword_while >> expressions_do.as(:while_cond) >> expressions_end.as(:body)
end
end
end

View File

@ -3,12 +3,13 @@ module Parser
include Parslet
rule(:simple_expression) { function_call | integer | string | name }
rule(:expression) { (conditional | simple_expression ) >> newline.maybe }
rule(:expression) { (while_do | conditional | simple_expression ) >> newline.maybe }
def delimited_expressions( delimit )
( (delimit.absent? >> (operator_expression | expression)).repeat(1)).as(:expressions) >> delimit >> newline.maybe
end
rule(:expressions_do) { delimited_expressions(keyword_do) }
rule(:expressions_else) { delimited_expressions(keyword_else) }
rule(:expressions_end) { delimited_expressions(keyword_end) }

View File

@ -1,6 +1,7 @@
module Parser
module Keywords
include Parslet
rule(:keyword_begin) { str('begin').as(:begin) >> space?}
rule(:keyword_def) { str('def') >> space? }
rule(:keyword_do) { str('do').as(:do) >> space?}
@ -14,5 +15,11 @@ module Parser
rule(:keyword_unless) { str('unless').as(:unless) >> space?}
rule(:keyword_until) { str('until').as(:until) >> space?}
rule(:keyword_while) { str('while').as(:while) >> space?}
# 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('begin') | str('def') | str('do') | str('else') | str('end') |
str('false')| str('if')| str('rescue')| str('true')| str('nil') |
str('unless')| str('until')| str('while')}
end
end

View File

@ -24,7 +24,8 @@ module Parser
Ast::ConditionalExpression.new(conditional, if_true, if_false)
end
rule(:while => simple(:while), :while_cond => simple(:while_cond) , :do => simple(:do),
rule(:while => simple(:while),
:while_cond => { :expressions => sequence(:while_cond) , :do => simple(:do)} ,
:body => {:expressions => sequence(:body) , :end => simple(:e) }) do
Ast::WhileExpression.new(while_cond, body)
end