|
10 | 10 | // |
11 | 11 | //===----------------------------------------------------------------------===// |
12 | 12 |
|
| 13 | +import SwiftSyntax |
| 14 | + |
13 | 15 | /// Represents the specification for a Token in the TokenSyntax file. |
14 | | -public class TokenSpec { |
15 | | - public let name: String |
| 16 | +public struct TokenSpec { |
| 17 | + public enum Kind { |
| 18 | + case punctuation |
| 19 | + /// The `keyword` TokenKind that contains the actual keyword as an associated value |
| 20 | + case keyword |
| 21 | + case other |
| 22 | + } |
| 23 | + |
| 24 | + public let varOrCaseName: TokenSyntax |
16 | 25 | public let nameForDiagnostics: String |
17 | 26 | public let text: String? |
18 | | - public let associatedValueClass: String? |
19 | | - |
20 | | - public var swiftKind: String { |
21 | | - return lowercaseFirstWord(name: self.name) |
22 | | - } |
| 27 | + public let kind: Kind |
23 | 28 |
|
24 | | - init( |
| 29 | + fileprivate init( |
25 | 30 | name: String, |
26 | 31 | nameForDiagnostics: String, |
27 | 32 | text: String? = nil, |
28 | | - associatedValueClass: String? = nil |
| 33 | + kind: Kind |
29 | 34 | ) { |
30 | | - self.name = name |
| 35 | + self.varOrCaseName = .identifier(name) |
31 | 36 | self.nameForDiagnostics = nameForDiagnostics |
32 | 37 | self.text = text |
33 | | - self.associatedValueClass = associatedValueClass |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -public class PoundSpec: TokenSpec { |
38 | | - init( |
39 | | - name: String, |
40 | | - nameForDiagnostics: String? = nil, |
41 | | - text: String |
42 | | - ) { |
43 | | - super.init( |
44 | | - name: name, |
45 | | - nameForDiagnostics: nameForDiagnostics ?? text, |
46 | | - text: text |
47 | | - ) |
48 | | - } |
49 | | -} |
50 | | - |
51 | | -public class PoundObjectLiteralSpec: PoundSpec { |
52 | | - let `protocol`: String |
53 | | - |
54 | | - init( |
55 | | - name: String, |
56 | | - text: String, |
57 | | - nameForDiagnostics: String, |
58 | | - `protocol`: String |
59 | | - ) { |
60 | | - self.`protocol` = `protocol` |
61 | | - super.init( |
62 | | - name: name, |
63 | | - nameForDiagnostics: nameForDiagnostics, |
64 | | - text: text |
65 | | - ) |
| 38 | + self.kind = kind |
66 | 39 | } |
67 | | -} |
68 | | - |
69 | | -public class PoundConfigSpec: PoundSpec {} |
70 | 40 |
|
71 | | -public class PoundDirectiveSpec: PoundSpec { |
72 | | - init( |
73 | | - name: String, |
74 | | - text: String |
75 | | - ) { |
76 | | - super.init( |
| 41 | + static func punctuator(name: String, text: String) -> TokenSpec { |
| 42 | + return TokenSpec( |
77 | 43 | name: name, |
78 | | - text: text |
| 44 | + nameForDiagnostics: text, |
| 45 | + text: text, |
| 46 | + kind: .punctuation |
79 | 47 | ) |
80 | 48 | } |
81 | | -} |
82 | 49 |
|
83 | | -public class PoundConditionalDirectiveSpec: PoundDirectiveSpec { |
84 | | - override init( |
85 | | - name: String, |
86 | | - text: String |
87 | | - ) { |
88 | | - super.init( |
| 50 | + static func poundKeyword(name: String, text: String) -> TokenSpec { |
| 51 | + return TokenSpec( |
89 | 52 | name: name, |
90 | | - text: text |
| 53 | + nameForDiagnostics: text, |
| 54 | + text: text, |
| 55 | + kind: .other |
91 | 56 | ) |
92 | 57 | } |
93 | | -} |
94 | 58 |
|
95 | | -public class PunctuatorSpec: TokenSpec { |
96 | | - init( |
97 | | - name: String, |
98 | | - text: String |
99 | | - ) { |
100 | | - super.init( |
| 59 | + static func other(name: String, nameForDiagnostics: String, text: String? = nil) -> TokenSpec { |
| 60 | + TokenSpec( |
101 | 61 | name: name, |
102 | | - nameForDiagnostics: text, |
103 | | - text: text |
| 62 | + nameForDiagnostics: nameForDiagnostics, |
| 63 | + text: text, |
| 64 | + kind: .other |
104 | 65 | ) |
105 | 66 | } |
106 | 67 | } |
107 | 68 |
|
108 | | -public class LiteralSpec: TokenSpec {} |
109 | | - |
110 | | -public class MiscSpec: TokenSpec {} |
111 | | - |
112 | 69 | public let SYNTAX_TOKENS: [TokenSpec] = [ |
113 | | - PunctuatorSpec(name: "Arrow", text: "->"), |
114 | | - PunctuatorSpec(name: "AtSign", text: "@"), |
115 | | - PunctuatorSpec(name: "Backslash", text: "\\"), |
116 | | - PunctuatorSpec(name: "Backtick", text: "`"), |
117 | | - MiscSpec(name: "BinaryOperator", nameForDiagnostics: "binary operator"), |
118 | | - PunctuatorSpec(name: "Colon", text: ":"), |
119 | | - PunctuatorSpec(name: "Comma", text: ","), |
120 | | - MiscSpec(name: "DollarIdentifier", nameForDiagnostics: "dollar identifier"), |
121 | | - PunctuatorSpec(name: "Ellipsis", text: "..."), |
122 | | - MiscSpec(name: "EndOfFile", nameForDiagnostics: "end of file", text: ""), |
123 | | - PunctuatorSpec(name: "Equal", text: "="), |
124 | | - PunctuatorSpec(name: "ExclamationMark", text: "!"), |
125 | | - MiscSpec(name: "ExtendedRegexDelimiter", nameForDiagnostics: "extended delimiter"), |
126 | | - LiteralSpec(name: "FloatingLiteral", nameForDiagnostics: "floating literal"), |
127 | | - MiscSpec(name: "Identifier", nameForDiagnostics: "identifier"), |
128 | | - PunctuatorSpec(name: "InfixQuestionMark", text: "?"), |
129 | | - LiteralSpec(name: "IntegerLiteral", nameForDiagnostics: "integer literal"), |
130 | | - MiscSpec(name: "Keyword", nameForDiagnostics: "keyword", associatedValueClass: "Keyword"), |
131 | | - PunctuatorSpec(name: "LeftAngle", text: "<"), |
132 | | - PunctuatorSpec(name: "LeftBrace", text: "{"), |
133 | | - PunctuatorSpec(name: "LeftParen", text: "("), |
134 | | - PunctuatorSpec(name: "LeftSquare", text: "["), |
135 | | - PunctuatorSpec(name: "MultilineStringQuote", text: "\"\"\""), |
136 | | - PunctuatorSpec(name: "Period", text: "."), |
137 | | - MiscSpec(name: "PostfixOperator", nameForDiagnostics: "postfix operator"), |
138 | | - PunctuatorSpec(name: "PostfixQuestionMark", text: "?"), |
139 | | - PunctuatorSpec(name: "Pound", text: "#"), |
140 | | - PoundConfigSpec(name: "PoundAvailable", text: "#available"), |
141 | | - PoundConditionalDirectiveSpec(name: "PoundElse", text: "#else"), |
142 | | - PoundConditionalDirectiveSpec(name: "PoundElseif", text: "#elseif"), |
143 | | - PoundConditionalDirectiveSpec(name: "PoundEndif", text: "#endif"), |
144 | | - PoundConditionalDirectiveSpec(name: "PoundIf", text: "#if"), |
145 | | - PoundDirectiveSpec(name: "PoundSourceLocation", text: "#sourceLocation"), |
146 | | - PoundConfigSpec(name: "PoundUnavailable", text: "#unavailable"), |
147 | | - PunctuatorSpec(name: "PrefixAmpersand", text: "&"), |
148 | | - MiscSpec(name: "PrefixOperator", nameForDiagnostics: "prefix operator"), |
149 | | - MiscSpec(name: "RawStringDelimiter", nameForDiagnostics: "raw string delimiter"), |
150 | | - MiscSpec(name: "RegexLiteralPattern", nameForDiagnostics: "regex pattern"), |
151 | | - PunctuatorSpec(name: "RegexSlash", text: "/"), |
152 | | - PunctuatorSpec(name: "RightAngle", text: ">"), |
153 | | - PunctuatorSpec(name: "RightBrace", text: "}"), |
154 | | - PunctuatorSpec(name: "RightParen", text: ")"), |
155 | | - PunctuatorSpec(name: "RightSquare", text: "]"), |
156 | | - PunctuatorSpec(name: "Semicolon", text: ";"), |
157 | | - PunctuatorSpec(name: "SingleQuote", text: "\'"), |
158 | | - PunctuatorSpec(name: "StringQuote", text: "\""), |
159 | | - MiscSpec(name: "StringSegment", nameForDiagnostics: "string segment"), |
160 | | - MiscSpec(name: "Unknown", nameForDiagnostics: "token"), |
161 | | - MiscSpec(name: "Wildcard", nameForDiagnostics: "wildcard", text: "_"), |
| 70 | + .punctuator(name: "arrow", text: "->"), |
| 71 | + .punctuator(name: "atSign", text: "@"), |
| 72 | + .punctuator(name: "backslash", text: "\\"), |
| 73 | + .punctuator(name: "backtick", text: "`"), |
| 74 | + .other(name: "binaryOperator", nameForDiagnostics: "binary operator"), |
| 75 | + .punctuator(name: "colon", text: ":"), |
| 76 | + .punctuator(name: "comma", text: ","), |
| 77 | + .other(name: "dollarIdentifier", nameForDiagnostics: "dollar identifier"), |
| 78 | + .punctuator(name: "ellipsis", text: "..."), |
| 79 | + .other(name: "endOfFile", nameForDiagnostics: "end of file", text: ""), |
| 80 | + .punctuator(name: "equal", text: "="), |
| 81 | + .punctuator(name: "exclamationMark", text: "!"), |
| 82 | + .other(name: "extendedRegexDelimiter", nameForDiagnostics: "extended delimiter"), |
| 83 | + .other(name: "floatingLiteral", nameForDiagnostics: "floating literal"), |
| 84 | + .other(name: "identifier", nameForDiagnostics: "identifier"), |
| 85 | + .punctuator(name: "infixQuestionMark", text: "?"), |
| 86 | + .other(name: "integerLiteral", nameForDiagnostics: "integer literal"), |
| 87 | + TokenSpec(name: "keyword", nameForDiagnostics: "keyword", text: nil, kind: .keyword), |
| 88 | + .punctuator(name: "leftAngle", text: "<"), |
| 89 | + .punctuator(name: "leftBrace", text: "{"), |
| 90 | + .punctuator(name: "leftParen", text: "("), |
| 91 | + .punctuator(name: "leftSquare", text: "["), |
| 92 | + .punctuator(name: "multilineStringQuote", text: "\"\"\""), |
| 93 | + .punctuator(name: "period", text: "."), |
| 94 | + .other(name: "postfixOperator", nameForDiagnostics: "postfix operator"), |
| 95 | + .punctuator(name: "postfixQuestionMark", text: "?"), |
| 96 | + .punctuator(name: "pound", text: "#"), |
| 97 | + .poundKeyword(name: "poundAvailable", text: "#available"), |
| 98 | + .poundKeyword(name: "poundElse", text: "#else"), |
| 99 | + .poundKeyword(name: "poundElseif", text: "#elseif"), |
| 100 | + .poundKeyword(name: "poundEndif", text: "#endif"), |
| 101 | + .poundKeyword(name: "poundIf", text: "#if"), |
| 102 | + .poundKeyword(name: "poundSourceLocation", text: "#sourceLocation"), |
| 103 | + .poundKeyword(name: "poundUnavailable", text: "#unavailable"), |
| 104 | + .punctuator(name: "prefixAmpersand", text: "&"), |
| 105 | + .other(name: "prefixOperator", nameForDiagnostics: "prefix operator"), |
| 106 | + .other(name: "rawStringDelimiter", nameForDiagnostics: "raw string delimiter"), |
| 107 | + .other(name: "regexLiteralPattern", nameForDiagnostics: "regex pattern"), |
| 108 | + .punctuator(name: "regexSlash", text: "/"), |
| 109 | + .punctuator(name: "rightAngle", text: ">"), |
| 110 | + .punctuator(name: "rightBrace", text: "}"), |
| 111 | + .punctuator(name: "rightParen", text: ")"), |
| 112 | + .punctuator(name: "rightSquare", text: "]"), |
| 113 | + .punctuator(name: "semicolon", text: ";"), |
| 114 | + .punctuator(name: "singleQuote", text: "\'"), |
| 115 | + .punctuator(name: "stringQuote", text: "\""), |
| 116 | + .other(name: "stringSegment", nameForDiagnostics: "string segment"), |
| 117 | + .other(name: "unknown", nameForDiagnostics: "token"), |
| 118 | + .other(name: "wildcard", nameForDiagnostics: "wildcard", text: "_"), |
162 | 119 | ] |
163 | 120 |
|
164 | 121 | public let SYNTAX_TOKEN_MAP = Dictionary( |
165 | | - uniqueKeysWithValues: SYNTAX_TOKENS.map { ("\($0.name)Token", $0) } |
| 122 | + uniqueKeysWithValues: SYNTAX_TOKENS.map { ("\($0.varOrCaseName.description.withFirstCharacterUppercased)Token", $0) } |
166 | 123 | ) |
0 commit comments