From 33c29ddb7df166300441592a74ec88a07f645f28 Mon Sep 17 00:00:00 2001 From: Kaspar Schiess Date: Wed, 9 Feb 2011 18:49:24 +0100 Subject: [PATCH] + Fixes atoms_spec (and thus most of parslet) --- lib/parslet/atoms/base.rb | 9 ++++++--- lib/parslet/atoms/str.rb | 2 +- spec/parslet/atoms_spec.rb | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/parslet/atoms/base.rb b/lib/parslet/atoms/base.rb index 5e1e983..81412b5 100644 --- a/lib/parslet/atoms/base.rb +++ b/lib/parslet/atoms/base.rb @@ -194,10 +194,13 @@ def flatten(value, named=false) # :nodoc: end def flatten_sequence(list) # :nodoc: - list.compact.inject('') { |r, e| # and then merge flat elements + foldl(list.compact) { |r, e| # and then merge flat elements merge_fold(r, e) } end + def foldl(list, &block) + list[1..-1].inject(list.first, &block) + end def merge_fold(l, r) # :nodoc: # equal pairs: merge. if l.class == r.class @@ -212,8 +215,8 @@ def merge_fold(l, r) # :nodoc: # unequal pairs: hoist to same level. # special case: If one of them is a string, the other is more important - return l if r.class == String - return r if l.class == String + return l if r.respond_to? :to_str + return r if l.respond_to? :to_str # otherwise just create an array for one of them to live in return l + [r] if r.class == Hash diff --git a/lib/parslet/atoms/str.rb b/lib/parslet/atoms/str.rb index b57da6d..e45c118 100644 --- a/lib/parslet/atoms/str.rb +++ b/lib/parslet/atoms/str.rb @@ -26,7 +26,7 @@ def try(source, context) # :nodoc: # Failures: return error(source, @error_msgs[:premature]) unless s && s.size==str.size - return error(source, @error_msgs[:failed]+s.inspect, error_pos) + return error(source, @error_msgs[:failed]+s.str.inspect, error_pos) end def to_s_inner(prec) # :nodoc: diff --git a/spec/parslet/atoms_spec.rb b/spec/parslet/atoms_spec.rb index 4f71414..aa2b4ee 100644 --- a/spec/parslet/atoms_spec.rb +++ b/spec/parslet/atoms_spec.rb @@ -327,7 +327,7 @@ def src(str); Parslet::Source.new str; end end context "(str('a') >> str('ignore') >> str('b')) (no .as(...))" do - it "should return an empty subtree" do + it "should return simply the original string" do (str('a') >> str('ignore') >> str('b')). parse('aignoreb').should == 'aignoreb' end