2014-04-27 15:30:32 +02:00
|
|
|
require_relative "basic_types"
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 14:44:34 +02:00
|
|
|
module Parser
|
2014-04-27 15:30:32 +02:00
|
|
|
class Composed < Parslet::Parser
|
|
|
|
include BasicTypes
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
rule(:args) {
|
|
|
|
lparen >>
|
|
|
|
((expression.as(:arg) >> (comma >> expression.as(:arg)).repeat(0)).maybe).as(:args) >>
|
|
|
|
rparen
|
|
|
|
}
|
|
|
|
|
|
|
|
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(:cond) {
|
|
|
|
if_kw >> lparen >> expression.as(:cond) >> rparen >>
|
|
|
|
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(:if_kw) { str('if') >> space? }
|
|
|
|
rule(:else_kw) { str('else') >> space? }
|
|
|
|
|
|
|
|
rule(:func) {
|
|
|
|
func_kw >> name.as(:func) >> params >> body
|
|
|
|
}
|
|
|
|
|
|
|
|
rule(:func_kw) { str('function') >> space? }
|
|
|
|
|
|
|
|
rule(:params) {
|
|
|
|
lparen >>
|
|
|
|
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
|
|
|
|
rparen
|
|
|
|
}
|
2014-04-24 20:02:27 +02:00
|
|
|
rule(:root){ func.repeat(0) >> expression | expression | args }
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
end
|