mambascript 0.5.5 → 2.0.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/CHANGELOG.md +326 -0
- package/README.md +509 -267
- package/bin/mamba +1 -258
- package/decision.js +1 -246
- package/docs/coverage.md +70 -0
- package/docs/features.md +3348 -0
- package/docs/perf.md +94 -0
- package/docs/reference.md +1295 -0
- package/docs/vs-coffeescript.md +1593 -0
- package/docs/vs-iced-coffeescript.md +626 -0
- package/docs/vs-typescript.md +107 -0
- package/esm.js +8 -0
- package/jest.js +33 -0
- package/lib/black.js +17 -13
- package/lib/browser.js +2 -4
- package/lib/cli.js +207 -136
- package/lib/compiler.js +1162 -93
- package/lib/decision.js +10 -10
- package/lib/diagnostics.js +219 -0
- package/lib/esm-hooks.js +60 -0
- package/lib/fast-parser.js +3469 -0
- package/lib/functional-helpers.js +26 -9
- package/lib/helpers.js +67 -50
- package/lib/js-nodes.js +151 -12
- package/lib/lexer.js +1499 -0
- package/lib/light-module.js +200 -63
- package/lib/mamba.js +372 -0
- package/lib/module.js +23 -172
- package/lib/nodes.js +84 -21
- package/lib/optimiser.js +29 -9
- package/lib/optparse.js +32 -15
- package/lib/parser.js +9433 -4840
- package/lib/preprocessor.js +3 -5
- package/lib/register.js +49 -56
- package/lib/repl.js +28 -9
- package/lib/reporter.js +106 -69
- package/lib/run.js +3 -5
- package/lib/string-scanner.js +94 -0
- package/lib/syntax-tables.js +480 -0
- package/lib/type-checker.js +258 -44
- package/lib/type-helpers.js +28 -10
- package/lib/type-resolver.js +256 -34
- package/lib/type-scope.js +10 -6
- package/lib/type-walker.js +664 -85
- package/lib/types.js +17 -7
- package/lib/underscore.js +170 -76
- package/lib/unicode-classes.js +122 -0
- package/mambaview.js +1 -0
- package/package.json +49 -46
- package/stdlib.js +1 -0
- package/vendor/README.md +40 -0
- package/vendor/escodegen/LICENSE.BSD +19 -0
- package/vendor/escodegen/escodegen.js +2289 -0
- package/vendor/estraverse/LICENSE.BSD +19 -0
- package/vendor/estraverse/estraverse.js +706 -0
- package/vendor/mambacodegen/LICENSE +24 -0
- package/vendor/mambacodegen/lib/mscodegen.js +620 -0
- package/vendor/patches/escodegen.diff +130 -0
- package/vendor/patches/estraverse.diff +51 -0
- package/vendor/patches/pegjs.diff +70 -0
- package/vendor/patches/source-map.diff +160 -0
- package/vendor/source-map/LICENSE +28 -0
- package/vendor/source-map/lib/source-map/array-set.js +92 -0
- package/vendor/source-map/lib/source-map/base64-vlq.js +140 -0
- package/vendor/source-map/lib/source-map/base64.js +38 -0
- package/vendor/source-map/lib/source-map/binary-search.js +77 -0
- package/vendor/source-map/lib/source-map/source-map-consumer.js +404 -0
- package/vendor/source-map/lib/source-map/source-map-generator.js +229 -0
- package/vendor/source-map/lib/source-map/source-node.js +201 -0
- package/vendor/source-map/lib/source-map/util.js +57 -0
- package/vendor/source-map/lib/source-map.js +8 -0
- package/bin/fix +0 -17
- package/bin/updateImports +0 -46
- package/build.js +0 -25
- package/lib/module-esm.js +0 -901
- package/lib/pegjs-kofu-plugin.js +0 -35
- package/lib/pegjs-mamba-plugin.js +0 -35
- package/lib/processor-esm.js +0 -507
- package/mamba +0 -259
- package/msast.js +0 -1
- package/utils/gulp-mamba.js +0 -29
- /package/{lib/esm.js → vendor/patches/mambacodegen.diff} +0 -0
package/lib/light-module.js
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
|
-
// Generated by MambaScript 0.
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// Generated by MambaScript 2.0.0
|
|
2
|
+
var cache$, cleanMarkers, Compiler, DEFAULT_PARSER, Diagnostics, escodegen, escodegenFormat, ESMRewriter, FastParser, humanReadable, literateToCode, MambaScript, mscodegen, Nodes, Optimiser, Parser, parserChoice, PARSERS, path, pkg, Preprocessor, reporter, TYPE_CHECK_MODES, TypeError, TypeWalker;
|
|
3
|
+
cache$ = require('./helpers');
|
|
4
|
+
literateToCode = cache$.literateToCode;
|
|
5
|
+
cleanMarkers = cache$.cleanMarkers;
|
|
6
|
+
humanReadable = cache$.humanReadable;
|
|
7
|
+
Diagnostics = require('./diagnostics');
|
|
6
8
|
Nodes = require('./nodes');
|
|
7
9
|
Preprocessor = require('./preprocessor').Preprocessor;
|
|
8
10
|
Parser = require('./parser');
|
|
11
|
+
FastParser = require('./fast-parser');
|
|
9
12
|
TypeWalker = require('./type-walker');
|
|
10
13
|
Optimiser = require('./optimiser').Optimiser;
|
|
11
14
|
Compiler = require('./compiler').Compiler;
|
|
12
15
|
reporter = require('./reporter');
|
|
13
16
|
TypeError = require('./type-helpers').TypeError;
|
|
14
|
-
mscodegen =
|
|
15
|
-
|
|
16
|
-
return require('mambacodegen');
|
|
17
|
-
} catch (e$) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
}.call(this);
|
|
21
|
-
escodegen = function () {
|
|
22
|
-
try {
|
|
23
|
-
return require('escodegen');
|
|
24
|
-
} catch (e$1) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
}.call(this);
|
|
17
|
+
mscodegen = require('../vendor/mambacodegen/lib/mscodegen');
|
|
18
|
+
escodegen = require('../vendor/escodegen/escodegen');
|
|
28
19
|
ESMRewriter = require('../utils/esm');
|
|
20
|
+
path = require('path');
|
|
29
21
|
pkg = require('./../package.json');
|
|
30
22
|
escodegenFormat = {
|
|
31
23
|
indent: {
|
|
@@ -37,64 +29,192 @@ escodegenFormat = {
|
|
|
37
29
|
quotes: 'auto',
|
|
38
30
|
parentheses: false
|
|
39
31
|
};
|
|
32
|
+
TYPE_CHECK_MODES = [
|
|
33
|
+
'off',
|
|
34
|
+
'warn',
|
|
35
|
+
'error'
|
|
36
|
+
];
|
|
37
|
+
PARSERS = [
|
|
38
|
+
'peg',
|
|
39
|
+
'fast'
|
|
40
|
+
];
|
|
41
|
+
DEFAULT_PARSER = 'fast';
|
|
42
|
+
parserChoice = function (options) {
|
|
43
|
+
var name;
|
|
44
|
+
name = null != options.parser ? options.parser : null != process.env.MAMBA_PARSER ? process.env.MAMBA_PARSER : DEFAULT_PARSER;
|
|
45
|
+
if (!in$(name, PARSERS))
|
|
46
|
+
throw new Error('parser must be one of ' + PARSERS.join(', ') + ' (got ' + name + ')');
|
|
47
|
+
return name;
|
|
48
|
+
};
|
|
40
49
|
MambaScript = {
|
|
41
50
|
MambaScript: MambaScript,
|
|
42
51
|
Compiler: Compiler,
|
|
43
52
|
Optimiser: Optimiser,
|
|
44
53
|
Parser: Parser,
|
|
54
|
+
FastParser: FastParser,
|
|
55
|
+
parsers: {
|
|
56
|
+
peg: Parser,
|
|
57
|
+
fast: FastParser
|
|
58
|
+
},
|
|
59
|
+
defaultParser: DEFAULT_PARSER,
|
|
45
60
|
Preprocessor: Preprocessor,
|
|
46
61
|
Nodes: Nodes,
|
|
62
|
+
TypeError: TypeError,
|
|
63
|
+
reporter: reporter,
|
|
64
|
+
Diagnostics: Diagnostics,
|
|
47
65
|
VERSION: pkg.version,
|
|
66
|
+
stdlibPath: path.join(__dirname, 'underscore'),
|
|
48
67
|
parse: function (mamba, options) {
|
|
49
|
-
var e, parsed,
|
|
68
|
+
var column, ctx, e, err, errorSource, file, found, headline, mode, parsed, raw, source, warn, which;
|
|
50
69
|
if (null == options)
|
|
51
70
|
options = {};
|
|
71
|
+
mode = null != options.typeCheck ? options.typeCheck : 'warn';
|
|
72
|
+
if (!in$(mode, TYPE_CHECK_MODES))
|
|
73
|
+
throw new Error('typeCheck must be one of ' + TYPE_CHECK_MODES.join(', ') + ' (got ' + mode + ')');
|
|
74
|
+
file = null != options.filename ? options.filename : options.inputSource;
|
|
75
|
+
raw = options.raw || mode !== 'off';
|
|
76
|
+
which = parserChoice(options);
|
|
77
|
+
reporter.clean();
|
|
78
|
+
errorSource = mamba;
|
|
52
79
|
try {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
80
|
+
if (which === 'peg') {
|
|
81
|
+
errorSource = Preprocessor.process(mamba, { literate: options.literate });
|
|
82
|
+
parsed = Parser.parse(errorSource, {
|
|
83
|
+
raw: raw,
|
|
84
|
+
inputSource: null != options.inputSource ? options.inputSource : file
|
|
85
|
+
});
|
|
86
|
+
} else {
|
|
87
|
+
source = options.literate ? literateToCode(mamba) : mamba;
|
|
88
|
+
parsed = FastParser.parse(source, {
|
|
89
|
+
raw: raw,
|
|
90
|
+
inputSource: null != options.inputSource ? options.inputSource : file,
|
|
91
|
+
filename: file,
|
|
92
|
+
pegCompat: options.pegCompat
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
Object.defineProperty(parsed, 'sourceText', {
|
|
96
|
+
value: errorSource,
|
|
97
|
+
enumerable: false,
|
|
98
|
+
configurable: true
|
|
57
99
|
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
100
|
+
Object.defineProperty(parsed, 'sourceFile', {
|
|
101
|
+
value: null != file ? file : '<input>',
|
|
102
|
+
enumerable: false,
|
|
103
|
+
configurable: true
|
|
104
|
+
});
|
|
105
|
+
if (!(mode === 'off')) {
|
|
106
|
+
TypeWalker.checkNodes(parsed, { persistScope: options.persistScope });
|
|
107
|
+
ctx = {
|
|
108
|
+
file: file,
|
|
109
|
+
source: errorSource
|
|
110
|
+
};
|
|
111
|
+
warn = null != options.warn ? options.warn : function (msg) {
|
|
112
|
+
return console.warn(msg);
|
|
113
|
+
};
|
|
114
|
+
if (reporter.has_warnings())
|
|
115
|
+
warn(reporter.reportWarnings(ctx));
|
|
116
|
+
if (reporter.has_errors())
|
|
117
|
+
if (mode === 'error') {
|
|
118
|
+
throw new TypeError(reporter.report(ctx), reporter.errors.slice(), file);
|
|
119
|
+
} else {
|
|
120
|
+
warn(reporter.report(ctx));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
61
123
|
return options.optimise ? Optimiser.optimise(parsed) : parsed;
|
|
62
|
-
} catch (e$
|
|
63
|
-
e = e
|
|
124
|
+
} catch (e$) {
|
|
125
|
+
e = e$;
|
|
64
126
|
if (e instanceof TypeError)
|
|
65
|
-
throw e.message;
|
|
66
|
-
if (!(e instanceof Parser.SyntaxError))
|
|
67
127
|
throw e;
|
|
68
|
-
|
|
128
|
+
if (e instanceof Parser.SyntaxError) {
|
|
129
|
+
column = cleanMarkers(('' + errorSource.split('\n')[e.line - 1] + '\n').slice(0, e.column)).length;
|
|
130
|
+
found = null != e.found ? humanReadable(e.found) : null;
|
|
131
|
+
headline = null != found ? 'unexpected ' + JSON.stringify(found) : 'unexpected end of input';
|
|
132
|
+
err = new Error(headline);
|
|
133
|
+
err.name = 'SyntaxError';
|
|
134
|
+
err.line = e.line;
|
|
135
|
+
err.column = column;
|
|
136
|
+
err.found = e.found;
|
|
137
|
+
throw Diagnostics.attach(err, {
|
|
138
|
+
kind: 'SyntaxError',
|
|
139
|
+
code: 'MS1900',
|
|
140
|
+
file: null != file ? file : '<input>',
|
|
141
|
+
line: e.line,
|
|
142
|
+
column: column,
|
|
143
|
+
endColumn: column + (null != found && found.indexOf('\n') < 0 ? Math.max(1, found.length) : 1),
|
|
144
|
+
headline: headline,
|
|
145
|
+
why: 'The fallback parser (--parser peg) stopped here and cannot name the construct that is incomplete. Run without --parser peg for a precise message.',
|
|
146
|
+
source: errorSource
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (which === 'peg' && !(e instanceof FastParser.SyntaxError)) {
|
|
150
|
+
headline = ('' + e.message).split('\n')[0];
|
|
151
|
+
err = new Error(headline);
|
|
152
|
+
err.name = 'SyntaxError';
|
|
153
|
+
err.line = null != e.line ? e.line : 1;
|
|
154
|
+
err.column = null != e.column ? e.column : 1;
|
|
155
|
+
throw Diagnostics.attach(err, {
|
|
156
|
+
kind: 'SyntaxError',
|
|
157
|
+
code: 'MS1900',
|
|
158
|
+
file: null != file ? file : '<input>',
|
|
159
|
+
line: err.line,
|
|
160
|
+
column: err.column,
|
|
161
|
+
endColumn: err.column + 1,
|
|
162
|
+
headline: headline,
|
|
163
|
+
why: 'The fallback parser (--parser peg) stopped here and cannot name the construct that is incomplete. Run without --parser peg for a precise message.',
|
|
164
|
+
source: errorSource
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
if (e instanceof FastParser.SyntaxError)
|
|
168
|
+
throw Diagnostics.attach(e, {
|
|
169
|
+
kind: 'SyntaxError',
|
|
170
|
+
code: null != e.code ? e.code : 'MS1125',
|
|
171
|
+
file: null != file ? file : '<input>',
|
|
172
|
+
line: e.line,
|
|
173
|
+
column: e.column,
|
|
174
|
+
endColumn: e.endColumn,
|
|
175
|
+
headline: e.message,
|
|
176
|
+
why: e.why,
|
|
177
|
+
fix: e.fix,
|
|
178
|
+
related: e.related,
|
|
179
|
+
source: errorSource
|
|
180
|
+
});
|
|
181
|
+
throw e;
|
|
69
182
|
}
|
|
70
183
|
},
|
|
71
|
-
esCompile: function (mamba, options) {
|
|
72
|
-
var jsAST, msAst;
|
|
73
|
-
if (null == options)
|
|
74
|
-
options = {};
|
|
75
|
-
msAst = this.parse(mamba);
|
|
76
|
-
jsAST = this.compile(msAst, { bare: true });
|
|
77
|
-
return this.jsEsm(jsAST);
|
|
78
|
-
},
|
|
79
184
|
compile: function (msAst, options) {
|
|
80
|
-
|
|
185
|
+
var e;
|
|
186
|
+
try {
|
|
187
|
+
return Compiler.compile(msAst, options).toBasicObject();
|
|
188
|
+
} catch (e$) {
|
|
189
|
+
e = e$;
|
|
190
|
+
if (!(('undefined' !== typeof e && null != e ? e.compileStage : void 0) && null != e.line))
|
|
191
|
+
throw e;
|
|
192
|
+
throw Diagnostics.attach(e, {
|
|
193
|
+
kind: 'SyntaxError',
|
|
194
|
+
code: null != e.code ? e.code : 'MS1300',
|
|
195
|
+
file: null != msAst.sourceFile ? msAst.sourceFile : '<input>',
|
|
196
|
+
line: e.line,
|
|
197
|
+
column: e.column,
|
|
198
|
+
endColumn: e.endColumn,
|
|
199
|
+
headline: e.message,
|
|
200
|
+
why: e.why,
|
|
201
|
+
fix: e.fix,
|
|
202
|
+
source: msAst.sourceText
|
|
203
|
+
});
|
|
204
|
+
}
|
|
81
205
|
},
|
|
82
206
|
compileTypedToCS: function (msAst, options) {
|
|
83
|
-
return mscodegen(msAst);
|
|
84
|
-
},
|
|
85
|
-
cs: function (msAst, options) {
|
|
207
|
+
return mscodegen.generate(msAst, options);
|
|
86
208
|
},
|
|
87
209
|
jsWithSourceMap: function (jsAst, name, options) {
|
|
88
|
-
var targetName;
|
|
210
|
+
var cache$1, targetName;
|
|
89
211
|
if (null == name)
|
|
90
212
|
name = 'unknown';
|
|
91
213
|
if (null == options)
|
|
92
214
|
options = {};
|
|
93
|
-
if (!(null != escodegen))
|
|
94
|
-
throw new Error('escodegen not found: run `npm install escodegen`');
|
|
95
215
|
if (!{}.hasOwnProperty.call(jsAst, 'type'))
|
|
96
216
|
jsAst = jsAst.toBasicObject();
|
|
97
|
-
targetName = options.sourceMapFile || options.sourceMap && options.output.match(/^.*[\\\/]([^\\\/]+)$/)[1];
|
|
217
|
+
targetName = options.sourceMapFile || options.sourceMap && options.output && (null != (cache$1 = options.output.match(/^.*[\\\/]([^\\\/]+)$/)) ? cache$1[1] : void 0);
|
|
98
218
|
return escodegen.generate(jsAst, {
|
|
99
219
|
comment: !options.compact,
|
|
100
220
|
sourceMapWithCode: true,
|
|
@@ -123,30 +243,47 @@ MambaScript = {
|
|
|
123
243
|
else
|
|
124
244
|
options.optimise = true;
|
|
125
245
|
msAst = MambaScript.parse(input, options);
|
|
126
|
-
jsAst = MambaScript.compile(msAst, {
|
|
127
|
-
|
|
246
|
+
jsAst = MambaScript.compile(msAst, {
|
|
247
|
+
bare: options.bare,
|
|
248
|
+
stdlibPath: options.stdlibPath,
|
|
249
|
+
modules: options.modules
|
|
250
|
+
});
|
|
251
|
+
return MambaScript.js(jsAst, { compact: options.compact });
|
|
128
252
|
},
|
|
129
253
|
smoothCompile: function (input, options) {
|
|
130
|
-
|
|
254
|
+
if (null == options)
|
|
255
|
+
options = {};
|
|
256
|
+
options.modules = 'esm';
|
|
257
|
+
options.bare = true;
|
|
258
|
+
return this.cs2js(input, options);
|
|
259
|
+
},
|
|
260
|
+
registerEsm: function () {
|
|
261
|
+
return require('../esm');
|
|
262
|
+
},
|
|
263
|
+
esCompile: function (mamba, options) {
|
|
264
|
+
if (null == options)
|
|
265
|
+
options = {};
|
|
266
|
+
return this.smoothCompile(mamba, options);
|
|
267
|
+
},
|
|
268
|
+
svelteCompile: function (input, options) {
|
|
131
269
|
if (null == options)
|
|
132
270
|
options = {};
|
|
133
271
|
if (null != options.optimise)
|
|
134
272
|
options.optimise;
|
|
135
273
|
else
|
|
136
274
|
options.optimise = true;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
var options;
|
|
143
|
-
options = {
|
|
144
|
-
optimize: true,
|
|
145
|
-
bare: true,
|
|
146
|
-
header: true,
|
|
147
|
-
compact: true
|
|
148
|
-
};
|
|
275
|
+
options.bare = true;
|
|
276
|
+
if (null != options.compact)
|
|
277
|
+
options.compact;
|
|
278
|
+
else
|
|
279
|
+
options.compact = true;
|
|
149
280
|
return this.cs2js(input, options);
|
|
150
281
|
}
|
|
151
282
|
};
|
|
152
283
|
module.exports = MambaScript;
|
|
284
|
+
function in$(member, list) {
|
|
285
|
+
for (var i = 0, length = list.length; i < length; ++i)
|
|
286
|
+
if (i in list && list[i] === member)
|
|
287
|
+
return true;
|
|
288
|
+
return false;
|
|
289
|
+
}
|
package/lib/mamba.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
// Generated by MambaScript 2.0.0
|
|
2
|
+
var argv, BANNER, compileFiles, compileSource, e, execute, fail, fpath, fs, MambaScript, moduleCode, OptionParser, outputPathFor, packageIsModule, parseFile, parser, path, positional, repl, reportCompileError, runMain, scaffold, SWITCHES, typeCheckMode, usesModules, writeOut;
|
|
3
|
+
fs = require('fs');
|
|
4
|
+
path = require('path');
|
|
5
|
+
OptionParser = require('./optparse').OptionParser;
|
|
6
|
+
MambaScript = require('./module');
|
|
7
|
+
repl = require('./repl');
|
|
8
|
+
runMain = require('./run').runMain;
|
|
9
|
+
SWITCHES = [
|
|
10
|
+
[
|
|
11
|
+
'-c',
|
|
12
|
+
'--compile',
|
|
13
|
+
'compile .mamba files to JavaScript'
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
'-z',
|
|
17
|
+
'--svelte',
|
|
18
|
+
'same as --esm (kept for 0.5 scripts)'
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
'--esm',
|
|
22
|
+
'compile to ES modules (import/export); the default when package.json says "type": "module"'
|
|
23
|
+
],
|
|
24
|
+
[
|
|
25
|
+
'--cjs',
|
|
26
|
+
'compile to CommonJS (require/module.exports) even in a "type": "module" package'
|
|
27
|
+
],
|
|
28
|
+
[
|
|
29
|
+
'-e',
|
|
30
|
+
'--execute [FILE]',
|
|
31
|
+
'run a .mamba file (default when a file is given)'
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
'-o',
|
|
35
|
+
'--out [DIR]',
|
|
36
|
+
'output directory for compiled files'
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
'-j',
|
|
40
|
+
'--join [FILE]',
|
|
41
|
+
'concatenate all compiled files into FILE'
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
'-b',
|
|
45
|
+
'--bare',
|
|
46
|
+
'omit the top-level function wrapper'
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
'-p',
|
|
50
|
+
'--print',
|
|
51
|
+
'print compiled JavaScript to stdout instead of writing files'
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
'-s',
|
|
55
|
+
'--strict',
|
|
56
|
+
'fail compilation on type errors'
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
'-d',
|
|
60
|
+
'--debug',
|
|
61
|
+
'print the parsed AST (with --csast) or compiled code on stderr'
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
'-v',
|
|
65
|
+
'--version',
|
|
66
|
+
'print the version'
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
'-h',
|
|
70
|
+
'--help',
|
|
71
|
+
'show this help'
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
'--typecheck',
|
|
75
|
+
'type check (default: on, warnings only; --no-typecheck to disable)'
|
|
76
|
+
],
|
|
77
|
+
[
|
|
78
|
+
'--parser [NAME]',
|
|
79
|
+
'parser to use: fast (hand-written) or peg (the previous PEG.js parser)'
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
'--csast',
|
|
83
|
+
'print the MambaScript AST and stop'
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
'--json',
|
|
87
|
+
'print compile errors as JSON (one array on stderr), for editors'
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
'--mscodegen',
|
|
91
|
+
'print the mscodegen rendering of the AST and stop'
|
|
92
|
+
]
|
|
93
|
+
];
|
|
94
|
+
BANNER = 'Usage: mamba [OPTIONS] [FILE ...] [-- ARGS]\n\n mamba app.mamba run app.mamba\n mamba -c src/*.mamba -o lib compile files into lib/\n mamba -c --esm src/*.mamba -o dist compile to ES modules\n mamba init my-app create a new project in ./my-app\n mamba start the REPL';
|
|
95
|
+
parser = new OptionParser(SWITCHES, BANNER);
|
|
96
|
+
fail = function (message) {
|
|
97
|
+
console.error(message);
|
|
98
|
+
return process.exit(1);
|
|
99
|
+
};
|
|
100
|
+
reportCompileError = function (fpath, e) {
|
|
101
|
+
var diagnostics;
|
|
102
|
+
if (argv.json) {
|
|
103
|
+
diagnostics = null != e.diagnostic ? [MambaScript.Diagnostics.toJSON(e.diagnostic)] : null != e.errors ? MambaScript.reporter.toJSON({ file: null != e.file ? e.file : fpath }) : [{
|
|
104
|
+
kind: null != e.name ? e.name : 'Error',
|
|
105
|
+
file: fpath,
|
|
106
|
+
headline: e.message
|
|
107
|
+
}];
|
|
108
|
+
console.error(JSON.stringify(diagnostics));
|
|
109
|
+
process.exit(1);
|
|
110
|
+
}
|
|
111
|
+
console.error('Error in ' + fpath);
|
|
112
|
+
return fail(argv.debug ? e.stack || e.message : e.message);
|
|
113
|
+
};
|
|
114
|
+
try {
|
|
115
|
+
argv = parser.parse(process.argv.slice(2));
|
|
116
|
+
if (argv.compile || argv.svelte || argv.esm || argv.print || argv.cjs)
|
|
117
|
+
argv = parser.parse(process.argv.slice(2), { interleaved: true });
|
|
118
|
+
} catch (e$) {
|
|
119
|
+
e = e$;
|
|
120
|
+
fail('' + e.message + '\n' + parser.help());
|
|
121
|
+
}
|
|
122
|
+
typeCheckMode = argv.typecheck === false ? 'off' : argv.strict ? 'error' : 'warn';
|
|
123
|
+
moduleCode = 'var _include_,_module_;_module_=function(e){return function(t,n,r){var i,s,o,u,a,f,l;r==null&&(r=o),o=(f=typeof window!=="undefined"&&window!==null?window:global)!=null?f:e,r==null&&(r=o),i=[],l=t.split(".");for(u=0,a=l.length;u<a;u++)s=l[u],r[s]==null&&(r[s]={}),r=r[s],i.push(r);return n.apply(r,i)}}(this),_include_=function(e,t){var n,r;for(n in t)r=t[n],e[n]=r;return t};';
|
|
124
|
+
usesModules = function () {
|
|
125
|
+
return null != global._root_ ? global._root_.hasModule : void 0;
|
|
126
|
+
};
|
|
127
|
+
parseFile = function (fpath, source, extra) {
|
|
128
|
+
var opts;
|
|
129
|
+
if (null == extra)
|
|
130
|
+
extra = {};
|
|
131
|
+
opts = {
|
|
132
|
+
optimise: null != extra.optimise ? extra.optimise : true,
|
|
133
|
+
raw: true,
|
|
134
|
+
inputSource: fpath,
|
|
135
|
+
filename: fpath,
|
|
136
|
+
typeCheck: typeCheckMode,
|
|
137
|
+
parser: argv.parser,
|
|
138
|
+
warn: function (msg) {
|
|
139
|
+
return console.error(msg);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
return MambaScript.parse(source, opts);
|
|
143
|
+
};
|
|
144
|
+
compileSource = function (fpath, source, param$) {
|
|
145
|
+
var bare, cache$, code, csAst, esm, jsAst, stdlibPath;
|
|
146
|
+
if (null == param$)
|
|
147
|
+
param$ = {};
|
|
148
|
+
{
|
|
149
|
+
cache$ = param$;
|
|
150
|
+
esm = cache$.esm;
|
|
151
|
+
bare = cache$.bare;
|
|
152
|
+
stdlibPath = cache$.stdlibPath;
|
|
153
|
+
}
|
|
154
|
+
csAst = parseFile(fpath, source, { optimise: false });
|
|
155
|
+
if (argv.csast) {
|
|
156
|
+
console.log(JSON.stringify(csAst.toBasicObject(), null, 2));
|
|
157
|
+
process.exit(0);
|
|
158
|
+
}
|
|
159
|
+
if (argv.mscodegen) {
|
|
160
|
+
console.log(MambaScript.compileTypedToCS(csAst));
|
|
161
|
+
process.exit(0);
|
|
162
|
+
}
|
|
163
|
+
jsAst = MambaScript.compile(csAst, {
|
|
164
|
+
bare: null != bare ? bare : !!argv.bare,
|
|
165
|
+
stdlibPath: stdlibPath,
|
|
166
|
+
modules: esm ? 'esm' : 'cjs'
|
|
167
|
+
});
|
|
168
|
+
code = MambaScript.js(jsAst);
|
|
169
|
+
if (usesModules())
|
|
170
|
+
code = '' + moduleCode + '\n' + code;
|
|
171
|
+
return {
|
|
172
|
+
code: code,
|
|
173
|
+
jsAst: jsAst
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
outputPathFor = function (fpath) {
|
|
177
|
+
var name;
|
|
178
|
+
name = path.basename(fpath).replace(/\.(mamba|kofu|coffee)$/, '') + '.js';
|
|
179
|
+
if (argv.out) {
|
|
180
|
+
return path.join(argv.out, name);
|
|
181
|
+
} else {
|
|
182
|
+
return path.join(path.dirname(fpath), name);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
writeOut = function (to, code) {
|
|
186
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
187
|
+
fs.writeFileSync(to, code);
|
|
188
|
+
return console.error('compile: -> ' + to);
|
|
189
|
+
};
|
|
190
|
+
compileFiles = function (files, esm) {
|
|
191
|
+
var cache$, code, fpath, o, outputs, source, to;
|
|
192
|
+
if (!files.length)
|
|
193
|
+
fail('mamba: no input files\n' + parser.help());
|
|
194
|
+
outputs = [];
|
|
195
|
+
for (var i$ = 0, length$ = files.length; i$ < length$; ++i$) {
|
|
196
|
+
fpath = files[i$];
|
|
197
|
+
source = fs.readFileSync(fpath).toString();
|
|
198
|
+
try {
|
|
199
|
+
cache$ = compileSource(fpath, source, {
|
|
200
|
+
esm: esm,
|
|
201
|
+
bare: true
|
|
202
|
+
});
|
|
203
|
+
code = cache$.code;
|
|
204
|
+
cache$;
|
|
205
|
+
} catch (e$1) {
|
|
206
|
+
e = e$1;
|
|
207
|
+
reportCompileError(fpath, e);
|
|
208
|
+
}
|
|
209
|
+
outputs.push({
|
|
210
|
+
from: fpath,
|
|
211
|
+
to: outputPathFor(fpath),
|
|
212
|
+
code: code
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (argv.print) {
|
|
216
|
+
return function (accum$) {
|
|
217
|
+
for (var i$1 = 0, length$1 = outputs.length; i$1 < length$1; ++i$1) {
|
|
218
|
+
o = outputs[i$1];
|
|
219
|
+
accum$.push(process.stdout.write(o.code + '\n'));
|
|
220
|
+
}
|
|
221
|
+
return accum$;
|
|
222
|
+
}.call(this, []);
|
|
223
|
+
} else if (argv.join) {
|
|
224
|
+
to = argv.out ? path.join(argv.out, argv.join) : argv.join;
|
|
225
|
+
return writeOut(to, function (accum$1) {
|
|
226
|
+
for (var i$2 = 0, length$2 = outputs.length; i$2 < length$2; ++i$2) {
|
|
227
|
+
o = outputs[i$2];
|
|
228
|
+
accum$1.push(o.code);
|
|
229
|
+
}
|
|
230
|
+
return accum$1;
|
|
231
|
+
}.call(this, []).join('\n'));
|
|
232
|
+
} else {
|
|
233
|
+
return function (accum$2) {
|
|
234
|
+
for (var i$3 = 0, length$3 = outputs.length; i$3 < length$3; ++i$3) {
|
|
235
|
+
o = outputs[i$3];
|
|
236
|
+
accum$2.push(writeOut(o.to, o.code));
|
|
237
|
+
}
|
|
238
|
+
return accum$2;
|
|
239
|
+
}.call(this, []);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
packageIsModule = function (fpath) {
|
|
243
|
+
var candidate, dir, parent;
|
|
244
|
+
if (!(null != fpath))
|
|
245
|
+
return false;
|
|
246
|
+
dir = path.dirname(path.resolve(fpath));
|
|
247
|
+
return function (accum$) {
|
|
248
|
+
while (true) {
|
|
249
|
+
candidate = path.join(dir, 'package.json');
|
|
250
|
+
if (fs.existsSync(candidate))
|
|
251
|
+
try {
|
|
252
|
+
return JSON.parse(fs.readFileSync(candidate, 'utf8')).type === 'module';
|
|
253
|
+
} catch (e$1) {
|
|
254
|
+
e = e$1;
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
parent = path.dirname(dir);
|
|
258
|
+
if (parent === dir)
|
|
259
|
+
return false;
|
|
260
|
+
accum$.push(dir = parent);
|
|
261
|
+
}
|
|
262
|
+
return accum$;
|
|
263
|
+
}.call(this, []);
|
|
264
|
+
};
|
|
265
|
+
execute = function (fpath, scriptArgs) {
|
|
266
|
+
var cache$, code, fileUrl, jsAst, source;
|
|
267
|
+
if (!fs.existsSync(fpath))
|
|
268
|
+
fail('mamba: file not found: ' + fpath);
|
|
269
|
+
if ((argv.esm || packageIsModule(fpath)) && !argv.cjs) {
|
|
270
|
+
MambaScript.registerEsm();
|
|
271
|
+
process.argv = [
|
|
272
|
+
process.argv[0],
|
|
273
|
+
path.resolve(fpath)
|
|
274
|
+
].concat(scriptArgs);
|
|
275
|
+
fileUrl = require('url').pathToFileURL(path.resolve(fpath)).href;
|
|
276
|
+
eval('import(fileUrl)')['catch'](function (e) {
|
|
277
|
+
console.error(null != (null != e ? e.stack : void 0) ? null != e ? e.stack : void 0 : e);
|
|
278
|
+
return process.exit(1);
|
|
279
|
+
});
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
source = fs.readFileSync(fpath).toString();
|
|
283
|
+
MambaScript.register();
|
|
284
|
+
try {
|
|
285
|
+
cache$ = compileSource(fpath, source, {
|
|
286
|
+
bare: true,
|
|
287
|
+
stdlibPath: MambaScript.stdlibPath
|
|
288
|
+
});
|
|
289
|
+
code = cache$.code;
|
|
290
|
+
jsAst = cache$.jsAst;
|
|
291
|
+
cache$;
|
|
292
|
+
} catch (e$1) {
|
|
293
|
+
e = e$1;
|
|
294
|
+
reportCompileError(fpath, e);
|
|
295
|
+
}
|
|
296
|
+
if (argv.debug)
|
|
297
|
+
console.error(code);
|
|
298
|
+
process.argv = [
|
|
299
|
+
process.argv[0],
|
|
300
|
+
path.resolve(fpath)
|
|
301
|
+
].concat(scriptArgs);
|
|
302
|
+
return runMain(source, code, jsAst, path.resolve(fpath));
|
|
303
|
+
};
|
|
304
|
+
scaffold = function (dir) {
|
|
305
|
+
var content, files, full, name, rel, target;
|
|
306
|
+
if (!dir)
|
|
307
|
+
fail('mamba init: please name the project directory (mamba init my-app)');
|
|
308
|
+
target = path.resolve(dir);
|
|
309
|
+
if (fs.existsSync(target) && fs.readdirSync(target).length)
|
|
310
|
+
fail('mamba init: ' + dir + ' exists and is not empty');
|
|
311
|
+
name = path.basename(target).toLowerCase().replace(/[^a-z0-9-_]/g, '-');
|
|
312
|
+
files = {
|
|
313
|
+
'package.json': JSON.stringify({
|
|
314
|
+
name: name,
|
|
315
|
+
version: '0.1.0',
|
|
316
|
+
description: 'A MambaScript project',
|
|
317
|
+
main: 'src/index.mamba',
|
|
318
|
+
scripts: {
|
|
319
|
+
start: 'mamba src/index.mamba',
|
|
320
|
+
build: 'mamba -c src/*.mamba -o lib',
|
|
321
|
+
test: 'jest'
|
|
322
|
+
},
|
|
323
|
+
dependencies: { mambascript: '^' + MambaScript.VERSION },
|
|
324
|
+
devDependencies: { jest: '^29.7.0' },
|
|
325
|
+
jest: {
|
|
326
|
+
testEnvironment: 'node',
|
|
327
|
+
moduleFileExtensions: [
|
|
328
|
+
'mamba',
|
|
329
|
+
'js'
|
|
330
|
+
],
|
|
331
|
+
transform: { '\\.mamba$': 'mambascript/jest' },
|
|
332
|
+
testMatch: ['<rootDir>/tdd/**/*.spec.mamba']
|
|
333
|
+
}
|
|
334
|
+
}, null, 2) + '\n',
|
|
335
|
+
'src/index.mamba': '# ' + name + " - written in MambaScript\ngrab {greet} from './greeter'\n\nblueprint App\n name :: String\n constructor: (name :: String) ->\n @name = name\n\n start: ->\n message = waitForIt greet @name\n present message\n message\n\napp = make App 'world'\napp.start() if require.main is module\n\nshare {App}",
|
|
336
|
+
'src/greeter.mamba': '# Functions that use waitForIt become async automatically.\ngreet = (name :: String) ->\n waitForIt new Promise (resolve) -> setTimeout resolve, 10\n "Hello, #{name}!"\n\nshare {greet}',
|
|
337
|
+
'tdd/index.spec.mamba': "{App} = require '../src/index'\n\ndescribe 'App', ->\n it 'greets by name', ->\n app = make App 'mamba'\n expect(waitForIt app.start()).toBe 'Hello, mamba!'\n return",
|
|
338
|
+
'.gitignore': 'node_modules/\nlib/\n',
|
|
339
|
+
'README.md': '# ' + name + '\n\nA [MambaScript](https://github.com/arthurbernierjr/mambascript) project.\n\n npm install\n npm start # run src/index.mamba\n npm test # jest, with .mamba specs under tdd/\n npm run build # compile src/ into lib/'
|
|
340
|
+
};
|
|
341
|
+
for (rel in files) {
|
|
342
|
+
if (!isOwn$(files, rel))
|
|
343
|
+
continue;
|
|
344
|
+
content = files[rel];
|
|
345
|
+
full = path.join(target, rel);
|
|
346
|
+
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
347
|
+
fs.writeFileSync(full, content);
|
|
348
|
+
console.log(' create ' + path.join(dir, rel));
|
|
349
|
+
}
|
|
350
|
+
return console.log('\nDone. Next:\n cd ' + dir + '\n npm install\n npm start');
|
|
351
|
+
};
|
|
352
|
+
positional = null != argv['arguments'] ? argv['arguments'] : [];
|
|
353
|
+
if (argv.help) {
|
|
354
|
+
console.log(parser.help());
|
|
355
|
+
} else if (argv.version) {
|
|
356
|
+
console.log('MambaScript ' + MambaScript.VERSION);
|
|
357
|
+
} else if (positional[0] === 'init') {
|
|
358
|
+
scaffold(positional[1]);
|
|
359
|
+
} else if (argv.svelte || argv.esm) {
|
|
360
|
+
compileFiles(positional, true);
|
|
361
|
+
} else if (argv.compile || argv.print || argv.cjs) {
|
|
362
|
+
compileFiles(positional, !argv.cjs && packageIsModule(positional[0]));
|
|
363
|
+
} else if (null != argv.execute || positional.length) {
|
|
364
|
+
fpath = typeof argv.execute === 'string' ? argv.execute : positional.shift();
|
|
365
|
+
execute(fpath, positional);
|
|
366
|
+
} else {
|
|
367
|
+
MambaScript.register();
|
|
368
|
+
repl.start();
|
|
369
|
+
}
|
|
370
|
+
function isOwn$(o, p) {
|
|
371
|
+
return {}.hasOwnProperty.call(o, p);
|
|
372
|
+
}
|