Skip to content

Commit f17ec89

Browse files
committed
Add diagnostic message for identifier editor placeholder
1 parent 39b3336 commit f17ec89

File tree

8 files changed

+309
-279
lines changed

8 files changed

+309
-279
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,7 @@ extension Lexer.Cursor {
20442044
extension Lexer.Cursor {
20452045
mutating func tryLexEditorPlaceholder(sourceBufferStart: Lexer.Cursor) -> Lexer.Result {
20462046
precondition(self.is(at: "<") && self.is(offset: 1, at: "#"))
2047+
let start = self
20472048
var ptr = self
20482049
let leftAngleConsumed = ptr.advance(matching: "<")
20492050
let poundConsumed = ptr.advance(matching: "#")
@@ -2058,7 +2059,10 @@ extension Lexer.Cursor {
20582059
let closingAngleConsumed = ptr.advance(matching: ">")
20592060
precondition(closingAngleConsumed)
20602061
self = ptr
2061-
return Lexer.Result(.identifier)
2062+
return Lexer.Result(
2063+
.identifier,
2064+
error: LexingDiagnostic(.editorPlaceholder, position: start)
2065+
)
20622066
default:
20632067
break
20642068
}

Sources/SwiftParserDiagnostics/LexerDiagnosticMessages.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public extension TokenError {
3838

3939
/// Please order the cases in this enum alphabetically by case name.
4040
public enum StaticTokenError: String, DiagnosticMessage {
41+
case editorPlaceholder = "editor placeholder in source file"
4142
case expectedBinaryExponentInHexFloatLiteral = "hexadecimal floating point literal must end with an exponent"
4243
case expectedClosingBraceInUnicodeEscape = #"expected '}' in \u{...} escape sequence"#
4344
case expectedDigitInFloatLiteral = "expected a digit in floating point exponent"
@@ -132,6 +133,7 @@ public extension SwiftSyntax.TokenDiagnostic {
132133
}
133134

134135
switch self.kind {
136+
case .editorPlaceholder: return StaticTokenError.editorPlaceholder
135137
case .expectedBinaryExponentInHexFloatLiteral: return StaticTokenError.expectedBinaryExponentInHexFloatLiteral
136138
case .expectedClosingBraceInUnicodeEscape: return StaticTokenError.expectedClosingBraceInUnicodeEscape
137139
case .expectedDigitInFloatLiteral: return StaticTokenError.expectedDigitInFloatLiteral

Sources/SwiftSyntax/TokenDiagnostic.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public struct TokenDiagnostic: Hashable {
2222
public enum Kind {
2323
// Please order these alphabetically
2424

25+
case editorPlaceholder
2526
case expectedBinaryExponentInHexFloatLiteral
2627
case expectedClosingBraceInUnicodeEscape
2728
case expectedDigitInFloatLiteral
@@ -94,6 +95,7 @@ public struct TokenDiagnostic: Hashable {
9495

9596
public var severity: Severity {
9697
switch kind {
98+
case .editorPlaceholder: return .error
9799
case .expectedBinaryExponentInHexFloatLiteral: return .error
98100
case .expectedClosingBraceInUnicodeEscape: return .error
99101
case .expectedDigitInFloatLiteral: return .error

Tests/SwiftParserTest/Assertions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private func assertTokens(
8787
var lexemeStartOffset = 0
8888
for (actualLexeme, expectedLexeme) in zip(actual, expected) {
8989
defer {
90-
lexemeStartOffset = actualLexeme.byteLength
90+
lexemeStartOffset += actualLexeme.byteLength
9191
}
9292
if actualLexeme.rawTokenKind != expectedLexeme.rawTokenKind {
9393
XCTFail(

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,15 @@ final class DeclarationTests: XCTestCase {
13791379
assertParse(
13801380
"""
13811381
class Foo {
1382-
<#code#>
1382+
1️⃣<#code#>
13831383
}
13841384
""",
13851385
substructure: Syntax(
13861386
MemberDeclListItemSyntax(decl: EditorPlaceholderDeclSyntax(identifier: .identifier("<#code#>")))
1387-
)
1387+
),
1388+
diagnostics: [
1389+
DiagnosticSpec(message: "editor placeholder in source file")
1390+
]
13881391
)
13891392
}
13901393

0 commit comments

Comments
 (0)