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.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combining

#capturing?, #with_ws

Methods inherited from Parser

#&, #capturing?, #one_or_more, #optional, #skip, #variable_capture_count?, #with_ws, #zero_or_more, #|

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #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.



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

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

.parsed(results, *_) ⇒ Object



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

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

Instance Method Details

#labeled?Boolean

Always true

Returns:

  • (Boolean)

    true



34
35
36
# File 'lib/rattler/parsers/label.rb', line 34

def labeled?
  true
end

#parse(scanner, rules, scope = {}) ⇒ Object

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

Returns:

  • the decorated parser’s parse result



44
45
46
47
48
49
# File 'lib/rattler/parsers/label.rb', line 44

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