1 #!/usr/bin/ruby 2 # encoding: utf-8 3 4 require 'antlr3/test/functional' 5 6 class TestRuleTracing < ANTLR3::Test::Functional 7 8 inline_grammar( <<-'END' ) 9 grammar Traced; 10 options { 11 language = Ruby; 12 } 13 14 @parser::init { 15 @stack = nil 16 @traces = [] 17 } 18 19 @parser::members { 20 attr_accessor :stack, :traces 21 22 def trace_in(rule_name, rule_index) 23 @traces << ">#{rule_name}" 24 end 25 26 def trace_out(rule_name, rule_index) 27 @traces << "<#{rule_name}" 28 end 29 } 30 31 @lexer::init { 32 @stack = nil 33 @traces = [] 34 } 35 36 @lexer::members { 37 attr_accessor :stack, :traces 38 39 def trace_in(rule_name, rule_index) 40 @traces << ">#{rule_name}" 41 end 42 43 def trace_out(rule_name, rule_index) 44 @traces << "<#{rule_name}" 45 end 46 } 47 48 a: '<' ((INT '+')=>b|c) '>'; 49 b: c ('+' c)*; 50 c: INT ; 51 52 INT: ('0'..'9')+; 53 WS: (' ' | '\n' | '\t')+ {$channel = HIDDEN;}; 54 END 55 56 compile_options :trace => true 57 58 example "setting up rule tracing" do 59 lexer = Traced::Lexer.new( '< 1 + 2 + 3 >' ) 60 parser = Traced::Parser.new lexer 61 parser.a 62 lexer.traces.should == [ 63 '>t__6!', '<t__6!', '>ws!', '<ws!', '>int!', '<int!', '>ws!', '<ws!', 64 '>t__8!', '<t__8!', '>ws!', '<ws!', '>int!', '<int!', '>ws!', '<ws!', 65 '>t__8!', '<t__8!', '>ws!', '<ws!', '>int!', '<int!', '>ws!', '<ws!', 66 '>t__7!', '<t__7!' 67 ] 68 parser.traces.should == [ 69 '>a', '>synpred1_Traced', '<synpred1_Traced', 70 '>b', '>c', '<c', '>c', '<c', '>c', '<c', '<b', '<a' 71 ] 72 end 73 end 74