2015-09-15 17:57:31 +02:00
|
|
|
|
2015-09-15 19:03:21 +02:00
|
|
|
#include is private in 1.9, who'd have known without travis
|
|
|
|
Parslet::Context.send :include , AST::Sexp
|
2014-06-04 18:55:04 +02:00
|
|
|
|
|
|
|
module Parser
|
|
|
|
class Transform < Parslet::Transform
|
2015-09-15 17:57:31 +02:00
|
|
|
|
|
|
|
rule(:string => sequence(:chars)) { s(:string , chars.join) }
|
2014-06-04 18:55:04 +02:00
|
|
|
rule(:esc => simple(:esc)) { '\\' + esc }
|
|
|
|
rule(char: simple(:char)) { char }
|
2014-06-30 16:51:07 +02:00
|
|
|
|
2015-09-15 17:57:31 +02:00
|
|
|
rule(:true => simple(:true)) { s(:true) }
|
|
|
|
rule(:false => simple(:false)) { s(:false) }
|
|
|
|
rule(:nil => simple(:nil)) { s(:nil) }
|
|
|
|
rule(:integer => simple(:value)) { s(:int ,value.to_i) }
|
2015-09-19 13:55:03 +02:00
|
|
|
rule(:name => simple(:name)) { s(:name , name.to_sym) }
|
2015-09-15 17:57:31 +02:00
|
|
|
rule(:type => simple(:type), :name => simple(:name)) { s(:field , type.to_sym , name.to_sym) }
|
2015-09-17 23:02:52 +02:00
|
|
|
|
2015-09-15 17:57:31 +02:00
|
|
|
rule(:module_name => simple(:module_name)) { s(:module,module_name.to_s) }
|
2014-06-04 18:55:04 +02:00
|
|
|
|
2015-09-19 13:55:03 +02:00
|
|
|
rule(:array_constant => sequence(:array_constant) ) { s(:array , *array_constant) }
|
2014-06-04 18:55:04 +02:00
|
|
|
rule(:array_element => simple(:array_element)) { array_element }
|
2015-09-19 13:55:03 +02:00
|
|
|
rule(:hash_constant => sequence(:hash_constant) ) { s(:hash , *hash_constant) }
|
2015-09-15 17:57:31 +02:00
|
|
|
rule(:hash_key => simple(:hash_key) , :hash_value => simple(:hash_value)) { s(:assoc , hash_key , hash_value) }
|
2014-06-04 18:55:04 +02:00
|
|
|
rule(:hash_pair => simple(:hash_pair) ) { hash_pair }
|
|
|
|
|
|
|
|
rule(:argument => simple(:argument)) { argument }
|
|
|
|
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
|
|
|
|
|
|
|
#Two rules for calls, simple and qualified. Keeps the rules simpler
|
2015-09-15 17:57:31 +02:00
|
|
|
rule( :call_site => simple(:call_site),
|
2014-06-04 18:55:04 +02:00
|
|
|
:argument_list => sequence(:argument_list)) do
|
2015-09-18 21:04:08 +02:00
|
|
|
s(:call , call_site, s(:arguments , *argument_list) )
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
2015-09-15 17:57:31 +02:00
|
|
|
rule( :receiver => simple(:receiver) , :call_site => simple(:call_site),
|
2014-06-04 18:55:04 +02:00
|
|
|
:argument_list => sequence(:argument_list)) do
|
2015-09-18 21:04:08 +02:00
|
|
|
s(:call , call_site, s(:arguments , *argument_list) , s(:receiver , receiver))
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
rule(:if => simple(:if), :conditional => simple(:conditional),
|
|
|
|
:if_true => {:expressions => sequence(:if_true) , :else => simple(:else) },
|
|
|
|
:if_false => {:expressions => sequence(:if_false) , :end => simple(:e) }) do
|
2015-09-19 16:11:15 +02:00
|
|
|
s(:if , s(:condition, conditional), s(:if_true, *if_true), s(:if_false , *if_false))
|
2015-09-15 17:57:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
rule(:if => simple(:if), :conditional => simple(:conditional),
|
|
|
|
:if_true => {:expressions => sequence(:if_true) , :end => simple(:e) }) do
|
2015-09-18 21:04:08 +02:00
|
|
|
s(:if , s(:condition, conditional), s(:if_true, *if_true), s(:if_false , nil) )
|
2015-09-15 17:57:31 +02:00
|
|
|
end
|
2014-06-04 18:55:04 +02:00
|
|
|
|
|
|
|
rule(:while => simple(:while),
|
2015-09-15 17:57:31 +02:00
|
|
|
:while_cond => simple(:while_cond) ,
|
2014-06-04 18:55:04 +02:00
|
|
|
:body => {:expressions => sequence(:body) , :end => simple(:e) }) do
|
2015-09-18 21:04:08 +02:00
|
|
|
s(:while , s(:condition , while_cond), s(:expressions , *body))
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
rule(:return => simple(:return) , :return_expression => simple(:return_expression))do
|
2015-09-15 17:57:31 +02:00
|
|
|
s(:return , return_expression)
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
|
|
|
|
2014-06-19 18:33:08 +02:00
|
|
|
rule(:parameter => simple(:parameter)) { parameter }
|
2014-06-04 18:55:04 +02:00
|
|
|
|
|
|
|
# Also two rules for function definitions, unqualified and qualified
|
2015-09-17 23:02:52 +02:00
|
|
|
rule(:type => simple(:type) ,
|
|
|
|
:function_name => simple(:function_name),
|
2014-06-19 18:33:08 +02:00
|
|
|
:parameter_list => sequence(:parameter_list),
|
2014-06-04 18:55:04 +02:00
|
|
|
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
2015-09-18 21:04:08 +02:00
|
|
|
s(:function, type.to_sym , function_name, s(:parameters , *parameter_list ),
|
|
|
|
s(:expressions , *expressions))
|
2014-06-26 11:46:34 +02:00
|
|
|
end
|
|
|
|
|
2015-09-17 23:02:52 +02:00
|
|
|
rule(:type => simple(:type) , :receiver=> simple(:receiver),
|
2014-06-04 18:55:04 +02:00
|
|
|
:function_name => simple(:function_name),
|
2015-09-18 21:04:08 +02:00
|
|
|
:parameter_list => simple(:parameter_list),
|
2014-06-04 18:55:04 +02:00
|
|
|
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
2015-09-18 21:04:08 +02:00
|
|
|
s(:function, type.to_sym , function_name, s(:parameters , *parameter_list ),
|
|
|
|
s(:expressions , *expressions) , receiver)
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
|
|
|
|
2015-09-15 17:57:31 +02:00
|
|
|
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
|
2014-06-24 18:33:21 +02:00
|
|
|
op = o.to_s.strip
|
|
|
|
if op == "="
|
2015-09-15 17:57:31 +02:00
|
|
|
s(:assign , l ,r)
|
2014-06-24 18:33:21 +02:00
|
|
|
else
|
2015-09-15 17:57:31 +02:00
|
|
|
s(:operator, op , l ,r)
|
2014-06-24 18:33:21 +02:00
|
|
|
end
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
2015-09-15 17:57:31 +02:00
|
|
|
|
2014-06-04 18:55:04 +02:00
|
|
|
#modules and classes are understandibly quite similar Class < Module
|
|
|
|
rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
|
2015-09-19 13:55:03 +02:00
|
|
|
s(:module , module_name.to_s.to_sym , *module_expressions)
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
2014-06-19 18:16:54 +02:00
|
|
|
rule( :module_name => simple(:module_name) , :derived_name => simple(:derived_name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
|
2015-09-19 13:55:03 +02:00
|
|
|
s(:class , module_name.to_s.to_sym ,
|
|
|
|
s(:derives, derived_name ? derived_name.to_a.first.to_sym : nil) , *class_expressions)
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
2015-09-15 17:57:31 +02:00
|
|
|
|
2014-06-25 21:45:09 +02:00
|
|
|
rule(:expression_list => sequence(:expression_list)) {
|
2015-09-19 13:55:03 +02:00
|
|
|
s(:expressions , *expression_list)
|
2014-06-25 21:45:09 +02:00
|
|
|
}
|
2015-09-19 13:55:03 +02:00
|
|
|
|
2014-06-04 18:55:04 +02:00
|
|
|
#shortcut to get the ast tree for a given string
|
2015-09-15 17:57:31 +02:00
|
|
|
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
2014-06-04 18:55:04 +02:00
|
|
|
def self.ast string , rule = :root
|
|
|
|
syntax = Parser.new.send(rule).parse(string)
|
|
|
|
tree = Transform.new.apply(syntax)
|
|
|
|
tree
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|