class Rattler::Parsers::Sequence

Sequence combines two or more parsers and matches by trying each one in sequence and failing unless they all succeed.

Public Instance Methods

&(other) click to toggle source

(see Parser#&)

# File lib/rattler/parsers/sequence.rb, line 32
def &(other)
  Sequence[(children + [other])]
end
>>(semantic) click to toggle source

(see Parser#>>)

# File lib/rattler/parsers/sequence.rb, line 37
def >>(semantic)
  AttributedSequence[(children + [semantic])]
end
capture_count() click to toggle source

@return [Fixnum] the number of child parsers that are capturing

# File lib/rattler/parsers/sequence.rb, line 42
def capture_count
  @capture_count ||= count {|_| _.capturing? }
end
parse(scanner, rules, scope = ParserScope.empty) { |scope| ... } click to toggle source

Try each parser in sequence, and if they all succeed return an array of captured results, or return false if any parser fails.

@param (see Match#parse)

@return an array of captured results of each parser in sequence, or

+false+
# File lib/rattler/parsers/sequence.rb, line 22
def parse(scanner, rules, scope = ParserScope.empty)
  backtracking(scanner) do
    if scope = parse_children(scanner, rules, scope.nest)
      yield scope if block_given?
      parse_result(scope)
    end
  end
end

Private Instance Methods

parse_result(scope) click to toggle source
# File lib/rattler/parsers/sequence.rb, line 48
def parse_result(scope)
  case capture_count
  when 0 then true
  when 1 then scope.captures[0]
  else scope.captures
  end
end