rubyx/lib/parser/composed.rb

46 lines
1.3 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"
require_relative "keywords"
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
include Keywords
2014-04-27 17:13:10 +02:00
rule(:argument_list) {
2014-04-27 17:13:10 +02:00
left_parenthesis >>
((expression.as(:argument) >> (comma >> expression.as(:argument)).repeat(0)).maybe).as(:argument_list) >>
2014-04-27 17:13:10 +02:00
right_parenthesis
2014-04-24 14:43:20 +02:00
}
rule(:function_call) { name.as(:function_call) >> argument_list }
2014-04-24 14:43:20 +02:00
rule(:expression) { cond | function_call | integer | name }
2014-04-24 14:43:20 +02:00
rule(:cond) {
keyword_if >> left_parenthesis >> expression.as(:cond) >> right_parenthesis >>
block.as(:if_true) >>
keyword_else >>
block.as(:if_false)
2014-04-24 14:43:20 +02:00
}
rule(:block) { left_brace >> expression.as(:block) >> right_brace }
2014-04-24 14:43:20 +02:00
rule(:function_definition) {
keyword_def >> name.as(:function_definition) >> params >> block
2014-04-24 14:43:20 +02:00
}
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
}
rule(:root){ function_definition.repeat(0) >> expression | expression | argument_list }
2014-04-24 14:43:20 +02:00
end
end