Skip to content

Commit 87d4a8e

Browse files
authored
Merge pull request #758 from ahoppen/ahoppen/debug-descriptions
Add debug descriptions for `Lexeme` and `LexemeSequence`
2 parents 2a9f941 + f9d8e9f commit 87d4a8e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Sources/SwiftParser/Lexer.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct Lexer {
2222
/// A lexeme is the fundamental output unit of lexical analysis. Each lexeme
2323
/// represents a fully identified, meaningful part of the input text that
2424
/// will can be consumed by a ``Parser``.
25-
public struct Lexeme {
25+
public struct Lexeme: CustomDebugStringConvertible {
2626
public struct Flags: OptionSet {
2727
public var rawValue: UInt8
2828

@@ -101,13 +101,17 @@ public struct Lexer {
101101
SyntaxText(baseAddress: start.advanced(by: leadingTriviaByteLength+textByteLength),
102102
count: trailingTriviaByteLength)
103103
}
104+
105+
public var debugDescription: String {
106+
return String(syntaxText: SyntaxText(baseAddress: start, count: byteLength))
107+
}
104108
}
105109
}
106110

107111
extension Lexer {
108112
/// A sequence of ``Lexer/Lexeme`` tokens starting from a ``Lexer/Cursor``
109113
/// that points into an input buffer.
110-
public struct LexemeSequence: IteratorProtocol, Sequence {
114+
public struct LexemeSequence: IteratorProtocol, Sequence, CustomDebugStringConvertible {
111115
fileprivate let start: Lexer.Cursor
112116
fileprivate var cursor: Lexer.Cursor
113117
fileprivate var nextToken: Lexer.Lexeme
@@ -154,6 +158,15 @@ extension Lexer {
154158
func peek() -> Lexer.Lexeme {
155159
return self.nextToken
156160
}
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+
}
157170
}
158171

159172
@_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)