Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

. Slices returned from Source should now be serializable

This fixes issue #45. We didn't need the extra info, another case of not thinking about interfaces
first...
  • Loading branch information...
commit f58ae38f983b1d15f93d15768cc89d7102ad2a8e 1 parent a4367af
@kschiess authored
View
41 lib/parslet/slice.rb
@@ -3,16 +3,6 @@
# any other string, except that it remembers where it came from (offset in
# original input).
#
-# Some slices also know what parent slice they are a small part of. This
-# allows the slice to be concatenated to other slices from the same buffer by
-# reslicing it against that original buffer.
-#
-# Why the complexity? Slices allow retaining offset information. This will
-# allow to assign line and column to each small bit of output from the parslet
-# parser. Also, while we keep that information, we might as well try to do
-# something useful with it. Reslicing the same buffers should in theory keep
-# buffer copies and allocations down.
-#
# == Extracting line and column
#
# Using the #line_and_column method, you can extract the line and column in
@@ -32,27 +22,16 @@
# These omissions are somewhat intentional. Rather than maintaining a full
# delegation, we opt for a partial emulation that gets the job done.
#
-# Note also that there are some things that work with strings that will never
-# work when using slices. For instance, you cannot concatenate slices that
-# aren't from the same source or that don't join up:
-#
-# Example:
-# big_slice = 'abcdef'
-# a = big_slice.slice(0, 2) # => "ab"@0
-# b = big_slice.slice(4, 2) # => "ef"@4
-#
-# a + b # raises Parslet::InvalidSliceOperation
-#
-# This avoids creating slices with impossible offsets or that are
-# discontinous.
-#
class Parslet::Slice
attr_reader :str, :offset
- attr_reader :source
+ attr_reader :line_cache
- def initialize(string, offset, source=nil)
+ # Construct a slice using a string, an offset and an optional line cache.
+ # The line cache should be able to answer to the #line_and_column message.
+ #
+ def initialize(string, offset, line_cache=nil)
@str, @offset = string, offset
- @source = source
+ @line_cache = line_cache
end
# Compares slices to other slices or strings.
@@ -78,16 +57,16 @@ def size
# as the one of this slice.
#
def +(other)
- self.class.new(str + other.to_s, offset, source)
+ self.class.new(str + other.to_s, offset, line_cache)
end
# Returns a <line, column> tuple referring to the original input.
#
def line_and_column
- raise ArgumentError, "No source was given, cannot infer line and column." \
- unless source
+ raise ArgumentError, "No line cache was given, cannot infer line and column." \
+ unless line_cache
- source.line_and_column(self.offset)
+ line_cache.line_and_column(self.offset)
end
View
4 lib/parslet/source.rb
@@ -51,7 +51,7 @@ def read_slice(needed)
# cache line ends
@line_cache.scan_for_line_endings(start, buf)
- Parslet::Slice.new(buf || '', start, self)
+ Parslet::Slice.new(buf || '', start, @line_cache)
end
if RUBY_VERSION !~ /^1.9/
@@ -62,7 +62,7 @@ def read_slice(needed)
# cache line ends
@line_cache.scan_for_line_endings(start, buf)
- Parslet::Slice.new(buf || '', start, self)
+ Parslet::Slice.new(buf || '', start, @line_cache)
end
end
end
View
21 spec/parslet/slice_spec.rb
@@ -46,13 +46,9 @@
end
context "when constructed with a source" do
- before(:each) {
- flexmock(slice, :source => flexmock(:source).
- tap { |sm| sm.
- should_receive(:line_and_column).
- with(40).
- and_return([13, 14]) })
- }
+ let(:slice) { described_class.new(
+ 'foobar', 40,
+ flexmock(:cache, :line_and_column => [13, 14])) }
it "should return proper line and column" do
slice.line_and_column.should == [13, 14]
end
@@ -123,5 +119,16 @@
it { should == 'foobar' }
end
end
+ describe "serializability" do
+ it "should serialize" do
+ Marshal.dump(slice)
+ end
+ context "when storing a line cache" do
+ let(:slice) { described_class.new('foobar', 40, Parslet::Source::LineCache.new()) }
+ it "should serialize" do
+ Marshal.dump(slice)
+ end
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.