Class: Rattler::Parsers::Sequence
- Inherits:
-
Parser
- Object
- Util::Node
- Parser
- Rattler::Parsers::Sequence
- 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
-
#&(other) ⇒ Sequence
A new parser that tries both this parser and
other
and fails unless both parse in sequence. -
#>>(semantic) ⇒ AttributedSequence
A new parser that tries this parser and returns the result of the semantic action if it succeeds.
-
#capture_count ⇒ Fixnum
The number of child parsers that are capturing.
-
#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.
Methods included from Sequencing
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
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
Returns a new parser that tries both this parser and other
and fails unless both parse in sequence.
32 33 34 |
# File 'lib/rattler/parsers/sequence.rb', line 32 def &(other) Sequence[(children + [other])] end |
#>>(semantic) ⇒ AttributedSequence
A new parser that tries this parser and returns the result of the semantic action if it succeeds
37 38 39 |
# File 'lib/rattler/parsers/sequence.rb', line 37 def >>(semantic) AttributedSequence[(children + [semantic])] end |
#capture_count ⇒ Fixnum
Returns the number of child parsers that are capturing.
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 |