From dcb4dd33c0ec423d6bf682027dbd7894dd654de4 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 26 Jun 2014 12:26:34 +0300 Subject: [PATCH] roots hash and array constants --- lib/parser/crystal.rb | 3 ++- test/roots/test_compound.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/roots/test_compound.rb diff --git a/lib/parser/crystal.rb b/lib/parser/crystal.rb index a625723..4d4decf 100644 --- a/lib/parser/crystal.rb +++ b/lib/parser/crystal.rb @@ -31,7 +31,8 @@ module Parser include Operators include ModuleDef - rule(:root_body) {(module_definition | class_definition | function_definition | expression | call_site | basic_type )} + rule(:root_body) {(module_definition | class_definition | function_definition | expression | call_site | + basic_type | hash_constant | array_constant )} rule(:root) { root_body.repeat.as(:expression_list) } end end diff --git a/test/roots/test_compound.rb b/test/roots/test_compound.rb new file mode 100644 index 0000000..1244a19 --- /dev/null +++ b/test/roots/test_compound.rb @@ -0,0 +1,37 @@ +require_relative "../parser_helper" + +class TestCompound < MiniTest::Test + # include the magic (setup and parse -> test method translation), see there + include ParserHelper + + def test_one_array + @string_input = '[42]' + @parse_output = {:expression_list=>[{:array_constant=>[{:array_element=>{:integer=>"42"}}]}]} + @transform_output = Ast::ExpressionList.new( [Ast::ArrayExpression.new([Ast::IntegerExpression.new(42)])]) + end + + def test_array_list + @string_input = '[42, foo]' + @parse_output = {:expression_list=>[{:array_constant=>[{:array_element=>{:integer=>"42"}}, {:array_element=>{:name=>"foo"}}]}]} + @transform_output = Ast::ExpressionList.new( [Ast::ArrayExpression.new([Ast::IntegerExpression.new(42), Ast::NameExpression.new(:foo)])]) + end + + def test_array_ops + @string_input = '[ 3 + 4 , foo(22) ]' + @parse_output = {:expression_list=>[{:array_constant=>[{:array_element=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}, {:array_element=>{:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"22"}}]}}]}]} + @transform_output = Ast::ExpressionList.new( [Ast::ArrayExpression.new([Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)), Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(22)] ,Ast::NameExpression.new(:self))])]) + end + + def test_hash + @string_input = '{ foo => 33 }' + @parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}]}]} + @transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::IntegerExpression.new(33))])]) + end + + def test_hash_list + @string_input = "{foo => 33 , bar => 42}" + @parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}, {:hash_pair=>{:hash_key=>{:name=>"bar"}, :hash_value=>{:integer=>"42"}}}]}]} + @transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::IntegerExpression.new(33)), Ast::AssociationExpression.new(Ast::NameExpression.new(:bar) , Ast::IntegerExpression.new(42))])]) + end + +end \ No newline at end of file