@@ -138,12 +138,13 @@ def initialize(name, comment)
138
138
# as a placeholder for collecting all of the various places that nodes are
139
139
# used.
140
140
class Node
141
- attr_reader :name , :comment , :attributes
141
+ attr_reader :name , :comment , :attributes , :visitor_method
142
142
143
- def initialize ( name , comment , attributes )
143
+ def initialize ( name , comment , attributes , visitor_method )
144
144
@name = name
145
145
@comment = comment
146
146
@attributes = attributes
147
+ @visitor_method = visitor_method
147
148
end
148
149
end
149
150
@@ -196,6 +197,10 @@ def parse_comments(statements, index)
196
197
Attribute . new ( :location , "[Location] the location of this node" )
197
198
}
198
199
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
+
199
204
statements = main_statement . bodystmt . statements . body
200
205
statements . each_with_index do |statement , statement_index |
201
206
case statement
@@ -225,16 +230,25 @@ def parse_comments(statements, index)
225
230
end
226
231
227
232
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
228
238
end
229
239
end
230
240
241
+ # If we never found a visitor method, then we have an error.
242
+ raise if visitor_method . nil?
243
+
231
244
# Finally, set it up in the hash of nodes so that we can use it later.
232
245
comments = parse_comments ( main_statements , main_statement_index )
233
246
node =
234
247
Node . new (
235
248
main_statement . constant . constant . value . to_sym ,
236
249
"#{ comments . join ( "\n " ) } \n " ,
237
- attributes
250
+ attributes ,
251
+ visitor_method
238
252
)
239
253
240
254
@nodes [ node . name ] = node
0 commit comments