1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
import SwiftSyntaxBuilder
import SyntaxSupport
import Utils
func tokenCaseMatch(
_ caseName: TokenSyntax,
experimentalFeature: ExperimentalFeature?
) -> SwitchCaseSyntax {
var whereClause = ""
if let feature = experimentalFeature {
whereClause += "where experimentalFeatures.contains(.\(feature.token))"
}
return "case TokenSpec(.\(caseName))\(raw: whereClause): self = .\(caseName)"
}
let parserTokenSpecSetFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax(
"""
#if swift(>=6)
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) public import SwiftSyntax
#else
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
#endif
"""
)
for layoutNode in SYNTAX_NODES.compactMap(\.layoutNode) {
for child in layoutNode.children {
if case let .token(choices, _, _) = child.kind, choices.count > 1 {
try! ExtensionDeclSyntax("extension \(layoutNode.kind.syntaxType)") {
try EnumDeclSyntax(
"""
@_spi(Diagnostics)
public enum \(child.tokenSpecSetType): TokenSpecSet
"""
) {
for choice in choices {
switch choice {
case .keyword(let keyword):
DeclSyntax(
"""
\(keyword.spec.apiAttributes)\
case \(keyword.spec.varOrCaseName.backtickedIfNeeded)
"""
)
case .token(let token):
DeclSyntax("case \(token.spec.varOrCaseName)")
}
}
try InitializerDeclSyntax(
"init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures)"
) {
try SwitchExprSyntax("switch PrepareForKeywordMatch(lexeme)") {
for choice in choices {
switch choice {
case .keyword(let keyword):
tokenCaseMatch(
keyword.spec.varOrCaseName,
experimentalFeature: keyword.spec.experimentalFeature
)
case .token(let token):
tokenCaseMatch(
token.spec.varOrCaseName,
experimentalFeature: token.spec.experimentalFeature
)
}
}
SwitchCaseSyntax("default: return nil")
}
}
try InitializerDeclSyntax("public init?(token: TokenSyntax)") {
try SwitchExprSyntax("switch token") {
for choice in choices {
SwitchCaseSyntax(
"case TokenSpec(.\(choice.varOrCaseName)): self = .\(choice.varOrCaseName)"
)
}
SwitchCaseSyntax("default: return nil")
}
}
try VariableDeclSyntax("var spec: TokenSpec") {
try SwitchExprSyntax("switch self") {
for choice in choices {
switch choice {
case .keyword(let keyword):
let caseName = keyword.spec.varOrCaseName
SwitchCaseSyntax("case .\(caseName): return .keyword(.\(caseName))")
case .token(let token):
let caseName = token.spec.varOrCaseName
SwitchCaseSyntax("case .\(caseName): return .\(caseName)")
}
}
}
}
try VariableDeclSyntax(
"""
/// Returns a token that satisfies the `TokenSpec` of this case.
///
/// If the token kind of this spec has variable text, e.g. for an identifier, this returns a token with empty text.
@_spi(Diagnostics)
public var tokenSyntax: TokenSyntax
"""
) {
try SwitchExprSyntax("switch self") {
for choice in choices {
switch choice {
case .keyword(let keyword):
let caseName = keyword.spec.varOrCaseName
SwitchCaseSyntax("case .\(caseName): return .keyword(.\(caseName))")
case .token(let token):
let caseName = token.spec.varOrCaseName
if token.spec.text != nil {
SwitchCaseSyntax("case .\(caseName): return .\(caseName)Token()")
} else {
SwitchCaseSyntax(#"case .\#(caseName): return .\#(caseName)("")"#)
}
}
}
}
}
}
}
}
}
}
}
|