-
Notifications
You must be signed in to change notification settings - Fork 207
/
shift-reader.js
479 lines (430 loc) · 13.4 KB
/
shift-reader.js
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
import Tokenizer from "shift-parser/dist/tokenizer";
import { TokenClass, TokenType } from "shift-parser/dist/tokenizer";
import { List } from "immutable";
import Syntax from "./syntax";
import * as R from 'ramda';
import { Maybe } from 'ramda-fantasy';
import { assert } from './errors';
const Just = Maybe.Just;
const Nothing = Maybe.Nothing;
import Term from './terms';
const LSYNTAX = { name: 'left-syntax' };
const RSYNTAX = { name: 'right-syntax' };
const AT = { klass: TokenClass.Punctuator, name: "@" };
// TODO: also, need to handle contextual yield
const literalKeywords = ['this', 'null', 'true', 'false'];
// Token -> Boolean
const isLeftBracket = R.whereEq({ type: TokenType.LBRACK });
const isLeftBrace = R.whereEq({ type: TokenType.LBRACE });
const isLeftParen = R.whereEq({ type: TokenType.LPAREN });
const isRightBracket = R.whereEq({ type: TokenType.RBRACK });
const isRightBrace = R.whereEq({ type: TokenType.RBRACE });
const isRightParen = R.whereEq({ type: TokenType.RPAREN });
const isEOS = R.whereEq({ type: TokenType.EOS });
const isHash = R.whereEq({ type: TokenType.IDENTIFIER, value: '#'});
const isLeftSyntax = R.whereEq({ type: LSYNTAX });
const isRightSyntax = R.whereEq({ type: RSYNTAX });
const isLeftDelimiter = R.anyPass([isLeftBracket,
isLeftBrace,
isLeftParen,
isLeftSyntax]);
const isRightDelimiter = R.anyPass([isRightBracket,
isRightBrace,
isRightParen,
isRightSyntax]);
const isMatchingDelimiters = R.cond([
[isLeftBracket, (_, b) => isRightBracket(b)],
[isLeftBrace, (_, b) => isRightBrace(b)],
[isLeftParen, (_, b) => isRightParen(b)],
[isLeftSyntax, (_, b) => isRightSyntax(b)],
[R.T, R.F]
]);
const assignOps = ["=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=",
"&=", "|=", "^=", ","];
const binaryOps = ["+", "-", "*", "/", "%","<<", ">>", ">>>", "&", "|", "^",
"&&", "||", "?", ":",
"===", "==", ">=", "<=", "<", ">", "!=", "!==", "instanceof"];
const unaryOps = ["++", "--", "~", "!", "delete", "void", "typeof", "yield", "throw", "new"];
// List -> Boolean
const isEmpty = R.whereEq({size: 0});
// Syntax -> Boolean
const isPunctuator = s => s.isPunctuator();
const isKeyword = s => s.isKeyword();
const isDelimiter = s => s.isDelimiter();
const isParens = s => s.isParens();
const isBraces = s => s.isBraces();
const isBrackets = s => s.isBrackets();
const isIdentifier = s => s.isIdentifier();
// Syntax -> any
const val = s => s.val();
// Any -> Syntax -> Boolean
const isVal = R.curry((v, s) => s.val() === v);
// Syntax -> Boolean
const isDot = R.allPass([isPunctuator, isVal('.')]);
const isColon = R.allPass([isPunctuator, isVal(':')]);
const isFunctionKeyword = R.allPass([isKeyword, isVal('function')]);
const isOperator = s => (s.isPunctuator() || s.isKeyword()) &&
R.any(R.equals(s.val()),
assignOps.concat(binaryOps).concat(unaryOps));
const isNonLiteralKeyword = R.allPass([isKeyword,
s => R.none(R.equals(s.val()), literalKeywords)]);
const isKeywordExprPrefix = R.allPass([isKeyword,
s => R.any(R.equals(s.val()), ['instanceof', 'typeof', 'delete', 'void',
'yield', 'throw', 'new', 'case'])]);
// List a -> a?
let last = p => p.last();
// List a -> Maybe a
let safeLast = R.pipe(R.cond([
[isEmpty, R.always(Nothing())],
[R.T, R.compose(Maybe.of, last)]
]));
// TODO: better name
// List -> Boolean -> Maybe List
let stuffTrue = R.curry((p, b) => b ? Just(p) : Nothing());
let stuffFalse = R.curry((p, b) => !b ? Just(p) : Nothing());
// List a -> Boolean
let isTopColon = R.pipe(
safeLast,
R.map(isColon),
Maybe.maybe(false, R.identity)
);
// List a -> Boolean
let isTopPunctuator = R.pipe(
safeLast,
R.map(isPunctuator),
Maybe.maybe(false, R.identity)
);
// Number -> List -> Boolean
let isExprReturn = R.curry((l, p) => {
let retKwd = safeLast(p);
let maybeDot = pop(p).chain(safeLast);
if (maybeDot.map(isDot).getOrElse(false)) {
return true;
}
return retKwd.map(s => {
return s.isKeyword() && s.val() === 'return' && s.lineNumber() === l;
}).getOrElse(false);
});
const isTopOperator = R.pipe(
safeLast,
R.map(isOperator),
Maybe.maybe(false, R.identity)
);
const isTopKeywordExprPrefix = R.pipe(
safeLast,
R.map(isKeywordExprPrefix),
Maybe.maybe(false, R.identity)
);
// Number -> Boolean -> List -> Boolean
let isExprPrefix = R.curry((l, b) => R.cond([
// ... ({x: 42} /r/i)
[isEmpty, R.always(b)],
// ... ({x: {x: 42} /r/i })
[isTopColon, R.always(b)],
// ... throw {x: 42} /r/i
[isTopKeywordExprPrefix, R.T],
// ... 42 + {x: 42} /r/i
[isTopOperator, R.T],
// ... for ( ; {x: 42}/r/i)
[isTopPunctuator, R.always(b)],
// ... return {x: 42} /r /i
// ... return\n{x: 42} /r /i
[isExprReturn(l), R.T],
[R.T, R.F],
]));
// List a -> Maybe List a
let curly = p => safeLast(p).map(isBraces).chain(stuffTrue(p));
let paren = p => safeLast(p).map(isParens).chain(stuffTrue(p));
let func = p => safeLast(p).map(isFunctionKeyword).chain(stuffTrue(p));
let ident = p => safeLast(p).map(isIdentifier).chain(stuffTrue(p));
let nonLiteralKeyword = p => safeLast(p).map(isNonLiteralKeyword).chain(stuffTrue(p));
let opt = R.curry((a, b, p) => {
let result = R.pipeK(a, b)(Maybe.of(p));
return Maybe.isJust(result) ? result : Maybe.of(p);
});
let notDot = R.ifElse(
R.whereEq({size: 0}),
Just,
p => safeLast(p).map(s => !(s.isPunctuator() && s.val() === '.')).chain(stuffTrue(p))
);
// List a -> Maybe List a
let pop = R.compose(Just, p => p.pop());
// Maybe List a -> Maybe List a
const functionPrefix = R.pipeK(
curly,
pop,
paren,
pop,
opt(ident, pop),
func);
// Boolean -> List a -> Boolean
const isRegexPrefix = b => R.anyPass([
// ε
isEmpty,
// P . t where t ∈ Punctuator
isTopPunctuator,
// P . t . t' where t \not = "." and t' ∈ (Keyword \setminus LiteralKeyword)
R.pipe(
Maybe.of,
R.pipeK(
nonLiteralKeyword,
pop,
notDot
),
Maybe.isJust
),
// P . t . t' . (T) where t \not = "." and t' ∈ (Keyword \setminus LiteralKeyword)
R.pipe(
Maybe.of,
R.pipeK(
paren,
pop,
nonLiteralKeyword,
pop,
notDot
),
Maybe.isJust
),
// P . function^l . x? . () . {} where isExprPrefix(P, b, l) = false
R.pipe(
Maybe.of,
functionPrefix,
R.chain(p => {
return safeLast(p)
.map(s => s.lineNumber())
.chain(fnLine => {
return pop(p).map(isExprPrefix(fnLine, b));
})
.chain(stuffFalse(p));
}
),
Maybe.isJust
),
// P . {T}^l where isExprPrefix(P, b, l) = false
p => {
let isCurly = Maybe.isJust(safeLast(p).map(isBraces));
let alreadyCheckedFunction = R.pipe(
Maybe.of,
functionPrefix,
Maybe.isJust
)(p);
if (alreadyCheckedFunction) {
return false;
}
return R.pipe(
Maybe.of,
R.chain(curly),
R.chain(p => {
return safeLast(p)
.map(s => s.lineNumber())
.chain(curlyLine => {
return pop(p).map(isExprPrefix(curlyLine, b));
})
.chain(stuffFalse(p));
}),
Maybe.isJust
)(p);
}
]);
function lastEl(l) {
return l[l.length - 1];
}
export default class Reader extends Tokenizer {
constructor(strings, context, replacements) {
super(Array.isArray(strings) ? strings.join('') : strings);
this.delimStack = new Map();
this.insideSyntaxTemplate = [false];
this.context = context;
// setup splicing replacement array
if (Array.isArray(strings)) {
let totalIndex = 0;
this.replacementIndex = R.reduce((acc, strRep) => {
acc.push({
index: totalIndex + strRep[0].length,
replacement: strRep[1]
});
totalIndex += strRep[0].length;
return acc;
}, [], R.zip(strings, replacements));
}
}
read(stack = [], b = false, singleDelimiter = false) {
let prefix = List();
while (true) {
let tok = this.advance(prefix, b);
// splicing allows syntax and terms
if (tok instanceof Syntax || tok instanceof Term) {
stack.push(tok);
continue;
}
if (Array.isArray(tok)) {
Array.prototype.push.apply(stack, tok);
continue;
}
if (List.isList(tok)) {
Array.prototype.push.apply(stack, tok.toArray());
continue;
}
if (isEOS(tok)) {
if (stack[0] && isLeftDelimiter(stack[0].token)) {
throw this.createUnexpected(tok);
}
break;
}
if (isLeftDelimiter(tok)) {
if (isLeftSyntax(tok)) {
this.insideSyntaxTemplate.push(true);
}
let line = tok.slice.startLocation.line;
let innerB = isLeftBrace(tok) ? isExprPrefix(line, b)(prefix) : true;
let inner = this.read([new Syntax(tok)],
innerB,
false);
let stx = new Syntax(inner, this.context);
prefix = prefix.concat(stx);
stack.push(stx);
if (singleDelimiter) {
break;
}
} else if (isRightDelimiter(tok)) {
if (stack[0] && !isMatchingDelimiters(stack[0].token, tok)) {
throw this.createUnexpected(tok);
}
let stx = new Syntax(tok, this.context);
stack.push(stx);
if (lastEl(this.insideSyntaxTemplate) && isRightSyntax(tok)) {
this.insideSyntaxTemplate.pop();
}
break;
} else {
let stx = new Syntax(tok, this.context);
prefix = prefix.concat(stx);
stack.push(stx);
}
}
return List(stack);
}
advance(prefix, b)/*: any */ {
let startLocation = this.getLocation();
this.lastIndex = this.index;
this.lastLine = this.line;
this.lastLineStart = this.lineStart;
this.skipComment();
this.startIndex = this.index;
this.startLine = this.line;
this.startLineStart = this.lineStart;
if (this.replacementIndex && this.replacementIndex[0] && this.index >= this.replacementIndex[0].index) {
let rep = this.replacementIndex[0].replacement;
this.replacementIndex.shift();
return rep;
}
let charCode = this.source.charCodeAt(this.index);
if (charCode === 0x60) { // `
let element, items = [];
let startLocation = this.getLocation();
let start = this.index;
this.index++;
if (lastEl(this.insideSyntaxTemplate)) {
let slice = this.getSlice(start, startLocation);
return {
type: RSYNTAX,
value: '`',
slice: slice
};
}
do {
element = this.scanTemplateElement();
items.push(element);
if (element.interp) {
// only read the single delimiter
element = this.read([], false, true);
assert(element.size === 1, "should only have read a single delimiter inside a template");
items.push(element.get(0));
}
} while (!element.tail);
return {
type: TokenType.TEMPLATE,
items: List(items)
};
} else if (charCode === 35) { // #
let startLocation = this.getLocation();
let start = this.index;
let slice = this.getSlice(start, startLocation);
this.index++;
// TODO: handle ` inside of syntax template interpolations
if (this.source.charCodeAt(this.index) === 0x60) { // `
this.index++;
return {
type: LSYNTAX,
value: '#`',
slice: slice
};
}
return {
type: TokenType.IDENTIFIER,
value: '#',
slice: slice
};
} else if (charCode === 64) { // @
let startLocation = this.getLocation();
let start = this.index;
let slice = this.getSlice(start, startLocation);
this.index++;
return {
type: AT,
value: '@',
slice
};
}
let lookahead = super.advance();
if (lookahead.type === TokenType.DIV && isRegexPrefix(b)(prefix)) {
return super.scanRegExp("/");
}
return lookahead;
}
// need to override how templates are lexed because of delimiters
scanTemplateElement() {
let startLocation = this.getLocation();
let start = this.index;
while (this.index < this.source.length) {
let ch = this.source.charCodeAt(this.index);
switch (ch) {
case 0x60: // `
// don't include the traling "`"
let slice = this.getSlice(start, startLocation);
this.index++;
return {
type: TokenType.TEMPLATE,
tail: true,
interp: false,
slice: slice
};
case 0x24: // $
if (this.source.charCodeAt(this.index + 1) === 0x7B) { // {
// don't include the trailing "$"
let slice = this.getSlice(start, startLocation);
this.index += 1;
return {
type: TokenType.TEMPLATE,
tail: false,
interp: true,
slice: slice
};
}
this.index++;
break;
case 0x5C: // \\
{
let octal = this.scanStringEscape("", null)[1];
if (octal != null) {
throw this.createILLEGAL();
}
break;
}
default:
this.index++;
}
}
throw this.createILLEGAL();
}
}