rapydscript-ns 0.8.0
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/.agignore +1 -0
- package/.gitattributes +4 -0
- package/.github/workflows/ci.yml +38 -0
- package/.github/workflows/web-repl-page-deploy.yml +42 -0
- package/=template.pyj +5 -0
- package/CHANGELOG.md +456 -0
- package/CONTRIBUTORS +13 -0
- package/HACKING.md +103 -0
- package/LICENSE +24 -0
- package/README.md +2512 -0
- package/TODO.md +327 -0
- package/add-toc-to-readme +2 -0
- package/bin/export +75 -0
- package/bin/rapydscript +70 -0
- package/bin/web-repl-export +102 -0
- package/build +3 -0
- package/package.json +46 -0
- package/publish.py +37 -0
- package/release/baselib-plain-pretty.js +4370 -0
- package/release/baselib-plain-ugly.js +3 -0
- package/release/compiler.js +18394 -0
- package/release/signatures.json +31 -0
- package/session.vim +4 -0
- package/setup.cfg +2 -0
- package/src/ast.pyj +1356 -0
- package/src/baselib-builtins.pyj +279 -0
- package/src/baselib-containers.pyj +723 -0
- package/src/baselib-errors.pyj +37 -0
- package/src/baselib-internal.pyj +421 -0
- package/src/baselib-itertools.pyj +97 -0
- package/src/baselib-str.pyj +798 -0
- package/src/compiler.pyj +36 -0
- package/src/errors.pyj +30 -0
- package/src/lib/aes.pyj +646 -0
- package/src/lib/collections.pyj +695 -0
- package/src/lib/elementmaker.pyj +83 -0
- package/src/lib/encodings.pyj +126 -0
- package/src/lib/functools.pyj +148 -0
- package/src/lib/gettext.pyj +569 -0
- package/src/lib/itertools.pyj +580 -0
- package/src/lib/math.pyj +193 -0
- package/src/lib/numpy.pyj +2101 -0
- package/src/lib/operator.pyj +11 -0
- package/src/lib/pythonize.pyj +20 -0
- package/src/lib/random.pyj +118 -0
- package/src/lib/re.pyj +470 -0
- package/src/lib/traceback.pyj +63 -0
- package/src/lib/uuid.pyj +77 -0
- package/src/monaco-language-service/analyzer.js +526 -0
- package/src/monaco-language-service/builtins.js +543 -0
- package/src/monaco-language-service/completions.js +498 -0
- package/src/monaco-language-service/diagnostics.js +643 -0
- package/src/monaco-language-service/dts.js +550 -0
- package/src/monaco-language-service/hover.js +121 -0
- package/src/monaco-language-service/index.js +386 -0
- package/src/monaco-language-service/scope.js +162 -0
- package/src/monaco-language-service/signature.js +144 -0
- package/src/output/__init__.pyj +0 -0
- package/src/output/classes.pyj +296 -0
- package/src/output/codegen.pyj +492 -0
- package/src/output/comments.pyj +45 -0
- package/src/output/exceptions.pyj +105 -0
- package/src/output/functions.pyj +491 -0
- package/src/output/literals.pyj +109 -0
- package/src/output/loops.pyj +444 -0
- package/src/output/modules.pyj +329 -0
- package/src/output/operators.pyj +429 -0
- package/src/output/statements.pyj +463 -0
- package/src/output/stream.pyj +309 -0
- package/src/output/treeshake.pyj +182 -0
- package/src/output/utils.pyj +72 -0
- package/src/parse.pyj +3106 -0
- package/src/string_interpolation.pyj +72 -0
- package/src/tokenizer.pyj +702 -0
- package/src/unicode_aliases.pyj +576 -0
- package/src/utils.pyj +192 -0
- package/test/_import_one.pyj +37 -0
- package/test/_import_two/__init__.pyj +11 -0
- package/test/_import_two/level2/__init__.pyj +0 -0
- package/test/_import_two/level2/deep.pyj +4 -0
- package/test/_import_two/other.pyj +6 -0
- package/test/_import_two/sub.pyj +13 -0
- package/test/aes_vectors.pyj +421 -0
- package/test/annotations.pyj +80 -0
- package/test/baselib.pyj +319 -0
- package/test/classes.pyj +452 -0
- package/test/collections.pyj +152 -0
- package/test/decorators.pyj +77 -0
- package/test/dict_spread.pyj +76 -0
- package/test/docstrings.pyj +39 -0
- package/test/elementmaker_test.pyj +45 -0
- package/test/ellipsis.pyj +49 -0
- package/test/functions.pyj +151 -0
- package/test/generators.pyj +41 -0
- package/test/generic.pyj +370 -0
- package/test/imports.pyj +72 -0
- package/test/internationalization.pyj +73 -0
- package/test/lint.pyj +164 -0
- package/test/loops.pyj +85 -0
- package/test/numpy.pyj +734 -0
- package/test/omit_function_metadata.pyj +20 -0
- package/test/regexp.pyj +55 -0
- package/test/repl.pyj +121 -0
- package/test/scoped_flags.pyj +76 -0
- package/test/starargs.pyj +506 -0
- package/test/starred_assign.pyj +104 -0
- package/test/str.pyj +198 -0
- package/test/subscript_tuple.pyj +53 -0
- package/test/unit/fixtures/fibonacci_expected.js +46 -0
- package/test/unit/index.js +2989 -0
- package/test/unit/language-service-builtins.js +815 -0
- package/test/unit/language-service-completions.js +1067 -0
- package/test/unit/language-service-dts.js +543 -0
- package/test/unit/language-service-hover.js +455 -0
- package/test/unit/language-service-scope.js +833 -0
- package/test/unit/language-service-signature.js +458 -0
- package/test/unit/language-service.js +705 -0
- package/test/unit/run-language-service.js +41 -0
- package/test/unit/web-repl.js +484 -0
- package/tools/build-language-service.js +190 -0
- package/tools/cli.js +547 -0
- package/tools/compile.js +219 -0
- package/tools/compiler.js +108 -0
- package/tools/completer.js +131 -0
- package/tools/embedded_compiler.js +251 -0
- package/tools/export.js +316 -0
- package/tools/gettext.js +185 -0
- package/tools/ini.js +65 -0
- package/tools/lint.js +705 -0
- package/tools/msgfmt.js +187 -0
- package/tools/repl.js +223 -0
- package/tools/self.js +162 -0
- package/tools/test.js +118 -0
- package/tools/utils.js +128 -0
- package/tools/web_repl.js +95 -0
- package/try +41 -0
- package/web-repl/env.js +74 -0
- package/web-repl/index.html +163 -0
- package/web-repl/language-service.js +4084 -0
- package/web-repl/main.js +254 -0
- package/web-repl/prism.css +139 -0
- package/web-repl/prism.js +113 -0
- package/web-repl/rapydscript.js +435 -0
- package/web-repl/sha1.js +25 -0
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
from __python__ import hash_literals
|
|
4
|
+
|
|
5
|
+
# globals:console,regenerate,writefile
|
|
6
|
+
|
|
7
|
+
from utils import noop
|
|
8
|
+
from parse import PRECEDENCE
|
|
9
|
+
from ast import (
|
|
10
|
+
AST_Array, AST_Assign, AST_BaseCall, AST_Binary, AST_BlockStatement, AST_Break,
|
|
11
|
+
AST_Class, AST_Conditional, AST_Constant, AST_Continue,
|
|
12
|
+
AST_Debugger, AST_Definitions, AST_Directive, AST_Do, AST_Dot, is_node_type,
|
|
13
|
+
AST_EmptyStatement, AST_Exit, AST_ExpressiveObject, AST_ForIn,
|
|
14
|
+
AST_ForJS, AST_Function, AST_Hole, AST_If, AST_Imports, AST_Infinity,
|
|
15
|
+
AST_Lambda, AST_ListComprehension, AST_LoopControl, AST_NaN, AST_NamedExpr, AST_New, AST_Node,
|
|
16
|
+
AST_Number, AST_Object, AST_ObjectKeyVal, AST_ObjectProperty, AST_ObjectSpread, AST_PropAccess,
|
|
17
|
+
AST_RegExp, AST_Return, AST_Set, AST_Seq, AST_SimpleStatement, AST_Splice,
|
|
18
|
+
AST_Statement, AST_StatementWithBody, AST_String, AST_Sub, AST_ItemAccess,
|
|
19
|
+
AST_Symbol, AST_This, AST_Throw, AST_Toplevel, AST_Try, AST_Unary,
|
|
20
|
+
AST_UnaryPrefix, AST_Undefined, AST_Var, AST_VarDef, AST_Assert,
|
|
21
|
+
AST_Verbatim, AST_While, AST_With, AST_Yield, AST_Await, TreeWalker, AST_Existential,
|
|
22
|
+
AST_Match, AST_AnnotatedAssign, AST_Super
|
|
23
|
+
)
|
|
24
|
+
from output.exceptions import print_try
|
|
25
|
+
from output.classes import print_class
|
|
26
|
+
from output.literals import print_array, print_obj_literal, print_object, print_set, print_regexp
|
|
27
|
+
from output.loops import print_do_loop, print_while_loop, print_for_loop_body, print_for_in, print_list_comprehension
|
|
28
|
+
from output.modules import print_top_level, print_imports
|
|
29
|
+
from output.comments import print_comments
|
|
30
|
+
from output.operators import (
|
|
31
|
+
print_getattr, print_getitem, print_rich_getitem, print_splice_assignment,
|
|
32
|
+
print_unary_prefix, print_binary_op, print_assign,
|
|
33
|
+
print_conditional, print_seq, print_existential, print_named_expr
|
|
34
|
+
)
|
|
35
|
+
from output.functions import print_function, print_function_call
|
|
36
|
+
from output.statements import print_bracketed, first_in_statement, force_statement, print_with, print_assert, print_match, print_annotated_assign
|
|
37
|
+
from output.utils import make_block, make_num
|
|
38
|
+
|
|
39
|
+
# -----[ code generators ]-----
|
|
40
|
+
def generate_code():
|
|
41
|
+
# -----[ utils ]-----
|
|
42
|
+
def DEFPRINT(nodetype, generator):
|
|
43
|
+
nodetype.prototype._codegen = generator
|
|
44
|
+
|
|
45
|
+
AST_Node.prototype.print = def(stream, force_parens):
|
|
46
|
+
self = this
|
|
47
|
+
generator = self._codegen
|
|
48
|
+
stream.push_node(self)
|
|
49
|
+
if force_parens or self.needs_parens(stream):
|
|
50
|
+
stream.with_parens(def():
|
|
51
|
+
self.add_comments(stream)
|
|
52
|
+
generator(self, stream)
|
|
53
|
+
)
|
|
54
|
+
else:
|
|
55
|
+
self.add_comments(stream)
|
|
56
|
+
generator(self, stream)
|
|
57
|
+
|
|
58
|
+
stream.pop_node()
|
|
59
|
+
|
|
60
|
+
# -----[ comments ]-----
|
|
61
|
+
AST_Node.prototype.add_comments = def(output):
|
|
62
|
+
if not is_node_type(this, AST_Toplevel):
|
|
63
|
+
print_comments(this, output)
|
|
64
|
+
|
|
65
|
+
# -----[ PARENTHESES ]-----
|
|
66
|
+
def PARENS(nodetype, func):
|
|
67
|
+
nodetype.prototype.needs_parens = func
|
|
68
|
+
|
|
69
|
+
PARENS(AST_Node, def():
|
|
70
|
+
return False
|
|
71
|
+
)
|
|
72
|
+
# a function expression needs parens around it when it's provably
|
|
73
|
+
# the first token to appear in a statement.
|
|
74
|
+
PARENS(AST_Function, def(output):
|
|
75
|
+
return first_in_statement(output)
|
|
76
|
+
)
|
|
77
|
+
# same goes for an object literal, because otherwise it would be
|
|
78
|
+
# interpreted as a block of code.
|
|
79
|
+
PARENS(AST_Object, def(output):
|
|
80
|
+
return first_in_statement(output)
|
|
81
|
+
)
|
|
82
|
+
PARENS(AST_Unary, def(output):
|
|
83
|
+
p = output.parent()
|
|
84
|
+
return is_node_type(p, AST_PropAccess) and p.expression is this
|
|
85
|
+
)
|
|
86
|
+
PARENS(AST_Seq, def(output):
|
|
87
|
+
p = output.parent()
|
|
88
|
+
return is_node_type(p, AST_Unary) or is_node_type(p, AST_VarDef) or is_node_type(p, AST_Dot) or is_node_type(p, AST_ObjectProperty) or is_node_type(p, AST_Conditional)
|
|
89
|
+
)
|
|
90
|
+
PARENS(AST_Binary, def(output):
|
|
91
|
+
p = output.parent()
|
|
92
|
+
# (foo && bar)()
|
|
93
|
+
if is_node_type(p, AST_BaseCall) and p.expression is this:
|
|
94
|
+
return True
|
|
95
|
+
|
|
96
|
+
# typeof (foo && bar)
|
|
97
|
+
if is_node_type(p, AST_Unary):
|
|
98
|
+
return True
|
|
99
|
+
|
|
100
|
+
# (foo && bar)["prop"], (foo && bar).prop
|
|
101
|
+
if is_node_type(p, AST_PropAccess) and p.expression is this:
|
|
102
|
+
return True
|
|
103
|
+
|
|
104
|
+
# this deals with precedence: 3 * (2 + 1)
|
|
105
|
+
if is_node_type(p, AST_Binary):
|
|
106
|
+
po = p.operator
|
|
107
|
+
pp = PRECEDENCE[po]
|
|
108
|
+
so = this.operator
|
|
109
|
+
sp = PRECEDENCE[so]
|
|
110
|
+
if pp > sp or pp is sp and this is p.right and not (so is po and (so is "*" or so is "&&" or so is "||")):
|
|
111
|
+
return True
|
|
112
|
+
)
|
|
113
|
+
PARENS(AST_PropAccess, def(output):
|
|
114
|
+
p = output.parent()
|
|
115
|
+
if is_node_type(p, AST_New) and p.expression is this:
|
|
116
|
+
# i.e. new (foo.bar().baz)
|
|
117
|
+
#
|
|
118
|
+
# if there's one call into this subtree, then we need
|
|
119
|
+
# parens around it too, otherwise the call will be
|
|
120
|
+
# interpreted as passing the arguments to the upper New
|
|
121
|
+
# expression.
|
|
122
|
+
try:
|
|
123
|
+
this.walk(new TreeWalker(def(node):
|
|
124
|
+
if is_node_type(node, AST_BaseCall):
|
|
125
|
+
raise p
|
|
126
|
+
))
|
|
127
|
+
except as ex:
|
|
128
|
+
if ex is not p:
|
|
129
|
+
raise ex
|
|
130
|
+
return True
|
|
131
|
+
)
|
|
132
|
+
PARENS(AST_BaseCall, def(output):
|
|
133
|
+
p = output.parent()
|
|
134
|
+
return is_node_type(p, AST_New) and p.expression is this
|
|
135
|
+
)
|
|
136
|
+
PARENS(AST_New, def(output):
|
|
137
|
+
p = output.parent()
|
|
138
|
+
if this.args.length is 0 and (is_node_type(p, AST_PropAccess) or is_node_type(p, AST_BaseCall) and p.expression is this):
|
|
139
|
+
# (new foo)(bar)
|
|
140
|
+
return True
|
|
141
|
+
)
|
|
142
|
+
PARENS(AST_Number, def(output):
|
|
143
|
+
p = output.parent()
|
|
144
|
+
if this.value < 0 and is_node_type(p, AST_PropAccess) and p.expression is this:
|
|
145
|
+
return True
|
|
146
|
+
)
|
|
147
|
+
PARENS(AST_NaN, def(output):
|
|
148
|
+
p = output.parent()
|
|
149
|
+
if is_node_type(p, AST_PropAccess) and p.expression is this:
|
|
150
|
+
return True
|
|
151
|
+
)
|
|
152
|
+
def assign_and_conditional_paren_rules(output):
|
|
153
|
+
p = output.parent()
|
|
154
|
+
# !(a = false) → true
|
|
155
|
+
if is_node_type(p, AST_Unary):
|
|
156
|
+
return True
|
|
157
|
+
|
|
158
|
+
# 1 + (a = 2) + 3 → 6, side effect setting a = 2
|
|
159
|
+
if is_node_type(p, AST_Binary) and not (is_node_type(p, AST_Assign)):
|
|
160
|
+
return True
|
|
161
|
+
|
|
162
|
+
# (a = func)() —or— new (a = Object)()
|
|
163
|
+
if is_node_type(p, AST_BaseCall) and p.expression is this:
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
# bar if a = foo else baz
|
|
167
|
+
if is_node_type(p, AST_Conditional) and p.condition is this:
|
|
168
|
+
return True
|
|
169
|
+
|
|
170
|
+
# (a = foo)["prop"] —or— (a = foo).prop
|
|
171
|
+
if is_node_type(p, AST_PropAccess) and p.expression is this:
|
|
172
|
+
return True
|
|
173
|
+
|
|
174
|
+
PARENS(AST_Assign, assign_and_conditional_paren_rules)
|
|
175
|
+
PARENS(AST_Conditional, assign_and_conditional_paren_rules)
|
|
176
|
+
# AST_NamedExpr always needs parens when not already the outermost expression
|
|
177
|
+
# (print_named_expr itself wraps in parens, so needs_parens stays false)
|
|
178
|
+
PARENS(AST_NamedExpr, def():
|
|
179
|
+
return False
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# -----[ PRINTERS ]-----
|
|
183
|
+
DEFPRINT(AST_Directive, def(self, output):
|
|
184
|
+
output.print_string(self.value)
|
|
185
|
+
output.semicolon()
|
|
186
|
+
)
|
|
187
|
+
DEFPRINT(AST_Debugger, def(self, output):
|
|
188
|
+
output.print("debugger")
|
|
189
|
+
output.semicolon()
|
|
190
|
+
)
|
|
191
|
+
AST_StatementWithBody.prototype._do_print_body = def(output):
|
|
192
|
+
force_statement(this.body, output)
|
|
193
|
+
|
|
194
|
+
DEFPRINT(AST_Statement, def(self, output):
|
|
195
|
+
self.body.print(output)
|
|
196
|
+
output.semicolon()
|
|
197
|
+
)
|
|
198
|
+
DEFPRINT(AST_Toplevel, print_top_level)
|
|
199
|
+
|
|
200
|
+
DEFPRINT(AST_Imports, print_imports)
|
|
201
|
+
|
|
202
|
+
DEFPRINT(AST_SimpleStatement, def(self, output):
|
|
203
|
+
if not (is_node_type(self.body, AST_EmptyStatement)):
|
|
204
|
+
self.body.print(output)
|
|
205
|
+
output.semicolon()
|
|
206
|
+
)
|
|
207
|
+
DEFPRINT(AST_AnnotatedAssign, print_annotated_assign)
|
|
208
|
+
DEFPRINT(AST_BlockStatement, def(self, output):
|
|
209
|
+
print_bracketed(self, output)
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
DEFPRINT(AST_EmptyStatement, def(self, output):
|
|
213
|
+
pass
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
DEFPRINT(AST_Do, print_do_loop)
|
|
217
|
+
|
|
218
|
+
DEFPRINT(AST_While, print_while_loop)
|
|
219
|
+
|
|
220
|
+
AST_ForIn.prototype._do_print_body = print_for_loop_body
|
|
221
|
+
|
|
222
|
+
DEFPRINT(AST_ForIn, print_for_in)
|
|
223
|
+
|
|
224
|
+
AST_ForJS.prototype._do_print_body = def(output):
|
|
225
|
+
self = this
|
|
226
|
+
output.with_block(def():
|
|
227
|
+
for stmt in self.body.body:
|
|
228
|
+
output.indent()
|
|
229
|
+
stmt.print(output)
|
|
230
|
+
output.newline()
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
DEFPRINT(AST_ForJS, def(self, output):
|
|
234
|
+
output.print("for")
|
|
235
|
+
output.space()
|
|
236
|
+
output.with_parens(def():
|
|
237
|
+
self.condition.print(output)
|
|
238
|
+
)
|
|
239
|
+
output.space()
|
|
240
|
+
self._do_print_body(output)
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
DEFPRINT(AST_ListComprehension, print_list_comprehension)
|
|
244
|
+
|
|
245
|
+
DEFPRINT(AST_With, print_with)
|
|
246
|
+
|
|
247
|
+
DEFPRINT(AST_Assert, print_assert)
|
|
248
|
+
|
|
249
|
+
DEFPRINT(AST_Match, print_match)
|
|
250
|
+
|
|
251
|
+
AST_Lambda.prototype._do_print = print_function
|
|
252
|
+
|
|
253
|
+
DEFPRINT(AST_Lambda, def(self, output):
|
|
254
|
+
self._do_print(output)
|
|
255
|
+
)
|
|
256
|
+
AST_Class.prototype._do_print = print_class
|
|
257
|
+
DEFPRINT(AST_Class, def(self, output):
|
|
258
|
+
self._do_print(output)
|
|
259
|
+
)
|
|
260
|
+
# -----[ exits ]-----
|
|
261
|
+
AST_Exit.prototype._do_print = def(output, kind):
|
|
262
|
+
self = this
|
|
263
|
+
output.print(kind)
|
|
264
|
+
if self.value:
|
|
265
|
+
output.space()
|
|
266
|
+
self.value.print(output)
|
|
267
|
+
|
|
268
|
+
output.semicolon()
|
|
269
|
+
|
|
270
|
+
DEFPRINT(AST_Yield, def(self, output):
|
|
271
|
+
self._do_print(output, "yield" + ('*' if self.is_yield_from else ''))
|
|
272
|
+
)
|
|
273
|
+
DEFPRINT(AST_Await, def(self, output):
|
|
274
|
+
output.print('await')
|
|
275
|
+
output.space()
|
|
276
|
+
self.value.print(output)
|
|
277
|
+
)
|
|
278
|
+
DEFPRINT(AST_Return, def(self, output):
|
|
279
|
+
self._do_print(output, "return")
|
|
280
|
+
)
|
|
281
|
+
DEFPRINT(AST_Throw, def(self, output):
|
|
282
|
+
self._do_print(output, "throw")
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
# -----[ loop control ]-----
|
|
286
|
+
AST_LoopControl.prototype._do_print = def(output, kind):
|
|
287
|
+
output.print(kind)
|
|
288
|
+
if this.label:
|
|
289
|
+
output.space()
|
|
290
|
+
this.label.print(output)
|
|
291
|
+
|
|
292
|
+
output.semicolon()
|
|
293
|
+
|
|
294
|
+
DEFPRINT(AST_Break, def(self, output):
|
|
295
|
+
self._do_print(output, "break")
|
|
296
|
+
)
|
|
297
|
+
DEFPRINT(AST_Continue, def(self, output):
|
|
298
|
+
self._do_print(output, "continue")
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
# -----[ if ]-----
|
|
302
|
+
def make_then(self, output):
|
|
303
|
+
if output.options.bracketize:
|
|
304
|
+
make_block(self.body, output)
|
|
305
|
+
return
|
|
306
|
+
|
|
307
|
+
# The squeezer replaces "block"-s that contain only a single
|
|
308
|
+
# statement with the statement itself; technically, the AST
|
|
309
|
+
# is correct, but this can create problems when we output an
|
|
310
|
+
# IF having an ELSE clause where the THEN clause ends in an
|
|
311
|
+
# IF *without* an ELSE block (then the outer ELSE would refer
|
|
312
|
+
# to the inner IF). This function checks for this case and
|
|
313
|
+
# adds the block brackets if needed.
|
|
314
|
+
if not self.body:
|
|
315
|
+
return output.force_semicolon()
|
|
316
|
+
|
|
317
|
+
if is_node_type(self.body, AST_Do) and output.options.ie_proof:
|
|
318
|
+
# https://github.com/mishoo/RapydScript/issues/#issue/57 IE
|
|
319
|
+
# croaks with "syntax error" on code like this: if (foo)
|
|
320
|
+
# do ... while(cond); else ... we need block brackets
|
|
321
|
+
# around do/while
|
|
322
|
+
make_block(self.body, output)
|
|
323
|
+
return
|
|
324
|
+
|
|
325
|
+
b = self.body
|
|
326
|
+
while True:
|
|
327
|
+
if is_node_type(b, AST_If):
|
|
328
|
+
if not b.alternative:
|
|
329
|
+
make_block(self.body, output)
|
|
330
|
+
return
|
|
331
|
+
|
|
332
|
+
b = b.alternative
|
|
333
|
+
elif is_node_type(b, AST_StatementWithBody):
|
|
334
|
+
b = b.body
|
|
335
|
+
else:
|
|
336
|
+
break
|
|
337
|
+
|
|
338
|
+
force_statement(self.body, output)
|
|
339
|
+
|
|
340
|
+
DEFPRINT(AST_If, def(self, output):
|
|
341
|
+
output.print("if")
|
|
342
|
+
output.space()
|
|
343
|
+
output.with_parens(def(): self.condition.print(output);)
|
|
344
|
+
output.space()
|
|
345
|
+
if self.alternative:
|
|
346
|
+
make_then(self, output)
|
|
347
|
+
output.space()
|
|
348
|
+
output.print("else")
|
|
349
|
+
output.space()
|
|
350
|
+
force_statement(self.alternative, output)
|
|
351
|
+
else:
|
|
352
|
+
self._do_print_body(output)
|
|
353
|
+
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
# -----[ exceptions ]-----
|
|
357
|
+
DEFPRINT(AST_Try, print_try)
|
|
358
|
+
|
|
359
|
+
# -----[ var/const ]-----
|
|
360
|
+
AST_Definitions.prototype._do_print = def(output, kind):
|
|
361
|
+
output.print(kind)
|
|
362
|
+
output.space()
|
|
363
|
+
for i, def_ in enumerate(this.definitions):
|
|
364
|
+
if i:
|
|
365
|
+
output.comma()
|
|
366
|
+
def_.print(output)
|
|
367
|
+
p = output.parent()
|
|
368
|
+
in_for = is_node_type(p, AST_ForIn)
|
|
369
|
+
avoid_semicolon = in_for and p.init is this
|
|
370
|
+
if not avoid_semicolon:
|
|
371
|
+
output.semicolon()
|
|
372
|
+
|
|
373
|
+
DEFPRINT(AST_Var, def(self, output):
|
|
374
|
+
self._do_print(output, "var")
|
|
375
|
+
)
|
|
376
|
+
def parenthesize_for_noin(node, output, noin):
|
|
377
|
+
if not noin:
|
|
378
|
+
node.print(output)
|
|
379
|
+
else:
|
|
380
|
+
try:
|
|
381
|
+
# need to take some precautions here:
|
|
382
|
+
# https://github.com/mishoo/RapydScript2/issues/60
|
|
383
|
+
node.walk(new TreeWalker(def(node):
|
|
384
|
+
if is_node_type(node, AST_Binary) and node.operator is "in":
|
|
385
|
+
raise output
|
|
386
|
+
))
|
|
387
|
+
node.print(output)
|
|
388
|
+
except as ex:
|
|
389
|
+
if ex is not output:
|
|
390
|
+
raise ex
|
|
391
|
+
node.print(output, True)
|
|
392
|
+
|
|
393
|
+
DEFPRINT(AST_VarDef, def(self, output):
|
|
394
|
+
self.name.print(output)
|
|
395
|
+
if self.value:
|
|
396
|
+
output.assign("")
|
|
397
|
+
# output.space()
|
|
398
|
+
# output.print("=")
|
|
399
|
+
# output.space()
|
|
400
|
+
p = output.parent(1)
|
|
401
|
+
noin = is_node_type(p, AST_ForIn)
|
|
402
|
+
parenthesize_for_noin(self.value, output, noin)
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
# -----[ other expressions ]-----
|
|
406
|
+
DEFPRINT(AST_BaseCall, print_function_call)
|
|
407
|
+
|
|
408
|
+
DEFPRINT(AST_Super, def(self, output):
|
|
409
|
+
# super() used without a method call — emit a runtime error
|
|
410
|
+
output.print('(function(){ throw new TypeError("super() must be used as super().method(...)"); })()')
|
|
411
|
+
)
|
|
412
|
+
|
|
413
|
+
AST_Seq.prototype._do_print = print_seq
|
|
414
|
+
|
|
415
|
+
DEFPRINT(AST_Seq, def(self, output):
|
|
416
|
+
self._do_print(output)
|
|
417
|
+
)
|
|
418
|
+
DEFPRINT(AST_Dot, print_getattr)
|
|
419
|
+
|
|
420
|
+
DEFPRINT(AST_Sub, print_getitem)
|
|
421
|
+
|
|
422
|
+
DEFPRINT(AST_ItemAccess, print_rich_getitem)
|
|
423
|
+
|
|
424
|
+
DEFPRINT(AST_Splice, print_splice_assignment)
|
|
425
|
+
|
|
426
|
+
DEFPRINT(AST_UnaryPrefix, print_unary_prefix)
|
|
427
|
+
|
|
428
|
+
DEFPRINT(AST_Binary, print_binary_op)
|
|
429
|
+
|
|
430
|
+
DEFPRINT(AST_Existential, print_existential)
|
|
431
|
+
|
|
432
|
+
DEFPRINT(AST_Assign, print_assign)
|
|
433
|
+
|
|
434
|
+
DEFPRINT(AST_Conditional, print_conditional)
|
|
435
|
+
|
|
436
|
+
DEFPRINT(AST_NamedExpr, print_named_expr)
|
|
437
|
+
|
|
438
|
+
# -----[ literals ]-----
|
|
439
|
+
DEFPRINT(AST_Array, print_array)
|
|
440
|
+
|
|
441
|
+
DEFPRINT(AST_ExpressiveObject, print_obj_literal)
|
|
442
|
+
|
|
443
|
+
DEFPRINT(AST_Object, print_object)
|
|
444
|
+
|
|
445
|
+
DEFPRINT(AST_ObjectKeyVal, def(self, output):
|
|
446
|
+
self.key.print(output)
|
|
447
|
+
output.colon()
|
|
448
|
+
self.value.print(output)
|
|
449
|
+
)
|
|
450
|
+
DEFPRINT(AST_ObjectSpread, def(self, output):
|
|
451
|
+
output.print('**')
|
|
452
|
+
self.value.print(output)
|
|
453
|
+
)
|
|
454
|
+
DEFPRINT(AST_Set, print_set)
|
|
455
|
+
|
|
456
|
+
AST_Symbol.prototype.definition = def():
|
|
457
|
+
return this.thedef
|
|
458
|
+
|
|
459
|
+
DEFPRINT(AST_Symbol, def(self, output):
|
|
460
|
+
def_ = self.definition()
|
|
461
|
+
output.print_name((def_.mangled_name or def_.name) if def_ else self.name)
|
|
462
|
+
)
|
|
463
|
+
DEFPRINT(AST_Undefined, def(self, output):
|
|
464
|
+
output.print("void 0")
|
|
465
|
+
)
|
|
466
|
+
DEFPRINT(ρσ_modules.ast.AST_Ellipsis, def(self, output):
|
|
467
|
+
output.print("ρσ_Ellipsis")
|
|
468
|
+
)
|
|
469
|
+
DEFPRINT(AST_Hole, noop)
|
|
470
|
+
|
|
471
|
+
DEFPRINT(AST_Infinity, def(self, output):
|
|
472
|
+
output.print("1/0")
|
|
473
|
+
)
|
|
474
|
+
DEFPRINT(AST_NaN, def(self, output):
|
|
475
|
+
output.print("0/0")
|
|
476
|
+
)
|
|
477
|
+
DEFPRINT(AST_This, def(self, output):
|
|
478
|
+
output.print("this")
|
|
479
|
+
)
|
|
480
|
+
DEFPRINT(AST_Constant, def(self, output):
|
|
481
|
+
output.print(self.value)
|
|
482
|
+
)
|
|
483
|
+
DEFPRINT(AST_String, def(self, output):
|
|
484
|
+
output.print_string(self.value)
|
|
485
|
+
)
|
|
486
|
+
DEFPRINT(AST_Verbatim, def(self, output):
|
|
487
|
+
output.print(self.value)
|
|
488
|
+
)
|
|
489
|
+
DEFPRINT(AST_Number, def(self, output):
|
|
490
|
+
output.print(make_num(self.value))
|
|
491
|
+
)
|
|
492
|
+
DEFPRINT(AST_RegExp, print_regexp)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
from __python__ import hash_literals
|
|
4
|
+
|
|
5
|
+
from ast import AST_Exit, is_node_type
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def output_comments(comments, output, nlb):
|
|
9
|
+
for comm in comments:
|
|
10
|
+
if comm.type is "comment1":
|
|
11
|
+
output.print("//" + comm.value + "\n")
|
|
12
|
+
output.indent()
|
|
13
|
+
elif comm.type is "comment2":
|
|
14
|
+
output.print("/*" + comm.value + "*/")
|
|
15
|
+
if nlb:
|
|
16
|
+
output.print("\n")
|
|
17
|
+
output.indent()
|
|
18
|
+
else:
|
|
19
|
+
output.space()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def print_comments(self, output):
|
|
23
|
+
c = output.options.comments
|
|
24
|
+
if c:
|
|
25
|
+
start = self.start
|
|
26
|
+
if start and not start._comments_dumped:
|
|
27
|
+
start._comments_dumped = True
|
|
28
|
+
comments = start.comments_before
|
|
29
|
+
# XXX: ugly fix for https://github.com/mishoo/RapydScript2/issues/112
|
|
30
|
+
# if this node is `return` or `throw`, we cannot allow comments before
|
|
31
|
+
# the returned or thrown value.
|
|
32
|
+
if is_node_type(self, AST_Exit) and self.value and self.value.start.comments_before and self.value.start.comments_before.length > 0:
|
|
33
|
+
comments = (comments or v'[]').concat(self.value.start.comments_before)
|
|
34
|
+
self.value.start.comments_before = v'[]'
|
|
35
|
+
|
|
36
|
+
if c.test:
|
|
37
|
+
comments = comments.filter(def(comment):
|
|
38
|
+
return c.test(comment.value)
|
|
39
|
+
)
|
|
40
|
+
elif jstype(c) is "function":
|
|
41
|
+
comments = comments.filter(def(comment):
|
|
42
|
+
return c(self, comment)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
output_comments(comments, output, start.nlb)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
from __python__ import hash_literals
|
|
4
|
+
|
|
5
|
+
from output.statements import print_bracketed
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def print_try(self, output):
|
|
9
|
+
else_var_name = None
|
|
10
|
+
def update_output_var(output):
|
|
11
|
+
output.indent(), output.assign(else_var_name), output.print('true'), output.end_statement()
|
|
12
|
+
if self.belse:
|
|
13
|
+
else_var_name = output.new_try_else_counter()
|
|
14
|
+
output.assign('var ' + else_var_name), output.print('false'), output.end_statement(), output.indent()
|
|
15
|
+
output.print("try")
|
|
16
|
+
output.space()
|
|
17
|
+
print_bracketed(self, output, False, None, None, update_output_var if else_var_name else None)
|
|
18
|
+
if self.bcatch:
|
|
19
|
+
output.space()
|
|
20
|
+
print_catch(self.bcatch, output)
|
|
21
|
+
|
|
22
|
+
if self.bfinally:
|
|
23
|
+
output.space()
|
|
24
|
+
print_finally(self.bfinally, output, self.belse, else_var_name)
|
|
25
|
+
elif self.belse:
|
|
26
|
+
output.newline()
|
|
27
|
+
print_else(self.belse, else_var_name, output)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def print_catch(self, output):
|
|
31
|
+
output.print("catch")
|
|
32
|
+
output.space()
|
|
33
|
+
output.with_parens(def():
|
|
34
|
+
output.print("ρσ_Exception")
|
|
35
|
+
)
|
|
36
|
+
output.space()
|
|
37
|
+
output.with_block(def():
|
|
38
|
+
output.indent()
|
|
39
|
+
output.spaced('ρσ_last_exception', '=', 'ρσ_Exception'), output.end_statement()
|
|
40
|
+
output.indent()
|
|
41
|
+
no_default = True
|
|
42
|
+
for i, exception in enumerate(self.body):
|
|
43
|
+
if i:
|
|
44
|
+
output.print("else ")
|
|
45
|
+
|
|
46
|
+
if exception.errors.length:
|
|
47
|
+
output.print("if")
|
|
48
|
+
output.space()
|
|
49
|
+
output.with_parens(def():
|
|
50
|
+
for i, err in enumerate(exception.errors):
|
|
51
|
+
if i:
|
|
52
|
+
output.newline()
|
|
53
|
+
output.indent()
|
|
54
|
+
output.print("||")
|
|
55
|
+
output.space()
|
|
56
|
+
|
|
57
|
+
output.print("ρσ_Exception")
|
|
58
|
+
output.space()
|
|
59
|
+
output.print("instanceof")
|
|
60
|
+
output.space()
|
|
61
|
+
if err.name is 'Exception':
|
|
62
|
+
output.print('Error')
|
|
63
|
+
else:
|
|
64
|
+
err.print(output)
|
|
65
|
+
)
|
|
66
|
+
output.space()
|
|
67
|
+
else:
|
|
68
|
+
no_default = False
|
|
69
|
+
print_bracketed(exception, output, True)
|
|
70
|
+
output.space()
|
|
71
|
+
if no_default:
|
|
72
|
+
output.print("else")
|
|
73
|
+
output.space()
|
|
74
|
+
output.with_block(def():
|
|
75
|
+
output.indent()
|
|
76
|
+
output.print("throw")
|
|
77
|
+
output.space()
|
|
78
|
+
output.print("ρσ_Exception")
|
|
79
|
+
output.semicolon()
|
|
80
|
+
output.newline()
|
|
81
|
+
)
|
|
82
|
+
output.newline()
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def print_finally(self, output, belse, else_var_name):
|
|
87
|
+
output.print("finally")
|
|
88
|
+
output.space()
|
|
89
|
+
if else_var_name:
|
|
90
|
+
output.with_block(def():
|
|
91
|
+
output.indent(), output.print("try")
|
|
92
|
+
output.space()
|
|
93
|
+
output.with_block(def():
|
|
94
|
+
print_else(belse, else_var_name, output)
|
|
95
|
+
)
|
|
96
|
+
print_finally(self, output)
|
|
97
|
+
)
|
|
98
|
+
else:
|
|
99
|
+
print_bracketed(self, output)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def print_else(self, else_var_name, output):
|
|
103
|
+
output.indent(), output.spaced('if', '(' + else_var_name + ')')
|
|
104
|
+
output.space()
|
|
105
|
+
print_bracketed(self, output)
|