Class: TireSwing::Node

Inherits:
Object show all
Defined in:
lib/tire_swing/node.rb

Overview

TireSwing nodes are auto-generated by node definitions (see TireSwing::NodeDefinition). A node is meant to be a part of an AST external to the AST that Treetop generates at parse-time with Treetop::Runtime::SyntaxNodes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Node

Instantiate a node.

Values can either be a hash of values to set the values of this node’s attributes, or a Treetop syntax node which is used to automatically build this node.



44
45
46
47
48
49
50
51
52
# File 'lib/tire_swing/node.rb', line 44

def initialize(values={})
  if values.kind_of?(Treetop::Runtime::SyntaxNode)
    build_from_parsed_node(values)
  else
    values.each do |name, value|
      send("#{name}=", value)
    end
  end
end

Instance Attribute Details

#parentObject

keep track of where this node lives in the tree



37
38
39
# File 'lib/tire_swing/node.rb', line 37

def parent
  @parent
end

Class Method Details

.add_mapping(name, value) ⇒ Object



31
32
33
34
# File 'lib/tire_swing/node.rb', line 31

def self.add_mapping(name, value)
  attribute(name.to_s) { raise "no value given for #{name}" }
  attribute_mapping[name.to_s] = value
end

.attribute_mappingObject

The mapping of attribute names to the auto-build values/methods/lambdas used by create_node



27
28
29
# File 'lib/tire_swing/node.rb', line 27

def self.attribute_mapping
  @attribute_mapping ||= {}
end

.create(*attribs) ⇒ Object

Create a subclass of Node with the given attributes, both simple and mapped.

(see NodeDefinitions#node for more information)



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tire_swing/node.rb', line 13

def self.create(*attribs)
  Class.new(self) do
    attribs.each do |attrib|
      case attrib
      when Symbol, String
        add_mapping(attrib, attrib)
      when Hash
        attrib.each { |name, symbol| add_mapping(name, symbol) }
      end
    end
  end
end

Instance Method Details

#cloneObject

Deep-copy of this node and any children. Use this if you need to manipulate an AST without modifying the original



55
56
57
# File 'lib/tire_swing/node.rb', line 55

def clone
  Marshal.load(Marshal.dump(self))
end