From ff612446bed4e6f13957c2fa4339974738895c9f Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 12 May 2014 21:44:51 +0300 Subject: [PATCH] using hash_key and value rules --- lib/parser/compound_types.rb | 3 +-- lib/parser/transform.rb | 2 +- test/parser/test_all.rb | 2 +- test/parser/test_compound.rb | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/parser/compound_types.rb b/lib/parser/compound_types.rb index 82f08244..f09443da 100644 --- a/lib/parser/compound_types.rb +++ b/lib/parser/compound_types.rb @@ -10,8 +10,7 @@ module Parser space? >> right_bracket end - - rule(:hash_pair) { basic_type.as(:argument) >> association >> (operator_expression|value_expression).as(:element) } + rule(:hash_pair) { basic_type.as(:hash_key) >> association >> (operator_expression|value_expression).as(:hash_value) } rule(:hash) { left_brace >> ((hash_pair.as(:hash_pair) >> (comma >> space? >> hash_pair.as(:hash_pair)).repeat(0)).repeat(0,1)).as(:hash)>> space? >> right_brace } diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index 5b84c5d2..d7740a89 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -13,7 +13,7 @@ module Parser rule(:array => sequence(:array) ) { Ast::ArrayExpression.new(array) } rule(:element => simple(:element)) { element } rule(:hash => sequence(:hash) ) { Ast::HashExpression.new(hash) } - rule(:argument => simple(:argument) , :element => simple(:element)) { Ast::AssociationExpression.new(argument,element) } + rule(:hash_key => simple(:hash_key) , :hash_value => simple(:hash_value)) { Ast::AssociationExpression.new(hash_key,hash_value) } rule(:hash_pair => simple(:hash_pair) ) { hash_pair } rule(:argument => simple(:argument)) { argument } diff --git a/test/parser/test_all.rb b/test/parser/test_all.rb index 43d132b9..7c2aa29c 100644 --- a/test/parser/test_all.rb +++ b/test/parser/test_all.rb @@ -1,6 +1,6 @@ require_relative "test_basic" -require_relative "test_compund" +require_relative "test_compound" require_relative "test_arguments" require_relative "test_expressions" require_relative "test_function_call" diff --git a/test/parser/test_compound.rb b/test/parser/test_compound.rb index 437b93e3..c2b3d58e 100644 --- a/test/parser/test_compound.rb +++ b/test/parser/test_compound.rb @@ -29,14 +29,14 @@ class TestCompound < MiniTest::Test def test_hash @string_input = '{ foo => 33 }' - @parse_output = {:hash=>[{:hash_pair=>{:argument=>{:name=>"foo"}, :element=>{:integer=>"33"}}}]} + @parse_output = {:hash=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}]} @transform_output = Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new("foo") , Ast::IntegerExpression.new(33))]) @parser = @parser.hash end def test_hash_list @string_input = "{foo => 33 , bar => 42}" - @parse_output = {:hash=>[{:hash_pair=>{:argument=>{:name=>"foo"}, :element=>{:integer=>"33"}}}, {:hash_pair=>{:argument=>{:name=>"bar"}, :element=>{:integer=>"42"}}}]} + @parse_output = {:hash=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}, {:hash_pair=>{:hash_key=>{:name=>"bar"}, :hash_value=>{:integer=>"42"}}}]} @transform_output = 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))]) @parser = @parser.hash end