Class: ANTLR3::Debug::RuleTracer

Inherits:
Object
  • Object
show all
Includes:
EventListener
Defined in:
lib/antlr3/debug/rule-tracer.rb

Overview

RuleTracer is simple debug event listener that writes the names of rule methods as they are entered and exitted to an output stream.

Constant Summary collapse

ARROW_IN =
'--> '.freeze
ARROW_OUT =
'<-- '.freeze

Constants included from EventListener

EventListener::EVENTS, EventListener::PROTOCOL_VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventListener

#add_child, #backtrack, #become_root, #begin_backtrack, #begin_resync, #commence, #consume_hidden_token, #consume_node, #consume_token, #create_node, #end_backtrack, #end_resync, #enter_alternative, #enter_decision, #enter_subrule, #error_node, #examine_rule_memoization, #exit_decision, #exit_subrule, #flat_node, #location, #look, #mark, #on, #recognition_exception, #resync, #rewind, #semantic_predicate, #set_token_boundaries, #terminate

Constructor Details

#initialize(options = {}) ⇒ RuleTracer

Returns a new instance of RuleTracer.



21
22
23
24
25
26
# File 'lib/antlr3/debug/rule-tracer.rb', line 21

def initialize( options = {} )
  @input = options[ :input ]
  @level = 0
  @spaces_per_indent = options[ :spaces_per_indent ] || 2
  @device = options[ :device ] || options[ :output ] || $stderr
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



19
20
21
# File 'lib/antlr3/debug/rule-tracer.rb', line 19

def device
  @device
end

#levelObject (readonly)

Returns the value of attribute level.



18
19
20
# File 'lib/antlr3/debug/rule-tracer.rb', line 18

def level
  @level
end

#spaces_per_indentObject

Returns the value of attribute spaces_per_indent.



19
20
21
# File 'lib/antlr3/debug/rule-tracer.rb', line 19

def spaces_per_indent
  @spaces_per_indent
end

Instance Method Details

#enter_rule(grammar_file, name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/antlr3/debug/rule-tracer.rb', line 28

def enter_rule( grammar_file, name )
  indent = @level * @spaces_per_indent
  
  @device.print( ' ' * indent, ARROW_IN, name )
  if @input
    input_symbol = @input.look || :EOF
    @device.puts( " look = %p" % input_symbol )
  else @device.print( "\n" )
  end
  
  @level += 1
end

#exit_rule(grammar_file, name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/antlr3/debug/rule-tracer.rb', line 41

def exit_rule( grammar_file, name )
  @level -= 1
  
  indent = @level * @spaces_per_indent
  
  @device.print( ' ' * indent, ARROW_OUT, name )
  if @input
    input_symbol = ( @input.look || :EOF )
    @device.puts( " look = %p" % input_symbol )
  else @device.print( "\n" )
  end
end