Skip to content

Commit 3b2a570

Browse files
committed
Remove properties from Token.py that are not relevant for the compiler
1 parent 1ebe7ef commit 3b2a570

File tree

1 file changed

+54
-138
lines changed

1 file changed

+54
-138
lines changed

utils/gyb_syntax_support/Token.py

Lines changed: 54 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,25 @@
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-
211
class Token(object):
222
"""
233
Represents the specification for a Token in the TokenSyntax file.
244
"""
255

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):
307
self.name = name
31-
self.kind = kind
328
if unprefixed_kind is None:
339
self.unprefixed_kind = kind
3410
else:
3511
self.unprefixed_kind = unprefixed_kind
36-
self.name_for_diagnostics = name_for_diagnostics
3712
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
4713

4814

4915
class Keyword(Token):
5016
"""
5117
Represents a keyword token.
5218
"""
5319

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)
6423

6524
def macro_name(self):
6625
return "KEYWORD"
@@ -97,32 +56,25 @@ def macro_name(self):
9756

9857

9958
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):
10260
if name_for_diagnostics is None:
10361
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)
11164

11265
def macro_name(self):
11366
return "POUND_KEYWORD"
11467

11568

11669
class PoundObjectLiteral(PoundKeyword):
11770
def __init__(self, name, kind, text, name_for_diagnostics,
118-
protocol, classification='ObjectLiteral'):
71+
protocol):
11972
PoundKeyword.__init__(
12073
self,
12174
name=name,
12275
kind=kind,
12376
text=text,
124-
name_for_diagnostics=name_for_diagnostics,
125-
classification=classification
77+
name_for_diagnostics=name_for_diagnostics
12678
)
12779
self.description = name_for_diagnostics
12880
self.protocol = protocol
@@ -137,49 +89,39 @@ def macro_name(self):
13789

13890

13991
class PoundDirectiveKeyword(PoundKeyword):
140-
def __init__(self, name, kind, text,
141-
classification='PoundDirectiveKeyword'):
92+
def __init__(self, name, kind, text):
14293
PoundKeyword.__init__(
14394
self,
14495
name=name,
14596
kind=kind,
146-
text=text,
147-
classification=classification
97+
text=text
14898
)
14999

150100
def macro_name(self):
151101
return "POUND_DIRECTIVE_KEYWORD"
152102

153103

154104
class PoundConditionalDirectiveKeyword(PoundDirectiveKeyword):
155-
def __init__(self, name, kind, text,
156-
classification='PoundDirectiveKeyword'):
105+
def __init__(self, name, kind, text):
157106
PoundKeyword.__init__(
158107
self,
159108
name=name,
160109
kind=kind,
161-
text=text,
162-
classification=classification
110+
text=text
163111
)
164112

165113
def macro_name(self):
166114
return "POUND_COND_DIRECTIVE_KEYWORD"
167115

168116

169117
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):
172119
Token.__init__(
173120
self,
174121
name=name,
175122
kind=kind,
176-
name_for_diagnostics=text,
177123
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
183125
)
184126

185127
def macro_name(self):
@@ -226,7 +168,7 @@ def macro_name(self):
226168
StmtKeyword('Defer', 'defer'),
227169
StmtKeyword('If', 'if'),
228170
StmtKeyword('Guard', 'guard'),
229-
StmtKeyword('Do', 'do', requires_trailing_space=False),
171+
StmtKeyword('Do', 'do'),
230172
StmtKeyword('Repeat', 'repeat'),
231173
StmtKeyword('Else', 'else'),
232174
StmtKeyword('For', 'for'),
@@ -238,23 +180,22 @@ def macro_name(self):
238180
StmtKeyword('Fallthrough', 'fallthrough'),
239181
StmtKeyword('Switch', 'switch'),
240182
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'),
245186
StmtKeyword('Throw', 'throw'),
246187

247188
# Expression keywords
248189
ExprKeyword('As', 'as'),
249190
ExprKeyword('Any', 'Any'),
250-
ExprKeyword('False', 'false', requires_trailing_space=False),
191+
ExprKeyword('False', 'false'),
251192
ExprKeyword('Is', 'is'),
252-
ExprKeyword('Nil', 'nil', requires_trailing_space=False),
193+
ExprKeyword('Nil', 'nil'),
253194
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'),
258199
ExprKeyword('Try', 'try'),
259200
ExprKeyword('Throws', 'throws'),
260201

@@ -264,29 +205,25 @@ def macro_name(self):
264205
# Punctuators
265206
Punctuator('LeftParen', 'l_paren', text='('),
266207
Punctuator('RightParen', 'r_paren', text=')'),
267-
Punctuator('LeftBrace', 'l_brace', text='{', requires_leading_space=True),
208+
Punctuator('LeftBrace', 'l_brace', text='{'),
268209
Punctuator('RightBrace', 'r_brace', text='}'),
269210
Punctuator('LeftSquareBracket', 'l_square', text='['),
270211
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='>'),
275214

276215
Punctuator('Period', 'period', text='.'),
277216
Punctuator('PrefixPeriod', 'period_prefix', text='.'),
278-
Punctuator('Comma', 'comma', text=',', requires_trailing_space=True),
217+
Punctuator('Comma', 'comma', text=','),
279218
Punctuator('Ellipsis', 'ellipsis', text='...'),
280-
Punctuator('Colon', 'colon', text=':', requires_trailing_space=True),
219+
Punctuator('Colon', 'colon', text=':'),
281220
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='@'),
285223
Punctuator('Pound', 'pound', text='#'),
286224

287225
Punctuator('PrefixAmpersand', 'amp_prefix', text='&'),
288-
Punctuator('Arrow', 'arrow', text='->', requires_leading_space=True,
289-
requires_trailing_space=True),
226+
Punctuator('Arrow', 'arrow', text='->'),
290227

291228
Punctuator('Backtick', 'backtick', text='`'),
292229

@@ -297,12 +234,10 @@ def macro_name(self):
297234
Punctuator('PostfixQuestionMark', 'question_postfix', text='?'),
298235
Punctuator('InfixQuestionMark', 'question_infix', text='?'),
299236

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='\\\''),
304239
Punctuator('MultilineStringQuote', 'multiline_string_quote',
305-
text='\\\"\\\"\\\"', classification='StringLiteral'),
240+
text='\\\"\\\"\\\"'),
306241

307242
# Keywords prefixed with a '#'.
308243

@@ -347,44 +282,25 @@ def macro_name(self):
347282

348283
PoundConfig('PoundHasSymbol', '_hasSymbol', text='#_hasSymbol'),
349284

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'),
386303

387304
]
388305

389306
SYNTAX_TOKEN_MAP = {token.name + 'Token': token for token in SYNTAX_TOKENS}
390-

0 commit comments

Comments
 (0)