@@ -141,6 +141,10 @@ def initialize(type, location)
141
141
def deconstruct_keys ( keys )
142
142
{ type : type , location : location }
143
143
end
144
+
145
+ def inspect
146
+ "#<YARP::Comment @type=#{ @type . inspect } @location=#{ @location . inspect } >"
147
+ end
144
148
end
145
149
146
150
# This represents an error that was encountered during parsing.
@@ -155,6 +159,10 @@ def initialize(message, location)
155
159
def deconstruct_keys ( keys )
156
160
{ message : message , location : location }
157
161
end
162
+
163
+ def inspect
164
+ "#<YARP::ParseError @message=#{ @message . inspect } @location=#{ @location . inspect } >"
165
+ end
158
166
end
159
167
160
168
# This represents a warning that was encountered during parsing.
@@ -169,6 +177,10 @@ def initialize(message, location)
169
177
def deconstruct_keys ( keys )
170
178
{ message : message , location : location }
171
179
end
180
+
181
+ def inspect
182
+ "#<YARP::ParseWarning @message=#{ @message . inspect } @location=#{ @location . inspect } >"
183
+ end
172
184
end
173
185
174
186
# A class that knows how to walk down the tree. None of the individual visit
@@ -323,7 +335,6 @@ def pretty_print(q)
323
335
q . nest ( 2 ) do
324
336
deconstructed = deconstruct_keys ( [ ] )
325
337
deconstructed . delete ( :location )
326
-
327
338
q . breakable ( "" )
328
339
q . seplist ( deconstructed , lambda { q . comma_breakable } , :each_value ) { |value | q . pp ( value ) }
329
340
end
@@ -333,6 +344,71 @@ def pretty_print(q)
333
344
end
334
345
end
335
346
347
+ # This object is responsible for generating the output for the inspect method
348
+ # implementations of child nodes.
349
+ class NodeInspector
350
+ attr_reader :prefix , :output
351
+
352
+ def initialize ( prefix = "" )
353
+ @prefix = prefix
354
+ @output = +""
355
+ end
356
+
357
+ # Appends a line to the output with the current prefix.
358
+ def <<( line )
359
+ output << "#{ prefix } #{ line } "
360
+ end
361
+
362
+ # This generates a string that is used as the header of the inspect output
363
+ # for any given node.
364
+ def header ( node )
365
+ output = +"@ #{ node . class . name . split ( "::" ) . last } ("
366
+ output << "location: (#{ node . location . start_offset } ...#{ node . location . end_offset } )"
367
+ output << ", newline: true" if node . newline?
368
+ output << ")\n "
369
+ output
370
+ end
371
+
372
+ # Generates a string that represents a list of nodes. It handles properly
373
+ # using the box drawing characters to make the output look nice.
374
+ def list ( prefix , nodes )
375
+ output = +"(length: #{ nodes . length } )\n "
376
+ last_index = nodes . length - 1
377
+
378
+ nodes . each_with_index do |node , index |
379
+ pointer , preadd = ( index == last_index ) ? [ "└── " , " " ] : [ "├── " , "│ " ]
380
+ node_prefix = "#{ prefix } #{ preadd } "
381
+ output << node . inspect ( NodeInspector . new ( node_prefix ) ) . sub ( node_prefix , "#{ prefix } #{ pointer } " )
382
+ end
383
+
384
+ output
385
+ end
386
+
387
+ # Generates a string that represents a location field on a node.
388
+ def location ( value )
389
+ if value
390
+ "(#{ value . start_offset } ...#{ value . end_offset } ) = #{ value . slice . inspect } "
391
+ else
392
+ "∅"
393
+ end
394
+ end
395
+
396
+ # Generates a string that represents a child node.
397
+ def child_node ( node , append )
398
+ node . inspect ( child_inspector ( append ) ) . delete_prefix ( prefix )
399
+ end
400
+
401
+ # Returns a new inspector that can be used to inspect a child node.
402
+ def child_inspector ( append )
403
+ NodeInspector . new ( "#{ prefix } #{ append } " )
404
+ end
405
+
406
+ # Returns the output as a string.
407
+ def to_str
408
+ output
409
+ end
410
+ end
411
+
336
412
class FloatNode < Node
337
413
def value
338
414
Float ( slice )
0 commit comments