Class: Rattler::Parsers::AttributedSequence

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

Overview

AttributedSequence combines one or more parsers with a semantic action and matches the parser in sequence and applies the action to the captured results.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sequencing

#sequence?

Methods included from Combining

#semantic?, #with_ws

Methods inherited from Parser

#&, #>>, #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



12
13
14
15
# File 'lib/rattler/parsers/attributed_sequence.rb', line 12

def self.parsed(results, *_) #:nodoc:
  op, action = results
  (op + [action]).reduce(:>>)
end

Instance Method Details

#capture_countFixnum

Returns the number of child parsers that are capturing.

Returns:

  • (Fixnum)

    the number of child parsers that are capturing



45
46
47
# File 'lib/rattler/parsers/attributed_sequence.rb', line 45

def capture_count
  @capture_count ||= children[0...-1].count {|_| _.capturing? }
end

#capturing?Boolean

Returns true if the parser returns parse results on success, or false if the parser simply returns true on success.

Returns:

  • (Boolean)

    true if the parser returns parse results on success, or false if the parser simply returns true on success



35
36
37
# File 'lib/rattler/parsers/attributed_sequence.rb', line 35

def capturing?
  children.last.capturing?
end

#capturing_decidable?Boolean

true if it can be determined statically whether the parser returns parse results on success

Returns:

  • (Boolean)

    true if it can be determined statically whether the parser returns parse results on success



40
41
42
# File 'lib/rattler/parsers/attributed_sequence.rb', line 40

def capturing_decidable?
  false
end

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

Parse each parser in sequence, and if they all succeed return the result of applying the semantic action to the captured results.

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:

  • the result of applying the semantic action to the captured results of each parser, or +false



24
25
26
27
28
29
30
31
32
# File 'lib/rattler/parsers/attributed_sequence.rb', line 24

def parse(scanner, rules, scope = ParserScope.empty)
  result = false
  backtracking(scanner) do
    if scope = parse_children(scanner, rules, scope.nest) {|r| result = r }
      yield scope if block_given?
      result
    end
  end
end