rapydscript-ng 0.7.23 → 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 (114) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +72 -26
  3. package/bin/package.json +1 -0
  4. package/bin/rapydscript +65 -62
  5. package/editor-plugins/nvim/rapydscript/README.md +184 -0
  6. package/editor-plugins/nvim/rapydscript/bin/rapydscript.so +0 -0
  7. package/editor-plugins/nvim/rapydscript/ftdetect/rapydscript.lua +10 -0
  8. package/editor-plugins/nvim/rapydscript/lua/rapydscript/health.lua +88 -0
  9. package/editor-plugins/nvim/rapydscript/lua/rapydscript/init.lua +279 -0
  10. package/local-agent.md +28 -0
  11. package/package.json +4 -5
  12. package/release/baselib-plain-pretty.js +448 -326
  13. package/release/baselib-plain-ugly.js +3 -3
  14. package/release/compiler.js +2528 -1672
  15. package/release/signatures.json +17 -17
  16. package/src/ast.pyj +73 -25
  17. package/src/baselib-builtins.pyj +2 -2
  18. package/src/baselib-containers.pyj +153 -151
  19. package/src/baselib-internal.pyj +3 -3
  20. package/src/baselib-str.pyj +5 -5
  21. package/src/lib/aes.pyj +1 -1
  22. package/src/lib/encodings.pyj +1 -1
  23. package/src/lib/pythonize.pyj +1 -1
  24. package/src/lib/re.pyj +2 -2
  25. package/src/lib/traceback.pyj +165 -28
  26. package/src/lib/uuid.pyj +1 -1
  27. package/src/output/codegen.pyj +10 -3
  28. package/src/output/functions.pyj +31 -40
  29. package/src/output/literals.pyj +11 -14
  30. package/src/output/loops.pyj +25 -87
  31. package/src/output/modules.pyj +5 -47
  32. package/src/output/statements.pyj +1 -1
  33. package/src/output/stream.pyj +58 -31
  34. package/src/parse.pyj +777 -427
  35. package/src/tokenizer.pyj +16 -4
  36. package/src/utils.pyj +1 -1
  37. package/test/_treeshake_mod.pyj +30 -0
  38. package/test/_treeshake_side_effect.pyj +9 -0
  39. package/test/ast_serialization.pyj +392 -0
  40. package/test/async.pyj +95 -0
  41. package/test/baselib.pyj +1 -2
  42. package/test/embedded_compiler.pyj +57 -0
  43. package/test/fmt.pyj +201 -0
  44. package/test/generic.pyj +7 -3
  45. package/test/imports.pyj +8 -6
  46. package/test/internationalization.pyj +38 -37
  47. package/test/lint.pyj +120 -115
  48. package/test/lsp.pyj +222 -0
  49. package/test/repl.pyj +70 -65
  50. package/test/sourcemaps.pyj +315 -0
  51. package/test/starargs.pyj +46 -39
  52. package/test/traceback.pyj +263 -0
  53. package/test/treeshaking.pyj +181 -0
  54. package/test/web_repl_export.pyj +88 -0
  55. package/tools/ast_serialize.mjs +557 -0
  56. package/tools/cli.mjs +510 -0
  57. package/tools/compile.mjs +201 -0
  58. package/tools/compiler.mjs +127 -0
  59. package/tools/{completer.js → completer.mjs} +6 -6
  60. package/tools/{embedded_compiler.js → embedded_compiler.mjs} +17 -13
  61. package/tools/export.js +109 -56
  62. package/tools/fmt.mjs +735 -0
  63. package/tools/{gettext.js → gettext.mjs} +46 -53
  64. package/tools/{ini.js → ini.mjs} +9 -10
  65. package/tools/{lint.js → lint.mjs} +107 -56
  66. package/tools/lsp.mjs +914 -0
  67. package/tools/lsp_protocol.mjs +259 -0
  68. package/tools/lsp_symbols.mjs +418 -0
  69. package/tools/{msgfmt.js → msgfmt.mjs} +18 -18
  70. package/tools/{repl.js → repl.mjs} +52 -41
  71. package/tools/{self.js → self.mjs} +56 -46
  72. package/tools/sourcemap.mjs +123 -0
  73. package/tools/test.mjs +152 -0
  74. package/tools/treeshake.mjs +400 -0
  75. package/tools/{utils.js → utils.mjs} +26 -23
  76. package/tools/{web_repl.js → web_repl.mjs} +15 -13
  77. package/tools/web_repl_export.mjs +93 -0
  78. package/tree-sitter/README.md +101 -0
  79. package/tree-sitter/grammar.js +992 -0
  80. package/tree-sitter/package.json +43 -0
  81. package/tree-sitter/queries/rapydscript/highlights.scm +232 -0
  82. package/tree-sitter/queries/rapydscript/indents.scm +64 -0
  83. package/tree-sitter/queries/rapydscript/injections.scm +14 -0
  84. package/tree-sitter/queries/rapydscript/locals.scm +108 -0
  85. package/tree-sitter/src/grammar.json +4976 -0
  86. package/tree-sitter/src/node-types.json +2901 -0
  87. package/tree-sitter/src/parser.c +196465 -0
  88. package/tree-sitter/src/scanner.c +294 -0
  89. package/tree-sitter/src/tree_sitter/alloc.h +54 -0
  90. package/tree-sitter/src/tree_sitter/array.h +330 -0
  91. package/tree-sitter/src/tree_sitter/parser.h +286 -0
  92. package/tree-sitter/test/corpus/chaining.txt +99 -0
  93. package/tree-sitter/test/corpus/classes.txt +147 -0
  94. package/tree-sitter/test/corpus/comprehensions.txt +155 -0
  95. package/tree-sitter/test/corpus/containers.txt +242 -0
  96. package/tree-sitter/test/corpus/control_flow.txt +361 -0
  97. package/tree-sitter/test/corpus/decorators.txt +64 -0
  98. package/tree-sitter/test/corpus/existential.txt +102 -0
  99. package/tree-sitter/test/corpus/functions.txt +293 -0
  100. package/tree-sitter/test/corpus/imports.txt +117 -0
  101. package/tree-sitter/test/corpus/literals.txt +135 -0
  102. package/tree-sitter/test/corpus/operators.txt +296 -0
  103. package/tree-sitter/test/corpus/statements.txt +189 -0
  104. package/tree-sitter/test/corpus/strings.txt +90 -0
  105. package/tree-sitter/test/corpus/subscripts.txt +227 -0
  106. package/tree-sitter/tree-sitter.json +36 -0
  107. package/web-repl/env.js +35 -23
  108. package/web-repl/main.js +8 -8
  109. package/bin/export +0 -75
  110. package/bin/web-repl-export +0 -102
  111. package/tools/cli.js +0 -523
  112. package/tools/compile.js +0 -184
  113. package/tools/compiler.js +0 -84
  114. package/tools/test.js +0 -110
package/tools/export.js CHANGED
@@ -1,4 +1,4 @@
1
- /*
1
+ /*
2
2
  * Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
3
3
  *
4
4
  * Distributed under terms of the BSD license
@@ -58,6 +58,70 @@ function basename(path) {
58
58
  return path;
59
59
  }
60
60
 
61
+ function esm_to_cjs(content) {
62
+ var pending_exports = [];
63
+
64
+ // import X from 'Y'
65
+ content = content.replace(/^import\s+(\w+)\s+from\s+(['"][^'"]*['"]);?\s*$/mg,
66
+ 'var $1 = require($2);');
67
+
68
+ // import { A, B } from 'Y'
69
+ content = content.replace(/^import\s+\{([^}]*)\}\s+from\s+(['"][^'"]*['"]);?\s*$/mg,
70
+ function(_, names, src) { return 'var {' + names + '} = require(' + src + ');'; });
71
+
72
+ // import * as X from 'Y'
73
+ content = content.replace(/^import\s+\*\s+as\s+(\w+)\s+from\s+(['"][^'"]*['"]);?\s*$/mg,
74
+ 'var $1 = require($2);');
75
+
76
+ // import 'Y' (side-effect only)
77
+ content = content.replace(/^import\s+(['"][^'"]*['"]);?\s*$/mg, 'require($1);');
78
+
79
+ // export default function/class name (named - add to exports at end)
80
+ content = content.replace(/^export\s+default\s+(function|class)\s+(\w+)/mg,
81
+ function(_, kw, name) { pending_exports.push(['default', name]); return kw + ' ' + name; });
82
+
83
+ // export default function/class (anonymous)
84
+ content = content.replace(/^export\s+default\s+((?:async\s+)?function\b|class\b)/mg,
85
+ 'module.exports = $1');
86
+
87
+ // export default <expression>
88
+ content = content.replace(/^export\s+default\s+/mg, 'module.exports = ');
89
+
90
+ // export async function X / export function X
91
+ content = content.replace(/^export\s+(async\s+)?function\s+(\w+)/mg,
92
+ function(_, async_, name) { pending_exports.push(name); return (async_ || '') + 'function ' + name; });
93
+
94
+ // export class X
95
+ content = content.replace(/^export\s+class\s+(\w+)/mg,
96
+ function(_, name) { pending_exports.push(name); return 'class ' + name; });
97
+
98
+ // export var/let/const X
99
+ content = content.replace(/^export\s+(var|let|const)\s+(\w+)/mg,
100
+ function(_, kw, name) { pending_exports.push(name); return kw + ' ' + name; });
101
+
102
+ // export { A, B as C }
103
+ content = content.replace(/^export\s+\{([^}]+)\}\s*;?\s*$/mg,
104
+ function(_, names) {
105
+ names.split(',').forEach(function(spec) {
106
+ spec = spec.trim();
107
+ if (!spec) return;
108
+ var m = spec.match(/^(\w+)\s+as\s+(\w+)$/);
109
+ if (m) pending_exports.push([m[2], m[1]]);
110
+ else pending_exports.push(spec);
111
+ });
112
+ return '';
113
+ });
114
+
115
+ if (pending_exports.length) {
116
+ content += '\n';
117
+ pending_exports.forEach(function(spec) {
118
+ content += '\nexports.' + (Array.isArray(spec) ? spec[0] + ' = ' + spec[1] : spec + ' = ' + spec) + ';';
119
+ });
120
+ }
121
+
122
+ return content;
123
+ }
124
+
61
125
  var cache = {};
62
126
 
63
127
  function load(filepath) {
@@ -71,6 +135,7 @@ function load(filepath) {
71
135
  if (!content) throw 'Failed to load: ' + JSON.stringify(filepath);
72
136
 
73
137
  if (filepath.slice(-5) == '.json') { module.exports = JSON.parse(content); return module.exports; }
138
+ if (filepath.slice(-4) == '.mjs') content = esm_to_cjs(content);
74
139
 
75
140
  var base = dirname(filepath);
76
141
  function mrequire(x) {
@@ -94,6 +159,7 @@ function has(x, y) { return Object.prototype.hasOwnProperty.call(x, y); }
94
159
  function try_files(candidate) {
95
160
  if (has(data, candidate)) return candidate;
96
161
  if (has(data, candidate + '.js')) return candidate + '.js';
162
+ if (has(data, candidate + '.mjs')) return candidate + '.mjs';
97
163
  if (has(data, candidate + '.json')) return candidate + '.json';
98
164
  return null;
99
165
  }
@@ -145,52 +211,18 @@ function vrequire(name, base) {
145
211
  return load(modpath);
146
212
  }
147
213
 
148
- var UglifyJS = null, regenerator = null;
214
+ var Terser = null;
149
215
  var crypto = null, fs = require('fs');
150
216
 
151
217
  function uglify(x) {
152
- if (!UglifyJS) UglifyJS = vrequire("uglify-js");
153
- ans = UglifyJS.minify(x);
218
+ if (!Terser) Terser = vrequire("terser");
219
+ var ans = Terser.minify_sync(x);
154
220
  if (ans.error) throw ans.error;
155
221
  return ans.code;
156
222
  }
157
223
 
158
- function regenerate(code, beautify) {
159
- var orig = fs.readFileSync;
160
- fs.readFileSync = function(name) {
161
- if (!has(data, name)) {
162
- throw {message: "Failed to readfile from data: " + name};
163
- }
164
- return data[name];
165
- };
166
- if (!regenerator) regenerator = vrequire('regenerator');
167
- var ans;
168
- if (code) {
169
- try {
170
- ans = regenerator.compile(code).code;
171
- } catch (e) {
172
- console.error('regenerator failed for code: ' + code + 'with error stack:\n' + e.stack);
173
- throw e;
174
- }
175
- if (!beautify) ans = uglify(ans);
176
- } else {
177
- // Return the runtime
178
- ans = regenerator.compile('', {includeRuntime:true}).code;
179
- start = ans.indexOf('=') + 1;
180
- end = ans.lastIndexOf('typeof');
181
- end = ans.lastIndexOf('}(', end);
182
- ans = ans.slice(start + 1, end);
183
- if (!beautify) {
184
- var extra = '})()';
185
- ans = uglify(ans + extra).slice(0, extra.length);
186
- }
187
- }
188
- fs.readFileSync = orig;
189
- return ans;
190
- }
191
-
192
224
  if (typeof this != 'object' || typeof this.sha1sum !== 'function') {
193
- var sha1sum = function (data) {
225
+ var sha1sum = function (data) {
194
226
  if (!crypto) crypto = require('crypto');
195
227
  var h = crypto.createHash('sha1');
196
228
  h.update(data);
@@ -199,19 +231,38 @@ if (typeof this != 'object' || typeof this.sha1sum !== 'function') {
199
231
  } else var sha1sum = this.sha1sum;
200
232
 
201
233
  function create_compiler() {
202
- var compilerjs = data['compiler.js'];
203
234
  var module = {'id':'compiler', 'exports':{}};
204
- var wrapped = '(function(module, exports, readfile, writefile, sha1sum, regenerate) {' + data['compiler.js'] + ';\n})';
205
- vm.runInThisContext(wrapped, {'filename': 'compiler.js'})(module, module.exports, fs.readFileSync, fs.writeFileSync, sha1sum, regenerate);
235
+ // _sh (serializer holder) is populated after the compiler loads, solving the
236
+ // chicken-and-egg: ast_to_json needs compiler_exports, which aren't ready yet.
237
+ var _sh = {};
238
+ var wrapped = '(function(module, exports, readfile, writefile, stat_file, sha1sum, _sh) {' +
239
+ 'var ast_to_json = function(r) { return _sh.ast_to_json(r); };' +
240
+ 'var ast_from_json = function(d) { return _sh.ast_from_json(d); };' +
241
+ 'var make_lazy_ast_module = function(a, b) { return _sh.make_lazy_ast_module(a, b); };' +
242
+ 'var encode_cache = function(m, r) { return _sh.encode_cache(m, r); };' +
243
+ 'var decode_cache = function(d) { return _sh.decode_cache(d); };' +
244
+ data['compiler.js'] + ';\n})';
245
+ vm.runInThisContext(wrapped, {'filename': 'compiler.js'})(module, module.exports, readfile, writefile, stat_file, sha1sum, _sh);
246
+ var s = vrequire('tools/ast_serialize.mjs').make_ast_serializer(module.exports);
247
+ _sh.ast_to_json = s.ast_to_json;
248
+ _sh.ast_from_json = s.ast_from_json;
249
+ _sh.make_lazy_ast_module = s.make_lazy_ast_module;
250
+ _sh.encode_cache = s.encode_cache;
251
+ _sh.decode_cache = s.decode_cache;
252
+ module.exports.ast_to_json = s.ast_to_json;
253
+ module.exports.ast_from_json = s.ast_from_json;
254
+ module.exports.make_lazy_ast_module = s.make_lazy_ast_module;
255
+ module.exports.encode_cache = s.encode_cache;
256
+ module.exports.decode_cache = s.decode_cache;
206
257
  return module.exports;
207
258
  }
208
259
 
209
260
  var RapydScript = null;
210
261
 
211
- function compile(code, filename, options) {
262
+ async function compile(code, filename, options) {
212
263
  if (!RapydScript) RapydScript = create_compiler();
213
264
  options = options || {};
214
- var ast = RapydScript.parse(code, {
265
+ var ast = await RapydScript.parse(code, {
215
266
  filename: filename || '<eval>',
216
267
  basedir: options.basedir || dirname(filename || ''),
217
268
  libdir: options.libdir,
@@ -220,7 +271,7 @@ function compile(code, filename, options) {
220
271
  beautify: (options.beautify === undefined ? true : options.beautify),
221
272
  private_scope: !options.bare,
222
273
  omit_baselib: !!options.omit_baselib,
223
- js_version: options.js_version || 5,
274
+ js_version: options.js_version || 6,
224
275
  };
225
276
  if (!out_ops.omit_baselib) out_ops.baselib_plain = data['baselib-plain-' + (out_ops.beautify ? 'pretty' : 'ugly') + '.js'];
226
277
  var out = new RapydScript.OutputStream(out_ops);
@@ -228,39 +279,41 @@ function compile(code, filename, options) {
228
279
  return out.get();
229
280
  }
230
281
 
231
- function create_embedded_compiler(runjs) {
232
- var c = vrequire('tools/embedded_compiler.js');
233
- return c(create_compiler(), data['baselib-plain-pretty.js'], runjs);
282
+ async function create_embedded_compiler(runjs) {
283
+ var c = vrequire('tools/embedded_compiler');
284
+ var ts = vrequire('tools/treeshake');
285
+ var sm = vrequire('tools/sourcemap');
286
+ return await c(create_compiler(), data['baselib-plain-pretty.js'], runjs, undefined, ts, sm.generate_source_map);
234
287
  }
235
288
 
236
- function web_repl() {
237
- var repl = vrequire('tools/web_repl.js');
238
- return repl(create_compiler(), data['baselib-plain-pretty.js']);
289
+ async function web_repl() {
290
+ var repl = vrequire('tools/web_repl');
291
+ return await repl(create_compiler(), data['baselib-plain-pretty.js']);
239
292
  }
240
293
 
241
294
  function init_repl(options) {
242
- var repl = vrequire('tools/repl.js');
295
+ var repl = vrequire('tools/repl');
243
296
  options.baselib = data['baselib-plain-pretty.js'];
244
297
  return repl(options);
245
298
  }
246
299
 
247
300
  function gettext_parse(catalog, code, filename) {
248
- g = vrequire('tools/gettext.js');
301
+ g = vrequire('tools/gettext');
249
302
  g.gettext(catalog, code, filename);
250
303
  }
251
304
 
252
305
  function gettext_output(catalog, options, write) {
253
- g = vrequire('tools/gettext.js');
306
+ g = vrequire('tools/gettext');
254
307
  g.write_output(catalog, options, write);
255
308
  }
256
309
 
257
310
  function msgfmt(data, options) {
258
- m = vrequire('tools/msgfmt.js');
311
+ m = vrequire('tools/msgfmt');
259
312
  return m.build(data, options);
260
313
  }
261
314
 
262
315
  function completer(compiler, options) {
263
- m = vrequire('tools/completer.js');
316
+ m = vrequire('tools/completer');
264
317
  return m(compiler, options);
265
318
  }
266
319