cariboujs 2.0.7 → 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +4 -3
  2. package/asm/asm8080/asm8080Lexer.js +253 -152
  3. package/asm/asm8080/asm8080Listener.js +72 -0
  4. package/asm/asm8080/asm8080Parser.js +1307 -289
  5. package/asm/asm8080/asm8080Visitor.js +48 -0
  6. package/asm/asmZ80/asmZ80Lexer.js +152 -128
  7. package/asm/asmZ80/asmZ80Listener.js +9 -0
  8. package/asm/asmZ80/asmZ80Parser.js +316 -170
  9. package/asm/asmZ80/asmZ80Visitor.js +6 -0
  10. package/c/CParser.js +1352 -1344
  11. package/cypher/CypherLexer.js +208 -205
  12. package/cypher/CypherParser.js +1180 -984
  13. package/cypher/CypherParserListener.js +18 -0
  14. package/cypher/CypherParserVisitor.js +12 -0
  15. package/package.json +1 -1
  16. package/python/python3_14/PythonLexer.js +1238 -0
  17. package/python/python3_14/PythonLexerBase.js +819 -0
  18. package/python/{python3_13 → python3_14}/PythonParser.js +4141 -3347
  19. package/python/{python3_13 → python3_14}/PythonParserListener.js +81 -0
  20. package/python/{python3_13 → python3_14}/PythonParserVisitor.js +54 -0
  21. package/sql/plsql/PlSqlLexer.js +4114 -4113
  22. package/sql/plsql/PlSqlParser.js +37074 -31651
  23. package/sql/plsql/PlSqlParserBase.js +31 -0
  24. package/sql/plsql/PlSqlParserListener.js +58 -4
  25. package/sql/plsql/PlSqlParserVisitor.js +38 -2
  26. package/sysml-v2/SysMLv2Lexer.js +617 -605
  27. package/sysml-v2/SysMLv2Parser.js +7135 -10083
  28. package/sysml-v2/SysMLv2ParserListener.js +45 -423
  29. package/sysml-v2/SysMLv2ParserVisitor.js +30 -282
  30. package/xpath/xpath4/XPath4Lexer.js +667 -0
  31. package/xpath/xpath4/XPath4Parser.js +17424 -0
  32. package/xpath/xpath4/XPath4ParserBase.js +34 -0
  33. package/xpath/xpath4/XPath4ParserListener.js +1521 -0
  34. package/xpath/xpath4/XPath4ParserVisitor.js +1018 -0
  35. package/xquery/xquery4/XQuery4Lexer.js +1396 -0
  36. package/xquery/xquery4/XQuery4LexerBase.js +42 -0
  37. package/xquery/xquery4/XQuery4Parser.js +27899 -0
  38. package/xquery/xquery4/XQuery4ParserBase.js +38 -0
  39. package/xquery/xquery4/XQuery4ParserListener.js +2322 -0
  40. package/xquery/xquery4/XQuery4ParserVisitor.js +1552 -0
  41. package/awk/awkLexer.js +0 -317
  42. package/awk/awkListener.js +0 -378
  43. package/awk/awkParser.js +0 -5859
  44. package/awk/awkVisitor.js +0 -256
  45. package/python/python3_13/PythonLexer.js +0 -974
  46. package/python/python3_13/PythonLexerBase.js +0 -676
@@ -1,676 +0,0 @@
1
- /*
2
- The MIT License (MIT)
3
- Copyright (c) 2021 Robert Einhorn
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
- The above copyright notice and this permission notice shall be included in
12
- all copies or substantial portions of the Software.
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
20
- */
21
-
22
- /*
23
- *
24
- * Project : Python Indent/Dedent handler for ANTLR4 grammars
25
- *
26
- * Developed by : Robert Einhorn, robert.einhorn.hu@gmail.com
27
- *
28
- */
29
-
30
- import { CharStreams, CommonTokenStream, Token, CommonToken, Lexer } from "antlr4";
31
- import PythonLexer from "./PythonLexer.js";
32
- import PythonParser from "./PythonParser.js";
33
-
34
- export default class PythonLexerBase extends Lexer {
35
- constructor(input) {
36
- super(input);
37
-
38
- // A stack that keeps track of the indentation lengths
39
- this.indentLengthStack;
40
- // A list where tokens are waiting to be loaded into the token stream
41
- this.pendingTokens;
42
-
43
- // last pending token types
44
- this.previousPendingTokenType;
45
- this.lastPendingTokenTypeFromDefaultChannel;
46
-
47
- // The amount of opened parentheses, square brackets or curly braces
48
- this.opened;
49
- // The amount of opened parentheses and square brackets in the current lexer mode
50
- this.paren_or_bracket_openedStack;
51
- // A stack that stores expression(s) between braces in fstring
52
- this.braceExpressionStack;
53
- this.prevBraceExpression;
54
-
55
- // Instead of this._mode (_mode is not implemented in each ANTLR4 runtime)
56
- this.curLexerMode;
57
- // Instead of this._modeStack (_modeStack is not implemented in each ANTLR4 runtime)
58
- this.lexerModeStack;
59
-
60
- this.wasSpaceIndentation;
61
- this.wasTabIndentation;
62
- this.wasIndentationMixedWithSpacesAndTabs;
63
-
64
- this.curToken; // current (under processing) token
65
- this.ffgToken; // following (look ahead) token
66
-
67
- this.#init();
68
- }
69
-
70
- get #INVALID_LENGTH() { return -1; }
71
- get #ERR_TXT() { return " ERROR: "; }
72
-
73
- nextToken() { // reading the input stream until a return EOF
74
- this.#checkNextToken();
75
- return this.pendingTokens.shift() /* stack pollFirst() */; // add the queued token to the token stream
76
- }
77
-
78
- reset() {
79
- this.#init();
80
- super.reset();
81
- }
82
-
83
- #init() {
84
- this.indentLengthStack = [];
85
- this.pendingTokens = [];
86
- this.previousPendingTokenType = 0;
87
- this.lastPendingTokenTypeFromDefaultChannel = 0;
88
- this.opened = 0;
89
- this.paren_or_bracket_openedStack = [];
90
- this.braceExpressionStack = [];
91
- this.prevBraceExpression = "";
92
- this.curLexerMode = 0;
93
- this.lexerModeStack = [];
94
- this.wasSpaceIndentation = false;
95
- this.wasTabIndentation = false;
96
- this.wasIndentationMixedWithSpacesAndTabs = false;
97
- this.curToken = null;
98
- this.ffgToken = null;
99
- }
100
-
101
- #checkNextToken() {
102
- if (this.previousPendingTokenType === Token.EOF)
103
- return;
104
-
105
- if (this.indentLengthStack.length === 0) { // We're at the first token
106
- this.#insertENCODINGtoken();
107
- this.#setCurrentAndFollowingTokens();
108
- this.#handleStartOfInput();
109
- } else {
110
- this.#setCurrentAndFollowingTokens();
111
- }
112
-
113
- switch (this.curToken.type) {
114
- case PythonLexer.NEWLINE:
115
- this.#handleNEWLINEtoken();
116
- break;
117
- case PythonLexer.LPAR:
118
- case PythonLexer.LSQB:
119
- case PythonLexer.LBRACE:
120
- this.opened++;
121
- this.#addPendingToken(this.curToken);
122
- break;
123
- case PythonLexer.RPAR:
124
- case PythonLexer.RSQB:
125
- case PythonLexer.RBRACE:
126
- this.opened--;
127
- this.#addPendingToken(this.curToken);
128
- break;
129
- case PythonLexer.FSTRING_MIDDLE:
130
- this.#handleFSTRING_MIDDLEtokenWithDoubleBrace(); // does not affect the opened field
131
- this.#addPendingToken(this.curToken);
132
- break;
133
- case PythonLexer.COLONEQUAL:
134
- this.#handleCOLONEQUALtokenInFString();
135
- break;
136
- case PythonLexer.ERRORTOKEN:
137
- this.#reportLexerError(`token recognition error at: '${this.curToken.text}'`);
138
- this.#addPendingToken(this.curToken);
139
- break;
140
- case Token.EOF:
141
- this.#handleEOFtoken();
142
- break;
143
- default:
144
- this.#addPendingToken(this.curToken);
145
- }
146
- this.#handleFORMAT_SPECIFICATION_MODE();
147
- }
148
-
149
- #setCurrentAndFollowingTokens() {
150
- this.curToken = this.ffgToken == undefined ?
151
- super.nextToken() :
152
- this.ffgToken;
153
-
154
- this.#checkCurToken(); // ffgToken cannot be used in this method and its sub methods (ffgToken is not yet set)!
155
-
156
- this.ffgToken = this.curToken.type === Token.EOF ?
157
- this.curToken :
158
- super.nextToken();
159
- }
160
-
161
- #insertENCODINGtoken() {
162
- let lineBuilder = [];
163
- let encodingName = "";
164
- let lineCount = 0;
165
- const ws_commentPattern = /^[ \t\f]*(#.*)?$/;
166
- const inputStream = this.inputStream;
167
- const size = inputStream.size;
168
-
169
- inputStream.seek(0);
170
- for (let i = 0; i < size; i++) {
171
- let c = String.fromCharCode(inputStream.LA(i + 1));
172
- lineBuilder.push(c);
173
-
174
- if (c == '\n' || i == size - 1) {
175
- let line = lineBuilder.join("").replace("\r", "").replace("\n", "");
176
- if (ws_commentPattern.test(line)) { // WS* + COMMENT? found
177
- encodingName = this.#getEncodingName(line);
178
- if (encodingName !== "") {
179
- break; // encoding found
180
- }
181
- } else {
182
- break; // statement or backslash found (line is not empty, not whitespace, not comment)
183
- }
184
-
185
- lineCount++;
186
- if (lineCount >= 2) {
187
- break; // check only the first two lines
188
- }
189
- lineBuilder = [];
190
- }
191
- }
192
-
193
- if (encodingName === "") {
194
- encodingName = "utf-8"; // default Python source code encoding
195
- }
196
-
197
- const encodingToken = new CommonToken([null, null], PythonLexer.ENCODING, Token.HIDDEN_CHANNEL, 0, 0);
198
- encodingToken.text = encodingName;
199
- encodingToken.line = 0;
200
- encodingToken.column = -1;
201
- this.#addPendingToken(encodingToken);
202
- }
203
-
204
- #getEncodingName(commentText) { // https://peps.python.org/pep-0263/#defining-the-encoding
205
- const encodingCommentPattern = /^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)/;
206
- const match = commentText.match(encodingCommentPattern);
207
- return match ? match[1] : "";
208
- }
209
-
210
- // initialize the _indentLengthStack
211
- // hide the leading NEWLINE token(s)
212
- // if exists, find the first statement (not NEWLINE, not EOF token) that comes from the default channel
213
- // insert a leading INDENT token if necessary
214
- #handleStartOfInput() {
215
- // initialize the stack with a default 0 indentation length
216
- this.indentLengthStack.push(0); // this will never be popped off
217
- while (this.curToken.type !== Token.EOF) {
218
- if (this.curToken.channel === Token.DEFAULT_CHANNEL) {
219
- if (this.curToken.type === PythonLexer.NEWLINE) {
220
- // all the NEWLINE tokens must be ignored before the first statement
221
- this.#hideAndAddPendingToken(this.curToken);
222
- } else { // We're at the first statement
223
- this.#insertLeadingIndentToken();
224
- return; // continue the processing of the current token with #checkNextToken()
225
- }
226
- } else {
227
- this.#addPendingToken(this.curToken); // it can be WS, EXPLICIT_LINE_JOINING or COMMENT token
228
- }
229
- this.#setCurrentAndFollowingTokens();
230
- } // continue the processing of the EOF token with #checkNextToken()
231
- }
232
-
233
- #insertLeadingIndentToken() {
234
- if (this.previousPendingTokenType === PythonLexer.WS) {
235
- const prevToken = this.pendingTokens.at(- 1); /* stack peekLast() */ // WS token
236
- if (this.#getIndentationLength(prevToken.text) !== 0) { // there is an "indentation" before the first statement
237
- const errMsg = "first statement indented";
238
- this.#reportLexerError(errMsg);
239
- // insert an INDENT token before the first statement to raise an 'unexpected indent' error later by the parser
240
- this.#createAndAddPendingToken(PythonLexer.INDENT, Token.DEFAULT_CHANNEL, this.#ERR_TXT + errMsg, this.curToken);
241
- }
242
- }
243
- }
244
-
245
- #handleNEWLINEtoken() {
246
- if (this.lexerModeStack.length > 0) {
247
- this.#addPendingToken(this.curToken);
248
- } else if (this.opened > 0) { // We're in an implicit line joining, ignore the current NEWLINE token
249
- this.#hideAndAddPendingToken(this.curToken);
250
- } else {
251
- const nlToken = this.curToken.clone(); // save the current NEWLINE token
252
- const isLookingAhead = this.ffgToken.type === PythonLexer.WS;
253
- if (isLookingAhead) {
254
- this.#setCurrentAndFollowingTokens(); // set the next two tokens
255
- }
256
-
257
- switch (this.ffgToken.type) {
258
- case PythonLexer.NEWLINE: // We're before a blank line
259
- case PythonLexer.COMMENT: // We're before a comment
260
- this.#hideAndAddPendingToken(nlToken);
261
- if (isLookingAhead) {
262
- this.#addPendingToken(this.curToken); // WS token
263
- }
264
- break;
265
- default:
266
- this.#addPendingToken(nlToken);
267
- if (isLookingAhead) { // We're on a whitespace(s) followed by a statement
268
- const indentationLength = this.ffgToken.type === Token.EOF ?
269
- 0 :
270
- this.#getIndentationLength(this.curToken.text);
271
-
272
- if (indentationLength !== this.#INVALID_LENGTH) {
273
- this.#addPendingToken(this.curToken); // WS token
274
- this.#insertIndentOrDedentToken(indentationLength); // may insert INDENT token or DEDENT token(s)
275
- } else {
276
- this.#reportError("inconsistent use of tabs and spaces in indentation");
277
- }
278
- } else { // We're at a newline followed by a statement (there is no whitespace before the statement)
279
- this.#insertIndentOrDedentToken(0); // may insert DEDENT token(s)
280
- }
281
- }
282
- }
283
- }
284
-
285
- #insertIndentOrDedentToken(curIndentLength) {
286
- let prevIndentLength = this.indentLengthStack.at(-1) /* peek() */;
287
- if (curIndentLength > prevIndentLength) {
288
- this.#createAndAddPendingToken(PythonLexer.INDENT, Token.DEFAULT_CHANNEL, null, this.ffgToken);
289
- this.indentLengthStack.push(curIndentLength);
290
- } else {
291
- while (curIndentLength < prevIndentLength) { // more than 1 DEDENT token may be inserted to the token stream
292
- this.indentLengthStack.pop();
293
- prevIndentLength = this.indentLengthStack.at(-1) /* peek() */;
294
- if (curIndentLength <= prevIndentLength) {
295
- this.#createAndAddPendingToken(PythonLexer.DEDENT, Token.DEFAULT_CHANNEL, null, this.ffgToken);
296
- } else {
297
- this.#reportError("inconsistent dedent");
298
- }
299
- }
300
- }
301
- }
302
-
303
- #checkCurToken() {
304
- switch (this.curToken.type) {
305
- case PythonLexer.FSTRING_START:
306
- this.#setLexerModeByFSTRING_STARTtoken();
307
- return;
308
- case PythonLexer.FSTRING_MIDDLE:
309
- this.#handleFSTRING_MIDDLEtokenWithQuoteAndLBrace(); // affect the opened field
310
- if (this.curToken.type === PythonLexer.FSTRING_MIDDLE) {
311
- return; // No curToken exchange happened
312
- }
313
- break;
314
- case PythonLexer.FSTRING_END:
315
- this.#popLexerMode();
316
- return;
317
- default:
318
- if (this.lexerModeStack.length === 0) {
319
- return; // Not in fstring mode
320
- }
321
- }
322
-
323
- switch (this.curToken.type) { // the following tokens can only come from default mode (after an LBRACE in fstring)
324
- case PythonLexer.NEWLINE:
325
- // append the current brace expression with the current newline
326
- this.#appendToBraceExpression(this.curToken.text)
327
- this.curToken.channel = Token.HIDDEN_CHANNEL;
328
- break;
329
- case PythonLexer.LBRACE:
330
- // the outermost brace expression cannot be a dictionary comprehension or a set comprehension
331
- this.braceExpressionStack.push("{");
332
- this.paren_or_bracket_openedStack.push(0);
333
- this.#pushLexerMode(Lexer.DEFAULT_MODE);
334
- break;
335
- case PythonLexer.LPAR:
336
- case PythonLexer.LSQB:
337
- // append the current brace expression with a "(" or a "["
338
- this.#appendToBraceExpression(this.curToken.text)
339
- // https://peps.python.org/pep-0498/#lambdas-inside-expressions
340
- this.#incrementBraceStack();
341
- break;
342
- case PythonLexer.RPAR:
343
- case PythonLexer.RSQB:
344
- // append the current brace expression with a ")" or a "]"
345
- this.#appendToBraceExpression(this.curToken.text)
346
- this.#decrementBraceStack();
347
- break;
348
- case PythonLexer.COLON:
349
- case PythonLexer.COLONEQUAL:
350
- // append the current brace expression with a ":" or a ":="
351
- this.#appendToBraceExpression(this.curToken.text)
352
- this.#setLexerModeByCOLONorCOLONEQUALtoken();
353
- break;
354
- case PythonLexer.RBRACE:
355
- this.#setLexerModeAfterRBRACEtoken();
356
- break;
357
- default:
358
- // append the current brace expression with the current token text
359
- this.#appendToBraceExpression(this.curToken.text)
360
- }
361
- }
362
-
363
- #appendToBraceExpression(text) {
364
- this.braceExpressionStack[this.braceExpressionStack.length - 1] += text;
365
- }
366
-
367
- #incrementBraceStack() { // increment the last element (peek() + 1)
368
- this.paren_or_bracket_openedStack[this.paren_or_bracket_openedStack.length - 1]++;
369
- }
370
-
371
- #decrementBraceStack() { // decrement the last element (peek() - 1)
372
- this.paren_or_bracket_openedStack[this.paren_or_bracket_openedStack.length - 1]--;
373
- }
374
-
375
- #setLexerModeAfterRBRACEtoken() {
376
- switch (this.curLexerMode) {
377
- case Lexer.DEFAULT_MODE:
378
- this.#popLexerMode();
379
- this.#popByBRACE();
380
- break;
381
- case PythonLexer.SQ1__FORMAT_SPECIFICATION_MODE:
382
- case PythonLexer.SQ1R_FORMAT_SPECIFICATION_MODE:
383
- case PythonLexer.DQ1__FORMAT_SPECIFICATION_MODE:
384
- case PythonLexer.DQ1R_FORMAT_SPECIFICATION_MODE:
385
- case PythonLexer.SQ3__FORMAT_SPECIFICATION_MODE:
386
- case PythonLexer.SQ3R_FORMAT_SPECIFICATION_MODE:
387
- case PythonLexer.DQ3__FORMAT_SPECIFICATION_MODE:
388
- case PythonLexer.DQ3R_FORMAT_SPECIFICATION_MODE:
389
- this.#popLexerMode();
390
- this.#popLexerMode();
391
- this.#popByBRACE();
392
- break;
393
- default:
394
- this.#reportLexerError("f-string: single '}' is not allowed");
395
- }
396
- }
397
-
398
- #setLexerModeByFSTRING_STARTtoken() {
399
- const text = this.curToken.text.toLowerCase();
400
- const modeMap = {
401
- "f'": PythonLexer.SQ1__FSTRING_MODE,
402
- "rf'": PythonLexer.SQ1R_FSTRING_MODE,
403
- "fr'": PythonLexer.SQ1R_FSTRING_MODE,
404
- 'f"': PythonLexer.DQ1__FSTRING_MODE,
405
- 'rf"': PythonLexer.DQ1R_FSTRING_MODE,
406
- 'fr"': PythonLexer.DQ1R_FSTRING_MODE,
407
- "f'''": PythonLexer.SQ3__FSTRING_MODE,
408
- "rf'''": PythonLexer.SQ3R_FSTRING_MODE,
409
- "fr'''": PythonLexer.SQ3R_FSTRING_MODE,
410
- 'f"""': PythonLexer.DQ3__FSTRING_MODE,
411
- 'rf"""': PythonLexer.DQ3R_FSTRING_MODE,
412
- 'fr"""': PythonLexer.DQ3R_FSTRING_MODE,
413
- };
414
- const mode = modeMap[text];
415
- if (mode !== undefined) {
416
- this.#pushLexerMode(mode);
417
- }
418
- }
419
-
420
- #setLexerModeByCOLONorCOLONEQUALtoken() {
421
- if (this.paren_or_bracket_openedStack[this.paren_or_bracket_openedStack.length - 1] === 0) { // stack peek == 0
422
- const previousMode = this.lexerModeStack[this.lexerModeStack.length - 1]; // stack peek
423
- switch (previousMode) { // check the previous lexer mode (the current is DEFAULT_MODE)
424
- case PythonLexer.SQ1__FSTRING_MODE:
425
- case PythonLexer.SQ1__FORMAT_SPECIFICATION_MODE:
426
- this.#pushLexerMode(PythonLexer.SQ1__FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
427
- break;
428
- case PythonLexer.SQ1R_FSTRING_MODE:
429
- case PythonLexer.SQ1R_FORMAT_SPECIFICATION_MODE:
430
- this.#pushLexerMode(PythonLexer.SQ1R_FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
431
- break;
432
- case PythonLexer.DQ1__FSTRING_MODE:
433
- case PythonLexer.DQ1__FORMAT_SPECIFICATION_MODE:
434
- this.#pushLexerMode(PythonLexer.DQ1__FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
435
- break;
436
- case PythonLexer.DQ1R_FSTRING_MODE:
437
- case PythonLexer.DQ1R_FORMAT_SPECIFICATION_MODE:
438
- this.#pushLexerMode(PythonLexer.DQ1R_FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
439
- break;
440
- case PythonLexer.SQ3__FSTRING_MODE:
441
- case PythonLexer.SQ3__FORMAT_SPECIFICATION_MODE:
442
- this.#pushLexerMode(PythonLexer.SQ3__FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
443
- break;
444
- case PythonLexer.SQ3R_FSTRING_MODE:
445
- case PythonLexer.SQ3R_FORMAT_SPECIFICATION_MODE:
446
- this.#pushLexerMode(PythonLexer.SQ3R_FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
447
- break;
448
- case PythonLexer.DQ3__FSTRING_MODE:
449
- case PythonLexer.DQ3__FORMAT_SPECIFICATION_MODE:
450
- this.#pushLexerMode(PythonLexer.DQ3__FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
451
- break;
452
- case PythonLexer.DQ3R_FSTRING_MODE:
453
- case PythonLexer.DQ3R_FORMAT_SPECIFICATION_MODE:
454
- this.#pushLexerMode(PythonLexer.DQ3R_FORMAT_SPECIFICATION_MODE); // continue in format spec. mode
455
- break;
456
- }
457
- }
458
- }
459
-
460
- #popByBRACE() {
461
- this.paren_or_bracket_openedStack.pop();
462
- this.prevBraceExpression = this.braceExpressionStack.pop() + "}";
463
- if (this.braceExpressionStack.length > 0) {
464
- // append the current brace expression with the previous brace expression
465
- this.braceExpressionStack[this.braceExpressionStack.length - 1] += this.prevBraceExpression;
466
- }
467
- }
468
-
469
- #handleFSTRING_MIDDLEtokenWithDoubleBrace() {
470
- // replace the trailing double brace with a single brace and insert a hidden brace token
471
- switch (this.#getLastTwoCharsOfTheCurTokenText()) {
472
- case "{{":
473
- this.#trimLastCharAddPendingTokenSetCurToken(PythonLexer.LBRACE, "{", Token.HIDDEN_CHANNEL);
474
- break;
475
- case "}}":
476
- this.#trimLastCharAddPendingTokenSetCurToken(PythonLexer.RBRACE, "}", Token.HIDDEN_CHANNEL);
477
- break;
478
- }
479
- }
480
-
481
- #handleFSTRING_MIDDLEtokenWithQuoteAndLBrace() {
482
- // replace the trailing quote + left_brace with a quote and insert an LBRACE token
483
- // replace the trailing backslash + left_brace with a backslash and insert an LBRACE token
484
- switch (this.#getLastTwoCharsOfTheCurTokenText()) {
485
- case "\"{":
486
- case "'{":
487
- case "\\{":
488
- this.#trimLastCharAddPendingTokenSetCurToken(PythonLexer.LBRACE, "{", Token.DEFAULT_CHANNEL);
489
- break;
490
- }
491
- }
492
-
493
- #getLastTwoCharsOfTheCurTokenText() {
494
- return this.curToken.text.slice(-2);
495
- }
496
-
497
- #trimLastCharAddPendingTokenSetCurToken(type, text, channel) {
498
- // trim the last char and add the modified curToken to the pendingTokens stack
499
- const tokenTextWithoutLastChar = this.curToken.text.slice(0, -1);
500
- this.curToken.text = tokenTextWithoutLastChar;
501
- this.curToken.stop -= 1;
502
- this.#addPendingToken(this.curToken);
503
-
504
- this.#createNewCurToken(type, text, channel); // set curToken
505
- }
506
-
507
- #handleCOLONEQUALtokenInFString() {
508
- if (this.lexerModeStack.length > 0 &&
509
- this.paren_or_bracket_openedStack[this.paren_or_bracket_openedStack.length - 1] === 0) { // stack peek == 0
510
-
511
- // In fstring a colonequal (walrus operator) can only be used in parentheses
512
- // Not in parentheses, replace COLONEQUAL token with COLON as format specifier
513
- // and insert the equal symbol to the following FSTRING_MIDDLE token
514
- this.curToken.type = PythonLexer.COLON;
515
- this.curToken.text = ":";
516
- this.curToken.stop = this.curToken.start;
517
-
518
- if (this.ffgToken.type === PythonLexer.FSTRING_MIDDLE) {
519
- this.ffgToken.text = "=" + this.ffgToken.text;
520
- this.ffgToken.start -= 1;
521
- this.ffgToken.column -= 1;
522
- } else {
523
- this.#addPendingToken(this.curToken);
524
- this.#createNewCurToken(PythonLexer.FSTRING_MIDDLE, "=", Token.DEFAULT_CHANNEL);
525
- }
526
- }
527
- this.#addPendingToken(this.curToken);
528
- }
529
-
530
- #createNewCurToken(type, text, channel) {
531
- const ctkn = this.curToken.clone();
532
- ctkn.type = type;
533
- ctkn.text = text;
534
- ctkn.channel = channel;
535
- ctkn.column += 1;
536
- ctkn.start += 1;
537
- ctkn.stop = ctkn.start;
538
- this.curToken = ctkn;
539
- }
540
-
541
- #pushLexerMode(mode) {
542
- this.pushMode(mode);
543
- this.lexerModeStack.push(this.curLexerMode);
544
- this.curLexerMode = mode;
545
- }
546
-
547
- #popLexerMode() {
548
- this.popMode();
549
- this.curLexerMode = this.lexerModeStack.pop();
550
- }
551
-
552
- #handleFORMAT_SPECIFICATION_MODE() {
553
- if (this.lexerModeStack.length > 0 &&
554
- this.ffgToken.type === PythonLexer.RBRACE) {
555
-
556
- // insert an empty FSTRING_MIDDLE token instead of the missing format specification
557
- switch (this.curToken.type) {
558
- case PythonLexer.COLON:
559
- this.#createAndAddPendingToken(PythonLexer.FSTRING_MIDDLE, Token.DEFAULT_CHANNEL, "", this.ffgToken);
560
- break;
561
- case PythonLexer.RBRACE:
562
- // only if the previous brace expression is not a dictionary comprehension or set comprehension
563
- if (!this.#isDictionaryComprehensionOrSetComprehension(this.prevBraceExpression)) {
564
- this.#createAndAddPendingToken(PythonLexer.FSTRING_MIDDLE, Token.DEFAULT_CHANNEL, "", this.ffgToken);
565
- }
566
- break;
567
- }
568
- }
569
- }
570
-
571
- #isDictionaryComprehensionOrSetComprehension(code) {
572
- const inputStream = CharStreams.fromString(code);
573
- const lexer = new PythonLexer(inputStream);
574
- const tokenStream = new CommonTokenStream(lexer);
575
- let parser = new PythonParser(tokenStream);
576
-
577
- // Disable error listeners to suppress console output
578
- lexer.removeErrorListeners();
579
- parser.removeErrorListeners();
580
-
581
- parser.dictcomp(); // Try parsing as dictionary comprehension
582
- if (parser.syntaxErrorsCount === 0)
583
- return true;
584
-
585
- parser = new PythonParser(tokenStream);
586
- tokenStream.seek(0);
587
- parser.removeErrorListeners();
588
- parser.setcomp(); // Try parsing as set comprehension
589
- return parser.syntaxErrorsCount === 0;
590
- }
591
-
592
- #insertTrailingTokens() {
593
- switch (this.lastPendingTokenTypeFromDefaultChannel) {
594
- case PythonLexer.NEWLINE:
595
- case PythonLexer.DEDENT:
596
- break; // no trailing NEWLINE token is needed
597
- default:
598
- // insert an extra trailing NEWLINE token that serves as the end of the last statement
599
- this.#createAndAddPendingToken(PythonLexer.NEWLINE, Token.DEFAULT_CHANNEL, null, this.ffgToken); // _ffgToken is EOF
600
- }
601
- this.#insertIndentOrDedentToken(0); // Now insert as much trailing DEDENT tokens as needed
602
- }
603
-
604
- #handleEOFtoken() {
605
- if (this.lastPendingTokenTypeFromDefaultChannel > 0) {
606
- // there was a statement in the input (leading NEWLINE tokens are hidden)
607
- this.#insertTrailingTokens();
608
- }
609
- this.#addPendingToken(this.curToken);
610
- }
611
-
612
- #hideAndAddPendingToken(ctkn) {
613
- ctkn.channel = Token.HIDDEN_CHANNEL;
614
- this.#addPendingToken(ctkn);
615
- }
616
-
617
- #createAndAddPendingToken(type, channel, text, sampleToken) {
618
- const ctkn = sampleToken.clone();
619
- ctkn.type = type;
620
- ctkn.channel = channel;
621
- ctkn.stop = sampleToken.start - 1;
622
- ctkn.text = text == null ?
623
- `<${this.getSymbolicNames()[type]}>` :
624
- text;
625
-
626
- this.#addPendingToken(ctkn);
627
- }
628
-
629
- #addPendingToken(tkn) {
630
- // save the last pending token type because the _pendingTokens linked list can be empty by the nextToken()
631
- this.previousPendingTokenType = tkn.type;
632
- if (tkn.channel === Token.DEFAULT_CHANNEL) {
633
- this.lastPendingTokenTypeFromDefaultChannel = this.previousPendingTokenType;
634
- }
635
- this.pendingTokens.push(tkn) /* .addLast(token) */;
636
- }
637
-
638
- #getIndentationLength(indentText) { // the indentText may contain spaces, tabs or form feeds
639
- const TAB_LENGTH = 8; // the standard number of spaces to replace a tab to spaces
640
- let length = 0;
641
- for (let ch of indentText) {
642
- switch (ch) {
643
- case " ":
644
- this.wasSpaceIndentation = true;
645
- length += 1;
646
- break;
647
- case "\t":
648
- this.wasTabIndentation = true;
649
- length += TAB_LENGTH - (length % TAB_LENGTH);
650
- break;
651
- case "\f": // form feed
652
- length = 0;
653
- break;
654
- }
655
- }
656
-
657
- if (this.wasTabIndentation && this.wasSpaceIndentation) {
658
- if (!this.wasIndentationMixedWithSpacesAndTabs) {
659
- this.wasIndentationMixedWithSpacesAndTabs = true;
660
- length = this.#INVALID_LENGTH; // only for the first inconsistent indent
661
- }
662
- }
663
- return length;
664
- }
665
-
666
- #reportLexerError(errMsg) {
667
- this.getErrorListener().syntaxError(this, this.curToken, this.curToken.line, this.curToken.column, " LEXER" + this.#ERR_TXT + errMsg, null);
668
- }
669
-
670
- #reportError(errMsg) {
671
- this.#reportLexerError(errMsg);
672
-
673
- // the ERRORTOKEN will raise an error in the parser
674
- this.#createAndAddPendingToken(PythonLexer.ERRORTOKEN, Token.DEFAULT_CHANNEL, this.#ERR_TXT + errMsg, this.ffgToken);
675
- }
676
- }