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-27 20:12:42 +02:00
|
|
|
require_relative "keywords"
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 14:44:34 +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-27 20:12:42 +02:00
|
|
|
include Keywords
|
2014-04-27 17:13:10 +02:00
|
|
|
|
2014-04-27 20:51:06 +02:00
|
|
|
# Note about the lists (argument and parameter)
|
|
|
|
# Beause of the maybe for the comma ... part , the rules may return either
|
|
|
|
# - a single simple version with one element
|
|
|
|
# a sequence with many element.
|
|
|
|
# This leads to the fact that two transform rules are needed for either
|
2014-04-27 20:12:42 +02:00
|
|
|
rule(:argument_list) {
|
2014-04-27 17:13:10 +02:00
|
|
|
left_parenthesis >>
|
2014-04-27 20:12:42 +02:00
|
|
|
((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
|
|
|
}
|
|
|
|
|
2014-04-27 20:12:42 +02:00
|
|
|
rule(:function_call) { name.as(:function_call) >> argument_list }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-28 21:07:13 +02:00
|
|
|
rule(:assignment) { name.as(:asignee) >> equal_sign >> expression.as(:asigned) }
|
2014-04-28 20:21:12 +02:00
|
|
|
|
2014-04-29 10:17:19 +02:00
|
|
|
rule(:expression) { conditional | function_call | integer | string | (name >> space? >> equal_sign.absent?) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-28 14:46:57 +02:00
|
|
|
def delimited_expressions( delimit )
|
2014-04-28 21:07:13 +02:00
|
|
|
( space? >> (delimit.absent? >> (assignment | expression)).repeat(1)).as(:expressions) >> delimit
|
2014-04-28 14:46:57 +02:00
|
|
|
end
|
|
|
|
|
2014-04-27 20:41:38 +02:00
|
|
|
rule(:conditional) {
|
|
|
|
keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >>
|
2014-04-28 14:46:57 +02:00
|
|
|
delimited_expressions(keyword_else).as(:if_true) >>
|
|
|
|
delimited_expressions(keyword_end).as(:if_false)
|
2014-04-24 14:43:20 +02:00
|
|
|
}
|
2014-04-28 20:21:12 +02:00
|
|
|
|
2014-04-28 14:46:57 +02:00
|
|
|
rule(:expressions_else) { delimited_expressions(keyword_else) }
|
|
|
|
rule(:expressions_end) { delimited_expressions(keyword_end) }
|
|
|
|
|
2014-04-27 20:12:42 +02:00
|
|
|
rule(:function_definition) {
|
2014-04-28 14:46:57 +02:00
|
|
|
keyword_def >> name.as(:function_definition) >> parmeter_list >> expressions_end
|
2014-04-24 14:43:20 +02:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:41:38 +02:00
|
|
|
rule(:parmeter_list) {
|
2014-04-27 17:13:10 +02:00
|
|
|
left_parenthesis >>
|
2014-04-27 20:41:38 +02:00
|
|
|
((name.as(:parmeter) >> (comma >> name.as(:parmeter)).repeat(0)).maybe).as(:parmeter_list) >>
|
2014-04-27 17:13:10 +02:00
|
|
|
right_parenthesis
|
2014-04-24 14:43:20 +02:00
|
|
|
}
|
2014-04-28 20:21:12 +02:00
|
|
|
rule(:root){ function_definition | expression | assignment }
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
end
|