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,296 @@
|
|
|
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 ast import AST_Class, AST_Method, is_node_type
|
|
6
|
+
from output.functions import decorate, function_definition, function_annotation
|
|
7
|
+
from output.utils import create_doctring
|
|
8
|
+
from utils import has_prop
|
|
9
|
+
|
|
10
|
+
def print_class(output):
|
|
11
|
+
self = this
|
|
12
|
+
if self.external:
|
|
13
|
+
return
|
|
14
|
+
|
|
15
|
+
def class_def(method, is_var):
|
|
16
|
+
output.indent()
|
|
17
|
+
self.name.print(output)
|
|
18
|
+
if not is_var and method and has_prop(self.static, method):
|
|
19
|
+
output.assign("." + method)
|
|
20
|
+
else:
|
|
21
|
+
if is_var:
|
|
22
|
+
output.assign(".prototype[" + method + "]")
|
|
23
|
+
else:
|
|
24
|
+
output.assign(".prototype" + (("." + method) if method else ""))
|
|
25
|
+
|
|
26
|
+
def define_method(stmt, is_property):
|
|
27
|
+
name = stmt.name.name
|
|
28
|
+
is_static = has_prop(self.static, name)
|
|
29
|
+
is_classmethod = stmt.is_classmethod
|
|
30
|
+
# classmethods are defined on the class itself (like statics), with cls=this
|
|
31
|
+
strip_first = not is_static
|
|
32
|
+
|
|
33
|
+
if is_classmethod:
|
|
34
|
+
# define on the class object (not prototype): ClassName.method = function(...)
|
|
35
|
+
output.indent()
|
|
36
|
+
self.name.print(output)
|
|
37
|
+
output.assign("." + name)
|
|
38
|
+
elif not is_property:
|
|
39
|
+
class_def(name)
|
|
40
|
+
|
|
41
|
+
# decorate the method
|
|
42
|
+
if stmt.decorators and stmt.decorators.length:
|
|
43
|
+
decorate(stmt.decorators, output, def():function_definition(stmt, output, strip_first, True);)
|
|
44
|
+
output.end_statement()
|
|
45
|
+
else:
|
|
46
|
+
function_definition(stmt, output, strip_first)
|
|
47
|
+
if not is_property:
|
|
48
|
+
output.end_statement()
|
|
49
|
+
if is_classmethod:
|
|
50
|
+
fname = self.name.name + '.' + name
|
|
51
|
+
function_annotation(stmt, output, strip_first, fname)
|
|
52
|
+
# prototype delegation so instances can call the classmethod
|
|
53
|
+
output.indent()
|
|
54
|
+
self.name.print(output)
|
|
55
|
+
output.assign('.prototype.' + name)
|
|
56
|
+
clsname = self.name.name
|
|
57
|
+
output.print('function() { return ' + clsname + '.' + name + '.apply(this.constructor, arguments); }')
|
|
58
|
+
output.end_statement()
|
|
59
|
+
else:
|
|
60
|
+
fname = self.name.name + ('.' if is_static else '.prototype.') + name
|
|
61
|
+
function_annotation(stmt, output, strip_first, fname)
|
|
62
|
+
|
|
63
|
+
def define_default_method(name, body):
|
|
64
|
+
class_def(name)
|
|
65
|
+
output.spaced('function', name, '()', '')
|
|
66
|
+
output.with_block(def(): output.indent(), body();)
|
|
67
|
+
output.end_statement()
|
|
68
|
+
|
|
69
|
+
def add_hidden_property(name, proceed):
|
|
70
|
+
output.indent(), output.print('Object.defineProperty(')
|
|
71
|
+
self.name.print(output), output.print('.prototype'), output.comma(), output.print(JSON.stringify(name)), output.comma()
|
|
72
|
+
output.spaced('{value:', ''), proceed(), output.print('})'), output.end_statement()
|
|
73
|
+
|
|
74
|
+
# generate constructor
|
|
75
|
+
def write_constructor():
|
|
76
|
+
output.print("function")
|
|
77
|
+
output.space()
|
|
78
|
+
self.name.print(output)
|
|
79
|
+
output.print("()")
|
|
80
|
+
output.space()
|
|
81
|
+
|
|
82
|
+
output.with_block(def():
|
|
83
|
+
output.indent()
|
|
84
|
+
cname = self.name.name
|
|
85
|
+
output.print('if (!(this instanceof ' + cname + ')) return new ' + cname + '(...arguments);')
|
|
86
|
+
output.newline()
|
|
87
|
+
output.indent()
|
|
88
|
+
output.spaced('if', '(this.ρσ_object_id', '===', 'undefined)', 'Object.defineProperty(this,', '"ρσ_object_id",', '{"value":++ρσ_object_counter})')
|
|
89
|
+
output.end_statement()
|
|
90
|
+
if self.bound.length:
|
|
91
|
+
output.indent()
|
|
92
|
+
self.name.print(output), output.print(".prototype.__bind_methods__.call(this)")
|
|
93
|
+
output.end_statement()
|
|
94
|
+
output.indent()
|
|
95
|
+
self.name.print(output)
|
|
96
|
+
output.print(".prototype.__init__.apply(this"), output.comma(), output.print('arguments)')
|
|
97
|
+
output.end_statement()
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
decorators = self.decorators or v'[]'
|
|
101
|
+
if decorators.length:
|
|
102
|
+
output.print('var ')
|
|
103
|
+
output.assign(self.name)
|
|
104
|
+
write_constructor()
|
|
105
|
+
output.semicolon()
|
|
106
|
+
else:
|
|
107
|
+
write_constructor()
|
|
108
|
+
output.newline()
|
|
109
|
+
if decorators.length:
|
|
110
|
+
output.indent()
|
|
111
|
+
self.name.print(output)
|
|
112
|
+
output.spaced('.ρσ_decorators', '=', '[')
|
|
113
|
+
num = decorators.length
|
|
114
|
+
for i in range(num):
|
|
115
|
+
decorators[i].expression.print(output)
|
|
116
|
+
output.spaced(',' if i < num - 1 else ']')
|
|
117
|
+
output.semicolon()
|
|
118
|
+
output.newline()
|
|
119
|
+
|
|
120
|
+
# inheritance
|
|
121
|
+
if self.parent:
|
|
122
|
+
output.indent()
|
|
123
|
+
output.print("ρσ_extends")
|
|
124
|
+
output.with_parens(def():
|
|
125
|
+
self.name.print(output)
|
|
126
|
+
output.comma()
|
|
127
|
+
self.parent.print(output)
|
|
128
|
+
)
|
|
129
|
+
output.end_statement()
|
|
130
|
+
|
|
131
|
+
# method binding
|
|
132
|
+
if self.bound.length:
|
|
133
|
+
seen_methods = Object.create(None)
|
|
134
|
+
add_hidden_property('__bind_methods__', def():
|
|
135
|
+
output.spaced('function', '()', '')
|
|
136
|
+
output.with_block(def():
|
|
137
|
+
if self.bases.length:
|
|
138
|
+
for v'var i = self.bases.length - 1; i >= 0; i--':
|
|
139
|
+
base = self.bases[i]
|
|
140
|
+
output.indent(), base.print(output), output.spaced('.prototype.__bind_methods__', '&&', '')
|
|
141
|
+
base.print(output), output.print('.prototype.__bind_methods__.call(this)')
|
|
142
|
+
output.end_statement()
|
|
143
|
+
for bname in self.bound:
|
|
144
|
+
if seen_methods[bname] or self.dynamic_properties[bname]:
|
|
145
|
+
continue
|
|
146
|
+
seen_methods[bname] = True
|
|
147
|
+
output.indent(), output.assign('this.' + bname)
|
|
148
|
+
self.name.print(output), output.print('.prototype.' + bname + '.bind(this)')
|
|
149
|
+
output.end_statement()
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
# dynamic properties
|
|
154
|
+
property_names = Object.keys(self.dynamic_properties)
|
|
155
|
+
if property_names.length:
|
|
156
|
+
output.indent()
|
|
157
|
+
output.print('Object.defineProperties')
|
|
158
|
+
output.with_parens(def():
|
|
159
|
+
self.name.print(output), output.print('.prototype'), output.comma(), output.space(), output.with_block(def():
|
|
160
|
+
for name in property_names:
|
|
161
|
+
prop = self.dynamic_properties[name]
|
|
162
|
+
output.indent(), output.print(JSON.stringify(name) + ':'), output.space()
|
|
163
|
+
output.with_block(def():
|
|
164
|
+
output.indent(), output.print('"enumerable":'), output.space(), output.print('true'), output.comma(), output.newline()
|
|
165
|
+
if prop.getter:
|
|
166
|
+
output.indent(), output.print('"get":'), output.space()
|
|
167
|
+
define_method(prop.getter, True), output.comma(), output.newline()
|
|
168
|
+
output.indent(), output.print('"set":'), output.space()
|
|
169
|
+
if prop.setter:
|
|
170
|
+
define_method(prop.setter, True), output.newline()
|
|
171
|
+
else:
|
|
172
|
+
output.spaced('function', '()', '{', '''throw new AttributeError("can't set attribute")''', '}'), output.newline()
|
|
173
|
+
)
|
|
174
|
+
output.comma(), output.newline()
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
output.end_statement()
|
|
178
|
+
|
|
179
|
+
# actual methods
|
|
180
|
+
if not self.init:
|
|
181
|
+
# Create a default __init__ method
|
|
182
|
+
define_default_method('__init__', def():
|
|
183
|
+
if self.parent:
|
|
184
|
+
self.parent.print(output)
|
|
185
|
+
output.spaced('.prototype.__init__', '&&')
|
|
186
|
+
output.space(), self.parent.print(output)
|
|
187
|
+
output.print(".prototype.__init__.apply")
|
|
188
|
+
output.with_parens(def():
|
|
189
|
+
output.print("this")
|
|
190
|
+
output.comma()
|
|
191
|
+
output.print("arguments")
|
|
192
|
+
)
|
|
193
|
+
output.end_statement()
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
defined_methods = {}
|
|
197
|
+
|
|
198
|
+
for stmt in self.body:
|
|
199
|
+
if is_node_type(stmt, AST_Method):
|
|
200
|
+
if stmt.is_getter or stmt.is_setter:
|
|
201
|
+
continue
|
|
202
|
+
define_method(stmt)
|
|
203
|
+
defined_methods[stmt.name.name] = True
|
|
204
|
+
sname = stmt.name.name
|
|
205
|
+
if sname is '__init__':
|
|
206
|
+
# Copy argument handling data so that kwarg interpolation works when calling the constructor
|
|
207
|
+
for attr in ['.__argnames__', '.__handles_kwarg_interpolation__']:
|
|
208
|
+
output.indent(), self.name.print(output), output.assign(attr)
|
|
209
|
+
self.name.print(output), output.print('.prototype.__init__' + attr), output.end_statement()
|
|
210
|
+
if sname is '__iter__':
|
|
211
|
+
class_def('ρσ_iterator_symbol', True)
|
|
212
|
+
self.name.print(output)
|
|
213
|
+
output.print('.prototype.' + stmt.name.name)
|
|
214
|
+
output.end_statement()
|
|
215
|
+
|
|
216
|
+
elif is_node_type(stmt, AST_Class):
|
|
217
|
+
console.error('Nested classes aren\'t supported yet') # noqa:undef
|
|
218
|
+
|
|
219
|
+
if not defined_methods['__repr__']:
|
|
220
|
+
define_default_method('__repr__', def():
|
|
221
|
+
if self.parent:
|
|
222
|
+
output.print('if('), self.parent.print(output), output.spaced('.prototype.__repr__)', 'return', self.parent)
|
|
223
|
+
output.print('.prototype.__repr__.call(this)'), output.end_statement()
|
|
224
|
+
output.indent(), output.spaced('return', '"<"', '+', '__name__', '+', '"."', '+', 'this.constructor.name', '')
|
|
225
|
+
output.spaced('+', '" #"', '+', 'this.ρσ_object_id', '+', '">"')
|
|
226
|
+
output.end_statement()
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
if not defined_methods['__str__']:
|
|
230
|
+
define_default_method('__str__', def():
|
|
231
|
+
if self.parent:
|
|
232
|
+
output.print('if('), self.parent.print(output), output.spaced('.prototype.__str__)', 'return', self.parent)
|
|
233
|
+
output.print('.prototype.__str__.call(this)'), output.end_statement()
|
|
234
|
+
output.spaced('return', 'this.__repr__()')
|
|
235
|
+
output.end_statement()
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
# Multiple inheritance
|
|
239
|
+
add_hidden_property('__bases__', def():
|
|
240
|
+
output.print('[')
|
|
241
|
+
for v'var i = 0; i < self.bases.length; i++':
|
|
242
|
+
self.bases[i].print(output)
|
|
243
|
+
if i < self.bases.length - 1:
|
|
244
|
+
output.comma()
|
|
245
|
+
output.print(']')
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
# Python-compatible class identity properties
|
|
249
|
+
cname_str = JSON.stringify(self.name.name)
|
|
250
|
+
output.indent(), self.name.print(output), output.print('.__name__ = ' + cname_str), output.end_statement()
|
|
251
|
+
output.indent(), self.name.print(output), output.print('.__qualname__ = ' + cname_str), output.end_statement()
|
|
252
|
+
output.indent(), self.name.print(output), output.print('.__module__ = ' + JSON.stringify(self.module_id)), output.end_statement()
|
|
253
|
+
output.indent()
|
|
254
|
+
output.print('Object.defineProperty(')
|
|
255
|
+
self.name.print(output)
|
|
256
|
+
output.print('.prototype, "__class__", {get: function() { return this.constructor; }, configurable: true})')
|
|
257
|
+
output.end_statement()
|
|
258
|
+
|
|
259
|
+
if self.bases.length > 1:
|
|
260
|
+
output.indent()
|
|
261
|
+
output.print("ρσ_mixin(")
|
|
262
|
+
self.name.print(output)
|
|
263
|
+
for v'var i = 1; i < self.bases.length; i++':
|
|
264
|
+
output.comma()
|
|
265
|
+
self.bases[i].print(output)
|
|
266
|
+
output.print(')'), output.end_statement()
|
|
267
|
+
|
|
268
|
+
# Docstring
|
|
269
|
+
if self.docstrings and self.docstrings.length and output.options.keep_docstrings:
|
|
270
|
+
add_hidden_property('__doc__', def():
|
|
271
|
+
output.print(JSON.stringify(create_doctring(self.docstrings)))
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
# Other statements in the class context
|
|
275
|
+
for stmt in self.statements:
|
|
276
|
+
if not is_node_type(stmt, AST_Method):
|
|
277
|
+
output.indent()
|
|
278
|
+
stmt.print(output)
|
|
279
|
+
output.newline()
|
|
280
|
+
|
|
281
|
+
if decorators.length:
|
|
282
|
+
output.indent()
|
|
283
|
+
output.assign(self.name)
|
|
284
|
+
for di in range(decorators.length):
|
|
285
|
+
self.name.print(output)
|
|
286
|
+
output.print(f'.ρσ_decorators[{di}](')
|
|
287
|
+
self.name.print(output)
|
|
288
|
+
output.print(')' * decorators.length)
|
|
289
|
+
output.semicolon()
|
|
290
|
+
output.newline()
|
|
291
|
+
output.indent()
|
|
292
|
+
output.spaced('delete ')
|
|
293
|
+
self.name.print(output)
|
|
294
|
+
output.print('.ρσ_decorators')
|
|
295
|
+
output.semicolon()
|
|
296
|
+
output.newline()
|