Skip to content

Commit f9d8e9f

Browse files
committed
Make LexemeSequence CustomDebugStringConvertible
1 parent b604a48 commit f9d8e9f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Sources/SwiftParser/Lexer.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public struct Lexer {
111111
extension Lexer {
112112
/// A sequence of ``Lexer/Lexeme`` tokens starting from a ``Lexer/Cursor``
113113
/// that points into an input buffer.
114-
public struct LexemeSequence: IteratorProtocol, Sequence {
114+
public struct LexemeSequence: IteratorProtocol, Sequence, CustomDebugStringConvertible {
115115
fileprivate let start: Lexer.Cursor
116116
fileprivate var cursor: Lexer.Cursor
117117
fileprivate var nextToken: Lexer.Lexeme
@@ -158,6 +158,15 @@ extension Lexer {
158158
func peek() -> Lexer.Lexeme {
159159
return self.nextToken
160160
}
161+
162+
public var debugDescription: String {
163+
let remainingText = self.nextToken.debugDescription + String(syntaxText: SyntaxText(baseAddress: self.cursor.input.baseAddress, count: self.cursor.input.count))
164+
if remainingText.count > 100 {
165+
return remainingText.prefix(100) + "..."
166+
} else {
167+
return remainingText
168+
}
169+
}
161170
}
162171

163172
@_spi(RawSyntax)

Sources/SwiftParser/SwiftParser.docc/FixingBugs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Guide to write test cases in the parser’s test suite and how to debug failures
44

55
The general approach to fixing bugs in the parser is to first write an automated test case that reproduces the test case in isolation. This allows you to invoke the parser with minimal dependencies and allows you to set breakpoints inside of it.
66

7-
Once you’ve written a test case (see below), set a breakpoint in `Parser.parseSourceFile` and navigate the debugger to the place where the parser behaves unexpectedly. While the debugger is stopped at an instance function in <doc:SwiftParser/Parser>, `po self.currentToken` can show you the next token that will be parsed.
7+
Once you’ve written a test case (see below), set a breakpoint in `Parser.parseSourceFile` and navigate the debugger to the place where the parser behaves unexpectedly. While the debugger is stopped at an instance function in <doc:SwiftParser/Parser>, `po self.currentToken` can show you the next token that will be parsed. `po self.lexemes` can show the next tokens that will be parsed.
88

99
## Round-Trip Failure or Parser Crash
1010

0 commit comments

Comments
 (0)