Class: Parslet::Atoms::Alternative

### Home Classes Methods

### In Files

- lib/parslet/atoms/alternative.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::Alternative

Alternative during matching. Contains a list of parslets that is tried each one in turn. Only fails if all alternatives fail.

Example:

    str('a') | str('b')   # matches either 'a' or 'b'

### Attributes

alternatives[R]

### Public Class Methods

new(*alternatives) click to toggle source

Constructs an Alternative instance using all given parslets in the order given. This is what happens if you call '|' on existing parslets, like this:

    str('a') | str('b')

    # File lib/parslet/atoms/alternative.rb, line 18
    def initialize(*alternatives)
      super()

      @alternatives = alternatives
      @error_msg = "Expected one of #{alternatives.inspect}"
    end

### Public Instance Methods

accept(visitor) click to toggle source

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

    # File lib/parslet/atoms/visitor.rb, line 60
    def accept(visitor)
      visitor.visit_alternative(alternatives)
    end

to_s_inner(prec) click to toggle source

    # File lib/parslet/atoms/alternative.rb, line 47
    def to_s_inner(prec)
      alternatives.map { |a| a.to_s(prec) }.join(' / ')
    end

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

    # File lib/parslet/atoms/alternative.rb, line 33
    def try(source, context, consume_all)
      errors = alternatives.map { |a|
        success, value = result = a.apply(source, context, consume_all)
        return result if success

        # Aggregate all errors
        value
      }

      # If we reach this point, all alternatives have failed.
      context.err(self, source, @error_msg, errors)
    end

|(parslet) click to toggle source

+

    # File lib/parslet/atoms/alternative.rb, line 29
    def |(parslet)
      self.class.new(*@alternatives + [parslet])
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.