using new modules

This commit is contained in:
Torsten Ruger 2014-04-27 18:13:10 +03:00
parent d8f228da11
commit 032758db8b

View File

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