Class: BabelBridge::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/babel_bridge/nodes/node.rb

Overview

base class for all parse-tree nodes

Direct Known Subclasses

EmptyNode, NonTerminalNode, RootNode, TerminalNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Node

Returns a new instance of Node.



76
77
78
# File 'lib/babel_bridge/nodes/node.rb', line 76

def initialize(parent)
  node_init(parent)
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def delimiter
  @delimiter
end

#many_delimiterObject

Returns the value of attribute many_delimiter.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def many_delimiter
  @many_delimiter
end

#match_lengthObject

Returns the value of attribute match_length.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def match_length
  @match_length
end

#offsetObject

Returns the value of attribute offset.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def offset
  @offset
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def parent
  @parent
end

#parserObject

Returns the value of attribute parser.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def parser
  @parser
end

#srcObject

Returns the value of attribute src.



10
11
12
# File 'lib/babel_bridge/nodes/node.rb', line 10

def src
  @src
end

Instance Method Details

#columnObject



42
43
44
45
# File 'lib/babel_bridge/nodes/node.rb', line 42

def column
  init_line_column unless @column
  @column
end

#init_line_columnObject



33
34
35
# File 'lib/babel_bridge/nodes/node.rb', line 33

def init_line_column
  @line, @column = Tools.line_column(src, offset)
end

#inspect(options = {}) ⇒ Object

Returns a human-readable representation of the parse tree options

:simple => output a simplified representation of the parse tree


83
84
85
# File 'lib/babel_bridge/nodes/node.rb', line 83

def inspect(options={})
  "(TODO: def #{self.class}#inspect(options={}))"
end

#lengthObject

length returns the number of sub-nodes



94
95
96
# File 'lib/babel_bridge/nodes/node.rb', line 94

def length
  0
end

#lineObject



37
38
39
40
# File 'lib/babel_bridge/nodes/node.rb', line 37

def line
  init_line_column unless @line
  @line
end

#match_rangeObject



25
26
27
# File 'lib/babel_bridge/nodes/node.rb', line 25

def match_range
  offset..(offset+match_length-1)
end

#node_init(parent_or_parser) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/babel_bridge/nodes/node.rb', line 58

def node_init(parent_or_parser)
  self.match_length=0
  case parent_or_parser
  when Parser then
    self.parser=parent_or_parser
    self.offset=0
    self.src=parser.src
  when Node then
    self.parent=parent_or_parser
    self.parser=parent.parser
    self.offset=parent.next
    self.src=parent.src
    raise "parent node does not have parser set" unless parser
  else
    raise "parent_or_parser(#{parent_or_parser.class}) must be a Node or a Parser"
  end
end

#node_pathObject



116
117
118
# File 'lib/babel_bridge/nodes/node.rb', line 116

def node_path
  path_string parent_list
end

#offset_after_matchObject Also known as: next

the index of the first character after the match



17
18
19
# File 'lib/babel_bridge/nodes/node.rb', line 17

def offset_after_match
  offset + match_length
end

#on_matchedObject

called when a ruled is matched



30
31
# File 'lib/babel_bridge/nodes/node.rb', line 30

def on_matched
end

#onlychildren_listObject

walk down the children chain as long as there is only one child at each level log and return the path



104
105
106
107
108
109
110
# File 'lib/babel_bridge/nodes/node.rb', line 104

def onlychildren_list
  if matches.length == 1
    [self] + matches[0].onlychildren_list
  else
    [self]
  end
end

#parent_listObject



98
99
100
# File 'lib/babel_bridge/nodes/node.rb', line 98

def parent_list
  return parent ? parent.parent_list+[parent] : []
end

#path_string(node_list) ⇒ Object



112
113
114
# File 'lib/babel_bridge/nodes/node.rb', line 112

def path_string(node_list)
  node_list.collect{|n|n.class}.join ' > '
end

#relative_class_nameObject



12
13
14
# File 'lib/babel_bridge/nodes/node.rb', line 12

def relative_class_name
  (self.class.to_s.split(parser.class.to_s+"::",2)[1]||self.class.to_s).strip
end

#relative_source_fileObject



48
# File 'lib/babel_bridge/nodes/node.rb', line 48

def relative_source_file; parser.relative_source_file; end

#remaining_src(sub_offset) ⇒ Object



21
22
23
# File 'lib/babel_bridge/nodes/node.rb', line 21

def remaining_src(sub_offset)
  src[self.next+sub_offset..-1]
end

#source_fileObject



47
# File 'lib/babel_bridge/nodes/node.rb', line 47

def source_file; parser.source_file; end

#textObject

the substring in src matched



91
# File 'lib/babel_bridge/nodes/node.rb', line 91

def text; match_length == 0 ? "" : src[match_range] end

#to_sObject



50
51
52
# File 'lib/babel_bridge/nodes/node.rb', line 50

def to_s
  text
end

#to_symObject



54
55
56
# File 'lib/babel_bridge/nodes/node.rb', line 54

def to_sym
  to_s.to_sym
end