2014-06-04 18:55:04 +02:00
|
|
|
require 'parslet'
|
2015-09-15 17:57:31 +02:00
|
|
|
require "ast"
|
|
|
|
|
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) }
|
|
|
|
rule(:name => simple(:name)) { s(:name , name.to_s) }
|
|
|
|
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-15 17:57:31 +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-15 17:57:31 +02:00
|
|
|
rule(:hash_constant => sequence(:hash_constant) ) { s(:hash , hash_constant) }
|
|
|
|
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-15 17:57:31 +02:00
|
|
|
s(:call , call_site, 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-15 17:57:31 +02:00
|
|
|
s(:call , call_site, argument_list , 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-15 17:57:31 +02:00
|
|
|
s(:if , conditional, if_true, if_false)
|
|
|
|
end
|
|
|
|
|
|
|
|
rule(:if => simple(:if), :conditional => simple(:conditional),
|
|
|
|
:if_true => {:expressions => sequence(:if_true) , :end => simple(:e) }) do
|
|
|
|
s(:if , conditional, if_true, nil)
|
|
|
|
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-15 17:57:31 +02:00
|
|
|
s(:while , while_cond, 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 }
|
|
|
|
rule(:parameter_list => sequence(:parameter_list)) { parameter_list }
|
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-17 23:02:52 +02:00
|
|
|
s(:function, type.to_sym , function_name, parameter_list, expressions)
|
2014-06-04 18:55:04 +02:00
|
|
|
end
|
|
|
|
|
2015-09-17 23:02:52 +02:00
|
|
|
rule(:type => simple(:type) , :function_name => simple(:function_name),
|
2014-06-26 11:46:34 +02:00
|
|
|
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
2015-09-17 23:02:52 +02:00
|
|
|
s(:function , type.to_sym, function_name, [], 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),
|
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-17 23:02:52 +02:00
|
|
|
s(:function, type.to_sym , function_name, parameter_list, 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-15 17:57:31 +02:00
|
|
|
s(:module , module_name , 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-15 17:57:31 +02:00
|
|
|
s(:class , module_name , derived_name ? derived_name : 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-15 17:57:31 +02:00
|
|
|
s(:list , expression_list)
|
2014-06-25 21:45:09 +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
|