class Parslet::Slice
delegation, we opt for a partial emulation that gets the job done.
These omissions are somewhat intentional. Rather than maintaining a full
calling #to_s.
are not yet in Slice. You can always extract the internal string instance by
however is not complete - many of the myriad of operations String supports
Parslet::Slice behaves in many ways like a Ruby String. This likeness
== Likeness to strings
slice.offset # => 12
slice.line_and_column # => [1, 13]
Example:
the original input where this slice starts.
Using the #line_and_column method, you can extract the line and column in
== Extracting line and column
original input).
any other string, except that it remembers where it came from (offset in
A slice is a small part from the parse input. A slice mainly behaves like
def +(other)
as the one of this slice.
where the first one ends. The offset of the resulting slice is the same
Concatenate two slices; it is assumed that the second slice begins
def +(other) self.class.new(@position, str + other.to_s, line_cache) end
def == other
Compares slices to other slices or strings.
def == other str == other end
def initialize(position, string, line_cache=nil)
The line cache should be able to answer to the #line_and_column message.
Construct a slice using a string, an offset and an optional line cache.
def initialize(position, string, line_cache=nil) @position = position @str = string @line_cache = line_cache end
def inspect
"string"@offset
.
def inspect str.inspect + "@#{offset}" end
def line_and_column
Returns a
def line_and_column raise ArgumentError, "No line cache was given, cannot infer line and column." \ unless line_cache line_cache.line_and_column(@position.bytepos) end
def match(regexp)
Match regular expressions.
def match(regexp) str.match(regexp) end
def offset
def offset @position.charpos end
def size
Returns the slices size in characters.
def size str.size end
def to_f
def to_f str.to_f end
def to_i
def to_i self.str.to_i end
def to_slice
def to_slice self end
def to_str
def to_str str end
def to_sym
def to_sym str.to_sym end