using hash_key and value rules

This commit is contained in:
Torsten Ruger 2014-05-12 21:44:51 +03:00
parent fdb5dd4f67
commit ff612446be
4 changed files with 5 additions and 6 deletions

View File

@ -10,8 +10,7 @@ module Parser
space? >> right_bracket space? >> right_bracket
end end
rule(:hash_pair) { basic_type.as(:hash_key) >> association >> (operator_expression|value_expression).as(:hash_value) }
rule(:hash_pair) { basic_type.as(:argument) >> association >> (operator_expression|value_expression).as(:element) }
rule(:hash) { left_brace >> ((hash_pair.as(:hash_pair) >> rule(:hash) { left_brace >> ((hash_pair.as(:hash_pair) >>
(comma >> space? >> hash_pair.as(:hash_pair)).repeat(0)).repeat(0,1)).as(:hash)>> (comma >> space? >> hash_pair.as(:hash_pair)).repeat(0)).repeat(0,1)).as(:hash)>>
space? >> right_brace } space? >> right_brace }

View File

@ -13,7 +13,7 @@ module Parser
rule(:array => sequence(:array) ) { Ast::ArrayExpression.new(array) } rule(:array => sequence(:array) ) { Ast::ArrayExpression.new(array) }
rule(:element => simple(:element)) { element } rule(:element => simple(:element)) { element }
rule(:hash => sequence(:hash) ) { Ast::HashExpression.new(hash) } 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(:hash_pair => simple(:hash_pair) ) { hash_pair }
rule(:argument => simple(:argument)) { argument } rule(:argument => simple(:argument)) { argument }

View File

@ -1,6 +1,6 @@
require_relative "test_basic" require_relative "test_basic"
require_relative "test_compund" require_relative "test_compound"
require_relative "test_arguments" require_relative "test_arguments"
require_relative "test_expressions" require_relative "test_expressions"
require_relative "test_function_call" require_relative "test_function_call"

View File

@ -29,14 +29,14 @@ class TestCompound < MiniTest::Test
def test_hash def test_hash
@string_input = '{ foo => 33 }' @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))]) @transform_output = Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new("foo") , Ast::IntegerExpression.new(33))])
@parser = @parser.hash @parser = @parser.hash
end end
def test_hash_list def test_hash_list
@string_input = "{foo => 33 , bar => 42}" @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))]) @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 @parser = @parser.hash
end end