-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
tokens.js
507 lines (473 loc) · 13.6 KB
/
tokens.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/* eslint-disable no-unused-vars */
"use strict";
const { createToken: createTokenOrg, Lexer } = require("chevrotain");
const fs = require("fs");
let chars;
// A little mini DSL for easier lexer definition.
const fragments = {};
try {
chars = require("./unicodesets");
} catch (e) {
throw Error(
"unicodesets.js file could not be found. Did you try to run the command: yarn run build ?"
);
}
function inlineFragments(def) {
let inlinedDef = def;
Object.keys(fragments).forEach(prevFragmentName => {
const prevFragmentDef = fragments[prevFragmentName];
const templateRegExp = new RegExp(`{{${prevFragmentName}}}`, "g");
inlinedDef = inlinedDef.replace(templateRegExp, prevFragmentDef);
});
return inlinedDef;
}
function FRAGMENT(name, def) {
fragments[name] = inlineFragments(def);
}
function MAKE_PATTERN(def, flags) {
const inlinedDef = inlineFragments(def);
return new RegExp(inlinedDef, flags);
}
// The order of fragments definitions is important
FRAGMENT("Digits", "[0-9]([0-9_]*[0-9])?");
FRAGMENT("ExponentPart", "[eE][+-]?{{Digits}}");
FRAGMENT("HexDigit", "[0-9a-fA-F]");
FRAGMENT("HexDigits", "{{HexDigit}}(({{HexDigit}}|'_')*{{HexDigit}})?");
FRAGMENT("FloatTypeSuffix", "[fFdD]");
FRAGMENT("LineTerminator", "(\\x0A|(\\x0D(\\x0A)?))");
FRAGMENT("UnicodeMarker", "uu*");
FRAGMENT("UnicodeEscape", "\\\\{{UnicodeMarker}}{{HexDigit}}{4}");
FRAGMENT("RawInputCharacter", "\\\\{{UnicodeMarker}}[0-9a-fA-F]{4}");
FRAGMENT("UnicodeInputCharacter", "({{UnicodeEscape}}|{{RawInputCharacter}})");
FRAGMENT("OctalDigit", "[0-7]");
FRAGMENT("ZeroToThree", "[0-3]");
FRAGMENT(
"OctalEscape",
"\\\\({{OctalDigit}}|{{ZeroToThree}}?{{OctalDigit}}{2})"
);
FRAGMENT("EscapeSequence", "\\\\[btnfr\"'\\\\]|{{OctalEscape}}");
// Not using InputCharacter terminology there because CR and LF are already captured in EscapeSequence
FRAGMENT(
"StringCharacter",
"(?:(?:{{EscapeSequence}})|{{UnicodeInputCharacter}})"
);
function matchJavaIdentifier(text, startOffset) {
let endOffset = startOffset;
let charCode = text.codePointAt(endOffset);
// We verifiy if the first character is from one of these categories
// Corresponds to the isJavaIdentifierStart function from Java
if (chars.firstIdentChar.has(charCode)) {
endOffset++;
// If we encounter a surrogate pair (something that is beyond 65535/FFFF)
// We skip another offset because a surrogate pair is of length 2.
if (charCode > 65535) {
endOffset++;
}
charCode = text.codePointAt(endOffset);
}
// We verify if the remaining characters is from one of these categories
// Corresponds to the isJavaIdentifierPart function from Java
while (chars.restIdentChar.has(charCode)) {
endOffset++;
// See above.
if (charCode > 65535) {
endOffset++;
}
charCode = text.codePointAt(endOffset);
}
// No match, must return null to conform with the RegExp.prototype.exec signature
if (endOffset === startOffset) {
return null;
}
const matchedString = text.substring(startOffset, endOffset);
// according to the RegExp.prototype.exec API the first item in the returned array must be the whole matched string.
return [matchedString];
}
const Identifier = createTokenOrg({
name: "Identifier",
pattern: { exec: matchJavaIdentifier },
line_breaks: false,
start_chars_hint: Array.from(chars.firstIdentChar, x =>
String.fromCharCode(x)
)
});
const allTokens = [];
const tokenDictionary = {};
function createToken(options) {
// TODO create a test to check all the tokenbs have a label defined
if (!options.label) {
// simple token (e.g operator)
if (typeof options.pattern === "string") {
options.label = `'${options.pattern}'`;
}
// Complex token (e.g literal)
else if (options.pattern instanceof RegExp) {
options.label = `'${options.name}'`;
}
}
const newTokenType = createTokenOrg(options);
allTokens.push(newTokenType);
tokenDictionary[options.name] = newTokenType;
return newTokenType;
}
function createKeywordLikeToken(options) {
// A keyword 'like' token uses the "longer_alt" config option
// to resolve ambiguities, see: http://sap.github.io/chevrotain/docs/features/token_alternative_matches.html
options.longer_alt = Identifier;
return createToken(options);
}
// Token Categories
// Used a Token Category to mark all restricted keywords.
// This could be used in syntax highlights implementation.
const RestrictedKeyword = createToken({
name: "RestrictedKeyword",
pattern: Lexer.NA
});
// Used a Token Category to mark all keywords.
// This could be used in syntax highlights implementation.
const Keyword = createToken({
name: "Keyword",
pattern: Lexer.NA
});
const AssignmentOperator = createToken({
name: "AssignmentOperator",
pattern: Lexer.NA
});
const BinaryOperator = createToken({
name: "BinaryOperator",
pattern: Lexer.NA
});
const UnaryPrefixOperator = createToken({
name: "UnaryPrefixOperator",
pattern: Lexer.NA
});
const UnaryPrefixOperatorNotPlusMinus = createToken({
name: "UnaryPrefixOperatorNotPlusMinus",
pattern: Lexer.NA
});
const UnarySuffixOperator = createToken({
name: "UnarySuffixOperator",
pattern: Lexer.NA
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.6
// Note [\\x09\\x20\\x0C] is equivalent to [\\t\\x20\\f] and that \\x20 represents
// space character
createToken({
name: "WhiteSpace",
pattern: MAKE_PATTERN("[\\x09\\x20\\x0C]|{{LineTerminator}}"),
group: Lexer.SKIPPED
});
createToken({
name: "LineComment",
pattern: /\/\/[^\n\r]*/,
group: "comments"
});
createToken({
name: "TraditionalComment",
pattern: /\/\*([^*]|\*(?!\/))*\*\//,
group: "comments"
});
createToken({ name: "BinaryLiteral", pattern: /0[bB][01]([01_]*[01])?[lL]?/ });
createToken({
name: "FloatLiteral",
pattern: MAKE_PATTERN(
"{{Digits}}\\.({{Digits}})?({{ExponentPart}})?({{FloatTypeSuffix}})?|" +
"\\.{{Digits}}({{ExponentPart}})?({{FloatTypeSuffix}})?|" +
"{{Digits}}{{ExponentPart}}({{FloatTypeSuffix}})?|" +
"{{Digits}}({{ExponentPart}})?{{FloatTypeSuffix}}"
)
});
createToken({ name: "OctalLiteral", pattern: /0_*[0-7]([0-7_]*[0-7])?[lL]?/ });
createToken({
name: "HexFloatLiteral",
pattern: MAKE_PATTERN(
"0[xX]({{HexDigits}}\\.?|({{HexDigits}})?\\.{{HexDigits}})[pP][+-]?{{Digits}}[fFdD]?"
)
});
createToken({
name: "HexLiteral",
pattern: /0[xX][0-9a-fA-F]([0-9a-fA-F_]*[0-9a-fA-F])?[lL]?/
});
createToken({
name: "DecimalLiteral",
pattern: MAKE_PATTERN("(0|[1-9](_+{{Digits}}|({{Digits}})?))[lL]?")
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.10.4
createToken({
name: "CharLiteral",
// Not using SingleCharacter Terminology because ' and \ are captured in EscapeSequence
pattern: MAKE_PATTERN(
"'(?:[^\\\\']|(?:(?:{{EscapeSequence}})|{{UnicodeInputCharacter}}))'"
)
});
createToken({
name: "StringLiteral",
pattern: MAKE_PATTERN('"(?:[^\\\\"]|{{StringCharacter}})*"')
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.9
// TODO: how to handle the special rule (see spec above) for "requires" and "transitive"
const restrictedKeywords = [
"open",
"module",
"requires",
"transitive",
"exports",
"opens",
"to",
"uses",
"provides",
"with"
];
// By sorting the keywords in descending order we avoid ambiguities
// of common prefixes.
sortDescLength(restrictedKeywords).forEach(word => {
createKeywordLikeToken({
name: word[0].toUpperCase() + word.substr(1),
pattern: word,
// restricted keywords can also be used as an Identifiers according to the spec.
// TODO: inspect this causes no ambiguities
categories: [Identifier, RestrictedKeyword]
});
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.9
const keywords = [
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"if",
"package",
"synchronized",
"boolean",
"do",
"goto",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
// "instanceof", // special handling for "instanceof" operator below
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"const",
"float",
"native",
"super",
"while",
["_", "underscore"]
];
sortDescLength(keywords).forEach(word => {
// For handling symbols keywords (underscore)
const isPair = Array.isArray(word);
const actualName = isPair ? word[1] : word;
const actualPattern = isPair ? word[0] : word;
const options = {
name: actualName[0].toUpperCase() + actualName.substr(1),
pattern: actualPattern,
categories: Keyword
};
if (isPair) {
options.label = `'${actualName}'`;
}
createKeywordLikeToken(options);
});
createKeywordLikeToken({
name: "Instanceof",
pattern: "instanceof",
categories: [Keyword, BinaryOperator]
});
createKeywordLikeToken({
name: "Var",
pattern: "var",
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-Keyword
// "var is not a keyword, but rather an identifier with special meaning as the type of a local variable declaration"
categories: Identifier
});
createKeywordLikeToken({ name: "True", pattern: "true" });
createKeywordLikeToken({ name: "False", pattern: "false" });
createKeywordLikeToken({ name: "Null", pattern: "null" });
// punctuation and symbols
createToken({ name: "At", pattern: "@" });
createToken({ name: "Arrow", pattern: "->" });
createToken({ name: "DotDotDot", pattern: "..." });
createToken({ name: "Dot", pattern: "." });
createToken({ name: "Comma", pattern: "," });
createToken({ name: "Semicolon", pattern: ";" });
createToken({ name: "ColonColon", pattern: "::" });
createToken({ name: "Colon", pattern: ":" });
createToken({ name: "QuestionMark", pattern: "?" });
createToken({ name: "LBrace", pattern: "(" });
createToken({ name: "RBrace", pattern: ")" });
createToken({ name: "LCurly", pattern: "{" });
createToken({ name: "RCurly", pattern: "}" });
createToken({ name: "LSquare", pattern: "[" });
createToken({ name: "RSquare", pattern: "]" });
// prefix and suffix operators
// must be defined before "-"
createToken({
name: "MinusMinus",
pattern: "--",
categories: [
UnaryPrefixOperator,
UnarySuffixOperator,
UnaryPrefixOperatorNotPlusMinus
]
});
// must be defined before "+"
createToken({
name: "PlusPlus",
pattern: "++",
categories: [
UnaryPrefixOperator,
UnarySuffixOperator,
UnaryPrefixOperatorNotPlusMinus
]
});
createToken({
name: "Complement",
pattern: "~",
categories: [UnaryPrefixOperator, UnaryPrefixOperatorNotPlusMinus]
});
createToken({
name: "LessEquals",
pattern: "<=",
categories: [BinaryOperator]
});
createToken({
name: "LessLessEquals",
pattern: "<<=",
categories: [AssignmentOperator]
});
createToken({ name: "Less", pattern: "<", categories: [BinaryOperator] });
createToken({
name: "GreaterEquals",
pattern: ">=",
categories: [BinaryOperator]
});
createToken({
name: "GreaterGreaterEquals",
pattern: ">>=",
categories: [AssignmentOperator]
});
createToken({
name: "GreaterGreaterGreaterEquals",
pattern: ">>>=",
categories: [AssignmentOperator]
});
createToken({ name: "Greater", pattern: ">", categories: [BinaryOperator] });
createToken({
name: "EqualsEquals",
pattern: "==",
categories: [BinaryOperator]
});
createToken({
name: "Equals",
pattern: "=",
categories: [BinaryOperator, AssignmentOperator]
});
createToken({
name: "MinusEquals",
pattern: "-=",
categories: [AssignmentOperator]
});
createToken({
name: "Minus",
pattern: "-",
categories: [BinaryOperator, UnaryPrefixOperator]
});
createToken({
name: "PlusEquals",
pattern: "+=",
categories: [AssignmentOperator]
});
createToken({
name: "Plus",
pattern: "+",
categories: [BinaryOperator, UnaryPrefixOperator]
});
createToken({ name: "AndAnd", pattern: "&&", categories: [BinaryOperator] });
createToken({
name: "AndEquals",
pattern: "&=",
categories: [AssignmentOperator]
});
createToken({ name: "And", pattern: "&", categories: [BinaryOperator] });
createToken({
name: "XorEquals",
pattern: "^=",
categories: [AssignmentOperator]
});
createToken({ name: "Xor", pattern: "^", categories: [BinaryOperator] });
createToken({ name: "NotEquals", pattern: "!=", categories: [BinaryOperator] });
createToken({ name: "OrOr", pattern: "||", categories: [BinaryOperator] });
createToken({
name: "OrEquals",
pattern: "|=",
categories: [AssignmentOperator]
});
createToken({ name: "Or", pattern: "|", categories: [BinaryOperator] });
createToken({
name: "MultiplyEquals",
pattern: "*=",
categories: [AssignmentOperator]
});
createToken({ name: "Star", pattern: "*", categories: [BinaryOperator] });
createToken({
name: "DivideEquals",
pattern: "/=",
categories: [AssignmentOperator]
});
createToken({ name: "Divide", pattern: "/", categories: [BinaryOperator] });
createToken({
name: "ModuloEquals",
pattern: "%=",
categories: [AssignmentOperator]
});
createToken({ name: "Modulo", pattern: "%", categories: [BinaryOperator] });
// must be defined after "!="
createToken({
name: "Not",
pattern: "!",
categories: [UnaryPrefixOperator, UnaryPrefixOperatorNotPlusMinus]
});
// Identifier must appear AFTER all the keywords to avoid ambiguities.
// See: https://github.com/SAP/chevrotain/blob/master/examples/lexer/keywords_vs_identifiers/keywords_vs_identifiers.js
allTokens.push(Identifier);
tokenDictionary["Identifier"] = Identifier;
function sortDescLength(arr) {
// sort is not stable, but that will not affect the lexing results.
return arr.sort((a, b) => {
return b.length - a.length;
});
}
module.exports = {
allTokens,
tokens: tokenDictionary
};