Class: BabelBridge::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



73
74
75
# File 'lib/nodes/node.rb', line 73

def initialize(parent)
  node_init(parent)
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



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

def delimiter
  @delimiter
end

#many_delimiterObject

Returns the value of attribute many_delimiter.



10
11
12
# File 'lib/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/nodes/node.rb', line 10

def match_length
  @match_length
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#parserObject

Returns the value of attribute parser.



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

def parser
  @parser
end

#srcObject

Returns the value of attribute src.



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

def src
  @src
end

Instance Method Details

#columnObject



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

def column
  init_line_column unless @column
  @column
end

#init_line_columnObject



33
34
35
# File 'lib/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


80
81
82
# File 'lib/nodes/node.rb', line 80

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

#lengthObject

length returns the number of sub-nodes



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

def length
  0
end

#lineObject



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

def line
  init_line_column unless @line
  @line
end

#match_rangeObject



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

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

#node_init(parent_or_parser) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nodes/node.rb', line 55

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



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

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/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/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



101
102
103
104
105
106
107
# File 'lib/nodes/node.rb', line 101

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

#parent_listObject



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

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

#path_string(node_list) ⇒ Object



109
110
111
# File 'lib/nodes/node.rb', line 109

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

#relative_class_nameObject



12
13
14
# File 'lib/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

#remaining_src(sub_offset) ⇒ Object



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

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

#textObject

the substring in src matched



88
# File 'lib/nodes/node.rb', line 88

def text; src[match_range] end

#to_sObject



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

def to_s
  text
end

#to_symObject



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

def to_sym
  to_s.to_sym
end