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
|
@@ -0,0 +1,3469 @@
|
|
|
1
|
+
// Generated by MambaScript 2.0.0
|
|
2
|
+
var BINARY_OP_CLASS, BINARY_OP_OF_KIND, binaryNode, cache$, CAN_START, CAN_START_KINDS, CAN_START_STATEMENT, CHAINABLE, CONTROL_FLOW, copyPosition, createInterpolation, createInterpolationIn, CS, DEDENT, EOF, FIRST_WORD, FLAG_DOUBLE_QUOTE, FLAG_HEREDOC, FLAG_NL_BEFORE_INDENT, FLAG_SPACE_BEFORE, foldBinaryExpr, HEREGEX_CLASS, HEREGEX_CLASS_CLOSE, HEREGEX_CLASS_OPEN, HEREGEX_SUBKIND_MASK, HEREGEX_TEXT, HEREGEX_WS, IDENT, INDENT, isChainable, isLeftAssoc, isWordKind, JS_HINTS, KIND_NAMES, kindId, LAST_WORD, lexer, MambaSyntaxError, MEMBER_ACCESS, NEEDS_MORE, NEWLINE, parse, Parser, pkg, PRECEDENCE, PREFIX_OP_CLASS, RIGHT_ASSOC, stripLeadingWhitespace, stripLeadingWhitespaceInterpolation, T, tables, templateParts, typeIdentifierNode;
|
|
3
|
+
CS = require('./nodes');
|
|
4
|
+
tables = require('./syntax-tables');
|
|
5
|
+
lexer = require('./lexer');
|
|
6
|
+
pkg = require('../package.json');
|
|
7
|
+
cache$ = tables;
|
|
8
|
+
T = cache$.T;
|
|
9
|
+
KIND_NAMES = cache$.KIND_NAMES;
|
|
10
|
+
BINARY_OP_CLASS = cache$.BINARY_OP_CLASS;
|
|
11
|
+
PRECEDENCE = cache$.PRECEDENCE;
|
|
12
|
+
BINARY_OP_OF_KIND = cache$.BINARY_OP_OF_KIND;
|
|
13
|
+
PREFIX_OP_CLASS = cache$.PREFIX_OP_CLASS;
|
|
14
|
+
CHAINABLE = tables.CHAINABLE_COMPARISON_OPS;
|
|
15
|
+
RIGHT_ASSOC = tables.RIGHT_ASSOC_OPS;
|
|
16
|
+
FLAG_SPACE_BEFORE = tables.FLAG_SPACE_BEFORE;
|
|
17
|
+
FLAG_NL_BEFORE_INDENT = tables.FLAG_NL_BEFORE_INDENT;
|
|
18
|
+
FLAG_HEREDOC = tables.FLAG_HEREDOC;
|
|
19
|
+
FLAG_DOUBLE_QUOTE = tables.FLAG_DOUBLE_QUOTE;
|
|
20
|
+
HEREGEX_SUBKIND_MASK = tables.HEREGEX_SUBKIND_MASK;
|
|
21
|
+
HEREGEX_WS = tables.HEREGEX_WS;
|
|
22
|
+
HEREGEX_TEXT = tables.HEREGEX_TEXT;
|
|
23
|
+
HEREGEX_CLASS = tables.HEREGEX_CLASS;
|
|
24
|
+
HEREGEX_CLASS_OPEN = tables.HEREGEX_CLASS_OPEN;
|
|
25
|
+
HEREGEX_CLASS_CLOSE = tables.HEREGEX_CLASS_CLOSE;
|
|
26
|
+
MambaSyntaxError = lexer.SyntaxError;
|
|
27
|
+
EOF = T.EOF;
|
|
28
|
+
NEWLINE = T.NEWLINE;
|
|
29
|
+
INDENT = T.INDENT;
|
|
30
|
+
DEDENT = T.DEDENT;
|
|
31
|
+
IDENT = T.IDENT;
|
|
32
|
+
FIRST_WORD = T.AND;
|
|
33
|
+
LAST_WORD = T.RESERVED;
|
|
34
|
+
isWordKind = function (k) {
|
|
35
|
+
return k === IDENT || k >= FIRST_WORD && k <= LAST_WORD;
|
|
36
|
+
};
|
|
37
|
+
NEEDS_MORE = {
|
|
38
|
+
'else': 'a statement',
|
|
39
|
+
otherwise: 'a statement',
|
|
40
|
+
then: 'a statement',
|
|
41
|
+
by: 'the step value',
|
|
42
|
+
when: 'a value',
|
|
43
|
+
'in': 'the list to loop over',
|
|
44
|
+
of: 'the object to loop over',
|
|
45
|
+
from: 'the module name in quotes',
|
|
46
|
+
'if': 'a condition',
|
|
47
|
+
unless: 'a condition',
|
|
48
|
+
'while': 'a condition',
|
|
49
|
+
until: 'a condition',
|
|
50
|
+
make: 'a blueprint or constructor name',
|
|
51
|
+
'new': 'a blueprint or constructor name',
|
|
52
|
+
not: 'a value',
|
|
53
|
+
'typeof': 'a value',
|
|
54
|
+
thanos: 'the property to delete',
|
|
55
|
+
'delete': 'the property to delete',
|
|
56
|
+
panic: 'an error or a message',
|
|
57
|
+
waitForIt: 'a promise or a call',
|
|
58
|
+
grab: 'a name or a module in quotes',
|
|
59
|
+
share: 'a value or a {list of names}',
|
|
60
|
+
consider: 'the value to compare',
|
|
61
|
+
'switch': 'the value to compare',
|
|
62
|
+
forEvery: 'the loop variables',
|
|
63
|
+
'for': 'the loop variables',
|
|
64
|
+
inheritsFrom: 'the parent blueprint',
|
|
65
|
+
'extends': 'the parent class',
|
|
66
|
+
blueprint: 'a name',
|
|
67
|
+
'class': 'a name',
|
|
68
|
+
struct: 'a name',
|
|
69
|
+
module: 'a name',
|
|
70
|
+
attempt: 'an indented block',
|
|
71
|
+
'try': 'an indented block',
|
|
72
|
+
onFail: 'an indented block',
|
|
73
|
+
'catch': 'an indented block',
|
|
74
|
+
regardless: 'an indented block',
|
|
75
|
+
'finally': 'an indented block'
|
|
76
|
+
};
|
|
77
|
+
JS_HINTS = {
|
|
78
|
+
'import': {
|
|
79
|
+
why: 'import is how JavaScript loads a module; MambaScript spells it grab.',
|
|
80
|
+
fix: "grab express from 'express' for a default export, grab {join} from 'path' for named exports, grab * as fs from 'fs' for a namespace."
|
|
81
|
+
},
|
|
82
|
+
'export': {
|
|
83
|
+
why: 'export is how JavaScript publishes from a module; MambaScript spells it share.',
|
|
84
|
+
fix: 'share {a, b} publishes names; share Thing alone is the default export.'
|
|
85
|
+
},
|
|
86
|
+
'function': {
|
|
87
|
+
why: 'MambaScript has no function keyword; a function is an arrow.',
|
|
88
|
+
fix: 'write (a, b) -> a + b, or name it with greet = (name) -> "Hi #{name}".'
|
|
89
|
+
},
|
|
90
|
+
'var': {
|
|
91
|
+
why: 'MambaScript declares variables for you the first time you assign them.',
|
|
92
|
+
fix: 'drop the keyword: x = 1.'
|
|
93
|
+
},
|
|
94
|
+
'let': {
|
|
95
|
+
why: 'MambaScript declares variables for you the first time you assign them.',
|
|
96
|
+
fix: 'drop the keyword: x = 1.'
|
|
97
|
+
},
|
|
98
|
+
'const': {
|
|
99
|
+
why: 'MambaScript declares variables for you the first time you assign them; there is no const.',
|
|
100
|
+
fix: 'drop the keyword: x = 1 (and do not reassign it).'
|
|
101
|
+
},
|
|
102
|
+
'switch': {
|
|
103
|
+
why: 'switch is spelled consider in MambaScript (switch still works as an alias when it starts a statement).',
|
|
104
|
+
fix: 'consider value, then indented when branches and otherwise.'
|
|
105
|
+
},
|
|
106
|
+
'case': {
|
|
107
|
+
why: 'A branch of consider is written with when.',
|
|
108
|
+
fix: "when 'a' followed by the branch body."
|
|
109
|
+
},
|
|
110
|
+
'default': {
|
|
111
|
+
why: 'The fallback branch of consider is written otherwise.',
|
|
112
|
+
fix: 'otherwise followed by the branch body.'
|
|
113
|
+
},
|
|
114
|
+
'static': {
|
|
115
|
+
why: 'Static members are written with @ inside a blueprint.',
|
|
116
|
+
fix: '@count = 0 for a static property, @create: -> for a static method.'
|
|
117
|
+
},
|
|
118
|
+
'void': {
|
|
119
|
+
why: 'There is no void operator; use missing (undefined).',
|
|
120
|
+
fix: 'write missing where you had void 0.'
|
|
121
|
+
},
|
|
122
|
+
'with': {
|
|
123
|
+
why: 'with is not supported.',
|
|
124
|
+
fix: 'read the properties you need into variables, or use destructuring: {a, b} = obj.'
|
|
125
|
+
},
|
|
126
|
+
'enum': {
|
|
127
|
+
why: 'enum is not supported.',
|
|
128
|
+
fix: 'use a struct or an object of constants: Color = {red: 0, green: 1}.'
|
|
129
|
+
},
|
|
130
|
+
'interface': {
|
|
131
|
+
why: 'An interface is written as a struct.',
|
|
132
|
+
fix: 'struct Point then indented name :: Type members; a blueprint can then implements Point.'
|
|
133
|
+
},
|
|
134
|
+
'typeof': {
|
|
135
|
+
why: 'typeof works in MambaScript, but not as a bare statement.',
|
|
136
|
+
fix: 'use it in an expression: kind = typeof value.'
|
|
137
|
+
},
|
|
138
|
+
'null': {
|
|
139
|
+
why: 'null is a value, not a statement.',
|
|
140
|
+
fix: 'use it in an expression, or write nothing.'
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
CAN_START = new Uint8Array(KIND_NAMES.length);
|
|
144
|
+
CAN_START_KINDS = [
|
|
145
|
+
T.IDENT,
|
|
146
|
+
T.MACRO,
|
|
147
|
+
T.NUMBER_INT,
|
|
148
|
+
T.NUMBER_FLOAT,
|
|
149
|
+
T.NUMBER_BIGINT,
|
|
150
|
+
T.STRING_START,
|
|
151
|
+
T.REGEX,
|
|
152
|
+
T.HEREGEX_START,
|
|
153
|
+
T.JSLITERAL,
|
|
154
|
+
T.TRUE,
|
|
155
|
+
T.FALSE,
|
|
156
|
+
T.NULL,
|
|
157
|
+
T.UNDEFINED,
|
|
158
|
+
T.THIS,
|
|
159
|
+
T.AT,
|
|
160
|
+
T.SUPER,
|
|
161
|
+
T.NEW,
|
|
162
|
+
T.DO,
|
|
163
|
+
T.TYPEOF,
|
|
164
|
+
T.DELETE,
|
|
165
|
+
T.WAITFORIT,
|
|
166
|
+
T.WAITFORALL,
|
|
167
|
+
T.YIELD,
|
|
168
|
+
T.NOT,
|
|
169
|
+
T.BANG,
|
|
170
|
+
T.TILDE,
|
|
171
|
+
T.PLUSPLUS,
|
|
172
|
+
T.MINUSMINUS,
|
|
173
|
+
T.PLUS,
|
|
174
|
+
T.MINUS,
|
|
175
|
+
T.LPAREN,
|
|
176
|
+
T.LBRACKET,
|
|
177
|
+
T.LBRACE,
|
|
178
|
+
T.ARROW,
|
|
179
|
+
T.FAT_ARROW,
|
|
180
|
+
T.DCOLON,
|
|
181
|
+
T.IF,
|
|
182
|
+
T.UNLESS,
|
|
183
|
+
T.WHILE,
|
|
184
|
+
T.UNTIL,
|
|
185
|
+
T.LOOP,
|
|
186
|
+
T.TRY,
|
|
187
|
+
T.FOR,
|
|
188
|
+
T.FOREVERY,
|
|
189
|
+
T.SWITCH,
|
|
190
|
+
T.CLASS,
|
|
191
|
+
T.BLUEPRINT,
|
|
192
|
+
T.RESERVED
|
|
193
|
+
];
|
|
194
|
+
for (var i$ = 0, length$ = CAN_START_KINDS.length; i$ < length$; ++i$) {
|
|
195
|
+
kindId = CAN_START_KINDS[i$];
|
|
196
|
+
CAN_START[kindId] = 1;
|
|
197
|
+
}
|
|
198
|
+
CAN_START_STATEMENT = new Uint8Array(KIND_NAMES.length);
|
|
199
|
+
for (var cache$1 = [
|
|
200
|
+
T.RETURN,
|
|
201
|
+
T.BREAK,
|
|
202
|
+
T.CONTINUE,
|
|
203
|
+
T.THROW,
|
|
204
|
+
T.PANIC,
|
|
205
|
+
T.DEBUGGER,
|
|
206
|
+
T.GRAB,
|
|
207
|
+
T.SHARE,
|
|
208
|
+
T.STRUCT
|
|
209
|
+
], i$1 = 0, length$1 = cache$1.length; i$1 < length$1; ++i$1) {
|
|
210
|
+
kindId = cache$1[i$1];
|
|
211
|
+
CAN_START_STATEMENT[kindId] = 1;
|
|
212
|
+
}
|
|
213
|
+
CONTROL_FLOW = new Uint8Array(KIND_NAMES.length);
|
|
214
|
+
templateParts = function (pieces) {
|
|
215
|
+
var current, expressions, piece, quasis;
|
|
216
|
+
quasis = [];
|
|
217
|
+
expressions = [];
|
|
218
|
+
current = null;
|
|
219
|
+
for (var i$2 = 0, length$2 = pieces.length; i$2 < length$2; ++i$2) {
|
|
220
|
+
piece = pieces[i$2];
|
|
221
|
+
if (piece instanceof CS.String) {
|
|
222
|
+
if (null != current) {
|
|
223
|
+
current.data += piece.data;
|
|
224
|
+
} else {
|
|
225
|
+
current = piece;
|
|
226
|
+
}
|
|
227
|
+
} else {
|
|
228
|
+
quasis.push(null != current ? current : new CS.String('').g());
|
|
229
|
+
expressions.push(null != piece.expr ? piece.expr : piece);
|
|
230
|
+
current = null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
quasis.push(null != current ? current : new CS.String('').g());
|
|
234
|
+
return {
|
|
235
|
+
quasis: quasis,
|
|
236
|
+
expressions: expressions
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
for (var cache$2 = [
|
|
240
|
+
T.IF,
|
|
241
|
+
T.UNLESS,
|
|
242
|
+
T.WHILE,
|
|
243
|
+
T.UNTIL,
|
|
244
|
+
T.LOOP,
|
|
245
|
+
T.FOR,
|
|
246
|
+
T.FOREVERY,
|
|
247
|
+
T.SWITCH
|
|
248
|
+
], i$2 = 0, length$2 = cache$2.length; i$2 < length$2; ++i$2) {
|
|
249
|
+
kindId = cache$2[i$2];
|
|
250
|
+
CONTROL_FLOW[kindId] = 1;
|
|
251
|
+
}
|
|
252
|
+
MEMBER_ACCESS = [
|
|
253
|
+
CS.MemberAccessOp,
|
|
254
|
+
CS.ProtoMemberAccessOp,
|
|
255
|
+
CS.SoakedMemberAccessOp,
|
|
256
|
+
CS.SoakedProtoMemberAccessOp,
|
|
257
|
+
CS.DynamicMemberAccessOp,
|
|
258
|
+
CS.DynamicProtoMemberAccessOp,
|
|
259
|
+
CS.SoakedDynamicMemberAccessOp,
|
|
260
|
+
CS.SoakedDynamicProtoMemberAccessOp
|
|
261
|
+
];
|
|
262
|
+
binaryNode = function (op, left, right) {
|
|
263
|
+
if (op.length > 4 && op.slice(0, 4) === 'not ')
|
|
264
|
+
return new CS.LogicalNotOp(binaryNode(op.slice(4), left, right));
|
|
265
|
+
if (op === 'has')
|
|
266
|
+
return new CS.OfOp(right, left);
|
|
267
|
+
return new CS[BINARY_OP_CLASS[op]](left, right);
|
|
268
|
+
};
|
|
269
|
+
isChainable = function (op) {
|
|
270
|
+
return CHAINABLE.indexOf(op) >= 0;
|
|
271
|
+
};
|
|
272
|
+
isLeftAssoc = function (op) {
|
|
273
|
+
return null != PRECEDENCE[op] && RIGHT_ASSOC.indexOf(op) < 0;
|
|
274
|
+
};
|
|
275
|
+
foldBinaryExpr = function (parts, ignoreChains) {
|
|
276
|
+
var chainStack, expr, leftOperand, nextOp, nextPrec, op, operator, prec, reduce, rightOperand, stack;
|
|
277
|
+
if (parts.length < 3)
|
|
278
|
+
return parts[0];
|
|
279
|
+
stack = parts.slice(0, 3);
|
|
280
|
+
parts = parts.slice(3);
|
|
281
|
+
while (parts.length > 0) {
|
|
282
|
+
nextOp = parts[0];
|
|
283
|
+
if (!ignoreChains && stack.length > 2) {
|
|
284
|
+
operator = stack[stack.length - 2];
|
|
285
|
+
if (isChainable(operator) && isChainable(nextOp)) {
|
|
286
|
+
chainStack = stack.slice(-3);
|
|
287
|
+
stack = stack.slice(0, stack.length - 3);
|
|
288
|
+
nextPrec = 0;
|
|
289
|
+
prec = 0;
|
|
290
|
+
while (true) {
|
|
291
|
+
operator = nextOp;
|
|
292
|
+
chainStack.push(parts.shift(), parts.shift());
|
|
293
|
+
nextOp = parts[0];
|
|
294
|
+
if (null != nextOp) {
|
|
295
|
+
nextPrec = PRECEDENCE[nextOp];
|
|
296
|
+
prec = PRECEDENCE[operator];
|
|
297
|
+
}
|
|
298
|
+
if (!(null != nextOp && (nextPrec > prec || isChainable(nextOp))))
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
stack.push(new CS.ChainedComparisonOp(foldBinaryExpr(chainStack, true)));
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
while (true) {
|
|
306
|
+
if (!(stack.length > 2))
|
|
307
|
+
break;
|
|
308
|
+
operator = stack[stack.length - 2];
|
|
309
|
+
prec = PRECEDENCE[operator];
|
|
310
|
+
nextPrec = PRECEDENCE[nextOp];
|
|
311
|
+
reduce = nextPrec < prec || isChainable(operator) && isChainable(nextOp) || nextPrec === prec && isLeftAssoc(operator);
|
|
312
|
+
if (!reduce)
|
|
313
|
+
break;
|
|
314
|
+
rightOperand = stack.pop();
|
|
315
|
+
stack.pop();
|
|
316
|
+
leftOperand = stack.pop();
|
|
317
|
+
stack.push(binaryNode(operator, leftOperand, rightOperand));
|
|
318
|
+
}
|
|
319
|
+
stack.push(parts.shift());
|
|
320
|
+
stack.push(parts.shift());
|
|
321
|
+
}
|
|
322
|
+
expr = stack.pop();
|
|
323
|
+
while (stack.length > 0) {
|
|
324
|
+
op = stack.pop();
|
|
325
|
+
expr = binaryNode(op, stack.pop(), expr);
|
|
326
|
+
}
|
|
327
|
+
return expr;
|
|
328
|
+
};
|
|
329
|
+
copyPosition = function (to, from) {
|
|
330
|
+
to.line = from.line;
|
|
331
|
+
to.column = from.column;
|
|
332
|
+
to.offset = from.offset;
|
|
333
|
+
return to;
|
|
334
|
+
};
|
|
335
|
+
createInterpolationIn = function (es, raw) {
|
|
336
|
+
var init, left, memo, merged, s;
|
|
337
|
+
init = new CS.String('').g();
|
|
338
|
+
memo = init;
|
|
339
|
+
for (var i$3 = 0, length$3 = es.length; i$3 < length$3; ++i$3) {
|
|
340
|
+
s = es[i$3];
|
|
341
|
+
if (s instanceof CS.String) {
|
|
342
|
+
left = memo;
|
|
343
|
+
merged = false;
|
|
344
|
+
while (left) {
|
|
345
|
+
if (left instanceof CS.String) {
|
|
346
|
+
if (left === init) {
|
|
347
|
+
if (raw)
|
|
348
|
+
copyPosition(left, s);
|
|
349
|
+
delete left.generated;
|
|
350
|
+
}
|
|
351
|
+
left.data = left.data + s.data;
|
|
352
|
+
merged = true;
|
|
353
|
+
break;
|
|
354
|
+
} else if (left instanceof CS.ConcatOp) {
|
|
355
|
+
left = left.right;
|
|
356
|
+
} else {
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (merged)
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
memo = new CS.ConcatOp(memo, s);
|
|
364
|
+
}
|
|
365
|
+
return memo;
|
|
366
|
+
};
|
|
367
|
+
stripLeadingWhitespace = function (str) {
|
|
368
|
+
var candidate, indent, match, matchStr;
|
|
369
|
+
str = str.replace(/\s+$/, '');
|
|
370
|
+
indent = null;
|
|
371
|
+
matchStr = str;
|
|
372
|
+
while (match = /\n+([^\n\S]*)/.exec(matchStr)) {
|
|
373
|
+
candidate = match[1];
|
|
374
|
+
matchStr = matchStr.slice(match.index + match[0].length);
|
|
375
|
+
if (!(null != indent) || 0 < candidate.length && candidate.length < indent.length)
|
|
376
|
+
indent = candidate;
|
|
377
|
+
}
|
|
378
|
+
if (indent)
|
|
379
|
+
str = str.replace(new RegExp('\\n' + indent, 'g'), '\n');
|
|
380
|
+
str = str.replace(/^\n/, '');
|
|
381
|
+
return str;
|
|
382
|
+
};
|
|
383
|
+
stripLeadingWhitespaceInterpolation = function (pieces) {
|
|
384
|
+
var candidate, i, indent, l, match, matchStr, piece;
|
|
385
|
+
indent = null;
|
|
386
|
+
l = pieces.length;
|
|
387
|
+
i = 0;
|
|
388
|
+
while (i < l) {
|
|
389
|
+
piece = pieces[i];
|
|
390
|
+
if (piece instanceof CS.String) {
|
|
391
|
+
if (i === l - 1)
|
|
392
|
+
piece.data = piece.data.replace(/\s+$/, '');
|
|
393
|
+
matchStr = piece.data;
|
|
394
|
+
while (match = /\n+([^\n\S]*)/.exec(matchStr)) {
|
|
395
|
+
candidate = match[1];
|
|
396
|
+
matchStr = matchStr.slice(match.index + match[0].length);
|
|
397
|
+
if (!(null != indent) || 0 < candidate.length && candidate.length < indent.length)
|
|
398
|
+
indent = candidate;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
i++;
|
|
402
|
+
}
|
|
403
|
+
if (indent) {
|
|
404
|
+
i = 0;
|
|
405
|
+
while (i < l) {
|
|
406
|
+
piece = pieces[i];
|
|
407
|
+
if (piece instanceof CS.String) {
|
|
408
|
+
piece.data = piece.data.replace(new RegExp('\\n' + indent, 'g'), '\n');
|
|
409
|
+
if (i === l - 1)
|
|
410
|
+
piece.data = piece.data.replace(/(\n\s*)+$/, '');
|
|
411
|
+
if (i === 0)
|
|
412
|
+
piece.data = piece.data.replace(/^\n/, '');
|
|
413
|
+
}
|
|
414
|
+
i++;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return pieces;
|
|
418
|
+
};
|
|
419
|
+
typeIdentifierNode = function (identifier) {
|
|
420
|
+
var node;
|
|
421
|
+
node = new CS.Int(0);
|
|
422
|
+
node.identifier = identifier;
|
|
423
|
+
node.nodeType = 'identifier';
|
|
424
|
+
return node;
|
|
425
|
+
};
|
|
426
|
+
Parser = function () {
|
|
427
|
+
function Parser(src, options) {
|
|
428
|
+
var lx;
|
|
429
|
+
this.src = src;
|
|
430
|
+
this.options = options || {};
|
|
431
|
+
this.raw = !!this.options.raw;
|
|
432
|
+
lx = lexer.tokenize(src, this.options);
|
|
433
|
+
this.kinds = lx.kinds;
|
|
434
|
+
this.starts = lx.starts;
|
|
435
|
+
this.ends = lx.ends;
|
|
436
|
+
this.lines = lx.lines;
|
|
437
|
+
this.cols = lx.cols;
|
|
438
|
+
this.flags = lx.flags;
|
|
439
|
+
this.pair = lx.pair;
|
|
440
|
+
this.vals = lx.vals;
|
|
441
|
+
this.n = lx.n;
|
|
442
|
+
this.i = 0;
|
|
443
|
+
this.lastEnd = 0;
|
|
444
|
+
this.splitGt = 0;
|
|
445
|
+
}
|
|
446
|
+
Parser.prototype.kind = function () {
|
|
447
|
+
return this.kinds[this.i];
|
|
448
|
+
};
|
|
449
|
+
Parser.prototype.kindAt = function (t) {
|
|
450
|
+
if (t < this.n) {
|
|
451
|
+
return this.kinds[t];
|
|
452
|
+
} else {
|
|
453
|
+
return EOF;
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
Parser.prototype.at = function (k) {
|
|
457
|
+
return this.kinds[this.i] === k;
|
|
458
|
+
};
|
|
459
|
+
Parser.prototype.text = function (t) {
|
|
460
|
+
return this.src.slice(this.starts[t], this.ends[t]);
|
|
461
|
+
};
|
|
462
|
+
Parser.prototype.spaceBefore = function (t) {
|
|
463
|
+
return (this.flags[t] & FLAG_SPACE_BEFORE) !== 0;
|
|
464
|
+
};
|
|
465
|
+
Parser.prototype.spaceAfter = function (t) {
|
|
466
|
+
var k;
|
|
467
|
+
k = this.kindAt(t + 1);
|
|
468
|
+
return k === NEWLINE || k === EOF || k === INDENT || k === DEDENT || this.spaceBefore(t + 1);
|
|
469
|
+
};
|
|
470
|
+
Parser.prototype.next = function () {
|
|
471
|
+
var t;
|
|
472
|
+
t = this.i;
|
|
473
|
+
this.lastEnd = this.ends[t];
|
|
474
|
+
this.i = t + 1;
|
|
475
|
+
return t;
|
|
476
|
+
};
|
|
477
|
+
Parser.prototype.skip = function () {
|
|
478
|
+
return this.i++;
|
|
479
|
+
};
|
|
480
|
+
Parser.prototype.skipNewline = function () {
|
|
481
|
+
if (this.kinds[this.i] === NEWLINE)
|
|
482
|
+
this.i++;
|
|
483
|
+
};
|
|
484
|
+
Parser.prototype.expect = function (k, what) {
|
|
485
|
+
if (!(this.kinds[this.i] === k))
|
|
486
|
+
this.fail('expected ' + what + ', found ' + this.describe(this.i), null, {
|
|
487
|
+
code: 'MS1103',
|
|
488
|
+
why: 'The parser needed ' + what + ' to continue the construct it was reading and found ' + this.describe(this.i) + ' instead.',
|
|
489
|
+
fix: 'check the construct around this point: a missing bracket, comma or keyword usually explains it.'
|
|
490
|
+
});
|
|
491
|
+
return this.next();
|
|
492
|
+
};
|
|
493
|
+
Parser.prototype.unexpected = function (t) {
|
|
494
|
+
var hint, k, needs, op, p, pk, text, word;
|
|
495
|
+
if (null == t)
|
|
496
|
+
t = this.i;
|
|
497
|
+
k = this.kindAt(t);
|
|
498
|
+
if (null != this.furthest && (this.furthestMark <= this.starts[t] && this.starts[t] <= this.furthest.offset))
|
|
499
|
+
throw this.furthest;
|
|
500
|
+
if (k === IDENT && t > 0 && !this.spaceBefore(t)) {
|
|
501
|
+
pk = this.kindAt(t - 1);
|
|
502
|
+
if (pk === T.REGEX)
|
|
503
|
+
this.fail("'" + this.text(t) + "' is not a regular expression flag", t, {
|
|
504
|
+
code: 'MS1017',
|
|
505
|
+
why: 'The flags after a regular expression are chosen from g, i, m, s, u, y, d and v.',
|
|
506
|
+
fix: "remove '" + this.text(t) + "', or put a space before it if it is code."
|
|
507
|
+
});
|
|
508
|
+
if (pk === T.NUMBER_INT || pk === T.NUMBER_FLOAT || pk === T.NUMBER_BIGINT)
|
|
509
|
+
this.fail("'" + this.text(t - 1) + this.text(t) + "' is not a valid number", t - 1, {
|
|
510
|
+
code: 'MS1002',
|
|
511
|
+
endColumn: this.cols[t] + (this.ends[t] - this.starts[t]),
|
|
512
|
+
why: 'A number is digits with an optional decimal point or exponent (1.5e3), or 0x, 0b, 0o followed by digits of that base.',
|
|
513
|
+
fix: 'fix the digits, or put a space between the number and the word after it.'
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
if (k === T.COLON && t > 0 && this.kindAt(t - 1) === T.RBRACKET)
|
|
517
|
+
this.fail('a computed key needs braces around the object', t, {
|
|
518
|
+
code: 'MS1129',
|
|
519
|
+
why: 'Without braces an object literal is told apart from other arguments by its plain keys; a [computed] key cannot be, so the brackets were read as an array.',
|
|
520
|
+
fix: 'wrap the object in braces: f {a: 1, [key]: value}.'
|
|
521
|
+
});
|
|
522
|
+
if (t > 0 && k !== EOF && k !== NEWLINE && k !== DEDENT && k !== INDENT && !this.canStartExpression(t)) {
|
|
523
|
+
pk = this.kindAt(t - 1);
|
|
524
|
+
if (isWordKind(pk) && null != (needs = NEEDS_MORE[this.text(t - 1)])) {
|
|
525
|
+
word = this.text(t - 1);
|
|
526
|
+
this.fail('expected ' + needs + ' after ' + word + ', found ' + this.describe(t), t, {
|
|
527
|
+
code: 'MS1107',
|
|
528
|
+
why: '' + word + ' must be followed by ' + needs + '.',
|
|
529
|
+
fix: 'add ' + needs + ' after ' + word + '.'
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (k === EOF || k === NEWLINE || k === DEDENT || k === INDENT) {
|
|
534
|
+
p = t - 1;
|
|
535
|
+
while (p > 0 && (this.kindAt(p) === NEWLINE || this.kindAt(p) === DEDENT || this.kindAt(p) === INDENT)) {
|
|
536
|
+
p--;
|
|
537
|
+
}
|
|
538
|
+
pk = this.kindAt(p);
|
|
539
|
+
op = BINARY_OP_OF_KIND[pk];
|
|
540
|
+
if (null != op)
|
|
541
|
+
this.fail('the right side of ' + op + ' is missing', p, {
|
|
542
|
+
code: 'MS1107',
|
|
543
|
+
why: '' + op + ' needs a value on both sides; the line ends right after it.',
|
|
544
|
+
fix: 'add the right operand after ' + op + ', or remove the operator.'
|
|
545
|
+
});
|
|
546
|
+
if (pk === T.ASSIGN)
|
|
547
|
+
this.fail('the value after = is missing', p, {
|
|
548
|
+
code: 'MS1107',
|
|
549
|
+
why: 'An assignment needs a value after the =.',
|
|
550
|
+
fix: 'add the value after =, or put it on the next line, indented.'
|
|
551
|
+
});
|
|
552
|
+
if (isWordKind(pk) && null != (needs = NEEDS_MORE[this.text(p)])) {
|
|
553
|
+
word = this.text(p);
|
|
554
|
+
this.fail('expected ' + needs + ' after ' + word, p, {
|
|
555
|
+
code: 'MS1107',
|
|
556
|
+
why: '' + word + ' is not complete on its own; the line ends right after it.',
|
|
557
|
+
fix: 'add ' + needs + ' after ' + word + '.'
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
switch (k) {
|
|
562
|
+
case INDENT:
|
|
563
|
+
return this.fail('this line is indented but nothing opened a block', t, {
|
|
564
|
+
code: 'MS1101',
|
|
565
|
+
why: 'Only a line that opens a block (a function body, if, forEvery, blueprint, an object literal, \u2026) may be followed by a deeper indentation. The line above this one does not.',
|
|
566
|
+
fix: 'outdent this line to match the line above, or end the line above with something that opens a block (->, then, :).'
|
|
567
|
+
});
|
|
568
|
+
case EOF:
|
|
569
|
+
return this.fail('the file ended before this statement was complete', t, {
|
|
570
|
+
code: 'MS1102',
|
|
571
|
+
why: 'The last construct in the file is missing its end: an operand, a closing bracket, a block body or a quote.',
|
|
572
|
+
fix: 'finish the last line, or remove the unfinished part.'
|
|
573
|
+
});
|
|
574
|
+
case NEWLINE:
|
|
575
|
+
return this.fail('the line ended before this statement was complete', t, {
|
|
576
|
+
code: 'MS1107',
|
|
577
|
+
why: 'Something on this line expects more input: an operator with no right side, or a construct that is missing its end.',
|
|
578
|
+
fix: 'finish the expression on this line, or continue it on the next line at the same indentation.'
|
|
579
|
+
});
|
|
580
|
+
case DEDENT:
|
|
581
|
+
return this.fail('the block ended before this statement was complete', t, {
|
|
582
|
+
code: 'MS1102',
|
|
583
|
+
why: 'The last line of the block expects more input: an operator with no right side, or a construct that is missing its end.',
|
|
584
|
+
fix: 'finish the last line of the block.'
|
|
585
|
+
});
|
|
586
|
+
default: {
|
|
587
|
+
text = this.text(t);
|
|
588
|
+
hint = JS_HINTS[text];
|
|
589
|
+
if (text === '=' && t > 0 && !this.spaceBefore(t) && in$(this.text(t - 1), [
|
|
590
|
+
'==',
|
|
591
|
+
'!='
|
|
592
|
+
])) {
|
|
593
|
+
word = this.text(t - 1) === '==' ? 'is' : 'isnt';
|
|
594
|
+
this.fail('' + this.text(t - 1) + '= is JavaScript; MambaScript spells it ' + word, t - 1, {
|
|
595
|
+
code: 'MS1124',
|
|
596
|
+
endColumn: this.cols[t] + 1,
|
|
597
|
+
why: 'Every comparison in MambaScript is strict, so there is no separate === spelling: ' + word + ' (and the two-character ' + this.text(t - 1) + ') compile to ' + this.text(t - 1) + '=.',
|
|
598
|
+
fix: 'write a ' + word + ' b.'
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
if (null != hint) {
|
|
602
|
+
return this.fail('' + text + ' is JavaScript; MambaScript spells it differently', t, {
|
|
603
|
+
code: 'MS1124',
|
|
604
|
+
why: hint.why,
|
|
605
|
+
fix: hint.fix
|
|
606
|
+
});
|
|
607
|
+
} else if (k !== IDENT && isWordKind(k)) {
|
|
608
|
+
return this.fail('' + text + ' cannot be used here', t, {
|
|
609
|
+
code: 'MS1124',
|
|
610
|
+
why: '' + text + ' is a keyword: it starts a statement and does not produce a value, so it cannot appear in the middle of an expression.',
|
|
611
|
+
fix: 'put ' + text + ' at the start of its own line, or choose a different name if you meant a variable.'
|
|
612
|
+
});
|
|
613
|
+
} else {
|
|
614
|
+
return this.fail('unexpected ' + this.describe(t), t, {
|
|
615
|
+
code: 'MS1125',
|
|
616
|
+
why: 'This token cannot start or continue a statement here.',
|
|
617
|
+
fix: 'check the line above for an unfinished expression: an operator at the end of a line, or an unclosed bracket or string.'
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
Parser.prototype.expectDedent = function () {
|
|
624
|
+
this.skipNewline();
|
|
625
|
+
if (!this.at(DEDENT))
|
|
626
|
+
this.fail('expected the indented block to end here, found ' + this.describe(this.i), null, {
|
|
627
|
+
code: 'MS1103',
|
|
628
|
+
why: 'A block ends when a line is indented less than its first line; this token cannot continue the block.',
|
|
629
|
+
fix: 'check the indentation of this line against the first line of the block.'
|
|
630
|
+
});
|
|
631
|
+
this.lastEnd = this.starts[this.i];
|
|
632
|
+
return this.skip();
|
|
633
|
+
};
|
|
634
|
+
Parser.prototype.describe = function (t) {
|
|
635
|
+
switch (this.kindAt(t)) {
|
|
636
|
+
case EOF:
|
|
637
|
+
return 'end of input';
|
|
638
|
+
case NEWLINE:
|
|
639
|
+
return 'newline';
|
|
640
|
+
case INDENT:
|
|
641
|
+
return 'indentation';
|
|
642
|
+
case DEDENT:
|
|
643
|
+
return 'the end of the indented block';
|
|
644
|
+
case T.STRING_START:
|
|
645
|
+
case T.STRING_PIECE:
|
|
646
|
+
case T.STRING_END:
|
|
647
|
+
return 'a string';
|
|
648
|
+
default:
|
|
649
|
+
return "'" + this.text(t) + "'";
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
Parser.prototype.fail = function (message, t, extra) {
|
|
653
|
+
var found, k, node, o;
|
|
654
|
+
if (null == extra)
|
|
655
|
+
extra = {};
|
|
656
|
+
if (!(null != t))
|
|
657
|
+
t = this.i;
|
|
658
|
+
if (t >= this.n)
|
|
659
|
+
t = this.n - 1;
|
|
660
|
+
if (null != this.furthest && (this.furthestMark <= this.starts[t] && this.starts[t] < this.furthest.offset))
|
|
661
|
+
throw this.furthest;
|
|
662
|
+
k = this.kindAt(t);
|
|
663
|
+
found = function () {
|
|
664
|
+
switch (k) {
|
|
665
|
+
case EOF:
|
|
666
|
+
return null;
|
|
667
|
+
case NEWLINE:
|
|
668
|
+
return '\n';
|
|
669
|
+
case INDENT:
|
|
670
|
+
return '(INDENT)';
|
|
671
|
+
case DEDENT:
|
|
672
|
+
return '(DEDENT)';
|
|
673
|
+
default:
|
|
674
|
+
return this.text(t);
|
|
675
|
+
}
|
|
676
|
+
}.call(this);
|
|
677
|
+
if (null != extra.atNode) {
|
|
678
|
+
node = extra.atNode;
|
|
679
|
+
delete extra.atNode;
|
|
680
|
+
if (null != node.line && null != node.column) {
|
|
681
|
+
if (null != extra.endColumn)
|
|
682
|
+
extra.endColumn;
|
|
683
|
+
else
|
|
684
|
+
extra.endColumn = node.column + (typeof node.raw === 'string' && node.raw.indexOf('\n') < 0 ? Math.max(1, node.raw.length) : 1);
|
|
685
|
+
throw new MambaSyntaxError(message, node.line, node.column, null != node.offset ? node.offset : this.starts[t], found, extra);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if (!(null != extra.endColumn))
|
|
689
|
+
extra.endColumn = k === EOF || k === NEWLINE || k === INDENT || k === DEDENT ? this.cols[t] + 1 : this.cols[t] + Math.max(1, this.ends[t] - this.starts[t]);
|
|
690
|
+
if (null != extra.opener) {
|
|
691
|
+
o = extra.opener;
|
|
692
|
+
extra.related = [{
|
|
693
|
+
line: this.lines[o],
|
|
694
|
+
column: this.cols[o],
|
|
695
|
+
endColumn: this.cols[o] + Math.max(1, this.ends[o] - this.starts[o]),
|
|
696
|
+
label: null != extra.openerLabel ? extra.openerLabel : 'opened here'
|
|
697
|
+
}];
|
|
698
|
+
delete extra.opener;
|
|
699
|
+
delete extra.openerLabel;
|
|
700
|
+
}
|
|
701
|
+
throw new MambaSyntaxError(message, this.lines[t], this.cols[t], this.starts[t], found, extra);
|
|
702
|
+
};
|
|
703
|
+
Parser.prototype.rp = function (node, t) {
|
|
704
|
+
if (this.raw) {
|
|
705
|
+
node.line = this.lines[t];
|
|
706
|
+
node.column = this.cols[t];
|
|
707
|
+
node.offset = this.starts[t];
|
|
708
|
+
node.raw = this.src.slice(this.starts[t], this.lastEnd);
|
|
709
|
+
}
|
|
710
|
+
return node;
|
|
711
|
+
};
|
|
712
|
+
Parser.prototype.p = function (node, t) {
|
|
713
|
+
if (this.raw) {
|
|
714
|
+
node.line = this.lines[t];
|
|
715
|
+
node.column = this.cols[t];
|
|
716
|
+
node.offset = this.starts[t];
|
|
717
|
+
}
|
|
718
|
+
return node;
|
|
719
|
+
};
|
|
720
|
+
Parser.prototype.r = function (node, t) {
|
|
721
|
+
if (this.raw)
|
|
722
|
+
node.raw = this.src.slice(this.starts[t], this.lastEnd);
|
|
723
|
+
return node;
|
|
724
|
+
};
|
|
725
|
+
Parser.prototype.c = function (to, from) {
|
|
726
|
+
if (this.raw)
|
|
727
|
+
copyPosition(to, from);
|
|
728
|
+
return to;
|
|
729
|
+
};
|
|
730
|
+
Parser.prototype.speculate = function (fn) {
|
|
731
|
+
var e, le, mark, sg;
|
|
732
|
+
mark = this.i;
|
|
733
|
+
le = this.lastEnd;
|
|
734
|
+
sg = this.splitGt;
|
|
735
|
+
try {
|
|
736
|
+
return fn();
|
|
737
|
+
} catch (e$) {
|
|
738
|
+
e = e$;
|
|
739
|
+
if (!(e instanceof MambaSyntaxError))
|
|
740
|
+
throw e;
|
|
741
|
+
if (null != e.offset && e.offset > this.starts[mark] && (!(null != this.furthest) || e.offset > this.furthest.offset)) {
|
|
742
|
+
this.furthest = e;
|
|
743
|
+
this.furthestMark = this.starts[mark];
|
|
744
|
+
}
|
|
745
|
+
this.i = mark;
|
|
746
|
+
this.lastEnd = le;
|
|
747
|
+
this.splitGt = sg;
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
Parser.prototype.isObjectKeyStart = function (t) {
|
|
752
|
+
var k;
|
|
753
|
+
k = this.kindAt(t);
|
|
754
|
+
if (isWordKind(k) || k === T.NUMBER_INT || k === T.NUMBER_FLOAT)
|
|
755
|
+
return this.kindAt(t + 1) === T.COLON;
|
|
756
|
+
if (k === T.STRING_START)
|
|
757
|
+
return this.kindAt(this.pair[t] + 1) === T.COLON;
|
|
758
|
+
return false;
|
|
759
|
+
};
|
|
760
|
+
Parser.prototype.isFunctionStart = function (t) {
|
|
761
|
+
var j, k, nk;
|
|
762
|
+
k = this.kindAt(t);
|
|
763
|
+
if (k === T.ARROW || k === T.FAT_ARROW || k === T.DCOLON)
|
|
764
|
+
return true;
|
|
765
|
+
if (k === T.LPAREN) {
|
|
766
|
+
j = this.pair[t];
|
|
767
|
+
if (j === 0)
|
|
768
|
+
return false;
|
|
769
|
+
nk = this.kindAt(j + 1);
|
|
770
|
+
return nk === T.ARROW || nk === T.FAT_ARROW || nk === T.DCOLON;
|
|
771
|
+
}
|
|
772
|
+
return false;
|
|
773
|
+
};
|
|
774
|
+
Parser.prototype.startsExpressionworthy = function (t) {
|
|
775
|
+
var k;
|
|
776
|
+
if (this.isObjectKeyStart(t))
|
|
777
|
+
return true;
|
|
778
|
+
k = this.kindAt(t);
|
|
779
|
+
if (CONTROL_FLOW[k] === 1 || k === T.TRY || k === T.CLASS || k === T.BLUEPRINT)
|
|
780
|
+
return true;
|
|
781
|
+
return this.isFunctionStart(t);
|
|
782
|
+
};
|
|
783
|
+
Parser.prototype.parseExpressionworthyOrNull = function (noObj) {
|
|
784
|
+
var k;
|
|
785
|
+
k = this.kind();
|
|
786
|
+
if ((k === T.LPAREN || CONTROL_FLOW[k] === 1) && !this.isObjectKeyStart(this.i))
|
|
787
|
+
return this.speculate(function (this$) {
|
|
788
|
+
return function () {
|
|
789
|
+
return this$.parseExpressionworthy(noObj);
|
|
790
|
+
};
|
|
791
|
+
}(this));
|
|
792
|
+
return this.parseExpressionworthy(noObj);
|
|
793
|
+
};
|
|
794
|
+
Parser.prototype.controlFlowArgPossible = function (t) {
|
|
795
|
+
var j, k, pr;
|
|
796
|
+
j = t + 1;
|
|
797
|
+
while (j < this.n) {
|
|
798
|
+
k = this.kinds[j];
|
|
799
|
+
if (k === T.THEN)
|
|
800
|
+
return true;
|
|
801
|
+
if (k === NEWLINE)
|
|
802
|
+
return (this.flags[j] & FLAG_NL_BEFORE_INDENT) !== 0;
|
|
803
|
+
if (k === EOF || k === T.RPAREN || k === T.RBRACKET || k === T.RBRACE || k === T.INTERP_END || k === DEDENT)
|
|
804
|
+
return false;
|
|
805
|
+
if (k === T.LPAREN || k === T.LBRACKET || k === T.LBRACE || k === T.STRING_START || k === T.HEREGEX_START) {
|
|
806
|
+
pr = this.pair[j];
|
|
807
|
+
if (pr === 0)
|
|
808
|
+
return false;
|
|
809
|
+
j = pr;
|
|
810
|
+
}
|
|
811
|
+
j++;
|
|
812
|
+
}
|
|
813
|
+
return false;
|
|
814
|
+
};
|
|
815
|
+
Parser.prototype.isKeywordObjectKey = function (t) {
|
|
816
|
+
var k;
|
|
817
|
+
k = this.kindAt(t);
|
|
818
|
+
return k !== IDENT && isWordKind(k) && this.kindAt(t + 1) === T.COLON;
|
|
819
|
+
};
|
|
820
|
+
Parser.prototype.canStartExpression = function (t) {
|
|
821
|
+
var k;
|
|
822
|
+
k = this.kindAt(t);
|
|
823
|
+
if (this.isKeywordObjectKey(t))
|
|
824
|
+
return true;
|
|
825
|
+
if (!(CAN_START[k] === 1))
|
|
826
|
+
return false;
|
|
827
|
+
if (CONTROL_FLOW[k] === 1)
|
|
828
|
+
return this.controlFlowArgPossible(t);
|
|
829
|
+
return true;
|
|
830
|
+
};
|
|
831
|
+
Parser.prototype.canStartImplicitArg = function (t, noObj) {
|
|
832
|
+
var k, nk;
|
|
833
|
+
if (!this.spaceBefore(t))
|
|
834
|
+
return false;
|
|
835
|
+
k = this.kindAt(t);
|
|
836
|
+
switch (k) {
|
|
837
|
+
case T.PLUS:
|
|
838
|
+
case T.MINUS:
|
|
839
|
+
return !this.spaceAfter(t);
|
|
840
|
+
break;
|
|
841
|
+
case T.SLASH:
|
|
842
|
+
case T.COMPOUND_ASSIGN:
|
|
843
|
+
case T.EXISTS_ASSIGN:
|
|
844
|
+
return false;
|
|
845
|
+
break;
|
|
846
|
+
case T.NOT:
|
|
847
|
+
nk = this.kindAt(t + 1);
|
|
848
|
+
return !(nk === T.IN || nk === T.OF || nk === T.INSTANCEOF || nk === T.AREYOUA);
|
|
849
|
+
break;
|
|
850
|
+
case T.DCOLON:
|
|
851
|
+
return false;
|
|
852
|
+
}
|
|
853
|
+
if (this.isKeywordObjectKey(t))
|
|
854
|
+
return !noObj;
|
|
855
|
+
if (!(CAN_START[k] === 1))
|
|
856
|
+
return false;
|
|
857
|
+
if (CONTROL_FLOW[k] === 1)
|
|
858
|
+
return this.controlFlowArgPossible(t);
|
|
859
|
+
return true;
|
|
860
|
+
};
|
|
861
|
+
Parser.prototype.parseProgram = function () {
|
|
862
|
+
var body, prog;
|
|
863
|
+
this.skipNewline();
|
|
864
|
+
if (this.at(EOF)) {
|
|
865
|
+
prog = new CS.Program(null);
|
|
866
|
+
} else {
|
|
867
|
+
body = this.parseBlock(true);
|
|
868
|
+
this.skipNewline();
|
|
869
|
+
if (!this.at(EOF))
|
|
870
|
+
this.unexpected(this.i);
|
|
871
|
+
prog = new CS.Program(body);
|
|
872
|
+
}
|
|
873
|
+
if (this.raw) {
|
|
874
|
+
prog.line = 1;
|
|
875
|
+
prog.column = 1;
|
|
876
|
+
prog.offset = 0;
|
|
877
|
+
prog.raw = this.src.slice(0, this.lastEnd);
|
|
878
|
+
}
|
|
879
|
+
return prog;
|
|
880
|
+
};
|
|
881
|
+
Parser.prototype.parseBlock = function (toplevel) {
|
|
882
|
+
var k, start, statements, word;
|
|
883
|
+
start = this.i;
|
|
884
|
+
statements = [];
|
|
885
|
+
while (true) {
|
|
886
|
+
k = this.kind();
|
|
887
|
+
if (toplevel && (k === T.RETURN || k === T.BREAK || k === T.CONTINUE)) {
|
|
888
|
+
word = this.text(this.i);
|
|
889
|
+
this.fail('' + word + ' is not allowed at the top level of a program', null, {
|
|
890
|
+
code: 'MS1104',
|
|
891
|
+
why: word === 'return' ? 'return leaves a function; there is no function to leave at the top level.' : '' + word + ' controls a loop; there is no loop here.',
|
|
892
|
+
fix: word === 'return' ? 'move it inside a function, or remove it.' : 'move it inside a forEvery, while, until or loop, or remove it.'
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
statements.push(this.parseStatement());
|
|
896
|
+
if (this.at(NEWLINE)) {
|
|
897
|
+
this.skip();
|
|
898
|
+
k = this.kind();
|
|
899
|
+
if (k === DEDENT || k === EOF || k === T.RPAREN || k === T.RBRACKET || k === T.RBRACE || k === T.INTERP_END)
|
|
900
|
+
break;
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
break;
|
|
904
|
+
}
|
|
905
|
+
return this.rp(new CS.Block(statements), start);
|
|
906
|
+
};
|
|
907
|
+
Parser.prototype.parseStatement = function () {
|
|
908
|
+
var k, node;
|
|
909
|
+
k = this.kind();
|
|
910
|
+
switch (k) {
|
|
911
|
+
case T.STRUCT:
|
|
912
|
+
return this.parseStructDef();
|
|
913
|
+
break;
|
|
914
|
+
case T.GRAB:
|
|
915
|
+
return this.parseGrab();
|
|
916
|
+
break;
|
|
917
|
+
case T.SHARE:
|
|
918
|
+
return this.parseShare();
|
|
919
|
+
break;
|
|
920
|
+
case IDENT:
|
|
921
|
+
if (this.text(this.i) === 'module' && this.spaceBefore(this.i + 1) && isWordKind(this.kindAt(this.i + 1))) {
|
|
922
|
+
node = this.speculate(function (this$) {
|
|
923
|
+
return function () {
|
|
924
|
+
return this$.parseModule();
|
|
925
|
+
};
|
|
926
|
+
}(this));
|
|
927
|
+
if (null != node)
|
|
928
|
+
return node;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
return this.parseExpression();
|
|
932
|
+
};
|
|
933
|
+
Parser.prototype.parseExpression = function () {
|
|
934
|
+
var left, node, right, start;
|
|
935
|
+
if (this.startsExpressionworthy(this.i)) {
|
|
936
|
+
node = this.parseExpressionworthyOrNull(false);
|
|
937
|
+
if (null != node)
|
|
938
|
+
return node;
|
|
939
|
+
}
|
|
940
|
+
start = this.i;
|
|
941
|
+
left = this.parsePostfixControlFlow();
|
|
942
|
+
if (this.at(T.SEMI)) {
|
|
943
|
+
this.next();
|
|
944
|
+
this.skipNewline();
|
|
945
|
+
right = this.parseExpression();
|
|
946
|
+
return this.rp(new CS.SeqOp(left, right), start);
|
|
947
|
+
}
|
|
948
|
+
return left;
|
|
949
|
+
};
|
|
950
|
+
Parser.prototype.parseExpressionworthy = function (noObj) {
|
|
951
|
+
var k;
|
|
952
|
+
if (this.isObjectKeyStart(this.i))
|
|
953
|
+
return this.parseImplicitObjectLiteral();
|
|
954
|
+
k = this.kind();
|
|
955
|
+
switch (k) {
|
|
956
|
+
case T.IF:
|
|
957
|
+
case T.UNLESS:
|
|
958
|
+
return this.parseConditional();
|
|
959
|
+
case T.WHILE:
|
|
960
|
+
case T.UNTIL:
|
|
961
|
+
return this.parseWhile();
|
|
962
|
+
case T.LOOP:
|
|
963
|
+
return this.parseLoop();
|
|
964
|
+
case T.TRY:
|
|
965
|
+
return this.parseTry();
|
|
966
|
+
case T.FOR:
|
|
967
|
+
case T.FOREVERY:
|
|
968
|
+
return this.parseFor();
|
|
969
|
+
case T.SWITCH:
|
|
970
|
+
return this.parseSwitch();
|
|
971
|
+
case T.CLASS:
|
|
972
|
+
case T.BLUEPRINT:
|
|
973
|
+
return this.parseClass();
|
|
974
|
+
default:
|
|
975
|
+
return this.parseFunctionLiteral();
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
Parser.prototype.parsePostfixControlFlow = function () {
|
|
979
|
+
var cond, expr, head, k, start;
|
|
980
|
+
start = this.i;
|
|
981
|
+
expr = this.parseSecondaryStatement();
|
|
982
|
+
while (true) {
|
|
983
|
+
k = this.kind();
|
|
984
|
+
if (k === T.IF || k === T.UNLESS) {
|
|
985
|
+
this.next();
|
|
986
|
+
cond = this.parseAssignment(false);
|
|
987
|
+
if (k === T.UNLESS) {
|
|
988
|
+
expr = this.rp(new CS.NegatedConditional(new CS.LogicalNotOp(cond).g(), expr, null), start);
|
|
989
|
+
} else {
|
|
990
|
+
expr = this.rp(new CS.Conditional(cond, expr, null), start);
|
|
991
|
+
}
|
|
992
|
+
} else if (k === T.WHILE || k === T.UNTIL) {
|
|
993
|
+
this.next();
|
|
994
|
+
cond = this.parseAssignment(false);
|
|
995
|
+
if (k === T.UNTIL) {
|
|
996
|
+
expr = this.rp(new CS.NegatedWhile(new CS.LogicalNotOp(cond).g(), expr), start);
|
|
997
|
+
} else {
|
|
998
|
+
expr = this.rp(new CS.While(cond, expr), start);
|
|
999
|
+
}
|
|
1000
|
+
} else if (k === T.FOR || k === T.FOREVERY) {
|
|
1001
|
+
this.next();
|
|
1002
|
+
head = this.parseForHead(k === T.FOREVERY, false);
|
|
1003
|
+
if (head.isFrom) {
|
|
1004
|
+
expr = this.rp(new CS.ForFrom(head.isAwait, head.val, head.target, head.filter, expr), start);
|
|
1005
|
+
} else if (head.isIn) {
|
|
1006
|
+
expr = this.rp(new CS.ForIn(head.val, head.key, head.target, head.step, head.filter, expr), start);
|
|
1007
|
+
} else {
|
|
1008
|
+
expr = this.rp(new CS.ForOf(head.own, head.key, head.val, head.target, head.filter, expr), start);
|
|
1009
|
+
}
|
|
1010
|
+
} else {
|
|
1011
|
+
break;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
return expr;
|
|
1015
|
+
};
|
|
1016
|
+
Parser.prototype.parseSecondaryStatement = function () {
|
|
1017
|
+
var ctor, e, node, start;
|
|
1018
|
+
start = this.i;
|
|
1019
|
+
switch (this.kind()) {
|
|
1020
|
+
case T.GRAB:
|
|
1021
|
+
return this.parseGrab();
|
|
1022
|
+
case T.SHARE:
|
|
1023
|
+
return this.parseShare();
|
|
1024
|
+
case T.RETURN:
|
|
1025
|
+
this.next();
|
|
1026
|
+
e = null;
|
|
1027
|
+
if (this.canStartExpression(this.i))
|
|
1028
|
+
if (CONTROL_FLOW[this.kind()] === 1) {
|
|
1029
|
+
e = this.speculate(function (this$) {
|
|
1030
|
+
return function () {
|
|
1031
|
+
return this$.parseSecondaryExpression(false);
|
|
1032
|
+
};
|
|
1033
|
+
}(this));
|
|
1034
|
+
} else {
|
|
1035
|
+
e = this.parseSecondaryExpression(false);
|
|
1036
|
+
}
|
|
1037
|
+
return this.rp(new CS.Return(e), start);
|
|
1038
|
+
case T.CONTINUE:
|
|
1039
|
+
this.next();
|
|
1040
|
+
node = this.rp(new CS.Continue, start);
|
|
1041
|
+
node.label = this.parseLoopLabel('keepGoing');
|
|
1042
|
+
return node;
|
|
1043
|
+
case T.BREAK:
|
|
1044
|
+
this.next();
|
|
1045
|
+
node = this.rp(new CS.Break, start);
|
|
1046
|
+
node.label = this.parseLoopLabel('bail');
|
|
1047
|
+
return node;
|
|
1048
|
+
case T.DEBUGGER:
|
|
1049
|
+
this.next();
|
|
1050
|
+
return this.rp(new CS.Debugger, start);
|
|
1051
|
+
case T.THROW:
|
|
1052
|
+
this.next();
|
|
1053
|
+
return this.rp(new CS.Throw(this.parseSecondaryExpression(false)), start);
|
|
1054
|
+
case T.PANIC:
|
|
1055
|
+
this.next();
|
|
1056
|
+
e = this.parseSecondaryExpression(false);
|
|
1057
|
+
if (e instanceof CS.String || e instanceof CS.ConcatOp) {
|
|
1058
|
+
ctor = this.c(new CS.Identifier('Error'), e);
|
|
1059
|
+
e = this.c(new CS.NewOp(ctor, [e]), e);
|
|
1060
|
+
}
|
|
1061
|
+
return this.rp(new CS.Throw(e), start);
|
|
1062
|
+
default:
|
|
1063
|
+
return this.parseSecondaryExpression(false);
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
Parser.prototype.parseSecondaryExpression = function (noObj) {
|
|
1067
|
+
var node;
|
|
1068
|
+
if (this.at(T.YIELD))
|
|
1069
|
+
return this.parseYield(noObj);
|
|
1070
|
+
if (this.startsExpressionworthy(this.i)) {
|
|
1071
|
+
node = this.parseExpressionworthyOrNull(noObj);
|
|
1072
|
+
if (null != node)
|
|
1073
|
+
return node;
|
|
1074
|
+
}
|
|
1075
|
+
return this.parseAssignment(noObj);
|
|
1076
|
+
};
|
|
1077
|
+
Parser.prototype.parseYield = function (noObj) {
|
|
1078
|
+
var isDelegate, node, operand, t;
|
|
1079
|
+
t = this.i;
|
|
1080
|
+
this.next();
|
|
1081
|
+
isDelegate = false;
|
|
1082
|
+
if (this.at(IDENT) && this.text(this.i) === 'from' && this.lines[this.i] === this.lines[t]) {
|
|
1083
|
+
this.next();
|
|
1084
|
+
isDelegate = true;
|
|
1085
|
+
}
|
|
1086
|
+
operand = null;
|
|
1087
|
+
if (this.lines[this.i] === this.lines[t] && this.canStartExpression(this.i) && !this.at(T.THEN))
|
|
1088
|
+
operand = this.parseSecondaryExpression(noObj);
|
|
1089
|
+
if (isDelegate && !(null != operand))
|
|
1090
|
+
this.fail('yield from needs an iterable', null, {
|
|
1091
|
+
code: 'MS4002',
|
|
1092
|
+
why: 'yield from hands control to another iterable (a generator, an array, a Map \u2026) and yields each of its values in turn.',
|
|
1093
|
+
fix: 'write yield from otherGenerator(), or plain yield value.'
|
|
1094
|
+
});
|
|
1095
|
+
node = this.rp(new CS.Yield(isDelegate, operand), t);
|
|
1096
|
+
return node;
|
|
1097
|
+
};
|
|
1098
|
+
Parser.prototype.parseAssignment = function (noObj) {
|
|
1099
|
+
var annotation, dcolon, k, left, op, right, start, type;
|
|
1100
|
+
start = this.i;
|
|
1101
|
+
left = this.parseBinary(noObj);
|
|
1102
|
+
k = this.kind();
|
|
1103
|
+
if (k === T.DCOLON && this.spaceBefore(this.i) && (left instanceof CS.Identifier || left instanceof CS.MemberAccessOp && left.expression instanceof CS.This)) {
|
|
1104
|
+
dcolon = this.i;
|
|
1105
|
+
this.next();
|
|
1106
|
+
type = this.parseTypeExpr();
|
|
1107
|
+
annotation = this.rp(type, dcolon);
|
|
1108
|
+
if (this.at(T.ASSIGN)) {
|
|
1109
|
+
left.typeAnnotation = annotation;
|
|
1110
|
+
this.rp(left, start);
|
|
1111
|
+
k = this.kind();
|
|
1112
|
+
} else {
|
|
1113
|
+
return this.vardefNode(left, type, start);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
if (k === T.ASSIGN) {
|
|
1117
|
+
this.toAssignable(left);
|
|
1118
|
+
this.next();
|
|
1119
|
+
right = this.parseAssignmentRhs(noObj);
|
|
1120
|
+
return this.rp(new CS.AssignOp(left, right), start);
|
|
1121
|
+
}
|
|
1122
|
+
if (k === T.COMPOUND_ASSIGN || k === T.EXISTS_ASSIGN) {
|
|
1123
|
+
if (!this.isCompoundAssignable(left))
|
|
1124
|
+
this.fail('the left side of a compound assignment must be a variable or a property', start, {
|
|
1125
|
+
code: 'MS1105',
|
|
1126
|
+
why: 'a += 1 reads the current value and writes it back, so the left side must be a place that can hold a value.',
|
|
1127
|
+
fix: 'assign to a variable or a property, e.g. total += price.'
|
|
1128
|
+
});
|
|
1129
|
+
op = k === T.EXISTS_ASSIGN ? '?' : this.vals[this.i];
|
|
1130
|
+
this.next();
|
|
1131
|
+
right = this.parseAssignmentRhs(noObj);
|
|
1132
|
+
return this.rp(new CS.CompoundAssignOp(BINARY_OP_CLASS[op], left, right), start);
|
|
1133
|
+
}
|
|
1134
|
+
return left;
|
|
1135
|
+
};
|
|
1136
|
+
Parser.prototype.parseAssignmentRhs = function (noObj) {
|
|
1137
|
+
var e;
|
|
1138
|
+
if (this.at(NEWLINE)) {
|
|
1139
|
+
if (this.kindAt(this.i + 1) === INDENT) {
|
|
1140
|
+
this.skip();
|
|
1141
|
+
this.skip();
|
|
1142
|
+
e = this.parseSecondaryExpression(noObj);
|
|
1143
|
+
this.expectDedent();
|
|
1144
|
+
return e;
|
|
1145
|
+
}
|
|
1146
|
+
this.skip();
|
|
1147
|
+
}
|
|
1148
|
+
return this.parseSecondaryExpression(noObj);
|
|
1149
|
+
};
|
|
1150
|
+
Parser.prototype.isCompoundAssignable = function (node) {
|
|
1151
|
+
var klass;
|
|
1152
|
+
if (node instanceof CS.Identifier)
|
|
1153
|
+
return node.data !== 'arguments' && node.data !== 'eval';
|
|
1154
|
+
for (var i$3 = 0, length$3 = MEMBER_ACCESS.length; i$3 < length$3; ++i$3) {
|
|
1155
|
+
klass = MEMBER_ACCESS[i$3];
|
|
1156
|
+
if (node instanceof klass)
|
|
1157
|
+
return true;
|
|
1158
|
+
}
|
|
1159
|
+
return false;
|
|
1160
|
+
};
|
|
1161
|
+
Parser.prototype.toAssignable = function (node) {
|
|
1162
|
+
var j, klass, m, meaning, members, rest, word;
|
|
1163
|
+
if (node instanceof CS.Identifier) {
|
|
1164
|
+
if (node.data === 'arguments' || node.data === 'eval')
|
|
1165
|
+
this.fail('' + node.data + ' cannot be assigned to', null, {
|
|
1166
|
+
code: 'MS1106',
|
|
1167
|
+
why: '' + node.data + ' is a keyword or a literal value; it cannot hold a value.',
|
|
1168
|
+
fix: 'choose a name that is not a keyword (the reserved words are listed in the docs).'
|
|
1169
|
+
});
|
|
1170
|
+
if (!(null != node.typeAnnotation))
|
|
1171
|
+
node.typeAnnotation = null;
|
|
1172
|
+
return node;
|
|
1173
|
+
}
|
|
1174
|
+
for (var i$3 = 0, length$3 = MEMBER_ACCESS.length; i$3 < length$3; ++i$3) {
|
|
1175
|
+
klass = MEMBER_ACCESS[i$3];
|
|
1176
|
+
if (node instanceof klass)
|
|
1177
|
+
return node;
|
|
1178
|
+
}
|
|
1179
|
+
if (node instanceof CS.ArrayInitialiser) {
|
|
1180
|
+
members = node.members;
|
|
1181
|
+
j = 0;
|
|
1182
|
+
while (j < members.length) {
|
|
1183
|
+
m = members[j];
|
|
1184
|
+
if (m instanceof CS.Spread) {
|
|
1185
|
+
rest = new CS.Rest(this.toAssignable(m.expression));
|
|
1186
|
+
rest.line = m.line;
|
|
1187
|
+
rest.column = m.column;
|
|
1188
|
+
rest.offset = m.offset;
|
|
1189
|
+
rest.raw = m.raw;
|
|
1190
|
+
members[j] = rest;
|
|
1191
|
+
} else if (m instanceof CS.AssignOp) {
|
|
1192
|
+
members[j] = this.defaulted(m);
|
|
1193
|
+
} else {
|
|
1194
|
+
this.toAssignable(m);
|
|
1195
|
+
}
|
|
1196
|
+
j++;
|
|
1197
|
+
}
|
|
1198
|
+
return node;
|
|
1199
|
+
}
|
|
1200
|
+
if (node instanceof CS.ObjectInitialiser) {
|
|
1201
|
+
for (var i$4 = 0, length$4 = node.members.length; i$4 < length$4; ++i$4) {
|
|
1202
|
+
m = node.members[i$4];
|
|
1203
|
+
j = i$4;
|
|
1204
|
+
if (m instanceof CS.Spread) {
|
|
1205
|
+
rest = new CS.Rest(this.toAssignable(m.expression));
|
|
1206
|
+
rest.line = m.line;
|
|
1207
|
+
rest.column = m.column;
|
|
1208
|
+
rest.offset = m.offset;
|
|
1209
|
+
rest.raw = m.raw;
|
|
1210
|
+
node.members[j] = rest;
|
|
1211
|
+
continue;
|
|
1212
|
+
}
|
|
1213
|
+
if (m.computed) {
|
|
1214
|
+
if (m.expression instanceof CS.AssignOp) {
|
|
1215
|
+
m.expression = this.defaulted(m.expression);
|
|
1216
|
+
} else {
|
|
1217
|
+
this.toAssignable(m.expression);
|
|
1218
|
+
}
|
|
1219
|
+
continue;
|
|
1220
|
+
}
|
|
1221
|
+
if (m.key instanceof CS.String && m.key.generated && m.expression instanceof CS.MemberAccessOp && m.expression.expression instanceof CS.This) {
|
|
1222
|
+
delete m.key.generated;
|
|
1223
|
+
if (this.raw)
|
|
1224
|
+
m.key.raw = m.expression.raw;
|
|
1225
|
+
} else if (m.expression instanceof CS.DefaultParam) {
|
|
1226
|
+
this.toAssignable(m.expression.param);
|
|
1227
|
+
} else if (m.expression instanceof CS.AssignOp) {
|
|
1228
|
+
m.expression = this.defaulted(m.expression);
|
|
1229
|
+
} else {
|
|
1230
|
+
this.toAssignable(m.expression);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
return node;
|
|
1234
|
+
}
|
|
1235
|
+
if ((null != node ? node.className : void 0) === 'Null' || (null != node ? node.className : void 0) === 'Undefined' || (null != node ? node.className : void 0) === 'Bool') {
|
|
1236
|
+
word = function () {
|
|
1237
|
+
switch (node.className) {
|
|
1238
|
+
case 'Null':
|
|
1239
|
+
return 'nothing';
|
|
1240
|
+
case 'Undefined':
|
|
1241
|
+
return 'missing';
|
|
1242
|
+
default:
|
|
1243
|
+
if (node.data) {
|
|
1244
|
+
return 'yes';
|
|
1245
|
+
} else {
|
|
1246
|
+
return 'no';
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
}.call(this);
|
|
1250
|
+
meaning = function () {
|
|
1251
|
+
switch (node.className) {
|
|
1252
|
+
case 'Null':
|
|
1253
|
+
return 'null';
|
|
1254
|
+
case 'Undefined':
|
|
1255
|
+
return 'undefined';
|
|
1256
|
+
default:
|
|
1257
|
+
return '' + node.data;
|
|
1258
|
+
}
|
|
1259
|
+
}.call(this);
|
|
1260
|
+
this.fail('' + word + ' is a keyword and cannot be assigned to', null, {
|
|
1261
|
+
code: 'MS1106',
|
|
1262
|
+
atNode: node,
|
|
1263
|
+
why: '' + word + " is MambaScript's spelling of the value " + meaning + '; it cannot hold a value of its own.',
|
|
1264
|
+
fix: 'choose another name, e.g. ' + word + 'Items or is' + (word[0].toUpperCase() + word.slice(1)) + '.'
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
return this.fail('the left side of = is not something that can be assigned to', null, {
|
|
1268
|
+
code: 'MS1105',
|
|
1269
|
+
why: 'The left side of = must be a variable, a property (a.b or a[i]) or a destructuring pattern ({a, b} or [x, y]).',
|
|
1270
|
+
fix: 'swap the sides if the value is on the left, or assign to a variable.'
|
|
1271
|
+
});
|
|
1272
|
+
};
|
|
1273
|
+
Parser.prototype.defaulted = function (assign) {
|
|
1274
|
+
var d;
|
|
1275
|
+
d = new CS.DefaultParam(this.toAssignable(assign.assignee), assign.expression);
|
|
1276
|
+
d.line = assign.line;
|
|
1277
|
+
d.column = assign.column;
|
|
1278
|
+
d.offset = assign.offset;
|
|
1279
|
+
d.raw = assign.raw;
|
|
1280
|
+
return d;
|
|
1281
|
+
};
|
|
1282
|
+
Parser.prototype.parseLoopLabel = function (word) {
|
|
1283
|
+
var label;
|
|
1284
|
+
if (!(this.at(IDENT) && this.spaceBefore(this.i) && this.lines[this.i] === this.lines[this.i - 1]))
|
|
1285
|
+
return null;
|
|
1286
|
+
label = this.text(this.i);
|
|
1287
|
+
this.next();
|
|
1288
|
+
return label;
|
|
1289
|
+
};
|
|
1290
|
+
Parser.prototype.vardefNode = function (left, type, start) {
|
|
1291
|
+
var identifier, isStatic, name, ref, s;
|
|
1292
|
+
isStatic = false;
|
|
1293
|
+
if (left instanceof CS.MemberAccessOp && left.expression instanceof CS.This) {
|
|
1294
|
+
isStatic = true;
|
|
1295
|
+
ref = left.memberName;
|
|
1296
|
+
} else {
|
|
1297
|
+
ref = left.data;
|
|
1298
|
+
}
|
|
1299
|
+
identifier = { typeRef: ref };
|
|
1300
|
+
if (null != left.typeArguments)
|
|
1301
|
+
identifier.typeArguments = left.typeArguments;
|
|
1302
|
+
name = typeIdentifierNode(identifier);
|
|
1303
|
+
this.c(name, left);
|
|
1304
|
+
if (this.raw)
|
|
1305
|
+
name.raw = left.raw;
|
|
1306
|
+
s = this.rp(new CS.Int(0), start);
|
|
1307
|
+
s.nodeType = 'vardef';
|
|
1308
|
+
s.name = name;
|
|
1309
|
+
s.expr = type;
|
|
1310
|
+
s.isStatic = isStatic;
|
|
1311
|
+
return s;
|
|
1312
|
+
};
|
|
1313
|
+
Parser.prototype.binaryOpHere = function () {
|
|
1314
|
+
var k, nk, op;
|
|
1315
|
+
k = this.kind();
|
|
1316
|
+
op = BINARY_OP_OF_KIND[k];
|
|
1317
|
+
if (null != op)
|
|
1318
|
+
return op;
|
|
1319
|
+
if (k === T.NOT) {
|
|
1320
|
+
nk = this.kindAt(this.i + 1);
|
|
1321
|
+
if (nk === T.INSTANCEOF || nk === T.AREYOUA || nk === T.IN || nk === T.OF)
|
|
1322
|
+
return 'not ' + BINARY_OP_OF_KIND[nk];
|
|
1323
|
+
}
|
|
1324
|
+
return null;
|
|
1325
|
+
};
|
|
1326
|
+
Parser.prototype.parseBinary = function (noObj) {
|
|
1327
|
+
var left, op, parts, right, start;
|
|
1328
|
+
start = this.i;
|
|
1329
|
+
left = this.parsePrefix(noObj);
|
|
1330
|
+
parts = null;
|
|
1331
|
+
while (true) {
|
|
1332
|
+
op = this.binaryOpHere();
|
|
1333
|
+
if (!(null != op))
|
|
1334
|
+
break;
|
|
1335
|
+
this.next();
|
|
1336
|
+
if (op.length > 4 && op.slice(0, 4) === 'not ')
|
|
1337
|
+
this.next();
|
|
1338
|
+
if (this.at(NEWLINE)) {
|
|
1339
|
+
if (this.kindAt(this.i + 1) === INDENT)
|
|
1340
|
+
this.fail('the right side of ' + op + ' is indented', this.i + 1, {
|
|
1341
|
+
code: 'MS1107',
|
|
1342
|
+
why: 'After an operator at the end of a line, the right operand may follow on the next line only at the same indentation; an indented line starts a block, and ' + op + ' does not take a block.',
|
|
1343
|
+
fix: 'put the right operand on the same line, or outdent it to match the start of this line.'
|
|
1344
|
+
});
|
|
1345
|
+
this.skip();
|
|
1346
|
+
}
|
|
1347
|
+
right = null;
|
|
1348
|
+
if (this.startsExpressionworthy(this.i))
|
|
1349
|
+
right = this.parseExpressionworthyOrNull(noObj);
|
|
1350
|
+
if (!(null != right))
|
|
1351
|
+
right = this.parsePrefix(noObj);
|
|
1352
|
+
if (!(null != parts))
|
|
1353
|
+
parts = [left];
|
|
1354
|
+
parts.push(op, right);
|
|
1355
|
+
}
|
|
1356
|
+
if (!(null != parts))
|
|
1357
|
+
return left;
|
|
1358
|
+
if (parts.length === 3)
|
|
1359
|
+
return this.rp(binaryNode(parts[1], parts[0], parts[2]), start);
|
|
1360
|
+
return this.rp(foldBinaryExpr(parts, false), start);
|
|
1361
|
+
};
|
|
1362
|
+
Parser.prototype.parsePrefix = function (noObj) {
|
|
1363
|
+
var e, j, k, ops, promiseAll, start;
|
|
1364
|
+
start = this.i;
|
|
1365
|
+
k = this.kind();
|
|
1366
|
+
if (k === T.DO) {
|
|
1367
|
+
this.next();
|
|
1368
|
+
e = this.speculate(function (this$) {
|
|
1369
|
+
return function () {
|
|
1370
|
+
return this$.parseNfe(start + 1);
|
|
1371
|
+
};
|
|
1372
|
+
}(this));
|
|
1373
|
+
if (!(null != e) && this.startsExpressionworthy(this.i))
|
|
1374
|
+
e = this.parseExpressionworthyOrNull(noObj);
|
|
1375
|
+
if (!(null != e))
|
|
1376
|
+
e = this.parsePrefix(noObj);
|
|
1377
|
+
return this.rp(new CS.DoOp(e), start);
|
|
1378
|
+
}
|
|
1379
|
+
if (null != PREFIX_OP_CLASS[k] || k === T.WAITFORALL) {
|
|
1380
|
+
ops = [];
|
|
1381
|
+
while (true) {
|
|
1382
|
+
k = this.kind();
|
|
1383
|
+
if (!(null != PREFIX_OP_CLASS[k] || k === T.WAITFORALL || k === T.DO))
|
|
1384
|
+
break;
|
|
1385
|
+
ops.push(k);
|
|
1386
|
+
this.next();
|
|
1387
|
+
}
|
|
1388
|
+
e = null;
|
|
1389
|
+
if (this.startsExpressionworthy(this.i))
|
|
1390
|
+
e = this.parseExpressionworthyOrNull(noObj);
|
|
1391
|
+
if (!(null != e))
|
|
1392
|
+
e = this.parsePrefix(noObj);
|
|
1393
|
+
j = ops.length - 1;
|
|
1394
|
+
while (j >= 0) {
|
|
1395
|
+
k = ops[j];
|
|
1396
|
+
if (k === T.WAITFORALL) {
|
|
1397
|
+
promiseAll = new CS.MemberAccessOp(new CS.Identifier('Promise'), 'all');
|
|
1398
|
+
e = new CS.AwaitOp(new CS.FunctionApplication(promiseAll, [e]));
|
|
1399
|
+
} else if (k === T.DO) {
|
|
1400
|
+
e = new CS.DoOp(e);
|
|
1401
|
+
} else {
|
|
1402
|
+
e = new CS[PREFIX_OP_CLASS[k]](e);
|
|
1403
|
+
}
|
|
1404
|
+
j--;
|
|
1405
|
+
}
|
|
1406
|
+
return this.rp(e, start);
|
|
1407
|
+
}
|
|
1408
|
+
return this.parsePostfix(noObj);
|
|
1409
|
+
};
|
|
1410
|
+
Parser.prototype.parseNfe = function (start) {
|
|
1411
|
+
var a, f;
|
|
1412
|
+
a = this.parseAssignable();
|
|
1413
|
+
if (!this.at(T.ASSIGN))
|
|
1414
|
+
this.fail('expected = in the named function expression', null, {
|
|
1415
|
+
code: 'MS1116',
|
|
1416
|
+
why: 'A named function is written name = (args) -> body.',
|
|
1417
|
+
fix: 'add the = between the name and the function.'
|
|
1418
|
+
});
|
|
1419
|
+
this.next();
|
|
1420
|
+
if (!this.isFunctionStart(this.i))
|
|
1421
|
+
this.fail('expected a function literal', null, {
|
|
1422
|
+
code: 'MS1116',
|
|
1423
|
+
why: 'A function is written (args) -> body or (args) => body; -> alone is a function with no parameters.',
|
|
1424
|
+
fix: 'write the function with -> or =>.'
|
|
1425
|
+
});
|
|
1426
|
+
f = this.parseFunctionLiteral();
|
|
1427
|
+
return this.rp(new CS.AssignOp(a, f), start);
|
|
1428
|
+
};
|
|
1429
|
+
Parser.prototype.parsePostfix = function (noObj) {
|
|
1430
|
+
var any, e, k, start, t;
|
|
1431
|
+
start = this.i;
|
|
1432
|
+
e = this.parseCall(noObj);
|
|
1433
|
+
any = false;
|
|
1434
|
+
while (true) {
|
|
1435
|
+
k = this.kind();
|
|
1436
|
+
t = this.i;
|
|
1437
|
+
if (k === T.QUESTION && !this.spaceBefore(t)) {
|
|
1438
|
+
this.next();
|
|
1439
|
+
e = new CS.UnaryExistsOp(e);
|
|
1440
|
+
} else if (k === T.EXISTS) {
|
|
1441
|
+
this.next();
|
|
1442
|
+
e = new CS.UnaryExistsOp(e);
|
|
1443
|
+
} else if (k === T.PLUSPLUS && !this.spaceBefore(t)) {
|
|
1444
|
+
this.next();
|
|
1445
|
+
e = new CS.PostIncrementOp(e);
|
|
1446
|
+
} else if (k === T.MINUSMINUS && !this.spaceBefore(t)) {
|
|
1447
|
+
this.next();
|
|
1448
|
+
e = new CS.PostDecrementOp(e);
|
|
1449
|
+
} else {
|
|
1450
|
+
break;
|
|
1451
|
+
}
|
|
1452
|
+
any = true;
|
|
1453
|
+
}
|
|
1454
|
+
return this.rp(e, start);
|
|
1455
|
+
};
|
|
1456
|
+
Parser.prototype.parseCall = function (noObj) {
|
|
1457
|
+
var args, e, k, node, start;
|
|
1458
|
+
start = this.i;
|
|
1459
|
+
k = this.kind();
|
|
1460
|
+
if (k === T.SUPER) {
|
|
1461
|
+
this.next();
|
|
1462
|
+
if (this.at(T.LPAREN) && !this.spaceBefore(this.i)) {
|
|
1463
|
+
args = this.parseArgumentList();
|
|
1464
|
+
node = this.rp(new CS.Super(args), start);
|
|
1465
|
+
return this.parseChain(node, start, noObj, true);
|
|
1466
|
+
}
|
|
1467
|
+
if ((this.at(T.DOT) || this.at(T.LBRACKET) || this.at(T.DCOLON)) && !this.spaceBefore(this.i))
|
|
1468
|
+
this.fail('super followed by a property access is not supported', null, {
|
|
1469
|
+
code: 'MS1117',
|
|
1470
|
+
why: 'super(...) calls the parent constructor or the parent version of the current method; its result is not meant to be chained.',
|
|
1471
|
+
fix: 'call super(...) on its own line, then work with the result through this (@), or call the parent class directly: Parent::method.call this, args.'
|
|
1472
|
+
});
|
|
1473
|
+
args = this.implicitArgumentsOrNull(this.i, noObj);
|
|
1474
|
+
if (null != args)
|
|
1475
|
+
return this.rp(new CS.Super(args), start);
|
|
1476
|
+
return this.rp(new CS.Super([]), start);
|
|
1477
|
+
}
|
|
1478
|
+
if (k === T.NEW) {
|
|
1479
|
+
node = this.parseNew(noObj);
|
|
1480
|
+
return this.parseChain(node, start, noObj, true);
|
|
1481
|
+
}
|
|
1482
|
+
e = this.parsePrimary(noObj);
|
|
1483
|
+
return this.parseChain(e, start, noObj, true);
|
|
1484
|
+
};
|
|
1485
|
+
Parser.prototype.parseNew = function (noObj) {
|
|
1486
|
+
var args, ctor, ctorStart, e, k, node, obj, parenthesised, start, typeArgs;
|
|
1487
|
+
start = this.i;
|
|
1488
|
+
this.next();
|
|
1489
|
+
k = this.kind();
|
|
1490
|
+
if (this.startsExpressionworthy(this.i) && !this.isObjectKeyStart(this.i)) {
|
|
1491
|
+
e = this.parseExpressionworthyOrNull(noObj);
|
|
1492
|
+
if (null != e)
|
|
1493
|
+
return this.rp(new CS.NewOp(e, []), start);
|
|
1494
|
+
}
|
|
1495
|
+
if (!(CAN_START[k] === 1))
|
|
1496
|
+
this.fail('expected a class or constructor after make, found ' + this.describe(this.i), null, {
|
|
1497
|
+
code: 'MS1118',
|
|
1498
|
+
why: 'make (new) creates an instance, so it must be followed by the blueprint or constructor to instantiate.',
|
|
1499
|
+
fix: 'write make ClassName or make ClassName(arguments).'
|
|
1500
|
+
});
|
|
1501
|
+
if (null != PREFIX_OP_CLASS[k] || k === T.WAITFORALL || k === T.DO) {
|
|
1502
|
+
e = this.parsePrefix(noObj);
|
|
1503
|
+
return this.rp(new CS.NewOp(e, []), start);
|
|
1504
|
+
}
|
|
1505
|
+
if (k === T.NEW) {
|
|
1506
|
+
ctor = this.parseNew(noObj);
|
|
1507
|
+
} else {
|
|
1508
|
+
ctorStart = this.i;
|
|
1509
|
+
parenthesised = this.at(T.LPAREN);
|
|
1510
|
+
ctor = this.parsePrimary(noObj);
|
|
1511
|
+
if (!parenthesised)
|
|
1512
|
+
ctor = this.parseChain(ctor, ctorStart, noObj, false);
|
|
1513
|
+
}
|
|
1514
|
+
if (this.at(T.LT) && !this.spaceBefore(this.i)) {
|
|
1515
|
+
typeArgs = this.speculate(function (this$) {
|
|
1516
|
+
return function () {
|
|
1517
|
+
return this$.parseTypeArgumentList();
|
|
1518
|
+
};
|
|
1519
|
+
}(this));
|
|
1520
|
+
} else {
|
|
1521
|
+
typeArgs = null;
|
|
1522
|
+
}
|
|
1523
|
+
if (this.at(T.LPAREN) && !this.spaceBefore(this.i)) {
|
|
1524
|
+
args = this.parseArgumentList();
|
|
1525
|
+
node = this.rp(new CS.NewOp(ctor, args), start);
|
|
1526
|
+
node.typeArguments = typeArgs || null;
|
|
1527
|
+
return this.parseChain(node, start, noObj, false);
|
|
1528
|
+
}
|
|
1529
|
+
args = this.implicitArgumentsOrNull(this.i, noObj);
|
|
1530
|
+
if (null != args) {
|
|
1531
|
+
node = this.rp(new CS.NewOp(ctor, args), start);
|
|
1532
|
+
node.typeArguments = typeArgs || null;
|
|
1533
|
+
return node;
|
|
1534
|
+
}
|
|
1535
|
+
if (!noObj && this.at(NEWLINE) && (this.flags[this.i] & FLAG_NL_BEFORE_INDENT) !== 0 && this.isObjectKeyStart(this.i + 2)) {
|
|
1536
|
+
this.skip();
|
|
1537
|
+
this.skip();
|
|
1538
|
+
obj = this.parseImplicitObjectLiteral();
|
|
1539
|
+
this.expectDedent();
|
|
1540
|
+
node = this.rp(new CS.NewOp(ctor, [obj]), start);
|
|
1541
|
+
node.typeArguments = typeArgs || null;
|
|
1542
|
+
return node;
|
|
1543
|
+
}
|
|
1544
|
+
return this.rp(new CS.NewOp(ctor, []), start);
|
|
1545
|
+
};
|
|
1546
|
+
Parser.prototype.chain = function (node, base, prev, accessStart, accessEnd) {
|
|
1547
|
+
var end;
|
|
1548
|
+
if (this.raw) {
|
|
1549
|
+
node.line = base.line;
|
|
1550
|
+
node.column = base.column;
|
|
1551
|
+
node.offset = base.offset;
|
|
1552
|
+
end = accessEnd > 0 ? accessEnd : this.lastEnd;
|
|
1553
|
+
node.raw = (null != prev.raw ? prev.raw : '') + this.src.slice(accessStart, end);
|
|
1554
|
+
}
|
|
1555
|
+
return node;
|
|
1556
|
+
};
|
|
1557
|
+
Parser.prototype.parseChain = function (e, start, noObj, allowCalls, blockState) {
|
|
1558
|
+
var accessEnd, accessStart, args, base, cache$3, cache$4, dedent, expressions, first, idx, k, le, mark, name, nk, nsp, obj, quasis, sp, t, typeArgs;
|
|
1559
|
+
base = e;
|
|
1560
|
+
first = true;
|
|
1561
|
+
if (!(null != blockState))
|
|
1562
|
+
blockState = 0;
|
|
1563
|
+
while (true) {
|
|
1564
|
+
k = this.kind();
|
|
1565
|
+
t = this.i;
|
|
1566
|
+
sp = this.spaceBefore(t);
|
|
1567
|
+
accessEnd = -1;
|
|
1568
|
+
if (blockState === 1) {
|
|
1569
|
+
accessStart = this.starts[t];
|
|
1570
|
+
} else if (blockState === 3) {
|
|
1571
|
+
accessStart = this.blockRawStart;
|
|
1572
|
+
accessEnd = this.blockRawEnd;
|
|
1573
|
+
} else {
|
|
1574
|
+
accessStart = this.lastEnd;
|
|
1575
|
+
}
|
|
1576
|
+
if (blockState === 1 || blockState === 3)
|
|
1577
|
+
blockState = 2;
|
|
1578
|
+
if (k === T.DOT) {
|
|
1579
|
+
this.next();
|
|
1580
|
+
if (this.at(NEWLINE) && (this.flags[this.i] & FLAG_NL_BEFORE_INDENT) === 0 && isWordKind(this.kindAt(this.i + 1)))
|
|
1581
|
+
this.skip();
|
|
1582
|
+
name = this.parseMemberName();
|
|
1583
|
+
e = this.chain(new CS.MemberAccessOp(e, name), base, e, accessStart, accessEnd);
|
|
1584
|
+
} else if (k === NEWLINE && this.kindAt(t + 1) === T.DOT) {
|
|
1585
|
+
this.skip();
|
|
1586
|
+
continue;
|
|
1587
|
+
} else if (k === NEWLINE && (this.flags[t] & FLAG_NL_BEFORE_INDENT) !== 0 && this.kindAt(t + 2) === T.DOT && allowCalls) {
|
|
1588
|
+
dedent = this.blockDedent(t + 1);
|
|
1589
|
+
if (this.blockHasCalls(t + 2, dedent)) {
|
|
1590
|
+
this.skip();
|
|
1591
|
+
this.skip();
|
|
1592
|
+
e = this.parseChain(e, start, noObj, true, 1);
|
|
1593
|
+
} else {
|
|
1594
|
+
this.blockRawStart = this.starts[t];
|
|
1595
|
+
this.blockRawEnd = this.starts[dedent];
|
|
1596
|
+
this.skip();
|
|
1597
|
+
this.skip();
|
|
1598
|
+
e = this.parseChain(e, start, noObj, true, 3);
|
|
1599
|
+
}
|
|
1600
|
+
this.expectDedent();
|
|
1601
|
+
if (blockState === 0)
|
|
1602
|
+
return e;
|
|
1603
|
+
continue;
|
|
1604
|
+
} else if (k === T.QUESTION && !sp) {
|
|
1605
|
+
nk = this.kindAt(t + 1);
|
|
1606
|
+
nsp = this.spaceBefore(t + 1);
|
|
1607
|
+
if (nk === T.DOT && !nsp) {
|
|
1608
|
+
this.next();
|
|
1609
|
+
this.next();
|
|
1610
|
+
name = this.parseMemberName();
|
|
1611
|
+
e = this.chain(new CS.SoakedMemberAccessOp(e, name), base, e, accessStart, accessEnd);
|
|
1612
|
+
} else if (nk === T.LBRACKET && !nsp) {
|
|
1613
|
+
this.next();
|
|
1614
|
+
this.next();
|
|
1615
|
+
idx = this.parseExpression();
|
|
1616
|
+
this.expect(T.RBRACKET, ']');
|
|
1617
|
+
e = this.chain(new CS.SoakedDynamicMemberAccessOp(e, idx), base, e, accessStart, accessEnd);
|
|
1618
|
+
} else if (nk === T.DCOLON && !nsp) {
|
|
1619
|
+
this.next();
|
|
1620
|
+
this.next();
|
|
1621
|
+
if (this.at(T.LBRACKET) && !this.spaceBefore(this.i)) {
|
|
1622
|
+
this.next();
|
|
1623
|
+
idx = this.parseExpression();
|
|
1624
|
+
this.expect(T.RBRACKET, ']');
|
|
1625
|
+
e = this.chain(new CS.SoakedDynamicProtoMemberAccessOp(e, idx), base, e, accessStart, accessEnd);
|
|
1626
|
+
} else {
|
|
1627
|
+
name = this.parseMemberName();
|
|
1628
|
+
e = this.chain(new CS.SoakedProtoMemberAccessOp(e, name), base, e, accessStart, accessEnd);
|
|
1629
|
+
}
|
|
1630
|
+
} else if (nk === T.LPAREN && !nsp && allowCalls) {
|
|
1631
|
+
this.next();
|
|
1632
|
+
args = this.parseArgumentList();
|
|
1633
|
+
e = this.chain(new CS.SoakedFunctionApplication(e, args), base, e, accessStart, accessEnd);
|
|
1634
|
+
} else if (allowCalls && this.canStartImplicitArg(t + 1, noObj)) {
|
|
1635
|
+
mark = this.i;
|
|
1636
|
+
le = this.lastEnd;
|
|
1637
|
+
this.next();
|
|
1638
|
+
args = this.implicitArgumentsOrNull(this.i, noObj);
|
|
1639
|
+
if (!(null != args)) {
|
|
1640
|
+
this.i = mark;
|
|
1641
|
+
this.lastEnd = le;
|
|
1642
|
+
return e;
|
|
1643
|
+
}
|
|
1644
|
+
return this.rp(new CS.SoakedFunctionApplication(e, args), start);
|
|
1645
|
+
} else if (allowCalls && !noObj && nk === NEWLINE && (this.flags[t + 1] & FLAG_NL_BEFORE_INDENT) !== 0 && this.isObjectKeyStart(t + 3)) {
|
|
1646
|
+
this.next();
|
|
1647
|
+
this.skip();
|
|
1648
|
+
this.skip();
|
|
1649
|
+
obj = this.parseImplicitObjectLiteral();
|
|
1650
|
+
this.expectDedent();
|
|
1651
|
+
return this.rp(new CS.SoakedFunctionApplication(e, [obj]), start);
|
|
1652
|
+
} else {
|
|
1653
|
+
return e;
|
|
1654
|
+
}
|
|
1655
|
+
} else if (k === T.LBRACKET && !sp) {
|
|
1656
|
+
this.next();
|
|
1657
|
+
e = this.parseIndexOrSlice(e, base, accessStart, accessEnd);
|
|
1658
|
+
} else if (k === T.DCOLON && !sp) {
|
|
1659
|
+
this.next();
|
|
1660
|
+
if (this.at(T.LBRACKET) && !this.spaceBefore(this.i)) {
|
|
1661
|
+
this.next();
|
|
1662
|
+
idx = this.parseExpression();
|
|
1663
|
+
this.expect(T.RBRACKET, ']');
|
|
1664
|
+
e = this.chain(new CS.DynamicProtoMemberAccessOp(e, idx), base, e, accessStart, accessEnd);
|
|
1665
|
+
} else {
|
|
1666
|
+
name = this.parseMemberName();
|
|
1667
|
+
e = this.chain(new CS.ProtoMemberAccessOp(e, name), base, e, accessStart, accessEnd);
|
|
1668
|
+
}
|
|
1669
|
+
} else if (k === T.STRING_START && !sp && allowCalls) {
|
|
1670
|
+
cache$3 = this.parseTemplatePieces();
|
|
1671
|
+
quasis = cache$3.quasis;
|
|
1672
|
+
expressions = cache$3.expressions;
|
|
1673
|
+
e = this.chain(new CS.TaggedTemplate(e, quasis, expressions), base, e, accessStart, accessEnd);
|
|
1674
|
+
} else if (k === T.JSLITERAL && !sp && allowCalls) {
|
|
1675
|
+
this.fail('backticks are embedded JavaScript, not a template', null, {
|
|
1676
|
+
code: 'MS1132',
|
|
1677
|
+
why: 'In MambaScript a backtick string is raw JavaScript pasted into the output; a tagged template is written with an ordinary string glued to the tag.',
|
|
1678
|
+
fix: 'write ' + (cache$4 = this.text(t - 1), null != cache$4 ? cache$4 : 'tag') + '"\u2026" for a tagged template, or put a space before the backtick to pass raw JavaScript to a call.'
|
|
1679
|
+
});
|
|
1680
|
+
} else if (k === T.LPAREN && !sp && allowCalls) {
|
|
1681
|
+
args = this.parseArgumentList();
|
|
1682
|
+
e = this.chain(new CS.FunctionApplication(e, args), base, e, accessStart, accessEnd);
|
|
1683
|
+
} else if (k === T.LT && !sp && first) {
|
|
1684
|
+
typeArgs = this.speculate(function (this$) {
|
|
1685
|
+
return function () {
|
|
1686
|
+
return this$.parseTypeArgumentList();
|
|
1687
|
+
};
|
|
1688
|
+
}(this));
|
|
1689
|
+
if (null != typeArgs) {
|
|
1690
|
+
e.typeArguments = typeArgs;
|
|
1691
|
+
} else {
|
|
1692
|
+
return e;
|
|
1693
|
+
}
|
|
1694
|
+
} else if (allowCalls && this.canStartImplicitArg(t, noObj)) {
|
|
1695
|
+
args = this.implicitArgumentsOrNull(t, noObj);
|
|
1696
|
+
if (!(null != args))
|
|
1697
|
+
return e;
|
|
1698
|
+
return this.rp(new CS.FunctionApplication(e, args), start);
|
|
1699
|
+
} else if (allowCalls && !noObj && k === NEWLINE && (this.flags[t] & FLAG_NL_BEFORE_INDENT) !== 0 && this.isObjectKeyStart(t + 2)) {
|
|
1700
|
+
this.skip();
|
|
1701
|
+
this.skip();
|
|
1702
|
+
obj = this.parseImplicitObjectLiteral();
|
|
1703
|
+
this.expectDedent();
|
|
1704
|
+
return this.rp(new CS.FunctionApplication(e, [obj]), start);
|
|
1705
|
+
} else {
|
|
1706
|
+
return e;
|
|
1707
|
+
}
|
|
1708
|
+
first = false;
|
|
1709
|
+
}
|
|
1710
|
+
};
|
|
1711
|
+
Parser.prototype.blockDedent = function (indentTok) {
|
|
1712
|
+
var depth, j, k;
|
|
1713
|
+
depth = 0;
|
|
1714
|
+
j = indentTok;
|
|
1715
|
+
while (j < this.n) {
|
|
1716
|
+
k = this.kinds[j];
|
|
1717
|
+
if (k === INDENT) {
|
|
1718
|
+
depth++;
|
|
1719
|
+
} else if (k === DEDENT) {
|
|
1720
|
+
depth--;
|
|
1721
|
+
if (depth === 0)
|
|
1722
|
+
return j;
|
|
1723
|
+
}
|
|
1724
|
+
j++;
|
|
1725
|
+
}
|
|
1726
|
+
return this.n - 1;
|
|
1727
|
+
};
|
|
1728
|
+
Parser.prototype.blockHasCalls = function (from, to) {
|
|
1729
|
+
var j;
|
|
1730
|
+
j = from;
|
|
1731
|
+
while (j < to) {
|
|
1732
|
+
if (this.kinds[j] === T.LPAREN && !this.spaceBefore(j))
|
|
1733
|
+
return true;
|
|
1734
|
+
j++;
|
|
1735
|
+
}
|
|
1736
|
+
return false;
|
|
1737
|
+
};
|
|
1738
|
+
Parser.prototype.parseMemberName = function () {
|
|
1739
|
+
var k;
|
|
1740
|
+
k = this.kind();
|
|
1741
|
+
if (!isWordKind(k))
|
|
1742
|
+
this.fail('expected a property name after the dot, found ' + this.describe(this.i), null, {
|
|
1743
|
+
code: 'MS1112',
|
|
1744
|
+
why: 'After a dot comes the name of a property (user.name) or a keyword used as a name (user.class).',
|
|
1745
|
+
fix: 'add the property name, or remove the dot.'
|
|
1746
|
+
});
|
|
1747
|
+
return this.text(this.next());
|
|
1748
|
+
};
|
|
1749
|
+
Parser.prototype.parseIndexOrSlice = function (e, base, accessStart, accessEnd) {
|
|
1750
|
+
var inclusive, k, left, right;
|
|
1751
|
+
k = this.kind();
|
|
1752
|
+
if (k === T.DOTDOT || k === T.ELLIPSIS) {
|
|
1753
|
+
inclusive = k === T.DOTDOT;
|
|
1754
|
+
this.next();
|
|
1755
|
+
right = this.at(T.RBRACKET) ? null : this.parseAssignment(false);
|
|
1756
|
+
this.expect(T.RBRACKET, ']');
|
|
1757
|
+
return this.chain(new CS.Slice(e, inclusive, null, right), base, e, accessStart, accessEnd);
|
|
1758
|
+
}
|
|
1759
|
+
left = this.parseExpression();
|
|
1760
|
+
k = this.kind();
|
|
1761
|
+
if (k === T.DOTDOT || k === T.ELLIPSIS) {
|
|
1762
|
+
inclusive = k === T.DOTDOT;
|
|
1763
|
+
this.next();
|
|
1764
|
+
right = this.at(T.RBRACKET) ? null : this.parseAssignment(false);
|
|
1765
|
+
this.expect(T.RBRACKET, ']');
|
|
1766
|
+
return this.chain(new CS.Slice(e, inclusive, left, right), base, e, accessStart, accessEnd);
|
|
1767
|
+
}
|
|
1768
|
+
this.expect(T.RBRACKET, ']');
|
|
1769
|
+
return this.chain(new CS.DynamicMemberAccessOp(e, left), base, e, accessStart, accessEnd);
|
|
1770
|
+
};
|
|
1771
|
+
Parser.prototype.parseArgumentList = function () {
|
|
1772
|
+
var args, indented, k, open;
|
|
1773
|
+
open = this.i;
|
|
1774
|
+
this.next();
|
|
1775
|
+
args = [];
|
|
1776
|
+
indented = false;
|
|
1777
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
1778
|
+
this.skip();
|
|
1779
|
+
this.skip();
|
|
1780
|
+
indented = true;
|
|
1781
|
+
} else if (this.at(T.RPAREN)) {
|
|
1782
|
+
this.next();
|
|
1783
|
+
return args;
|
|
1784
|
+
}
|
|
1785
|
+
while (true) {
|
|
1786
|
+
k = this.kind();
|
|
1787
|
+
if (k === T.RPAREN || k === DEDENT)
|
|
1788
|
+
break;
|
|
1789
|
+
args.push(this.parseArgument());
|
|
1790
|
+
k = this.kind();
|
|
1791
|
+
if (k === T.COMMA || k === NEWLINE) {
|
|
1792
|
+
this.skip();
|
|
1793
|
+
continue;
|
|
1794
|
+
}
|
|
1795
|
+
break;
|
|
1796
|
+
}
|
|
1797
|
+
if (indented) {
|
|
1798
|
+
this.expectDedent();
|
|
1799
|
+
this.skipNewline();
|
|
1800
|
+
}
|
|
1801
|
+
if (!this.at(T.RPAREN))
|
|
1802
|
+
this.fail('expected ) to close the call that started on line ' + this.lines[open] + ', column ' + this.cols[open] + ', found ' + this.describe(this.i), null, {
|
|
1803
|
+
code: 'MS1111',
|
|
1804
|
+
opener: open,
|
|
1805
|
+
openerLabel: 'the call was opened here',
|
|
1806
|
+
why: 'The parentheses of a call must be closed on the same expression.',
|
|
1807
|
+
fix: 'add the closing ), or check the arguments for an unclosed bracket or string.'
|
|
1808
|
+
});
|
|
1809
|
+
this.next();
|
|
1810
|
+
return args;
|
|
1811
|
+
};
|
|
1812
|
+
Parser.prototype.parseArgument = function () {
|
|
1813
|
+
var e, start;
|
|
1814
|
+
start = this.i;
|
|
1815
|
+
e = this.parseExpression();
|
|
1816
|
+
if (this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
1817
|
+
this.next();
|
|
1818
|
+
return this.rp(new CS.Spread(e), start);
|
|
1819
|
+
}
|
|
1820
|
+
return e;
|
|
1821
|
+
};
|
|
1822
|
+
Parser.prototype.implicitArgumentsOrNull = function (t, noObj) {
|
|
1823
|
+
if (!this.canStartImplicitArg(t, noObj))
|
|
1824
|
+
return null;
|
|
1825
|
+
if (CONTROL_FLOW[this.kindAt(t)] === 1 && !this.isObjectKeyStart(t))
|
|
1826
|
+
return this.speculate(function (this$) {
|
|
1827
|
+
return function () {
|
|
1828
|
+
return this$.parseImplicitArguments(noObj);
|
|
1829
|
+
};
|
|
1830
|
+
}(this));
|
|
1831
|
+
return this.parseImplicitArguments(noObj);
|
|
1832
|
+
};
|
|
1833
|
+
Parser.prototype.parseImplicitArguments = function (noObj) {
|
|
1834
|
+
var args, e, start;
|
|
1835
|
+
args = [];
|
|
1836
|
+
while (true) {
|
|
1837
|
+
start = this.i;
|
|
1838
|
+
if (!noObj && this.isObjectKeyStart(this.i)) {
|
|
1839
|
+
args.push(this.parseSingleLineImplicitObjectLiteral());
|
|
1840
|
+
} else {
|
|
1841
|
+
e = this.parseSecondaryExpression(noObj);
|
|
1842
|
+
if (this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
1843
|
+
this.next();
|
|
1844
|
+
e = this.rp(new CS.Spread(e), start);
|
|
1845
|
+
}
|
|
1846
|
+
args.push(e);
|
|
1847
|
+
}
|
|
1848
|
+
if (this.at(T.COMMA)) {
|
|
1849
|
+
this.next();
|
|
1850
|
+
if (this.at(NEWLINE)) {
|
|
1851
|
+
if (this.kindAt(this.i + 1) === INDENT) {
|
|
1852
|
+
if (!noObj && this.isObjectKeyStart(this.i + 2)) {
|
|
1853
|
+
this.skip();
|
|
1854
|
+
this.skip();
|
|
1855
|
+
args.push(this.parseImplicitObjectLiteral());
|
|
1856
|
+
this.expectDedent();
|
|
1857
|
+
}
|
|
1858
|
+
break;
|
|
1859
|
+
}
|
|
1860
|
+
this.skip();
|
|
1861
|
+
}
|
|
1862
|
+
continue;
|
|
1863
|
+
}
|
|
1864
|
+
if (!noObj && this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT && this.isObjectKeyStart(this.i + 2)) {
|
|
1865
|
+
this.skip();
|
|
1866
|
+
this.skip();
|
|
1867
|
+
args.push(this.parseImplicitObjectLiteral());
|
|
1868
|
+
this.expectDedent();
|
|
1869
|
+
}
|
|
1870
|
+
break;
|
|
1871
|
+
}
|
|
1872
|
+
return args;
|
|
1873
|
+
};
|
|
1874
|
+
Parser.prototype.parsePrimary = function (noObj) {
|
|
1875
|
+
var e, fn, k, name, node, self, source, t, text, v, value, word;
|
|
1876
|
+
t = this.i;
|
|
1877
|
+
k = this.kind();
|
|
1878
|
+
switch (k) {
|
|
1879
|
+
case T.MACRO:
|
|
1880
|
+
return this.parseMacro();
|
|
1881
|
+
break;
|
|
1882
|
+
case T.NUMBER_BIGINT:
|
|
1883
|
+
this.next();
|
|
1884
|
+
text = this.text(t).replace(/_/g, '');
|
|
1885
|
+
return this.rp(new CS.BigInt(text.slice(0, -1)), t);
|
|
1886
|
+
break;
|
|
1887
|
+
case T.NUMBER_INT:
|
|
1888
|
+
this.next();
|
|
1889
|
+
text = this.text(t).replace(/_/g, '');
|
|
1890
|
+
value = text.charCodeAt(0) === 48 && text.length > 1 ? function () {
|
|
1891
|
+
switch (text.charCodeAt(1)) {
|
|
1892
|
+
case 120:
|
|
1893
|
+
return parseInt(text.slice(2), 16);
|
|
1894
|
+
case 111:
|
|
1895
|
+
return parseInt(text.slice(2), 8);
|
|
1896
|
+
case 98:
|
|
1897
|
+
return parseInt(text.slice(2), 2);
|
|
1898
|
+
default:
|
|
1899
|
+
return +text;
|
|
1900
|
+
}
|
|
1901
|
+
}.call(this) : +text;
|
|
1902
|
+
return this.rp(new CS.Int(value), t);
|
|
1903
|
+
break;
|
|
1904
|
+
case T.NUMBER_FLOAT:
|
|
1905
|
+
this.next();
|
|
1906
|
+
return this.rp(new CS.Float(parseFloat(this.text(t).replace(/_/g, ''))), t);
|
|
1907
|
+
break;
|
|
1908
|
+
case T.TRUE:
|
|
1909
|
+
this.next();
|
|
1910
|
+
return this.rp(new CS.Bool(true), t);
|
|
1911
|
+
break;
|
|
1912
|
+
case T.FALSE:
|
|
1913
|
+
this.next();
|
|
1914
|
+
return this.rp(new CS.Bool(false), t);
|
|
1915
|
+
break;
|
|
1916
|
+
case T.NULL:
|
|
1917
|
+
this.next();
|
|
1918
|
+
return this.rp(new CS.Null, t);
|
|
1919
|
+
break;
|
|
1920
|
+
case T.UNDEFINED:
|
|
1921
|
+
this.next();
|
|
1922
|
+
return this.rp(new CS.Undefined, t);
|
|
1923
|
+
break;
|
|
1924
|
+
case T.THIS:
|
|
1925
|
+
this.next();
|
|
1926
|
+
return this.rp(new CS.This, t);
|
|
1927
|
+
break;
|
|
1928
|
+
case T.AT:
|
|
1929
|
+
this.next();
|
|
1930
|
+
if (isWordKind(this.kind()) && !this.spaceBefore(this.i)) {
|
|
1931
|
+
self = this.rp(new CS.This, t);
|
|
1932
|
+
name = this.text(this.next());
|
|
1933
|
+
node = this.rp(new CS.MemberAccessOp(self, name), t);
|
|
1934
|
+
return node;
|
|
1935
|
+
}
|
|
1936
|
+
return this.rp(new CS.This, t);
|
|
1937
|
+
break;
|
|
1938
|
+
case IDENT:
|
|
1939
|
+
word = this.text(t);
|
|
1940
|
+
if ((word === 'await' || word === 'async') && this.canStartImplicitArg(t + 1, false))
|
|
1941
|
+
if (word === 'await') {
|
|
1942
|
+
this.fail('await is JavaScript; MambaScript spells it waitForIt', t, {
|
|
1943
|
+
code: 'MS1124',
|
|
1944
|
+
why: 'MambaScript waits on a promise with waitForIt, and the function that contains it becomes async by itself. Written as await, this line would compile to a call of a function named await.',
|
|
1945
|
+
fix: 'write waitForIt in place of await: result = waitForIt fetchUser id.'
|
|
1946
|
+
});
|
|
1947
|
+
} else {
|
|
1948
|
+
this.fail('async is JavaScript; MambaScript needs no async keyword', t, {
|
|
1949
|
+
code: 'MS1124',
|
|
1950
|
+
why: 'A function becomes async by itself when its body uses waitForIt or waitForAll. Written as async, this line would compile to a call of a function named async.',
|
|
1951
|
+
fix: 'drop async and use waitForIt inside the function: load = -> waitForIt fetchUser id.'
|
|
1952
|
+
});
|
|
1953
|
+
}
|
|
1954
|
+
this.next();
|
|
1955
|
+
node = this.rp(new CS.Identifier(word), t);
|
|
1956
|
+
node.typeArguments = null;
|
|
1957
|
+
return node;
|
|
1958
|
+
break;
|
|
1959
|
+
case T.LBRACKET:
|
|
1960
|
+
return this.parseArrayOrRange();
|
|
1961
|
+
break;
|
|
1962
|
+
case T.LBRACE:
|
|
1963
|
+
return this.parseObjectLiteral();
|
|
1964
|
+
break;
|
|
1965
|
+
case T.STRING_START:
|
|
1966
|
+
return this.parseString();
|
|
1967
|
+
break;
|
|
1968
|
+
case T.JSLITERAL:
|
|
1969
|
+
this.next();
|
|
1970
|
+
return this.rp(new CS.JavaScript(this.vals[t]), t);
|
|
1971
|
+
break;
|
|
1972
|
+
case T.REGEX:
|
|
1973
|
+
this.next();
|
|
1974
|
+
v = this.vals[t];
|
|
1975
|
+
return this.rp(new CS.RegExp(v.data, v.flags.split('')), t);
|
|
1976
|
+
break;
|
|
1977
|
+
case T.HEREGEX_START:
|
|
1978
|
+
return this.parseHeregex();
|
|
1979
|
+
break;
|
|
1980
|
+
case T.LPAREN:
|
|
1981
|
+
if (this.isFunctionStart(t)) {
|
|
1982
|
+
fn = this.speculate(function (this$) {
|
|
1983
|
+
return function () {
|
|
1984
|
+
return this$.parseFunctionLiteral();
|
|
1985
|
+
};
|
|
1986
|
+
}(this));
|
|
1987
|
+
if (null != fn)
|
|
1988
|
+
return fn;
|
|
1989
|
+
}
|
|
1990
|
+
this.next();
|
|
1991
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
1992
|
+
this.skip();
|
|
1993
|
+
this.skip();
|
|
1994
|
+
e = this.parseExpression();
|
|
1995
|
+
this.expectDedent();
|
|
1996
|
+
this.skipNewline();
|
|
1997
|
+
} else {
|
|
1998
|
+
e = this.parseExpression();
|
|
1999
|
+
this.skipNewline();
|
|
2000
|
+
}
|
|
2001
|
+
if (!this.at(T.RPAREN)) {
|
|
2002
|
+
if (this.at(T.COMMA))
|
|
2003
|
+
this.fail('expected ) to close the ( on line ' + this.lines[t] + ', column ' + this.cols[t] + ", found ','", null, {
|
|
2004
|
+
code: 'MS1108',
|
|
2005
|
+
opener: t,
|
|
2006
|
+
openerLabel: 'the ( was opened here',
|
|
2007
|
+
why: 'Parentheses group one expression; a comma inside them only makes sense in a parameter list that is followed by -> or =>, or in a call.',
|
|
2008
|
+
fix: 'for a function write (a, b) -> body; for a list write [a, b]; for a call put the function name before the (.'
|
|
2009
|
+
});
|
|
2010
|
+
this.fail('expected ) to close the ( on line ' + this.lines[t] + ', column ' + this.cols[t] + ', found ' + this.describe(this.i), null, {
|
|
2011
|
+
code: 'MS1108',
|
|
2012
|
+
opener: t,
|
|
2013
|
+
openerLabel: 'the ( was opened here',
|
|
2014
|
+
why: 'Parentheses must be balanced; this ( is still open.',
|
|
2015
|
+
fix: 'add the closing ), or check the expression inside for an unclosed bracket or string.'
|
|
2016
|
+
});
|
|
2017
|
+
}
|
|
2018
|
+
this.next();
|
|
2019
|
+
return this.r(e.clone(), t);
|
|
2020
|
+
break;
|
|
2021
|
+
case T.ARROW:
|
|
2022
|
+
case T.FAT_ARROW:
|
|
2023
|
+
case T.DCOLON:
|
|
2024
|
+
return this.parseFunctionLiteral();
|
|
2025
|
+
}
|
|
2026
|
+
if (k === T.RESERVED && this.text(this.i) === 'import' && this.kindAt(this.i + 1) === T.LPAREN && !this.spaceBefore(this.i + 1)) {
|
|
2027
|
+
this.next();
|
|
2028
|
+
this.next();
|
|
2029
|
+
source = this.parseExpression();
|
|
2030
|
+
this.expect(T.RPAREN, ') to close import(');
|
|
2031
|
+
return this.rp(new CS.DynamicImport(source), t);
|
|
2032
|
+
}
|
|
2033
|
+
if (CAN_START_STATEMENT[k] === 1)
|
|
2034
|
+
this.unexpected(this.i);
|
|
2035
|
+
return this.unexpected(this.i);
|
|
2036
|
+
};
|
|
2037
|
+
Parser.prototype.parseMacro = function () {
|
|
2038
|
+
var name, t;
|
|
2039
|
+
t = this.i;
|
|
2040
|
+
name = this.text(this.next());
|
|
2041
|
+
switch (name) {
|
|
2042
|
+
case '__LINE__':
|
|
2043
|
+
return this.rp(new CS.Int(this.lines[t]), t);
|
|
2044
|
+
case '__FILENAME__':
|
|
2045
|
+
return this.rp(new CS.String(this.options.inputSource || ''), t);
|
|
2046
|
+
case '__DATE__':
|
|
2047
|
+
return this.rp(new CS.String(new Date().toDateString().slice(4)), t);
|
|
2048
|
+
case '__TIME__':
|
|
2049
|
+
return this.rp(new CS.String(new Date().toTimeString().slice(0, 8)), t);
|
|
2050
|
+
case '__DATETIMEMS__':
|
|
2051
|
+
return this.rp(new CS.Int(+new Date), t);
|
|
2052
|
+
case '__MAMBA_VERSION__':
|
|
2053
|
+
return this.rp(new CS.String(pkg.version), t);
|
|
2054
|
+
default:
|
|
2055
|
+
return this.fail('' + name + ' is not a macro', t, {
|
|
2056
|
+
code: 'MS1115',
|
|
2057
|
+
why: 'The macros are __LINE__, __FILENAME__, __DATE__, __TIME__, __DATETIMEMS__ and __MAMBA_VERSION__.',
|
|
2058
|
+
fix: 'check the spelling, or use a plain variable name without double underscores.'
|
|
2059
|
+
});
|
|
2060
|
+
}
|
|
2061
|
+
};
|
|
2062
|
+
Parser.prototype.parseArrayOrRange = function () {
|
|
2063
|
+
var e, first, inclusive, indented, isRange, k, members, mstart, right, t;
|
|
2064
|
+
t = this.i;
|
|
2065
|
+
this.next();
|
|
2066
|
+
members = [];
|
|
2067
|
+
indented = false;
|
|
2068
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2069
|
+
this.skip();
|
|
2070
|
+
this.skip();
|
|
2071
|
+
indented = true;
|
|
2072
|
+
}
|
|
2073
|
+
first = true;
|
|
2074
|
+
while (true) {
|
|
2075
|
+
k = this.kind();
|
|
2076
|
+
if (k === T.RBRACKET || k === DEDENT)
|
|
2077
|
+
break;
|
|
2078
|
+
if (k === NEWLINE && this.kindAt(this.i + 1) === T.RBRACKET) {
|
|
2079
|
+
this.skip();
|
|
2080
|
+
break;
|
|
2081
|
+
}
|
|
2082
|
+
if (k === NEWLINE && this.kindAt(this.i + 1) === INDENT && this.isObjectKeyStart(this.i + 2)) {
|
|
2083
|
+
this.skip();
|
|
2084
|
+
this.skip();
|
|
2085
|
+
members.push(this.parseImplicitObjectLiteral());
|
|
2086
|
+
this.expectDedent();
|
|
2087
|
+
} else {
|
|
2088
|
+
mstart = this.i;
|
|
2089
|
+
e = this.parseExpression();
|
|
2090
|
+
isRange = first && !indented && (this.at(T.DOTDOT) || this.at(T.ELLIPSIS) && CAN_START[this.kindAt(this.i + 1)] === 1);
|
|
2091
|
+
if (isRange) {
|
|
2092
|
+
inclusive = this.at(T.DOTDOT);
|
|
2093
|
+
this.next();
|
|
2094
|
+
right = this.parseSecondaryExpression(false);
|
|
2095
|
+
if (!this.at(T.RBRACKET))
|
|
2096
|
+
this.fail('expected ] to close the range that started on line ' + this.lines[t] + ', found ' + this.describe(this.i), null, {
|
|
2097
|
+
code: 'MS1127',
|
|
2098
|
+
opener: t,
|
|
2099
|
+
openerLabel: 'the range was opened here',
|
|
2100
|
+
why: 'A range is [start..end] (inclusive) or [start...end] (exclusive), with one expression on each side.',
|
|
2101
|
+
fix: 'add the closing ], or check the two bounds.'
|
|
2102
|
+
});
|
|
2103
|
+
this.next();
|
|
2104
|
+
return this.rp(new CS.Range(inclusive, e, right), t);
|
|
2105
|
+
}
|
|
2106
|
+
if (this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
2107
|
+
this.next();
|
|
2108
|
+
e = this.rp(new CS.Spread(e), mstart);
|
|
2109
|
+
}
|
|
2110
|
+
members.push(e);
|
|
2111
|
+
}
|
|
2112
|
+
first = false;
|
|
2113
|
+
if (this.at(NEWLINE)) {
|
|
2114
|
+
this.skip();
|
|
2115
|
+
if (this.at(T.COMMA))
|
|
2116
|
+
this.next();
|
|
2117
|
+
continue;
|
|
2118
|
+
}
|
|
2119
|
+
if (this.at(T.COMMA)) {
|
|
2120
|
+
this.next();
|
|
2121
|
+
if (!(this.kindAt(this.i + 1) === INDENT))
|
|
2122
|
+
this.skipNewline();
|
|
2123
|
+
continue;
|
|
2124
|
+
}
|
|
2125
|
+
break;
|
|
2126
|
+
}
|
|
2127
|
+
if (indented)
|
|
2128
|
+
this.expectDedent();
|
|
2129
|
+
this.skipNewline();
|
|
2130
|
+
if (!this.at(T.RBRACKET))
|
|
2131
|
+
this.fail('expected ] to close the [ on line ' + this.lines[t] + ', column ' + this.cols[t] + ', found ' + this.describe(this.i), null, {
|
|
2132
|
+
code: 'MS1109',
|
|
2133
|
+
opener: t,
|
|
2134
|
+
openerLabel: 'the [ was opened here',
|
|
2135
|
+
why: 'Brackets must be balanced; this [ is still open.',
|
|
2136
|
+
fix: 'add the closing ], or separate the array members with commas or newlines.'
|
|
2137
|
+
});
|
|
2138
|
+
this.next();
|
|
2139
|
+
return this.rp(new CS.ArrayInitialiser(members), t);
|
|
2140
|
+
};
|
|
2141
|
+
Parser.prototype.parseObjectKey = function () {
|
|
2142
|
+
var k, node, t;
|
|
2143
|
+
t = this.i;
|
|
2144
|
+
k = this.kind();
|
|
2145
|
+
if (isWordKind(k)) {
|
|
2146
|
+
this.next();
|
|
2147
|
+
return this.rp(new CS.Identifier(this.text(t)), t);
|
|
2148
|
+
}
|
|
2149
|
+
if (k === T.NUMBER_INT || k === T.NUMBER_FLOAT)
|
|
2150
|
+
return this.parsePrimary(false);
|
|
2151
|
+
if (k === T.STRING_START) {
|
|
2152
|
+
node = this.parseString();
|
|
2153
|
+
if (!(node instanceof CS.String))
|
|
2154
|
+
this.fail('an object key cannot be an interpolated string', t, {
|
|
2155
|
+
code: 'MS1114',
|
|
2156
|
+
why: 'Object keys are names, plain strings or numbers; an interpolated string is computed at run time.',
|
|
2157
|
+
fix: 'build the object first and assign the computed key afterwards: obj["#{key}"] = value.'
|
|
2158
|
+
});
|
|
2159
|
+
return node;
|
|
2160
|
+
}
|
|
2161
|
+
return this.fail('expected an object key, found ' + this.describe(this.i), null, {
|
|
2162
|
+
code: 'MS1113',
|
|
2163
|
+
why: 'Inside {\u2026} each member is key: value or a shorthand name.',
|
|
2164
|
+
fix: 'add a key before the colon, or remove the stray comma.'
|
|
2165
|
+
});
|
|
2166
|
+
};
|
|
2167
|
+
Parser.prototype.parseObjectLiteral = function () {
|
|
2168
|
+
var indented, k, members, t;
|
|
2169
|
+
t = this.i;
|
|
2170
|
+
this.next();
|
|
2171
|
+
members = [];
|
|
2172
|
+
indented = false;
|
|
2173
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2174
|
+
this.skip();
|
|
2175
|
+
this.skip();
|
|
2176
|
+
indented = true;
|
|
2177
|
+
}
|
|
2178
|
+
while (true) {
|
|
2179
|
+
k = this.kind();
|
|
2180
|
+
if (k === T.RBRACE || k === DEDENT)
|
|
2181
|
+
break;
|
|
2182
|
+
if (k === NEWLINE && this.kindAt(this.i + 1) === T.RBRACE) {
|
|
2183
|
+
this.skip();
|
|
2184
|
+
break;
|
|
2185
|
+
}
|
|
2186
|
+
members.push(this.parseObjectLiteralMember());
|
|
2187
|
+
if (this.at(NEWLINE)) {
|
|
2188
|
+
this.skip();
|
|
2189
|
+
if (this.at(T.COMMA))
|
|
2190
|
+
this.next();
|
|
2191
|
+
continue;
|
|
2192
|
+
}
|
|
2193
|
+
if (this.at(T.COMMA)) {
|
|
2194
|
+
this.next();
|
|
2195
|
+
if (!(this.kindAt(this.i + 1) === INDENT))
|
|
2196
|
+
this.skipNewline();
|
|
2197
|
+
continue;
|
|
2198
|
+
}
|
|
2199
|
+
break;
|
|
2200
|
+
}
|
|
2201
|
+
if (indented)
|
|
2202
|
+
this.expectDedent();
|
|
2203
|
+
this.skipNewline();
|
|
2204
|
+
if (!this.at(T.RBRACE))
|
|
2205
|
+
this.fail('expected } to close the { on line ' + this.lines[t] + ', column ' + this.cols[t] + ', found ' + this.describe(this.i), null, {
|
|
2206
|
+
code: 'MS1110',
|
|
2207
|
+
opener: t,
|
|
2208
|
+
openerLabel: 'the { was opened here',
|
|
2209
|
+
why: 'Braces must be balanced; this { is still open.',
|
|
2210
|
+
fix: 'add the closing }, or separate the members with commas or newlines.'
|
|
2211
|
+
});
|
|
2212
|
+
this.next();
|
|
2213
|
+
return this.rp(new CS.ObjectInitialiser(members), t);
|
|
2214
|
+
};
|
|
2215
|
+
Parser.prototype.parseObjectLiteralMember = function () {
|
|
2216
|
+
var d, dp, key, keyExpr, m, t, v, val, valueWord;
|
|
2217
|
+
t = this.i;
|
|
2218
|
+
if (this.at(T.LBRACKET)) {
|
|
2219
|
+
this.next();
|
|
2220
|
+
keyExpr = this.parseExpression();
|
|
2221
|
+
this.expect(T.RBRACKET, '] to close the computed key');
|
|
2222
|
+
this.expect(T.COLON, ': after the computed key');
|
|
2223
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2224
|
+
this.skip();
|
|
2225
|
+
this.skip();
|
|
2226
|
+
val = this.parseExpression();
|
|
2227
|
+
this.expectDedent();
|
|
2228
|
+
} else {
|
|
2229
|
+
val = this.parseExpression();
|
|
2230
|
+
}
|
|
2231
|
+
m = this.rp(new CS.ObjectInitialiserMember(keyExpr, val), t);
|
|
2232
|
+
m.computed = true;
|
|
2233
|
+
return m;
|
|
2234
|
+
}
|
|
2235
|
+
if (this.at(T.ELLIPSIS))
|
|
2236
|
+
this.fail('write the spread as name... (the dots go after the expression)', null, {
|
|
2237
|
+
code: 'MS1128',
|
|
2238
|
+
why: 'MambaScript spreads with trailing dots, like f args... and [first, rest...]; JavaScript puts them in front.',
|
|
2239
|
+
fix: 'move the dots after the expression: {options..., debug: yes}.'
|
|
2240
|
+
});
|
|
2241
|
+
valueWord = in$(this.kindAt(this.i), [
|
|
2242
|
+
T.TRUE,
|
|
2243
|
+
T.FALSE,
|
|
2244
|
+
T.NULL,
|
|
2245
|
+
T.UNDEFINED,
|
|
2246
|
+
T.THIS
|
|
2247
|
+
]) && this.kindAt(this.i + 1) === T.ELLIPSIS;
|
|
2248
|
+
if (this.at(T.LBRACE) || this.at(T.LPAREN) || valueWord) {
|
|
2249
|
+
v = this.parsePrimary(false);
|
|
2250
|
+
v = this.parseChain(v, t, true, false);
|
|
2251
|
+
if (!(this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)))
|
|
2252
|
+
this.fail('expected ... after the object to spread it, found ' + this.describe(this.i), null, {
|
|
2253
|
+
code: 'MS1113',
|
|
2254
|
+
why: 'Inside braces an object or a parenthesised expression can only appear as a spread (expr...); a key is a plain name, string or number.',
|
|
2255
|
+
fix: 'write {source..., key: value}, or give the value a key.'
|
|
2256
|
+
});
|
|
2257
|
+
this.next();
|
|
2258
|
+
return this.rp(new CS.Spread(v), t);
|
|
2259
|
+
}
|
|
2260
|
+
if (this.isObjectKeyStart(t))
|
|
2261
|
+
return this.parseImplicitObjectMember();
|
|
2262
|
+
if (this.at(T.AT) && isWordKind(this.kindAt(this.i + 1)) && !this.spaceBefore(this.i + 1)) {
|
|
2263
|
+
v = this.parsePrimary(false);
|
|
2264
|
+
if (this.at(T.DOT) || this.at(T.LBRACKET) || this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
2265
|
+
v = this.parseChain(v, t, true, false);
|
|
2266
|
+
if (!(this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)))
|
|
2267
|
+
this.fail('expected ... after ' + this.text(t) + this.text(t + 1) + ' to spread it into the object, found ' + this.describe(this.i), null, {
|
|
2268
|
+
code: 'MS1128',
|
|
2269
|
+
why: 'Inside braces a member expression can only appear as a spread (expr...); a key is a plain name, string or number.',
|
|
2270
|
+
fix: 'write {@name...} to spread it, or give it a key: name: @name.'
|
|
2271
|
+
});
|
|
2272
|
+
this.next();
|
|
2273
|
+
return this.rp(new CS.Spread(v), t);
|
|
2274
|
+
}
|
|
2275
|
+
key = this.p(new CS.String(v.memberName).g(), t);
|
|
2276
|
+
return this.rp(new CS.ObjectInitialiserMember(key, v), t);
|
|
2277
|
+
}
|
|
2278
|
+
v = this.parseObjectKey();
|
|
2279
|
+
if (v instanceof CS.Identifier && (this.at(T.DOT) || this.at(T.LBRACKET)) && !this.spaceBefore(this.i)) {
|
|
2280
|
+
v = this.parseChain(v, t, true, false);
|
|
2281
|
+
if (!(this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)))
|
|
2282
|
+
this.fail('expected ... after ' + this.text(t) + (this.at(T.COLON) ? '' : '') + ' to spread it into the object, found ' + this.describe(this.i), null, {
|
|
2283
|
+
code: 'MS1128',
|
|
2284
|
+
why: 'Inside braces a member expression can only appear as a spread (expr...); a key is a plain name, string or number.',
|
|
2285
|
+
fix: 'write ' + this.text(t) + '... to spread it, or give the member a key: name: ' + this.text(t) + '.x.'
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
if (this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
2289
|
+
this.next();
|
|
2290
|
+
return this.rp(new CS.Spread(v), t);
|
|
2291
|
+
}
|
|
2292
|
+
if (this.at(T.ASSIGN) && v instanceof CS.Identifier) {
|
|
2293
|
+
this.next();
|
|
2294
|
+
d = this.parseSecondaryExpression(false);
|
|
2295
|
+
dp = this.rp(new CS.DefaultParam(v, d), t);
|
|
2296
|
+
return this.rp(new CS.ObjectInitialiserMember(v, dp), t);
|
|
2297
|
+
}
|
|
2298
|
+
return this.rp(new CS.ObjectInitialiserMember(v, v), t);
|
|
2299
|
+
};
|
|
2300
|
+
Parser.prototype.parseImplicitObjectMember = function () {
|
|
2301
|
+
var key, t, val;
|
|
2302
|
+
t = this.i;
|
|
2303
|
+
key = this.parseObjectKey();
|
|
2304
|
+
this.expect(T.COLON, ':');
|
|
2305
|
+
if (this.isObjectKeyStart(this.i)) {
|
|
2306
|
+
val = this.parseSingleLineImplicitObjectLiteral();
|
|
2307
|
+
} else if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2308
|
+
this.skip();
|
|
2309
|
+
this.skip();
|
|
2310
|
+
val = this.parseExpression();
|
|
2311
|
+
this.expectDedent();
|
|
2312
|
+
} else {
|
|
2313
|
+
val = this.parseExpression();
|
|
2314
|
+
}
|
|
2315
|
+
return this.rp(new CS.ObjectInitialiserMember(key, val), t);
|
|
2316
|
+
};
|
|
2317
|
+
Parser.prototype.parseImplicitObjectLiteral = function () {
|
|
2318
|
+
var j, members, t;
|
|
2319
|
+
t = this.i;
|
|
2320
|
+
members = [this.parseImplicitObjectMember()];
|
|
2321
|
+
while (true) {
|
|
2322
|
+
if (this.at(NEWLINE)) {
|
|
2323
|
+
j = this.i + 1;
|
|
2324
|
+
if (this.kindAt(j) === T.COMMA)
|
|
2325
|
+
j++;
|
|
2326
|
+
if (!this.isObjectKeyStart(j))
|
|
2327
|
+
break;
|
|
2328
|
+
this.i = j;
|
|
2329
|
+
} else if (this.at(T.COMMA)) {
|
|
2330
|
+
j = this.i + 1;
|
|
2331
|
+
if (this.kindAt(j) === NEWLINE)
|
|
2332
|
+
j++;
|
|
2333
|
+
if (!this.isObjectKeyStart(j))
|
|
2334
|
+
break;
|
|
2335
|
+
this.next();
|
|
2336
|
+
this.i = j;
|
|
2337
|
+
} else {
|
|
2338
|
+
break;
|
|
2339
|
+
}
|
|
2340
|
+
members.push(this.parseImplicitObjectMember());
|
|
2341
|
+
}
|
|
2342
|
+
return this.rp(new CS.ObjectInitialiser(members), t);
|
|
2343
|
+
};
|
|
2344
|
+
Parser.prototype.parseSingleLineImplicitObjectLiteral = function () {
|
|
2345
|
+
var members, t;
|
|
2346
|
+
t = this.i;
|
|
2347
|
+
members = [this.parseImplicitObjectMember()];
|
|
2348
|
+
while (this.at(T.COMMA) && this.isObjectKeyStart(this.i + 1)) {
|
|
2349
|
+
this.next();
|
|
2350
|
+
members.push(this.parseImplicitObjectMember());
|
|
2351
|
+
}
|
|
2352
|
+
return this.rp(new CS.ObjectInitialiser(members), t);
|
|
2353
|
+
};
|
|
2354
|
+
Parser.prototype.parseString = function () {
|
|
2355
|
+
var data, dq, flags, heredoc, interpolated, k, pieces, pt, t;
|
|
2356
|
+
t = this.i;
|
|
2357
|
+
flags = this.flags[t];
|
|
2358
|
+
heredoc = (flags & FLAG_HEREDOC) !== 0;
|
|
2359
|
+
dq = (flags & FLAG_DOUBLE_QUOTE) !== 0;
|
|
2360
|
+
this.next();
|
|
2361
|
+
pieces = [];
|
|
2362
|
+
interpolated = false;
|
|
2363
|
+
while (!this.at(T.STRING_END)) {
|
|
2364
|
+
k = this.kind();
|
|
2365
|
+
if (k === T.STRING_PIECE) {
|
|
2366
|
+
pt = this.next();
|
|
2367
|
+
pieces.push(this.rp(new CS.String(this.vals[pt]), pt));
|
|
2368
|
+
} else if (k === T.INTERP_START) {
|
|
2369
|
+
interpolated = true;
|
|
2370
|
+
this.next();
|
|
2371
|
+
pieces.push(this.parseExpression());
|
|
2372
|
+
this.expect(T.INTERP_END, '} to close the interpolation');
|
|
2373
|
+
} else {
|
|
2374
|
+
this.fail('unexpected ' + this.describe(this.i) + ' inside a string interpolation', null, {
|
|
2375
|
+
code: 'MS1011',
|
|
2376
|
+
why: 'Inside #{\u2026} there is exactly one expression, then the closing }.',
|
|
2377
|
+
fix: 'close the interpolation with }, or check the expression inside it.'
|
|
2378
|
+
});
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2381
|
+
this.next();
|
|
2382
|
+
if (pieces.length === 0)
|
|
2383
|
+
return this.rp(new CS.String(''), t);
|
|
2384
|
+
if (dq) {
|
|
2385
|
+
if (heredoc)
|
|
2386
|
+
stripLeadingWhitespaceInterpolation(pieces);
|
|
2387
|
+
return this.rp(createInterpolationIn(pieces, this.raw), t);
|
|
2388
|
+
}
|
|
2389
|
+
data = pieces[0].data;
|
|
2390
|
+
if (heredoc)
|
|
2391
|
+
data = stripLeadingWhitespace(data);
|
|
2392
|
+
return this.rp(new CS.String(data), t);
|
|
2393
|
+
};
|
|
2394
|
+
Parser.prototype.parseTemplatePieces = function () {
|
|
2395
|
+
var dq, flags, heredoc, k, pieces, pt, t;
|
|
2396
|
+
t = this.i;
|
|
2397
|
+
flags = this.flags[t];
|
|
2398
|
+
heredoc = (flags & FLAG_HEREDOC) !== 0;
|
|
2399
|
+
dq = (flags & FLAG_DOUBLE_QUOTE) !== 0;
|
|
2400
|
+
this.next();
|
|
2401
|
+
pieces = [];
|
|
2402
|
+
while (!this.at(T.STRING_END)) {
|
|
2403
|
+
k = this.kind();
|
|
2404
|
+
if (k === T.STRING_PIECE) {
|
|
2405
|
+
pt = this.next();
|
|
2406
|
+
pieces.push(this.rp(new CS.String(this.vals[pt]), pt));
|
|
2407
|
+
} else if (k === T.INTERP_START) {
|
|
2408
|
+
this.next();
|
|
2409
|
+
pieces.push({ expr: this.parseExpression() });
|
|
2410
|
+
this.expect(T.INTERP_END, '} to close the interpolation');
|
|
2411
|
+
} else {
|
|
2412
|
+
this.fail('unexpected ' + this.describe(this.i) + ' inside a string interpolation', null, {
|
|
2413
|
+
code: 'MS1011',
|
|
2414
|
+
why: 'Inside #{\u2026} there is exactly one expression, then the closing }.',
|
|
2415
|
+
fix: 'close the interpolation with }, or check the expression inside it.'
|
|
2416
|
+
});
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
this.next();
|
|
2420
|
+
if (heredoc)
|
|
2421
|
+
if (dq) {
|
|
2422
|
+
stripLeadingWhitespaceInterpolation(pieces);
|
|
2423
|
+
} else if (pieces.length) {
|
|
2424
|
+
pieces[0].data = stripLeadingWhitespace(pieces[0].data);
|
|
2425
|
+
}
|
|
2426
|
+
return templateParts(pieces);
|
|
2427
|
+
};
|
|
2428
|
+
Parser.prototype.parseHeregex = function () {
|
|
2429
|
+
var classStart, endTok, flags, interp, k, pieces, pt, s, sub, t;
|
|
2430
|
+
t = this.i;
|
|
2431
|
+
this.next();
|
|
2432
|
+
pieces = [];
|
|
2433
|
+
classStart = -1;
|
|
2434
|
+
while (!this.at(T.HEREGEX_END)) {
|
|
2435
|
+
k = this.kind();
|
|
2436
|
+
if (k === T.HEREGEX_PIECE) {
|
|
2437
|
+
pt = this.next();
|
|
2438
|
+
sub = this.flags[pt] & HEREGEX_SUBKIND_MASK;
|
|
2439
|
+
s = new CS.String(this.vals[pt]);
|
|
2440
|
+
if (sub === HEREGEX_WS || sub === HEREGEX_TEXT) {
|
|
2441
|
+
pieces.push(this.rp(s.g(), pt));
|
|
2442
|
+
} else if (sub === HEREGEX_CLASS_OPEN) {
|
|
2443
|
+
classStart = pt;
|
|
2444
|
+
pieces.push(this.p(s, pt));
|
|
2445
|
+
} else if (sub === HEREGEX_CLASS || sub === HEREGEX_CLASS_CLOSE) {
|
|
2446
|
+
pieces.push(this.p(s, classStart === -1 ? pt : classStart));
|
|
2447
|
+
if (sub === HEREGEX_CLASS_CLOSE)
|
|
2448
|
+
classStart = -1;
|
|
2449
|
+
} else {
|
|
2450
|
+
pieces.push(this.rp(s, pt));
|
|
2451
|
+
}
|
|
2452
|
+
} else if (k === T.INTERP_START) {
|
|
2453
|
+
this.next();
|
|
2454
|
+
pieces.push(this.parseExpression());
|
|
2455
|
+
this.expect(T.INTERP_END, '} to close the interpolation');
|
|
2456
|
+
} else {
|
|
2457
|
+
this.fail('unexpected ' + this.describe(this.i) + ' inside a heregex interpolation', null, {
|
|
2458
|
+
code: 'MS1018',
|
|
2459
|
+
why: 'Inside #{\u2026} in a heregex there is exactly one expression, then the closing }.',
|
|
2460
|
+
fix: 'close the interpolation with }, or check the expression inside it.'
|
|
2461
|
+
});
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
endTok = this.next();
|
|
2465
|
+
flags = this.vals[endTok].split('');
|
|
2466
|
+
interp = pieces.length === 0 ? new CS.String('') : createInterpolationIn(pieces, this.raw);
|
|
2467
|
+
if (interp instanceof CS.String)
|
|
2468
|
+
return this.p(new CS.RegExp(interp.data, flags), t);
|
|
2469
|
+
return this.rp(new CS.HeregExp(interp, flags), t);
|
|
2470
|
+
};
|
|
2471
|
+
Parser.prototype.parseFunctionLiteral = function () {
|
|
2472
|
+
var argTypes, body, ctor, k, node, param, params, returnType, t;
|
|
2473
|
+
t = this.i;
|
|
2474
|
+
params = [];
|
|
2475
|
+
if (this.at(T.LPAREN)) {
|
|
2476
|
+
this.next();
|
|
2477
|
+
if (!this.at(T.RPAREN))
|
|
2478
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2479
|
+
this.skip();
|
|
2480
|
+
this.skip();
|
|
2481
|
+
params = this.parseParameterList();
|
|
2482
|
+
this.expectDedent();
|
|
2483
|
+
this.skipNewline();
|
|
2484
|
+
} else {
|
|
2485
|
+
params = this.parseParameterList();
|
|
2486
|
+
}
|
|
2487
|
+
if (!this.at(T.RPAREN))
|
|
2488
|
+
this.fail('expected ) to close the parameter list that started on line ' + this.lines[t] + ', found ' + this.describe(this.i), null, {
|
|
2489
|
+
code: 'MS1108',
|
|
2490
|
+
opener: t,
|
|
2491
|
+
openerLabel: 'the parameter list was opened here',
|
|
2492
|
+
why: 'A parameter list is (a, b = 1, rest...) followed by -> or =>.',
|
|
2493
|
+
fix: 'add the closing ), or separate the parameters with commas.'
|
|
2494
|
+
});
|
|
2495
|
+
this.next();
|
|
2496
|
+
}
|
|
2497
|
+
returnType = null;
|
|
2498
|
+
if (this.at(T.DCOLON)) {
|
|
2499
|
+
this.next();
|
|
2500
|
+
if (!(this.at(T.ARROW) || this.at(T.FAT_ARROW)))
|
|
2501
|
+
returnType = this.parseReturnTypeExpr();
|
|
2502
|
+
}
|
|
2503
|
+
k = this.kind();
|
|
2504
|
+
if (!(k === T.ARROW || k === T.FAT_ARROW))
|
|
2505
|
+
this.fail('expected -> or => after the parameter list, found ' + this.describe(this.i), null, {
|
|
2506
|
+
code: 'MS1116',
|
|
2507
|
+
why: 'A parameter list in parentheses must be followed by -> or => to make a function; otherwise the parentheses are just a grouped expression.',
|
|
2508
|
+
fix: 'add -> after the ), or remove the parentheses if you meant a value.'
|
|
2509
|
+
});
|
|
2510
|
+
this.next();
|
|
2511
|
+
body = null;
|
|
2512
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2513
|
+
this.skip();
|
|
2514
|
+
this.skip();
|
|
2515
|
+
body = this.parseBlock(false);
|
|
2516
|
+
this.expectDedent();
|
|
2517
|
+
} else if (this.canStartExpression(this.i) || CAN_START_STATEMENT[this.kind()] === 1) {
|
|
2518
|
+
body = this.parseStatement();
|
|
2519
|
+
}
|
|
2520
|
+
ctor = k === T.ARROW ? CS.Function : CS.BoundFunction;
|
|
2521
|
+
node = this.rp(new ctor(params, body), t);
|
|
2522
|
+
argTypes = [];
|
|
2523
|
+
for (var i$3 = 0, length$3 = params.length; i$3 < length$3; ++i$3) {
|
|
2524
|
+
param = params[i$3];
|
|
2525
|
+
argTypes.push(null != param.typeAnnotation ? param.typeAnnotation : null);
|
|
2526
|
+
}
|
|
2527
|
+
node.typeAnnotation = {
|
|
2528
|
+
nodeType: 'functionType',
|
|
2529
|
+
returnType: returnType,
|
|
2530
|
+
'arguments': argTypes
|
|
2531
|
+
};
|
|
2532
|
+
return node;
|
|
2533
|
+
};
|
|
2534
|
+
Parser.prototype.parseParameterList = function () {
|
|
2535
|
+
var a, d, params, start;
|
|
2536
|
+
params = [];
|
|
2537
|
+
while (true) {
|
|
2538
|
+
start = this.i;
|
|
2539
|
+
a = this.parseAssignable();
|
|
2540
|
+
if (this.at(T.ASSIGN)) {
|
|
2541
|
+
this.next();
|
|
2542
|
+
d = this.parseSecondaryExpression(false);
|
|
2543
|
+
a = this.rp(new CS.DefaultParam(a, d), start);
|
|
2544
|
+
} else if (this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
2545
|
+
this.next();
|
|
2546
|
+
a = this.rp(new CS.Rest(a), start);
|
|
2547
|
+
}
|
|
2548
|
+
params.push(a);
|
|
2549
|
+
if (this.at(T.COMMA)) {
|
|
2550
|
+
this.next();
|
|
2551
|
+
if (!(this.kindAt(this.i + 1) === DEDENT))
|
|
2552
|
+
this.skipNewline();
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) !== DEDENT && this.kindAt(this.i + 1) !== T.RPAREN) {
|
|
2556
|
+
this.skip();
|
|
2557
|
+
continue;
|
|
2558
|
+
}
|
|
2559
|
+
break;
|
|
2560
|
+
}
|
|
2561
|
+
return params;
|
|
2562
|
+
};
|
|
2563
|
+
Parser.prototype.parseAssignable = function () {
|
|
2564
|
+
var dcolon, k, node, t, type;
|
|
2565
|
+
t = this.i;
|
|
2566
|
+
k = this.kind();
|
|
2567
|
+
if (k === T.LBRACKET || k === T.LBRACE) {
|
|
2568
|
+
node = this.parsePrimary(true);
|
|
2569
|
+
return this.toAssignable(node);
|
|
2570
|
+
}
|
|
2571
|
+
if (!(k === IDENT || k === T.AT || k === T.THIS || isWordKind(k) || CAN_START[k] === 1))
|
|
2572
|
+
this.fail('expected a variable, property or destructuring pattern, found ' + this.describe(this.i), null, {
|
|
2573
|
+
code: 'MS1105',
|
|
2574
|
+
why: 'This position names where a value goes: a variable, a property (a.b or a[i]) or a pattern ({a, b} or [x, y]).',
|
|
2575
|
+
fix: 'write a name here.'
|
|
2576
|
+
});
|
|
2577
|
+
node = this.parsePrimary(true);
|
|
2578
|
+
node = this.parseChain(node, t, true, false);
|
|
2579
|
+
if (node instanceof CS.Identifier) {
|
|
2580
|
+
if (node.data === 'arguments' || node.data === 'eval')
|
|
2581
|
+
this.fail('' + node.data + ' cannot be used as an assignment target', t, {
|
|
2582
|
+
code: 'MS1106',
|
|
2583
|
+
why: '' + node.data + ' is a keyword or a literal value; it cannot hold a value.',
|
|
2584
|
+
fix: 'choose a name that is not a keyword (the reserved words are listed in the docs).'
|
|
2585
|
+
});
|
|
2586
|
+
if (this.at(T.DCOLON) && this.spaceBefore(this.i)) {
|
|
2587
|
+
dcolon = this.next();
|
|
2588
|
+
type = this.parseTypeExpr();
|
|
2589
|
+
node.typeAnnotation = this.rp(type, dcolon);
|
|
2590
|
+
} else {
|
|
2591
|
+
node.typeAnnotation = null;
|
|
2592
|
+
}
|
|
2593
|
+
return this.rp(node, t);
|
|
2594
|
+
}
|
|
2595
|
+
if (node instanceof CS.MemberAccessOp && node.expression instanceof CS.This && this.at(T.DCOLON) && this.spaceBefore(this.i)) {
|
|
2596
|
+
dcolon = this.next();
|
|
2597
|
+
type = this.parseTypeExpr();
|
|
2598
|
+
node.typeAnnotation = this.rp(type, dcolon);
|
|
2599
|
+
return this.rp(node, t);
|
|
2600
|
+
}
|
|
2601
|
+
return this.toAssignable(node);
|
|
2602
|
+
};
|
|
2603
|
+
Parser.prototype.parseConditionalBody = function () {
|
|
2604
|
+
var b;
|
|
2605
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2606
|
+
this.skip();
|
|
2607
|
+
this.skip();
|
|
2608
|
+
b = this.parseBlock(false);
|
|
2609
|
+
this.expectDedent();
|
|
2610
|
+
return b;
|
|
2611
|
+
}
|
|
2612
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === T.THEN)
|
|
2613
|
+
this.skip();
|
|
2614
|
+
if (this.at(T.THEN)) {
|
|
2615
|
+
this.next();
|
|
2616
|
+
if (this.canStartExpression(this.i) || CAN_START_STATEMENT[this.kind()] === 1)
|
|
2617
|
+
return this.parseStatement();
|
|
2618
|
+
return null;
|
|
2619
|
+
}
|
|
2620
|
+
return this.fail('expected an indented block or then, found ' + this.describe(this.i), null, {
|
|
2621
|
+
code: 'MS1119',
|
|
2622
|
+
why: 'if, unless, while, until, forEvery and consider need a body: an indented block on the next line, or then followed by one statement on the same line.',
|
|
2623
|
+
fix: 'indent the body on the next line, or write then and the statement on this line.'
|
|
2624
|
+
});
|
|
2625
|
+
};
|
|
2626
|
+
Parser.prototype.parseFunctionBody = function () {
|
|
2627
|
+
var b;
|
|
2628
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2629
|
+
this.skip();
|
|
2630
|
+
this.skip();
|
|
2631
|
+
b = this.parseBlock(false);
|
|
2632
|
+
this.expectDedent();
|
|
2633
|
+
return b;
|
|
2634
|
+
}
|
|
2635
|
+
return this.parseStatement();
|
|
2636
|
+
};
|
|
2637
|
+
Parser.prototype.parseConditional = function () {
|
|
2638
|
+
var alt, body, cond, negated, t;
|
|
2639
|
+
t = this.i;
|
|
2640
|
+
negated = this.at(T.UNLESS);
|
|
2641
|
+
this.next();
|
|
2642
|
+
cond = this.parseAssignment(true);
|
|
2643
|
+
body = this.parseConditionalBody();
|
|
2644
|
+
alt = null;
|
|
2645
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === T.ELSE)
|
|
2646
|
+
this.skip();
|
|
2647
|
+
if (this.at(T.ELSE)) {
|
|
2648
|
+
this.next();
|
|
2649
|
+
alt = this.parseFunctionBody();
|
|
2650
|
+
}
|
|
2651
|
+
if (negated) {
|
|
2652
|
+
cond = new CS.LogicalNotOp(cond).g();
|
|
2653
|
+
return this.rp(new CS.NegatedConditional(cond, body, alt), t);
|
|
2654
|
+
}
|
|
2655
|
+
return this.rp(new CS.Conditional(cond, body, alt), t);
|
|
2656
|
+
};
|
|
2657
|
+
Parser.prototype.parseWhile = function () {
|
|
2658
|
+
var body, cond, negated, t;
|
|
2659
|
+
t = this.i;
|
|
2660
|
+
negated = this.at(T.UNTIL);
|
|
2661
|
+
this.next();
|
|
2662
|
+
cond = this.parseAssignment(true);
|
|
2663
|
+
body = this.parseConditionalBody();
|
|
2664
|
+
if (negated) {
|
|
2665
|
+
cond = new CS.LogicalNotOp(cond).g();
|
|
2666
|
+
return this.rp(new CS.NegatedWhile(cond, body), t);
|
|
2667
|
+
}
|
|
2668
|
+
return this.rp(new CS.While(cond, body), t);
|
|
2669
|
+
};
|
|
2670
|
+
Parser.prototype.parseLoop = function () {
|
|
2671
|
+
var body, t;
|
|
2672
|
+
t = this.i;
|
|
2673
|
+
this.next();
|
|
2674
|
+
body = this.parseConditionalBody();
|
|
2675
|
+
return this.rp(new CS.Loop(body), t);
|
|
2676
|
+
};
|
|
2677
|
+
Parser.prototype.parseTry = function () {
|
|
2678
|
+
var assignee, body, catchBody, finallyBody, t;
|
|
2679
|
+
t = this.i;
|
|
2680
|
+
this.next();
|
|
2681
|
+
body = this.parseFunctionBody();
|
|
2682
|
+
assignee = null;
|
|
2683
|
+
catchBody = null;
|
|
2684
|
+
finallyBody = null;
|
|
2685
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === T.CATCH)
|
|
2686
|
+
this.skip();
|
|
2687
|
+
if (this.at(T.CATCH)) {
|
|
2688
|
+
this.next();
|
|
2689
|
+
if (this.canStartExpression(this.i) && !this.at(T.THEN))
|
|
2690
|
+
assignee = this.parseAssignable();
|
|
2691
|
+
if (this.at(T.THEN) || this.at(NEWLINE) && (this.kindAt(this.i + 1) === INDENT || this.kindAt(this.i + 1) === T.THEN)) {
|
|
2692
|
+
catchBody = this.parseConditionalBody();
|
|
2693
|
+
} else {
|
|
2694
|
+
catchBody = new CS.Block([]);
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === T.FINALLY)
|
|
2698
|
+
this.skip();
|
|
2699
|
+
if (this.at(T.FINALLY)) {
|
|
2700
|
+
this.next();
|
|
2701
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT || this.canStartExpression(this.i) || CAN_START_STATEMENT[this.kind()] === 1)
|
|
2702
|
+
finallyBody = this.parseFunctionBody();
|
|
2703
|
+
}
|
|
2704
|
+
return this.rp(new CS.Try(body, assignee, catchBody, finallyBody), t);
|
|
2705
|
+
};
|
|
2706
|
+
Parser.prototype.parseSwitch = function () {
|
|
2707
|
+
var alt, cases, e, t;
|
|
2708
|
+
t = this.i;
|
|
2709
|
+
this.next();
|
|
2710
|
+
e = null;
|
|
2711
|
+
if (!(this.at(NEWLINE) || this.at(T.THEN))) {
|
|
2712
|
+
if (this.startsExpressionworthy(this.i))
|
|
2713
|
+
e = this.parseExpressionworthyOrNull(false);
|
|
2714
|
+
if (!(null != e))
|
|
2715
|
+
e = this.parseAssignment(false);
|
|
2716
|
+
}
|
|
2717
|
+
cases = [];
|
|
2718
|
+
alt = null;
|
|
2719
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2720
|
+
this.skip();
|
|
2721
|
+
this.skip();
|
|
2722
|
+
while (true) {
|
|
2723
|
+
if (this.at(T.WHEN)) {
|
|
2724
|
+
cases.push(this.parseCase());
|
|
2725
|
+
} else if (this.at(T.ELSE)) {
|
|
2726
|
+
this.next();
|
|
2727
|
+
alt = this.parseFunctionBody();
|
|
2728
|
+
} else {
|
|
2729
|
+
this.fail('expected when or otherwise inside the consider block, found ' + this.describe(this.i), null, {
|
|
2730
|
+
code: 'MS1120',
|
|
2731
|
+
why: 'Inside consider every branch starts with when value (one or more values, comma separated) or otherwise.',
|
|
2732
|
+
fix: 'start this line with when, or outdent it to leave the consider block.'
|
|
2733
|
+
});
|
|
2734
|
+
}
|
|
2735
|
+
if (this.at(NEWLINE) && (this.kindAt(this.i + 1) === T.WHEN || this.kindAt(this.i + 1) === T.ELSE)) {
|
|
2736
|
+
this.skip();
|
|
2737
|
+
continue;
|
|
2738
|
+
}
|
|
2739
|
+
break;
|
|
2740
|
+
}
|
|
2741
|
+
this.expectDedent();
|
|
2742
|
+
} else if (this.at(T.THEN)) {
|
|
2743
|
+
this.next();
|
|
2744
|
+
if (this.at(T.WHEN))
|
|
2745
|
+
cases.push(this.parseCase());
|
|
2746
|
+
} else {
|
|
2747
|
+
this.fail('expected an indented block of when branches, found ' + this.describe(this.i), null, {
|
|
2748
|
+
code: 'MS1120',
|
|
2749
|
+
why: 'consider needs at least one when branch, indented under it.',
|
|
2750
|
+
fix: 'add an indented when value line with its body.'
|
|
2751
|
+
});
|
|
2752
|
+
}
|
|
2753
|
+
return this.rp(new CS.Switch(e, cases, alt), t);
|
|
2754
|
+
};
|
|
2755
|
+
Parser.prototype.parseCase = function () {
|
|
2756
|
+
var body, conditions, t;
|
|
2757
|
+
t = this.i;
|
|
2758
|
+
this.expect(T.WHEN, 'when');
|
|
2759
|
+
conditions = [this.parseAssignment(true)];
|
|
2760
|
+
while (this.at(T.COMMA)) {
|
|
2761
|
+
this.next();
|
|
2762
|
+
conditions.push(this.parseAssignment(true));
|
|
2763
|
+
}
|
|
2764
|
+
body = this.parseConditionalBody();
|
|
2765
|
+
return this.rp(new CS.SwitchCase(conditions, body), t);
|
|
2766
|
+
};
|
|
2767
|
+
Parser.prototype.parseForHead = function (isEvery, prefix) {
|
|
2768
|
+
var filter, first, fromTok, isAwait, k, nk, own, second, step, target;
|
|
2769
|
+
own = false;
|
|
2770
|
+
isAwait = false;
|
|
2771
|
+
if (this.at(T.WAITFORIT)) {
|
|
2772
|
+
this.next();
|
|
2773
|
+
isAwait = true;
|
|
2774
|
+
}
|
|
2775
|
+
k = this.kind();
|
|
2776
|
+
if (k === IDENT && this.text(this.i) === 'own') {
|
|
2777
|
+
nk = this.kindAt(this.i + 1);
|
|
2778
|
+
if ((nk === IDENT || nk === T.AT || nk === T.LBRACKET || nk === T.LBRACE) && !(nk === T.OF || nk === T.IN)) {
|
|
2779
|
+
own = true;
|
|
2780
|
+
this.next();
|
|
2781
|
+
}
|
|
2782
|
+
}
|
|
2783
|
+
first = null;
|
|
2784
|
+
second = null;
|
|
2785
|
+
if (!this.at(T.IN)) {
|
|
2786
|
+
first = this.parseAssignable();
|
|
2787
|
+
if (this.at(T.COMMA)) {
|
|
2788
|
+
this.next();
|
|
2789
|
+
second = this.parseAssignable();
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
if (this.at(IDENT) && this.text(this.i) === 'from') {
|
|
2793
|
+
fromTok = this.i;
|
|
2794
|
+
if (null != second)
|
|
2795
|
+
this.fail('from loops take one variable', null, {
|
|
2796
|
+
code: 'MS4003',
|
|
2797
|
+
why: 'from walks an iterator (a generator, a Map, a string \u2026); an iterator hands out values one at a time, so there is no index or key to bind.',
|
|
2798
|
+
fix: 'keep one variable (forEvery item from source) and count with a variable of your own, or use in over an array for an index.'
|
|
2799
|
+
});
|
|
2800
|
+
if (own)
|
|
2801
|
+
this.fail('own has no meaning with from', null, {
|
|
2802
|
+
code: 'MS4003',
|
|
2803
|
+
why: 'own filters the keys an object owns; an iterator has no keys.',
|
|
2804
|
+
fix: 'remove own.'
|
|
2805
|
+
});
|
|
2806
|
+
this.next();
|
|
2807
|
+
target = this.parseAssignment(prefix);
|
|
2808
|
+
if (this.at(T.BY))
|
|
2809
|
+
this.fail('by has no meaning with from', null, {
|
|
2810
|
+
code: 'MS4003',
|
|
2811
|
+
why: 'An iterator hands out values one at a time; it cannot be stepped.',
|
|
2812
|
+
fix: 'remove by, or filter with when.'
|
|
2813
|
+
});
|
|
2814
|
+
filter = null;
|
|
2815
|
+
if (this.at(T.WHEN)) {
|
|
2816
|
+
this.next();
|
|
2817
|
+
filter = this.parseAssignment(prefix);
|
|
2818
|
+
}
|
|
2819
|
+
return {
|
|
2820
|
+
isFrom: true,
|
|
2821
|
+
isAwait: isAwait,
|
|
2822
|
+
val: first,
|
|
2823
|
+
target: target,
|
|
2824
|
+
filter: filter
|
|
2825
|
+
};
|
|
2826
|
+
}
|
|
2827
|
+
if (isAwait)
|
|
2828
|
+
this.fail('waitForIt in a loop header needs from', null, {
|
|
2829
|
+
code: 'MS4003',
|
|
2830
|
+
why: 'forEvery waitForIt x from source walks an async iterable; in and of walk plain arrays and objects, which need no waiting.',
|
|
2831
|
+
fix: 'write forEvery waitForIt item from stream, or drop waitForIt.'
|
|
2832
|
+
});
|
|
2833
|
+
if (this.at(T.IN)) {
|
|
2834
|
+
if (own)
|
|
2835
|
+
this.fail('own can only be used with of', null, {
|
|
2836
|
+
code: 'MS1121',
|
|
2837
|
+
why: 'own keeps only the keys an object owns (forEvery own key, value of obj); arrays and iterables have no own keys to filter.',
|
|
2838
|
+
fix: 'remove own, or loop with of over an object.'
|
|
2839
|
+
});
|
|
2840
|
+
this.next();
|
|
2841
|
+
target = this.parseAssignment(prefix);
|
|
2842
|
+
step = null;
|
|
2843
|
+
filter = null;
|
|
2844
|
+
if (this.at(T.BY)) {
|
|
2845
|
+
this.next();
|
|
2846
|
+
step = this.parseAssignment(prefix);
|
|
2847
|
+
}
|
|
2848
|
+
if (this.at(T.WHEN)) {
|
|
2849
|
+
this.next();
|
|
2850
|
+
filter = this.parseAssignment(prefix);
|
|
2851
|
+
}
|
|
2852
|
+
if (!(null != step))
|
|
2853
|
+
step = new CS.Int(1).r('1').g();
|
|
2854
|
+
return {
|
|
2855
|
+
isIn: true,
|
|
2856
|
+
val: first,
|
|
2857
|
+
key: second,
|
|
2858
|
+
target: target,
|
|
2859
|
+
step: step,
|
|
2860
|
+
filter: filter
|
|
2861
|
+
};
|
|
2862
|
+
}
|
|
2863
|
+
if (this.at(T.OF)) {
|
|
2864
|
+
if (isEvery && !prefix)
|
|
2865
|
+
this.fail('a key-value loop (of) cannot be written in postfix form', null, {
|
|
2866
|
+
code: 'MS1121',
|
|
2867
|
+
why: 'present k for k, v of obj is ambiguous with a comprehension over values, so the of form is a statement only.',
|
|
2868
|
+
fix: 'write the loop as a statement with an indented body.'
|
|
2869
|
+
});
|
|
2870
|
+
if (!(null != first))
|
|
2871
|
+
this.fail('expected a variable before of', null, {
|
|
2872
|
+
code: 'MS1121',
|
|
2873
|
+
why: 'A key-value loop names at least the key: forEvery key of obj, or forEvery key, value of obj.',
|
|
2874
|
+
fix: 'add the variable name before of.'
|
|
2875
|
+
});
|
|
2876
|
+
this.next();
|
|
2877
|
+
target = this.parseAssignment(prefix);
|
|
2878
|
+
filter = null;
|
|
2879
|
+
if (this.at(T.WHEN)) {
|
|
2880
|
+
this.next();
|
|
2881
|
+
filter = this.parseAssignment(prefix);
|
|
2882
|
+
}
|
|
2883
|
+
return {
|
|
2884
|
+
isIn: false,
|
|
2885
|
+
own: own,
|
|
2886
|
+
key: first,
|
|
2887
|
+
val: second,
|
|
2888
|
+
target: target,
|
|
2889
|
+
filter: filter
|
|
2890
|
+
};
|
|
2891
|
+
}
|
|
2892
|
+
return this.fail('expected in or of in the loop header, found ' + this.describe(this.i), null, {
|
|
2893
|
+
code: 'MS1121',
|
|
2894
|
+
why: 'A loop header is forEvery item in list (values), or forEvery key, value of object (keys).',
|
|
2895
|
+
fix: 'add in or of after the loop variables.'
|
|
2896
|
+
});
|
|
2897
|
+
};
|
|
2898
|
+
Parser.prototype.parseFor = function () {
|
|
2899
|
+
var body, head, isEvery, t;
|
|
2900
|
+
t = this.i;
|
|
2901
|
+
isEvery = this.at(T.FOREVERY);
|
|
2902
|
+
this.next();
|
|
2903
|
+
head = this.parseForHead(isEvery, true);
|
|
2904
|
+
body = this.parseConditionalBody();
|
|
2905
|
+
if (head.isFrom)
|
|
2906
|
+
return this.rp(new CS.ForFrom(head.isAwait, head.val, head.target, head.filter, body), t);
|
|
2907
|
+
if (head.isIn)
|
|
2908
|
+
return this.rp(new CS.ForIn(head.val, head.key, head.target, head.step, head.filter, body), t);
|
|
2909
|
+
return this.rp(new CS.ForOf(head.own, head.key, head.val, head.target, head.filter, body), t);
|
|
2910
|
+
};
|
|
2911
|
+
Parser.prototype.parseClass = function () {
|
|
2912
|
+
var body, boundMembers, ctor, implementArguments, isBlueprint, k, m, name, node, nt, parent, stmts, t, typeArgs;
|
|
2913
|
+
t = this.i;
|
|
2914
|
+
isBlueprint = this.at(T.BLUEPRINT);
|
|
2915
|
+
this.next();
|
|
2916
|
+
name = null;
|
|
2917
|
+
k = this.kind();
|
|
2918
|
+
if (k === IDENT && this.kindAt(this.i + 1) === T.LT && !this.spaceBefore(this.i + 1)) {
|
|
2919
|
+
nt = this.next();
|
|
2920
|
+
name = this.rp(new CS.Identifier(this.text(nt)), nt);
|
|
2921
|
+
name.typeAnnotation = null;
|
|
2922
|
+
} else if (k === IDENT || k === T.AT || k === T.THIS || k === T.LBRACKET || k === T.LBRACE) {
|
|
2923
|
+
name = this.parseAssignable();
|
|
2924
|
+
}
|
|
2925
|
+
typeArgs = null;
|
|
2926
|
+
if (this.at(T.LT) && !this.spaceBefore(this.i))
|
|
2927
|
+
typeArgs = this.speculate(function (this$) {
|
|
2928
|
+
return function () {
|
|
2929
|
+
return this$.parseTypeArgumentList();
|
|
2930
|
+
};
|
|
2931
|
+
}(this));
|
|
2932
|
+
parent = null;
|
|
2933
|
+
if (isBlueprint && this.at(T.INHERITSFROM) || !isBlueprint && this.at(T.EXTENDS)) {
|
|
2934
|
+
this.next();
|
|
2935
|
+
parent = this.parseSecondaryExpression(true);
|
|
2936
|
+
}
|
|
2937
|
+
implementArguments = null;
|
|
2938
|
+
if (this.at(T.IMPLEMENTS)) {
|
|
2939
|
+
this.next();
|
|
2940
|
+
implementArguments = [this.parseTypeIdentifier()];
|
|
2941
|
+
while (this.at(T.COMMA)) {
|
|
2942
|
+
this.next();
|
|
2943
|
+
implementArguments.push(this.parseTypeIdentifier());
|
|
2944
|
+
}
|
|
2945
|
+
}
|
|
2946
|
+
body = null;
|
|
2947
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
2948
|
+
this.skip();
|
|
2949
|
+
this.skip();
|
|
2950
|
+
body = this.parseClassBlock();
|
|
2951
|
+
this.expectDedent();
|
|
2952
|
+
} else if (this.at(T.THEN)) {
|
|
2953
|
+
this.next();
|
|
2954
|
+
body = this.parseClassStatement();
|
|
2955
|
+
} else {
|
|
2956
|
+
body = new CS.Block([]);
|
|
2957
|
+
}
|
|
2958
|
+
ctor = null;
|
|
2959
|
+
boundMembers = [];
|
|
2960
|
+
stmts = null != body.statements ? body.statements : [body];
|
|
2961
|
+
for (var i$3 = 0, length$3 = stmts.length; i$3 < length$3; ++i$3) {
|
|
2962
|
+
m = stmts[i$3];
|
|
2963
|
+
if (m['instanceof'](CS.Constructor)) {
|
|
2964
|
+
ctor = m;
|
|
2965
|
+
} else if (m['instanceof'](CS.ClassProtoAssignOp) && m.expression['instanceof'](CS.BoundFunction)) {
|
|
2966
|
+
boundMembers.push(m);
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
node = this.rp(new CS.Class(name, parent, ctor, body, boundMembers), t);
|
|
2970
|
+
node.implementArguments = implementArguments;
|
|
2971
|
+
node.typeArguments = null != typeArgs ? typeArgs : [];
|
|
2972
|
+
return node;
|
|
2973
|
+
};
|
|
2974
|
+
Parser.prototype.parseClassBlock = function () {
|
|
2975
|
+
var k, start, statements;
|
|
2976
|
+
start = this.i;
|
|
2977
|
+
statements = [];
|
|
2978
|
+
while (true) {
|
|
2979
|
+
statements.push(this.parseClassStatement());
|
|
2980
|
+
if (this.at(NEWLINE)) {
|
|
2981
|
+
this.skip();
|
|
2982
|
+
k = this.kind();
|
|
2983
|
+
if (k === DEDENT || k === EOF)
|
|
2984
|
+
break;
|
|
2985
|
+
continue;
|
|
2986
|
+
}
|
|
2987
|
+
break;
|
|
2988
|
+
}
|
|
2989
|
+
return this.rp(new CS.Block(statements), start);
|
|
2990
|
+
};
|
|
2991
|
+
Parser.prototype.parseClassStatement = function () {
|
|
2992
|
+
var e, f, isCtor, key, t;
|
|
2993
|
+
t = this.i;
|
|
2994
|
+
if (this.isObjectKeyStart(t)) {
|
|
2995
|
+
key = this.parseObjectKey();
|
|
2996
|
+
this.expect(T.COLON, ':');
|
|
2997
|
+
isCtor = (key instanceof CS.Identifier || key instanceof CS.String) && key.data === 'constructor';
|
|
2998
|
+
if (isCtor) {
|
|
2999
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
3000
|
+
this.skip();
|
|
3001
|
+
this.skip();
|
|
3002
|
+
e = this.parseExpression();
|
|
3003
|
+
this.expectDedent();
|
|
3004
|
+
} else {
|
|
3005
|
+
this.skipNewline();
|
|
3006
|
+
e = this.parseExpression();
|
|
3007
|
+
}
|
|
3008
|
+
if (e instanceof CS.BoundFunction) {
|
|
3009
|
+
f = new CS.Function(e.parameters, e.body);
|
|
3010
|
+
if (this.raw)
|
|
3011
|
+
f.raw = e.raw;
|
|
3012
|
+
e = this.c(f, e);
|
|
3013
|
+
}
|
|
3014
|
+
return this.rp(new CS.Constructor(e), t);
|
|
3015
|
+
}
|
|
3016
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
3017
|
+
this.skip();
|
|
3018
|
+
this.skip();
|
|
3019
|
+
e = this.parseExpression();
|
|
3020
|
+
this.expectDedent();
|
|
3021
|
+
} else if (this.isObjectKeyStart(this.i)) {
|
|
3022
|
+
e = this.parseSingleLineImplicitObjectLiteral();
|
|
3023
|
+
} else {
|
|
3024
|
+
this.skipNewline();
|
|
3025
|
+
e = this.parseSecondaryExpression(false);
|
|
3026
|
+
}
|
|
3027
|
+
return this.rp(new CS.ClassProtoAssignOp(key, e), t);
|
|
3028
|
+
}
|
|
3029
|
+
if (this.at(T.AT) && isWordKind(this.kindAt(this.i + 1)) && !this.spaceBefore(this.i + 1) && this.kindAt(this.i + 2) === T.COLON) {
|
|
3030
|
+
key = this.parsePrimary(false);
|
|
3031
|
+
this.expect(T.COLON, ':');
|
|
3032
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
3033
|
+
this.skip();
|
|
3034
|
+
this.skip();
|
|
3035
|
+
e = this.parseExpression();
|
|
3036
|
+
this.expectDedent();
|
|
3037
|
+
} else {
|
|
3038
|
+
this.skipNewline();
|
|
3039
|
+
e = this.parseExpression();
|
|
3040
|
+
}
|
|
3041
|
+
return this.rp(new CS.AssignOp(key, e), t);
|
|
3042
|
+
}
|
|
3043
|
+
return this.parseExpression();
|
|
3044
|
+
};
|
|
3045
|
+
Parser.prototype.parseGrab = function () {
|
|
3046
|
+
var a, isNamespace, node, req, s, t;
|
|
3047
|
+
t = this.i;
|
|
3048
|
+
this.next();
|
|
3049
|
+
if (this.at(T.STRING_START)) {
|
|
3050
|
+
s = this.parseString();
|
|
3051
|
+
req = this.c(new CS.Identifier('require'), s);
|
|
3052
|
+
node = this.rp(new CS.Grab(false, null, s), t);
|
|
3053
|
+
node.cjs = this.rp(new CS.FunctionApplication(req, [s]), t);
|
|
3054
|
+
return node;
|
|
3055
|
+
}
|
|
3056
|
+
isNamespace = false;
|
|
3057
|
+
if (this.at(T.STAR)) {
|
|
3058
|
+
this.next();
|
|
3059
|
+
if (!(this.at(IDENT) && this.text(this.i) === 'as' && this.kindAt(this.i + 1) === IDENT))
|
|
3060
|
+
this.fail('expected as and a name after * in a grab statement', null, {
|
|
3061
|
+
code: 'MS3004',
|
|
3062
|
+
why: "grab * as name from 'module' binds the whole module to one name.",
|
|
3063
|
+
fix: "write grab * as fs from 'fs'."
|
|
3064
|
+
});
|
|
3065
|
+
this.next();
|
|
3066
|
+
a = this.parseAssignable();
|
|
3067
|
+
isNamespace = true;
|
|
3068
|
+
} else {
|
|
3069
|
+
a = this.parseAssignable();
|
|
3070
|
+
}
|
|
3071
|
+
if (!(this.at(IDENT) && this.text(this.i) === 'from'))
|
|
3072
|
+
this.fail('expected from after the name in a grab statement, found ' + this.describe(this.i), null, {
|
|
3073
|
+
code: 'MS1122',
|
|
3074
|
+
why: "grab x from 'module' binds the module's export to x; grab 'module' alone just loads the module.",
|
|
3075
|
+
fix: "add from 'module-name' after the name."
|
|
3076
|
+
});
|
|
3077
|
+
this.next();
|
|
3078
|
+
if (!this.at(T.STRING_START))
|
|
3079
|
+
this.fail('expected a module name in quotes after from, found ' + this.describe(this.i), null, {
|
|
3080
|
+
code: 'MS1122',
|
|
3081
|
+
why: 'The module name is a string, exactly as you would pass it to require.',
|
|
3082
|
+
fix: "write it in quotes: grab join from 'path'."
|
|
3083
|
+
});
|
|
3084
|
+
s = this.parseString();
|
|
3085
|
+
req = this.c(new CS.FunctionApplication(this.c(new CS.Identifier('require'), s), [s]), s);
|
|
3086
|
+
node = this.rp(new CS.Grab(isNamespace, a, s), t);
|
|
3087
|
+
node.cjs = this.rp(new CS.AssignOp(a, req), t);
|
|
3088
|
+
return node;
|
|
3089
|
+
};
|
|
3090
|
+
Parser.prototype.parseShare = function () {
|
|
3091
|
+
var assign, e, et, exportsRef, node, req, s, source, t;
|
|
3092
|
+
t = this.i;
|
|
3093
|
+
this.next();
|
|
3094
|
+
if (this.at(T.STAR)) {
|
|
3095
|
+
this.next();
|
|
3096
|
+
if (!(this.at(IDENT) && this.text(this.i) === 'from'))
|
|
3097
|
+
this.fail('expected from after * in a share statement', null, {
|
|
3098
|
+
code: 'MS3004',
|
|
3099
|
+
why: "share * from 'module' re-exports everything the module exports.",
|
|
3100
|
+
fix: "write share * from './helpers'."
|
|
3101
|
+
});
|
|
3102
|
+
this.next();
|
|
3103
|
+
s = this.parseString();
|
|
3104
|
+
node = this.rp(new CS.Share(true, null, s), t);
|
|
3105
|
+
req = this.c(new CS.FunctionApplication(this.c(new CS.Identifier('require'), s), [s]), s);
|
|
3106
|
+
assign = this.c(new CS.MemberAccessOp(this.c(new CS.Identifier('Object'), s), 'assign'), s);
|
|
3107
|
+
exportsRef = this.c(new CS.MemberAccessOp(this.c(new CS.Identifier('module'), s), 'exports'), s);
|
|
3108
|
+
node.cjs = this.rp(new CS.FunctionApplication(assign, [
|
|
3109
|
+
exportsRef,
|
|
3110
|
+
req
|
|
3111
|
+
]), t);
|
|
3112
|
+
return node;
|
|
3113
|
+
}
|
|
3114
|
+
if (this.at(T.LBRACE)) {
|
|
3115
|
+
et = this.i;
|
|
3116
|
+
e = this.parsePrimary(false);
|
|
3117
|
+
e = this.parseChain(e, et, false, false);
|
|
3118
|
+
} else {
|
|
3119
|
+
e = this.parseSecondaryExpression(false);
|
|
3120
|
+
}
|
|
3121
|
+
source = null;
|
|
3122
|
+
if (e instanceof CS.ObjectInitialiser && this.at(IDENT) && this.text(this.i) === 'from') {
|
|
3123
|
+
this.next();
|
|
3124
|
+
source = this.parseString();
|
|
3125
|
+
}
|
|
3126
|
+
node = this.rp(new CS.Share(false, e, source), t);
|
|
3127
|
+
node.cjs = this.shareDesugar(e, source, t);
|
|
3128
|
+
return node;
|
|
3129
|
+
};
|
|
3130
|
+
Parser.prototype.shareDesugar = function (e, source, t) {
|
|
3131
|
+
var allNamed, assigns, exportsRef, m, req, value;
|
|
3132
|
+
exportsRef = function (this$) {
|
|
3133
|
+
return function () {
|
|
3134
|
+
return this$.c(new CS.MemberAccessOp(this$.c(new CS.Identifier('module'), e), 'exports'), e);
|
|
3135
|
+
};
|
|
3136
|
+
}(this);
|
|
3137
|
+
if (e instanceof CS.ObjectInitialiser && e.members.length > 0) {
|
|
3138
|
+
allNamed = true;
|
|
3139
|
+
for (var i$3 = 0, length$3 = e.members.length; i$3 < length$3; ++i$3) {
|
|
3140
|
+
m = e.members[i$3];
|
|
3141
|
+
if (!(m.key instanceof CS.Identifier || m.key instanceof CS.String))
|
|
3142
|
+
allNamed = false;
|
|
3143
|
+
}
|
|
3144
|
+
if (allNamed) {
|
|
3145
|
+
assigns = function (accum$) {
|
|
3146
|
+
for (var i$4 = 0, length$4 = e.members.length; i$4 < length$4; ++i$4) {
|
|
3147
|
+
m = e.members[i$4];
|
|
3148
|
+
value = null != source ? (req = this.c(new CS.FunctionApplication(this.c(new CS.Identifier('require'), source), [source]), source), this.c(new CS.MemberAccessOp(req, m.key.data), m)) : m.expression;
|
|
3149
|
+
accum$.push(this.c(new CS.AssignOp(this.c(new CS.MemberAccessOp(exportsRef(), m.key.data), m), value), m));
|
|
3150
|
+
}
|
|
3151
|
+
return accum$;
|
|
3152
|
+
}.call(this, []);
|
|
3153
|
+
return this.rp(new CS.Block(assigns), t);
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
return this.rp(new CS.AssignOp(exportsRef(), e), t);
|
|
3157
|
+
};
|
|
3158
|
+
Parser.prototype.parseModule = function () {
|
|
3159
|
+
var block, ident, identStart, t;
|
|
3160
|
+
t = this.i;
|
|
3161
|
+
this.next();
|
|
3162
|
+
identStart = this.i;
|
|
3163
|
+
ident = this.parsePrimary(true);
|
|
3164
|
+
ident = this.parseChain(ident, identStart, true, false);
|
|
3165
|
+
if (!(this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT))
|
|
3166
|
+
this.fail('expected an indented block after the module name', null, {
|
|
3167
|
+
code: 'MS1123',
|
|
3168
|
+
why: 'module Name opens a namespace whose members are declared in an indented block.',
|
|
3169
|
+
fix: 'indent the members on the lines below, or remove the module line.'
|
|
3170
|
+
});
|
|
3171
|
+
this.skip();
|
|
3172
|
+
this.skip();
|
|
3173
|
+
block = this.parseBlock(false);
|
|
3174
|
+
this.expectDedent();
|
|
3175
|
+
return this.rp(new CS.Module(ident, block), t);
|
|
3176
|
+
};
|
|
3177
|
+
Parser.prototype.parseStructDef = function () {
|
|
3178
|
+
var implementArguments, name, props, s, t;
|
|
3179
|
+
t = this.i;
|
|
3180
|
+
this.next();
|
|
3181
|
+
name = this.parseTypeIdentifier();
|
|
3182
|
+
implementArguments = null;
|
|
3183
|
+
if (this.at(T.IMPLEMENTS)) {
|
|
3184
|
+
this.next();
|
|
3185
|
+
implementArguments = [this.parseTypeIdentifier()];
|
|
3186
|
+
while (this.at(T.COMMA)) {
|
|
3187
|
+
this.next();
|
|
3188
|
+
implementArguments.push(this.parseTypeIdentifier());
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
3191
|
+
props = this.parseTypeBlock();
|
|
3192
|
+
s = this.rp(new CS.Int(0), t);
|
|
3193
|
+
s.nodeType = 'struct';
|
|
3194
|
+
s.name = name;
|
|
3195
|
+
s.expr = props;
|
|
3196
|
+
s.implementArguments = implementArguments;
|
|
3197
|
+
return s;
|
|
3198
|
+
};
|
|
3199
|
+
Parser.prototype.parseTypeBlock = function () {
|
|
3200
|
+
var indented, members, open;
|
|
3201
|
+
members = [];
|
|
3202
|
+
if (this.at(T.LBRACE)) {
|
|
3203
|
+
open = this.i;
|
|
3204
|
+
this.next();
|
|
3205
|
+
indented = false;
|
|
3206
|
+
if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
3207
|
+
this.skip();
|
|
3208
|
+
this.skip();
|
|
3209
|
+
indented = true;
|
|
3210
|
+
}
|
|
3211
|
+
members = this.parseTypeBlockMembers();
|
|
3212
|
+
if (indented)
|
|
3213
|
+
this.expectDedent();
|
|
3214
|
+
this.skipNewline();
|
|
3215
|
+
if (!this.at(T.RBRACE))
|
|
3216
|
+
this.fail('expected } to close the type block opened on line ' + this.lines[open] + ', found ' + this.describe(this.i), null, {
|
|
3217
|
+
code: 'MS1123',
|
|
3218
|
+
opener: open,
|
|
3219
|
+
openerLabel: 'the type block was opened here',
|
|
3220
|
+
why: 'An inline struct type is {name :: Type, other :: Type}.',
|
|
3221
|
+
fix: 'add the closing }, or separate the members with commas or newlines.'
|
|
3222
|
+
});
|
|
3223
|
+
this.next();
|
|
3224
|
+
} else if (this.at(NEWLINE) && this.kindAt(this.i + 1) === INDENT) {
|
|
3225
|
+
this.skip();
|
|
3226
|
+
this.skip();
|
|
3227
|
+
members = this.parseTypeBlockMembers();
|
|
3228
|
+
this.expectDedent();
|
|
3229
|
+
} else if (isWordKind(this.kind())) {
|
|
3230
|
+
members = this.parseTypeBlockMembers();
|
|
3231
|
+
}
|
|
3232
|
+
return {
|
|
3233
|
+
properties: members,
|
|
3234
|
+
nodeType: 'members'
|
|
3235
|
+
};
|
|
3236
|
+
};
|
|
3237
|
+
Parser.prototype.parseTypeBlockMembers = function () {
|
|
3238
|
+
var k, members, symbol;
|
|
3239
|
+
members = [];
|
|
3240
|
+
while (true) {
|
|
3241
|
+
k = this.kind();
|
|
3242
|
+
if (k === T.RBRACE || k === DEDENT || k === EOF)
|
|
3243
|
+
break;
|
|
3244
|
+
symbol = this.parseTypeIdentifier();
|
|
3245
|
+
this.expect(T.DCOLON, ':: and a type');
|
|
3246
|
+
symbol.typeAnnotation = this.parseTypeExpr();
|
|
3247
|
+
members.push(symbol);
|
|
3248
|
+
if (this.at(NEWLINE)) {
|
|
3249
|
+
if (this.kindAt(this.i + 1) === T.COMMA) {
|
|
3250
|
+
this.skip();
|
|
3251
|
+
this.next();
|
|
3252
|
+
} else if (this.kindAt(this.i + 1) === DEDENT || this.kindAt(this.i + 1) === T.RBRACE) {
|
|
3253
|
+
break;
|
|
3254
|
+
} else {
|
|
3255
|
+
this.skip();
|
|
3256
|
+
}
|
|
3257
|
+
continue;
|
|
3258
|
+
}
|
|
3259
|
+
if (this.at(T.COMMA)) {
|
|
3260
|
+
this.next();
|
|
3261
|
+
this.skipNewline();
|
|
3262
|
+
continue;
|
|
3263
|
+
}
|
|
3264
|
+
break;
|
|
3265
|
+
}
|
|
3266
|
+
return members;
|
|
3267
|
+
};
|
|
3268
|
+
Parser.prototype.parseTypeExpr = function () {
|
|
3269
|
+
var args, first, id, k, returnType, void_;
|
|
3270
|
+
k = this.kind();
|
|
3271
|
+
if (k === T.LBRACE || k === NEWLINE && this.kindAt(this.i + 1) === INDENT)
|
|
3272
|
+
return this.parseTypeBlock();
|
|
3273
|
+
if (k === T.LPAREN) {
|
|
3274
|
+
this.next();
|
|
3275
|
+
if (this.at(T.RPAREN)) {
|
|
3276
|
+
void_ = this.rp(typeIdentifierNode({
|
|
3277
|
+
isArray: false,
|
|
3278
|
+
typeRef: 'Void',
|
|
3279
|
+
typeArguments: []
|
|
3280
|
+
}), this.i - 1);
|
|
3281
|
+
this.next();
|
|
3282
|
+
return this.parseTypeFunctionTail([void_]);
|
|
3283
|
+
}
|
|
3284
|
+
first = this.parseTypeExpr();
|
|
3285
|
+
args = [first];
|
|
3286
|
+
while (this.at(T.COMMA)) {
|
|
3287
|
+
this.next();
|
|
3288
|
+
args.push(this.parseReturnTypeExpr());
|
|
3289
|
+
}
|
|
3290
|
+
this.expect(T.RPAREN, ')');
|
|
3291
|
+
if (this.at(T.ARROW)) {
|
|
3292
|
+
this.next();
|
|
3293
|
+
returnType = this.parseTypeExpr();
|
|
3294
|
+
return {
|
|
3295
|
+
'arguments': args,
|
|
3296
|
+
returnType: returnType,
|
|
3297
|
+
nodeType: 'functionType'
|
|
3298
|
+
};
|
|
3299
|
+
}
|
|
3300
|
+
if (args.length > 1)
|
|
3301
|
+
this.fail('expected -> after the argument types of a function type', null, {
|
|
3302
|
+
code: 'MS1123',
|
|
3303
|
+
why: 'A function type lists its argument types, then -> and the return type: (Int, String) -> Boolean, or Int * String -> Boolean.',
|
|
3304
|
+
fix: 'add -> and the return type.'
|
|
3305
|
+
});
|
|
3306
|
+
return this.parseTypeFunctionTail([first]);
|
|
3307
|
+
}
|
|
3308
|
+
id = this.parseTypeIdentifier();
|
|
3309
|
+
return this.parseTypeFunctionTail([id]);
|
|
3310
|
+
};
|
|
3311
|
+
Parser.prototype.parseTypeFunctionTail = function (args) {
|
|
3312
|
+
var returnType;
|
|
3313
|
+
while (this.at(T.STAR)) {
|
|
3314
|
+
this.next();
|
|
3315
|
+
args.push(this.parseReturnTypeExpr());
|
|
3316
|
+
}
|
|
3317
|
+
if (this.at(T.ARROW)) {
|
|
3318
|
+
this.next();
|
|
3319
|
+
returnType = this.parseTypeExpr();
|
|
3320
|
+
return {
|
|
3321
|
+
'arguments': args,
|
|
3322
|
+
returnType: returnType,
|
|
3323
|
+
nodeType: 'functionType'
|
|
3324
|
+
};
|
|
3325
|
+
}
|
|
3326
|
+
if (args.length > 1)
|
|
3327
|
+
this.fail('expected -> after the argument types of a function type', null, {
|
|
3328
|
+
code: 'MS1123',
|
|
3329
|
+
why: 'A function type lists its argument types, then -> and the return type: (Int, String) -> Boolean, or Int * String -> Boolean.',
|
|
3330
|
+
fix: 'add -> and the return type.'
|
|
3331
|
+
});
|
|
3332
|
+
return args[0];
|
|
3333
|
+
};
|
|
3334
|
+
Parser.prototype.parseReturnTypeExpr = function () {
|
|
3335
|
+
var inner, k, t;
|
|
3336
|
+
k = this.kind();
|
|
3337
|
+
if (k === T.LBRACE || k === NEWLINE && this.kindAt(this.i + 1) === INDENT)
|
|
3338
|
+
return this.parseTypeBlock();
|
|
3339
|
+
if (k === T.LPAREN) {
|
|
3340
|
+
t = this.i;
|
|
3341
|
+
this.next();
|
|
3342
|
+
if (this.at(T.RPAREN)) {
|
|
3343
|
+
this.next();
|
|
3344
|
+
return this.rp(typeIdentifierNode({
|
|
3345
|
+
isArray: false,
|
|
3346
|
+
typeRef: 'Void',
|
|
3347
|
+
typeArguments: []
|
|
3348
|
+
}), t);
|
|
3349
|
+
}
|
|
3350
|
+
inner = this.parseTypeExpr();
|
|
3351
|
+
this.expect(T.RPAREN, ')');
|
|
3352
|
+
return inner;
|
|
3353
|
+
}
|
|
3354
|
+
return this.parseTypeIdentifier();
|
|
3355
|
+
};
|
|
3356
|
+
Parser.prototype.parseTypeIdentifier = function () {
|
|
3357
|
+
var depth, inner, obj, ref, t;
|
|
3358
|
+
t = this.i;
|
|
3359
|
+
if (!isWordKind(this.kind())) {
|
|
3360
|
+
if (this.at(T.LPAREN)) {
|
|
3361
|
+
this.next();
|
|
3362
|
+
if (this.at(T.RPAREN)) {
|
|
3363
|
+
this.next();
|
|
3364
|
+
return this.rp(typeIdentifierNode({
|
|
3365
|
+
isArray: false,
|
|
3366
|
+
typeRef: 'Void',
|
|
3367
|
+
typeArguments: []
|
|
3368
|
+
}), t);
|
|
3369
|
+
}
|
|
3370
|
+
inner = this.parseTypeIdentifier();
|
|
3371
|
+
this.expect(T.RPAREN, ')');
|
|
3372
|
+
return this.rp(inner, t);
|
|
3373
|
+
}
|
|
3374
|
+
this.fail('expected a type after ::, found ' + this.describe(this.i), null, {
|
|
3375
|
+
code: 'MS1123',
|
|
3376
|
+
why: 'After :: comes a type: Int, Float, Number, String, Boolean, Any, a struct or blueprint name, T[] for an array, T? for a nullable value, or (A) -> B for a function.',
|
|
3377
|
+
fix: 'write the type, or remove the :: to leave the value untyped.'
|
|
3378
|
+
});
|
|
3379
|
+
}
|
|
3380
|
+
ref = this.text(this.next());
|
|
3381
|
+
while (this.at(T.DOT) && isWordKind(this.kindAt(this.i + 1))) {
|
|
3382
|
+
this.next();
|
|
3383
|
+
ref = {
|
|
3384
|
+
left: ref,
|
|
3385
|
+
right: this.text(this.next()),
|
|
3386
|
+
nodeType: 'MemberAccess'
|
|
3387
|
+
};
|
|
3388
|
+
}
|
|
3389
|
+
obj = { typeRef: ref };
|
|
3390
|
+
if (this.at(T.LT) && !this.spaceBefore(this.i))
|
|
3391
|
+
obj.typeArguments = this.parseTypeArgumentList();
|
|
3392
|
+
if (this.at(T.QUESTION) && !this.spaceBefore(this.i)) {
|
|
3393
|
+
this.next();
|
|
3394
|
+
obj.nullable = true;
|
|
3395
|
+
}
|
|
3396
|
+
depth = 0;
|
|
3397
|
+
while (this.at(T.LBRACKET) && this.kindAt(this.i + 1) === T.RBRACKET) {
|
|
3398
|
+
this.next();
|
|
3399
|
+
this.next();
|
|
3400
|
+
depth++;
|
|
3401
|
+
}
|
|
3402
|
+
if (depth > 0) {
|
|
3403
|
+
obj.isArray = true;
|
|
3404
|
+
if (depth > 1)
|
|
3405
|
+
obj.arrayDepth = depth;
|
|
3406
|
+
}
|
|
3407
|
+
if (this.at(T.QUESTION) && !this.spaceBefore(this.i)) {
|
|
3408
|
+
this.next();
|
|
3409
|
+
obj.wholeNullable = true;
|
|
3410
|
+
}
|
|
3411
|
+
if (this.at(T.ELLIPSIS) && !this.spaceBefore(this.i)) {
|
|
3412
|
+
this.next();
|
|
3413
|
+
obj.splats = true;
|
|
3414
|
+
}
|
|
3415
|
+
return this.rp(typeIdentifierNode(obj), t);
|
|
3416
|
+
};
|
|
3417
|
+
Parser.prototype.parseTypeArgumentList = function () {
|
|
3418
|
+
var args;
|
|
3419
|
+
this.expect(T.LT, '<');
|
|
3420
|
+
args = [this.parseTypeExpr()];
|
|
3421
|
+
while (this.at(T.COMMA) || this.at(T.STAR)) {
|
|
3422
|
+
this.next();
|
|
3423
|
+
args.push(this.parseTypeExpr());
|
|
3424
|
+
}
|
|
3425
|
+
this.expectGt();
|
|
3426
|
+
return args;
|
|
3427
|
+
};
|
|
3428
|
+
Parser.prototype.expectGt = function () {
|
|
3429
|
+
var k;
|
|
3430
|
+
k = this.kind();
|
|
3431
|
+
if (k === T.GT) {
|
|
3432
|
+
this.next();
|
|
3433
|
+
} else if (k === T.SHR && this.splitGt < 1 || k === T.USHR && this.splitGt < 2) {
|
|
3434
|
+
this.splitGt++;
|
|
3435
|
+
this.lastEnd = this.starts[this.i] + this.splitGt;
|
|
3436
|
+
} else if (k === T.SHR || k === T.USHR) {
|
|
3437
|
+
this.splitGt = 0;
|
|
3438
|
+
this.next();
|
|
3439
|
+
} else {
|
|
3440
|
+
this.fail('expected > to close the type arguments, found ' + this.describe(this.i), null, {
|
|
3441
|
+
code: 'MS1123',
|
|
3442
|
+
why: 'Type arguments are written between angle brackets: Box<Int>, Pair<String, Int>.',
|
|
3443
|
+
fix: 'add the closing >, or separate the type arguments with commas.'
|
|
3444
|
+
});
|
|
3445
|
+
}
|
|
3446
|
+
};
|
|
3447
|
+
return Parser;
|
|
3448
|
+
}();
|
|
3449
|
+
parse = function (src, options) {
|
|
3450
|
+
var parser;
|
|
3451
|
+
parser = new Parser(src, options);
|
|
3452
|
+
return parser.parseProgram();
|
|
3453
|
+
};
|
|
3454
|
+
createInterpolation = function (es) {
|
|
3455
|
+
return createInterpolationIn(es, true);
|
|
3456
|
+
};
|
|
3457
|
+
module.exports = {
|
|
3458
|
+
parse: parse,
|
|
3459
|
+
Parser: Parser,
|
|
3460
|
+
SyntaxError: MambaSyntaxError,
|
|
3461
|
+
foldBinaryExpr: foldBinaryExpr,
|
|
3462
|
+
createInterpolation: createInterpolation
|
|
3463
|
+
};
|
|
3464
|
+
function in$(member, list) {
|
|
3465
|
+
for (var i = 0, length = list.length; i < length; ++i)
|
|
3466
|
+
if (i in list && list[i] === member)
|
|
3467
|
+
return true;
|
|
3468
|
+
return false;
|
|
3469
|
+
}
|