Class: Rattler::Parsers::NodeCode
- Inherits:
-
Object
- Object
- Rattler::Parsers::NodeCode
- 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
-
#factory_method ⇒ Object
readonly
Returns the value of attribute factory_method.
-
#node_attrs ⇒ Object
readonly
Returns the value of attribute node_attrs.
-
#node_type ⇒ Object
readonly
Returns the value of attribute node_type.
Instance Method Summary collapse
-
#args_expr(scope) ⇒ String
Ruby code for the arguments to the node factory method.
-
#bind(scope) ⇒ String
Ruby code that creates a new node from the parse results.
-
#captures_expr(scope) ⇒ String
Ruby code for the captures argument to the node factory method.
-
#initialize(node_type, factory_method, node_attrs) ⇒ NodeCode
constructor
A new instance of NodeCode.
Constructor Details
#initialize(node_type, factory_method, node_attrs) ⇒ NodeCode
Returns a new instance of NodeCode.
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_method ⇒ Object (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_attrs ⇒ Object (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_type ⇒ Object (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.
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.
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.
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 |