Class: Parslet::Atoms::Lookahead

### Home Classes Methods

### In Files

- lib/parslet/atoms/lookahead.rb
- lib/parslet/atoms/visitor.rb

### Parent

Parslet::Atoms::Base

### Methods

- ::new
- #accept
- #to_s_inner
- #try

### 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::Atoms::Lookahead

Either positive or negative lookahead, doesn't consume its input.

Example:

    str('foo').present? # matches when the input contains 'foo', but leaves it

### Attributes

bound_parslet[R]
positive[R]

### Public Class Methods

new(bound_parslet, positive=true) click to toggle source

    # File lib/parslet/atoms/lookahead.rb, line 11
    def initialize(bound_parslet, positive=true)
      super()

      # Model positive and negative lookahead by testing this flag.
      @positive = positive
      @bound_parslet = bound_parslet

      @error_msgs = {
        :positive => ["Input should start with ", bound_parslet],
        :negative => ["Input should not start with ", bound_parslet]
      }
    end

### Public Instance Methods

accept(visitor) click to toggle source

Call back visitors visit_lookahead method. See parslet/export for an example.

    # File lib/parslet/atoms/visitor.rb, line 69
    def accept(visitor)
      visitor.visit_lookahead(positive, bound_parslet)
    end

to_s_inner(prec) click to toggle source

    # File lib/parslet/atoms/lookahead.rb, line 44
    def to_s_inner(prec)
      char = positive ? '&' : '!'

      "#{char}#{bound_parslet.to_s(prec)}"
    end

try(source, context, consume_all) click to toggle source

    # File lib/parslet/atoms/lookahead.rb, line 24
    def try(source, context, consume_all)
      pos = source.pos

      success, value = bound_parslet.apply(source, context, consume_all)

      if positive
        return succ(nil) if success
        return context.err_at(self, source, @error_msgs[:positive], pos)
      else
        return succ(nil) unless success
        return context.err_at(self, source, @error_msgs[:negative], pos)
      end

    # This is probably the only parslet that rewinds its input in #try.
    # Lookaheads NEVER consume their input, even on success, that's why.
    ensure
      source.pos = pos
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.