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 Method Summary collapse
- #bytepos ⇒ Object
- #bytepos=(n) ⇒ Object
-
#chars_left ⇒ Object
Returns how many chars remain in the input.
-
#chars_until(str) ⇒ Fixnum
Returns how many chars there are between current position and the string given.
-
#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.
-
#pos ⇒ Object
Position of the parse as a character offset into the original string.
Constructor Details
#initialize(str) ⇒ Source
Returns a new instance of Source.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/parslet/source.rb', line 12 def initialize(str) raise( ArgumentError, "Must construct Source with a string like object." ) unless str.respond_to?(:to_str) @str = StringScanner.new(str) # maps 1 => /./m, 2 => /../m, etc... @re_cache = Hash.new { |h,k| h[k] = /(.|$){#{k}}/m } @line_cache = LineCache.new @line_cache.scan_for_line_endings(0, str) end |
Instance Method Details
#bytepos ⇒ Object
77 78 79 |
# File 'lib/parslet/source.rb', line 77 def bytepos @str.pos end |
#bytepos=(n) ⇒ Object
Please be aware of encodings at this point.
83 84 85 86 |
# File 'lib/parslet/source.rb', line 83 def bytepos=(n) @str.pos = n rescue RangeError end |
#chars_left ⇒ Object
Returns how many chars remain in the input.
54 55 56 |
# File 'lib/parslet/source.rb', line 54 def chars_left @str.rest_size end |
#chars_until(str) ⇒ Fixnum
Returns how many chars there are between current position and the string given. If the string given doesn’t occur in the source, then the remaining chars (#chars_left) are returned.
64 65 66 67 68 |
# File 'lib/parslet/source.rb', line 64 def chars_until str slice_str = @str.check_until(Regexp.new(Regexp.escape(str))) return chars_left unless slice_str return slice_str.size - str.size end |
#consume(n) ⇒ Object
Consumes n characters from the input, returning them as a slice of the input.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/parslet/source.rb', line 41 def consume(n) position = self.pos slice_str = @str.scan(@re_cache[n]) slice = Parslet::Slice.new( position, slice_str, @line_cache) 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.
92 93 94 |
# File 'lib/parslet/source.rb', line 92 def line_and_column(position=nil) @line_cache.line_and_column(position || self.bytepos) end |
#matches?(pattern) ⇒ Boolean Also known as: match
Checks if the given pattern matches at the current input position.
33 34 35 |
# File 'lib/parslet/source.rb', line 33 def matches?(pattern) @str.match?(pattern) end |