Skip to content

Commit a19ab8b

Browse files
authored
Merge pull request #1320 from ahoppen/ahoppen/rename-lexererror
Rename `LexerError` -> `TokenDiagnostic`
2 parents c6f709c + 9b7605c commit a19ab8b

19 files changed

+219
-219
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

Lines changed: 48 additions & 48 deletions
Large diffs are not rendered by default.

Sources/SwiftParser/Lexer/Lexeme.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension Lexer {
4040
@_spi(RawSyntax)
4141
public var rawTokenKind: RawTokenKind
4242
public var flags: Lexeme.Flags
43-
public var error: LexerError?
43+
public var diagnostic: TokenDiagnostic?
4444
var start: UnsafePointer<UInt8>
4545
public var leadingTriviaByteLength: Int
4646
public var textByteLength: Int
@@ -60,7 +60,7 @@ extension Lexer {
6060
init(
6161
tokenKind: RawTokenKind,
6262
flags: Flags,
63-
error: LexerError?,
63+
diagnostic: TokenDiagnostic?,
6464
start: UnsafePointer<UInt8>,
6565
leadingTriviaLength: Int,
6666
textLength: Int,
@@ -69,7 +69,7 @@ extension Lexer {
6969
) {
7070
self.rawTokenKind = tokenKind
7171
self.flags = flags
72-
self.error = error
72+
self.diagnostic = diagnostic
7373
self.start = start
7474
self.leadingTriviaByteLength = leadingTriviaLength
7575
self.textByteLength = textLength

Sources/SwiftParser/Lexer/LexemeSequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension Lexer {
4747
self.nextToken = Lexeme(
4848
tokenKind: .eof,
4949
flags: [],
50-
error: nil,
50+
diagnostic: nil,
5151
start: self.cursor.pointer,
5252
leadingTriviaLength: 0,
5353
textLength: 0,

Sources/SwiftParser/Parser.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public struct Parser {
187187
wholeText: tok.wholeText,
188188
textRange: tok.textRange,
189189
presence: .present,
190-
lexerError: tok.error,
190+
tokenDiagnostic: tok.diagnostic,
191191
arena: arena
192192
)
193193
}
@@ -511,18 +511,18 @@ extension Parser {
511511
assert(tokenText.hasPrefix(prefix))
512512

513513
let endIndex = current.textRange.lowerBound.advanced(by: prefix.count)
514-
var lexerError = current.error
515-
if let error = lexerError, error.byteOffset > prefix.count + current.leadingTriviaByteLength {
514+
var tokenDiagnostic = current.diagnostic
515+
if let error = tokenDiagnostic, error.byteOffset > prefix.count + current.leadingTriviaByteLength {
516516
// The lexer error isn't in the prefix. Drop it.
517-
lexerError = nil
517+
tokenDiagnostic = nil
518518
}
519519

520520
let tok = RawTokenSyntax(
521521
kind: tokenKind,
522522
wholeText: SyntaxText(rebasing: current.wholeText[..<endIndex]),
523523
textRange: current.textRange.lowerBound..<endIndex,
524524
presence: .present,
525-
lexerError: lexerError,
525+
tokenDiagnostic: tokenDiagnostic,
526526
arena: self.arena
527527
)
528528

Sources/SwiftParser/StringLiterals.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ fileprivate class StringLiteralExpressionIndentationChecker {
5555
if hasSufficientIndentation {
5656
return nil
5757
}
58-
if token.tokenView.lexerError != nil {
58+
if token.tokenView.tokenDiagnostic != nil {
5959
// Token already has a lexer error, ignore the indentation error until that
6060
// error is fixed
6161
return nil
6262
}
63-
return token.tokenView.withLexerError(
64-
lexerError: LexerError(.insufficientIndentationInMultilineStringLiteral, byteOffset: 0),
63+
return token.tokenView.withTokenDiagnostic(
64+
tokenDiagnostic: TokenDiagnostic(.insufficientIndentationInMultilineStringLiteral, byteOffset: 0),
6565
arena: arena
6666
)
6767
}
@@ -136,7 +136,7 @@ extension Parser {
136136
in token: RawTokenSyntax,
137137
leading reclassifyLeading: SyntaxText = "",
138138
trailing reclassifyTrailing: SyntaxText = "",
139-
lexerError: LexerError? = nil
139+
tokenDiagnostic: TokenDiagnostic? = nil
140140
) -> RawTokenSyntax {
141141
assert(SyntaxText(rebasing: token.tokenText.prefix(reclassifyLeading.count)) == reclassifyLeading)
142142
assert(SyntaxText(rebasing: token.tokenText.suffix(reclassifyTrailing.count)) == reclassifyTrailing)
@@ -146,7 +146,7 @@ extension Parser {
146146
leadingTriviaPieces: token.leadingTriviaPieces + TriviaParser.parseTrivia(reclassifyLeading, position: .trailing),
147147
trailingTriviaPieces: TriviaParser.parseTrivia(reclassifyTrailing, position: .trailing) + token.trailingTriviaPieces,
148148
presence: token.presence,
149-
lexerError: token.tokenView.lexerError ?? lexerError,
149+
tokenDiagnostic: token.tokenView.tokenDiagnostic ?? tokenDiagnostic,
150150
arena: self.arena
151151
)
152152
}
@@ -253,8 +253,8 @@ extension Parser {
253253
// Empty lines don't need to be indented and there's no indentation we need to strip away.
254254
} else {
255255
let actualIndentation = SyntaxText(rebasing: segment.content.tokenText.prefix(while: { $0 == UInt8(ascii: " ") || $0 == UInt8(ascii: "\t") }))
256-
let lexerError = LexerError(.insufficientIndentationInMultilineStringLiteral, byteOffset: 0)
257-
let content = self.reclassifyTrivia(in: segment.content, leading: actualIndentation, lexerError: lexerError)
256+
let tokenDiagnostic = TokenDiagnostic(.insufficientIndentationInMultilineStringLiteral, byteOffset: 0)
257+
let content = self.reclassifyTrivia(in: segment.content, leading: actualIndentation, tokenDiagnostic: tokenDiagnostic)
258258
segment = RawStringSegmentSyntax(
259259
segment.unexpectedBeforeContent,
260260
content: content,
@@ -372,7 +372,7 @@ extension Parser {
372372
leadingTriviaPieces: parsedTrivia + closeQuote.leadingTriviaPieces,
373373
trailingTriviaPieces: closeQuote.trailingTriviaPieces,
374374
presence: closeQuote.presence,
375-
lexerError: closeQuote.tokenView.lexerError,
375+
tokenDiagnostic: closeQuote.tokenView.tokenDiagnostic,
376376
arena: self.arena
377377
)
378378
} else {

Sources/SwiftParserDiagnostics/LexerDiagnosticMessages.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import SwiftDiagnostics
1616
fileprivate let diagnosticDomain: String = "SwiftLexer"
1717

1818
/// A error diagnostic whose ID is determined by the diagnostic's type.
19-
public protocol LexerError: DiagnosticMessage {
19+
public protocol TokenError: DiagnosticMessage {
2020
var diagnosticID: MessageID { get }
2121
}
2222

23-
public extension LexerError {
23+
public extension TokenError {
2424
static var diagnosticID: MessageID {
2525
return MessageID(domain: diagnosticDomain, id: "\(self)")
2626
}
@@ -37,7 +37,7 @@ public extension LexerError {
3737
// MARK: - Errors (please sort alphabetically)
3838

3939
/// Please order the cases in this enum alphabetically by case name.
40-
public enum StaticLexerError: String, DiagnosticMessage {
40+
public enum StaticTokenError: String, DiagnosticMessage {
4141
case expectedBinaryExponentInHexFloatLiteral = "hexadecimal floating point literal must end with an exponent"
4242
case expectedClosingBraceInUnicodeEscape = #"expected '}' in \u{...} escape sequence"#
4343
case expectedDigitInFloatLiteral = "expected a digit in floating point exponent"
@@ -48,7 +48,7 @@ public enum StaticLexerError: String, DiagnosticMessage {
4848
case invalidIdentifierStartCharacter = "an identifier cannot begin with this character"
4949
case invalidNumberOfHexDigitsInUnicodeEscape = #"\u{...} escape sequence expects between 1 and 8 hex digits"#
5050
case invalidUtf8 = "invalid UTF-8 found in source file"
51-
case lexerErrorOffsetOverflow = "the lexer dicovered an error in this token but was not able to represent its offset due to overflow; please split the token"
51+
case tokenDiagnosticOffsetOverflow = "the lexer dicovered an error in this token but was not able to represent its offset due to overflow; please split the token"
5252
case sourceConflictMarker = "source control conflict marker in source file"
5353
case unexpectedBlockCommentEnd = "unexpected end of block comment"
5454
case unicodeCurlyQuote = #"unicode curly quote found; use '"' instead"#
@@ -64,7 +64,7 @@ public enum StaticLexerError: String, DiagnosticMessage {
6464
}
6565

6666
/// Please order the cases in this enum alphabetically by case name.
67-
public enum StaticLexerWarning: String, DiagnosticMessage {
67+
public enum StaticTokenWarning: String, DiagnosticMessage {
6868
case nonBreakingSpace = "non-breaking space (U+00A0) used instead of regular space"
6969
case nulCharacter = "nul character embedded in middle of file"
7070

@@ -77,7 +77,7 @@ public enum StaticLexerWarning: String, DiagnosticMessage {
7777
public var severity: DiagnosticSeverity { .warning }
7878
}
7979

80-
public struct InvalidFloatingPointExponentDigit: LexerError {
80+
public struct InvalidFloatingPointExponentDigit: TokenError {
8181
public enum Kind {
8282
case digit(Unicode.Scalar)
8383
case character(Unicode.Scalar)
@@ -94,7 +94,7 @@ public struct InvalidFloatingPointExponentDigit: LexerError {
9494
}
9595
}
9696

97-
public struct InvalidDigitInIntegerLiteral: LexerError {
97+
public struct InvalidDigitInIntegerLiteral: TokenError {
9898
public enum Kind {
9999
case binary(Unicode.Scalar)
100100
case octal(Unicode.Scalar)
@@ -118,10 +118,10 @@ public struct InvalidDigitInIntegerLiteral: LexerError {
118118
}
119119
}
120120

121-
// MARK: - Convert LexerError from SwiftSyntax to error messages
121+
// MARK: - Convert TokenDiagnostic from SwiftSyntax to error messages
122122

123-
public extension SwiftSyntax.LexerError {
124-
/// `tokenText` is the entire text of the token in which the `LexerError`
123+
public extension SwiftSyntax.TokenDiagnostic {
124+
/// `tokenText` is the entire text of the token in which the `TokenDiagnostic`
125125
/// occurred, including trivia.
126126
@_spi(RawSyntax)
127127
func diagnosticMessage(wholeTextBytes: [UInt8]) -> DiagnosticMessage {
@@ -132,34 +132,34 @@ public extension SwiftSyntax.LexerError {
132132
}
133133

134134
switch self.kind {
135-
case .expectedBinaryExponentInHexFloatLiteral: return StaticLexerError.expectedBinaryExponentInHexFloatLiteral
136-
case .expectedClosingBraceInUnicodeEscape: return StaticLexerError.expectedClosingBraceInUnicodeEscape
137-
case .expectedDigitInFloatLiteral: return StaticLexerError.expectedDigitInFloatLiteral
138-
case .expectedHexCodeInUnicodeEscape: return StaticLexerError.expectedHexCodeInUnicodeEscape
139-
case .expectedHexDigitInHexLiteral: return StaticLexerError.expectedHexDigitInHexLiteral
135+
case .expectedBinaryExponentInHexFloatLiteral: return StaticTokenError.expectedBinaryExponentInHexFloatLiteral
136+
case .expectedClosingBraceInUnicodeEscape: return StaticTokenError.expectedClosingBraceInUnicodeEscape
137+
case .expectedDigitInFloatLiteral: return StaticTokenError.expectedDigitInFloatLiteral
138+
case .expectedHexCodeInUnicodeEscape: return StaticTokenError.expectedHexCodeInUnicodeEscape
139+
case .expectedHexDigitInHexLiteral: return StaticTokenError.expectedHexDigitInHexLiteral
140140
case .insufficientIndentationInMultilineStringLiteral:
141141
// This should be diagnosed when visiting the `StringLiteralExprSyntax`
142142
// inside `ParseDiagnosticsGenerator` but fall back to an error message
143143
// here in case the error is not diagnosed.
144144
return InvalidIndentationInMultiLineStringLiteralError(kind: .insufficientIndentation, lines: 1)
145145
case .invalidBinaryDigitInIntegerLiteral: return InvalidDigitInIntegerLiteral(kind: .binary(scalarAtErrorOffset))
146-
case .invalidCharacter: return StaticLexerError.invalidCharacter
146+
case .invalidCharacter: return StaticTokenError.invalidCharacter
147147
case .invalidDecimalDigitInIntegerLiteral: return InvalidDigitInIntegerLiteral(kind: .decimal(scalarAtErrorOffset))
148-
case .invalidEscapeSequenceInStringLiteral: return StaticLexerError.invalidEscapeSequenceInStringLiteral
148+
case .invalidEscapeSequenceInStringLiteral: return StaticTokenError.invalidEscapeSequenceInStringLiteral
149149
case .invalidFloatingPointExponentCharacter: return InvalidFloatingPointExponentDigit(kind: .character(scalarAtErrorOffset))
150150
case .invalidFloatingPointExponentDigit: return InvalidFloatingPointExponentDigit(kind: .digit(scalarAtErrorOffset))
151151
case .invalidHexDigitInIntegerLiteral: return InvalidDigitInIntegerLiteral(kind: .hex(scalarAtErrorOffset))
152-
case .invalidIdentifierStartCharacter: return StaticLexerError.invalidIdentifierStartCharacter
153-
case .invalidNumberOfHexDigitsInUnicodeEscape: return StaticLexerError.invalidNumberOfHexDigitsInUnicodeEscape
152+
case .invalidIdentifierStartCharacter: return StaticTokenError.invalidIdentifierStartCharacter
153+
case .invalidNumberOfHexDigitsInUnicodeEscape: return StaticTokenError.invalidNumberOfHexDigitsInUnicodeEscape
154154
case .invalidOctalDigitInIntegerLiteral: return InvalidDigitInIntegerLiteral(kind: .octal(scalarAtErrorOffset))
155-
case .invalidUtf8: return StaticLexerError.invalidUtf8
156-
case .lexerErrorOffsetOverflow: return StaticLexerError.lexerErrorOffsetOverflow
157-
case .nonBreakingSpace: return StaticLexerWarning.nonBreakingSpace
158-
case .nulCharacter: return StaticLexerWarning.nulCharacter
159-
case .sourceConflictMarker: return StaticLexerError.sourceConflictMarker
160-
case .unexpectedBlockCommentEnd: return StaticLexerError.unexpectedBlockCommentEnd
161-
case .unicodeCurlyQuote: return StaticLexerError.unicodeCurlyQuote
162-
case .unprintableAsciiCharacter: return StaticLexerError.unprintableAsciiCharacter
155+
case .invalidUtf8: return StaticTokenError.invalidUtf8
156+
case .tokenDiagnosticOffsetOverflow: return StaticTokenError.tokenDiagnosticOffsetOverflow
157+
case .nonBreakingSpace: return StaticTokenWarning.nonBreakingSpace
158+
case .nulCharacter: return StaticTokenWarning.nulCharacter
159+
case .sourceConflictMarker: return StaticTokenError.sourceConflictMarker
160+
case .unexpectedBlockCommentEnd: return StaticTokenError.unexpectedBlockCommentEnd
161+
case .unicodeCurlyQuote: return StaticTokenError.unicodeCurlyQuote
162+
case .unprintableAsciiCharacter: return StaticTokenError.unprintableAsciiCharacter
163163
}
164164
}
165165

Sources/SwiftParserDiagnostics/MultiLineStringLiteralDiagnoticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ final class MultiLineStringLiteralIndentatinDiagnosticsGenerator: SyntaxVisitor
144144
return .visitChildren
145145
}
146146

147-
if token.lexerError?.kind == .insufficientIndentationInMultilineStringLiteral {
147+
if token.tokenDiagnostic?.kind == .insufficientIndentationInMultilineStringLiteral {
148148
addIncorrectlyIndentedToken(token: token)
149149
} else {
150150
finishInProgressDiagnostic()

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fileprivate extension TokenSyntax {
3131
}
3232

3333
fileprivate extension DiagnosticSeverity {
34-
func matches(_ lexerErorSeverity: SwiftSyntax.LexerError.Severity) -> Bool {
34+
func matches(_ lexerErorSeverity: SwiftSyntax.TokenDiagnostic.Severity) -> Bool {
3535
switch (self, lexerErorSeverity) {
3636
case (.error, .error):
3737
return true
@@ -359,14 +359,14 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
359359
if token.presence == .missing {
360360
handleMissingToken(token)
361361
} else {
362-
if let lexerError = token.lexerError {
363-
let message = lexerError.diagnosticMessage(in: token)
364-
assert(message.severity.matches(lexerError.severity))
362+
if let tokenDiagnostic = token.tokenDiagnostic {
363+
let message = tokenDiagnostic.diagnosticMessage(in: token)
364+
assert(message.severity.matches(tokenDiagnostic.severity))
365365
self.addDiagnostic(
366366
token,
367-
position: token.position.advanced(by: Int(lexerError.byteOffset)),
367+
position: token.position.advanced(by: Int(tokenDiagnostic.byteOffset)),
368368
message,
369-
fixIts: lexerError.fixIts(in: token)
369+
fixIts: tokenDiagnostic.fixIts(in: token)
370370
)
371371
}
372372
}

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ add_swift_host_library(SwiftSyntax
1111
BumpPtrAllocator.swift
1212
CommonAncestor.swift
1313
IncrementalParseTransition.swift
14-
LexerError.swift
1514
SourceLength.swift
1615
SourceLocation.swift
1716
SourcePresence.swift
@@ -22,6 +21,7 @@ add_swift_host_library(SwiftSyntax
2221
SyntaxOtherNodes.swift
2322
SyntaxText.swift
2423
SyntaxTreeViewMode.swift
24+
TokenDiagnostic.swift
2525
Utils.swift
2626

2727
Raw/RawSyntax.swift

0 commit comments

Comments
 (0)