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
package/tools/compile.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* compile.js
|
|
3
|
+
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
|
|
4
|
+
*
|
|
5
|
+
* Distributed under terms of the BSD license.
|
|
6
|
+
*/
|
|
7
|
+
"use strict"; /*jshint node:true */
|
|
8
|
+
|
|
9
|
+
var fs = require('fs');
|
|
10
|
+
var path = require('path');
|
|
11
|
+
var vm = require('vm');
|
|
12
|
+
var RapydScript = require("./compiler").create_compiler();
|
|
13
|
+
var utils = require('./utils');
|
|
14
|
+
|
|
15
|
+
function read_whole_file(filename, cb) {
|
|
16
|
+
if (!filename) {
|
|
17
|
+
var chunks = [];
|
|
18
|
+
process.stdin.setEncoding('utf-8');
|
|
19
|
+
process.stdin.on('data', function (chunk) {
|
|
20
|
+
chunks.push(chunk);
|
|
21
|
+
}).on('end', function () {
|
|
22
|
+
cb(null, chunks.join(""));
|
|
23
|
+
});
|
|
24
|
+
process.openStdin();
|
|
25
|
+
} else {
|
|
26
|
+
fs.readFile(filename, "utf-8", cb);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function makedirs(dir) {
|
|
31
|
+
try {
|
|
32
|
+
fs.mkdirSync(dir);
|
|
33
|
+
} catch(e) {
|
|
34
|
+
if (e.code == 'EEXIST') return;
|
|
35
|
+
if (e.code == 'ENOENT') { makedirs(path.dirname(dir)); fs.mkdirSync(dir); }
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function process_cache_dir(dir) {
|
|
41
|
+
dir = path.resolve(path.normalize(dir));
|
|
42
|
+
makedirs(dir);
|
|
43
|
+
return dir;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function build_scoped_flags(flags_str) {
|
|
47
|
+
var result = Object.create(null);
|
|
48
|
+
if (!flags_str) return result;
|
|
49
|
+
flags_str.split(',').forEach(function(flag) {
|
|
50
|
+
flag = flag.trim();
|
|
51
|
+
if (!flag) return;
|
|
52
|
+
var val = true;
|
|
53
|
+
if (flag.slice(0, 3) === 'no_') { val = false; flag = flag.slice(3); }
|
|
54
|
+
result[flag] = val;
|
|
55
|
+
});
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = function(start_time, argv, base_path, src_path, lib_path) {
|
|
60
|
+
// configure settings for the output
|
|
61
|
+
var cache_dir = argv.cache_dir ? process_cache_dir(argv.cache_dir) : '';
|
|
62
|
+
var OUTPUT_OPTIONS = {
|
|
63
|
+
beautify: !argv.uglify,
|
|
64
|
+
private_scope: !argv.bare && !argv.export_main,
|
|
65
|
+
omit_baselib: argv.omit_baselib,
|
|
66
|
+
js_version: parseInt(argv.js_version),
|
|
67
|
+
keep_docstrings: argv.keep_docstrings,
|
|
68
|
+
discard_asserts: argv.discard_asserts,
|
|
69
|
+
module_cache_dir: cache_dir,
|
|
70
|
+
pythonize_strings: argv.pythonize_strings,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
var files = argv.files.slice();
|
|
74
|
+
var STATS = {}, TOPLEVEL;
|
|
75
|
+
var num_of_files = files.length || 1;
|
|
76
|
+
|
|
77
|
+
var global_scoped_flags = build_scoped_flags(argv.python_flags);
|
|
78
|
+
|
|
79
|
+
function parse_file(code, file, toplevel) {
|
|
80
|
+
return RapydScript.parse(code, {
|
|
81
|
+
filename: file,
|
|
82
|
+
toplevel: toplevel,
|
|
83
|
+
basedir: (file !== '<stdin>') ? path.dirname(file) : undefined,
|
|
84
|
+
libdir: path.join(src_path, 'lib'),
|
|
85
|
+
import_dirs: utils.get_import_dirs(argv.import_path),
|
|
86
|
+
discard_asserts: argv.discard_asserts,
|
|
87
|
+
module_cache_dir: cache_dir,
|
|
88
|
+
scoped_flags: global_scoped_flags,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function write_output(output) {
|
|
93
|
+
if (argv.output) {
|
|
94
|
+
// Node's filesystem module cannot write directly to /dev/stdout
|
|
95
|
+
if (argv.output == '/dev/stdout') console.log(output);
|
|
96
|
+
else if (argv.output == '/dev/stderr') console.error(output);
|
|
97
|
+
else fs.writeFileSync(argv.output, output, "utf8");
|
|
98
|
+
} else if (!argv.execute){
|
|
99
|
+
console.log(output);
|
|
100
|
+
}
|
|
101
|
+
if (argv.execute) {
|
|
102
|
+
vm.runInNewContext(output, {'console':console, 'require':require}, {'filename':files[0]});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function time_it(name, cont) {
|
|
107
|
+
var t1 = new Date().getTime();
|
|
108
|
+
var ret = cont();
|
|
109
|
+
if (argv.stats) {
|
|
110
|
+
var spent = new Date().getTime() - t1;
|
|
111
|
+
if (STATS[name]) STATS[name] += spent;
|
|
112
|
+
else STATS[name] = spent;
|
|
113
|
+
}
|
|
114
|
+
return ret;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function compile_single_file(err, code) {
|
|
118
|
+
var output;
|
|
119
|
+
if (err) {
|
|
120
|
+
console.error("ERROR: can't read file: " + files[0]);
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
time_it("parse", function(){
|
|
124
|
+
var file = files[0] || argv.filename_for_stdin || '<stdin>';
|
|
125
|
+
try {
|
|
126
|
+
TOPLEVEL = parse_file(code, file, TOPLEVEL);
|
|
127
|
+
} catch (e) {
|
|
128
|
+
if (!(e instanceof RapydScript.SyntaxError)) throw e;
|
|
129
|
+
console.error(e.toString());
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (TOPLEVEL.imports && Object.keys(TOPLEVEL.imports).length) {
|
|
135
|
+
time_it("treeshake", function() {
|
|
136
|
+
RapydScript.tree_shake(TOPLEVEL, {
|
|
137
|
+
parse: RapydScript.parse,
|
|
138
|
+
import_dirs: utils.get_import_dirs(argv.import_path),
|
|
139
|
+
basedir: (files[0] !== '<stdin>') ? path.dirname(files[0]) : undefined,
|
|
140
|
+
libdir: path.join(src_path, 'lib'),
|
|
141
|
+
discard_asserts: argv.discard_asserts,
|
|
142
|
+
module_cache_dir: cache_dir,
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
output = new RapydScript.OutputStream(OUTPUT_OPTIONS);
|
|
149
|
+
} catch(ex) {
|
|
150
|
+
if (ex instanceof RapydScript.DefaultsError) {
|
|
151
|
+
console.error(ex.message);
|
|
152
|
+
process.exit(1);
|
|
153
|
+
}
|
|
154
|
+
throw ex;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
time_it("generate", function(){
|
|
158
|
+
TOPLEVEL.print(output);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
output = output.get();
|
|
162
|
+
|
|
163
|
+
if (argv.export_main) {
|
|
164
|
+
output = output.replace(/^(function\smain)/gm, 'export $1')
|
|
165
|
+
.replace(/^(async\sfunction\smain)/gm, 'export $1');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
write_output(output);
|
|
169
|
+
|
|
170
|
+
files = files.slice(1);
|
|
171
|
+
if (files.length) {
|
|
172
|
+
setImmediate(read_whole_file, files[0], compile_single_file);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (argv.stats) {
|
|
176
|
+
console.error(RapydScript.string_template("Timing information (compressed {count} files):", {
|
|
177
|
+
count: num_of_files
|
|
178
|
+
}));
|
|
179
|
+
for (var i in STATS) if (Object.prototype.hasOwnProperty.call(STATS, i)) {
|
|
180
|
+
console.error(RapydScript.string_template("- {name}: {time}s", {
|
|
181
|
+
name: i,
|
|
182
|
+
time: (STATS[i] / 1000).toFixed(3)
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
if (argv.comments) {
|
|
190
|
+
if (/^\//.test(argv.comments)) {
|
|
191
|
+
OUTPUT_OPTIONS.comments = new Function("return(" + argv.comments + ")")(); // jshint ignore:line
|
|
192
|
+
} else if (argv.comments == "all") {
|
|
193
|
+
OUTPUT_OPTIONS.comments = true;
|
|
194
|
+
} else {
|
|
195
|
+
OUTPUT_OPTIONS.comments = function(node, comment) {
|
|
196
|
+
var text = comment.value;
|
|
197
|
+
var type = comment.type;
|
|
198
|
+
if (type == "comment2") {
|
|
199
|
+
// multiline comment
|
|
200
|
+
return /@preserve|@license|@cc_on/i.test(text);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!argv.omit_baselib) {
|
|
207
|
+
var which = (OUTPUT_OPTIONS.beautify) ? 'pretty' : 'ugly';
|
|
208
|
+
OUTPUT_OPTIONS.baselib_plain = fs.readFileSync(path.join(lib_path, 'baselib-plain-' + which + '.js'), 'utf-8');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (files.filter(function(el){ return el == "-"; }).length > 1) {
|
|
212
|
+
console.error("ERROR: Can read a single file from STDIN (two or more dashes specified)");
|
|
213
|
+
process.exit(1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
setImmediate(read_whole_file, files[0], compile_single_file);
|
|
217
|
+
|
|
218
|
+
};
|
|
219
|
+
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* vim:fileencoding=utf-8
|
|
2
|
+
*
|
|
3
|
+
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
|
|
4
|
+
*
|
|
5
|
+
* Distributed under terms of the BSD license
|
|
6
|
+
*/
|
|
7
|
+
"use strict"; /*jshint node:true */
|
|
8
|
+
|
|
9
|
+
// Thin wrapper around (release|dev)/compiler.js to setup some global facilities and
|
|
10
|
+
// export the compiler's symbols safely.
|
|
11
|
+
|
|
12
|
+
var path = require("path");
|
|
13
|
+
var fs = require("fs");
|
|
14
|
+
var crypto = require('crypto');
|
|
15
|
+
var vm = require("vm");
|
|
16
|
+
var regenerator = require('regenerator');
|
|
17
|
+
var UglifyJS = require("uglify-js");
|
|
18
|
+
|
|
19
|
+
function sha1sum(data) {
|
|
20
|
+
var h = crypto.createHash('sha1');
|
|
21
|
+
h.update(data);
|
|
22
|
+
return h.digest('hex');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function path_exists(path) {
|
|
26
|
+
try {
|
|
27
|
+
fs.statSync(path);
|
|
28
|
+
return true;
|
|
29
|
+
} catch(e) {
|
|
30
|
+
if (e.code != 'ENOENT') throw e;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function uglify(code) {
|
|
35
|
+
var ans = UglifyJS.minify(code);
|
|
36
|
+
if (ans.error) throw ans.error;
|
|
37
|
+
return ans.code;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
function regenerate(code, beautify) {
|
|
42
|
+
var ans, start, end;
|
|
43
|
+
if (code) {
|
|
44
|
+
ans = regenerator.compile(code).code;
|
|
45
|
+
if (!beautify) {
|
|
46
|
+
ans = uglify(ans);
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
// Return the runtime
|
|
50
|
+
ans = regenerator.compile('', {includeRuntime:true}).code;
|
|
51
|
+
start = ans.indexOf('=') + 1;
|
|
52
|
+
end = ans.lastIndexOf('typeof');
|
|
53
|
+
end = ans.lastIndexOf('}(', end);
|
|
54
|
+
ans = ans.slice(start + 1, end);
|
|
55
|
+
if (!beautify) {
|
|
56
|
+
var extra = '})()';
|
|
57
|
+
ans = uglify(ans + extra).slice(0, extra.length);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return ans;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
var _current_virtual_files = null;
|
|
64
|
+
|
|
65
|
+
function virtual_readfile(name, encoding) {
|
|
66
|
+
if (_current_virtual_files && name.indexOf('__virtual__/') === 0) {
|
|
67
|
+
var rel = name.slice('__virtual__/'.length);
|
|
68
|
+
if (rel.slice(-11) === '.pyj-cached') rel = rel.slice(0, -11);
|
|
69
|
+
else if (rel.slice(-4) === '.pyj') rel = rel.slice(0, -4);
|
|
70
|
+
if (rel.slice(-9) === '/__init__') rel = rel.slice(0, -9);
|
|
71
|
+
if (Object.prototype.hasOwnProperty.call(_current_virtual_files, rel)) {
|
|
72
|
+
return _current_virtual_files[rel];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return fs.readFileSync(name, encoding);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function virtual_writefile(name, content) {
|
|
79
|
+
// Silently discard cache writes for virtual files; __virtual__ is not a real
|
|
80
|
+
// directory, and the compiled `except Error` guard fails across vm contexts.
|
|
81
|
+
if (name.indexOf('__virtual__/') === 0) return;
|
|
82
|
+
return fs.writeFileSync(name, content);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function create_compiler() {
|
|
86
|
+
var compiler_exports = {};
|
|
87
|
+
var compiler_context = vm.createContext({
|
|
88
|
+
console : console,
|
|
89
|
+
readfile : virtual_readfile,
|
|
90
|
+
writefile : virtual_writefile,
|
|
91
|
+
sha1sum : sha1sum,
|
|
92
|
+
require : require,
|
|
93
|
+
regenerate : regenerate,
|
|
94
|
+
exports : compiler_exports,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
var base = path.dirname(path.dirname(module.filename));
|
|
98
|
+
var compiler_dir = path.join(base, 'dev');
|
|
99
|
+
if (!path_exists(path.join(compiler_dir, 'compiler.js'))) compiler_dir = path.join(base, 'release');
|
|
100
|
+
var compiler_file = path.join(compiler_dir, 'compiler.js');
|
|
101
|
+
var compilerjs = fs.readFileSync(compiler_file, 'utf-8');
|
|
102
|
+
vm.runInContext(compilerjs, compiler_context, path.relative(base, compiler_file));
|
|
103
|
+
return compiler_exports;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
exports.create_compiler = create_compiler;
|
|
107
|
+
exports.set_virtual_files = function(vf) { _current_virtual_files = vf; };
|
|
108
|
+
exports.clear_virtual_files = function() { _current_virtual_files = null; };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/* vim:fileencoding=utf-8
|
|
2
|
+
*
|
|
3
|
+
* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
|
|
4
|
+
*
|
|
5
|
+
* Distributed under terms of the BSD license
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
module.exports = function(compiler, options) {
|
|
10
|
+
"use strict";
|
|
11
|
+
var all_keywords = compiler.ALL_KEYWORDS.split(' ');
|
|
12
|
+
var vm = require('vm');
|
|
13
|
+
options = options || {};
|
|
14
|
+
if (!options.enum_global) options.enum_global = "var global = Function('return this')(); Object.getOwnPropertyNames(global);";
|
|
15
|
+
|
|
16
|
+
function global_names(ctx) {
|
|
17
|
+
try {
|
|
18
|
+
var ans = vm.runInContext(options.enum_global, ctx);
|
|
19
|
+
ans = ans.concat(all_keywords);
|
|
20
|
+
ans.sort();
|
|
21
|
+
var seen = {};
|
|
22
|
+
ans.filter(function (item) {
|
|
23
|
+
if (Object.prototype.hasOwnProperty.call(seen, item)) return false;
|
|
24
|
+
seen[item] = true;
|
|
25
|
+
return true;
|
|
26
|
+
});
|
|
27
|
+
return ans;
|
|
28
|
+
} catch(e) {
|
|
29
|
+
console.log(e.stack || e.toString());
|
|
30
|
+
}
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function object_names(obj, prefix) {
|
|
35
|
+
if (obj === null || obj === undefined) return [];
|
|
36
|
+
var groups = [], prefix_len = prefix.length, p;
|
|
37
|
+
|
|
38
|
+
function prefix_filter(name) { return (prefix_len) ? (name.substr(0, prefix_len) === prefix) : true; }
|
|
39
|
+
|
|
40
|
+
function add(o) {
|
|
41
|
+
var items = Object.getOwnPropertyNames(o).filter(prefix_filter);
|
|
42
|
+
if (items.length) groups.push(items);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (typeof obj === 'object' || typeof obj === 'function') {
|
|
46
|
+
add(obj);
|
|
47
|
+
p = Object.getPrototypeOf(obj);
|
|
48
|
+
} else p = obj.constructor ? obj.constructor.prototype : null;
|
|
49
|
+
|
|
50
|
+
// Walk the prototype chain
|
|
51
|
+
try {
|
|
52
|
+
var sentinel = 5;
|
|
53
|
+
while (p !== null && sentinel > 0) {
|
|
54
|
+
add(p);
|
|
55
|
+
p = Object.getPrototypeOf(p);
|
|
56
|
+
// Circular refs possible? Let's guard against that.
|
|
57
|
+
sentinel--;
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
// console.error("completion error walking prototype chain:" + e);
|
|
61
|
+
}
|
|
62
|
+
if (!groups.length) return [];
|
|
63
|
+
var seen = {}, ans = [];
|
|
64
|
+
function uniq(name) {
|
|
65
|
+
if (Object.prototype.hasOwnProperty.call(seen, name)) return false;
|
|
66
|
+
seen[name] = true;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
for (var i = 0; i < groups.length; i++) {
|
|
70
|
+
var group = groups[i];
|
|
71
|
+
group.sort();
|
|
72
|
+
ans = ans.concat(group.filter(uniq));
|
|
73
|
+
ans.push(''); // group separator
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
while (ans.length && ans[ans.length - 1] === '') ans.pop();
|
|
77
|
+
return ans;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function prefix_matches(prefix, items) {
|
|
81
|
+
var len = prefix.length;
|
|
82
|
+
var ans = items.filter(function(item) { return item.substr(0, len) === prefix; });
|
|
83
|
+
ans.sort();
|
|
84
|
+
return ans;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function find_completions(line, ctx) {
|
|
88
|
+
var t;
|
|
89
|
+
try {
|
|
90
|
+
t = compiler.tokenizer(line, '<repl>');
|
|
91
|
+
} catch(e) { return []; }
|
|
92
|
+
var tokens = [], token;
|
|
93
|
+
while (true) {
|
|
94
|
+
try {
|
|
95
|
+
token = t();
|
|
96
|
+
} catch (e) { return []; }
|
|
97
|
+
if (token.type === 'eof') break;
|
|
98
|
+
if (token.type === 'punc' && '(){},;:'.indexOf(token.value) > -1)
|
|
99
|
+
tokens = [];
|
|
100
|
+
tokens.push(token);
|
|
101
|
+
}
|
|
102
|
+
if (!tokens.length) {
|
|
103
|
+
// New line or trailing space
|
|
104
|
+
return [global_names(ctx), ''];
|
|
105
|
+
}
|
|
106
|
+
var last_tok = tokens[tokens.length - 1];
|
|
107
|
+
if (last_tok.value === '.' || (last_tok.type === 'name' && compiler.IDENTIFIER_PAT.test(last_tok.value))) {
|
|
108
|
+
last_tok = last_tok.value;
|
|
109
|
+
if (last_tok === '.') {
|
|
110
|
+
tokens.push({'value':''});
|
|
111
|
+
last_tok = '';
|
|
112
|
+
}
|
|
113
|
+
if (tokens.length > 1 && tokens[tokens.length - 2].value === '.') {
|
|
114
|
+
// A compound expression
|
|
115
|
+
var prefix = '', result;
|
|
116
|
+
tokens.slice(0, tokens.length - 2).forEach(function (tok) { prefix += tok.value; });
|
|
117
|
+
if (prefix) {
|
|
118
|
+
try {
|
|
119
|
+
result = vm.runInContext(prefix, ctx, {'displayErrors':false});
|
|
120
|
+
} catch(e) { return []; }
|
|
121
|
+
return [object_names(result, last_tok), last_tok];
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
return [prefix_matches(last_tok, global_names(ctx)), last_tok];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return find_completions;
|
|
131
|
+
};
|