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/msgfmt.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
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
|
+
function unesc(string) {
|
|
10
|
+
return string.replace(/\\"/g, '"').replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\t/g, '\t').replace(/\\\\/g, '\\');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function parse(data, on_error) {
|
|
14
|
+
// Parse a PO file using a state machine (does not work for POT files). Also only extracts data useful
|
|
15
|
+
// for JSON output.
|
|
16
|
+
var plural_forms = null;
|
|
17
|
+
var lines = data.split('\n');
|
|
18
|
+
var entries = [];
|
|
19
|
+
var current_entry = create_entry();
|
|
20
|
+
var lnum = 0;
|
|
21
|
+
var nplurals = null;
|
|
22
|
+
var language = null;
|
|
23
|
+
|
|
24
|
+
function fatal() {
|
|
25
|
+
var msg = Array.prototype.slice.call(arguments).join(' ');
|
|
26
|
+
if (on_error) { on_error(msg); return; }
|
|
27
|
+
console.error(msg);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function create_entry() {
|
|
32
|
+
return {msgid: null, fuzzy: false, msgstr:[], msgid_plural:null, lnum:null};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parse_header() {
|
|
36
|
+
var raw = current_entry.msgstr[0];
|
|
37
|
+
if (raw === undefined) fatal('Header has no msgstr');
|
|
38
|
+
raw.split('\n').forEach(function(line) {
|
|
39
|
+
if (line.startsWith('Plural-Forms:')) {
|
|
40
|
+
plural_forms = line.slice('Plural-Forms:'.length).trim();
|
|
41
|
+
var match = /^nplurals\s*=\s*(\d+)\s*;/.exec(plural_forms);
|
|
42
|
+
if (!match || match[1] === undefined) fatal('Invalid Plural-Forms header:', plural_forms);
|
|
43
|
+
nplurals = parseInt(match[1]);
|
|
44
|
+
}
|
|
45
|
+
else if (line.startsWith('Language:')) {
|
|
46
|
+
language = line.slice('Language:'.length).trim();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function commit_entry() {
|
|
52
|
+
if (current_entry.msgid) {
|
|
53
|
+
if (current_entry.msgid_plural !== null) {
|
|
54
|
+
if (nplurals === null) fatal('Plural-Forms header missing');
|
|
55
|
+
for (var i = 0; i < nplurals; i++) {
|
|
56
|
+
if (current_entry.msgstr[i] === undefined) fatal('Missing plural form for entry at line number:', lnum);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
entries.push(current_entry);
|
|
60
|
+
} else if (current_entry.msgid === '') parse_header();
|
|
61
|
+
current_entry = create_entry();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function read_string(line) {
|
|
65
|
+
line = line.trim();
|
|
66
|
+
if (!line || line[0] !== '"' || line[line.length - 1] !== '"') {
|
|
67
|
+
fatal('Expecting a string at line number:', lnum);
|
|
68
|
+
}
|
|
69
|
+
return unesc(line.slice(1, -1));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function continuation(line, lines, append, after) {
|
|
73
|
+
if (line[0] === '"') append(read_string(line));
|
|
74
|
+
else {
|
|
75
|
+
state = after;
|
|
76
|
+
after(line, lines);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function start(line, lines) {
|
|
81
|
+
if (line[0] === '#') {
|
|
82
|
+
if (line[1] === ',') {
|
|
83
|
+
line.slice(2).trimLeft().split(',').forEach(function(flag) {
|
|
84
|
+
if (flag.trim().toLowerCase() === 'fuzzy') current_entry.fuzzy = true;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
} else if (line.startsWith('msgid ')) {
|
|
88
|
+
current_entry.msgid = read_string(line.slice('msgid '.length));
|
|
89
|
+
current_entry.lnum = lnum;
|
|
90
|
+
state = function(line, lines) {
|
|
91
|
+
continuation(line, lines, function(x) { current_entry.msgid += x; }, after_msgid);
|
|
92
|
+
};
|
|
93
|
+
} else {
|
|
94
|
+
fatal('Expecting msgid at line number:', lnum);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function after_msgid(line, lines) {
|
|
99
|
+
if (line.startsWith('msgid_plural ')) {
|
|
100
|
+
current_entry.msgid_plural = read_string(line.slice('msgid_plural '.length));
|
|
101
|
+
state = function(line, lines) {
|
|
102
|
+
continuation(line, lines, function(x) { current_entry.msgid_plural += x; }, msgstr);
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
else if (line.startsWith('msgstr ') || line.startsWith('msgstr[')) {
|
|
107
|
+
state = msgstr;
|
|
108
|
+
msgstr(line, lines);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
else fatal('Expecting either msgstr or msgid_plural at line number:', lnum);
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function msgstr(line, lines) {
|
|
116
|
+
if (line.startsWith('msgstr ')) {
|
|
117
|
+
if (current_entry.msgid_plural !== null) fatal('Expecting msgstr[0] at line number:', lnum);
|
|
118
|
+
current_entry.msgstr.push(read_string(line.slice('msgstr '.length)));
|
|
119
|
+
state = function(line, lines) {
|
|
120
|
+
continuation(line, lines, function(x) { current_entry.msgstr[current_entry.msgstr.length - 1] += x; }, msgstr);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
else if (line[0] === '#' || line.startsWith('msgid ')) {
|
|
125
|
+
if (!current_entry.msgstr.length) fatal('Expecting msgstr at line number:', lnum);
|
|
126
|
+
commit_entry();
|
|
127
|
+
state = start;
|
|
128
|
+
start(line, lines);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
else if (line.startsWith('msgstr[')) {
|
|
132
|
+
if (current_entry.msgid_plural === null) fatal('Expecting non-plural msgstr at line number:', lnum);
|
|
133
|
+
var pnum = /^msgstr\[(\d+)\] /.exec(line);
|
|
134
|
+
if (!pnum || pnum[1] === undefined) fatal('Malformed msgstr at line number:', lnum);
|
|
135
|
+
var idx = parseInt(pnum[1]);
|
|
136
|
+
current_entry.msgstr[idx] = read_string(line.slice(pnum[0].length));
|
|
137
|
+
state = function(line, lines) {
|
|
138
|
+
continuation(line, lines, function(x) { current_entry.msgstr[idx] += x; }, msgstr);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
else fatal('Expecting msgstr or msgid at line number:', lnum);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
var state = start;
|
|
146
|
+
|
|
147
|
+
while (lines.length) {
|
|
148
|
+
var line = lines.shift().trim();
|
|
149
|
+
lnum += 1;
|
|
150
|
+
if (!line) continue;
|
|
151
|
+
state(line, lines);
|
|
152
|
+
}
|
|
153
|
+
commit_entry();
|
|
154
|
+
if (language === null) fatal('No language specified in the header of this po file');
|
|
155
|
+
return {entries:entries, plural_forms:plural_forms, nplurals:nplurals, language:language};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function read_stdin(cont) {
|
|
159
|
+
var chunks = [];
|
|
160
|
+
process.stdin.setEncoding('utf8');
|
|
161
|
+
|
|
162
|
+
process.stdin.on('readable', function () {
|
|
163
|
+
var chunk = process.stdin.read();
|
|
164
|
+
if (chunk) chunks.push(chunk);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
process.stdin.on('end', function() { cont(chunks.join('')); });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function serialize_catalog(catalog, options) {
|
|
171
|
+
if (!options.use_fuzzy) catalog.entries = catalog.entries.filter(function(e) { return !e.fuzzy; });
|
|
172
|
+
var entries = {};
|
|
173
|
+
catalog.entries.forEach(function (entry) {
|
|
174
|
+
entries[entry.msgid] = entry.msgstr;
|
|
175
|
+
});
|
|
176
|
+
return JSON.stringify({'plural_forms':catalog.plural_forms, 'entries':entries, 'language':catalog.language});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
module.exports.cli = function(argv, base_path, src_path, lib_path) {
|
|
180
|
+
read_stdin(function process(data) {
|
|
181
|
+
var catalog = parse(data);
|
|
182
|
+
console.log(serialize_catalog(catalog, argv));
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
module.exports.parse = parse;
|
|
187
|
+
module.exports.build = function(data, options) { return serialize_catalog(parse(data), options); };
|
package/tools/repl.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* repl.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 util = require('util');
|
|
13
|
+
var utils = require('./utils');
|
|
14
|
+
var completelib = require('./completer');
|
|
15
|
+
var colored = utils.safe_colored;
|
|
16
|
+
var RapydScript = (typeof create_rapydscript_compiler === 'function') ? create_rapydscript_compiler() : require('./compiler').create_compiler();
|
|
17
|
+
var has_prop = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
|
|
18
|
+
|
|
19
|
+
function strip_exports(js) { return js.replace(/^export ((?:async )?function |let )/gm, '$1'); }
|
|
20
|
+
|
|
21
|
+
function create_ctx(baselib, show_js, console) {
|
|
22
|
+
var ctx = vm.createContext({'console':console, 'show_js': !!show_js, 'RapydScript':RapydScript, 'require':require});
|
|
23
|
+
vm.runInContext(baselib, ctx, {'filename':'baselib-plain-pretty.js'});
|
|
24
|
+
vm.runInContext('var __name__ = "__repl__";', ctx);
|
|
25
|
+
return ctx;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
var homedir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
|
|
30
|
+
var cachedir = expanduser(process.env.XDG_CACHE_HOME || '~/.cache');
|
|
31
|
+
|
|
32
|
+
function expanduser(x) {
|
|
33
|
+
if (!x) return x;
|
|
34
|
+
if (x === '~') return homedir;
|
|
35
|
+
if (x.slice(0, 2) != '~/') return x;
|
|
36
|
+
return path.join(homedir, x.slice(2));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function repl_defaults(options) {
|
|
40
|
+
options = options || {};
|
|
41
|
+
if (!options.input) options.input = process.stdin;
|
|
42
|
+
if (!options.output) options.output = process.stdout;
|
|
43
|
+
if (options.show_js === undefined) options.show_js = true;
|
|
44
|
+
if (!options.ps1) options.ps1 = '>>> ';
|
|
45
|
+
if (!options.ps2) options.ps2 = '... ';
|
|
46
|
+
if (!options.console) options.console = console;
|
|
47
|
+
if (!options.readline) options.readline = require('readline');
|
|
48
|
+
if (options.terminal === undefined) options.terminal = options.output.isTTY;
|
|
49
|
+
if (options.histfile === undefined) options.histfile = path.join(cachedir, 'rapydscript-repl.history');
|
|
50
|
+
|
|
51
|
+
options.colored = (options.terminal) ? colored : (function (string) { return string; });
|
|
52
|
+
options.historySize = options.history_size || 1000;
|
|
53
|
+
return options;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function read_history(options) {
|
|
57
|
+
if (options.histfile) {
|
|
58
|
+
try {
|
|
59
|
+
return fs.readFileSync(options.histfile, 'utf-8').split('\n');
|
|
60
|
+
} catch (e) { return []; }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function write_history(options, history) {
|
|
65
|
+
if (options.histfile) {
|
|
66
|
+
history = history.join('\n');
|
|
67
|
+
try {
|
|
68
|
+
return fs.writeFileSync(options.histfile, history, 'utf-8');
|
|
69
|
+
} catch (e) {}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
module.exports = function(options) {
|
|
75
|
+
options = repl_defaults(options);
|
|
76
|
+
options.completer = completer;
|
|
77
|
+
var rl = options.readline.createInterface(options);
|
|
78
|
+
var ps1 = options.colored(options.ps1, 'green');
|
|
79
|
+
var ps2 = options.colored(options.ps2, 'yellow');
|
|
80
|
+
var ctx = create_ctx(print_ast(RapydScript.parse('(def ():\n yield 1\n)'), true), options.show_js, options.console);
|
|
81
|
+
var buffer = [];
|
|
82
|
+
var more = false;
|
|
83
|
+
var LINE_CONTINUATION_CHARS = ':\\';
|
|
84
|
+
var toplevel;
|
|
85
|
+
var import_dirs = utils.get_import_dirs();
|
|
86
|
+
var find_completions = completelib(RapydScript, options);
|
|
87
|
+
|
|
88
|
+
options.console.log(options.colored('Welcome to the RapydScript REPL! Press Ctrl+C then Ctrl+D to quit.', 'green', true));
|
|
89
|
+
if (options.show_js)
|
|
90
|
+
options.console.log(options.colored('Use show_js=False to stop the REPL from showing the compiled JavaScript.', 'green', true));
|
|
91
|
+
else
|
|
92
|
+
options.console.log(options.colored('Use show_js=True to have the REPL show the compiled JavaScript before executing it.', 'green', true));
|
|
93
|
+
options.console.log();
|
|
94
|
+
|
|
95
|
+
function print_ast(ast, keep_baselib) {
|
|
96
|
+
var output_options = {omit_baselib:!keep_baselib, write_name:false, private_scope:false, beautify:true, keep_docstrings:true};
|
|
97
|
+
if (keep_baselib) output_options.baselib_plain = fs.readFileSync(path.join(options.lib_path, 'baselib-plain-pretty.js'), 'utf-8');
|
|
98
|
+
var output = new RapydScript.OutputStream(output_options);
|
|
99
|
+
ast.print(output);
|
|
100
|
+
return output.get();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
function resetbuffer() { buffer = []; }
|
|
105
|
+
|
|
106
|
+
function completer(line) {
|
|
107
|
+
return find_completions(line, ctx);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function prompt() {
|
|
111
|
+
var lw = '';
|
|
112
|
+
if (more && buffer.length) {
|
|
113
|
+
var prev_line = buffer[buffer.length - 1];
|
|
114
|
+
if (prev_line.trimRight().substr(prev_line.length - 1) == ':') lw = ' ';
|
|
115
|
+
prev_line = prev_line.match(/^\s+/);
|
|
116
|
+
if (prev_line) lw += prev_line;
|
|
117
|
+
}
|
|
118
|
+
rl.setPrompt((more) ? ps2 : ps1);
|
|
119
|
+
if (rl.sync_prompt) rl.prompt(lw);
|
|
120
|
+
else {
|
|
121
|
+
rl.prompt();
|
|
122
|
+
if (lw) rl.write(lw);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function runjs(js) {
|
|
127
|
+
var result;
|
|
128
|
+
if (vm.runInContext('show_js', ctx)) {
|
|
129
|
+
options.console.log(options.colored('---------- Compiled JavaScript ---------', 'green', true));
|
|
130
|
+
options.console.log(js);
|
|
131
|
+
options.console.log(options.colored('---------- Running JavaScript ---------', 'green', true));
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
// Despite what the docs say node does not actually output any errors by itself
|
|
135
|
+
// so, in case this bug is fixed later, we turn it off explicitly.
|
|
136
|
+
result = vm.runInContext(strip_exports(js), ctx, {'filename':'<repl>', 'displayErrors':false});
|
|
137
|
+
} catch(e) {
|
|
138
|
+
if (e.stack) options.console.error(e.stack);
|
|
139
|
+
else options.console.error(e.toString());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (result !== undefined) {
|
|
143
|
+
options.console.log(util.inspect(result, {'colors':options.terminal}));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function compile_source(source) {
|
|
148
|
+
var classes = (toplevel) ? toplevel.classes : undefined;
|
|
149
|
+
var scoped_flags = (toplevel) ? toplevel.scoped_flags: undefined;
|
|
150
|
+
try {
|
|
151
|
+
toplevel = RapydScript.parse(source, {
|
|
152
|
+
'filename':'<repl>',
|
|
153
|
+
'basedir': process.cwd(),
|
|
154
|
+
'libdir': options.imp_path,
|
|
155
|
+
'import_dirs': import_dirs,
|
|
156
|
+
'classes': classes,
|
|
157
|
+
'scoped_flags': scoped_flags,
|
|
158
|
+
});
|
|
159
|
+
} catch(e) {
|
|
160
|
+
if (e.is_eof && e.line == buffer.length && e.col > 0) return true;
|
|
161
|
+
if (e.message && e.line !== undefined) options.console.log(e.line + ':' + e.col + ':' + e.message);
|
|
162
|
+
else options.console.log(e.stack || e.toString());
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
var output = print_ast(toplevel);
|
|
166
|
+
if (classes) {
|
|
167
|
+
var exports = {};
|
|
168
|
+
toplevel.exports.forEach(function (name) { exports[name] = true; });
|
|
169
|
+
Object.getOwnPropertyNames(classes).forEach(function (name) {
|
|
170
|
+
if (!has_prop(exports, name) && !has_prop(toplevel.classes, name))
|
|
171
|
+
toplevel.classes[name] = classes[name];
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
scoped_flags = toplevel.scoped_flags;
|
|
175
|
+
runjs(output);
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function push(line) {
|
|
180
|
+
buffer.push(line);
|
|
181
|
+
var ll = line.trimRight();
|
|
182
|
+
if (ll && LINE_CONTINUATION_CHARS.indexOf(ll.substr(ll.length - 1)) > -1)
|
|
183
|
+
return true;
|
|
184
|
+
var source = buffer.join('\n');
|
|
185
|
+
if (!source.trim()) { resetbuffer(); return false; }
|
|
186
|
+
var incomplete = compile_source(source);
|
|
187
|
+
if (!incomplete) resetbuffer();
|
|
188
|
+
return incomplete;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
rl.on('line', function(line) {
|
|
192
|
+
if (more) {
|
|
193
|
+
// We are in a block
|
|
194
|
+
var line_is_empty = !line.trimLeft();
|
|
195
|
+
if (line_is_empty && buffer.length && !buffer[buffer.length - 1].trimLeft()) {
|
|
196
|
+
// We have two empty lines, evaluate the block
|
|
197
|
+
more = push(line.trimLeft());
|
|
198
|
+
} else buffer.push(line);
|
|
199
|
+
} else more = push(line); // Not in a block, evaluate line
|
|
200
|
+
prompt();
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
.on('close', function() {
|
|
204
|
+
options.console.log('Bye!');
|
|
205
|
+
if (rl.history) write_history(options, rl.history);
|
|
206
|
+
process.exit(0);
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
.on('SIGINT', function() {
|
|
210
|
+
rl.clearLine();
|
|
211
|
+
options.console.log('Keyboard Interrupt');
|
|
212
|
+
resetbuffer();
|
|
213
|
+
more = false;
|
|
214
|
+
prompt();
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
.on('SIGCONT', function() {
|
|
218
|
+
prompt();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
rl.history = read_history(options);
|
|
222
|
+
prompt();
|
|
223
|
+
};
|
package/tools/self.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* self.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 path = require('path');
|
|
10
|
+
var crypto = require('crypto');
|
|
11
|
+
var fs = require('fs');
|
|
12
|
+
var vm = require('vm');
|
|
13
|
+
var zlib = require('zlib');
|
|
14
|
+
|
|
15
|
+
function compile_baselib(RapydScript, src_path) {
|
|
16
|
+
var items = fs.readdirSync(src_path).filter(function(name) {
|
|
17
|
+
return name.slice(0, 'baselib-'.length) === 'baselib-' && name.slice(-4) == '.pyj';
|
|
18
|
+
});
|
|
19
|
+
var ans = {'pretty': '', 'ugly': ''};
|
|
20
|
+
|
|
21
|
+
items.sort().forEach(function(fname) {
|
|
22
|
+
var name = fname.slice('baselib-'.length, -4), ast;
|
|
23
|
+
var raw = fs.readFileSync(path.join(src_path, fname), 'utf-8');
|
|
24
|
+
try {
|
|
25
|
+
ast = RapydScript.parse(raw, {filename:fname, basedir:src_path});
|
|
26
|
+
} catch (e) {
|
|
27
|
+
if (!(e instanceof RapydScript.SyntaxError)) throw e;
|
|
28
|
+
console.error(e.toString());
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
[true, false].forEach(function (beautify) {
|
|
32
|
+
var output = new RapydScript.OutputStream({
|
|
33
|
+
beautify: beautify, write_name: false, private_scope:false, omit_baselib: true,
|
|
34
|
+
});
|
|
35
|
+
ast.print(output);
|
|
36
|
+
ans[(beautify) ? 'pretty' : 'ugly'] += output.get();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
return ans;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function check_for_changes(base_path, src_path, signatures) {
|
|
43
|
+
// Check if any of the files involved in the build process have changed,
|
|
44
|
+
// as compared to the saved sha1 hashes from the last build
|
|
45
|
+
var saved_hashes = {}, hashes = {}, sources = {};
|
|
46
|
+
var compiler_changed = false, compiler_hash, source_hash;
|
|
47
|
+
try {
|
|
48
|
+
saved_hashes = JSON.parse(fs.readFileSync(signatures, 'utf-8'));
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (e.code != 'ENOENT') throw (e);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var src_file_names = [];
|
|
54
|
+
|
|
55
|
+
function process_dir(p) {
|
|
56
|
+
fs.readdirSync(p).forEach(function(name) {
|
|
57
|
+
var fp = path.join(p, name);
|
|
58
|
+
if (name.substr(-4) === '.pyj') src_file_names.push(path.relative(src_path, fp));
|
|
59
|
+
else if (name != 'lib' && fs.statSync(fp).isDirectory()) process_dir(fp);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
process_dir(src_path);
|
|
63
|
+
|
|
64
|
+
compiler_hash = crypto.createHash('sha1');
|
|
65
|
+
source_hash = crypto.createHash('sha1');
|
|
66
|
+
src_file_names.forEach(function(fname) {
|
|
67
|
+
var src = path.join(src_path, fname);
|
|
68
|
+
sources[src] = fs.readFileSync(src, 'utf-8');
|
|
69
|
+
compiler_hash.update(sources[src]);
|
|
70
|
+
source_hash.update(sources[src]);
|
|
71
|
+
var h = crypto.createHash('sha1');
|
|
72
|
+
h.update(sources[src]);
|
|
73
|
+
hashes[fname.split('.')[0]] = h.digest('hex');
|
|
74
|
+
});
|
|
75
|
+
var compiler_files = [module.filename, path.join(base_path, 'tools', 'compiler.js')];
|
|
76
|
+
compiler_files.forEach(function(fpath) {
|
|
77
|
+
compiler_hash.update(fs.readFileSync(fpath, 'utf-8'));
|
|
78
|
+
});
|
|
79
|
+
hashes['#compiler#'] = compiler_hash.digest('hex');
|
|
80
|
+
hashes['#compiled_with#'] = saved_hashes['#compiler#'] || 'unknown';
|
|
81
|
+
source_hash = source_hash.digest('hex');
|
|
82
|
+
if (hashes['#compiler#'] != saved_hashes['#compiler#']) {
|
|
83
|
+
console.log('There are changes to the source files of the compiler, rebuiliding');
|
|
84
|
+
compiler_changed = true;
|
|
85
|
+
} else if (hashes['#compiled_with#'] != saved_hashes['#compiled_with#']) {
|
|
86
|
+
console.log('Re-building compiler with updated version of itself');
|
|
87
|
+
compiler_changed = true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return [source_hash, compiler_changed, sources, hashes];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
function compile(src_path, lib_path, sources, source_hash, profile) {
|
|
95
|
+
var file = path.join(src_path, 'compiler.pyj');
|
|
96
|
+
var t1 = new Date().getTime();
|
|
97
|
+
var RapydScript = require('./compiler').create_compiler();
|
|
98
|
+
var output_options, profiler, cpu_profile;
|
|
99
|
+
var compiled_baselib = compile_baselib(RapydScript, src_path);
|
|
100
|
+
var out_path = path.join(path.dirname(lib_path), 'dev');
|
|
101
|
+
try {
|
|
102
|
+
fs.mkdirSync(out_path);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (e.code != 'EEXIST') throw e;
|
|
105
|
+
}
|
|
106
|
+
output_options = {'beautify': true, 'baselib_plain': compiled_baselib.pretty};
|
|
107
|
+
|
|
108
|
+
var raw = sources[file], toplevel;
|
|
109
|
+
|
|
110
|
+
function parse_file(code, file) {
|
|
111
|
+
return RapydScript.parse(code, {
|
|
112
|
+
filename: file,
|
|
113
|
+
basedir: path.dirname(file),
|
|
114
|
+
libdir: path.join(src_path, 'lib'),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
if (profile) {
|
|
120
|
+
profiler = require('v8-profiler');
|
|
121
|
+
profiler.startProfiling();
|
|
122
|
+
}
|
|
123
|
+
toplevel = parse_file(raw, file);
|
|
124
|
+
if (profile) {
|
|
125
|
+
cpu_profile = profiler.stopProfiling();
|
|
126
|
+
fs.writeFileSync('self.cpuprofile', JSON.stringify(cpu_profile), 'utf-8');
|
|
127
|
+
}
|
|
128
|
+
} catch (e) {
|
|
129
|
+
if (!(e instanceof RapydScript.SyntaxError)) throw e;
|
|
130
|
+
console.error(e.toString());
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
var output = new RapydScript.OutputStream(output_options);
|
|
134
|
+
toplevel.print(output);
|
|
135
|
+
output = output.get().replace('__COMPILER_VERSION__', source_hash);
|
|
136
|
+
fs.writeFileSync(path.join(out_path, 'compiler.js'), output, "utf8");
|
|
137
|
+
fs.writeFileSync(path.join(out_path, 'baselib-plain-pretty.js'), compiled_baselib.pretty, 'utf-8');
|
|
138
|
+
fs.writeFileSync(path.join(out_path, 'baselib-plain-ugly.js'), compiled_baselib.ugly, 'utf-8');
|
|
139
|
+
console.log('Compiler built in', (new Date().getTime() - t1)/1000, 'seconds\n');
|
|
140
|
+
return output;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function run_single_compile(base_path, src_path, lib_path, profile) {
|
|
144
|
+
var out_path = path.join(path.dirname(lib_path), 'dev');
|
|
145
|
+
var signatures = path.join(out_path, 'signatures.json');
|
|
146
|
+
var temp = check_for_changes(base_path, src_path, signatures);
|
|
147
|
+
var source_hash = temp[0], compiler_changed = temp[1], sources = temp[2], hashes = temp[3];
|
|
148
|
+
|
|
149
|
+
if (compiler_changed) {
|
|
150
|
+
compile(src_path, lib_path, sources, source_hash, profile);
|
|
151
|
+
fs.writeFileSync(signatures, JSON.stringify(hashes, null, 4));
|
|
152
|
+
} else console.log('Compiler is built with the up-to-date version of itself');
|
|
153
|
+
return compiler_changed;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
module.exports = function compile_self(base_path, src_path, lib_path, complete, profile) {
|
|
157
|
+
var changed;
|
|
158
|
+
do {
|
|
159
|
+
changed = run_single_compile(base_path, src_path, lib_path, profile);
|
|
160
|
+
lib_path = path.join(path.dirname(lib_path), 'dev');
|
|
161
|
+
} while (changed && complete);
|
|
162
|
+
};
|
package/tools/test.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* test.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
|
+
var path = require('path');
|
|
9
|
+
var fs = require('fs');
|
|
10
|
+
var RapydScript = require('./compiler').create_compiler();
|
|
11
|
+
var utils = require('./utils');
|
|
12
|
+
var colored = utils.safe_colored;
|
|
13
|
+
|
|
14
|
+
module.exports = function(argv, base_path, src_path, lib_path) {
|
|
15
|
+
// run all tests and exit
|
|
16
|
+
var assert = require("assert");
|
|
17
|
+
var os = require('os');
|
|
18
|
+
var failures = [];
|
|
19
|
+
var vm = require('vm');
|
|
20
|
+
var compiler_dir = path.join(base_path, 'dev');
|
|
21
|
+
if (!utils.path_exists(path.join(compiler_dir, 'compiler.js'))) compiler_dir = path.join(base_path, 'release');
|
|
22
|
+
var test_dir = path.join(base_path, 'test');
|
|
23
|
+
var baselib = fs.readFileSync(path.join(lib_path, 'baselib-plain-pretty.js'), 'utf-8');
|
|
24
|
+
var files;
|
|
25
|
+
var deep_eq = assert.deepEqual;
|
|
26
|
+
assert.deepEqual = function(a, b, message) {
|
|
27
|
+
// Compare array objects that have extra properties as simple arrays
|
|
28
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
29
|
+
if (a === b) return;
|
|
30
|
+
if (a.length !== b.length) throw new assert.AssertionError({actual:a, expected:b, operator:'deepEqual', stackStartFunction:assert.deepEqual});
|
|
31
|
+
for(var i=0; i < a.length; i++) assert.deepEqual(a[i], b[i], message);
|
|
32
|
+
} else if (a !== undefined && a !== null && typeof a.__eq__ === 'function') {
|
|
33
|
+
if (!a.__eq__(b)) throw new assert.AssertionError({actual:a, expected:b, operator:'deepEqual', stackStartFunction:assert.deepEqual});
|
|
34
|
+
} else return deep_eq(a, b, message);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
if (argv.files.length) {
|
|
38
|
+
files = [];
|
|
39
|
+
argv.files.forEach(function(fname) { files.push(fname + '.pyj'); });
|
|
40
|
+
} else {
|
|
41
|
+
files = fs.readdirSync(test_dir).filter(function(name){
|
|
42
|
+
return /^[^_].*\.pyj$/.test(name);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
files.forEach(function(file){
|
|
46
|
+
var ast;
|
|
47
|
+
var filepath = path.join(test_dir, file);
|
|
48
|
+
var failed = false;
|
|
49
|
+
try {
|
|
50
|
+
ast = RapydScript.parse(fs.readFileSync(filepath, "utf-8"), {
|
|
51
|
+
filename: file,
|
|
52
|
+
toplevel: ast,
|
|
53
|
+
basedir: test_dir,
|
|
54
|
+
libdir: path.join(src_path, 'lib'),
|
|
55
|
+
});
|
|
56
|
+
if (ast.imports && Object.keys(ast.imports).length) {
|
|
57
|
+
RapydScript.tree_shake(ast, {
|
|
58
|
+
parse: RapydScript.parse,
|
|
59
|
+
import_dirs: [],
|
|
60
|
+
basedir: test_dir,
|
|
61
|
+
libdir: path.join(src_path, 'lib'),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
} catch(e) {
|
|
65
|
+
failures.push(file);
|
|
66
|
+
failed = true;
|
|
67
|
+
console.log(colored(file, 'red') + ': ' + e + "\n\n");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var js_version = 5;
|
|
72
|
+
while (js_version < 7) {
|
|
73
|
+
// generate output
|
|
74
|
+
var output = new RapydScript.OutputStream({
|
|
75
|
+
baselib_plain: baselib,
|
|
76
|
+
beautify: true,
|
|
77
|
+
js_version: js_version,
|
|
78
|
+
keep_docstrings: true,
|
|
79
|
+
});
|
|
80
|
+
ast.print(output);
|
|
81
|
+
|
|
82
|
+
// test that output performs correct JS operations
|
|
83
|
+
var jsfile = path.join(os.tmpdir(), file + '-es' + js_version + '.js');
|
|
84
|
+
var code = output.toString();
|
|
85
|
+
try {
|
|
86
|
+
vm.runInNewContext(code, {
|
|
87
|
+
'assrt':assert,
|
|
88
|
+
'__name__': jsfile,
|
|
89
|
+
'require':require,
|
|
90
|
+
'fs':fs,
|
|
91
|
+
'RapydScript':RapydScript,
|
|
92
|
+
'console':console,
|
|
93
|
+
'compiler_dir': compiler_dir,
|
|
94
|
+
'test_path':test_dir,
|
|
95
|
+
'Buffer': Buffer,
|
|
96
|
+
}, {'filename':jsfile});
|
|
97
|
+
} catch (e) {
|
|
98
|
+
failures.push(file);
|
|
99
|
+
failed = true;
|
|
100
|
+
fs.writeFileSync(jsfile, code);
|
|
101
|
+
console.error('Failed running: ' + colored(jsfile, 'red'));
|
|
102
|
+
if (e.stack)
|
|
103
|
+
console.error(colored(file, 'red') + ":\n" + e.stack + "\n\n");
|
|
104
|
+
else
|
|
105
|
+
console.error(colored(file, 'red') + ": " + e + "\n\n");
|
|
106
|
+
js_version = 1000;
|
|
107
|
+
}
|
|
108
|
+
js_version++;
|
|
109
|
+
}
|
|
110
|
+
if (!failed) console.log(colored(file, 'green') + ": test completed successfully\n");
|
|
111
|
+
else { console.log(colored(file, 'red') + ":\ttest failed\n"); }
|
|
112
|
+
});
|
|
113
|
+
if (failures.length) {
|
|
114
|
+
console.log(colored('There were ' + failures.length + ' test failure(s):', 'red'));
|
|
115
|
+
console.log.apply(console, failures);
|
|
116
|
+
} else console.log(colored('All tests passed!', 'green'));
|
|
117
|
+
process.exit((failures.length) ? 1 : 0);
|
|
118
|
+
};
|