Skip to content

Commit 5545afb

Browse files
authored
Merge pull request #1224 from ahoppen/ahoppen/remap-consume-ifany
Remap identifiers to keywords in `consume(ifAny:)`
2 parents e09c64d + 4617bc2 commit 5545afb

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,17 @@ extension TokenConsumer {
181181
ifAny kinds: [RawTokenKind],
182182
allowTokenAtStartOfLine: Bool = true
183183
) -> Token? {
184-
if self.at(any: kinds, allowTokenAtStartOfLine: allowTokenAtStartOfLine) {
185-
return self.consumeAnyToken()
184+
if !allowTokenAtStartOfLine && self.currentToken.isAtStartOfLine {
185+
return nil
186+
}
187+
for kind in kinds {
188+
if case RawTokenKindMatch(kind) = self.currentToken {
189+
if case .keyword = kind {
190+
return self.consumeAnyToken(remapping: kind)
191+
} else {
192+
return self.consumeAnyToken()
193+
}
194+
}
186195
}
187196
return nil
188197
}

Sources/_SwiftSyntaxTestSupport/SyntaxProtocol+Initializer.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ private class InitializerExprFormat: BasicFormat {
3939
override func visit(_ node: TupleExprElementListSyntax) -> TupleExprElementListSyntax {
4040
let children = node.children(viewMode: .all)
4141
// If the function only takes a single argument, display it on the same line
42+
if let callee = node.parent?.as(FunctionCallExprSyntax.self)?.calledExpression.as(MemberAccessExprSyntax.self), callee.base == nil {
43+
// This is a constructor for tokens. Write them on a single line
44+
return super.visit(node)
45+
}
4246
if children.count > 1 {
4347
return TupleExprElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: TupleExprElementSyntax.self))
4448
} else {
@@ -127,24 +131,25 @@ extension SyntaxProtocol {
127131
} else if let token = Syntax(self).as(TokenSyntax.self) {
128132
let tokenKind = token.tokenKind
129133
let tokenInitializerName: String
130-
let requiresExplicitText: Bool
134+
let tokenKindArgument: ExprSyntax?
131135
if tokenKind.isLexerClassifiedKeyword || tokenKind == .eof {
132136
tokenInitializerName = String(describing: tokenKind)
133-
requiresExplicitText = false
137+
tokenKindArgument = nil
138+
} else if case .keyword(let keyword) = tokenKind {
139+
tokenInitializerName = "keyword"
140+
tokenKindArgument = ExprSyntax(MemberAccessExprSyntax(name: String(describing: keyword)))
134141
} else if tokenKind.decomposeToRaw().rawKind.defaultText != nil {
135142
tokenInitializerName = "\(String(describing: tokenKind))Token"
136-
requiresExplicitText = false
143+
tokenKindArgument = nil
137144
} else {
138145
let tokenKindStr = String(describing: tokenKind)
139146
tokenInitializerName = String(tokenKindStr[..<tokenKindStr.firstIndex(of: "(")!])
140-
requiresExplicitText = true
147+
tokenKindArgument = ExprSyntax(StringLiteralExprSyntax(content: token.text))
141148
}
142149
return ExprSyntax(
143150
FunctionCallExprSyntax(callee: MemberAccessExprSyntax(name: tokenInitializerName)) {
144-
if requiresExplicitText {
145-
TupleExprElementSyntax(
146-
expression: StringLiteralExprSyntax(content: token.text)
147-
)
151+
if let tokenKindArgument = tokenKindArgument {
152+
TupleExprElementSyntax(expression: tokenKindArgument)
148153
}
149154
if includeTrivia && !token.leadingTrivia.isEmpty {
150155
TupleExprElementSyntax(

0 commit comments

Comments
 (0)