rubyx/lib/parser/composed.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2014-04-27 15:30:32 +02:00
require_relative "basic_types"
2014-04-27 17:13:10 +02:00
require_relative "tokens"
2014-04-24 14:43:20 +02:00
module Parser
2014-04-27 17:13:10 +02:00
#obviously a work in progress !!
# We "compose" the parser from bits, divide and hopefully conquer
2014-04-27 15:30:32 +02:00
class Composed < Parslet::Parser
include BasicTypes
2014-04-27 17:13:10 +02:00
include Tokens
2014-04-24 14:43:20 +02:00
rule(:args) {
2014-04-27 17:13:10 +02:00
left_parenthesis >>
2014-04-24 14:43:20 +02:00
((expression.as(:arg) >> (comma >> expression.as(:arg)).repeat(0)).maybe).as(:args) >>
2014-04-27 17:13:10 +02:00
right_parenthesis
2014-04-24 14:43:20 +02:00
}
rule(:funcall) { name.as(:funcall) >> args }
2014-04-27 17:13:10 +02:00
rule(:expression) { cond | funcall | integer | name }
2014-04-24 14:43:20 +02:00
rule(:cond) {
2014-04-27 17:13:10 +02:00
if_kw >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >>
2014-04-24 14:43:20 +02:00
body.as(:if_true) >>
else_kw >>
body.as(:if_false)
}
2014-04-27 17:13:10 +02:00
rule(:body) { left_brace >> expression.as(:body) >> right_brace }
2014-04-24 14:43:20 +02:00
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) {
2014-04-27 17:13:10 +02:00
left_parenthesis >>
2014-04-24 14:43:20 +02:00
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
2014-04-27 17:13:10 +02:00
right_parenthesis
2014-04-24 14:43:20 +02:00
}
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