Skip to content

Commit 2048fb8

Browse files
committed
Classify Operator Identifiers
1 parent a1079dd commit 2048fb8

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

Sources/SwiftSyntax/gyb_generated/SyntaxClassification.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public enum SyntaxClassification {
2323
case typeIdentifier
2424
/// An identifier starting with `$` like `$0`.
2525
case dollarIdentifier
26+
/// An identifier referring to an operator.
27+
case operatorIdentifier
2628
/// An integer literal.
2729
case integerLiteral
2830
/// A floating point literal.
@@ -41,7 +43,7 @@ public enum SyntaxClassification {
4143
case objectLiteral
4244
/// An editor placeholder of the form `<#content#>`
4345
case editorPlaceholder
44-
/// A line comment starting with `//`.
46+
/// A line comment starting with `//`.
4547
case lineComment
4648
/// A doc line comment starting with `///`.
4749
case docLineComment
@@ -87,6 +89,8 @@ extension SyntaxClassification {
8789
return (.typeIdentifier, false)
8890
case (.memberTypeIdentifier, 5):
8991
return (.typeIdentifier, false)
92+
case (.operatorDecl, 1):
93+
return (.operatorIdentifier, false)
9094
case (.constrainedSugarType, 1):
9195
return (.keyword, false)
9296
case (.availabilityVersionRestriction, 1):
@@ -343,9 +347,9 @@ extension RawTokenKind {
343347
case .identifier:
344348
return .identifier
345349
case .unspacedBinaryOperator:
346-
return .none
350+
return .operatorIdentifier
347351
case .spacedBinaryOperator:
348-
return .none
352+
return .operatorIdentifier
349353
case .postfixOperator:
350354
return .none
351355
case .prefixOperator:

Sources/lit-test-helper/ClassifiedSyntaxTreePrinter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extension SyntaxClassification {
2121
case .identifier: return "id"
2222
case .typeIdentifier: return "type"
2323
case .dollarIdentifier: return "dollar"
24+
case .operatorIdentifier: return "op"
2425
case .integerLiteral: return "int"
2526
case .floatingLiteral: return "float"
2627
case .stringLiteral: return "str"

Tests/SwiftParserTest/ClassificationTests.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,56 @@ public class ClassificationTests: XCTestCase {
121121
XCTAssertEqual(classif.kind, .typeIdentifier)
122122
}
123123
}
124+
125+
public func testOperatorTokenClassification() {
126+
do {
127+
let source = "let x: Int = 4 + 5 / 6"
128+
let tree = Parser.parse(source: source)
129+
130+
let tokens = Array(tree.tokens(viewMode: .sourceAccurate))
131+
XCTAssertEqual(tokens.count, 10)
132+
guard tokens.count == 10 else {
133+
return
134+
}
135+
let classif = tokens.map { $0.tokenClassification }
136+
XCTAssertEqual(classif[0].kind, .keyword)
137+
XCTAssertEqual(classif[0].range, ByteSourceRange(offset: 0, length: 3))
138+
XCTAssertEqual(classif[1].kind, .identifier)
139+
XCTAssertEqual(classif[1].range, ByteSourceRange(offset: 4, length: 1))
140+
XCTAssertEqual(classif[2].kind, .none)
141+
XCTAssertEqual(classif[2].range, ByteSourceRange(offset: 5, length: 1))
142+
XCTAssertEqual(classif[3].kind, .typeIdentifier)
143+
XCTAssertEqual(classif[3].range, ByteSourceRange(offset: 7, length: 3))
144+
XCTAssertEqual(classif[4].kind, .none)
145+
XCTAssertEqual(classif[4].range, ByteSourceRange(offset: 11, length: 1))
146+
XCTAssertEqual(classif[5].kind, .integerLiteral)
147+
XCTAssertEqual(classif[5].range, ByteSourceRange(offset: 13, length: 1))
148+
XCTAssertEqual(classif[6].kind, .operatorIdentifier)
149+
XCTAssertEqual(classif[6].range, ByteSourceRange(offset: 15, length: 1))
150+
XCTAssertEqual(classif[7].kind, .integerLiteral)
151+
XCTAssertEqual(classif[7].range, ByteSourceRange(offset: 17, length: 1))
152+
XCTAssertEqual(classif[8].kind, .operatorIdentifier)
153+
XCTAssertEqual(classif[8].range, ByteSourceRange(offset: 19, length: 1))
154+
XCTAssertEqual(classif[9].kind, .integerLiteral)
155+
XCTAssertEqual(classif[9].range, ByteSourceRange(offset: 21, length: 1))
156+
}
157+
158+
do {
159+
let source = "infix operator *--*"
160+
let tree = Parser.parse(source: source)
161+
162+
let tokens = Array(tree.tokens(viewMode: .sourceAccurate))
163+
XCTAssertEqual(tokens.count, 3)
164+
guard tokens.count == 3 else {
165+
return
166+
}
167+
let classif = tokens.map { $0.tokenClassification }
168+
XCTAssertEqual(classif[0].kind, .keyword)
169+
XCTAssertEqual(classif[0].range, ByteSourceRange(offset: 0, length: 5))
170+
XCTAssertEqual(classif[1].kind, .keyword)
171+
XCTAssertEqual(classif[1].range, ByteSourceRange(offset: 6, length: 8))
172+
XCTAssertEqual(classif[2].kind, .operatorIdentifier)
173+
XCTAssertEqual(classif[2].range, ByteSourceRange(offset: 15, length: 4))
174+
}
175+
}
124176
}

0 commit comments

Comments
 (0)