Class: Rattler::Parsers::Label

Inherits:
Parser show all
Includes:
Combining
Defined in:
lib/rattler/parsers/label.rb

Overview

Label decorates a parser and associates a label with the decorated parser’s parse result if successful. The label only applies if nested in a Choice or Sequence decorated by an Action.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combining

#capturing?, #semantic?, #with_ws

Methods inherited from Parser

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

.[](label, parser) ⇒ Object

Create a new parser that decorates parser and associates label with parser‘s parse result on success.



18
19
20
# File 'lib/rattler/parsers/label.rb', line 18

def self.[](label, parser)
  self.new(parser, :label => label.to_sym)
end

.parsed(results, *_) ⇒ Object



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

def self.parsed(results, *_) #:nodoc:
  self[*results]
end

Instance Method Details

#capturing_decidable?Object

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

Returns:

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



23
24
25
# File 'lib/rattler/parsers/label.rb', line 23

def capturing_decidable?
  child.capturing_decidable?
end

#labeled?Boolean

Returns true.

Returns:

  • (Boolean)

    true



28
29
30
# File 'lib/rattler/parsers/label.rb', line 28

def labeled?
  true
end

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

Delegate to the decorated parser and associate #label with the parse result if successful.

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 decorated parser’s parse result



38
39
40
41
42
43
# File 'lib/rattler/parsers/label.rb', line 38

def parse(scanner, rules, scope = ParserScope.empty)
  if result = child.parse(scanner, rules, scope) {|_| scope = _ }
    yield scope.bind(label => result) if block_given?
    result
  end
end