Home | History | Annotate | Download | only in debug
      1 #!/usr/bin/ruby
      2 # encoding: utf-8
      3 
      4 module ANTLR3
      5 module Debug
      6 =begin rdoc ANTLR3::Debug::RuleTracer
      7 
      8 RuleTracer is simple debug event listener that writes the names of rule methods
      9 as they are entered and exitted to an output stream.
     10 
     11 =end
     12 class RuleTracer
     13   include EventListener
     14   
     15   ARROW_IN = '--> '.freeze
     16   ARROW_OUT = '<-- '.freeze
     17   
     18   attr_reader :level
     19   attr_accessor :spaces_per_indent, :device
     20   
     21   def initialize( options = {} )
     22     @input = options[ :input ]
     23     @level = 0
     24     @spaces_per_indent = options[ :spaces_per_indent ] || 2
     25     @device = options[ :device ] || options[ :output ] || $stderr
     26   end
     27   
     28   def enter_rule( grammar_file, name )
     29     indent = @level * @spaces_per_indent
     30     
     31     @device.print( ' ' * indent, ARROW_IN, name )
     32     if @input
     33       input_symbol = @input.look || :EOF
     34       @device.puts( " look = %p" % input_symbol )
     35     else @device.print( "\n" )
     36     end
     37     
     38     @level += 1
     39   end
     40   
     41   def exit_rule( grammar_file, name )
     42     @level -= 1
     43     
     44     indent = @level * @spaces_per_indent
     45     
     46     @device.print( ' ' * indent, ARROW_OUT, name )
     47     if @input
     48       input_symbol = ( @input.look || :EOF )
     49       @device.puts( " look = %p" % input_symbol )
     50     else @device.print( "\n" )
     51     end
     52   end
     53 end
     54 end
     55 end
     56