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.
Files changed (82) hide show
  1. package/CHANGELOG.md +326 -0
  2. package/README.md +509 -267
  3. package/bin/mamba +1 -258
  4. package/decision.js +1 -246
  5. package/docs/coverage.md +70 -0
  6. package/docs/features.md +3348 -0
  7. package/docs/perf.md +94 -0
  8. package/docs/reference.md +1295 -0
  9. package/docs/vs-coffeescript.md +1593 -0
  10. package/docs/vs-iced-coffeescript.md +626 -0
  11. package/docs/vs-typescript.md +107 -0
  12. package/esm.js +8 -0
  13. package/jest.js +33 -0
  14. package/lib/black.js +17 -13
  15. package/lib/browser.js +2 -4
  16. package/lib/cli.js +207 -136
  17. package/lib/compiler.js +1162 -93
  18. package/lib/decision.js +10 -10
  19. package/lib/diagnostics.js +219 -0
  20. package/lib/esm-hooks.js +60 -0
  21. package/lib/fast-parser.js +3469 -0
  22. package/lib/functional-helpers.js +26 -9
  23. package/lib/helpers.js +67 -50
  24. package/lib/js-nodes.js +151 -12
  25. package/lib/lexer.js +1499 -0
  26. package/lib/light-module.js +200 -63
  27. package/lib/mamba.js +372 -0
  28. package/lib/module.js +23 -172
  29. package/lib/nodes.js +84 -21
  30. package/lib/optimiser.js +29 -9
  31. package/lib/optparse.js +32 -15
  32. package/lib/parser.js +9433 -4840
  33. package/lib/preprocessor.js +3 -5
  34. package/lib/register.js +49 -56
  35. package/lib/repl.js +28 -9
  36. package/lib/reporter.js +106 -69
  37. package/lib/run.js +3 -5
  38. package/lib/string-scanner.js +94 -0
  39. package/lib/syntax-tables.js +480 -0
  40. package/lib/type-checker.js +258 -44
  41. package/lib/type-helpers.js +28 -10
  42. package/lib/type-resolver.js +256 -34
  43. package/lib/type-scope.js +10 -6
  44. package/lib/type-walker.js +664 -85
  45. package/lib/types.js +17 -7
  46. package/lib/underscore.js +170 -76
  47. package/lib/unicode-classes.js +122 -0
  48. package/mambaview.js +1 -0
  49. package/package.json +49 -46
  50. package/stdlib.js +1 -0
  51. package/vendor/README.md +40 -0
  52. package/vendor/escodegen/LICENSE.BSD +19 -0
  53. package/vendor/escodegen/escodegen.js +2289 -0
  54. package/vendor/estraverse/LICENSE.BSD +19 -0
  55. package/vendor/estraverse/estraverse.js +706 -0
  56. package/vendor/mambacodegen/LICENSE +24 -0
  57. package/vendor/mambacodegen/lib/mscodegen.js +620 -0
  58. package/vendor/patches/escodegen.diff +130 -0
  59. package/vendor/patches/estraverse.diff +51 -0
  60. package/vendor/patches/pegjs.diff +70 -0
  61. package/vendor/patches/source-map.diff +160 -0
  62. package/vendor/source-map/LICENSE +28 -0
  63. package/vendor/source-map/lib/source-map/array-set.js +92 -0
  64. package/vendor/source-map/lib/source-map/base64-vlq.js +140 -0
  65. package/vendor/source-map/lib/source-map/base64.js +38 -0
  66. package/vendor/source-map/lib/source-map/binary-search.js +77 -0
  67. package/vendor/source-map/lib/source-map/source-map-consumer.js +404 -0
  68. package/vendor/source-map/lib/source-map/source-map-generator.js +229 -0
  69. package/vendor/source-map/lib/source-map/source-node.js +201 -0
  70. package/vendor/source-map/lib/source-map/util.js +57 -0
  71. package/vendor/source-map/lib/source-map.js +8 -0
  72. package/bin/fix +0 -17
  73. package/bin/updateImports +0 -46
  74. package/build.js +0 -25
  75. package/lib/module-esm.js +0 -901
  76. package/lib/pegjs-kofu-plugin.js +0 -35
  77. package/lib/pegjs-mamba-plugin.js +0 -35
  78. package/lib/processor-esm.js +0 -507
  79. package/mamba +0 -259
  80. package/msast.js +0 -1
  81. package/utils/gulp-mamba.js +0 -29
  82. /package/{lib/esm.js → vendor/patches/mambacodegen.diff} +0 -0
package/lib/lexer.js ADDED
@@ -0,0 +1,1499 @@
1
+ // Generated by MambaScript 2.0.0
2
+ var alternatives, cache$, ci, CLOSER_OF, COMPOUND_ASSIGN, CTX_CURLY, CTX_INDENT, CTX_INTERP, CTX_PAREN, CTX_SQUARE, DEDENT, DIGIT, dumpTokens, DUNDER, EOF, EXISTS_ASSIGN, FIRST_WORD, FLAG_DOUBLE_QUOTE, FLAG_HEREDOC, FLAG_NEWLINE_BEFORE, FLAG_NL_BEFORE_INDENT, FLAG_SPACE_BEFORE, growInt32, growUint8, HEREGEX_CLASS, HEREGEX_CLASS_CLOSE, HEREGEX_CLASS_OPEN, HEREGEX_END, HEREGEX_ESCAPE, HEREGEX_PIECE, HEREGEX_START, HEREGEX_TEXT, HEREGEX_WS, HEX, IDENT, IDENT_PART, IDENT_PART_RE_SRC, IDENT_RE, IDENT_START, IDENT_START_RE_SRC, INDENT, INTERP_END, INTERP_START, isBinaryDigit, isDecimalDigit, isHexDigit, isOctalDigit, isWs, JSLITERAL, KEYWORD_KIND, KIND_NAMES, KW, kwKind, kwWord, LAST_WORD, Lexer, MACRO, MACRO_NAMES, macroName, MACROS, MambaSyntaxError, NEWLINE, NUMBER_BIGINT, NUMBER_FLOAT, NUMBER_INT, OPENER_OF, REGEX, STRING_END, STRING_PIECE, STRING_START, T, tables, tokenize, UC, VALUE_KIND, vk;
3
+ tables = require('./syntax-tables');
4
+ UC = require('./unicode-classes');
5
+ cache$ = tables;
6
+ T = cache$.T;
7
+ KIND_NAMES = cache$.KIND_NAMES;
8
+ KEYWORD_KIND = cache$.KEYWORD_KIND;
9
+ MACRO_NAMES = cache$.MACRO_NAMES;
10
+ FLAG_SPACE_BEFORE = tables.FLAG_SPACE_BEFORE;
11
+ FLAG_NL_BEFORE_INDENT = tables.FLAG_NL_BEFORE_INDENT;
12
+ FLAG_HEREDOC = tables.FLAG_HEREDOC;
13
+ FLAG_DOUBLE_QUOTE = tables.FLAG_DOUBLE_QUOTE;
14
+ FLAG_NEWLINE_BEFORE = tables.FLAG_NEWLINE_BEFORE;
15
+ HEREGEX_WS = tables.HEREGEX_WS;
16
+ HEREGEX_TEXT = tables.HEREGEX_TEXT;
17
+ HEREGEX_CLASS = tables.HEREGEX_CLASS;
18
+ HEREGEX_ESCAPE = tables.HEREGEX_ESCAPE;
19
+ HEREGEX_CLASS_OPEN = tables.HEREGEX_CLASS_OPEN;
20
+ HEREGEX_CLASS_CLOSE = tables.HEREGEX_CLASS_CLOSE;
21
+ EOF = T.EOF;
22
+ NEWLINE = T.NEWLINE;
23
+ INDENT = T.INDENT;
24
+ DEDENT = T.DEDENT;
25
+ IDENT = T.IDENT;
26
+ NUMBER_INT = T.NUMBER_INT;
27
+ NUMBER_FLOAT = T.NUMBER_FLOAT;
28
+ NUMBER_BIGINT = T.NUMBER_BIGINT;
29
+ STRING_START = T.STRING_START;
30
+ STRING_PIECE = T.STRING_PIECE;
31
+ STRING_END = T.STRING_END;
32
+ INTERP_START = T.INTERP_START;
33
+ INTERP_END = T.INTERP_END;
34
+ REGEX = T.REGEX;
35
+ HEREGEX_START = T.HEREGEX_START;
36
+ HEREGEX_PIECE = T.HEREGEX_PIECE;
37
+ HEREGEX_END = T.HEREGEX_END;
38
+ JSLITERAL = T.JSLITERAL;
39
+ MACRO = T.MACRO;
40
+ COMPOUND_ASSIGN = T.COMPOUND_ASSIGN;
41
+ EXISTS_ASSIGN = T.EXISTS_ASSIGN;
42
+ FIRST_WORD = T.AND;
43
+ LAST_WORD = T.RESERVED;
44
+ CTX_PAREN = 1;
45
+ CTX_SQUARE = 2;
46
+ CTX_CURLY = 3;
47
+ CTX_INTERP = 4;
48
+ CTX_INDENT = 5;
49
+ MambaSyntaxError = function (message, line, column, offset, found, extra) {
50
+ var err;
51
+ err = new Error(message);
52
+ err.name = 'SyntaxError';
53
+ err.line = line;
54
+ err.column = column;
55
+ err.offset = offset;
56
+ err.found = null != found ? found : null;
57
+ err.expected = [];
58
+ if (null != extra) {
59
+ if (null != extra.code)
60
+ err.code = extra.code;
61
+ if (null != extra.why)
62
+ err.why = extra.why;
63
+ if (null != extra.fix)
64
+ err.fix = extra.fix;
65
+ if (null != extra.endColumn)
66
+ err.endColumn = extra.endColumn;
67
+ if (null != extra.related)
68
+ err.related = extra.related;
69
+ }
70
+ Object.setPrototypeOf(err, MambaSyntaxError.prototype);
71
+ return err;
72
+ };
73
+ CLOSER_OF = {
74
+ '(': ')',
75
+ '[': ']',
76
+ '{': '}',
77
+ '#{': '}'
78
+ };
79
+ OPENER_OF = {
80
+ ')': '(',
81
+ ']': '[',
82
+ '}': '{'
83
+ };
84
+ MambaSyntaxError.prototype = Object.create(Error.prototype, {
85
+ constructor: {
86
+ value: MambaSyntaxError,
87
+ writable: true,
88
+ configurable: true
89
+ },
90
+ name: {
91
+ value: 'SyntaxError',
92
+ writable: true,
93
+ configurable: true
94
+ }
95
+ });
96
+ IDENT_START = new Uint8Array(128);
97
+ IDENT_PART = new Uint8Array(128);
98
+ DIGIT = new Uint8Array(128);
99
+ HEX = new Uint8Array(128);
100
+ ci = 0;
101
+ while (ci < 128) {
102
+ if (65 <= ci && ci <= 90 || 97 <= ci && ci <= 122 || ci === 36 || ci === 95) {
103
+ IDENT_START[ci] = 1;
104
+ IDENT_PART[ci] = 1;
105
+ }
106
+ if (48 <= ci && ci <= 57) {
107
+ IDENT_PART[ci] = 1;
108
+ DIGIT[ci] = 1;
109
+ HEX[ci] = 1;
110
+ }
111
+ if (65 <= ci && ci <= 70 || 97 <= ci && ci <= 102)
112
+ HEX[ci] = 1;
113
+ ci++;
114
+ }
115
+ isWs = function (c) {
116
+ return c === 32 || c === 9 || c === 11 || c === 12 || c === 13 || c === 160 || c === 65279 || c === 5760 || c === 6158 || 8192 <= c && c <= 8202 || c === 8239 || c === 8287 || c === 12288;
117
+ };
118
+ VALUE_KIND = new Uint8Array(KIND_NAMES.length);
119
+ for (var cache$1 = [
120
+ T.IDENT,
121
+ T.NUMBER_INT,
122
+ T.NUMBER_FLOAT,
123
+ T.NUMBER_BIGINT,
124
+ T.STRING_END,
125
+ T.REGEX,
126
+ T.HEREGEX_END,
127
+ T.JSLITERAL,
128
+ T.MACRO,
129
+ T.RPAREN,
130
+ T.RBRACKET,
131
+ T.RBRACE,
132
+ T.THIS,
133
+ T.AT,
134
+ T.NULL,
135
+ T.UNDEFINED,
136
+ T.TRUE,
137
+ T.FALSE,
138
+ T.RESERVED
139
+ ], i$ = 0, length$ = cache$1.length; i$ < length$; ++i$) {
140
+ vk = cache$1[i$];
141
+ VALUE_KIND[vk] = 1;
142
+ }
143
+ KW = Object.create(null);
144
+ for (kwWord in KEYWORD_KIND) {
145
+ if (!isOwn$(KEYWORD_KIND, kwWord))
146
+ continue;
147
+ kwKind = KEYWORD_KIND[kwWord];
148
+ KW[kwWord] = kwKind;
149
+ }
150
+ DUNDER = /^__[A-Z][A-Z0-9_]*__$/;
151
+ isDecimalDigit = function (ch) {
152
+ return ch < 128 && DIGIT[ch] === 1;
153
+ };
154
+ isHexDigit = function (ch) {
155
+ return ch < 128 && HEX[ch] === 1;
156
+ };
157
+ isOctalDigit = function (ch) {
158
+ return 48 <= ch && ch <= 55;
159
+ };
160
+ isBinaryDigit = function (ch) {
161
+ return ch === 48 || ch === 49;
162
+ };
163
+ MACROS = Object.create(null);
164
+ for (var i$1 = 0, length$1 = MACRO_NAMES.length; i$1 < length$1; ++i$1) {
165
+ macroName = MACRO_NAMES[i$1];
166
+ MACROS[macroName] = 1;
167
+ }
168
+ alternatives = function (cls, extra) {
169
+ var out, pair;
170
+ out = '[' + cls.body + extra + ']';
171
+ for (var i$2 = 0, length$2 = cls.pairs.length; i$2 < length$2; ++i$2) {
172
+ pair = cls.pairs[i$2];
173
+ out += '|' + pair[0] + '[' + pair[1] + ']';
174
+ }
175
+ return out;
176
+ };
177
+ IDENT_START_RE_SRC = '(?:' + alternatives(UC.UnicodeLetter, '$_') + '|\\\\u[0-9a-fA-F]{4})';
178
+ IDENT_PART_RE_SRC = '(?:' + alternatives(UC.UnicodeLetter, '$_') + '|' + alternatives(UC.UnicodeCombiningMark, '') + '|' + alternatives(UC.UnicodeDigit, '') + '|' + alternatives(UC.UnicodeConnectorPunctuation, '') + '|\\u200C|\\u200D|\\\\u[0-9a-fA-F]{4})';
179
+ IDENT_RE = new RegExp('' + IDENT_START_RE_SRC + IDENT_PART_RE_SRC + '*', 'y');
180
+ growInt32 = function (old, cap) {
181
+ var a;
182
+ a = new Int32Array(cap);
183
+ a.set(old);
184
+ return a;
185
+ };
186
+ growUint8 = function (old, cap) {
187
+ var a;
188
+ a = new Uint8Array(cap);
189
+ a.set(old);
190
+ return a;
191
+ };
192
+ Lexer = function () {
193
+ function Lexer(src, options) {
194
+ var cap;
195
+ this.src = src;
196
+ this.options = options || {};
197
+ this.len = src.length;
198
+ cap = (this.len >> 1) + 64;
199
+ this.cap = cap;
200
+ this.kinds = new Int32Array(cap);
201
+ this.starts = new Int32Array(cap);
202
+ this.ends = new Int32Array(cap);
203
+ this.lines = new Int32Array(cap);
204
+ this.cols = new Int32Array(cap);
205
+ this.flags = new Uint8Array(cap);
206
+ this.pair = new Int32Array(cap);
207
+ this.vals = [];
208
+ this.n = 0;
209
+ this.pos = 0;
210
+ this.line = 1;
211
+ this.lineStart = 0;
212
+ this.pending = 0;
213
+ this.prevKind = -1;
214
+ this.prevValue = false;
215
+ this.ctx = [];
216
+ this.ctxTok = [];
217
+ this.indentStart = [];
218
+ this.indentEnd = [];
219
+ this.baseStart = -1;
220
+ this.baseEnd = -1;
221
+ }
222
+ Lexer.prototype.positionOf = function (offset) {
223
+ var i, line, ls, src;
224
+ src = this.src;
225
+ line = 1;
226
+ ls = 0;
227
+ i = 0;
228
+ while (i < offset) {
229
+ if (src.charCodeAt(i) === 10) {
230
+ line++;
231
+ ls = i + 1;
232
+ }
233
+ i++;
234
+ }
235
+ return {
236
+ line: line,
237
+ column: offset - ls + 1
238
+ };
239
+ };
240
+ Lexer.prototype.fail = function (message, offset, found, extra) {
241
+ var len, o, p;
242
+ if (null == extra)
243
+ extra = {};
244
+ p = this.positionOf(offset);
245
+ if (!(null != extra.endColumn)) {
246
+ len = null != found && found.indexOf('\n') < 0 ? found.length : 1;
247
+ extra.endColumn = p.column + Math.max(1, len);
248
+ }
249
+ if (null != extra.opener) {
250
+ o = this.positionOf(extra.opener);
251
+ extra.related = [{
252
+ line: o.line,
253
+ column: o.column,
254
+ endColumn: o.column + (null != extra.openerLength ? extra.openerLength : 1),
255
+ label: null != extra.openerLabel ? extra.openerLabel : 'opened here'
256
+ }];
257
+ delete extra.opener;
258
+ delete extra.openerLabel;
259
+ delete extra.openerLength;
260
+ }
261
+ throw new MambaSyntaxError(message, p.line, p.column, offset, found, extra);
262
+ };
263
+ Lexer.prototype.grow = function () {
264
+ var cap;
265
+ cap = this.cap * 2;
266
+ this.kinds = growInt32(this.kinds, cap);
267
+ this.starts = growInt32(this.starts, cap);
268
+ this.ends = growInt32(this.ends, cap);
269
+ this.lines = growInt32(this.lines, cap);
270
+ this.cols = growInt32(this.cols, cap);
271
+ this.flags = growUint8(this.flags, cap);
272
+ this.pair = growInt32(this.pair, cap);
273
+ return this.cap = cap;
274
+ };
275
+ Lexer.prototype.push = function (kind, start, end, line, col) {
276
+ var n;
277
+ n = this.n;
278
+ if (n === this.cap)
279
+ this.grow();
280
+ this.kinds[n] = kind;
281
+ this.starts[n] = start;
282
+ this.ends[n] = end;
283
+ this.lines[n] = line;
284
+ this.cols[n] = col;
285
+ if (kind === NEWLINE || kind === INDENT || kind === DEDENT) {
286
+ this.flags[n] = 0;
287
+ this.prevValue = false;
288
+ } else {
289
+ this.flags[n] = this.pending;
290
+ this.pending = 0;
291
+ if (VALUE_KIND[kind] === 1) {
292
+ this.prevValue = true;
293
+ } else if (kind >= FIRST_WORD && kind <= LAST_WORD && (this.prevKind === T.DOT || this.prevKind === T.DCOLON || this.prevKind === T.AT)) {
294
+ this.prevValue = true;
295
+ } else {
296
+ this.prevValue = false;
297
+ }
298
+ }
299
+ this.prevKind = kind;
300
+ this.n = n + 1;
301
+ return n;
302
+ };
303
+ Lexer.prototype.pushHere = function (kind, start, end) {
304
+ return this.push(kind, start, end, this.line, start - this.lineStart + 1);
305
+ };
306
+ Lexer.prototype.tokenize = function () {
307
+ this.lineStartIndent();
308
+ this.lexBody(false);
309
+ this.finish();
310
+ return this;
311
+ };
312
+ Lexer.prototype.finish = function () {
313
+ var col, opener, pos, text, top;
314
+ pos = this.pos;
315
+ col = pos - this.lineStart + 1;
316
+ if (this.n > 0 && this.kinds[this.n - 1] !== NEWLINE)
317
+ this.push(NEWLINE, pos, pos, this.line, col);
318
+ while (this.ctx.length > 0) {
319
+ top = this.ctx.pop();
320
+ opener = this.ctxTok.pop();
321
+ switch (top) {
322
+ case CTX_INDENT:
323
+ this.indentStart.pop();
324
+ this.indentEnd.pop();
325
+ this.push(DEDENT, pos, pos, this.line, col);
326
+ this.push(NEWLINE, pos, pos, this.line, col);
327
+ break;
328
+ case CTX_INTERP:
329
+ this.fail('unclosed interpolation: the #{ on line ' + this.lines[opener] + ' is never closed', this.starts[opener], null, {
330
+ code: 'MS1011',
331
+ why: 'Every #{ inside a string must be closed with } before the string ends.',
332
+ fix: 'add the missing }, or write \\#{ if the text was not meant to be interpolated.'
333
+ });
334
+ break;
335
+ default: {
336
+ text = this.src.slice(this.starts[opener], this.ends[opener]);
337
+ this.fail('unclosed ' + text + ': the ' + text + ' on line ' + this.lines[opener] + ', column ' + this.cols[opener] + ' is never closed', this.starts[opener], text, {
338
+ code: text === '(' ? 'MS1108' : text === '[' ? 'MS1109' : 'MS1110',
339
+ why: 'Brackets must be balanced; this ' + text + ' is still open when its block or the file ends.',
340
+ fix: 'add the matching ' + CLOSER_OF[text] + ' where the expression ends, or remove this ' + text + '.'
341
+ });
342
+ }
343
+ }
344
+ }
345
+ return this.push(EOF, pos, pos, this.line, col);
346
+ };
347
+ Lexer.prototype.lexBody = function (inInterp) {
348
+ var c, len, opener, src;
349
+ src = this.src;
350
+ len = this.len;
351
+ while (this.pos < len) {
352
+ c = src.charCodeAt(this.pos);
353
+ switch (c) {
354
+ case 10:
355
+ this.newline();
356
+ break;
357
+ case 32:
358
+ case 9:
359
+ case 13:
360
+ case 11:
361
+ case 12:
362
+ case 160:
363
+ this.pos++;
364
+ this.pending |= FLAG_SPACE_BEFORE;
365
+ break;
366
+ case 35:
367
+ this.comment();
368
+ break;
369
+ case 92:
370
+ this.backslash();
371
+ break;
372
+ case 34:
373
+ case 39:
374
+ this.lexString(c);
375
+ break;
376
+ case 96:
377
+ this.lexJsLiteral();
378
+ break;
379
+ case 47:
380
+ this.lexSlash();
381
+ break;
382
+ case 40:
383
+ this.opener(T.LPAREN, CTX_PAREN);
384
+ break;
385
+ case 91:
386
+ this.opener(T.LBRACKET, CTX_SQUARE);
387
+ break;
388
+ case 123:
389
+ this.opener(T.LBRACE, CTX_CURLY);
390
+ break;
391
+ case 41:
392
+ this.closer(T.RPAREN, CTX_PAREN, ')');
393
+ break;
394
+ case 93:
395
+ this.closer(T.RBRACKET, CTX_SQUARE, ']');
396
+ break;
397
+ case 125:
398
+ this.closeIndents();
399
+ if (inInterp && this.ctx.length > 0 && this.ctx[this.ctx.length - 1] === CTX_INTERP) {
400
+ this.ctx.pop();
401
+ this.ctxTok.pop();
402
+ this.pushHere(INTERP_END, this.pos, this.pos + 1);
403
+ this.pos++;
404
+ return;
405
+ }
406
+ this.closer(T.RBRACE, CTX_CURLY, '}');
407
+ break;
408
+ default:
409
+ if (c < 128) {
410
+ if (IDENT_START[c] === 1) {
411
+ this.lexWord();
412
+ } else if (DIGIT[c] === 1) {
413
+ this.lexNumber();
414
+ } else {
415
+ this.punct(c);
416
+ }
417
+ } else if (isWs(c)) {
418
+ this.pos++;
419
+ this.pending |= FLAG_SPACE_BEFORE;
420
+ } else {
421
+ this.lexWord();
422
+ }
423
+ }
424
+ }
425
+ if (inInterp) {
426
+ opener = this.ctxTok[this.ctxTok.length - 1];
427
+ this.fail('unclosed interpolation: the #{ on line ' + this.lines[opener] + ' is never closed', this.starts[opener], null, {
428
+ code: 'MS1011',
429
+ why: 'Every #{ inside a string must be closed with } before the string ends.',
430
+ fix: 'add the missing }, or write \\#{ if the text was not meant to be interpolated.'
431
+ });
432
+ }
433
+ };
434
+ Lexer.prototype.newline = function () {
435
+ if (this.n > 0 && this.kinds[this.n - 1] !== NEWLINE)
436
+ this.push(NEWLINE, this.pos, this.pos + 1, this.line, this.pos - this.lineStart + 1);
437
+ this.pos++;
438
+ this.line++;
439
+ this.lineStart = this.pos;
440
+ this.pending = FLAG_NEWLINE_BEFORE;
441
+ return this.lineStartIndent();
442
+ };
443
+ Lexer.prototype.matchesAt = function (from, to, at) {
444
+ var i, j, src;
445
+ src = this.src;
446
+ if (at + (to - from) > this.len)
447
+ return false;
448
+ i = from;
449
+ j = at;
450
+ while (i < to) {
451
+ if (src.charCodeAt(i) !== src.charCodeAt(j))
452
+ return false;
453
+ i++;
454
+ j++;
455
+ }
456
+ return true;
457
+ };
458
+ Lexer.prototype.lineStartIndent = function () {
459
+ var c, col, depth, first, iend, istart, j, len, level, p, q, src, total;
460
+ src = this.src;
461
+ len = this.len;
462
+ while (true) {
463
+ p = this.pos;
464
+ while (p < len && isWs(src.charCodeAt(p))) {
465
+ p++;
466
+ }
467
+ if (p >= len) {
468
+ this.pos = p;
469
+ return;
470
+ }
471
+ c = src.charCodeAt(p);
472
+ if (c === 10) {
473
+ this.pos = p + 1;
474
+ this.line++;
475
+ this.lineStart = this.pos;
476
+ continue;
477
+ }
478
+ if (c === 35 && !(src.charCodeAt(p + 1) === 35 && src.charCodeAt(p + 2) === 35)) {
479
+ q = src.indexOf('\n', p);
480
+ if (q === -1)
481
+ break;
482
+ this.pos = q + 1;
483
+ this.line++;
484
+ this.lineStart = this.pos;
485
+ continue;
486
+ }
487
+ break;
488
+ }
489
+ if (this.baseStart === -1) {
490
+ this.baseStart = this.pos;
491
+ p = this.pos;
492
+ while (p < len && isWs(src.charCodeAt(p))) {
493
+ p++;
494
+ }
495
+ this.baseEnd = p;
496
+ this.pos = p;
497
+ } else {
498
+ if (!this.matchesAt(this.baseStart, this.baseEnd, this.pos))
499
+ this.fail('this line does not start at the indentation of the first line of the file', this.pos, null, {
500
+ code: 'MS1020',
501
+ why: 'Indentation is how MambaScript groups code, so every top-level line must start where the first line of the file starts.',
502
+ fix: 'remove the leading spaces on this line, or indent the whole file the same way.'
503
+ });
504
+ this.pos += this.baseEnd - this.baseStart;
505
+ }
506
+ level = 0;
507
+ depth = this.indentStart.length;
508
+ while (level < depth) {
509
+ istart = this.indentStart[level];
510
+ iend = this.indentEnd[level];
511
+ if (this.matchesAt(istart, iend, this.pos)) {
512
+ this.pos += iend - istart;
513
+ level++;
514
+ } else if (this.pos >= len || !isWs(src.charCodeAt(this.pos))) {
515
+ col = this.pos - this.lineStart + 1;
516
+ while (this.indentStart.length > level) {
517
+ this.indentStart.pop();
518
+ this.indentEnd.pop();
519
+ if (!(this.ctx.length > 0 && this.ctx[this.ctx.length - 1] === CTX_INDENT))
520
+ this.fail('this line ends a block while a bracket opened inside that block is still open', this.pos, null, {
521
+ code: 'MS1022',
522
+ why: 'A line indented less than its block ends the block, but a bracket opened inside it has not been closed, so the two structures overlap.',
523
+ fix: 'close the bracket before this line, or keep this line indented inside the block.'
524
+ });
525
+ this.ctx.pop();
526
+ this.ctxTok.pop();
527
+ this.push(DEDENT, this.pos, this.pos, this.line, col);
528
+ this.push(NEWLINE, this.pos, this.pos, this.line, col);
529
+ }
530
+ break;
531
+ } else {
532
+ total = 0;
533
+ j = 0;
534
+ while (j < depth) {
535
+ total += this.indentEnd[j] - this.indentStart[j];
536
+ j++;
537
+ }
538
+ first = this.pos;
539
+ while (first < len && isWs(src.charCodeAt(first))) {
540
+ first++;
541
+ }
542
+ col = first - this.lineStart + 1;
543
+ throw new MambaSyntaxError('this line is indented more than the block it ends and less than the block above it', this.line, col, first, null, {
544
+ code: 'MS1021',
545
+ endColumn: col + 1,
546
+ why: 'A line that is indented less than the previous line must line up with one of the open blocks; this one lines up with none of them.',
547
+ fix: 'indent it to match one of the surrounding lines, using the same number of spaces throughout a block.'
548
+ });
549
+ }
550
+ }
551
+ if (this.pos < len && isWs(src.charCodeAt(this.pos))) {
552
+ p = this.pos;
553
+ while (p < len && isWs(src.charCodeAt(p))) {
554
+ p++;
555
+ }
556
+ if (p < len && src.charCodeAt(p) !== 35 && src.charCodeAt(p) !== 10) {
557
+ this.indentStart.push(this.pos);
558
+ this.indentEnd.push(p);
559
+ this.ctx.push(CTX_INDENT);
560
+ this.ctxTok.push(-1);
561
+ this.pos = p;
562
+ if (this.n > 0 && this.kinds[this.n - 1] === NEWLINE)
563
+ this.flags[this.n - 1] |= FLAG_NL_BEFORE_INDENT;
564
+ this.push(INDENT, p, p, this.line, p - this.lineStart + 1);
565
+ } else {
566
+ this.pos = p;
567
+ this.pending |= FLAG_SPACE_BEFORE;
568
+ }
569
+ }
570
+ };
571
+ Lexer.prototype.closeIndents = function () {
572
+ var col;
573
+ while (this.ctx.length > 0 && this.ctx[this.ctx.length - 1] === CTX_INDENT) {
574
+ this.ctx.pop();
575
+ this.ctxTok.pop();
576
+ this.indentStart.pop();
577
+ this.indentEnd.pop();
578
+ col = this.pos - this.lineStart + 1;
579
+ this.push(DEDENT, this.pos, this.pos, this.line, col);
580
+ this.push(NEWLINE, this.pos, this.pos, this.line, col);
581
+ }
582
+ };
583
+ Lexer.prototype.opener = function (kind, ctxKind) {
584
+ var idx;
585
+ idx = this.pushHere(kind, this.pos, this.pos + 1);
586
+ this.ctx.push(ctxKind);
587
+ this.ctxTok.push(idx);
588
+ return this.pos++;
589
+ };
590
+ Lexer.prototype.closer = function (kind, ctxKind, text) {
591
+ var idx, open, otext;
592
+ this.closeIndents();
593
+ if (!(this.ctx.length > 0 && this.ctx[this.ctx.length - 1] === ctxKind)) {
594
+ if (this.ctx.length > 0 && this.ctx[this.ctx.length - 1] !== CTX_INTERP) {
595
+ open = this.ctxTok[this.ctxTok.length - 1];
596
+ otext = this.src.slice(this.starts[open], this.ends[open]);
597
+ this.fail('' + text + ' does not match the ' + otext + ' opened on line ' + this.lines[open] + ', column ' + this.cols[open], this.pos, text, {
598
+ code: 'MS1023',
599
+ opener: this.starts[open],
600
+ openerLabel: 'the ' + otext + ' was opened here',
601
+ why: 'Brackets close in the reverse order they were opened.',
602
+ fix: 'close the ' + otext + ' with ' + CLOSER_OF[otext] + ' first, or change this ' + text + ' to ' + CLOSER_OF[otext] + '.'
603
+ });
604
+ }
605
+ this.fail('' + text + ' closes nothing: no bracket is open here', this.pos, text, {
606
+ code: 'MS1023',
607
+ why: 'Every closing bracket needs an opening one before it.',
608
+ fix: 'remove this ' + text + ', or add the matching ' + OPENER_OF[text] + ' earlier.'
609
+ });
610
+ }
611
+ this.ctx.pop();
612
+ open = this.ctxTok.pop();
613
+ idx = this.pushHere(kind, this.pos, this.pos + 1);
614
+ this.pair[open] = idx;
615
+ this.pair[idx] = open;
616
+ return this.pos++;
617
+ };
618
+ Lexer.prototype.comment = function () {
619
+ var close, i, p, q, src;
620
+ src = this.src;
621
+ p = this.pos;
622
+ if (src.charCodeAt(p + 1) === 35 && src.charCodeAt(p + 2) === 35 && p + 3 < this.len && src.charCodeAt(p + 3) !== 35) {
623
+ close = src.indexOf('###', p + 4);
624
+ if (close === -1)
625
+ this.fail('unclosed block comment: this ### is never closed', p, '###', {
626
+ code: 'MS1014',
627
+ why: 'A ### block comment runs until the next ###.',
628
+ fix: 'add ### where the comment should end.'
629
+ });
630
+ i = p;
631
+ while (i < close) {
632
+ if (src.charCodeAt(i) === 10) {
633
+ this.line++;
634
+ this.lineStart = i + 1;
635
+ }
636
+ i++;
637
+ }
638
+ this.pos = close + 3;
639
+ } else {
640
+ q = src.indexOf('\n', p);
641
+ this.pos = q === -1 ? this.len : q;
642
+ }
643
+ return this.pending |= FLAG_SPACE_BEFORE;
644
+ };
645
+ Lexer.prototype.backslash = function () {
646
+ var c1, src;
647
+ src = this.src;
648
+ c1 = src.charCodeAt(this.pos + 1);
649
+ if (c1 === 10 || c1 === 13 && src.charCodeAt(this.pos + 2) === 10) {
650
+ this.pos += c1 === 10 ? 2 : 3;
651
+ this.line++;
652
+ this.lineStart = this.pos;
653
+ this.pending |= FLAG_SPACE_BEFORE;
654
+ return this.lineStartIndent();
655
+ } else {
656
+ return this.fail('a backslash is only allowed at the end of a line, to continue it', this.pos, '\\', {
657
+ code: 'MS1024',
658
+ why: 'Outside strings and regular expressions, a backslash has one job: as the last character of a line it joins the next line to this one.',
659
+ fix: 'move the backslash to the end of the line, or remove it.'
660
+ });
661
+ }
662
+ };
663
+ Lexer.prototype.matchIdentRe = function (start) {
664
+ var m;
665
+ IDENT_RE.lastIndex = start;
666
+ m = IDENT_RE.exec(this.src);
667
+ if (null != m) {
668
+ return start + m[0].length;
669
+ } else {
670
+ return -1;
671
+ }
672
+ };
673
+ Lexer.prototype.lexWord = function () {
674
+ var c, cc, idx, kind, len, p, src, start, text;
675
+ src = this.src;
676
+ len = this.len;
677
+ start = this.pos;
678
+ c = src.charCodeAt(start);
679
+ p = -1;
680
+ if (c < 128) {
681
+ p = start + 1;
682
+ cc = 0;
683
+ while (p < len) {
684
+ cc = src.charCodeAt(p);
685
+ if (!(cc < 128 && IDENT_PART[cc] === 1))
686
+ break;
687
+ p++;
688
+ }
689
+ if (p < len && (cc >= 128 || cc === 92))
690
+ p = this.matchIdentRe(start);
691
+ } else {
692
+ p = this.matchIdentRe(start);
693
+ }
694
+ if (p === -1)
695
+ this.fail("unexpected character '" + src.charAt(start) + "'", start, src.charAt(start), {
696
+ code: 'MS1001',
697
+ why: 'This character cannot start any MambaScript token.',
698
+ fix: 'remove it; if the code was pasted, check for smart quotes or non-breaking spaces.'
699
+ });
700
+ text = src.slice(start, p);
701
+ kind = KW[text];
702
+ if (!(null != kind))
703
+ kind = null != MACROS[text] || DUNDER.test(text) ? MACRO : IDENT;
704
+ this.pos = p;
705
+ if ((kind === T.AND || kind === T.OR || kind === T.ALSO) && src.charCodeAt(p) === 61 && src.charCodeAt(p + 1) !== 61) {
706
+ this.pos = p + 1;
707
+ idx = this.pushHere(COMPOUND_ASSIGN, start, p + 1);
708
+ this.vals[idx] = text;
709
+ return;
710
+ }
711
+ this.pushHere(kind, start, p);
712
+ };
713
+ Lexer.prototype.scanDigits = function (q, isDigit) {
714
+ var len, src;
715
+ src = this.src;
716
+ len = this.len;
717
+ while (true) {
718
+ while (q < len && isDigit(src.charCodeAt(q))) {
719
+ q++;
720
+ }
721
+ if (q < len && src.charCodeAt(q) === 95) {
722
+ if (q + 1 < len && isDigit(src.charCodeAt(q + 1)) && isDigit(src.charCodeAt(q - 1))) {
723
+ q++;
724
+ continue;
725
+ }
726
+ this.fail('a digit separator must sit between two digits', q, '_', {
727
+ code: 'MS1003',
728
+ why: 'The underscore in a number is only a visual separator (1_000_000); it needs a digit on each side and cannot be doubled.',
729
+ fix: 'write the number as digits with single underscores between them, e.g. 1_000.'
730
+ });
731
+ }
732
+ break;
733
+ }
734
+ return q;
735
+ };
736
+ Lexer.prototype.lexNumber = function () {
737
+ var c, c1, e, kind, len, p, q, s, src, start;
738
+ src = this.src;
739
+ len = this.len;
740
+ start = this.pos;
741
+ c = src.charCodeAt(start);
742
+ p = start;
743
+ kind = NUMBER_INT;
744
+ c1 = src.charCodeAt(start + 1);
745
+ if (c === 48 && (c1 === 98 || c1 === 111 || c1 === 120)) {
746
+ p = start + 2;
747
+ q = p;
748
+ if (c1 === 120) {
749
+ q = this.scanDigits(q, isHexDigit);
750
+ } else if (c1 === 111) {
751
+ q = this.scanDigits(q, isOctalDigit);
752
+ } else {
753
+ q = this.scanDigits(q, isBinaryDigit);
754
+ }
755
+ if (q === p)
756
+ this.fail("'" + src.slice(start, p) + "' is not a valid number", start, src.slice(start, p), {
757
+ code: 'MS1002',
758
+ why: 'A number is digits with an optional decimal point or exponent (1.5e3), or 0x, 0b, 0o followed by digits of that base.',
759
+ fix: 'fix the digits, or put a space between the number and the word after it.'
760
+ });
761
+ p = q;
762
+ } else {
763
+ if (c === 48) {
764
+ p = start + 1;
765
+ } else {
766
+ p = this.scanDigits(p, isDecimalDigit);
767
+ }
768
+ if (src.charCodeAt(p) === 46 && p + 1 < len && src.charCodeAt(p + 1) < 128 && DIGIT[src.charCodeAt(p + 1)] === 1) {
769
+ p = this.scanDigits(p + 1, isDecimalDigit);
770
+ kind = NUMBER_FLOAT;
771
+ }
772
+ e = src.charCodeAt(p);
773
+ if (e === 101 || e === 69) {
774
+ q = p + 1;
775
+ s = src.charCodeAt(q);
776
+ if (s === 43 || s === 45)
777
+ q++;
778
+ if (q < len && src.charCodeAt(q) < 128 && DIGIT[src.charCodeAt(q)] === 1) {
779
+ p = this.scanDigits(q, isDecimalDigit);
780
+ kind = NUMBER_FLOAT;
781
+ }
782
+ }
783
+ }
784
+ if (src.charCodeAt(p) === 110 && !(p + 1 < len && src.charCodeAt(p + 1) < 128 && IDENT_PART[src.charCodeAt(p + 1)] === 1)) {
785
+ if (kind === NUMBER_FLOAT)
786
+ this.fail("'" + src.slice(start, p + 1) + "' is not a valid BigInt", start, src.slice(start, p + 1), {
787
+ code: 'MS1002',
788
+ why: 'A BigInt is a whole number: digits (or 0x, 0b, 0o digits) followed by n; it cannot have a decimal point or an exponent.',
789
+ fix: 'drop the n to keep a Float, or drop the decimal point / exponent to make it a BigInt.'
790
+ });
791
+ kind = NUMBER_BIGINT;
792
+ p++;
793
+ }
794
+ this.pos = p;
795
+ this.pushHere(kind, start, p);
796
+ };
797
+ Lexer.prototype.lexString = function (q) {
798
+ var c, c1, chunk, dq, endTok, heredoc, idx, interp, len, pieceLine, pieceLineStart, pieceStart, quoteLen, src, start, startTok, val;
799
+ src = this.src;
800
+ len = this.len;
801
+ start = this.pos;
802
+ heredoc = src.charCodeAt(start + 1) === q && src.charCodeAt(start + 2) === q;
803
+ dq = q === 34;
804
+ quoteLen = heredoc ? 3 : 1;
805
+ startTok = this.pushHere(STRING_START, start, start + quoteLen);
806
+ this.flags[startTok] |= (heredoc ? FLAG_HEREDOC : 0) | (dq ? FLAG_DOUBLE_QUOTE : 0);
807
+ this.pos = start + quoteLen;
808
+ val = '';
809
+ chunk = this.pos;
810
+ pieceStart = this.pos;
811
+ pieceLine = this.line;
812
+ pieceLineStart = this.lineStart;
813
+ while (true) {
814
+ if (this.pos >= len)
815
+ this.fail('unclosed string: the ' + src.slice(start, start + quoteLen) + ' on line ' + this.lines[startTok] + ', column ' + this.cols[startTok] + ' is never closed', start, src.slice(start, start + quoteLen), {
816
+ code: 'MS1010',
817
+ why: quoteLen === 3 ? 'A block string ends with the same triple quote it started with.' : 'A string ends with the same quote it started with, on the same line; a string that spans lines needs triple quotes.',
818
+ fix: quoteLen === 3 ? 'add the closing ' + src.slice(start, start + quoteLen) + '.' : 'add the closing ' + src.charAt(start) + ', or use """ for a string that spans lines.'
819
+ });
820
+ c = src.charCodeAt(this.pos);
821
+ if (c === q && (!heredoc || src.charCodeAt(this.pos + 1) === q && src.charCodeAt(this.pos + 2) === q)) {
822
+ val += src.slice(chunk, this.pos);
823
+ if (val.length > 0) {
824
+ idx = this.push(STRING_PIECE, pieceStart, this.pos, pieceLine, pieceStart - pieceLineStart + 1);
825
+ this.vals[idx] = val;
826
+ }
827
+ endTok = this.pushHere(STRING_END, this.pos, this.pos + quoteLen);
828
+ this.pair[startTok] = endTok;
829
+ this.pair[endTok] = startTok;
830
+ this.pos += quoteLen;
831
+ return;
832
+ }
833
+ if (c === 92) {
834
+ c1 = src.charCodeAt(this.pos + 1);
835
+ val += src.slice(chunk, this.pos);
836
+ if (c1 === 10 || c1 === 13 && src.charCodeAt(this.pos + 2) === 10) {
837
+ this.pos += c1 === 10 ? 2 : 3;
838
+ this.line++;
839
+ this.lineStart = this.pos;
840
+ } else {
841
+ this.pos = this.escape(c1, val, function (s) {
842
+ return val = s;
843
+ });
844
+ }
845
+ chunk = this.pos;
846
+ } else if (c === 35 && dq && src.charCodeAt(this.pos + 1) === 123) {
847
+ val += src.slice(chunk, this.pos);
848
+ if (val.length > 0) {
849
+ idx = this.push(STRING_PIECE, pieceStart, this.pos, pieceLine, pieceStart - pieceLineStart + 1);
850
+ this.vals[idx] = val;
851
+ }
852
+ interp = this.pushHere(INTERP_START, this.pos, this.pos + 2);
853
+ this.pos += 2;
854
+ this.ctx.push(CTX_INTERP);
855
+ this.ctxTok.push(interp);
856
+ this.lexBody(true);
857
+ this.pair[interp] = this.n - 1;
858
+ this.pair[this.n - 1] = interp;
859
+ val = '';
860
+ chunk = this.pos;
861
+ pieceStart = this.pos;
862
+ pieceLine = this.line;
863
+ pieceLineStart = this.lineStart;
864
+ } else {
865
+ if (c === 10) {
866
+ this.line++;
867
+ this.lineStart = this.pos + 1;
868
+ }
869
+ this.pos++;
870
+ }
871
+ }
872
+ };
873
+ Lexer.prototype.escape = function (c1, val, set) {
874
+ var cc, j, nxt, ok, p, src;
875
+ src = this.src;
876
+ p = this.pos;
877
+ switch (c1) {
878
+ case 120:
879
+ if (p + 3 < src.length + 1 && HEX[src.charCodeAt(p + 2) & 127] === 1 && src.charCodeAt(p + 2) < 128 && HEX[src.charCodeAt(p + 3) & 127] === 1 && src.charCodeAt(p + 3) < 128) {
880
+ set(val + String.fromCharCode(parseInt(src.slice(p + 2, p + 4), 16)));
881
+ return p + 4;
882
+ }
883
+ set(val + 'x');
884
+ return p + 2;
885
+ break;
886
+ case 117:
887
+ ok = true;
888
+ j = 2;
889
+ while (j < 6) {
890
+ cc = src.charCodeAt(p + j);
891
+ if (!(cc < 128 && HEX[cc] === 1))
892
+ ok = false;
893
+ j++;
894
+ }
895
+ if (ok) {
896
+ set(val + String.fromCharCode(parseInt(src.slice(p + 2, p + 6), 16)));
897
+ return p + 6;
898
+ }
899
+ set(val + 'u');
900
+ return p + 2;
901
+ break;
902
+ case 48:
903
+ nxt = src.charCodeAt(p + 2);
904
+ if (nxt < 128 && DIGIT[nxt] === 1)
905
+ this.fail('octal escape sequences are not allowed in strings', p, src.slice(p, p + 3), {
906
+ code: 'MS1012',
907
+ why: 'Octal escapes like \\012 are a legacy JavaScript feature that strict mode forbids.',
908
+ fix: 'write the character as a hex escape (\\x0A) or a unicode escape (\\u000A).'
909
+ });
910
+ set(val + '\0');
911
+ return p + 2;
912
+ break;
913
+ case 98:
914
+ set(val + '\b');
915
+ break;
916
+ case 116:
917
+ set(val + '\t');
918
+ break;
919
+ case 110:
920
+ set(val + '\n');
921
+ break;
922
+ case 118:
923
+ set(val + '\x0B');
924
+ break;
925
+ case 102:
926
+ set(val + '\f');
927
+ break;
928
+ case 114:
929
+ set(val + '\r');
930
+ break;
931
+ default: {
932
+ if (p + 1 >= src.length)
933
+ this.fail('a string cannot end with a lone backslash', p, '\\', {
934
+ code: 'MS1013',
935
+ why: 'A backslash escapes the character after it, so a backslash right before the closing quote escapes the quote and the string never ends.',
936
+ fix: 'write \\\\ for a literal backslash, or remove it.'
937
+ });
938
+ if (c1 === 10) {
939
+ this.line++;
940
+ this.lineStart = p + 2;
941
+ }
942
+ set(val + src.charAt(p + 1));
943
+ }
944
+ }
945
+ return p + 2;
946
+ };
947
+ Lexer.prototype.lexJsLiteral = function () {
948
+ var close, col, i, idx, line, src, start;
949
+ src = this.src;
950
+ start = this.pos;
951
+ close = src.indexOf('`', start + 1);
952
+ if (close === -1)
953
+ this.fail('unclosed JavaScript literal: the ` is never closed', start, '`', {
954
+ code: 'MS1015',
955
+ why: 'Backticks hold raw JavaScript and run to the next backtick.',
956
+ fix: 'add the closing `, or escape a backtick inside the JavaScript as \\`.'
957
+ });
958
+ line = this.line;
959
+ col = start - this.lineStart + 1;
960
+ i = start;
961
+ while (i < close) {
962
+ if (src.charCodeAt(i) === 10) {
963
+ this.line++;
964
+ this.lineStart = i + 1;
965
+ }
966
+ i++;
967
+ }
968
+ this.pos = close + 1;
969
+ idx = this.push(JSLITERAL, start, close + 1, line, col);
970
+ this.vals[idx] = src.slice(start + 1, close);
971
+ };
972
+ Lexer.prototype.lexSlash = function () {
973
+ var allowed, close, end, flags, heregex, idx, nl, nxt, p, q, src;
974
+ src = this.src;
975
+ p = this.pos;
976
+ heregex = src.charCodeAt(p + 1) === 47 && src.charCodeAt(p + 2) === 47;
977
+ allowed = !this.prevValue;
978
+ if (!allowed && (this.pending & FLAG_SPACE_BEFORE) !== 0) {
979
+ nxt = src.charCodeAt(p + 1);
980
+ if (heregex) {
981
+ allowed = true;
982
+ } else if (nxt !== 61 && nxt !== 10 && !isWs(nxt)) {
983
+ close = src.indexOf('/', p + 1);
984
+ nl = src.indexOf('\n', p + 1);
985
+ allowed = close !== -1 && (nl === -1 || close < nl);
986
+ }
987
+ }
988
+ if (allowed && heregex) {
989
+ this.lexHeregex();
990
+ return;
991
+ }
992
+ if (allowed) {
993
+ end = this.scanRegexBody(p + 1);
994
+ if (end === p + 1)
995
+ this.fail('// is not an operator, and an empty regular expression is not allowed', p, '//', {
996
+ code: 'MS1025',
997
+ why: 'JavaScript has no integer-division operator and no empty regular expression literal: // would be read as an empty regex here.',
998
+ fix: 'for integer division write Math.floor(a / b); for a regex that matches the empty string write /(?:)/.'
999
+ });
1000
+ if (end !== -1) {
1001
+ q = end + 1;
1002
+ while (q < this.len && this.isRegexFlag(src.charCodeAt(q))) {
1003
+ q++;
1004
+ }
1005
+ flags = src.slice(end + 1, q);
1006
+ this.checkFlags(flags, end + 1);
1007
+ idx = this.pushHere(REGEX, p, q);
1008
+ this.vals[idx] = {
1009
+ data: src.slice(p + 1, end),
1010
+ flags: flags
1011
+ };
1012
+ this.pos = q;
1013
+ return;
1014
+ }
1015
+ if (!this.prevValue)
1016
+ this.fail('unterminated regular expression: the closing / must be on the same line', p, '/', {
1017
+ code: 'MS1016',
1018
+ why: 'A /regex/ ends on the line it starts.',
1019
+ fix: 'add the closing /, or use a heregex (///\u2026///) for a long or multi-line pattern.'
1020
+ });
1021
+ }
1022
+ if (src.charCodeAt(p + 1) === 61 && src.charCodeAt(p + 2) !== 61) {
1023
+ idx = this.pushHere(COMPOUND_ASSIGN, p, p + 2);
1024
+ this.vals[idx] = '/';
1025
+ this.pos = p + 2;
1026
+ } else {
1027
+ this.pushHere(T.SLASH, p, p + 1);
1028
+ this.pos = p + 1;
1029
+ }
1030
+ };
1031
+ Lexer.prototype.isRegexFlag = function (c) {
1032
+ return c === 103 || c === 105 || c === 109 || c === 115 || c === 117 || c === 121 || c === 100 || c === 118;
1033
+ };
1034
+ Lexer.prototype.checkFlags = function (flags, at) {
1035
+ var i;
1036
+ if (flags.length > 8)
1037
+ this.fail("'" + flags + "' is not a valid set of regular expression flags", at, flags, {
1038
+ code: 'MS1017',
1039
+ why: 'The flags after a regular expression are chosen from g, i, m, s, u, y, d and v.',
1040
+ fix: 'remove the flag that is not one of those, or add a space if the letters were meant as code.'
1041
+ });
1042
+ i = 0;
1043
+ while (i < flags.length) {
1044
+ if (flags.indexOf(flags.charAt(i), i + 1) !== -1)
1045
+ this.fail("the regular expression flag '" + flags.charAt(i) + "' is repeated", at, flags, {
1046
+ code: 'MS1017',
1047
+ why: 'Each flag may appear once.',
1048
+ fix: 'write it once: /\u2026/' + flags.charAt(i) + '.'
1049
+ });
1050
+ i++;
1051
+ }
1052
+ };
1053
+ Lexer.prototype.scanRegexBody = function (i) {
1054
+ var c, len, src;
1055
+ src = this.src;
1056
+ len = this.len;
1057
+ while (i < len) {
1058
+ c = src.charCodeAt(i);
1059
+ if (c === 10)
1060
+ return -1;
1061
+ if (c === 92) {
1062
+ if (src.charCodeAt(i + 1) === 10)
1063
+ return -1;
1064
+ i += 2;
1065
+ continue;
1066
+ }
1067
+ if (c === 47)
1068
+ return i;
1069
+ if (c === 91) {
1070
+ i++;
1071
+ while (i < len) {
1072
+ c = src.charCodeAt(i);
1073
+ if (c === 10)
1074
+ return -1;
1075
+ if (c === 92) {
1076
+ i += 2;
1077
+ continue;
1078
+ }
1079
+ if (c === 93)
1080
+ break;
1081
+ i++;
1082
+ }
1083
+ if (i >= len)
1084
+ return -1;
1085
+ }
1086
+ i++;
1087
+ }
1088
+ return -1;
1089
+ };
1090
+ Lexer.prototype.lexHeregex = function () {
1091
+ var c, col, flags, idx, len, line, n, p, pieceStart, q, src, start, startTok;
1092
+ src = this.src;
1093
+ len = this.len;
1094
+ start = this.pos;
1095
+ startTok = this.pushHere(HEREGEX_START, start, start + 3);
1096
+ this.pos = start + 3;
1097
+ while (true) {
1098
+ if (this.pos >= len)
1099
+ this.fail('unclosed heregex: the /// on line ' + this.lines[startTok] + ' is never closed', start, '///', {
1100
+ code: 'MS1018',
1101
+ why: 'A heregex runs from /// to the next ///; whitespace and # comments inside are ignored.',
1102
+ fix: 'add the closing ///.'
1103
+ });
1104
+ c = src.charCodeAt(this.pos);
1105
+ if (c === 47 && src.charCodeAt(this.pos + 1) === 47 && src.charCodeAt(this.pos + 2) === 47) {
1106
+ p = this.pos + 3;
1107
+ q = p;
1108
+ while (q < len && this.isRegexFlag(src.charCodeAt(q))) {
1109
+ q++;
1110
+ }
1111
+ flags = src.slice(p, q);
1112
+ this.checkFlags(flags, p);
1113
+ idx = this.pushHere(HEREGEX_END, this.pos, q);
1114
+ this.vals[idx] = flags;
1115
+ this.pair[startTok] = idx;
1116
+ this.pair[idx] = startTok;
1117
+ this.pos = q;
1118
+ return;
1119
+ }
1120
+ if (c === 32 || c === 13 || c === 10) {
1121
+ pieceStart = this.pos;
1122
+ line = this.line;
1123
+ col = this.pos - this.lineStart + 1;
1124
+ while (this.pos < len) {
1125
+ c = src.charCodeAt(this.pos);
1126
+ if (!(c === 32 || c === 13 || c === 10))
1127
+ break;
1128
+ if (c === 10) {
1129
+ this.line++;
1130
+ this.lineStart = this.pos + 1;
1131
+ }
1132
+ this.pos++;
1133
+ }
1134
+ idx = this.push(HEREGEX_PIECE, pieceStart, this.pos, line, col);
1135
+ this.flags[idx] |= HEREGEX_WS;
1136
+ this.vals[idx] = '';
1137
+ } else if (c === 35) {
1138
+ if (src.charCodeAt(this.pos + 1) === 123) {
1139
+ this.heregexInterpolation();
1140
+ } else {
1141
+ q = src.indexOf('\n', this.pos);
1142
+ this.pos = q === -1 ? len : q;
1143
+ }
1144
+ } else if (c === 92) {
1145
+ this.heregexPiece(this.pos, this.pos + 2, HEREGEX_ESCAPE);
1146
+ if (src.charCodeAt(this.pos + 1) === 10) {
1147
+ this.line++;
1148
+ this.lineStart = this.pos + 2;
1149
+ }
1150
+ this.pos += 2;
1151
+ } else if (c === 91) {
1152
+ this.lexHeregexClass();
1153
+ } else if (c === 47) {
1154
+ n = src.charCodeAt(this.pos + 1) === 47 ? 2 : 1;
1155
+ this.heregexPiece(this.pos, this.pos + n, HEREGEX_ESCAPE);
1156
+ this.pos += n;
1157
+ } else {
1158
+ p = this.pos;
1159
+ while (p < len) {
1160
+ c = src.charCodeAt(p);
1161
+ if (c === 92 || c === 47 || c === 35 || c === 91 || c === 32 || c === 13 || c === 10)
1162
+ break;
1163
+ p++;
1164
+ }
1165
+ this.heregexPiece(this.pos, p, HEREGEX_TEXT);
1166
+ this.pos = p;
1167
+ }
1168
+ }
1169
+ };
1170
+ Lexer.prototype.heregexPiece = function (from, to, subkind) {
1171
+ var idx;
1172
+ idx = this.push(HEREGEX_PIECE, from, to, this.line, from - this.lineStart + 1);
1173
+ this.flags[idx] |= subkind;
1174
+ this.vals[idx] = this.src.slice(from, to);
1175
+ return idx;
1176
+ };
1177
+ Lexer.prototype.heregexInterpolation = function () {
1178
+ var interp;
1179
+ interp = this.pushHere(INTERP_START, this.pos, this.pos + 2);
1180
+ this.pos += 2;
1181
+ this.ctx.push(CTX_INTERP);
1182
+ this.ctxTok.push(interp);
1183
+ this.lexBody(true);
1184
+ this.pair[interp] = this.n - 1;
1185
+ this.pair[this.n - 1] = interp;
1186
+ };
1187
+ Lexer.prototype.lexHeregexClass = function () {
1188
+ var c, col, idx, len, line, n, p, src;
1189
+ src = this.src;
1190
+ len = this.len;
1191
+ this.heregexPiece(this.pos, this.pos + 1, HEREGEX_CLASS_OPEN);
1192
+ this.pos++;
1193
+ while (true) {
1194
+ if (this.pos >= len)
1195
+ this.fail('unclosed character class in a heregex', this.pos, '[', {
1196
+ code: 'MS1019',
1197
+ why: 'A [ inside a regular expression starts a character class that must be closed with ].',
1198
+ fix: 'add the closing ], or escape the [ as \\[.'
1199
+ });
1200
+ c = src.charCodeAt(this.pos);
1201
+ if (c === 93) {
1202
+ this.heregexPiece(this.pos, this.pos + 1, HEREGEX_CLASS_CLOSE);
1203
+ this.pos++;
1204
+ return;
1205
+ }
1206
+ if (c === 92) {
1207
+ this.heregexPiece(this.pos, this.pos + 2, HEREGEX_ESCAPE);
1208
+ if (src.charCodeAt(this.pos + 1) === 10) {
1209
+ this.line++;
1210
+ this.lineStart = this.pos + 2;
1211
+ }
1212
+ this.pos += 2;
1213
+ } else if (c === 47) {
1214
+ n = src.charCodeAt(this.pos + 1) === 47 ? 2 : 1;
1215
+ this.heregexPiece(this.pos, this.pos + n, HEREGEX_ESCAPE);
1216
+ this.pos += n;
1217
+ } else if (c === 35 && src.charCodeAt(this.pos + 1) === 123) {
1218
+ this.heregexInterpolation();
1219
+ } else if (c === 35) {
1220
+ this.heregexPiece(this.pos, this.pos + 1, HEREGEX_ESCAPE);
1221
+ this.pos++;
1222
+ } else {
1223
+ p = this.pos;
1224
+ line = this.line;
1225
+ col = p - this.lineStart + 1;
1226
+ while (p < len) {
1227
+ c = src.charCodeAt(p);
1228
+ if (c === 93 || c === 92 || c === 47 || c === 35)
1229
+ break;
1230
+ if (c === 10) {
1231
+ this.line++;
1232
+ this.lineStart = p + 1;
1233
+ }
1234
+ p++;
1235
+ }
1236
+ idx = this.push(HEREGEX_PIECE, this.pos, p, line, col);
1237
+ this.flags[idx] |= HEREGEX_CLASS;
1238
+ this.vals[idx] = src.slice(this.pos, p);
1239
+ this.pos = p;
1240
+ }
1241
+ }
1242
+ };
1243
+ Lexer.prototype.punct = function (c) {
1244
+ var c1, c2, c3, compound, idx, kind, n, p, src;
1245
+ src = this.src;
1246
+ p = this.pos;
1247
+ c1 = src.charCodeAt(p + 1);
1248
+ c2 = src.charCodeAt(p + 2);
1249
+ c3 = src.charCodeAt(p + 3);
1250
+ kind = -1;
1251
+ n = 1;
1252
+ compound = null;
1253
+ switch (c) {
1254
+ case 46:
1255
+ if (c1 === 46) {
1256
+ if (c2 === 46) {
1257
+ kind = T.ELLIPSIS;
1258
+ n = 3;
1259
+ } else {
1260
+ kind = T.DOTDOT;
1261
+ n = 2;
1262
+ }
1263
+ } else {
1264
+ kind = T.DOT;
1265
+ }
1266
+ break;
1267
+ case 58:
1268
+ if (c1 === 58) {
1269
+ kind = T.DCOLON;
1270
+ n = 2;
1271
+ } else {
1272
+ kind = T.COLON;
1273
+ }
1274
+ break;
1275
+ case 44:
1276
+ kind = T.COMMA;
1277
+ break;
1278
+ case 59:
1279
+ kind = T.SEMI;
1280
+ break;
1281
+ case 64:
1282
+ kind = T.AT;
1283
+ break;
1284
+ case 126:
1285
+ kind = T.TILDE;
1286
+ break;
1287
+ case 63:
1288
+ if (c1 === 61 && c2 !== 61) {
1289
+ kind = EXISTS_ASSIGN;
1290
+ n = 2;
1291
+ } else {
1292
+ kind = T.QUESTION;
1293
+ }
1294
+ break;
1295
+ case 61:
1296
+ if (c1 === 62) {
1297
+ kind = T.FAT_ARROW;
1298
+ n = 2;
1299
+ } else if (c1 === 61) {
1300
+ kind = T.EQEQ;
1301
+ n = 2;
1302
+ } else {
1303
+ kind = T.ASSIGN;
1304
+ }
1305
+ break;
1306
+ case 45:
1307
+ if (c1 === 62) {
1308
+ kind = T.ARROW;
1309
+ n = 2;
1310
+ } else if (c1 === 45) {
1311
+ kind = T.MINUSMINUS;
1312
+ n = 2;
1313
+ } else if (c1 === 61 && c2 !== 61) {
1314
+ compound = '-';
1315
+ n = 2;
1316
+ } else {
1317
+ kind = T.MINUS;
1318
+ }
1319
+ break;
1320
+ case 43:
1321
+ if (c1 === 43) {
1322
+ kind = T.PLUSPLUS;
1323
+ n = 2;
1324
+ } else if (c1 === 61 && c2 !== 61) {
1325
+ compound = '+';
1326
+ n = 2;
1327
+ } else {
1328
+ kind = T.PLUS;
1329
+ }
1330
+ break;
1331
+ case 42:
1332
+ if (c1 === 42) {
1333
+ if (c2 === 61 && c3 !== 61) {
1334
+ compound = '**';
1335
+ n = 3;
1336
+ } else {
1337
+ kind = T.STARSTAR;
1338
+ n = 2;
1339
+ }
1340
+ } else if (c1 === 61 && c2 !== 61) {
1341
+ compound = '*';
1342
+ n = 2;
1343
+ } else {
1344
+ kind = T.STAR;
1345
+ }
1346
+ break;
1347
+ case 37:
1348
+ if (c1 === 61 && c2 !== 61) {
1349
+ compound = '%';
1350
+ n = 2;
1351
+ } else {
1352
+ kind = T.PERCENT;
1353
+ }
1354
+ break;
1355
+ case 60:
1356
+ if (c1 === 60) {
1357
+ if (c2 === 61 && c3 !== 61) {
1358
+ compound = '<<';
1359
+ n = 3;
1360
+ } else {
1361
+ kind = T.SHL;
1362
+ n = 2;
1363
+ }
1364
+ } else if (c1 === 61) {
1365
+ kind = T.LTE;
1366
+ n = 2;
1367
+ } else {
1368
+ kind = T.LT;
1369
+ }
1370
+ break;
1371
+ case 62:
1372
+ if (c1 === 62) {
1373
+ if (c2 === 62) {
1374
+ if (c3 === 61 && src.charCodeAt(p + 4) !== 61) {
1375
+ compound = '>>>';
1376
+ n = 4;
1377
+ } else {
1378
+ kind = T.USHR;
1379
+ n = 3;
1380
+ }
1381
+ } else if (c2 === 61 && c3 !== 61) {
1382
+ compound = '>>';
1383
+ n = 3;
1384
+ } else {
1385
+ kind = T.SHR;
1386
+ n = 2;
1387
+ }
1388
+ } else if (c1 === 61) {
1389
+ kind = T.GTE;
1390
+ n = 2;
1391
+ } else {
1392
+ kind = T.GT;
1393
+ }
1394
+ break;
1395
+ case 38:
1396
+ if (c1 === 38) {
1397
+ if (c2 === 61 && c3 !== 61) {
1398
+ compound = '&&';
1399
+ n = 3;
1400
+ } else {
1401
+ kind = T.AMPAMP;
1402
+ n = 2;
1403
+ }
1404
+ } else if (c1 === 61 && c2 !== 61) {
1405
+ compound = '&';
1406
+ n = 2;
1407
+ } else {
1408
+ kind = T.AMP;
1409
+ }
1410
+ break;
1411
+ case 124:
1412
+ if (c1 === 124) {
1413
+ if (c2 === 61 && c3 !== 61) {
1414
+ compound = '||';
1415
+ n = 3;
1416
+ } else {
1417
+ kind = T.PIPEPIPE;
1418
+ n = 2;
1419
+ }
1420
+ } else if (c1 === 61 && c2 !== 61) {
1421
+ compound = '|';
1422
+ n = 2;
1423
+ } else {
1424
+ kind = T.PIPE;
1425
+ }
1426
+ break;
1427
+ case 94:
1428
+ if (c1 === 61 && c2 !== 61) {
1429
+ compound = '^';
1430
+ n = 2;
1431
+ } else {
1432
+ kind = T.CARET;
1433
+ }
1434
+ break;
1435
+ case 33:
1436
+ if (c1 === 61) {
1437
+ kind = T.NEQ;
1438
+ n = 2;
1439
+ } else {
1440
+ kind = T.BANG;
1441
+ }
1442
+ break;
1443
+ default:
1444
+ this.fail("unexpected character '" + src.charAt(p) + "'", p, src.charAt(p), {
1445
+ code: 'MS1001',
1446
+ why: 'This character cannot start any MambaScript token.',
1447
+ fix: 'remove it; if the code was pasted, check for smart quotes or non-breaking spaces.'
1448
+ });
1449
+ }
1450
+ if (null != compound) {
1451
+ idx = this.pushHere(COMPOUND_ASSIGN, p, p + n);
1452
+ this.vals[idx] = compound;
1453
+ } else {
1454
+ this.pushHere(kind, p, p + n);
1455
+ }
1456
+ this.pos = p + n;
1457
+ };
1458
+ return Lexer;
1459
+ }();
1460
+ tokenize = function (src, options) {
1461
+ return new Lexer(src, options).tokenize();
1462
+ };
1463
+ dumpTokens = function (src, options) {
1464
+ var fl, i, kind, line, lx, out, text;
1465
+ lx = tokenize(src, options);
1466
+ out = [];
1467
+ i = 0;
1468
+ while (i < lx.n) {
1469
+ kind = lx.kinds[i];
1470
+ text = src.slice(lx.starts[i], lx.ends[i]);
1471
+ text = JSON.stringify(text);
1472
+ fl = [];
1473
+ if ((lx.flags[i] & FLAG_SPACE_BEFORE) !== 0)
1474
+ fl.push('space');
1475
+ if ((lx.flags[i] & FLAG_NEWLINE_BEFORE) !== 0)
1476
+ fl.push('nl');
1477
+ if ((lx.flags[i] & FLAG_NL_BEFORE_INDENT) !== 0)
1478
+ fl.push('indent-next');
1479
+ line = '' + lx.lines[i] + ':' + lx.cols[i] + ' ' + KIND_NAMES[kind] + ' ' + text;
1480
+ if (null != lx.vals[i])
1481
+ line += ' = ' + JSON.stringify(lx.vals[i]);
1482
+ if (fl.length > 0)
1483
+ line += ' [' + fl.join(' ') + ']';
1484
+ out.push(line);
1485
+ i++;
1486
+ }
1487
+ return out.join('\n');
1488
+ };
1489
+ module.exports = {
1490
+ Lexer: Lexer,
1491
+ tokenize: tokenize,
1492
+ dumpTokens: dumpTokens,
1493
+ SyntaxError: MambaSyntaxError,
1494
+ isWs: isWs,
1495
+ VALUE_KIND: VALUE_KIND
1496
+ };
1497
+ function isOwn$(o, p) {
1498
+ return {}.hasOwnProperty.call(o, p);
1499
+ }