Class: Parslet::Source

### Home Classes Methods

### In Files

- lib/parslet/source/line_cache.rb
- lib/parslet/source.rb

### Parent

Object

### Namespace

- MODULE Parslet::Source::RangeSearch
- CLASS Parslet::Source::LineCache

### Methods

- ::new
- #chars_left
- #consume
- #line_and_column
- #match
- #matches?

### Files

- README

### Class/Module Index

Quicksearch

- Parslet
- Parslet::Atoms
- Parslet::Atoms::Alternative
- Parslet::Atoms::Base
- Parslet::Atoms::CanFlatten
- Parslet::Atoms::Capture
- Parslet::Atoms::Context
- Parslet::Atoms::DSL
- Parslet::Atoms::Dynamic
- Parslet::Atoms::Entity
- Parslet::Atoms::Lookahead
- Parslet::Atoms::Named
- Parslet::Atoms::Precedence
- Parslet::Atoms::Re
- Parslet::Atoms::Repetition
- Parslet::Atoms::Scope
- Parslet::Atoms::Sequence
- Parslet::Atoms::Str
- Parslet::Cause
- Parslet::ClassMethods
- Parslet::DelayedMatchConstructor
- Parslet::ErrorReporter
- Parslet::ErrorReporter::Deepest
- Parslet::ErrorReporter::Tree
- Parslet::Expression
- Parslet::Expression::Treetop
- Parslet::Expression::Treetop::Parser
- Parslet::Expression::Treetop::Transform
- Parslet::ParseFailed
- Parslet::Parser
- Parslet::Parser::PrettyPrinter
- Parslet::Parser::Visitors
- Parslet::Parser::Visitors::Citrus
- Parslet::Parser::Visitors::Treetop
- Parslet::Pattern
- Parslet::Pattern::SequenceBind
- Parslet::Pattern::SimpleBind
- Parslet::Pattern::SubtreeBind
- Parslet::Scope
- Parslet::Scope::Binding
- Parslet::Scope::NotFound
- Parslet::Slice
- Parslet::Source
- Parslet::Source::LineCache
- Parslet::Source::RangeSearch
- Parslet::Transform
- Parslet::Transform::Context

No matching classes.

# Parslet::Source

Wraps the input string for parslet.

### Attributes

pos[RW]

Position of the parse as a character offset into the original string. @note: Encodings...

### Public Class Methods

new(str) click to toggle source

    # File lib/parslet/source.rb, line 10
    def initialize(str)
      raise ArgumentError unless str.respond_to?(:to_str)

      @pos = 0
      @str = str

      @line_cache = LineCache.new
      @line_cache.scan_for_line_endings(0, @str)
    end

### Public Instance Methods

chars_left() click to toggle source

Returns how many chars remain in the input.

    # File lib/parslet/source.rb, line 46
    def chars_left
      @str.size - @pos
    end

consume(n) click to toggle source

Consumes n characters from the input, returning them as a slice of the input.

    # 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) click to toggle source

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.

    # File lib/parslet/source.rb, line 58
    def line_and_column(position=nil)
      @line_cache.line_and_column(position || self.pos)
    end

match(pattern) click to toggle source
Alias for: matches?
matches?(pattern) click to toggle source

Checks if the given pattern matches at the current input position.

@param pattern [Regexp, String] pattern to check for @return [Boolean] true if the pattern matches at pos

    # File lib/parslet/source.rb, line 25
    def matches?(pattern)
      @str.index(pattern, @pos) == @pos
    end

Also aliased as: match

[Validate]

Generated with the Darkfish Rdoc Generator 2.