1
- def lowercase_first_word (name ):
2
- """
3
- Lowercases the first word in the provided camelCase or PascalCase string.
4
- EOF -> eof
5
- IfKeyword -> ifKeyword
6
- EOFToken -> eofToken
7
- """
8
- word_index = 0
9
- threshold_index = 1
10
- for c in name :
11
- if c .islower ():
12
- if word_index > threshold_index :
13
- word_index -= 1
14
- break
15
- word_index += 1
16
- if word_index == 0 :
17
- return name
18
- return name [:word_index ].lower () + name [word_index :]
19
-
20
-
21
1
class Token (object ):
22
2
"""
23
3
Represents the specification for a Token in the TokenSyntax file.
24
4
"""
25
5
26
- def __init__ (self , name , kind , name_for_diagnostics ,
27
- unprefixed_kind = None , text = None , classification = 'None' ,
28
- is_keyword = False , requires_leading_space = False ,
29
- requires_trailing_space = False ):
6
+ def __init__ (self , name , kind , unprefixed_kind = None , text = None ):
30
7
self .name = name
31
- self .kind = kind
32
8
if unprefixed_kind is None :
33
9
self .unprefixed_kind = kind
34
10
else :
35
11
self .unprefixed_kind = unprefixed_kind
36
- self .name_for_diagnostics = name_for_diagnostics
37
12
self .text = text
38
- self .is_keyword = is_keyword
39
- self .requires_leading_space = requires_leading_space
40
- self .requires_trailing_space = requires_trailing_space
41
-
42
- def swift_kind (self ):
43
- name = lowercase_first_word (self .name )
44
- if self .is_keyword :
45
- return name + 'Keyword'
46
- return name
47
13
48
14
49
15
class Keyword (Token ):
50
16
"""
51
17
Represents a keyword token.
52
18
"""
53
19
54
- def __init__ (self , name , text , classification = 'Keyword' ,
55
- requires_leading_space = False , requires_trailing_space = True ):
56
- Token .__init__ (self ,
57
- name = name ,
58
- kind = 'kw_' + text ,
59
- name_for_diagnostics = text ,
60
- unprefixed_kind = text , text = text ,
61
- classification = classification , is_keyword = True ,
62
- requires_leading_space = requires_leading_space ,
63
- requires_trailing_space = requires_trailing_space )
20
+ def __init__ (self , name , text ):
21
+ Token .__init__ (self , name = name , kind = 'kw_' + text , unprefixed_kind = text ,
22
+ text = text )
64
23
65
24
def macro_name (self ):
66
25
return "KEYWORD"
@@ -97,32 +56,25 @@ def macro_name(self):
97
56
98
57
99
58
class PoundKeyword (Token ):
100
- def __init__ (self , name , kind , text , name_for_diagnostics = None ,
101
- classification = 'Keyword' ):
59
+ def __init__ (self , name , kind , text , name_for_diagnostics = None ):
102
60
if name_for_diagnostics is None :
103
61
name_for_diagnostics = text
104
- Token .__init__ (self ,
105
- name = name ,
106
- kind = 'pound_' + kind ,
107
- name_for_diagnostics = name_for_diagnostics ,
108
- unprefixed_kind = kind , text = text ,
109
- classification = classification , is_keyword = True ,
110
- requires_trailing_space = True )
62
+ Token .__init__ (self , name = name , kind = 'pound_' + kind , unprefixed_kind = kind ,
63
+ text = text )
111
64
112
65
def macro_name (self ):
113
66
return "POUND_KEYWORD"
114
67
115
68
116
69
class PoundObjectLiteral (PoundKeyword ):
117
70
def __init__ (self , name , kind , text , name_for_diagnostics ,
118
- protocol , classification = 'ObjectLiteral' ):
71
+ protocol ):
119
72
PoundKeyword .__init__ (
120
73
self ,
121
74
name = name ,
122
75
kind = kind ,
123
76
text = text ,
124
- name_for_diagnostics = name_for_diagnostics ,
125
- classification = classification
77
+ name_for_diagnostics = name_for_diagnostics
126
78
)
127
79
self .description = name_for_diagnostics
128
80
self .protocol = protocol
@@ -137,49 +89,39 @@ def macro_name(self):
137
89
138
90
139
91
class PoundDirectiveKeyword (PoundKeyword ):
140
- def __init__ (self , name , kind , text ,
141
- classification = 'PoundDirectiveKeyword' ):
92
+ def __init__ (self , name , kind , text ):
142
93
PoundKeyword .__init__ (
143
94
self ,
144
95
name = name ,
145
96
kind = kind ,
146
- text = text ,
147
- classification = classification
97
+ text = text
148
98
)
149
99
150
100
def macro_name (self ):
151
101
return "POUND_DIRECTIVE_KEYWORD"
152
102
153
103
154
104
class PoundConditionalDirectiveKeyword (PoundDirectiveKeyword ):
155
- def __init__ (self , name , kind , text ,
156
- classification = 'PoundDirectiveKeyword' ):
105
+ def __init__ (self , name , kind , text ):
157
106
PoundKeyword .__init__ (
158
107
self ,
159
108
name = name ,
160
109
kind = kind ,
161
- text = text ,
162
- classification = classification
110
+ text = text
163
111
)
164
112
165
113
def macro_name (self ):
166
114
return "POUND_COND_DIRECTIVE_KEYWORD"
167
115
168
116
169
117
class Punctuator (Token ):
170
- def __init__ (self , name , kind , text , classification = 'None' ,
171
- requires_leading_space = False , requires_trailing_space = False ):
118
+ def __init__ (self , name , kind , text ):
172
119
Token .__init__ (
173
120
self ,
174
121
name = name ,
175
122
kind = kind ,
176
- name_for_diagnostics = text ,
177
123
unprefixed_kind = None ,
178
- text = text ,
179
- classification = classification ,
180
- is_keyword = False ,
181
- requires_leading_space = requires_leading_space ,
182
- requires_trailing_space = requires_trailing_space
124
+ text = text
183
125
)
184
126
185
127
def macro_name (self ):
@@ -226,7 +168,7 @@ def macro_name(self):
226
168
StmtKeyword ('Defer' , 'defer' ),
227
169
StmtKeyword ('If' , 'if' ),
228
170
StmtKeyword ('Guard' , 'guard' ),
229
- StmtKeyword ('Do' , 'do' , requires_trailing_space = False ),
171
+ StmtKeyword ('Do' , 'do' ),
230
172
StmtKeyword ('Repeat' , 'repeat' ),
231
173
StmtKeyword ('Else' , 'else' ),
232
174
StmtKeyword ('For' , 'for' ),
@@ -238,23 +180,22 @@ def macro_name(self):
238
180
StmtKeyword ('Fallthrough' , 'fallthrough' ),
239
181
StmtKeyword ('Switch' , 'switch' ),
240
182
StmtKeyword ('Case' , 'case' ),
241
- StmtKeyword ('Default' , 'default' , requires_trailing_space = False ),
242
- StmtKeyword ('Where' , 'where' , requires_leading_space = True ),
243
- StmtKeyword ('Catch' , 'catch' , requires_leading_space = True ,
244
- requires_trailing_space = False ),
183
+ StmtKeyword ('Default' , 'default' ),
184
+ StmtKeyword ('Where' , 'where' ),
185
+ StmtKeyword ('Catch' , 'catch' ),
245
186
StmtKeyword ('Throw' , 'throw' ),
246
187
247
188
# Expression keywords
248
189
ExprKeyword ('As' , 'as' ),
249
190
ExprKeyword ('Any' , 'Any' ),
250
- ExprKeyword ('False' , 'false' , requires_trailing_space = False ),
191
+ ExprKeyword ('False' , 'false' ),
251
192
ExprKeyword ('Is' , 'is' ),
252
- ExprKeyword ('Nil' , 'nil' , requires_trailing_space = False ),
193
+ ExprKeyword ('Nil' , 'nil' ),
253
194
ExprKeyword ('Rethrows' , 'rethrows' ),
254
- ExprKeyword ('Super' , 'super' , requires_trailing_space = False ),
255
- ExprKeyword ('Self' , 'self' , requires_trailing_space = False ),
256
- ExprKeyword ('CapitalSelf' , 'Self' , requires_trailing_space = False ),
257
- ExprKeyword ('True' , 'true' , requires_trailing_space = False ),
195
+ ExprKeyword ('Super' , 'super' ),
196
+ ExprKeyword ('Self' , 'self' ),
197
+ ExprKeyword ('CapitalSelf' , 'Self' ),
198
+ ExprKeyword ('True' , 'true' ),
258
199
ExprKeyword ('Try' , 'try' ),
259
200
ExprKeyword ('Throws' , 'throws' ),
260
201
@@ -264,29 +205,25 @@ def macro_name(self):
264
205
# Punctuators
265
206
Punctuator ('LeftParen' , 'l_paren' , text = '(' ),
266
207
Punctuator ('RightParen' , 'r_paren' , text = ')' ),
267
- Punctuator ('LeftBrace' , 'l_brace' , text = '{' , requires_leading_space = True ),
208
+ Punctuator ('LeftBrace' , 'l_brace' , text = '{' ),
268
209
Punctuator ('RightBrace' , 'r_brace' , text = '}' ),
269
210
Punctuator ('LeftSquareBracket' , 'l_square' , text = '[' ),
270
211
Punctuator ('RightSquareBracket' , 'r_square' , text = ']' ),
271
- Punctuator ('LeftAngle' , 'l_angle' , text = '<' , requires_leading_space = True ,
272
- requires_trailing_space = True ),
273
- Punctuator ('RightAngle' , 'r_angle' , text = '>' , requires_leading_space = True ,
274
- requires_trailing_space = True ),
212
+ Punctuator ('LeftAngle' , 'l_angle' , text = '<' ),
213
+ Punctuator ('RightAngle' , 'r_angle' , text = '>' ),
275
214
276
215
Punctuator ('Period' , 'period' , text = '.' ),
277
216
Punctuator ('PrefixPeriod' , 'period_prefix' , text = '.' ),
278
- Punctuator ('Comma' , 'comma' , text = ',' , requires_trailing_space = True ),
217
+ Punctuator ('Comma' , 'comma' , text = ',' ),
279
218
Punctuator ('Ellipsis' , 'ellipsis' , text = '...' ),
280
- Punctuator ('Colon' , 'colon' , text = ':' , requires_trailing_space = True ),
219
+ Punctuator ('Colon' , 'colon' , text = ':' ),
281
220
Punctuator ('Semicolon' , 'semi' , text = ';' ),
282
- Punctuator ('Equal' , 'equal' , text = '=' , requires_leading_space = True ,
283
- requires_trailing_space = True ),
284
- Punctuator ('AtSign' , 'at_sign' , text = '@' , classification = 'Attribute' ),
221
+ Punctuator ('Equal' , 'equal' , text = '=' ),
222
+ Punctuator ('AtSign' , 'at_sign' , text = '@' ),
285
223
Punctuator ('Pound' , 'pound' , text = '#' ),
286
224
287
225
Punctuator ('PrefixAmpersand' , 'amp_prefix' , text = '&' ),
288
- Punctuator ('Arrow' , 'arrow' , text = '->' , requires_leading_space = True ,
289
- requires_trailing_space = True ),
226
+ Punctuator ('Arrow' , 'arrow' , text = '->' ),
290
227
291
228
Punctuator ('Backtick' , 'backtick' , text = '`' ),
292
229
@@ -297,12 +234,10 @@ def macro_name(self):
297
234
Punctuator ('PostfixQuestionMark' , 'question_postfix' , text = '?' ),
298
235
Punctuator ('InfixQuestionMark' , 'question_infix' , text = '?' ),
299
236
300
- Punctuator ('StringQuote' , 'string_quote' , text = '\\ \" ' ,
301
- classification = 'StringLiteral' ),
302
- Punctuator ('SingleQuote' , 'single_quote' , text = '\\ \' ' ,
303
- classification = 'StringLiteral' ),
237
+ Punctuator ('StringQuote' , 'string_quote' , text = '\\ \" ' ),
238
+ Punctuator ('SingleQuote' , 'single_quote' , text = '\\ \' ' ),
304
239
Punctuator ('MultilineStringQuote' , 'multiline_string_quote' ,
305
- text = '\\ \" \\ \" \\ \" ' , classification = 'StringLiteral' ),
240
+ text = '\\ \" \\ \" \\ \" ' ),
306
241
307
242
# Keywords prefixed with a '#'.
308
243
@@ -347,44 +282,25 @@ def macro_name(self):
347
282
348
283
PoundConfig ('PoundHasSymbol' , '_hasSymbol' , text = '#_hasSymbol' ),
349
284
350
- Literal ('IntegerLiteral' , 'integer_literal' , name_for_diagnostics = 'integer literal' ,
351
- classification = 'IntegerLiteral' ),
352
- Literal ('FloatingLiteral' , 'floating_literal' ,
353
- name_for_diagnostics = 'floating literal' , classification = 'FloatingLiteral' ),
354
- Literal ('StringLiteral' , 'string_literal' , name_for_diagnostics = 'string literal' ,
355
- classification = 'StringLiteral' ),
356
- Literal ('RegexLiteral' , 'regex_literal' , name_for_diagnostics = 'regex literal' ),
357
-
358
- Misc ('Unknown' , 'unknown' , name_for_diagnostics = 'token' ),
359
- Misc ('Identifier' , 'identifier' , name_for_diagnostics = 'identifier' ,
360
- classification = 'Identifier' ),
361
- Misc ('UnspacedBinaryOperator' , 'oper_binary_unspaced' ,
362
- name_for_diagnostics = 'binary operator' ,
363
- classification = 'OperatorIdentifier' ),
364
- Misc ('SpacedBinaryOperator' , 'oper_binary_spaced' ,
365
- name_for_diagnostics = 'binary operator' ,
366
- classification = 'OperatorIdentifier' ,
367
- requires_leading_space = True , requires_trailing_space = True ),
368
- Misc ('PostfixOperator' , 'oper_postfix' , name_for_diagnostics = 'postfix operator' ,
369
- classification = 'OperatorIdentifier' ),
370
- Misc ('PrefixOperator' , 'oper_prefix' , name_for_diagnostics = 'prefix operator' ,
371
- classification = 'OperatorIdentifier' ),
372
- Misc ('DollarIdentifier' , 'dollarident' , name_for_diagnostics = 'dollar identifier' ,
373
- classification = 'DollarIdentifier' ),
374
-
375
- Misc ('ContextualKeyword' , 'contextual_keyword' , name_for_diagnostics = 'keyword' ,
376
- classification = 'Keyword' ),
377
- Misc ('RawStringDelimiter' , 'raw_string_delimiter' ,
378
- name_for_diagnostics = 'raw string delimiter' ),
379
- Misc ('StringSegment' , 'string_segment' , name_for_diagnostics = 'string segment' ,
380
- classification = 'StringLiteral' ),
381
- Misc ('StringInterpolationAnchor' , 'string_interpolation_anchor' ,
382
- name_for_diagnostics = 'string interpolation anchor' ,
383
- text = ')' , classification = 'StringInterpolationAnchor' ),
384
- Misc ('Yield' , 'kw_yield' , name_for_diagnostics = 'yield' ,
385
- text = 'yield' ),
285
+ Literal ('IntegerLiteral' , 'integer_literal' ),
286
+ Literal ('FloatingLiteral' , 'floating_literal' ),
287
+ Literal ('StringLiteral' , 'string_literal' ),
288
+ Literal ('RegexLiteral' , 'regex_literal' ),
289
+
290
+ Misc ('Unknown' , 'unknown' ),
291
+ Misc ('Identifier' , 'identifier' ),
292
+ Misc ('UnspacedBinaryOperator' , 'oper_binary_unspaced' ),
293
+ Misc ('SpacedBinaryOperator' , 'oper_binary_spaced' ),
294
+ Misc ('PostfixOperator' , 'oper_postfix' ),
295
+ Misc ('PrefixOperator' , 'oper_prefix' ),
296
+ Misc ('DollarIdentifier' , 'dollarident' ),
297
+
298
+ Misc ('ContextualKeyword' , 'contextual_keyword' ),
299
+ Misc ('RawStringDelimiter' , 'raw_string_delimiter' ),
300
+ Misc ('StringSegment' , 'string_segment' ),
301
+ Misc ('StringInterpolationAnchor' , 'string_interpolation_anchor' , text = ')' ),
302
+ Misc ('Yield' , 'kw_yield' , text = 'yield' ),
386
303
387
304
]
388
305
389
306
SYNTAX_TOKEN_MAP = {token .name + 'Token' : token for token in SYNTAX_TOKENS }
390
-
0 commit comments