|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const { SafeSet, MathMax, StringPrototypeIncludes } = primordials; |
| 3 | +const { |
| 4 | + ArrayPrototypePop, |
| 5 | + ArrayPrototypePush, |
| 6 | + MathMax, |
| 7 | + SafeSet, |
| 8 | + StringPrototypeIncludes, |
| 9 | + StringPrototypeTrim, |
| 10 | +} = primordials; |
4 | 11 | const { |
5 | 12 | codes: { ERR_TAP_LEXER_ERROR }, |
6 | 13 | } = require('internal/errors'); |
@@ -136,21 +143,21 @@ class TapLexer { |
136 | 143 | this.#lastScannedToken = token; |
137 | 144 | } |
138 | 145 |
|
| 146 | + ArrayPrototypePush(chunk, token); |
139 | 147 | if (token.kind === TokenKind.NEWLINE) { |
140 | 148 | // Store the current chunk + NEWLINE token |
141 | | - tokens.push([...chunk, token]); |
| 149 | + ArrayPrototypePush(tokens, chunk); |
142 | 150 | chunk = []; |
143 | | - } else { |
144 | | - chunk.push(token); |
145 | 151 | } |
146 | 152 | } |
147 | 153 |
|
148 | 154 | if (chunk.length > 0) { |
149 | | - tokens.push([...chunk, this.#scanEOL()]); |
| 155 | + ArrayPrototypePush(chunk, this.#scanEOL()); |
| 156 | + ArrayPrototypePush(tokens, chunk); |
150 | 157 | } |
151 | 158 |
|
152 | 159 | // send EOF as a separate chunk |
153 | | - tokens.push([this.#scanEOF()]); |
| 160 | + ArrayPrototypePush(tokens, [this.#scanEOF()]); |
154 | 161 |
|
155 | 162 | return tokens; |
156 | 163 | } |
@@ -238,7 +245,7 @@ class TapLexer { |
238 | 245 | this.#hasTheCurrentCharacterBeenEscaped() || |
239 | 246 | this.#source.peek(1) === TokenKind.WHITESPACE |
240 | 247 | ) { |
241 | | - this.#escapeStack.pop(); |
| 248 | + ArrayPrototypePop(this.#escapeStack); |
242 | 249 | return new Token({ |
243 | 250 | kind: TokenKind.LITERAL, |
244 | 251 | value: char, |
@@ -249,7 +256,7 @@ class TapLexer { |
249 | 256 | // Otherwise, consume the escape symbol as an escape symbol that should be ignored by the parser |
250 | 257 | // we also need to push the escape symbol to the escape stack |
251 | 258 | // and consume the next character as a literal (done in the next turn) |
252 | | - this.#escapeStack.push(char); |
| 259 | + ArrayPrototypePush(this.#escapeStack, char); |
253 | 260 | return new Token({ |
254 | 261 | kind: TokenKind.ESCAPE, |
255 | 262 | value: char, |
@@ -326,7 +333,7 @@ class TapLexer { |
326 | 333 | const charHasBeenEscaped = this.#hasTheCurrentCharacterBeenEscaped(); |
327 | 334 | if (this.#isComment || charHasBeenEscaped) { |
328 | 335 | if (charHasBeenEscaped) { |
329 | | - this.#escapeStack.pop(); |
| 336 | + ArrayPrototypePop(this.#escapeStack); |
330 | 337 | } |
331 | 338 |
|
332 | 339 | return new Token({ |
@@ -355,7 +362,7 @@ class TapLexer { |
355 | 362 | } |
356 | 363 | } |
357 | 364 |
|
358 | | - word = word.trim(); |
| 365 | + word = StringPrototypeTrim(word); |
359 | 366 |
|
360 | 367 | if (TapLexer.Keywords.has(word)) { |
361 | 368 | const token = this.#scanTAPKeyword(word); |
@@ -380,10 +387,9 @@ class TapLexer { |
380 | 387 | } |
381 | 388 |
|
382 | 389 | #scanTAPKeyword(word) { |
383 | | - const isLastScannedTokenEOLorNewLine = StringPrototypeIncludes( |
384 | | - [TokenKind.EOL, TokenKind.NEWLINE], |
385 | | - this.#lastScannedToken.kind |
386 | | - ); |
| 390 | + const isLastScannedTokenEOLorNewLine = |
| 391 | + TokenKind.EOL === this.#lastScannedToken.kind || |
| 392 | + TokenKind.NEWLINE === this.#lastScannedToken.kind; |
387 | 393 |
|
388 | 394 | if (word === 'TAP' && isLastScannedTokenEOLorNewLine) { |
389 | 395 | return new Token({ |
@@ -479,7 +485,7 @@ class TapLexer { |
479 | 485 | // We deliberately do not include "# \ + -"" in this list |
480 | 486 | // these are used for comments/reasons explanations, pragma and escape characters |
481 | 487 | // whitespace is not included because it is handled separately |
482 | | - return '!"$%&\'()*,./:;<=>?@[]^_`{|}~'.indexOf(char) > -1; |
| 488 | + return StringPrototypeIncludes('!"$%&\'()*,./:;<=>?@[]^_`{|}~', char); |
483 | 489 | } |
484 | 490 |
|
485 | 491 | #isWhitespaceSymbol(char) { |
|