Class: Furnace::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/furnace/ast/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, children = [], metadata = {}) ⇒ Node

Returns a new instance of Node.



5
6
7
# File 'lib/furnace/ast/node.rb', line 5

def initialize(type, children=[], ={})
  @type, @children, @metadata = type.to_sym, children, 
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/furnace/ast/node.rb', line 3

def children
  @children
end

#metadataObject

Returns the value of attribute metadata.



3
4
5
# File 'lib/furnace/ast/node.rb', line 3

def 
  @metadata
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/furnace/ast/node.rb', line 3

def parent
  @parent
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/furnace/ast/node.rb', line 3

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/furnace/ast/node.rb', line 45

def ==(other)
  if other.respond_to? :to_astlet
    other = other.to_astlet
    other.type == self.type &&
      other.children == self.children
  else
    false
  end
end

#dupObject



38
39
40
41
42
43
# File 'lib/furnace/ast/node.rb', line 38

def dup
  node = super
  node.children = @children.dup
  node. = @metadata.dup
  node
end

#indexObject



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

def index
  parent.children.find_index(self)
end

#nextObject



59
60
61
# File 'lib/furnace/ast/node.rb', line 59

def next
  parent.children[index + 1]
end

#normalize_hierarchy!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/furnace/ast/node.rb', line 9

def normalize_hierarchy!
  @children.each do |child|
    if child.respond_to? :parent=
      child.parent = self
    end

    if child.respond_to? :normalize_hierarchy!
      child.normalize_hierarchy!
    end
  end

  self
end

#prevObject



63
64
65
# File 'lib/furnace/ast/node.rb', line 63

def prev
  parent.children[index - 1]
end

#to_astletObject



91
92
93
# File 'lib/furnace/ast/node.rb', line 91

def to_astlet
  self
end

#to_sObject



67
68
69
# File 'lib/furnace/ast/node.rb', line 67

def to_s
  "(#{fancy_type} ...)"
end

#to_sexp(indent = 0) ⇒ Object Also known as: inspect



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/furnace/ast/node.rb', line 71

def to_sexp(indent=0)
  str = "#{"  " * indent}(#{fancy_type}"

  children.each do |child|
    if (!children[0].is_a?(Node) && child.is_a?(Node)) ||
        (children[0].is_a?(Node) && child.is_a?(Node) &&
          child.children.any? { |c| c.is_a?(Node) || c.is_a?(Array) }) ||
        (child.is_a?(Node) && child.[:label])
      str << "\n#{child.to_sexp(indent + 1)}"
    else
      str << " #{child.inspect}"
    end
  end

  str << ")"

  str
end

#update(type, children = nil, metadata = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/furnace/ast/node.rb', line 23

def update(type, children=nil, ={})
  @type     = type
  @children = children || @children

  # If something non-nil is passed, including default value, then merge.
  # Else, clear metadata store.
  if 
    @metadata.merge!()
  else
    @metadata = {}
  end

  self
end