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.
Files changed (144) hide show
  1. package/.agignore +1 -0
  2. package/.gitattributes +4 -0
  3. package/.github/workflows/ci.yml +38 -0
  4. package/.github/workflows/web-repl-page-deploy.yml +42 -0
  5. package/=template.pyj +5 -0
  6. package/CHANGELOG.md +456 -0
  7. package/CONTRIBUTORS +13 -0
  8. package/HACKING.md +103 -0
  9. package/LICENSE +24 -0
  10. package/README.md +2512 -0
  11. package/TODO.md +327 -0
  12. package/add-toc-to-readme +2 -0
  13. package/bin/export +75 -0
  14. package/bin/rapydscript +70 -0
  15. package/bin/web-repl-export +102 -0
  16. package/build +3 -0
  17. package/package.json +46 -0
  18. package/publish.py +37 -0
  19. package/release/baselib-plain-pretty.js +4370 -0
  20. package/release/baselib-plain-ugly.js +3 -0
  21. package/release/compiler.js +18394 -0
  22. package/release/signatures.json +31 -0
  23. package/session.vim +4 -0
  24. package/setup.cfg +2 -0
  25. package/src/ast.pyj +1356 -0
  26. package/src/baselib-builtins.pyj +279 -0
  27. package/src/baselib-containers.pyj +723 -0
  28. package/src/baselib-errors.pyj +37 -0
  29. package/src/baselib-internal.pyj +421 -0
  30. package/src/baselib-itertools.pyj +97 -0
  31. package/src/baselib-str.pyj +798 -0
  32. package/src/compiler.pyj +36 -0
  33. package/src/errors.pyj +30 -0
  34. package/src/lib/aes.pyj +646 -0
  35. package/src/lib/collections.pyj +695 -0
  36. package/src/lib/elementmaker.pyj +83 -0
  37. package/src/lib/encodings.pyj +126 -0
  38. package/src/lib/functools.pyj +148 -0
  39. package/src/lib/gettext.pyj +569 -0
  40. package/src/lib/itertools.pyj +580 -0
  41. package/src/lib/math.pyj +193 -0
  42. package/src/lib/numpy.pyj +2101 -0
  43. package/src/lib/operator.pyj +11 -0
  44. package/src/lib/pythonize.pyj +20 -0
  45. package/src/lib/random.pyj +118 -0
  46. package/src/lib/re.pyj +470 -0
  47. package/src/lib/traceback.pyj +63 -0
  48. package/src/lib/uuid.pyj +77 -0
  49. package/src/monaco-language-service/analyzer.js +526 -0
  50. package/src/monaco-language-service/builtins.js +543 -0
  51. package/src/monaco-language-service/completions.js +498 -0
  52. package/src/monaco-language-service/diagnostics.js +643 -0
  53. package/src/monaco-language-service/dts.js +550 -0
  54. package/src/monaco-language-service/hover.js +121 -0
  55. package/src/monaco-language-service/index.js +386 -0
  56. package/src/monaco-language-service/scope.js +162 -0
  57. package/src/monaco-language-service/signature.js +144 -0
  58. package/src/output/__init__.pyj +0 -0
  59. package/src/output/classes.pyj +296 -0
  60. package/src/output/codegen.pyj +492 -0
  61. package/src/output/comments.pyj +45 -0
  62. package/src/output/exceptions.pyj +105 -0
  63. package/src/output/functions.pyj +491 -0
  64. package/src/output/literals.pyj +109 -0
  65. package/src/output/loops.pyj +444 -0
  66. package/src/output/modules.pyj +329 -0
  67. package/src/output/operators.pyj +429 -0
  68. package/src/output/statements.pyj +463 -0
  69. package/src/output/stream.pyj +309 -0
  70. package/src/output/treeshake.pyj +182 -0
  71. package/src/output/utils.pyj +72 -0
  72. package/src/parse.pyj +3106 -0
  73. package/src/string_interpolation.pyj +72 -0
  74. package/src/tokenizer.pyj +702 -0
  75. package/src/unicode_aliases.pyj +576 -0
  76. package/src/utils.pyj +192 -0
  77. package/test/_import_one.pyj +37 -0
  78. package/test/_import_two/__init__.pyj +11 -0
  79. package/test/_import_two/level2/__init__.pyj +0 -0
  80. package/test/_import_two/level2/deep.pyj +4 -0
  81. package/test/_import_two/other.pyj +6 -0
  82. package/test/_import_two/sub.pyj +13 -0
  83. package/test/aes_vectors.pyj +421 -0
  84. package/test/annotations.pyj +80 -0
  85. package/test/baselib.pyj +319 -0
  86. package/test/classes.pyj +452 -0
  87. package/test/collections.pyj +152 -0
  88. package/test/decorators.pyj +77 -0
  89. package/test/dict_spread.pyj +76 -0
  90. package/test/docstrings.pyj +39 -0
  91. package/test/elementmaker_test.pyj +45 -0
  92. package/test/ellipsis.pyj +49 -0
  93. package/test/functions.pyj +151 -0
  94. package/test/generators.pyj +41 -0
  95. package/test/generic.pyj +370 -0
  96. package/test/imports.pyj +72 -0
  97. package/test/internationalization.pyj +73 -0
  98. package/test/lint.pyj +164 -0
  99. package/test/loops.pyj +85 -0
  100. package/test/numpy.pyj +734 -0
  101. package/test/omit_function_metadata.pyj +20 -0
  102. package/test/regexp.pyj +55 -0
  103. package/test/repl.pyj +121 -0
  104. package/test/scoped_flags.pyj +76 -0
  105. package/test/starargs.pyj +506 -0
  106. package/test/starred_assign.pyj +104 -0
  107. package/test/str.pyj +198 -0
  108. package/test/subscript_tuple.pyj +53 -0
  109. package/test/unit/fixtures/fibonacci_expected.js +46 -0
  110. package/test/unit/index.js +2989 -0
  111. package/test/unit/language-service-builtins.js +815 -0
  112. package/test/unit/language-service-completions.js +1067 -0
  113. package/test/unit/language-service-dts.js +543 -0
  114. package/test/unit/language-service-hover.js +455 -0
  115. package/test/unit/language-service-scope.js +833 -0
  116. package/test/unit/language-service-signature.js +458 -0
  117. package/test/unit/language-service.js +705 -0
  118. package/test/unit/run-language-service.js +41 -0
  119. package/test/unit/web-repl.js +484 -0
  120. package/tools/build-language-service.js +190 -0
  121. package/tools/cli.js +547 -0
  122. package/tools/compile.js +219 -0
  123. package/tools/compiler.js +108 -0
  124. package/tools/completer.js +131 -0
  125. package/tools/embedded_compiler.js +251 -0
  126. package/tools/export.js +316 -0
  127. package/tools/gettext.js +185 -0
  128. package/tools/ini.js +65 -0
  129. package/tools/lint.js +705 -0
  130. package/tools/msgfmt.js +187 -0
  131. package/tools/repl.js +223 -0
  132. package/tools/self.js +162 -0
  133. package/tools/test.js +118 -0
  134. package/tools/utils.js +128 -0
  135. package/tools/web_repl.js +95 -0
  136. package/try +41 -0
  137. package/web-repl/env.js +74 -0
  138. package/web-repl/index.html +163 -0
  139. package/web-repl/language-service.js +4084 -0
  140. package/web-repl/main.js +254 -0
  141. package/web-repl/prism.css +139 -0
  142. package/web-repl/prism.js +113 -0
  143. package/web-repl/rapydscript.js +435 -0
  144. 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()