rapydscript-ng 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +8 -0
- package/bin/build.ts +117 -0
- package/build_wheels.py +133 -0
- package/editor-plugins/README.md +80 -0
- package/editor-plugins/nvim.lua +321 -0
- package/package.json +1 -1
- package/publish.py +27 -17
- package/release/compiler.js +8125 -8093
- package/release/signatures.json +27 -27
- package/release/stdlib_modules.json +1 -0
- package/src/ast.pyj +379 -279
- package/src/baselib-builtins.pyj +47 -26
- package/src/baselib-containers.pyj +105 -92
- package/src/baselib-errors.pyj +8 -1
- package/src/baselib-internal.pyj +35 -20
- package/src/baselib-itertools.pyj +13 -7
- package/src/baselib-str.pyj +55 -80
- package/src/compiler.pyj +4 -4
- package/src/errors.pyj +3 -4
- package/src/lib/aes.pyj +46 -32
- package/src/lib/elementmaker.pyj +11 -9
- package/src/lib/encodings.pyj +15 -5
- package/src/lib/gettext.pyj +9 -4
- package/src/lib/math.pyj +106 -45
- package/src/lib/operator.pyj +10 -10
- package/src/lib/pythonize.pyj +2 -1
- package/src/lib/random.pyj +28 -21
- package/src/lib/re.pyj +490 -17
- package/src/lib/traceback.pyj +7 -2
- package/src/lib/uuid.pyj +2 -2
- package/src/output/classes.pyj +28 -27
- package/src/output/codegen.pyj +80 -83
- package/src/output/comments.pyj +8 -8
- package/src/output/exceptions.pyj +20 -21
- package/src/output/functions.pyj +37 -26
- package/src/output/literals.pyj +14 -10
- package/src/output/loops.pyj +63 -59
- package/src/output/modules.pyj +52 -23
- package/src/output/operators.pyj +40 -29
- package/src/output/statements.pyj +20 -14
- package/src/output/stream.pyj +33 -34
- package/src/output/utils.pyj +12 -8
- package/src/parse.pyj +234 -233
- package/src/string_interpolation.pyj +5 -3
- package/src/tokenizer.pyj +176 -148
- package/src/utils.pyj +31 -14
- package/test/fmt.pyj +94 -4
- package/test/generic.pyj +9 -0
- package/test/lsp.pyj +35 -0
- package/test/str.pyj +8 -0
- package/tools/cli.mjs +25 -4
- package/tools/compile.mjs +7 -3
- package/tools/compiler.mjs +34 -2
- package/tools/fmt.mjs +262 -22
- package/tools/ini.mjs +112 -1
- package/tools/lsp.mjs +56 -6
- package/tools/repl.mjs +5 -2
- package/tools/self.mjs +15 -0
- package/tools/test.mjs +100 -0
- package/tools/web_repl_export.mjs +4 -0
- package/tree-sitter/package.json +1 -1
- package/tree-sitter/tree-sitter.json +1 -1
- package/editor-plugins/nvim/rapydscript/README.md +0 -184
- package/editor-plugins/nvim/rapydscript/bin/rapydscript.so +0 -0
- package/editor-plugins/nvim/rapydscript/ftdetect/rapydscript.lua +0 -10
- package/editor-plugins/nvim/rapydscript/lua/rapydscript/health.lua +0 -88
- package/editor-plugins/nvim/rapydscript/lua/rapydscript/init.lua +0 -279
- /package/tree-sitter/queries/{rapydscript/highlights.scm → highlights.scm} +0 -0
- /package/tree-sitter/queries/{rapydscript/indents.scm → indents.scm} +0 -0
- /package/tree-sitter/queries/{rapydscript/injections.scm → injections.scm} +0 -0
- /package/tree-sitter/queries/{rapydscript/locals.scm → locals.scm} +0 -0
package/src/output/operators.pyj
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# vim:fileencoding=utf-8
|
|
2
2
|
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
3
|
from __python__ import hash_literals
|
|
4
|
+
|
|
4
5
|
from ast import (
|
|
5
6
|
AST_Array, AST_Assign, AST_BaseCall, AST_Binary, AST_Conditional,
|
|
6
7
|
AST_ItemAccess, AST_Number, AST_Object, AST_Return, AST_Seq, AST_Set,
|
|
@@ -16,11 +17,12 @@ def print_getattr(self, output, skip_expression): # AST_Dot
|
|
|
16
17
|
expr.print(output)
|
|
17
18
|
if is_node_type(expr, AST_Number) and expr.value >= 0:
|
|
18
19
|
if not /[xa-f.]/i.test(output.last()):
|
|
19
|
-
output.print(
|
|
20
|
-
output.print(
|
|
20
|
+
output.print('.')
|
|
21
|
+
output.print('.')
|
|
21
22
|
# the name after dot would be mapped about here.
|
|
22
23
|
output.print_name(self.property)
|
|
23
24
|
|
|
25
|
+
|
|
24
26
|
def print_getitem(self, output): # AST_Sub
|
|
25
27
|
expr = self.expression
|
|
26
28
|
prop = self.property
|
|
@@ -28,13 +30,13 @@ def print_getitem(self, output): # AST_Sub
|
|
|
28
30
|
expr.print(output)
|
|
29
31
|
output.print('['), prop.print(output), output.print(']')
|
|
30
32
|
return
|
|
31
|
-
is_negative_number = is_node_type(prop, AST_Unary) and prop.operator is
|
|
33
|
+
is_negative_number = is_node_type(prop, AST_Unary) and prop.operator is '-' and is_node_type(prop.expression, AST_Number)
|
|
32
34
|
is_repeatable = is_node_type(expr, AST_SymbolRef)
|
|
33
35
|
if is_repeatable:
|
|
34
36
|
expr.print(output)
|
|
35
37
|
else:
|
|
36
38
|
output.spaced('(ρσ_expr_temp', '=', expr), output.print(')')
|
|
37
|
-
expr = {'print': def(): output.print('ρσ_expr_temp');}
|
|
39
|
+
expr = {'print': def (): output.print('ρσ_expr_temp');}
|
|
38
40
|
|
|
39
41
|
if is_negative_number:
|
|
40
42
|
output.print('['), expr.print(output), output.print('.length'), prop.print(output), output.print(']')
|
|
@@ -45,7 +47,7 @@ def print_getitem(self, output): # AST_Sub
|
|
|
45
47
|
if is_repeatable:
|
|
46
48
|
output.spaced('[(typeof', prop, '===', '"number"', '&&', prop)
|
|
47
49
|
output.spaced('', '<', '0)', '?', expr), output.spaced('.length', '+', prop, ':', prop)
|
|
48
|
-
output.print(
|
|
50
|
+
output.print(']')
|
|
49
51
|
else:
|
|
50
52
|
output.print('[ρσ_bound_index('), prop.print(output), output.comma(), expr.print(output), output.print(')]')
|
|
51
53
|
|
|
@@ -68,6 +70,7 @@ def print_rich_getitem(self, output): # AST_ItemAccess
|
|
|
68
70
|
self.assignment.print(output)
|
|
69
71
|
output.print(')')
|
|
70
72
|
|
|
73
|
+
|
|
71
74
|
def print_splice_assignment(self, output): # AST_Splice
|
|
72
75
|
# splice assignment via pythonic array[start:end]
|
|
73
76
|
output.print('ρσ_splice(')
|
|
@@ -78,6 +81,7 @@ def print_splice_assignment(self, output): # AST_Splice
|
|
|
78
81
|
self.property2.print(output)
|
|
79
82
|
output.print(')')
|
|
80
83
|
|
|
84
|
+
|
|
81
85
|
def print_delete(self, output):
|
|
82
86
|
if is_node_type(self, AST_Symbol):
|
|
83
87
|
output.assign(self), output.print('undefined')
|
|
@@ -86,6 +90,7 @@ def print_delete(self, output):
|
|
|
86
90
|
else:
|
|
87
91
|
output.spaced('delete', self)
|
|
88
92
|
|
|
93
|
+
|
|
89
94
|
def print_unary_prefix(self, output):
|
|
90
95
|
op = self.operator
|
|
91
96
|
if op is 'delete':
|
|
@@ -94,14 +99,14 @@ def print_unary_prefix(self, output):
|
|
|
94
99
|
if /^[a-z]/i.test(op):
|
|
95
100
|
output.space()
|
|
96
101
|
if self.parenthesized:
|
|
97
|
-
output.with_parens(def():
|
|
102
|
+
output.with_parens(def ():
|
|
98
103
|
self.expression.print(output)
|
|
99
104
|
)
|
|
100
105
|
else:
|
|
101
106
|
self.expression.print(output)
|
|
102
107
|
|
|
103
|
-
def write_instanceof(left, right, output):
|
|
104
108
|
|
|
109
|
+
def write_instanceof(left, right, output):
|
|
105
110
|
def do_many(vals):
|
|
106
111
|
output.print('ρσ_instanceof.apply(null,'), output.space()
|
|
107
112
|
output.print('['), left.print(output), output.comma()
|
|
@@ -119,6 +124,7 @@ def write_instanceof(left, right, output):
|
|
|
119
124
|
output.print('ρσ_instanceof(')
|
|
120
125
|
left.print(output), output.comma(), right.print(output), output.print(')')
|
|
121
126
|
|
|
127
|
+
|
|
122
128
|
def write_smart_equality(self, output):
|
|
123
129
|
def is_ok(x):
|
|
124
130
|
return not (
|
|
@@ -141,14 +147,14 @@ def write_smart_equality(self, output):
|
|
|
141
147
|
|
|
142
148
|
|
|
143
149
|
comparators = {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
150
|
+
'<': True,
|
|
151
|
+
'>': True,
|
|
152
|
+
'<=': True,
|
|
153
|
+
'>=': True,
|
|
148
154
|
}
|
|
149
155
|
|
|
150
156
|
function_ops = {
|
|
151
|
-
|
|
157
|
+
'in': 'ρσ_in',
|
|
152
158
|
'nin': '!ρσ_in',
|
|
153
159
|
}
|
|
154
160
|
|
|
@@ -156,7 +162,7 @@ function_ops = {
|
|
|
156
162
|
def print_binary_op(self, output):
|
|
157
163
|
if function_ops[self.operator]:
|
|
158
164
|
output.print(function_ops[self.operator])
|
|
159
|
-
output.with_parens(def():
|
|
165
|
+
output.with_parens(def ():
|
|
160
166
|
self.left.print(output)
|
|
161
167
|
output.comma()
|
|
162
168
|
self.right.print(output)
|
|
@@ -174,15 +180,15 @@ def print_binary_op(self, output):
|
|
|
174
180
|
output.space()
|
|
175
181
|
output.print(self.left.operator)
|
|
176
182
|
output.space()
|
|
177
|
-
output.with_parens(def():
|
|
183
|
+
output.with_parens(def ():
|
|
178
184
|
nonlocal leftvar
|
|
179
|
-
output.assign(
|
|
185
|
+
output.assign('ρσ_cond_temp')
|
|
180
186
|
self.left.right.print(output)
|
|
181
|
-
leftvar =
|
|
187
|
+
leftvar = 'ρσ_cond_temp'
|
|
182
188
|
)
|
|
183
189
|
|
|
184
190
|
output.space()
|
|
185
|
-
output.print(
|
|
191
|
+
output.print('&&')
|
|
186
192
|
output.space()
|
|
187
193
|
output.print(leftvar)
|
|
188
194
|
output.space()
|
|
@@ -191,7 +197,7 @@ def print_binary_op(self, output):
|
|
|
191
197
|
self.right.print(output)
|
|
192
198
|
elif self.operator is '//':
|
|
193
199
|
output.print('Math.floor')
|
|
194
|
-
output.with_parens(def():
|
|
200
|
+
output.with_parens(def ():
|
|
195
201
|
self.left.print(output)
|
|
196
202
|
output.space()
|
|
197
203
|
output.print('/')
|
|
@@ -228,7 +234,8 @@ def print_binary_op(self, output):
|
|
|
228
234
|
else:
|
|
229
235
|
output.spaced(self.left, self.operator, self.right)
|
|
230
236
|
|
|
231
|
-
after_map = {'.':'d', '(':'c', '[':'d', 'g':'g', 'null':'n'}
|
|
237
|
+
after_map = {'.': 'd', '(': 'c', '[': 'd', 'g': 'g', 'null': 'n'}
|
|
238
|
+
|
|
232
239
|
|
|
233
240
|
def print_existential(self, output):
|
|
234
241
|
key = after_map[self.after] if self.after is None or jstype(self.after) is 'string' else 'e'
|
|
@@ -253,15 +260,16 @@ def print_existential(self, output):
|
|
|
253
260
|
output.comma(), self.after.print(output)
|
|
254
261
|
output.print(')')
|
|
255
262
|
|
|
263
|
+
|
|
256
264
|
def print_assignment(self, output):
|
|
257
265
|
flattened = False
|
|
258
266
|
left = self.left
|
|
259
267
|
if is_node_type(left, AST_Seq):
|
|
260
|
-
left = new AST_Array({'elements':v'[left.car, left.cdr]'})
|
|
268
|
+
left = new AST_Array({'elements': v'[left.car, left.cdr]'})
|
|
261
269
|
if is_node_type(left, AST_Array):
|
|
262
270
|
flat = left.flatten()
|
|
263
271
|
flattened = flat.length > left.elements.length
|
|
264
|
-
output.print(
|
|
272
|
+
output.print('ρσ_unpack')
|
|
265
273
|
else:
|
|
266
274
|
left.print(output)
|
|
267
275
|
output.space()
|
|
@@ -269,7 +277,7 @@ def print_assignment(self, output):
|
|
|
269
277
|
output.space()
|
|
270
278
|
if flattened:
|
|
271
279
|
output.print('ρσ_flatten')
|
|
272
|
-
output.with_parens(def():self.right.print(output);)
|
|
280
|
+
output.with_parens(def (): self.right.print(output);)
|
|
273
281
|
else:
|
|
274
282
|
self.right.print(output)
|
|
275
283
|
if is_node_type(left, AST_Array):
|
|
@@ -280,11 +288,12 @@ def print_assignment(self, output):
|
|
|
280
288
|
output.end_statement()
|
|
281
289
|
unpack_tuple(flat, output, True)
|
|
282
290
|
|
|
291
|
+
|
|
283
292
|
def print_assign(self, output):
|
|
284
293
|
if self.operator is '//=':
|
|
285
294
|
output.assign(self.left)
|
|
286
295
|
output.print('Math.floor')
|
|
287
|
-
output.with_parens(def():
|
|
296
|
+
output.with_parens(def ():
|
|
288
297
|
self.left.print(output)
|
|
289
298
|
output.space()
|
|
290
299
|
output.print('/')
|
|
@@ -300,11 +309,11 @@ def print_assign(self, output):
|
|
|
300
309
|
is_compound_assign = True
|
|
301
310
|
break
|
|
302
311
|
if is_compound_assign:
|
|
303
|
-
temp_rhs = new AST_SymbolRef({'name':'ρσ_chain_assign_temp'})
|
|
304
|
-
print_assignment(new AST_Assign({'left':temp_rhs, 'operator':'=', 'right':rhs}), output)
|
|
312
|
+
temp_rhs = new AST_SymbolRef({'name': 'ρσ_chain_assign_temp'})
|
|
313
|
+
print_assignment(new AST_Assign({'left': temp_rhs, 'operator': '=', 'right': rhs}), output)
|
|
305
314
|
for lhs in left_hand_sides:
|
|
306
315
|
output.end_statement(), output.indent()
|
|
307
|
-
print_assignment(new AST_Assign({'left':lhs, 'right':temp_rhs, 'operator':self.operator}), output)
|
|
316
|
+
print_assignment(new AST_Assign({'left': lhs, 'right': temp_rhs, 'operator': self.operator}), output)
|
|
308
317
|
else:
|
|
309
318
|
for lhs in left_hand_sides:
|
|
310
319
|
output.spaced(lhs, '=', '')
|
|
@@ -312,21 +321,23 @@ def print_assign(self, output):
|
|
|
312
321
|
else:
|
|
313
322
|
print_assignment(self, output)
|
|
314
323
|
|
|
324
|
+
|
|
315
325
|
def print_conditional(self, output, condition, consequent, alternative):
|
|
316
326
|
condition, consequent, alternative = self.condition, self.consequent, self.alternative
|
|
317
|
-
output.with_parens(def():condition.print(output);)
|
|
327
|
+
output.with_parens(def (): condition.print(output);)
|
|
318
328
|
output.space()
|
|
319
|
-
output.print(
|
|
329
|
+
output.print('?')
|
|
320
330
|
output.space()
|
|
321
331
|
consequent.print(output)
|
|
322
332
|
output.space()
|
|
323
333
|
output.colon()
|
|
324
334
|
alternative.print(output)
|
|
325
335
|
|
|
336
|
+
|
|
326
337
|
def print_seq(output):
|
|
327
338
|
self = this
|
|
328
339
|
p = output.parent()
|
|
329
|
-
print_seq = def():
|
|
340
|
+
print_seq = def ():
|
|
330
341
|
self.car.print(output)
|
|
331
342
|
if self.cdr:
|
|
332
343
|
output.comma()
|
|
@@ -8,14 +8,15 @@ from ast import (
|
|
|
8
8
|
AST_Conditional, AST_Binary, AST_BlockStatement, is_node_type
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
+
|
|
11
12
|
def force_statement(stat, output):
|
|
12
13
|
if output.options.bracketize:
|
|
13
14
|
if not stat or is_node_type(stat, AST_EmptyStatement):
|
|
14
|
-
output.print(
|
|
15
|
+
output.print('{}')
|
|
15
16
|
elif is_node_type(stat, AST_BlockStatement):
|
|
16
17
|
stat.print(output)
|
|
17
18
|
else:
|
|
18
|
-
output.with_block(def():
|
|
19
|
+
output.with_block(def ():
|
|
19
20
|
output.indent()
|
|
20
21
|
stat.print(output)
|
|
21
22
|
output.newline()
|
|
@@ -26,6 +27,7 @@ def force_statement(stat, output):
|
|
|
26
27
|
else:
|
|
27
28
|
stat.print(output)
|
|
28
29
|
|
|
30
|
+
|
|
29
31
|
# return true if the node at the top of the stack (that means the
|
|
30
32
|
# innermost node in the current output) is lexically the first in
|
|
31
33
|
# a statement.
|
|
@@ -54,7 +56,7 @@ def declare_vars(vars, output):
|
|
|
54
56
|
# declare all variables as local, unless explictly set otherwise
|
|
55
57
|
if vars.length:
|
|
56
58
|
output.indent()
|
|
57
|
-
output.print(
|
|
59
|
+
output.print('var')
|
|
58
60
|
output.space()
|
|
59
61
|
for i, arg in enumerate(vars):
|
|
60
62
|
if i:
|
|
@@ -64,6 +66,7 @@ def declare_vars(vars, output):
|
|
|
64
66
|
output.semicolon()
|
|
65
67
|
output.newline()
|
|
66
68
|
|
|
69
|
+
|
|
67
70
|
def display_body(body, is_toplevel, output):
|
|
68
71
|
last = body.length - 1
|
|
69
72
|
for i, stmt in enumerate(body):
|
|
@@ -80,10 +83,10 @@ def display_complex_body(node, is_toplevel, output, function_preamble):
|
|
|
80
83
|
# this is a method, add 'var self = this'
|
|
81
84
|
if is_node_type(node, AST_Method) and not node.static:
|
|
82
85
|
output.indent()
|
|
83
|
-
output.print(
|
|
86
|
+
output.print('var')
|
|
84
87
|
output.space()
|
|
85
88
|
output.assign(node.argnames.args[0])
|
|
86
|
-
output.print(
|
|
89
|
+
output.print('this')
|
|
87
90
|
output.semicolon()
|
|
88
91
|
output.newline()
|
|
89
92
|
offset += 1
|
|
@@ -95,18 +98,19 @@ def display_complex_body(node, is_toplevel, output, function_preamble):
|
|
|
95
98
|
elif is_node_type(node, AST_Except):
|
|
96
99
|
if node.argname:
|
|
97
100
|
output.indent()
|
|
98
|
-
output.print(
|
|
101
|
+
output.print('var')
|
|
99
102
|
output.space()
|
|
100
103
|
output.assign(node.argname)
|
|
101
|
-
output.print(
|
|
104
|
+
output.print('ρσ_Exception')
|
|
102
105
|
output.semicolon()
|
|
103
106
|
output.newline()
|
|
104
107
|
|
|
105
108
|
display_body(node.body, is_toplevel, output)
|
|
106
109
|
|
|
110
|
+
|
|
107
111
|
def print_bracketed(node, output, complex, function_preamble, before, after):
|
|
108
112
|
if node.body.length > 0:
|
|
109
|
-
output.with_block(def():
|
|
113
|
+
output.with_block(def ():
|
|
110
114
|
if before:
|
|
111
115
|
before(output)
|
|
112
116
|
if complex:
|
|
@@ -118,14 +122,15 @@ def print_bracketed(node, output, complex, function_preamble, before, after):
|
|
|
118
122
|
)
|
|
119
123
|
else:
|
|
120
124
|
if before or after:
|
|
121
|
-
output.with_block(def():
|
|
125
|
+
output.with_block(def ():
|
|
122
126
|
if before:
|
|
123
127
|
before(output)
|
|
124
128
|
if after:
|
|
125
129
|
after(output)
|
|
126
130
|
)
|
|
127
131
|
else:
|
|
128
|
-
output.print(
|
|
132
|
+
output.print('{}')
|
|
133
|
+
|
|
129
134
|
|
|
130
135
|
def print_with(self, output):
|
|
131
136
|
exits = v'[]'
|
|
@@ -143,22 +148,22 @@ def print_with(self, output):
|
|
|
143
148
|
output.print(clause_name + '.__enter__()')
|
|
144
149
|
output.end_statement()
|
|
145
150
|
output.indent(), output.print('try'), output.space()
|
|
146
|
-
output.with_block(def():
|
|
151
|
+
output.with_block(def ():
|
|
147
152
|
output.indent()
|
|
148
153
|
self._do_print_body(output)
|
|
149
154
|
output.newline()
|
|
150
155
|
)
|
|
151
156
|
output.space(), output.print('catch(e)')
|
|
152
|
-
output.with_block(def():
|
|
157
|
+
output.with_block(def ():
|
|
153
158
|
output.indent(), output.assign('ρσ_with_exception'), output.print('e'), output.end_statement()
|
|
154
159
|
)
|
|
155
160
|
output.newline(), output.indent(), output.spaced('if', '(ρσ_with_exception', '===', 'undefined)')
|
|
156
|
-
output.with_block(def():
|
|
161
|
+
output.with_block(def ():
|
|
157
162
|
for clause in exits:
|
|
158
163
|
output.indent(), output.print(clause + '.__exit__()'), output.end_statement()
|
|
159
164
|
)
|
|
160
165
|
output.space(), output.print('else'), output.space()
|
|
161
|
-
output.with_block(def():
|
|
166
|
+
output.with_block(def ():
|
|
162
167
|
output.indent(), output.assign('ρσ_with_suppress'), output.print('false'), output.end_statement()
|
|
163
168
|
for clause in exits:
|
|
164
169
|
output.indent()
|
|
@@ -168,6 +173,7 @@ def print_with(self, output):
|
|
|
168
173
|
output.indent(), output.spaced('if', '(!ρσ_with_suppress)', 'throw ρσ_with_exception'), output.end_statement()
|
|
169
174
|
)
|
|
170
175
|
|
|
176
|
+
|
|
171
177
|
def print_assert(self, output):
|
|
172
178
|
if output.options.discard_asserts:
|
|
173
179
|
return
|
package/src/output/stream.pyj
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
3
|
from __python__ import hash_literals
|
|
4
4
|
|
|
5
|
-
from utils import make_predicate, defaults, repeat_string
|
|
6
5
|
from tokenizer import is_identifier_char
|
|
6
|
+
from utils import make_predicate, defaults, repeat_string
|
|
7
7
|
|
|
8
8
|
DANGEROUS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
|
|
9
9
|
|
|
@@ -11,6 +11,7 @@ DANGEROUS = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u2
|
|
|
11
11
|
# hot path. Avoids the full unicode-aware is_identifier_char() for the common case.
|
|
12
12
|
_IDENT_CHARS = v'(function(){var t=new Uint8Array(128),s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$";for(var i=0;i<s.length;i++)t[s.charCodeAt(i)]=1;return t;})()'
|
|
13
13
|
|
|
14
|
+
|
|
14
15
|
def _is_ident_char(ch):
|
|
15
16
|
code = ch.charCodeAt(0)
|
|
16
17
|
if code < 128:
|
|
@@ -26,20 +27,20 @@ def as_hex(code, sz):
|
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
def to_ascii(str_, identifier):
|
|
29
|
-
return str_.replace(/[\u0080-\uffff]/g, def(ch):
|
|
30
|
+
return str_.replace(/[\u0080-\uffff]/g, def (ch):
|
|
30
31
|
code = ch.charCodeAt(0).toString(16)
|
|
31
32
|
if code.length <= 2 and not identifier:
|
|
32
|
-
return
|
|
33
|
+
return '\\x' + as_hex(code, 2)
|
|
33
34
|
else:
|
|
34
35
|
return '\\u' + as_hex(code, 4)
|
|
35
|
-
|
|
36
36
|
)
|
|
37
37
|
|
|
38
|
+
|
|
38
39
|
def encode_string(str_):
|
|
39
|
-
return JSON.stringify(str_).replace(DANGEROUS, def(a):
|
|
40
|
+
return JSON.stringify(str_).replace(DANGEROUS, def (a):
|
|
40
41
|
return '\\u' + as_hex(a.charCodeAt(0), 4)
|
|
41
42
|
)
|
|
42
|
-
require_semi_colon_chars = make_predicate(
|
|
43
|
+
require_semi_colon_chars = make_predicate('( [ + * / - , .')
|
|
43
44
|
|
|
44
45
|
output_stream_defaults = {
|
|
45
46
|
'indent_start': 0,
|
|
@@ -63,12 +64,12 @@ output_stream_defaults = {
|
|
|
63
64
|
'keep_docstrings': False,
|
|
64
65
|
'discard_asserts': False,
|
|
65
66
|
'module_cache_dir': '',
|
|
66
|
-
'js_version':6,
|
|
67
|
+
'js_version': 6,
|
|
67
68
|
'write_name': True,
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
class OutputStream:
|
|
71
71
|
|
|
72
|
+
class OutputStream:
|
|
72
73
|
def __init__(self, options):
|
|
73
74
|
self.options = defaults(options, output_stream_defaults, True)
|
|
74
75
|
self._indentation = 0
|
|
@@ -86,11 +87,11 @@ class OutputStream:
|
|
|
86
87
|
self._source_map_segments = v'[]' if self.options.source_map else None
|
|
87
88
|
# Indentation string cache: avoids repeat_string() on every indent() call
|
|
88
89
|
self._indent_cache_val = -1
|
|
89
|
-
self._indent_cache_str =
|
|
90
|
+
self._indent_cache_str = ''
|
|
90
91
|
# Install no-op add_mapping on the instance when source maps are disabled,
|
|
91
92
|
# avoiding a null check and array push per node in the hot output walk.
|
|
92
93
|
if not self.options.source_map:
|
|
93
|
-
self.add_mapping = def(node): pass
|
|
94
|
+
self.add_mapping = def (node): pass
|
|
94
95
|
|
|
95
96
|
def new_try_else_counter(self):
|
|
96
97
|
self.try_else_counter += 1
|
|
@@ -110,7 +111,7 @@ class OutputStream:
|
|
|
110
111
|
target = self.options.indent_start + self._indentation - (back or 0) * self.options.indent_level
|
|
111
112
|
if target == self._indent_cache_val:
|
|
112
113
|
return self._indent_cache_str
|
|
113
|
-
s = repeat_string(
|
|
114
|
+
s = repeat_string(' ', target)
|
|
114
115
|
self._indent_cache_val = target
|
|
115
116
|
self._indent_cache_str = s
|
|
116
117
|
return s
|
|
@@ -121,19 +122,19 @@ class OutputStream:
|
|
|
121
122
|
|
|
122
123
|
def maybe_newline(self):
|
|
123
124
|
if self.options.max_line_len and self.current_col > self.options.max_line_len:
|
|
124
|
-
self.print(
|
|
125
|
+
self.print('\n')
|
|
125
126
|
|
|
126
127
|
def print(self, str_):
|
|
127
128
|
str_ = v"String(str_)"
|
|
128
129
|
ch = str_.charAt(0)
|
|
129
130
|
if self.might_need_semicolon:
|
|
130
|
-
if (not ch or
|
|
131
|
+
if (not ch or ';}'.indexOf(ch) < 0) and not /[;]$/.test(self._last):
|
|
131
132
|
if self.options.semicolons or require_semi_colon_chars[ch]:
|
|
132
|
-
self._fragments.push(
|
|
133
|
+
self._fragments.push(';')
|
|
133
134
|
self.current_col += 1
|
|
134
135
|
self.current_pos += 1
|
|
135
136
|
else:
|
|
136
|
-
self._fragments.push(
|
|
137
|
+
self._fragments.push('\n')
|
|
137
138
|
self.current_pos += 1
|
|
138
139
|
self.current_line += 1
|
|
139
140
|
self.current_col = 0
|
|
@@ -141,25 +142,23 @@ class OutputStream:
|
|
|
141
142
|
if not self.options.beautify:
|
|
142
143
|
self.might_need_space = False
|
|
143
144
|
|
|
144
|
-
|
|
145
145
|
self.might_need_semicolon = False
|
|
146
146
|
self.maybe_newline()
|
|
147
147
|
|
|
148
148
|
if not self.options.beautify and self.options.preserve_line and self._stack[self._stack.length - 1]:
|
|
149
149
|
target_line = self._stack[self._stack.length - 1].start.line
|
|
150
150
|
while self.current_line < target_line:
|
|
151
|
-
self._fragments.push(
|
|
151
|
+
self._fragments.push('\n')
|
|
152
152
|
self.current_pos += 1
|
|
153
153
|
self.current_line += 1
|
|
154
154
|
self.current_col = 0
|
|
155
155
|
self.might_need_space = False
|
|
156
156
|
|
|
157
|
-
|
|
158
157
|
if self.might_need_space:
|
|
159
158
|
prev = self.last_char()
|
|
160
|
-
if _is_ident_char(prev) and (_is_ident_char(ch) or ch is
|
|
159
|
+
if _is_ident_char(prev) and (_is_ident_char(ch) or ch is '\\')
|
|
161
160
|
or /^[\+\-\/]$/.test(ch) and ch is prev:
|
|
162
|
-
self._fragments.push(
|
|
161
|
+
self._fragments.push(' ')
|
|
163
162
|
self.current_col += 1
|
|
164
163
|
self.current_pos += 1
|
|
165
164
|
|
|
@@ -211,17 +210,17 @@ class OutputStream:
|
|
|
211
210
|
|
|
212
211
|
def newline(self):
|
|
213
212
|
if self.options.beautify:
|
|
214
|
-
self.print(
|
|
213
|
+
self.print('\n')
|
|
215
214
|
|
|
216
215
|
def semicolon(self):
|
|
217
216
|
if self.options.beautify:
|
|
218
|
-
self.print(
|
|
217
|
+
self.print(';')
|
|
219
218
|
else:
|
|
220
219
|
self.might_need_semicolon = True
|
|
221
220
|
|
|
222
221
|
def force_semicolon(self):
|
|
223
222
|
self.might_need_semicolon = False
|
|
224
|
-
self.print(
|
|
223
|
+
self.print(';')
|
|
225
224
|
|
|
226
225
|
def next_indent(self):
|
|
227
226
|
return self._indentation + self.options.indent_level
|
|
@@ -241,34 +240,34 @@ class OutputStream:
|
|
|
241
240
|
|
|
242
241
|
def with_block(self, cont):
|
|
243
242
|
ret = None
|
|
244
|
-
self.print(
|
|
243
|
+
self.print('{')
|
|
245
244
|
self.newline()
|
|
246
|
-
self.with_indent(self.next_indent(), def():
|
|
245
|
+
self.with_indent(self.next_indent(), def ():
|
|
247
246
|
nonlocal ret
|
|
248
247
|
ret = cont()
|
|
249
248
|
)
|
|
250
249
|
self.indent()
|
|
251
|
-
self.print(
|
|
250
|
+
self.print('}')
|
|
252
251
|
return ret
|
|
253
252
|
|
|
254
253
|
def with_parens(self, cont):
|
|
255
|
-
self.print(
|
|
254
|
+
self.print('(')
|
|
256
255
|
ret = cont()
|
|
257
|
-
self.print(
|
|
256
|
+
self.print(')')
|
|
258
257
|
return ret
|
|
259
258
|
|
|
260
259
|
def with_square(self, cont):
|
|
261
|
-
self.print(
|
|
260
|
+
self.print('[')
|
|
262
261
|
ret = cont()
|
|
263
|
-
self.print(
|
|
262
|
+
self.print(']')
|
|
264
263
|
return ret
|
|
265
264
|
|
|
266
265
|
def comma(self):
|
|
267
|
-
self.print(
|
|
266
|
+
self.print(',')
|
|
268
267
|
self.space()
|
|
269
268
|
|
|
270
269
|
def colon(self):
|
|
271
|
-
self.print(
|
|
270
|
+
self.print(':')
|
|
272
271
|
if self.options.space_colon:
|
|
273
272
|
self.space()
|
|
274
273
|
|
|
@@ -278,12 +277,12 @@ class OutputStream:
|
|
|
278
277
|
|
|
279
278
|
def assign(self, name):
|
|
280
279
|
# generates: '[name] = '
|
|
281
|
-
if jstype(name) is
|
|
280
|
+
if jstype(name) is 'string':
|
|
282
281
|
self.print(name)
|
|
283
282
|
else:
|
|
284
283
|
name.print(self)
|
|
285
284
|
self.space()
|
|
286
|
-
self.print(
|
|
285
|
+
self.print('=')
|
|
287
286
|
self.space()
|
|
288
287
|
|
|
289
288
|
def current_width(self):
|
package/src/output/utils.pyj
CHANGED
|
@@ -4,6 +4,7 @@ from __python__ import hash_literals
|
|
|
4
4
|
|
|
5
5
|
from ast import AST_BlockStatement, is_node_type
|
|
6
6
|
|
|
7
|
+
|
|
7
8
|
def best_of(a):
|
|
8
9
|
best = a[0]
|
|
9
10
|
len_ = best.length
|
|
@@ -13,38 +14,41 @@ def best_of(a):
|
|
|
13
14
|
len_ = best.length
|
|
14
15
|
return best
|
|
15
16
|
|
|
17
|
+
|
|
16
18
|
def make_num(num):
|
|
17
19
|
str_ = num.toString(10)
|
|
18
|
-
a = [
|
|
20
|
+
a = [str_.replace(/^0\./, '.').replace('e+', 'e')]
|
|
19
21
|
m = None
|
|
20
22
|
|
|
21
23
|
if Math.floor(num) is num:
|
|
22
24
|
if num >= 0:
|
|
23
|
-
a.push(
|
|
24
|
-
|
|
25
|
+
a.push('0x' + num.toString(16).toLowerCase(), # probably pointless
|
|
26
|
+
'0' + num.toString(8))
|
|
25
27
|
else:
|
|
26
|
-
a.push(
|
|
27
|
-
|
|
28
|
+
a.push('-0x' + (-num).toString(16).toLowerCase(), # probably pointless
|
|
29
|
+
'-0' + (-num).toString(8))
|
|
28
30
|
|
|
29
31
|
if m = /^(.*?)(0+)$/.exec(num):
|
|
30
|
-
a.push(m[1] +
|
|
32
|
+
a.push(m[1] + 'e' + m[2].length)
|
|
31
33
|
|
|
32
34
|
elif m = /^0?\.(0+)(.*)$/.exec(num):
|
|
33
|
-
a.push(m[2] +
|
|
35
|
+
a.push(m[2] + 'e-' + (m[1].length + m[2].length), str_.substr(str_.indexOf('.')))
|
|
34
36
|
|
|
35
37
|
return best_of(a)
|
|
36
38
|
|
|
39
|
+
|
|
37
40
|
def make_block(stmt, output):
|
|
38
41
|
if is_node_type(stmt, AST_BlockStatement):
|
|
39
42
|
stmt.print(output)
|
|
40
43
|
return
|
|
41
44
|
|
|
42
|
-
output.with_block(def():
|
|
45
|
+
output.with_block(def ():
|
|
43
46
|
output.indent()
|
|
44
47
|
stmt.print(output)
|
|
45
48
|
output.newline()
|
|
46
49
|
)
|
|
47
50
|
|
|
51
|
+
|
|
48
52
|
def create_doctring(docstrings):
|
|
49
53
|
ans = v'[]'
|
|
50
54
|
for ds in docstrings:
|