Class: Rattler::Parsers::NodeAction

Inherits:
Parser show all
Includes:
Semantic
Defined in:
lib/rattler/parsers/node_action.rb

Overview

NodeAction is a pseudo-parser that creates a parse node object from captured parse results

Constant Summary collapse

DEFAULT_NODE_TYPE =
'Rattler::Runtime::ParseNode'
DEFAULT_FACTORY_METHOD =
:parsed

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Semantic

#bind, #bindable_code, #semantic?

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

.[](node_type, attrs = {}) ⇒ NodeAction

Returns a pseudo-parser that creates parse nodes.

Parameters:

  • node_type (String, Symbol)

    the name of the class to instantiate nodes from

  • attrs (Hash) (defaults to: {})

    attributes defining how to instantiate the node

Options Hash (attrs):

  • :node_attrs (Hash) — default: {}

    attributes to add to new parse nodes

  • :method (Strin, Symbol) — default: :parsed

    the class method used to create new parse nodes

Returns:

  • (NodeAction)

    a pseudo-parser that creates parse nodes



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

def self.[](node_type, attrs={})
  self.new(attrs.merge :node_type => node_type)
end

.parsed(results, *_) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rattler/parsers/node_action.rb', line 12

def self.parsed(results, *_) #:nodoc:
  optional_node_def, optional_name = results
  attrs = {}
  unless optional_name.empty?
    attrs[:node_attrs] = {:name => eval(optional_name[0], TOPLEVEL_BINDING)}
  end
  if optional_node_def.empty?
    self[DEFAULT_NODE_TYPE, attrs]
  else
    node_type, optional_method = optional_node_def[0]
    attrs[:method] = optional_method[0] unless optional_method.empty?
    self[node_type, attrs]
  end
end

Instance Method Details

#factory_methodSymbol

Returns the class method used to create new parse nodes.

Returns:

  • (Symbol)

    the class method used to create new parse nodes



50
51
52
# File 'lib/rattler/parsers/node_action.rb', line 50

def factory_method
  (attrs[:method] || DEFAULT_FACTORY_METHOD).to_sym
end

#node_attrsHash

Returns attributes to add to the instantiated node.

Returns:

  • (Hash)

    attributes to add to the instantiated node



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

def node_attrs
  attrs[:node_attrs] || {}
end

#node_typeSymbol

Returns the name of the class to instantiate nodes from.

Returns:

  • (Symbol)

    the name of the class to instantiate nodes from



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

def node_type
  (attrs[:node_type] || DEFAULT_NODE_TYPE).to_sym
end

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

Create a new parse node from parse results in scope

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:

  • a new parse node created from parse results in scope



59
60
61
# File 'lib/rattler/parsers/node_action.rb', line 59

def parse(scanner, rules, scope = ParserScope.empty)
  apply scope
end