简历(我把下面的长篇故事缩小到简单的问题)
tree = {:properties => [{:a => 'b'}, {:c => 'd'}]}
big_tree = {:properties => [{:a => 'b'}, {:c => 'd'}], :moves => [{:a => 'b'}, {:c => 'd'}]}
trans = Parslet::Transform.new do
rule(:properties => subtree(:nested)) do
out = {}
nested.each {|pair| out = out.merge pair}
{:properties => out}
end
end
pp tree
pp trans.apply(tree)
pp big_tree
pp trans.apply(big_tree)
# OUTPUT
{:properties=>[{:a=>"b"}, {:c=>"d"}]}
{:properties=>{:a=>"b", :c=>"d"}} # Worked with small tree
{:properties=>[{:a=>"b"}, {:c=>"d"}], :moves=>[{:a=>"b"}, {:c=>"d"}]}
{:properties=>[{:a=>"b"}, {:c=>"d"}], :moves=>[{:a=>"b"}, {:c=>"d"}]} # Didn't work with bigger tree
==========================FULL STORY(在标题之后不太相关)
我正在使用 Parslet 制作 SGF 文件解析器。
现在我正处于制作变形金刚的阶段。
从解析器我已经得到了这样的结构:
[{:properties=>
[{:name=>"GM"@2, :values=>[{:value=>"1"@5}]},
{:name=>"FF"@7, :values=>[{:value=>"4"@10}]},
{:name=>"SZ"@12, :values=>[{:value=>"19"@15}]},
{:name=>"AP"@18, :values=>[{:value=>"SmartGo Kifu:2.2"@21}]},
{:name=>"GN"@40, :values=>[{:value=>"2013-05-11g"@43}]},
{:name=>"PW"@57, :values=>[{:value=>"Dahan"@60}]},
{:name=>"PB"@68, :values=>[{:value=>"SmartGo"@71}]},
{:name=>"DT"@81, :values=>[{:value=>"2013-05-11"@84}]},
{:name=>"KM"@97, :values=>[{:value=>"6.5"@100}]},
{:name=>"RE"@106, :values=>[{:value=>"W+R"@109}]},
{:name=>"RU"@115, :values=>[{:value=>"AGA (Area)"@118}]},
{:name=>"ID"@129, :values=>[{:value=>"ch0"@132}]}],
:moves=>
[{:player=>"B"@137, :place=>"oq"@139},
{:player=>"W"@143, :place=>"dd"@145},
{:player=>"B"@149, :place=>"oo"@151},
...etc...
我用来转换的规则集:
# Rewrite player: COLOR, place: X to COLOR: X
rule( player: simple(:p), place: simple(:pl)) do
if p == 'W'
{ white: pl }
elsif p == 'B'
{ black: pl }
end
end
# Un-nest single-value hash
rule( value: simple(:v)) { v }
# Rewrite name: KEY, values: SINGLE_VALUE to KEY: SINGLE_VALUE
rule( name: simple(:n), values: [ simple(:v) ]) { {n.to_sym => v} }
# A Problem!!!
rule( properties: subtree(:props) ) do
out = {}
props.each {|pair| pair.each {|k, v| out[k] = v}}
{ properties: out }
end
有了这样的规则,我得到以下结构:
[{:properties=>
[{:GM=>"1"@5},
{:FF=>"4"@10},
{:SZ=>"19"@15},
{:AP=>"SmartGo Kifu:2.2"@21},
{:GN=>"2013-05-11g"@43},
{:PW=>"Dahan"@60},
{:PB=>"SmartGo"@71},
{:DT=>"2013-05-11"@84},
{:KM=>"6.5"@100},
{:RE=>"W+R"@109},
{:RU=>"AGA (Area)"@118},
{:ID=>"ch0"@132}],
:moves=>
[{:black=>"oq"@139},
{:white=>"dd"@145},
{:black=>"oo"@151},
...etc...
一切都很完美。我唯一的问题是 :properties 哈希数组。
最后我想拥有
[{:properties=>
{:GM=>"1"@5,
:FF=>"4"@10,
:SZ=>"19"@15,
:AP=>"SmartGo Kifu:2.2"@21,
:GN=>"2013-05-11g"@43,
:PW=>"Dahan"@60,
:PB=>"SmartGo"@71,
:DT=>"2013-05-11"@84,
:KM=>"6.5"@100,
:RE=>"W+R"@109,
:RU=>"AGA (Area)"@118,
:ID=>"ch0"@132},
:moves=>
[{:black=>"oq"@139},
{:white=>"dd"@145},
{:black=>"oo"@151},
...etc...
你看?合并 :properties 中的所有数组哈希,因为在之前的转换之后,它们现在具有唯一键。还将结构展平一点。
嘿!我可以手动完成。我的意思是运行一个单独的方法,比如
merged_stuff = {}
tree.first[:properties].each {|pair| pair.each {|k, v| merged_stuff[k] = v}}
tree.first[:properties] = merged_stuff
但是为什么我不能用整洁的转换规则来做到这一点,把所有的转换逻辑放在一个地方?
关键是rule( properties: subtree(:props) )
根本不会被解雇。即使我只是从块中返回 nil,它也不会改变任何东西。所以,看来,这subtree
并没有抓住事情,或者我没有。