Class: Rattler::Parsers::Sequence

Inherits:
Parser show all
Includes:
Sequencing
Defined in:
lib/rattler/parsers/sequence.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sequencing

#sequence?

Methods included from Combining

#capturing?, #capturing_decidable?, #semantic?, #with_ws

Methods inherited from Parser

#capturing?, #capturing_decidable?, #labeled?, #list, #one_or_more, #optional, #repeat, #semantic?, #sequence?, #skip, #variable_capture_count?, #with_ws, #zero_or_more, #|

Methods included from Runtime::ParserHelper

#select_captures

Methods inherited from Util::Node

#==, [], #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

This class inherits a constructor from Rattler::Util::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Class Method Details

.parsed(results, *_) ⇒ Object



11
12
13
# File 'lib/rattler/parsers/sequence.rb', line 11

def self.parsed(results, *_) #:nodoc:
  results.reduce(:&)
end

Instance Method Details

#&(other) ⇒ Sequence



32
33
34
# File 'lib/rattler/parsers/sequence.rb', line 32

def &(other)
  Sequence[(children + [other])]
end

#>>(semantic) ⇒ AttributedSequence



37
38
39
# File 'lib/rattler/parsers/sequence.rb', line 37

def >>(semantic)
  AttributedSequence[(children + [semantic])]
end

#capture_countFixnum



42
43
44
# File 'lib/rattler/parsers/sequence.rb', line 42

def capture_count
  @capture_count ||= count {|_| _.capturing? }
end

#parse(scanner, rules, scope = ParserScope.empty) ⇒ Object

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



22
23
24
25
26
27
28
29
# 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