Skip to content

Commit

Permalink
+ Reduces API surface of Source
Browse files Browse the repository at this point in the history
Removes eof?
  • Loading branch information
kschiess committed May 29, 2012
1 parent 80de8ea commit 34364a7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/parslet/atoms/base.rb
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/parslet/atoms/re.rb
Expand Up @@ -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])
Expand Down
4 changes: 0 additions & 4 deletions lib/parslet/source.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/parslet/atoms/base_spec.rb
Expand Up @@ -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).
Expand Down
13 changes: 6 additions & 7 deletions spec/parslet/source_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -148,7 +148,6 @@
source.consume(1)

source.consume(2)
source.eof?.should == true
source.chars_left.should == 0
end
end
Expand Down

0 comments on commit 34364a7

Please sign in to comment.