class Rattler::Parsers::Choice

Choice combines two or more parsers and matches by trying each one in order until one succeeds and returning that result.

Public Instance Methods

capturing_decidable?() click to toggle source

(see Parser#capturing_decidable?)

# File lib/rattler/parsers/choice.rb, line 31
def capturing_decidable?
  @capturing_decidable ||=
    children.all? {|_| _.capturing_decidable? } and
    ( children.all? {|_| _.capturing? } or
      children.none? {|_| _.capturing? } )
end
parse(scanner, rules, scope = ParserScope.empty) click to toggle source

Try each parser in order until one succeeds and return that result.

@param (see Match#parse)

@return the result of the first parser that matches, or false

# File lib/rattler/parsers/choice.rb, line 21
def parse(scanner, rules, scope = ParserScope.empty)
  for child in children
    if r = child.parse(scanner, rules, scope)
      return r
    end
  end
  false
end
|(other) click to toggle source

Return a new parser that tries this parser first and if it fails tries other.

@param other (see Parser#|) @return (see Parser#|)

# File lib/rattler/parsers/choice.rb, line 43
def |(other)
  Choice[(children + [other])]
end