forked from dflook/python-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_printer.py
More file actions
297 lines (228 loc) · 9.08 KB
/
token_printer.py
File metadata and controls
297 lines (228 loc) · 9.08 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""Tools for assembling python code from tokens."""
import re
import sys
class TokenTypes(object):
NoToken = 0
Identifier = 1
Keyword = 2
SoftKeyword = 3
NumberLiteral = 4
NonNumberLiteral = 5
Delimiter = 6
Operator = 7
NewLine = 8
EndStatement = 9
class Delimiter(object):
def __init__(self, terminal_printer, delimiter=',', add_parens=False):
"""
Delimited group printer
A group of items that should be delimited by a delimiter character.
Each call to new_item() will insert the delimiter character if necessary.
When used as a context manager, the group will be enclosed by the start and end characters if the group has any items.
>>> d = Delimiter(terminal_printer)
... d.new_item()
... terminal_printer.identifier('a')
... print(terminal_printer.code)
a
>>> d.new_item()
... terminal_printer.identifier('b')
... print(terminal_printer.code)
a,b
>>> with Delimiter(terminal_printer, add_parens=True) as d:
... d.new_item()
... terminal_printer.identifier('a')
... print(terminal_printer.code)
(a)
:param terminal_printer: The terminal printer to use.
:param delimiter: The delimiter to use.
:param add_parens: If the group should be enclosed by parentheses. Only used when used as a context manager.
"""
self._terminal_printer = terminal_printer
self._delimiter = delimiter
self._add_parens = add_parens
self._first = True
self._context_manager = False
def __enter__(self):
"""Open a delimited group."""
self._context_manager = True
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Close the delimited group."""
if not self._first and self._add_parens:
self._terminal_printer.delimiter(')')
def new_item(self):
"""Add a new item to the delimited group."""
if self._first:
self._first = False
if self._context_manager and self._add_parens:
self._terminal_printer.delimiter('(')
else:
self._terminal_printer.delimiter(self._delimiter)
class TokenPrinter(object):
"""
Concatenates terminal symbols of the python grammar
"""
def __init__(self, prefer_single_line=False, allow_invalid_num_warnings=False):
"""
:param prefer_single_line: If True, chooses to put as much code as possible on a single line.
:param allow_invalid_num_warnings: If True, allows invalid number literals to be printe that may cause warnings.
"""
self._prefer_single_line = prefer_single_line
self._allow_invalid_num_warnings = allow_invalid_num_warnings
self._code = ''
self.indent = 0
self.unicode_literals = False
self.previous_token = TokenTypes.NoToken
def __str__(self):
"""Return the output code."""
return self._code
def identifier(self, name):
"""Add an identifier to the output code."""
assert isinstance(name, str)
if self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword, TokenTypes.SoftKeyword, TokenTypes.NumberLiteral]:
self.delimiter(' ')
self._code += name
self.previous_token = TokenTypes.Identifier
def keyword(self, kw):
"""Add a keyword to the output code."""
assert kw in [
'False', 'None', 'True', 'and', 'as',
'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'nonlocal', 'not',
'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield', '_', 'case', 'match', 'print', 'exec'
]
if self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword, TokenTypes.SoftKeyword, TokenTypes.NumberLiteral]:
self.delimiter(' ')
self._code += kw
if kw in ['_', 'case', 'match']:
self.previous_token = TokenTypes.SoftKeyword
else:
self.previous_token = TokenTypes.Keyword
def stringliteral(self, value):
"""Add a string literal to the output code."""
s = repr(value)
if sys.version_info < (3, 0) and self.unicode_literals:
if s[0] == 'u':
# Remove the u prefix since literals are unicode by default
s = s[1:]
else:
# Add a b prefix to indicate it is NOT unicode
s = 'b' + s
if len(s) > 0 and s[0].isalpha() and self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword, TokenTypes.SoftKeyword]:
self.delimiter(' ')
self._code += s
self.previous_token = TokenTypes.NonNumberLiteral
def bytesliteral(self, value):
"""Add a bytes literal to the output code."""
s = repr(value)
if len(s) > 0 and s[0].isalpha() and self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword, TokenTypes.SoftKeyword]:
self.delimiter(' ')
self._code += s
self.previous_token = TokenTypes.NonNumberLiteral
def fstring(self, s):
"""Add an f-string to the output code."""
assert isinstance(s, str)
if self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword, TokenTypes.SoftKeyword]:
self.delimiter(' ')
self._code += s
self.previous_token = TokenTypes.NonNumberLiteral
def delimiter(self, d):
"""Add a delimiter to the output code."""
assert d in [
'(', ')', '[', ']', '{', '}', ' ',
',', ':', '.', ';', '@', '=', '->',
'+=', '-=', '*=', '/=', '//=', '%=', '@=',
'&=', '|=', '^=', '>>=', '<<=', '**=', '|',
'`'
]
self._code += d
self.previous_token = TokenTypes.Delimiter
def operator(self, o):
"""Add an operator to the output code."""
assert o in [
'+', '-', '*', '**', '/', '//', '%', '@',
'<<', '>>', '&', '|', '^', '~', ':=',
'<', '>', '<=', '>=', '==', '!='
]
self._code += o
self.previous_token = TokenTypes.Operator
def integer(self, v):
"""Add an integer to the output code."""
s = repr(v)
h = hex(v)
if self.previous_token == TokenTypes.SoftKeyword:
self.delimiter(' ')
elif self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword]:
self.delimiter(' ')
self._code += h if len(h) < len(s) else s
self.previous_token = TokenTypes.NumberLiteral
def imagnumber(self, value):
"""Add a complex number to the output code."""
assert isinstance(value, complex)
s = repr(value)
if s in ['infj', 'inf*j']:
s = '1e999j'
elif s in ['-infj', '-inf*j']:
s = '-1e999j'
if self.previous_token == TokenTypes.SoftKeyword:
self.delimiter(' ')
elif self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword]:
self.delimiter(' ')
self._code += s
self.previous_token = TokenTypes.NumberLiteral
def floatnumber(self, v):
"""Add a float to the output code."""
assert isinstance(v, float)
s = repr(v)
s = s.replace('e+', 'e')
add_e = re.match(r'^(\d+?)(0+).0$', s)
if add_e:
s = add_e.group(1) + 'e' + str(len(add_e.group(2)))
if s == 'inf':
s = '1e999'
elif s == '-inf':
s = '-1e999'
elif s.startswith('0.'):
s = s[1:]
elif s.startswith('-0.'):
s = '-' + s[2:]
elif s.endswith('.0'):
s = s[:-1]
if self.previous_token == TokenTypes.SoftKeyword:
self.delimiter(' ')
elif self.previous_token in [TokenTypes.Identifier, TokenTypes.Keyword]:
self.delimiter(' ')
self._code += s
self.previous_token = TokenTypes.NumberLiteral
def newline(self):
""" Add a newline to the code. """
if self._code == '':
return
self._code = self._code.rstrip('\n\t;')
self._code += '\n'
self._code += '\t' * self.indent
self.previous_token = TokenTypes.NewLine
def enter_block(self):
"""Enter a new block, indenting the code."""
self.indent += 1
self.newline()
def leave_block(self):
"""Leave a block, un-indenting the code."""
self.indent -= 1
self.newline()
def end_statement(self):
""" End a statement with a newline, or a semi-colon if it saves characters. """
if self.indent == 0:
self.newline()
else:
if self._code[-1] != ';':
self._code += ';'
self.previous_token = TokenTypes.EndStatement
def append(self, code, token_type):
""" Append arbitrary string to the output."""
self._code += code
self.previous_token = token_type