diff --git a/lib/treetop/runtime/syntax_node.rb b/lib/treetop/runtime/syntax_node.rb index 1a61e35..0710eaa 100644 --- a/lib/treetop/runtime/syntax_node.rb +++ b/lib/treetop/runtime/syntax_node.rb @@ -1,8 +1,11 @@ module Treetop module Runtime class SyntaxNode + @@dot_id_counter = 0 + attr_reader :input, :interval, :elements attr_accessor :parent + attr_reader :dot_id def initialize(input, interval, elements = nil) @input = input @@ -12,6 +15,9 @@ def initialize(input, interval, elements = nil) element.parent = self end end + + @dot_id = @@dot_id_counter + @@dot_id_counter += 1 end def terminal? @@ -67,6 +73,26 @@ def inspect(indent="") "" ) end + + 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 + + def write_dot_file(fname) + File.open(fname + ".dot","w") do + |file| + file.puts "digraph G {" + write_dot(file) + file.puts "}" + end + end end end end