Permalink
Browse files

! Fixes the missing recursive flatten

  • Loading branch information...
1 parent a96cf6a commit af98a8c3a92093c42a49d546ecccb43f968b8dd5 @kschiess committed Mar 16, 2010
Showing with 7 additions and 4 deletions.
  1. +1 −1 example/simple.lit
  2. +5 −3 lib/parslet.rb
  3. +1 −0 spec/parslet_spec.rb
View
@@ -1,3 +1,3 @@
123
12345
-" "
+" Some String with \"escapes\""
View
@@ -56,7 +56,9 @@ def flatten(value)
return value unless value.instance_of? Array
# Merges arrays:
- value.inject('') { |r, e|
+ value.
+ map { |e| flatten(e) }. # first flatten each element
+ inject('') { |r, e| # and then merge flat elements
case [r, e].map { |o| o.class }
when [Hash, Hash]
warn_about_duplicate_keys(r, e)
@@ -79,7 +81,7 @@ def error(position, str)
def warn_about_duplicate_keys(h1, h2)
d = h1.keys & h2.keys
unless d.empty?
- warn "Duplicate subtrees while merging result of #{self.inspect}, only the values"+
+ warn "Duplicate subtrees while merging result of \n #{self.inspect}\nonly the values"+
" of the latter will be kept. (keys: #{d.inspect})"
end
end
@@ -102,7 +104,7 @@ def inspect
end
private
- def produce_return_value(val)
+ def produce_return_value(val)
{ name => flatten(val) }
end
end
@@ -266,6 +266,7 @@ def call(val)
[ [{:a => :b}, {:a=>:d}], {:a => :d} ],
[ [{:a=>:b}, [["\n", nil]]], {:a=>:b} ],
[ [nil, " "], ' ' ],
+ [ [[nil, " "]], ' ' ],
].each do |input, output|
it "should #{input.inspect} to #{output.inspect}" do
call(input).should == output

0 comments on commit af98a8c

Please sign in to comment.