diff --git a/lib/parser/composed.rb b/lib/parser/composed.rb index 1fa62ae3..f973c365 100644 --- a/lib/parser/composed.rb +++ b/lib/parser/composed.rb @@ -1,34 +1,33 @@ require_relative "basic_types" +require_relative "tokens" module Parser + + #obviously a work in progress !! + # We "compose" the parser from bits, divide and hopefully conquer + class Composed < Parslet::Parser include BasicTypes - + include Tokens + rule(:args) { - lparen >> + left_parenthesis >> ((expression.as(:arg) >> (comma >> expression.as(:arg)).repeat(0)).maybe).as(:args) >> - rparen + right_parenthesis } rule(:funcall) { name.as(:funcall) >> args } - rule(:expression) { cond | funcall | number | name } - - rule(:lparen) { str('(') >> space? } - rule(:rparen) { str(')') >> space? } - rule(:comma) { str(',') >> space? } + rule(:expression) { cond | funcall | integer | name } rule(:cond) { - if_kw >> lparen >> expression.as(:cond) >> rparen >> + if_kw >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >> body.as(:if_true) >> else_kw >> body.as(:if_false) } - rule(:body) { lbrace >> expression.as(:body) >> rbrace } - rule(:lbrace) { str('{') >> space? } - rule(:rbrace) { str('}') >> space? } - rule(:comma) { str(',') >> space? } + rule(:body) { left_brace >> expression.as(:body) >> right_brace } rule(:if_kw) { str('if') >> space? } rule(:else_kw) { str('else') >> space? } @@ -39,9 +38,9 @@ module Parser rule(:func_kw) { str('function') >> space? } rule(:params) { - lparen >> + left_parenthesis >> ((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >> - rparen + right_parenthesis } rule(:root){ func.repeat(0) >> expression | expression | args } end