diff --git a/lib/parslet/atoms/base.rb b/lib/parslet/atoms/base.rb index a43dfdc..e18429a 100644 --- a/lib/parslet/atoms/base.rb +++ b/lib/parslet/atoms/base.rb @@ -51,7 +51,7 @@ def parse(io, options={}) # If we haven't consumed the input, then the pattern doesn't match. Try # to provide a good error message (even asking down below) - if !options[:prefix] && !source.eof? + if !options[:prefix] && source.chars_left > 0 old_pos = source.pos Parslet::Cause.format( source, old_pos, diff --git a/lib/parslet/atoms/re.rb b/lib/parslet/atoms/re.rb index 8e01c6e..87fe723 100644 --- a/lib/parslet/atoms/re.rb +++ b/lib/parslet/atoms/re.rb @@ -25,7 +25,7 @@ def try(source, context) # No string could be read return context.err(self, source, @error_msgs[:premature]) \ - if source.eof? + if source.chars_left < 1 # No match return context.err(self, source, @error_msgs[:failed]) diff --git a/lib/parslet/source.rb b/lib/parslet/source.rb index ade2fd7..3ec14a9 100644 --- a/lib/parslet/source.rb +++ b/lib/parslet/source.rb @@ -47,10 +47,6 @@ def chars_left @str.size - @pos end - def eof? - @pos >= @str.size - end - # Position of the parse as a character offset into the original string. # @note: Encodings... attr_accessor :pos diff --git a/spec/parslet/atoms/base_spec.rb b/spec/parslet/atoms/base_spec.rb index 3e4545a..7c8799d 100644 --- a/spec/parslet/atoms/base_spec.rb +++ b/spec/parslet/atoms/base_spec.rb @@ -55,7 +55,7 @@ def unnamed(obj) let(:source) { flexmock("source lookalike", :line_and_column => [1,2], :pos => 1, - :eof? => true) } + :chars_left => 0) } it "should not rewrap in a source" do flexmock(Parslet::Source). diff --git a/spec/parslet/source_spec.rb b/spec/parslet/source_spec.rb index 237f273..c50c11e 100644 --- a/spec/parslet/source_spec.rb +++ b/spec/parslet/source_spec.rb @@ -15,14 +15,14 @@ source.consume(100).should == 'a'*100 end end - describe "<- #eof?" do - subject { source.eof? } + describe "<- #chars_left" do + subject { source.chars_left } - it { should be_false } + it { should == 202 } context "after depleting the source" do before(:each) { source.consume(10000) } - it { should be_true } + it { should == 0 } end end describe "<- #pos" do @@ -98,7 +98,7 @@ attr_reader :results before(:each) { @results = {} - while not source.eof? + while source.chars_left>0 pos = source.pos @results[pos] = source.line_and_column source.consume(1) @@ -123,7 +123,7 @@ end it "should give the same results when reading" do cur = source.pos = 0 - while not source.eof? + while source.chars_left>0 source.line_and_column.should == results[cur] cur += 1 source.consume(1) @@ -148,7 +148,6 @@ source.consume(1) source.consume(2) - source.eof?.should == true source.chars_left.should == 0 end end