1 #!/usr/bin/ruby 2 # encoding: utf-8 3 4 module ANTLR3 5 module Debug 6 =begin rdoc ANTLR3::Debug::TreeAdaptor 7 8 Adds debugging event hooks to TreeAdaptor objects 9 10 =end 11 module TreeAdaptor 12 13 def self.wrap( adaptor, debug_listener = nil ) 14 adaptor.extend( self ) 15 adaptor.debug_listener = debug_listener 16 return( adaptor ) 17 end 18 19 attr_accessor :debug_listener 20 21 def create_with_payload( payload ) 22 node = super 23 @debug_listener.create_node( node, payload ) 24 return node 25 end 26 27 def create_from_token( token_type, from_token, text = nil ) 28 node = super 29 @debug_listener.create_node( node ) 30 return node 31 end 32 33 def create_from_type( token_type, text ) 34 node = super 35 @debug_listener.create_node( node ) 36 return node 37 end 38 39 def create_error_node( input, start, stop, exc ) 40 node = super 41 node.nil? or @debug_listener.error_node( node ) 42 return node 43 end 44 45 def copy_tree( tree ) 46 t = super 47 simulate_tree_construction( t ) 48 return t 49 end 50 51 def simulate_tree_construction( tree ) 52 @debug_listener.create_node( tree ) 53 child_count( tree ).times do |i| 54 child = self.child_of( tree, i ) 55 simulate_tree_construction( child ) 56 @debug_listener.add_child( tree, child ) 57 end 58 end 59 60 def copy_node( tree_node ) 61 duplicate = super 62 @debug_listener.create_node duplicate 63 return duplicate 64 end 65 66 def create_flat_list 67 node = super 68 @debug_listener.flat_node( node ) 69 return node 70 end 71 72 def add_child( tree, child ) 73 case child 74 when Token 75 node = create_with_payload( child ) 76 add_child( tree, node ) 77 else 78 tree.nil? || child.nil? and return 79 super( tree, child ) 80 @debug_listener.add_child( tree, child ) 81 end 82 end 83 84 def become_root( new_root, old_root ) 85 case new_root 86 when Token 87 n = create_with_payload( new_root ) 88 super( n, old_root ) 89 else 90 n = super( new_root, old_root ) 91 end 92 @debug_listener.become_root( new_root, old_root ) 93 return n 94 end 95 96 def set_token_boundaries( tree, start_token, stop_token ) 97 super( tree, start_token, stop_token ) 98 return unless tree && start_token && stop_token 99 @debug_listener.set_token_boundaries( tree, 100 start_token.token_index, stop_token.token_index ) 101 end 102 end 103 104 =begin rdoc ANTLR3::Debug::TreeNodeStream 105 106 A module that wraps token stream methods with debugging event code. A debuggable 107 parser will <tt>extend</tt> its input stream with this module if the stream is 108 not already a Debug::TreeNodeStream. 109 110 =end 111 class TreeNodeStream 112 113 def self.wrap( stream, debug_listener = nil ) 114 stream.extend( self ) 115 stream.debug_listener ||= debug_listener 116 end 117 attr_accessor :debug_listener 118 119 def consume 120 node = @input >> 1 121 super 122 @debug_listener.consume_node( node ) 123 end 124 125 def look( i = 1 ) 126 node = super 127 id = @adaptor.unique_id( node ) 128 text = @adaptor.text_of( node ) 129 type = @adaptor.type_of( node ) 130 @debug_listener.look( i, node ) 131 return( node ) 132 end 133 134 def peek( i = 1 ) 135 node = self >> 1 136 id = @adaptor.unique_id( node ) 137 text = @adaptor.text_of( node ) 138 type = @adaptor.type_of( node ) 139 @debug_listener.look( i, node ) 140 return( type ) 141 end 142 143 def mark 144 @last_marker = super 145 @debug_listener.mark( @last_marker ) 146 return( @last_marker ) 147 end 148 149 def rewind( marker = nil ) 150 @debug_listener.rewind( marker ) 151 super( marker || @last_marker ) 152 end 153 154 =begin This actually differs with reset in CommonTreeNodeStream -- why is this one blank? 155 def reset 156 # do nothing 157 end 158 =end 159 160 end 161 162 163 end 164 end 165