Class: Treetop::Runtime::SyntaxNode

Inherits:
Object
  • Object
show all
Defined in:
lib/treetop/runtime/syntax_node.rb

Constant Summary collapse

@@dot_id_counter =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, elements = nil) ⇒ SyntaxNode

Returns a new instance of SyntaxNode.



10
11
12
13
14
# File 'lib/treetop/runtime/syntax_node.rb', line 10

def initialize(input, interval, elements = nil)
  @input = input
  @interval = interval
  @elements = elements
end

Instance Attribute Details

#dot_idObject (readonly)

Returns the value of attribute dot_id.



6
7
8
# File 'lib/treetop/runtime/syntax_node.rb', line 6

def dot_id
  @dot_id
end

#inputObject (readonly)

Returns the value of attribute input.



4
5
6
# File 'lib/treetop/runtime/syntax_node.rb', line 4

def input
  @input
end

#intervalObject (readonly)

Returns the value of attribute interval.



4
5
6
# File 'lib/treetop/runtime/syntax_node.rb', line 4

def interval
  @interval
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/treetop/runtime/syntax_node.rb', line 5

def parent
  @parent
end

Instance Method Details

#<=>(other) ⇒ Object



51
52
53
# File 'lib/treetop/runtime/syntax_node.rb', line 51

def <=>(other)
  self.interval.first <=> other.interval.first
end

#elementsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/treetop/runtime/syntax_node.rb', line 16

def elements
  return @elements if terminal?
  # replace the character class placeholders in the sequence (lazy instantiation)
  last_element = nil
  @comprehensive_elements ||= @elements.map do |element|
    if element == true
      index = last_element ? last_element.interval.last : interval.first
      element = SyntaxNode.new(input, index...(index + 1))
    end
    element.parent = self
    last_element = element
  end

  @dot_id = @@dot_id_counter
  @@dot_id_counter += 1

  @comprehensive_elements
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/treetop/runtime/syntax_node.rb', line 47

def empty?
  interval.first == interval.last && interval.exclude_end?
end

#extension_modulesObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/treetop/runtime/syntax_node.rb', line 55

def extension_modules
  local_extensions =
    class <<self
      included_modules-Object.included_modules
    end
  if local_extensions.size > 0
    local_extensions
  else
    []    # There weren't any; must be a literal node
  end
end

#inspect(indent = "") ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/treetop/runtime/syntax_node.rb', line 67

def inspect(indent="")
  em = extension_modules
  interesting_methods = methods-[em.last ? em.last.methods : nil]-self.class.instance_methods
  im = interesting_methods.size > 0 ? " (#{interesting_methods.join(",")})" : ""
  tv = text_value
  tv = "...#{tv[-20..-1]}" if tv.size > 20

  indent +
  self.class.to_s.sub(/.*:/,'') +
    em.map{|m| "+"+m.to_s.sub(/.*:/,'')}*"" +
    " offset=#{interval.first}" +
    ", #{tv.inspect}" +
    im +
    (elements && elements.size > 0 ?
      ":" +
        (elements||[]).map{|e|
    begin
      "\n"+e.inspect(indent+"  ")
    rescue  # Defend against inspect not taking a parameter
      "\n"+indent+" "+e.inspect
    end
        }.join("") :
      ""
    )
end

#nonterminal?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/treetop/runtime/syntax_node.rb', line 39

def nonterminal?
  !terminal?
end

#terminal?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/treetop/runtime/syntax_node.rb', line 35

def terminal?
  @elements.nil?
end

#text_valueObject



43
44
45
# File 'lib/treetop/runtime/syntax_node.rb', line 43

def text_value
  input[interval]
end

#write_dot(io) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/treetop/runtime/syntax_node.rb', line 93

def write_dot(io)
  io.puts "node#{dot_id} [label=\"#{text_value}\"];"
  if nonterminal? then
    elements.each do
      |x|
      io.puts "node#{dot_id} -> node#{x.dot_id};"
      x.write_dot(io)
    end
  end
end

#write_dot_file(fname) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/treetop/runtime/syntax_node.rb', line 104

def write_dot_file(fname)
  File.open(fname + ".dot","w") do
    |file|
    file.puts "digraph G {"
    write_dot(file)
    file.puts "}"
  end
end