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.
- package/README.md +4 -3
- package/asm/asm8080/asm8080Lexer.js +253 -152
- package/asm/asm8080/asm8080Listener.js +72 -0
- package/asm/asm8080/asm8080Parser.js +1307 -289
- package/asm/asm8080/asm8080Visitor.js +48 -0
- package/asm/asmZ80/asmZ80Lexer.js +152 -128
- package/asm/asmZ80/asmZ80Listener.js +9 -0
- package/asm/asmZ80/asmZ80Parser.js +316 -170
- package/asm/asmZ80/asmZ80Visitor.js +6 -0
- package/c/CParser.js +1352 -1344
- package/cypher/CypherLexer.js +208 -205
- package/cypher/CypherParser.js +1180 -984
- package/cypher/CypherParserListener.js +18 -0
- package/cypher/CypherParserVisitor.js +12 -0
- package/package.json +1 -1
- package/python/python3_14/PythonLexer.js +1238 -0
- package/python/python3_14/PythonLexerBase.js +819 -0
- package/python/{python3_13 → python3_14}/PythonParser.js +4141 -3347
- package/python/{python3_13 → python3_14}/PythonParserListener.js +81 -0
- package/python/{python3_13 → python3_14}/PythonParserVisitor.js +54 -0
- package/sql/plsql/PlSqlLexer.js +4114 -4113
- package/sql/plsql/PlSqlParser.js +37074 -31651
- package/sql/plsql/PlSqlParserBase.js +31 -0
- package/sql/plsql/PlSqlParserListener.js +58 -4
- package/sql/plsql/PlSqlParserVisitor.js +38 -2
- package/sysml-v2/SysMLv2Lexer.js +617 -605
- package/sysml-v2/SysMLv2Parser.js +7135 -10083
- package/sysml-v2/SysMLv2ParserListener.js +45 -423
- package/sysml-v2/SysMLv2ParserVisitor.js +30 -282
- package/xpath/xpath4/XPath4Lexer.js +667 -0
- package/xpath/xpath4/XPath4Parser.js +17424 -0
- package/xpath/xpath4/XPath4ParserBase.js +34 -0
- package/xpath/xpath4/XPath4ParserListener.js +1521 -0
- package/xpath/xpath4/XPath4ParserVisitor.js +1018 -0
- package/xquery/xquery4/XQuery4Lexer.js +1396 -0
- package/xquery/xquery4/XQuery4LexerBase.js +42 -0
- package/xquery/xquery4/XQuery4Parser.js +27899 -0
- package/xquery/xquery4/XQuery4ParserBase.js +38 -0
- package/xquery/xquery4/XQuery4ParserListener.js +2322 -0
- package/xquery/xquery4/XQuery4ParserVisitor.js +1552 -0
- package/awk/awkLexer.js +0 -317
- package/awk/awkListener.js +0 -378
- package/awk/awkParser.js +0 -5859
- package/awk/awkVisitor.js +0 -256
- package/python/python3_13/PythonLexer.js +0 -974
- package/python/python3_13/PythonLexerBase.js +0 -676
|
@@ -0,0 +1,819 @@
|
|
|
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 : A helper class for an ANTLR4 Python lexer grammar that assists in tokenizing indentation,
|
|
25
|
+
* interpolated strings, and encoding declaration.
|
|
26
|
+
*
|
|
27
|
+
* Developed by : Robert Einhorn, robert.einhorn.hu@gmail.com
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
'use strict';
|
|
32
|
+
|
|
33
|
+
import { CharStreams, CommonTokenStream, Token, CommonToken, Lexer } from "antlr4";
|
|
34
|
+
import PythonLexer from "./PythonLexer.js";
|
|
35
|
+
import PythonParser from "./PythonParser.js";
|
|
36
|
+
|
|
37
|
+
export default class PythonLexerBase extends Lexer {
|
|
38
|
+
static #INVALID_LENGTH = -1;
|
|
39
|
+
static #ERR_TXT = " ERROR: ";
|
|
40
|
+
static #TAB_LENGTH = 8;
|
|
41
|
+
static #LEXER_MODES_FOR_ISTRING_START = new Map();
|
|
42
|
+
|
|
43
|
+
#encodingName;
|
|
44
|
+
|
|
45
|
+
// Indentation handling
|
|
46
|
+
#indentationLengthStack;
|
|
47
|
+
#pendingTokenQueue;
|
|
48
|
+
|
|
49
|
+
#previousPendingTokenType;
|
|
50
|
+
#lastPendingTokenTypeFromDefaultChannel;
|
|
51
|
+
|
|
52
|
+
// Parenthesis / bracket / brace counts
|
|
53
|
+
#openParenBracketBraceCount;
|
|
54
|
+
#paren_or_bracket_openedStack;
|
|
55
|
+
#braceExpressionStack;
|
|
56
|
+
#lastClosedBraceExpression;
|
|
57
|
+
|
|
58
|
+
// Current interpolated STRING_MIDDLE token type (FSTRING_MIDDLE or TSTRING_MIDDLE)
|
|
59
|
+
#activeInterpolatedStringMiddleTokenType;
|
|
60
|
+
|
|
61
|
+
// We reimplement mode/stack because not all runtimes expose _mode/_modeStack
|
|
62
|
+
#curLexerMode;
|
|
63
|
+
#lexerModeStack;
|
|
64
|
+
|
|
65
|
+
// Indentation diagnostics
|
|
66
|
+
#wasSpaceIndentation;
|
|
67
|
+
#wasTabIndentation;
|
|
68
|
+
#hasMixedIndentationBeenReported;
|
|
69
|
+
|
|
70
|
+
// Current / lookahead tokens
|
|
71
|
+
#curToken;
|
|
72
|
+
#laToken;
|
|
73
|
+
|
|
74
|
+
constructor(input) {
|
|
75
|
+
super(input);
|
|
76
|
+
this.#init();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
reset() {
|
|
80
|
+
this.#init();
|
|
81
|
+
super.reset();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#init() {
|
|
85
|
+
this.#encodingName = "";
|
|
86
|
+
this.#indentationLengthStack = [];
|
|
87
|
+
this.#pendingTokenQueue = [];
|
|
88
|
+
this.#previousPendingTokenType = 0;
|
|
89
|
+
this.#lastPendingTokenTypeFromDefaultChannel = 0;
|
|
90
|
+
this.#openParenBracketBraceCount = 0;
|
|
91
|
+
this.#paren_or_bracket_openedStack = [];
|
|
92
|
+
this.#braceExpressionStack = [];
|
|
93
|
+
this.#lastClosedBraceExpression = "";
|
|
94
|
+
this.#activeInterpolatedStringMiddleTokenType = 0;
|
|
95
|
+
this.#curLexerMode = Lexer.DEFAULT_MODE;
|
|
96
|
+
this.#lexerModeStack = [];
|
|
97
|
+
this.#wasSpaceIndentation = false;
|
|
98
|
+
this.#wasTabIndentation = false;
|
|
99
|
+
this.#hasMixedIndentationBeenReported = false;
|
|
100
|
+
this.#curToken = null;
|
|
101
|
+
this.#laToken = null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Sets the encoding name to emit an ENCODING token at the start of the token stream.
|
|
106
|
+
* Leave empty if not needed (e.g., when parsing from string).
|
|
107
|
+
*
|
|
108
|
+
* @param {string} encodingName - The encoding name (e.g., "utf-8"), or empty string to disable ENCODING token.
|
|
109
|
+
*/
|
|
110
|
+
setEncodingName(encodingName) {
|
|
111
|
+
this.#encodingName = encodingName;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
nextToken() { // Reading the input stream until EOF is reached
|
|
115
|
+
this.#processCurrentToken();
|
|
116
|
+
return this.#pendingTokenQueue.shift() /* stack pollFirst() */; // Add the queued token to the token stream
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#processCurrentToken() {
|
|
120
|
+
if (this.#previousPendingTokenType === Token.EOF) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
this.#setCurrentAndFollowingTokens();
|
|
125
|
+
if (this.#indentationLengthStack.length === 0) { // We're at the first token
|
|
126
|
+
this.#handleStartOfInput();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
switch (this.#curToken.type) {
|
|
130
|
+
case PythonLexer.NEWLINE:
|
|
131
|
+
this.#handleNEWLINEtoken();
|
|
132
|
+
break;
|
|
133
|
+
case PythonLexer.LPAR:
|
|
134
|
+
case PythonLexer.LSQB:
|
|
135
|
+
case PythonLexer.LBRACE:
|
|
136
|
+
this.#openParenBracketBraceCount++;
|
|
137
|
+
this.#addPendingToken(this.#curToken);
|
|
138
|
+
break;
|
|
139
|
+
case PythonLexer.RPAR:
|
|
140
|
+
case PythonLexer.RSQB:
|
|
141
|
+
case PythonLexer.RBRACE:
|
|
142
|
+
this.#openParenBracketBraceCount--;
|
|
143
|
+
this.#addPendingToken(this.#curToken);
|
|
144
|
+
break;
|
|
145
|
+
case PythonLexer.FSTRING_MIDDLE:
|
|
146
|
+
case PythonLexer.TSTRING_MIDDLE:
|
|
147
|
+
this.#handleFSTRING_MIDDLEtokenWithDoubleBrace(); // does not affect the opened field
|
|
148
|
+
this.#addPendingToken(this.#curToken);
|
|
149
|
+
break;
|
|
150
|
+
case PythonLexer.COLONEQUAL:
|
|
151
|
+
this.#handleCOLONEQUALtokenInIString();
|
|
152
|
+
break;
|
|
153
|
+
case PythonLexer.ERRORTOKEN:
|
|
154
|
+
this.#reportLexerError(`token recognition error at: '${this.#curToken.text}'`);
|
|
155
|
+
this.#addPendingToken(this.#curToken);
|
|
156
|
+
break;
|
|
157
|
+
case Token.EOF:
|
|
158
|
+
this.#handleEOFtoken();
|
|
159
|
+
break;
|
|
160
|
+
default:
|
|
161
|
+
this.#addPendingToken(this.#curToken);
|
|
162
|
+
}
|
|
163
|
+
this.#handleFORMAT_SPECIFICATION_MODE();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#setCurrentAndFollowingTokens() {
|
|
167
|
+
this.#curToken = this.#laToken == undefined ?
|
|
168
|
+
super.nextToken() :
|
|
169
|
+
this.#laToken;
|
|
170
|
+
|
|
171
|
+
this.#normalizeCurToken(); // Do not use laToken in this method or any of its submethods — it hasn't been set yet!
|
|
172
|
+
|
|
173
|
+
this.#laToken = this.#curToken.type === Token.EOF ?
|
|
174
|
+
this.#curToken :
|
|
175
|
+
super.nextToken();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// - initialize indent stack
|
|
179
|
+
// - skip BOM token
|
|
180
|
+
// - insert ENCODING token (if any)
|
|
181
|
+
// - hide leading NEWLINE(s)
|
|
182
|
+
// - insert leading INDENT if first statement is indented
|
|
183
|
+
|
|
184
|
+
#handleStartOfInput() {
|
|
185
|
+
// - initialize indent stack with a default 0 indentation length
|
|
186
|
+
// - skip BOM token
|
|
187
|
+
// - insert ENCODING token (if any)
|
|
188
|
+
// - hide leading NEWLINE(s)
|
|
189
|
+
// - insert leading INDENT if first statement is indented
|
|
190
|
+
this.#indentationLengthStack.push(0); // this will never be popped off
|
|
191
|
+
|
|
192
|
+
if (this.#curToken.type === PythonLexer.BOM) {
|
|
193
|
+
this.#setCurrentAndFollowingTokens();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
this.#insertENCODINGtoken();
|
|
197
|
+
|
|
198
|
+
while (this.#curToken.type !== Token.EOF) {
|
|
199
|
+
if (this.#curToken.channel === Token.DEFAULT_CHANNEL) {
|
|
200
|
+
if (this.#curToken.type === PythonLexer.NEWLINE) {
|
|
201
|
+
// all the NEWLINE tokens must be ignored before the first statement
|
|
202
|
+
this.#hideAndAddPendingToken(this.#curToken);
|
|
203
|
+
} else { // We're at the first statement
|
|
204
|
+
this.#insertLeadingIndentToken();
|
|
205
|
+
return; // continue the processing of the current token with #processCurrentToken()
|
|
206
|
+
}
|
|
207
|
+
} else {
|
|
208
|
+
this.#addPendingToken(this.#curToken); // it can be WS, EXPLICIT_LINE_JOINING or COMMENT token
|
|
209
|
+
}
|
|
210
|
+
this.#setCurrentAndFollowingTokens();
|
|
211
|
+
} // continue the processing of the EOF token with #processCurrentToken()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#insertENCODINGtoken() {
|
|
215
|
+
if (this.#encodingName === "") return
|
|
216
|
+
|
|
217
|
+
const sourcePair = [this, this._input];
|
|
218
|
+
const encodingToken = new CommonToken(sourcePair, PythonLexer.ENCODING, Token.HIDDEN_CHANNEL, /*start*/ 0, /*stop*/ 0);
|
|
219
|
+
encodingToken.text = this.#encodingName;
|
|
220
|
+
encodingToken.line = 0;
|
|
221
|
+
encodingToken.column = -1;
|
|
222
|
+
this.#addPendingToken(encodingToken);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#insertLeadingIndentToken() {
|
|
226
|
+
if (this.#previousPendingTokenType === PythonLexer.WS) {
|
|
227
|
+
const prevToken = this.#pendingTokenQueue.at(-1); /* stack peek */ // WS token
|
|
228
|
+
if (this.#getIndentationLength(prevToken.text) !== 0) { // there is an "indentation" before the first statement
|
|
229
|
+
const errMsg = "first statement indented";
|
|
230
|
+
this.#reportLexerError(errMsg);
|
|
231
|
+
// insert an INDENT token before the first statement to trigger an 'unexpected indent' error later in the parser
|
|
232
|
+
this.#createAndAddPendingToken(PythonLexer.INDENT, PythonLexerBase.#ERR_TXT + errMsg, this.#curToken);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ===================== Indentation Handling =====================
|
|
238
|
+
// Processes NEWLINE tokens, computes indentation length from leading
|
|
239
|
+
// whitespace, manages the INDENT/DEDENT stack, emits the appropriate
|
|
240
|
+
// indentation tokens, and detects inconsistent mixing of tabs and spaces
|
|
241
|
+
|
|
242
|
+
#handleNEWLINEtoken() {
|
|
243
|
+
if (this.#lexerModeStack.length > 0) { // for multi line f/t-string literals
|
|
244
|
+
this.#addPendingToken(this.#curToken);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (this.#openParenBracketBraceCount > 0) { // We're in an implicit line joining, ignore the current NEWLINE token
|
|
249
|
+
this.#hideAndAddPendingToken(this.#curToken);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const nlToken = this.#curToken.clone(); // save the current NEWLINE token
|
|
254
|
+
const isLookingAhead = this.#laToken.type === PythonLexer.WS;
|
|
255
|
+
if (isLookingAhead) {
|
|
256
|
+
this.#setCurrentAndFollowingTokens(); // set the next two tokens
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
switch (this.#laToken.type) {
|
|
260
|
+
case PythonLexer.NEWLINE: // We're before a blank line
|
|
261
|
+
case PythonLexer.COMMENT: // We're before a comment
|
|
262
|
+
this.#hideAndAddPendingToken(nlToken);
|
|
263
|
+
if (isLookingAhead) {
|
|
264
|
+
this.#addPendingToken(this.#curToken); // WS token
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
default:
|
|
268
|
+
this.#addPendingToken(nlToken);
|
|
269
|
+
if (isLookingAhead) { // We're on a whitespace(s) followed by a statement
|
|
270
|
+
const indentationLength = this.#laToken.type === Token.EOF ?
|
|
271
|
+
0 :
|
|
272
|
+
this.#getIndentationLength(this.#curToken.text);
|
|
273
|
+
|
|
274
|
+
if (indentationLength !== PythonLexerBase.#INVALID_LENGTH) {
|
|
275
|
+
this.#addPendingToken(this.#curToken); // WS token
|
|
276
|
+
this.#insertIndentOrDedentToken(indentationLength); // may insert INDENT token or DEDENT token(s)
|
|
277
|
+
} else {
|
|
278
|
+
this.#reportError("inconsistent use of tabs and spaces in indentation");
|
|
279
|
+
}
|
|
280
|
+
} else { // We're at a newline followed by a statement (there is no whitespace before the statement)
|
|
281
|
+
this.#insertIndentOrDedentToken(0); // may insert DEDENT token(s)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
#insertIndentOrDedentToken(curIndentLength) {
|
|
287
|
+
let prevIndentLength = this.#indentationLengthStack.at(-1) /* stack peek */;
|
|
288
|
+
if (curIndentLength > prevIndentLength) {
|
|
289
|
+
this.#createAndAddPendingToken(PythonLexer.INDENT, null, this.#laToken);
|
|
290
|
+
this.#indentationLengthStack.push(curIndentLength);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
while (curIndentLength < prevIndentLength) { // more than 1 DEDENT token may be inserted to the token stream
|
|
295
|
+
this.#indentationLengthStack.pop();
|
|
296
|
+
prevIndentLength = this.#indentationLengthStack.at(-1) /* stack peek */;
|
|
297
|
+
if (curIndentLength <= prevIndentLength) {
|
|
298
|
+
this.#createAndAddPendingToken(PythonLexer.DEDENT, null, this.#laToken);
|
|
299
|
+
} else {
|
|
300
|
+
this.#reportError("inconsistent dedent");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
#getIndentationLength(indentText) { // the indentText may contain spaces, tabs or form feeds
|
|
306
|
+
let length = 0;
|
|
307
|
+
for (let ch of indentText) {
|
|
308
|
+
switch (ch) {
|
|
309
|
+
case " ":
|
|
310
|
+
this.#wasSpaceIndentation = true;
|
|
311
|
+
length += 1;
|
|
312
|
+
break;
|
|
313
|
+
case "\t":
|
|
314
|
+
this.#wasTabIndentation = true;
|
|
315
|
+
length += PythonLexerBase.#TAB_LENGTH - (length % PythonLexerBase.#TAB_LENGTH);
|
|
316
|
+
break;
|
|
317
|
+
case "\f": // form feed
|
|
318
|
+
length = 0;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (this.#wasTabIndentation && this.#wasSpaceIndentation) {
|
|
324
|
+
if (!this.#hasMixedIndentationBeenReported) {
|
|
325
|
+
this.#hasMixedIndentationBeenReported = true;
|
|
326
|
+
length = PythonLexerBase.#INVALID_LENGTH; // only for the first inconsistent indent
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return length;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// ===================== IString Processing =====================
|
|
333
|
+
// Handles f- and t-string interpolation, including prefix detection,
|
|
334
|
+
// ISTRING_MIDDLE token rewriting, escaped brace handling ({{ and }}),
|
|
335
|
+
// quote+brace boundary cases, format-specifier mode transitions,
|
|
336
|
+
// and brace-expression accumulation inside interpolated segments.
|
|
337
|
+
|
|
338
|
+
#normalizeCurToken() {
|
|
339
|
+
switch (this.#curToken.type) {
|
|
340
|
+
case PythonLexer.FSTRING_START:
|
|
341
|
+
this.#activeInterpolatedStringMiddleTokenType = PythonLexer.FSTRING_MIDDLE;
|
|
342
|
+
this.#setLexerModeByISTRING_STARTtoken();
|
|
343
|
+
return;
|
|
344
|
+
case PythonLexer.TSTRING_START:
|
|
345
|
+
this.#activeInterpolatedStringMiddleTokenType = PythonLexer.TSTRING_MIDDLE;
|
|
346
|
+
this.#setLexerModeByISTRING_STARTtoken();
|
|
347
|
+
return;
|
|
348
|
+
case PythonLexer.FSTRING_MIDDLE:
|
|
349
|
+
case PythonLexer.TSTRING_MIDDLE:
|
|
350
|
+
this.#handleFSTRING_MIDDLEtokenWithQuoteAndLBrace(); // affect the opened field
|
|
351
|
+
switch (this.#curToken.type) {
|
|
352
|
+
case PythonLexer.FSTRING_MIDDLE:
|
|
353
|
+
case PythonLexer.TSTRING_MIDDLE:
|
|
354
|
+
return; // No curToken exchange happened
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
case PythonLexer.FSTRING_END:
|
|
358
|
+
case PythonLexer.TSTRING_END:
|
|
359
|
+
this.#popLexerMode();
|
|
360
|
+
return;
|
|
361
|
+
default:
|
|
362
|
+
if (this.#lexerModeStack.length === 0) {
|
|
363
|
+
return; // Not in fstring mode
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
}
|
|
367
|
+
this.#processBraceExpression();
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
#processBraceExpression() {
|
|
371
|
+
switch (this.#curToken.type) { // the following tokens can only come from default mode (after an LBRACE in f/t-string)
|
|
372
|
+
case PythonLexer.NEWLINE:
|
|
373
|
+
// append the current brace expression with the current newline
|
|
374
|
+
this.#appendToBraceExpression(this.#curToken.text)
|
|
375
|
+
this.#curToken.channel = Token.HIDDEN_CHANNEL;
|
|
376
|
+
break;
|
|
377
|
+
case PythonLexer.LBRACE:
|
|
378
|
+
// the outermost brace expression cannot be a dictionary comprehension or a set comprehension
|
|
379
|
+
this.#braceExpressionStack.push("{");
|
|
380
|
+
this.#paren_or_bracket_openedStack.push(0);
|
|
381
|
+
this.#pushLexerMode(Lexer.DEFAULT_MODE);
|
|
382
|
+
break;
|
|
383
|
+
case PythonLexer.LPAR:
|
|
384
|
+
case PythonLexer.LSQB:
|
|
385
|
+
// append the current brace expression with a "(" or a "["
|
|
386
|
+
this.#appendToBraceExpression(this.#curToken.text)
|
|
387
|
+
// https://peps.python.org/pep-0498/#lambdas-inside-expressions
|
|
388
|
+
this.#incrementBraceDepth();
|
|
389
|
+
break;
|
|
390
|
+
case PythonLexer.RPAR:
|
|
391
|
+
case PythonLexer.RSQB:
|
|
392
|
+
// append the current brace expression with a ")" or a "]"
|
|
393
|
+
this.#appendToBraceExpression(this.#curToken.text)
|
|
394
|
+
this.#decrementBraceDepth();
|
|
395
|
+
break;
|
|
396
|
+
case PythonLexer.COLON:
|
|
397
|
+
case PythonLexer.COLONEQUAL:
|
|
398
|
+
// append the current brace expression with a ":" or a ":="
|
|
399
|
+
this.#appendToBraceExpression(this.#curToken.text)
|
|
400
|
+
this.#setLexerModeByCOLONorCOLONEQUALtoken();
|
|
401
|
+
break;
|
|
402
|
+
case PythonLexer.RBRACE:
|
|
403
|
+
this.#setLexerModeAfterRBRACEtoken();
|
|
404
|
+
break;
|
|
405
|
+
default:
|
|
406
|
+
// append the current brace expression with the current token text
|
|
407
|
+
this.#appendToBraceExpression(this.#curToken.text)
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#appendToBraceExpression(text) {
|
|
412
|
+
const lastIndex = this.#braceExpressionStack.length - 1;
|
|
413
|
+
this.#braceExpressionStack[lastIndex] += text;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
#incrementBraceDepth() { // increment the last element (stack peek + 1)
|
|
417
|
+
const lastIndex = this.#paren_or_bracket_openedStack.length - 1;
|
|
418
|
+
this.#paren_or_bracket_openedStack[lastIndex]++;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
#decrementBraceDepth() { // decrement the last element (stack peek - 1)
|
|
422
|
+
const lastIndex = this.#paren_or_bracket_openedStack.length - 1;
|
|
423
|
+
this.#paren_or_bracket_openedStack[lastIndex]--;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
#setLexerModeAfterRBRACEtoken() {
|
|
427
|
+
switch (this.#curLexerMode) {
|
|
428
|
+
case Lexer.DEFAULT_MODE:
|
|
429
|
+
this.#popLexerMode();
|
|
430
|
+
this.#popByBRACE();
|
|
431
|
+
break;
|
|
432
|
+
case PythonLexer.SQ1__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
433
|
+
case PythonLexer.SQ1__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
434
|
+
case PythonLexer.SQ1R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
435
|
+
case PythonLexer.SQ1R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
436
|
+
case PythonLexer.DQ1__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
437
|
+
case PythonLexer.DQ1__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
438
|
+
case PythonLexer.DQ1R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
439
|
+
case PythonLexer.DQ1R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
440
|
+
case PythonLexer.SQ3__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
441
|
+
case PythonLexer.SQ3__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
442
|
+
case PythonLexer.SQ3R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
443
|
+
case PythonLexer.SQ3R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
444
|
+
case PythonLexer.DQ3__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
445
|
+
case PythonLexer.DQ3__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
446
|
+
case PythonLexer.DQ3R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
447
|
+
case PythonLexer.DQ3R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
448
|
+
this.#popLexerMode();
|
|
449
|
+
this.#popLexerMode();
|
|
450
|
+
this.#popByBRACE();
|
|
451
|
+
break;
|
|
452
|
+
default:
|
|
453
|
+
this.#reportLexerError("f-string: single '}' is not allowed");
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
#setLexerModeByISTRING_STARTtoken() { // ISTRING = interpolated string (FSTRING or TSTRING)
|
|
458
|
+
if (PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.size === 0) {
|
|
459
|
+
PythonLexerBase.#initLexerModesForIStringStart();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const interpolatedStringPrefix = this.#curToken.text.toLowerCase();
|
|
463
|
+
if (PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.has(interpolatedStringPrefix)) {
|
|
464
|
+
const newLexerMode = PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.get(interpolatedStringPrefix);
|
|
465
|
+
this.#pushLexerMode(newLexerMode);
|
|
466
|
+
} else {
|
|
467
|
+
this.#reportLexerError(
|
|
468
|
+
"internal error: unknown interpolated string literal prefix: " + this.#curToken.text
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
static #initLexerModesForIStringStart() {
|
|
474
|
+
// f-strings
|
|
475
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("f'", PythonLexer.SQ1__FSTRING_MODE);
|
|
476
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rf'", PythonLexer.SQ1R_FSTRING_MODE);
|
|
477
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("fr'", PythonLexer.SQ1R_FSTRING_MODE);
|
|
478
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("f\"", PythonLexer.DQ1__FSTRING_MODE);
|
|
479
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rf\"", PythonLexer.DQ1R_FSTRING_MODE);
|
|
480
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("fr\"", PythonLexer.DQ1R_FSTRING_MODE);
|
|
481
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("f'''", PythonLexer.SQ3__FSTRING_MODE);
|
|
482
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rf'''", PythonLexer.SQ3R_FSTRING_MODE);
|
|
483
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("fr'''", PythonLexer.SQ3R_FSTRING_MODE);
|
|
484
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("f\"\"\"", PythonLexer.DQ3__FSTRING_MODE);
|
|
485
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rf\"\"\"", PythonLexer.DQ3R_FSTRING_MODE);
|
|
486
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("fr\"\"\"", PythonLexer.DQ3R_FSTRING_MODE);
|
|
487
|
+
|
|
488
|
+
// t-strings
|
|
489
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("t'", PythonLexer.SQ1__TSTRING_MODE);
|
|
490
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rt'", PythonLexer.SQ1R_TSTRING_MODE);
|
|
491
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("tr'", PythonLexer.SQ1R_TSTRING_MODE);
|
|
492
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("t\"", PythonLexer.DQ1__TSTRING_MODE);
|
|
493
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rt\"", PythonLexer.DQ1R_TSTRING_MODE);
|
|
494
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("tr\"", PythonLexer.DQ1R_TSTRING_MODE);
|
|
495
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("t'''", PythonLexer.SQ3__TSTRING_MODE);
|
|
496
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rt'''", PythonLexer.SQ3R_TSTRING_MODE);
|
|
497
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("tr'''", PythonLexer.SQ3R_TSTRING_MODE);
|
|
498
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("t\"\"\"", PythonLexer.DQ3__TSTRING_MODE);
|
|
499
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("rt\"\"\"", PythonLexer.DQ3R_TSTRING_MODE);
|
|
500
|
+
PythonLexerBase.#LEXER_MODES_FOR_ISTRING_START.set("tr\"\"\"", PythonLexer.DQ3R_TSTRING_MODE);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
#setLexerModeByCOLONorCOLONEQUALtoken() {
|
|
504
|
+
// Exit early when the current lexer mode indicates an open parenthesis/bracket
|
|
505
|
+
const opened = this.#paren_or_bracket_openedStack.at(-1) > 0; /* stack peek */
|
|
506
|
+
if (opened) return;
|
|
507
|
+
|
|
508
|
+
// COLONEQUAL token will be replaced with a COLON token in processCurrentToken()
|
|
509
|
+
const prevLexerMode = this.#lexerModeStack.at(-1); /* stack peek */
|
|
510
|
+
switch (prevLexerMode) {
|
|
511
|
+
case PythonLexer.SQ1__FSTRING_MODE:
|
|
512
|
+
case PythonLexer.SQ1__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
513
|
+
this.#pushLexerMode(PythonLexer.SQ1__FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
514
|
+
break;
|
|
515
|
+
|
|
516
|
+
case PythonLexer.SQ1__TSTRING_MODE:
|
|
517
|
+
case PythonLexer.SQ1__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
518
|
+
this.#pushLexerMode(PythonLexer.SQ1__TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
519
|
+
break;
|
|
520
|
+
|
|
521
|
+
case PythonLexer.SQ1R_FSTRING_MODE:
|
|
522
|
+
case PythonLexer.SQ1R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
523
|
+
this.#pushLexerMode(PythonLexer.SQ1R_FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
524
|
+
break;
|
|
525
|
+
|
|
526
|
+
case PythonLexer.SQ1R_TSTRING_MODE:
|
|
527
|
+
case PythonLexer.SQ1R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
528
|
+
this.#pushLexerMode(PythonLexer.SQ1R_TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
529
|
+
break;
|
|
530
|
+
|
|
531
|
+
case PythonLexer.DQ1__FSTRING_MODE:
|
|
532
|
+
case PythonLexer.DQ1__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
533
|
+
this.#pushLexerMode(PythonLexer.DQ1__FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
534
|
+
break;
|
|
535
|
+
|
|
536
|
+
case PythonLexer.DQ1__TSTRING_MODE:
|
|
537
|
+
case PythonLexer.DQ1__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
538
|
+
this.#pushLexerMode(PythonLexer.DQ1__TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
539
|
+
break;
|
|
540
|
+
|
|
541
|
+
case PythonLexer.DQ1R_FSTRING_MODE:
|
|
542
|
+
case PythonLexer.DQ1R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
543
|
+
this.#pushLexerMode(PythonLexer.DQ1R_FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
544
|
+
break;
|
|
545
|
+
|
|
546
|
+
case PythonLexer.DQ1R_TSTRING_MODE:
|
|
547
|
+
case PythonLexer.DQ1R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
548
|
+
this.#pushLexerMode(PythonLexer.DQ1R_TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
549
|
+
break;
|
|
550
|
+
|
|
551
|
+
case PythonLexer.SQ3__FSTRING_MODE:
|
|
552
|
+
case PythonLexer.SQ3__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
553
|
+
this.#pushLexerMode(PythonLexer.SQ3__FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
554
|
+
break;
|
|
555
|
+
|
|
556
|
+
case PythonLexer.SQ3__TSTRING_MODE:
|
|
557
|
+
case PythonLexer.SQ3__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
558
|
+
this.#pushLexerMode(PythonLexer.SQ3__TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
559
|
+
break;
|
|
560
|
+
|
|
561
|
+
case PythonLexer.SQ3R_FSTRING_MODE:
|
|
562
|
+
case PythonLexer.SQ3R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
563
|
+
this.#pushLexerMode(PythonLexer.SQ3R_FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
564
|
+
break;
|
|
565
|
+
|
|
566
|
+
case PythonLexer.SQ3R_TSTRING_MODE:
|
|
567
|
+
case PythonLexer.SQ3R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
568
|
+
this.#pushLexerMode(PythonLexer.SQ3R_TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
569
|
+
break;
|
|
570
|
+
|
|
571
|
+
case PythonLexer.DQ3__FSTRING_MODE:
|
|
572
|
+
case PythonLexer.DQ3__FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
573
|
+
this.#pushLexerMode(PythonLexer.DQ3__FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
574
|
+
break;
|
|
575
|
+
|
|
576
|
+
case PythonLexer.DQ3__TSTRING_MODE:
|
|
577
|
+
case PythonLexer.DQ3__TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
578
|
+
this.#pushLexerMode(PythonLexer.DQ3__TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
579
|
+
break;
|
|
580
|
+
|
|
581
|
+
case PythonLexer.DQ3R_FSTRING_MODE:
|
|
582
|
+
case PythonLexer.DQ3R_FSTRING_FORMAT_SPECIFICATION_MODE:
|
|
583
|
+
this.#pushLexerMode(PythonLexer.DQ3R_FSTRING_FORMAT_SPECIFICATION_MODE);
|
|
584
|
+
break;
|
|
585
|
+
|
|
586
|
+
case PythonLexer.DQ3R_TSTRING_MODE:
|
|
587
|
+
case PythonLexer.DQ3R_TSTRING_FORMAT_SPECIFICATION_MODE:
|
|
588
|
+
this.#pushLexerMode(PythonLexer.DQ3R_TSTRING_FORMAT_SPECIFICATION_MODE);
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
#popByBRACE() {
|
|
594
|
+
this.#paren_or_bracket_openedStack.pop();
|
|
595
|
+
const curBraceExpression = this.#braceExpressionStack.pop();
|
|
596
|
+
this.#lastClosedBraceExpression = curBraceExpression + "}";
|
|
597
|
+
if (this.#braceExpressionStack.length > 0) {
|
|
598
|
+
// Extend the current brace expression by adding the previous expression
|
|
599
|
+
const lastIndex = this.#braceExpressionStack.length - 1;
|
|
600
|
+
this.#braceExpressionStack[lastIndex] += this.#lastClosedBraceExpression;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
#handleFSTRING_MIDDLEtokenWithDoubleBrace() { // ISTRING = interpolated string (FSTRING or TSTRING)
|
|
605
|
+
// replace the trailing double brace with a single brace and insert a hidden brace token
|
|
606
|
+
const lastTwoChars = this.#getLastTwoCharsOfTheCurTokenText();
|
|
607
|
+
switch (lastTwoChars) {
|
|
608
|
+
case "{{":
|
|
609
|
+
this.#trimLastCharAddPendingTokenSetCurToken(PythonLexer.LBRACE, "{", Token.HIDDEN_CHANNEL);
|
|
610
|
+
break;
|
|
611
|
+
case "}}":
|
|
612
|
+
this.#trimLastCharAddPendingTokenSetCurToken(PythonLexer.RBRACE, "}", Token.HIDDEN_CHANNEL);
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
#handleFSTRING_MIDDLEtokenWithQuoteAndLBrace() { // ISTRING = interpolated string (FSTRING or TSTRING)
|
|
618
|
+
// replace the trailing quote + left_brace with a quote and insert an LBRACE token
|
|
619
|
+
// replace the trailing backslash + left_brace with a backslash and insert an LBRACE token
|
|
620
|
+
const lastTwoChars = this.#getLastTwoCharsOfTheCurTokenText();
|
|
621
|
+
switch (lastTwoChars) {
|
|
622
|
+
case "\"{":
|
|
623
|
+
case "'{":
|
|
624
|
+
case "\\{":
|
|
625
|
+
this.#trimLastCharAddPendingTokenSetCurToken(PythonLexer.LBRACE, "{", Token.DEFAULT_CHANNEL);
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
#getLastTwoCharsOfTheCurTokenText() {
|
|
631
|
+
const text = this.#curToken.text;
|
|
632
|
+
return text.length <= 2 ? text : text.slice(-2);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
#trimLastCharAddPendingTokenSetCurToken(type, text, channel) {
|
|
636
|
+
// trim the last char and add the modified curToken to the pendingTokenQueue
|
|
637
|
+
const tokenTextWithoutLastChar = this.#curToken.text.slice(0, -1);
|
|
638
|
+
this.#curToken.text = tokenTextWithoutLastChar;
|
|
639
|
+
this.#curToken.stop -= 1;
|
|
640
|
+
this.#addPendingToken(this.#curToken);
|
|
641
|
+
|
|
642
|
+
this.#replaceCurrentToken(type, text, channel); // set curToken
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
#handleCOLONEQUALtokenInIString() { // ISTRING = interpolated string (FSTRING or TSTRING)
|
|
646
|
+
if (this.#lexerModeStack.length > 0 &&
|
|
647
|
+
this.#paren_or_bracket_openedStack.at(-1) === 0) { // stack peek === 0
|
|
648
|
+
|
|
649
|
+
// In an f/t-string, the walrus operator (:=) is only allowed inside parentheses.
|
|
650
|
+
// If used outside, split the COLONEQUAL token into a COLON
|
|
651
|
+
// (used as a format specifier instead of a walrus operator),
|
|
652
|
+
// and move the equal sign to the beginning of the next token (FSTRING_MIDDLE or TSTRING_MIDDLE).
|
|
653
|
+
this.#curToken.type = PythonLexer.COLON;
|
|
654
|
+
this.#curToken.text = ":";
|
|
655
|
+
this.#curToken.stop = this.#curToken.start;
|
|
656
|
+
|
|
657
|
+
switch (this.#laToken.type) {
|
|
658
|
+
case PythonLexer.FSTRING_MIDDLE:
|
|
659
|
+
case PythonLexer.TSTRING_MIDDLE: {
|
|
660
|
+
const token = this.#laToken.clone();
|
|
661
|
+
token.text = "=" + token.text;
|
|
662
|
+
token.start -= 1;
|
|
663
|
+
token.column -= 1;
|
|
664
|
+
this.#laToken = token;
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
default: {
|
|
668
|
+
this.#addPendingToken(this.#curToken);
|
|
669
|
+
this.#replaceCurrentToken(this.#activeInterpolatedStringMiddleTokenType, "=", Token.DEFAULT_CHANNEL);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
this.#addPendingToken(this.#curToken);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
#replaceCurrentToken(type, text, channel) {
|
|
677
|
+
const token = this.#curToken.clone();
|
|
678
|
+
token.type = type;
|
|
679
|
+
token.text = text;
|
|
680
|
+
token.channel = channel;
|
|
681
|
+
token.column += 1;
|
|
682
|
+
token.start += 1;
|
|
683
|
+
token.stop = token.start;
|
|
684
|
+
this.#curToken = token;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// ===================== Lexer‑Mode Management =====================
|
|
688
|
+
// Manages the custom lexer mode stack used for interpolated strings.
|
|
689
|
+
// Provides controlled push/pop operations independent of ANTLR’s
|
|
690
|
+
// internal mode stack to ensure correct behavior in complex IString flows.
|
|
691
|
+
|
|
692
|
+
#pushLexerMode(mode) {
|
|
693
|
+
this.pushMode(mode);
|
|
694
|
+
this.#lexerModeStack.push(this.#curLexerMode);
|
|
695
|
+
this.#curLexerMode = mode;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
#popLexerMode() {
|
|
699
|
+
this.popMode();
|
|
700
|
+
this.#curLexerMode = this.#lexerModeStack.pop();
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// ===================== Format‑Specifier Handling =====================
|
|
704
|
+
|
|
705
|
+
#handleFORMAT_SPECIFICATION_MODE() {
|
|
706
|
+
if (this.#lexerModeStack.length == 0 || this.#laToken.type !== PythonLexer.RBRACE) {
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// insert an empty FSTRING_MIDDLE or TSTRING_MIDDLE token instead of the missing format specification
|
|
711
|
+
switch (this.#curToken.type) {
|
|
712
|
+
case PythonLexer.COLON:
|
|
713
|
+
this.#createAndAddPendingToken(this.#activeInterpolatedStringMiddleTokenType, "", this.#laToken);
|
|
714
|
+
break;
|
|
715
|
+
case PythonLexer.RBRACE:
|
|
716
|
+
// only when the previous brace expression is not a dictionary comprehension or set comprehension
|
|
717
|
+
if (!this.#isValid_DictionaryOrSet_ComprehensionExpression(this.#lastClosedBraceExpression)) {
|
|
718
|
+
this.#createAndAddPendingToken(this.#activeInterpolatedStringMiddleTokenType, "", this.#laToken);
|
|
719
|
+
}
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// Determines whether the collected brace expression forms a dictionary or set
|
|
725
|
+
// comprehension. Used to enforce Python’s rule that outermost f-string brace
|
|
726
|
+
// expressions cannot be comprehensions.
|
|
727
|
+
#isValid_DictionaryOrSet_ComprehensionExpression(code) {
|
|
728
|
+
const inputStream = CharStreams.fromString(code);
|
|
729
|
+
const lexer = new PythonLexer(inputStream);
|
|
730
|
+
const tokenStream = new CommonTokenStream(lexer);
|
|
731
|
+
let parser = new PythonParser(tokenStream);
|
|
732
|
+
|
|
733
|
+
// Disable error listeners to suppress console output
|
|
734
|
+
lexer.removeErrorListeners();
|
|
735
|
+
parser.removeErrorListeners();
|
|
736
|
+
|
|
737
|
+
parser.dictcomp(); // Try parsing as dictionary comprehension
|
|
738
|
+
if (parser.syntaxErrorsCount === 0)
|
|
739
|
+
return true;
|
|
740
|
+
|
|
741
|
+
parser = new PythonParser(tokenStream);
|
|
742
|
+
tokenStream.seek(0);
|
|
743
|
+
parser.removeErrorListeners();
|
|
744
|
+
parser.setcomp(); // Try parsing as set comprehension
|
|
745
|
+
return parser.syntaxErrorsCount === 0;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// ===================== Trailing‑Token Finalization =====================
|
|
749
|
+
// Handles end-of-input cleanup, including emission of remaining DEDENT tokens,
|
|
750
|
+
// final NEWLINE normalization, and generation of the EOF token to properly
|
|
751
|
+
// terminate the logical token stream.
|
|
752
|
+
|
|
753
|
+
#insertTrailingTokens() {
|
|
754
|
+
switch (this.#lastPendingTokenTypeFromDefaultChannel) {
|
|
755
|
+
case PythonLexer.NEWLINE:
|
|
756
|
+
case PythonLexer.DEDENT:
|
|
757
|
+
break; // no trailing NEWLINE token is needed
|
|
758
|
+
default:
|
|
759
|
+
// insert an extra trailing NEWLINE token that serves as the end of the last statement
|
|
760
|
+
this.#createAndAddPendingToken(PythonLexer.NEWLINE, null, this.#laToken); // laToken is EOF
|
|
761
|
+
}
|
|
762
|
+
this.#insertIndentOrDedentToken(0); // Now insert as much trailing DEDENT tokens as needed
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
#handleEOFtoken() {
|
|
766
|
+
if (this.#lastPendingTokenTypeFromDefaultChannel > 0) {
|
|
767
|
+
// there was a statement in the input (leading NEWLINE tokens are hidden)
|
|
768
|
+
this.#insertTrailingTokens();
|
|
769
|
+
}
|
|
770
|
+
this.#addPendingToken(this.#curToken);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// ===================== Pending‑Token Management =====================
|
|
774
|
+
// Manages the queue of pending tokens, including emission of normal and hidden
|
|
775
|
+
// tokens, preserving correct output order for the token stream.
|
|
776
|
+
|
|
777
|
+
#hideAndAddPendingToken(originalToken) {
|
|
778
|
+
originalToken.channel = Token.HIDDEN_CHANNEL;
|
|
779
|
+
this.#addPendingToken(originalToken);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
#createAndAddPendingToken(type, text, originalToken) {
|
|
783
|
+
const token = originalToken.clone();
|
|
784
|
+
token.type = type;
|
|
785
|
+
token.channel = Token.DEFAULT_CHANNEL;
|
|
786
|
+
token.stop = originalToken.start - 1;
|
|
787
|
+
token.text = text == null ?
|
|
788
|
+
`<${PythonLexer.symbolicNames[type] ?? ""}>` :
|
|
789
|
+
text;
|
|
790
|
+
|
|
791
|
+
this.#addPendingToken(token);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
#addPendingToken(token) {
|
|
795
|
+
// save the last pending token type because the pendingTokenQueue can be empty by the nextToken()
|
|
796
|
+
this.#previousPendingTokenType = token.type;
|
|
797
|
+
if (token.channel === Token.DEFAULT_CHANNEL) {
|
|
798
|
+
this.#lastPendingTokenTypeFromDefaultChannel = this.#previousPendingTokenType;
|
|
799
|
+
}
|
|
800
|
+
this.#pendingTokenQueue.push(token) /* .addLast(token) */;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// ===================== Error Reporting & Diagnostics =====================
|
|
804
|
+
// Provides consistent lexer-level error reporting, including generation of
|
|
805
|
+
// ERRORTOKEN instances, construction of human-readable diagnostic messages,
|
|
806
|
+
// and insertion of error markers into the token stream to ensure that the
|
|
807
|
+
// parser receives accurate context for recovery.
|
|
808
|
+
|
|
809
|
+
#reportLexerError(errMsg) {
|
|
810
|
+
this.getErrorListener().syntaxError(this, this.#curToken.type, this.#curToken.line, this.#curToken.column, " LEXER" + PythonLexerBase.#ERR_TXT + errMsg, null);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
#reportError(errMsg) {
|
|
814
|
+
this.#reportLexerError(errMsg);
|
|
815
|
+
|
|
816
|
+
this.#createAndAddPendingToken(PythonLexer.ERRORTOKEN, PythonLexerBase.#ERR_TXT + errMsg, this.#laToken);
|
|
817
|
+
// the ERRORTOKEN also triggers a parser error
|
|
818
|
+
}
|
|
819
|
+
}
|