2014-04-24 14:43:20 +02:00
|
|
|
require 'parslet'
|
2014-05-05 08:51:16 +02:00
|
|
|
require 'ast/expression'
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 14:44:34 +02:00
|
|
|
module Parser
|
2014-04-24 14:43:20 +02:00
|
|
|
class Transform < Parslet::Transform
|
2014-05-09 12:51:47 +02:00
|
|
|
rule(:string => sequence(:chars)) { Ast::StringExpression.new chars.join }
|
|
|
|
rule(:esc => simple(:esc)) { '\\' + esc }
|
|
|
|
rule(char: simple(:char)) { char }
|
|
|
|
|
2014-05-05 08:51:16 +02:00
|
|
|
rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
|
|
|
|
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-05-27 14:41:34 +02:00
|
|
|
rule(:array_constant => sequence(:array_constant) ) { Ast::ArrayExpression.new(array_constant) }
|
2014-05-13 07:24:55 +02:00
|
|
|
rule(:array_element => simple(:array_element)) { array_element }
|
2014-05-27 14:41:34 +02:00
|
|
|
rule(:hash_constant => sequence(:hash_constant) ) { Ast::HashExpression.new(hash_constant) }
|
2014-05-12 20:44:51 +02:00
|
|
|
rule(:hash_key => simple(:hash_key) , :hash_value => simple(:hash_value)) { Ast::AssociationExpression.new(hash_key,hash_value) }
|
2014-05-12 20:36:38 +02:00
|
|
|
rule(:hash_pair => simple(:hash_pair) ) { hash_pair }
|
|
|
|
|
2014-04-27 20:12:42 +02:00
|
|
|
rule(:argument => simple(:argument)) { argument }
|
|
|
|
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-05-13 20:15:02 +02:00
|
|
|
rule( :call_site => simple(:call_site),
|
2014-04-27 20:41:38 +02:00
|
|
|
:argument_list => sequence(:argument_list)) do
|
2014-05-13 20:15:02 +02:00
|
|
|
Ast::CallSiteExpression.new(call_site.name, argument_list)
|
2014-04-27 20:12:42 +02:00
|
|
|
end
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-05-11 17:37:26 +02:00
|
|
|
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
|
2014-05-24 09:18:54 +02:00
|
|
|
Ast::IfExpression.new(conditional, if_true, if_false)
|
2014-04-28 15:07:34 +02:00
|
|
|
end
|
2014-04-27 20:41:38 +02:00
|
|
|
|
2014-05-12 11:14:04 +02:00
|
|
|
rule(:while => simple(:while),
|
2014-05-12 11:26:38 +02:00
|
|
|
:while_cond => simple(:while_cond) , :do => simple(:do) ,
|
2014-05-11 17:37:26 +02:00
|
|
|
:body => {:expressions => sequence(:body) , :end => simple(:e) }) do
|
2014-05-10 18:02:51 +02:00
|
|
|
Ast::WhileExpression.new(while_cond, body)
|
|
|
|
end
|
|
|
|
|
2014-05-27 15:33:24 +02:00
|
|
|
rule(:return => simple(:return) , :return_expression => simple(:return_expression))do
|
|
|
|
Ast::ReturnExpression.new(return_expression)
|
|
|
|
end
|
|
|
|
|
2014-04-27 20:41:38 +02:00
|
|
|
rule(:parmeter => simple(:parmeter)) { parmeter }
|
|
|
|
rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-05-12 12:47:47 +02:00
|
|
|
rule(:function_name => simple(:function_name),
|
2014-04-27 20:41:38 +02:00
|
|
|
:parmeter_list => sequence(:parmeter_list),
|
2014-05-11 17:37:26 +02:00
|
|
|
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
2014-05-12 12:47:47 +02:00
|
|
|
Ast::FunctionExpression.new(function_name.name, parmeter_list, expressions)
|
2014-04-27 20:12:42 +02:00
|
|
|
end
|
2014-04-27 15:30:32 +02:00
|
|
|
|
2014-05-10 20:41:46 +02:00
|
|
|
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
|
|
|
|
Ast::OperatorExpression.new( o.to_s.strip , l ,r)
|
|
|
|
end
|
|
|
|
|
2014-04-27 15:30:32 +02:00
|
|
|
#shortcut to get the ast tree for a given string
|
|
|
|
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
|
|
|
def self.ast string , rule = :root
|
|
|
|
syntax = Parser.new.send(rule).parse(string)
|
|
|
|
tree = Transform.new.apply(syntax)
|
|
|
|
tree
|
|
|
|
end
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
end
|