Class: Parslet::Source
- Inherits:
-
Object
- Object
- Parslet::Source
- Defined in:
- lib/parslet/source.rb,
lib/parslet/source/line_cache.rb
Overview
Wraps the input string for parslet.
Defined Under Namespace
Modules: RangeSearch Classes: LineCache
Instance Attribute Summary collapse
-
#pos ⇒ Object
Position of the parse as a character offset into the original string.
Instance Method Summary collapse
-
#chars_left ⇒ Object
Returns how many chars remain in the input.
-
#consume(n) ⇒ Object
Consumes n characters from the input, returning them as a slice of the input.
-
#initialize(str) ⇒ Source
constructor
A new instance of Source.
-
#line_and_column(position = nil) ⇒ Object
Returns a <line, column> tuple for the given position.
-
#matches?(pattern) ⇒ Boolean
(also: #match)
Checks if the given pattern matches at the current input position.
Constructor Details
Instance Attribute Details
#pos ⇒ Object
Position of the parse as a character offset into the original string. @note: Encodingsā¦
52 53 54 |
# File 'lib/parslet/source.rb', line 52 def pos @pos end |
Instance Method Details
#chars_left ⇒ Object
Returns how many chars remain in the input.
46 47 48 |
# File 'lib/parslet/source.rb', line 46 def chars_left @str.size - @pos end |
#consume(n) ⇒ Object
Consumes n characters from the input, returning them as a slice of the input.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/parslet/source.rb', line 33 def consume(n) slice_str = @str.slice(@pos, n) slice = Parslet::Slice.new( slice_str, pos, @line_cache) @pos += slice_str.size return slice end |
#line_and_column(position = nil) ⇒ Object
Returns a <line, column> tuple for the given position. If no position is given, line/column information is returned for the current position given by #pos.
58 59 60 |
# File 'lib/parslet/source.rb', line 58 def line_and_column(position=nil) @line_cache.line_and_column(position || self.pos) end |
#matches?(pattern) ⇒ Boolean Also known as: match
Checks if the given pattern matches at the current input position.
25 26 27 |
# File 'lib/parslet/source.rb', line 25 def matches?(pattern) @str.index(pattern, @pos) == @pos end |