@@ -138,12 +138,13 @@ def initialize(name, comment)
138138 # as a placeholder for collecting all of the various places that nodes are
139139 # used.
140140 class Node
141- attr_reader :name , :comment , :attributes
141+ attr_reader :name , :comment , :attributes , :visitor_method
142142
143- def initialize ( name , comment , attributes )
143+ def initialize ( name , comment , attributes , visitor_method )
144144 @name = name
145145 @comment = comment
146146 @attributes = attributes
147+ @visitor_method = visitor_method
147148 end
148149 end
149150
@@ -196,6 +197,10 @@ def parse_comments(statements, index)
196197 Attribute . new ( :location , "[Location] the location of this node" )
197198 }
198199
200+ # This is the name of the method tha gets called on the given visitor when
201+ # the accept method is called on this node.
202+ visitor_method = nil
203+
199204 statements = main_statement . bodystmt . statements . body
200205 statements . each_with_index do |statement , statement_index |
201206 case statement
@@ -225,16 +230,25 @@ def parse_comments(statements, index)
225230 end
226231
227232 attributes [ attribute . name ] = attribute
233+ when SyntaxTree ::DefNode
234+ if statement . name . value == "accept"
235+ call_node = statement . bodystmt . statements . body . first
236+ visitor_method = call_node . message . value . to_sym
237+ end
228238 end
229239 end
230240
241+ # If we never found a visitor method, then we have an error.
242+ raise if visitor_method . nil?
243+
231244 # Finally, set it up in the hash of nodes so that we can use it later.
232245 comments = parse_comments ( main_statements , main_statement_index )
233246 node =
234247 Node . new (
235248 main_statement . constant . constant . value . to_sym ,
236249 "#{ comments . join ( "\n " ) } \n " ,
237- attributes
250+ attributes ,
251+ visitor_method
238252 )
239253
240254 @nodes [ node . name ] = node
0 commit comments