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

Returns a new parser that tries both this parser and other and fails unless both parse in sequence.

Parameters:

  • other (Parser)

    the next parser to try if this parser succeeds.

Returns:

  • (Sequence)

    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

Parameters:

  • semantic (Parser)

    a semantic action.

Returns:

  • (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_countFixnum

Returns the number of child parsers that are capturing.

Returns:

  • (Fixnum)

    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.

Parameters:

  • scanner (StringScanner)

    the scanner for the current parse

  • rules (RuleSet)

    the grammar rules being used for the current parse

  • scope (ParserScope) (defaults to: ParserScope.empty)

    the scope of captured results

Returns:

  • an array of captured results of each parser in sequence, or false



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