Repository navigation
Expand file tree
/
Copy pathlscCoreSyntax.lsc
More file actions
483 lines (410 loc) · 18.5 KB
/
Copy pathlscCoreSyntax.lsc
File metadata and controls
483 lines (410 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Parsing for:
// - Whiteblock syntax
// - Paren-free test expressions
// - Full decimals only (1.0 instead of 1., 0.5 instead of .5)
// - LightScript ASI rules
// - Commaless syntax
// - Ban labeled expression statements
import { Plugin } from '../core/plugin'
import { types as tt } from "../../tokenizer/types"
import { types as ct } from "../../tokenizer/context"
import { isIdentifierStart } from "../../util/identifier"
import * as charCodes from "charcodes"
lscCoreSyntax = new Plugin({
name: 'lscCoreSyntax'
dependencies: ['significantWhitespace', 'seqExprRequiresParen', 'tryCatch']
load() ->
tt._class.startsExpr = true
mixin(ParserBase, opts) -> {
return class extends ParserBase:
constructor(options, input) -> {
super(options, input)
// Tokens that can terminate an enclosing whiteblock when they
// would otherwise be unexpected.
this.state.whiteBlockTerminalTokens = [
tt.comma
tt.parenR
tt.bracketR
tt.braceR
tt._else
tt._catch
tt._finally
tt.eof
]
// State flag that bans parsing of named arrows when it would cause
// unacceptable grammar ambiguities.
this.state.banAmbiguousArrows = false
}
// Expect a token but don't consume it
check(type, pos): boolean ->
this.match(type) or this.unexpected(pos, type)
// Determine if the token under the cursor starts an expression.
tokenStartsExpression(): boolean ->
this.state.type.startsExpr
// Determine if the cursor has crossed the boundary of an ongoing
// multiline whiteblock.
crossesWhiteBlockBoundary(): boolean ->
this.state.inWhiteBlock and
this.state.indentLevel <= this.state.whiteBlockIndentLevel
// Determine if the token under the cursor is a whiteblock ASI candidate
matchesWhiteBlockASIToken(): boolean ->
this.state.whiteBlockTerminalTokens.indexOf(this.state.type) > -1
// Throw if, after parsing a paren-free test expression, we don't land
// on something that looks like a consequent block.
expectParenFreeBlockStart(node): void ->
// if true: blah
// if true { blah }
// if (true) blah
// match (foo) as bar:
if (node && node.extra && node.extra.hasParens) {
this.expect(tt.parenR);
} else if (!(this.match(tt.colon) || this.match(tt.braceL) || this.isContextual("as"))) {
this.unexpected(null, "Paren-free test expressions must be followed by braces or a colon.");
}
lsc_saveParenExpressionState() ->
banAmbiguousArrows = this.state.banAmbiguousArrows
whiteBlockTerminalTokens = this.state.whiteBlockTerminalTokens
this.state.banAmbiguousArrows = true
this.state.whiteBlockTerminalTokens = whiteBlockTerminalTokens.concat([tt.colon])
{ banAmbiguousArrows, whiteBlockTerminalTokens }
lsc_restoreParenExpressionState(state): void ->
Object.assign(this.state, state)
lsc_parseParenExpression(node) -> {
if this.isLineBreak():
this.unexpected(this.state.lastTokEnd, "Illegal newline.")
oldState = this.lsc_saveParenExpressionState()
// There might actually be a paren in a "paren-free" test expression
// We have to disambiguate:
// e.g. `if (x) -1` or `if (x < 1) and y: -1`
if (this.match(tt.parenL)) {
const state = this.state.clone();
// first, try paren-free style
try {
const val = this.parseExpression();
this.expectParenFreeBlockStart(node);
if (val.extra && val.extra.parenthesized) {
// Remove accidentally added paren extra data. If this
// leaves the extra object empty, quash that as well.
// TODO: Factor this out. Also used in statement.js/parseDoWhile
delete val.extra.parenthesized;
delete val.extra.parenStart;
if (Object.keys(val.extra).length == 0) { delete val.extra; }
}
this.lsc_restoreParenExpressionState(oldState)
return val;
} catch (_err) {
// fall-through
}
// Could have been an un-parenthesized SeqExpr
if (this.hasPlugin("seqExprRequiresParen") && this.match(tt.comma)) {
this.unexpected();
}
// otherwise, try traditional parseParenExpression
this.state = state;
this.lsc_restoreParenExpressionState(oldState);
inner_result = super.parseParenExpression();
return inner_result;
} // if this.match(tt.parenL)
// No paren, so the expression is a plain paren-free test
expr = this.parseExpression()
this.expectParenFreeBlockStart(node)
this.lsc_restoreParenExpressionState(oldState)
expr
} // lsc_parseParenExpression
// In whitespace-sensitive parsing, it is often useful to look back to
// an opening construct that is at the same indent level as a later
// closing construct. The block stack enables this pattern.
getWhiteBlockStack() ->
if not this.state.blockStack: this.state.blockStack = []
this.state.blockStack
pushWhiteBlockState(blockType, indentLevel): void ->
this.getWhiteBlockStack().push({blockType, indentLevel})
matchWhiteBlockState(blockType, indentLevel): boolean ->
this.getWhiteBlockStack().some(
(x) -> (x.blockType == blockType) and (x.indentLevel == indentLevel)
)
popWhiteBlockState() ->
this.getWhiteBlockStack().pop()
// Parse multiline whiteblock body.
// c/p statement.js parseBlockOrModuleBlockBody
parseWhiteBlockBody(body, directives, topLevel, whiteBlockIndentLevel): void -> {
let parsedNonDirective = false;
let oldStrict;
let octalPosition;
oldInWhiteBlock = this.state.inWhiteBlock;
oldWhiteBlockIndentLevel = this.state.whiteBlockIndentLevel;
this.state.inWhiteBlock = true;
this.state.whiteBlockIndentLevel = whiteBlockIndentLevel;
isEnd() =>
this.state.indentLevel <= whiteBlockIndentLevel || this.matchesWhiteBlockASIToken()
while (!isEnd()) {
if (!parsedNonDirective && this.state.containsOctal && !octalPosition):
now octalPosition = this.state.octalPosition
const stmt = this.parseStatement(true, topLevel);
if (directives && !parsedNonDirective && this.isValidDirective(stmt)) {
const directive = this.stmtToDirective(stmt);
directives.push(directive);
if (oldStrict === undefined && directive.value.value === "use strict") {
now oldStrict = this.state.strict;
this.setStrict(true);
if (octalPosition) {
this.raise(octalPosition, "Octal literal in strict mode");
}
}
continue;
}
now parsedNonDirective = true;
body.push(stmt);
} // while (!isEnd())
this.state.inWhiteBlock = oldInWhiteBlock;
this.state.whiteBlockIndentLevel = oldWhiteBlockIndentLevel;
if (oldStrict === false) {
this.setStrict(false);
}
} // parseWhiteBlockBody
parseInlineWhiteBlock(node, allowExpression) ->
if (allowExpression and this.tokenStartsExpression())
return this.parseMaybeAssign();
// oneline statement case
node.body = [this.parseStatement(true)];
node.directives = [];
this.addExtra(node, "curly", false);
return this.finishNode(node, "BlockStatement");
parseMultilineWhiteBlock(node, indentLevel) ->
body = []
directives = []
this.parseWhiteBlockBody(body, null, false, indentLevel)
if body.length == 0:
this.unexpected(node.start, "Expected an Indent or Statement")
node.body = body
node.directives = directives
this.addExtra(node, "curly", false)
this.finishNode(node, "BlockStatement")
parseWhiteBlock(isExpression) ->
// Capture indent level of line prior to whiteblock body
indentLevel = this.state.indentLevel
// Start whiteblock node before eating the colon
node = this.startNode()
if not this.eat(tt.colon): this.unexpected(this.state.lastTokEnd, tt.colon)
// Distinguish whiteblock type
if not this.isLineBreak(): // Oneline whiteblock
if isExpression:
this.parseInlineWhiteBlock(node, true)
else:
this.parseStatement(false)
else: // Multiline whiteblock
this.parseMultilineWhiteBlock(node, indentLevel)
parseObjectBlockAmbiguity(isBlock, allowDirectives) -> {
if isBlock:
// We've received a hint from elsewhere in the parser that lets
// us know we have a block...
this.parseBlock(allowDirectives)
else:
node = this.startNode()
// Try to parse as if it were an object or assignment pattern.
state = this.state.clone()
let objParseError
try:
return this.parseExpressionStatement(node, this.parseExpression())
catch err:
this.state = state
now objParseError = err
// Try to parse as a block
try:
return this.parseBlock(allowDirectives)
catch err:
if objParseError.pos > err.pos:
throw objParseError
elif objParseError.pos < err.pos:
throw err
else:
objParseError.message = "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: " + objParseError.message
throw objParseError
} // parseObjectBlockAmbiguity
lsc_shouldAllowAnonymousClass(): boolean -> {
this.state.inFunction or
this.state.banInvalidIifeStatements // Pseudo-functional context
}
// Since naked decimals are disabled in LightScript, it is safe to parse
// questionDot even when a number appears afterward.
readToken_question(): void -> {
next = this.input.charCodeAt(this.state.pos + 1)
if next == charCodes.dot:
this.state.pos += 2
this.finishToken(tt.questionDot)
else:
super.readToken_question()
}
// Hooking parseBlock and parseStatement enables whiteblocks just about everywhere
parseBlock(allowDirectives) ->
if this.match(tt.colon):
this.parseWhiteBlock()
else:
block = super.parseBlock(allowDirectives)
this.addExtra(block, "curly", true)
block
parseStatementContent(declaration, topLevel) ->
// Retrieve state hints associated with object-block ambiguity
isBlock = this.state.nextBraceIsBlock
allowDirectives = this.state.nextBraceAllowDirectives
this.state.nextBraceIsBlock = null
this.state.nextBraceAllowDirectives = null
if this.match(tt.colon):
this.parseWhiteBlock()
elif this.match(tt.braceL):
this.parseObjectBlockAmbiguity(isBlock, allowDirectives)
else:
super.parseStatementContent(declaration, topLevel)
canInsertSemicolon(): boolean ->
this.matchesWhiteBlockASIToken() or
this.state.lastTokType == tt.semi or // can insert a semi after a semi
super.canInsertSemicolon()
// Hooking parseParenExpression enables paren-free for if,switch,while,with
parseParenExpression() ->
this.lsc_parseParenExpression(null)
// Hook subscript parsing
parseSubscript(base, startPos, startLoc, noCalls, state) ->
if this.crossesWhiteBlockBoundary():
// Don't let subscripts cross whiteblock boundaries
state.stop = true
return base
elif this.match(tt.dot) and (base.type === 'NumericLiteral'):
// catch malformed decimals (but allow `0.0.toString()`, which is actually valid)
if (!(base.extra && base.extra.raw && base.extra.raw.match(/\./))) {
this.unexpected(null, "Numbers with a decimal must end in a number (eg; `1.0`) in LightScript.");
}
elif this.match(tt.backQuote) and this.isLineBreak():
// Prevent parsing backquotes across newlines
state.stop = true
return base
elif this.match(tt.bracketL):
// Bracket must either be on the same line, or on a different
// but indented line.
if this.isNonIndentedBreakFrom(startPos):
state.stop = true
return base
elif (not noCalls) and this.match(tt.parenL):
// Opening paren of a call expr must be on the same line as the callee
if this.isLineBreak():
state.stop = true
return base
super.parseSubscript(base, startPos, startLoc, noCalls, state)
parseNumericPropertyAccess(node): void ->
node.property = this.parseExprAtom();
node.computed = true;
this.addExtra(node, "dotNumeric", true);
// ASI: newline followed by +/- followed by a non-space is
// a unary +/-
parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) ->
if this.isLineBreak():
if this.match(tt.plusMin) and (not this.isNextCharWhitespace()):
return left
super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn)
// ASI: unary +/- can't have a space between operator and operand
parseUnaryPrefix(refShorthandDefaultPos) ->
if this.match(tt.plusMin) and this.isNextCharWhitespace():
this.unexpected(null, "Unary +/- cannot be followed by a space in lightscript.")
super.parseUnaryPrefix(refShorthandDefaultPos)
// Prevent arrows from spanning lines
shouldParseAsyncArrow(): boolean ->
if this.isLineBreak(): return false
super.shouldParseAsyncArrow()
// Enforce full decimal syntax (0.0 rather than .0)
parseExprAtom(refShorthandDefaultPos) ->
if this.match(tt.dot) and this.state.numberFollowsDot:
this.unexpected(null, "Decimal numbers must be prefixed with a `0` in LightScript (eg; `0.1`).")
super.parseExprAtom(refShorthandDefaultPos)
shouldParseFloat() ->
// Don't parse a float if we just parsed a dot.
this.state.lastTokType != tt.dot
// ASI, args to new can't begin on a different line
parseNewArguments(node): void ->
if this.isLineBreak():
node.arguments = []
else:
super.parseNewArguments(node)
// ASI, method name must be on same line as get/set
isGetterOrSetterMethod(prop, isPattern: boolean): boolean ->
if this.isLineBreak(): false else: super.isGetterOrSetterMethod(prop, isPattern)
// ASI, regex parsing hooks
isOutsideJSXTag(): boolean ->
if this.hasPlugin("jsx"):
ctx = this.curContext()
(ctx != ct.j_oTag) and (ctx != ct.j_cTag)
else:
true
looksLikeRegex(): boolean ->
this.isLineBreak() and
(not this.isWhitespaceAt(this.state.pos + 1)) and
this.isOutsideJSXTag()
readRegexp(): void ->
if this.input.charCodeAt(this.state.pos) == 32:
this.raise(this.state.pos, "Regex literals cannot start with a space in lightscript; try '\\s' or '\\ ' instead.")
super.readRegexp()
// ASI, JSX opening tags vs <
readToken(code): void ->
if (code == charCodes.lessThan) and (this.hasPlugin("jsx")):
next = this.state.input.charCodeAt(this.state.pos + 1)
if this.isLineBreak() and isIdentifierStart(next):
++this.state.pos
return this.finishToken(tt.jsxTagStart)
super.readToken(code)
// Commaless
matchListSeparator(): boolean ->
this.match(tt.comma) or this.isLineBreak()
expectListSeparator(loc): void ->
if not (this.eat(tt.comma) or this.isLineBreak()):
this.unexpected(loc, tt.comma)
parseLabeledStatement(node, maybeName, expr) ->
// When `:` is an early whiteblock terminator, assume the user meant
// to terminate the whiteblock, rather than to apply a label.
if this.state.whiteBlockTerminalTokens.indexOf(tt.colon) > -1:
return this.parseExpressionStatement(node, expr)
// Illegalize labeled expression statements to resolve ambiguities
// in whiteblock parsing.
result = super.parseLabeledStatement(node, maybeName, expr)
if result.body.type == "ExpressionStatement":
this.raise(result.label.start, "Invalid labeled expression")
result
// Allow anonymous class bodies in functions (useful for implicit returns)
parseClassId(node, isStatement, optionalId) ->
super.parseClassId(node, isStatement, optionalId or this.lsc_shouldAllowAnonymousClass())
// Parser state changes for arrow parsing and block parsing are required
// when parsing ternaries.
//
// Sadly, Flow totally takes over `parseConditional`, so we have to
// c/p `parseMaybeConditional` which is safe.
parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos) -> {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const potentialArrowAt = this.state.potentialArrowAt;
const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
if (
expr.type === "ArrowFunctionExpression" &&
expr.start === potentialArrowAt
) {
return expr;
}
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
// XXX: parseConditional should really be broken up into
// parseConditionalTest, parseConditionalConsequent, parseConditionalAlternate
// and we really only need to ban ambiguities in the consequent --
// but the flow plugin is too complex to allow this to be easily done.
oldBanAmbiguousArrows = this.state.banAmbiguousArrows;
oldWhiteBlockTerminalTokens = this.state.whiteBlockTerminalTokens;
this.state.whiteBlockTerminalTokens = oldWhiteBlockTerminalTokens.concat([tt.colon]);
this.state.banAmbiguousArrows = true;
result = this.parseConditional(
expr,
noIn,
startPos,
startLoc,
refNeedsArrowPos,
);
this.state.banAmbiguousArrows = oldBanAmbiguousArrows;
this.state.whiteBlockTerminalTokens = oldWhiteBlockTerminalTokens;
return result;
}
} // mixin
})
export default lscCoreSyntax