@@ -68,6 +68,9 @@ public class PrettyPrinter {
68
68
/// breaks occur in the formatted output.
69
69
private var isDebugMode : Bool
70
70
71
+ /// If true, the token stream is printed to the console for debugging purposes.
72
+ private var printTokenStream : Bool
73
+
71
74
/// The current index (0..<6) of the color to be used for the next color group.
72
75
private var currentDebugGroupMarkerColor = 0
73
76
@@ -84,12 +87,13 @@ public class PrettyPrinter {
84
87
/// - Parameters:
85
88
/// - configuration: The configuration used to decide whitespace or breaking behavior.
86
89
/// - node: The node to be pretty printed.
87
- public init ( configuration: Configuration , node: Syntax , isDebugMode: Bool ) {
90
+ public init ( configuration: Configuration , node: Syntax , isDebugMode: Bool , printTokenStream : Bool ) {
88
91
self . configuration = configuration
89
92
self . tokens = node. makeTokenStream ( configuration: configuration)
90
93
self . maxLineLength = configuration. lineLength
91
94
self . isDebugMode = isDebugMode
92
95
self . spaceRemaining = self . maxLineLength
96
+ self . printTokenStream = printTokenStream
93
97
}
94
98
95
99
/// Append the input string to the output buffer
@@ -107,6 +111,9 @@ public class PrettyPrinter {
107
111
/// - token: The token to be printed.
108
112
/// - length: The length of the token (number of columns).
109
113
private func printToken( token: Token , length: Int ) {
114
+ if self . printTokenStream {
115
+ printDebugToken ( token: token, length: length)
116
+ }
110
117
assert ( length >= 0 , " Token lengths must be positive " )
111
118
switch token {
112
119
@@ -415,6 +422,73 @@ public class PrettyPrinter {
415
422
write ( Ansi . reset)
416
423
}
417
424
425
+ /// Used to track the indentation level for the debug token stream output
426
+ var debugIndent : Int = 0
427
+
428
+ /// Print out the token stream to the console for debugging.
429
+ ///
430
+ /// Indentation is applied to make identification of groups easier.
431
+ private func printDebugToken( token: Token , length: Int ) {
432
+ func printDebugIndent( ) {
433
+ print ( String ( repeating: " " , count: debugIndent) , terminator: " " )
434
+ }
435
+ switch token {
436
+ case . syntax( let syntax) :
437
+ printDebugIndent ( )
438
+ print ( " [SYNTAX \" \( syntax. text) \" Length: \( length) ] " )
439
+
440
+ case . break( let size, let offset) :
441
+ printDebugIndent ( )
442
+ print ( " [BREAK Size: \( size) Offset: \( offset) Length: \( length) ] " )
443
+
444
+ case . open( let breakstyle, let offset) :
445
+ printDebugIndent ( )
446
+ switch breakstyle {
447
+ case . consistent:
448
+ print ( " [OPEN Consistent Offset: \( offset) Length: \( length) ] " )
449
+ case . inconsistent:
450
+ print ( " [OPEN Inconsistent Offset: \( offset) Length: \( length) ] " )
451
+ }
452
+ debugIndent += 2
453
+
454
+ case . close:
455
+ debugIndent -= 2
456
+ printDebugIndent ( )
457
+ print ( " [CLOSE] " )
458
+
459
+ case . newlines( let N, let offset) :
460
+ printDebugIndent ( )
461
+ print ( " [NEWLINES N: \( N) Offset: \( offset) Length: \( length) ] " )
462
+
463
+ case . space( let size) :
464
+ printDebugIndent ( )
465
+ print ( " [SPACE Size: \( size) Length: \( length) ] " )
466
+
467
+ case . reset:
468
+ print ( " [RESET] " )
469
+
470
+ case . comment( let comment) :
471
+ printDebugIndent ( )
472
+ switch comment. kind {
473
+ case . line:
474
+ print ( " [COMMENT Line Length: \( length) ] " )
475
+ case . docLine:
476
+ print ( " [COMMENT DocLine Length: \( length) ] " )
477
+ case . block:
478
+ print ( " [COMMENT Block Length: \( length) ] " )
479
+ case . docBlock:
480
+ print ( " [COMMENT DocBlock Length: \( length) ] " )
481
+ }
482
+ printDebugIndent ( )
483
+ print ( comment. print ( indent: debugIndent) )
484
+
485
+ case . verbatim( let verbatim) :
486
+ printDebugIndent ( )
487
+ print ( " [VERBATIM Length: \( length) ] " )
488
+ print ( verbatim. print ( indent: debugIndent) )
489
+ }
490
+ }
491
+
418
492
/// Writes the given number of spaces to the output.
419
493
///
420
494
/// If debug mode is enabled, spaces are rendered as gray Unicode MIDDLE DOT characters.
0 commit comments