From 3156887a8100736751331d726c0e3303b8151a49 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 30 May 2014 14:25:08 +0300 Subject: [PATCH] add instance variables and module name as basic types (as name was there already) and add tests --- lib/parser/basic_types.rb | 4 ++-- test/parser/test_class.rb | 6 +++--- test/parser/test_module.rb | 13 ++++++++++++- test/parser/test_operators.rb | 12 ++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/parser/basic_types.rb b/lib/parser/basic_types.rb index 6dfae4a5..1b83dfb7 100644 --- a/lib/parser/basic_types.rb +++ b/lib/parser/basic_types.rb @@ -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 \ No newline at end of file diff --git a/test/parser/test_class.rb b/test/parser/test_class.rb index d8a9365f..5d3f65ab 100644 --- a/test/parser/test_class.rb +++ b/test/parser/test_class.rb @@ -19,13 +19,13 @@ HERE @string_input = <"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 diff --git a/test/parser/test_module.rb b/test/parser/test_module.rb index 82e8acb6..b19ab9a9 100644 --- a/test/parser/test_module.rb +++ b/test/parser/test_module.rb @@ -19,7 +19,7 @@ HERE @string_input = <"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 = <{: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 \ No newline at end of file