Class: Rattler::Parsers::NodeCode

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/parsers/node_code.rb

Overview

NodeCode defines a set of attributes that can be found to captured parse returns to produce ruby code that creates a new node from the parse results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_type, factory_method, node_attrs) ⇒ NodeCode

Returns a new instance of NodeCode.

Parameters:

  • node_type (Symbol)

    the name of the class to instantiate nodes from

  • factory_method (Symbol)

    the class method used to create new parse nodes

  • node_attrs (Hash)

    attributes to add to new parse nodes



13
14
15
16
17
# File 'lib/rattler/parsers/node_code.rb', line 13

def initialize(node_type, factory_method, node_attrs)
  @node_type = node_type
  @factory_method = factory_method
  @node_attrs = node_attrs
end

Instance Attribute Details

#factory_methodObject (readonly)

Returns the value of attribute factory_method.



19
20
21
# File 'lib/rattler/parsers/node_code.rb', line 19

def factory_method
  @factory_method
end

#node_attrsObject (readonly)

Returns the value of attribute node_attrs.



19
20
21
# File 'lib/rattler/parsers/node_code.rb', line 19

def node_attrs
  @node_attrs
end

#node_typeObject (readonly)

Returns the value of attribute node_type.



19
20
21
# File 'lib/rattler/parsers/node_code.rb', line 19

def node_type
  @node_type
end

Instance Method Details

#args_expr(scope) ⇒ String

Returns ruby code for the arguments to the node factory method.

Parameters:

  • scope (ParserScope)

    the scope of captured parse results

Returns:

  • (String)

    ruby code for the arguments to the node factory method



29
30
31
32
33
34
# File 'lib/rattler/parsers/node_code.rb', line 29

def args_expr(scope)
  args = [captures_expr(scope)]
  attrs = encoded_binding_attrs(scope).merge encoded_node_attrs(scope)
  args << encode_assocs(attrs) unless attrs.empty?
  args.join(', ')
end

#bind(scope) ⇒ String

Returns ruby code that creates a new node from the parse results.

Parameters:

  • scope (ParserScope)

    the scope of captured parse results

Returns:

  • (String)

    ruby code that creates a new node from the parse results



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

def bind(scope)
  "#{node_type}.#{factory_method}(#{args_expr scope})"
end

#captures_expr(scope) ⇒ String

Returns ruby code for the captures argument to the node factory method.

Parameters:

  • scope (ParserScope)

    the scope of captured parse results

Returns:

  • (String)

    ruby code for the captures argument to the node factory method



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

def captures_expr(scope)
  expr = '[' + scope.captures.join(', ') + ']'
  scope.captures_decidable? ? expr : "select_captures(#{expr})"
end