add instance variables and module name as basic types (as name was there already) and add tests
This commit is contained in:
parent
497211f027
commit
3156887a81
@ -28,7 +28,7 @@ module Parser
|
||||
# TODO rule forbit names like if_true, because it starts with a keyword. a little looser please!
|
||||
rule(:name) { keyword.absent? >> (match['a-z_'] >> match['a-zA-Z0-9_'].repeat).as(:name) >> space? }
|
||||
# instance variables must have the @
|
||||
rule(:instance_variable) { (match('@') >> name).as(:instance_variable) }
|
||||
rule(:instance_variable) { (str('@') >> name).as(:instance_variable) }
|
||||
# and class/module names must start with capital
|
||||
# (admittatly the rule matches constants too, but one step at a time)
|
||||
rule(:module_name) { keyword.absent? >> (match['A-Z'] >> match['a-zA-Z0-9_'].repeat).as(:module_name) >> space? }
|
||||
@ -43,6 +43,6 @@ module Parser
|
||||
|
||||
rule(:float) { integer >> dot >> integer >>
|
||||
(exponent >> sign.maybe >> digit.repeat(1,3)).maybe >> space?}
|
||||
rule(:basic_type){ integer | name | string | float }
|
||||
rule(:basic_type){ integer | name | string | float | instance_variable | module_name }
|
||||
end
|
||||
end
|
@ -19,13 +19,13 @@ HERE
|
||||
@string_input = <<HERE
|
||||
class Opers
|
||||
def foo(x)
|
||||
abba = 5
|
||||
@abba = 5
|
||||
2 + 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Opers", :class_expressions=>[{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] )
|
||||
@parse_output = {:module_name=>"Opers", :class_expressions=>[{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::VariableExpression.new("abba"),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] )
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
|
||||
|
@ -19,7 +19,7 @@ HERE
|
||||
@string_input = <<HERE
|
||||
module Opers
|
||||
def foo(x)
|
||||
abba = 5
|
||||
abba = 5
|
||||
2 + 5
|
||||
end
|
||||
end
|
||||
@ -29,6 +29,17 @@ HERE
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_assign_instance
|
||||
@string_input = <<HERE
|
||||
module Opers
|
||||
@abba = 5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Opers", :module_expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Opers ,[Ast::OperatorExpression.new("=", Ast::VariableExpression.new("abba"),Ast::IntegerExpression.new(5))] )
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_if
|
||||
@string_input = <<HERE
|
||||
module Foo
|
||||
|
@ -40,6 +40,12 @@ class TestExpressions < MiniTest::Test
|
||||
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_op_instance_variable
|
||||
@string_input = "@a - 5"
|
||||
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"- ", :r=>{:integer=>"5"}}
|
||||
@transform_output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new("a"),Ast::IntegerExpression.new(5))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_op_variable_string
|
||||
@string_input = 'a - "st"'
|
||||
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}
|
||||
@ -70,5 +76,11 @@ class TestExpressions < MiniTest::Test
|
||||
@transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_assignment_instance
|
||||
@string_input = "@a = 5"
|
||||
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"= ", :r=>{:integer=>"5"}}
|
||||
@transform_output = Ast::OperatorExpression.new("=", Ast::VariableExpression.new("a"),Ast::IntegerExpression.new(5))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user