Skip to content

Commit 5cd1712

Browse files
committed
refactor(lexer): store constants as properties in Lexer class
1 parent 462ce66 commit 5cd1712

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/lexer/lexer.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ import { TrieNode } from "../utils/utils.js";
1313

1414
/* Lexer */
1515
export class Lexer {
16+
/* Properties */
1617
private readonly code: string;
1718
private readonly tokens: Token.Token[];
1819
private curPos: number;
1920
private curChar: string;
2021

22+
/* Properties for constants */
23+
public CONSTANT_KEYWORDS = CONSTANT_KEYWORDS;
24+
public KEYWORDS = KEYWORDS;
25+
public OPERATOR_KEYWORDS = OPERATOR_KEYWORDS;
26+
public OPERATOR_TRIE = OPERATOR_TRIE;
27+
public SPECIAL_CHARS_MAP = SPECIAL_CHARS_MAP;
28+
public TOKENIZER_ESCAPED_CHARACTER_CONVERSIONS =
29+
TOKENIZER_ESCAPED_CHARACTER_CONVERSIONS;
30+
public VALID_CHARACTERS = VALID_CHARACTERS;
31+
2132
/* Constructor */
2233
constructor(code: string) {
2334
this.code = code;
@@ -49,7 +60,7 @@ export class Lexer {
4960
}
5061
private throwUnexpectedCharacterError(expected: string): never {
5162
const convertedCurrentChar =
52-
SPECIAL_CHARS_MAP[this.curChar] ?? this.curChar;
63+
this.SPECIAL_CHARS_MAP[this.curChar] ?? this.curChar;
5364
Lexer.throwError(
5465
`Unexpected character '${convertedCurrentChar}', expected '${expected}'`,
5566
);
@@ -132,7 +143,7 @@ export class Lexer {
132143
// Checks if the next character sequence is an operator,
133144
// if so, consumes it and returns the operator string.
134145
private consumeOperatorIfPossible(): string | false {
135-
let node: TrieNode = OPERATOR_TRIE;
146+
let node: TrieNode = this.OPERATOR_TRIE;
136147
let operator: string | undefined;
137148

138149
const currentCharPos = this.curPos;
@@ -255,7 +266,7 @@ export class Lexer {
255266
if (this.curChar === "\\") {
256267
this.advance(1); // Skip the "\"
257268
const escapedChar =
258-
TOKENIZER_ESCAPED_CHARACTER_CONVERSIONS[this.curChar];
269+
this.TOKENIZER_ESCAPED_CHARACTER_CONVERSIONS[this.curChar];
259270
if (escapedChar) {
260271
string += escapedChar;
261272
this.advance(1); // Skip the escaped character
@@ -305,11 +316,11 @@ export class Lexer {
305316
this.consumeWhitespace();
306317
} else if (Lexer.isIdentifierStart(currentChar)) {
307318
const identifier = this.consumeIdentifier();
308-
if (OPERATOR_KEYWORDS.has(identifier)) {
319+
if (this.OPERATOR_KEYWORDS.has(identifier)) {
309320
this.tokens.push(new Token.OperatorToken(identifier));
310-
} else if (CONSTANT_KEYWORDS.has(identifier)) {
321+
} else if (this.CONSTANT_KEYWORDS.has(identifier)) {
311322
this.tokens.push(new Token.ConstantToken(identifier));
312-
} else if (KEYWORDS.has(identifier)) {
323+
} else if (this.KEYWORDS.has(identifier)) {
313324
this.tokens.push(new Token.KeywordToken(identifier));
314325
} else {
315326
this.tokens.push(new Token.IdentifierToken(identifier));
@@ -338,7 +349,7 @@ export class Lexer {
338349
if (operator) {
339350
this.tokens.push(new Token.OperatorToken(operator));
340351
} else {
341-
if (!VALID_CHARACTERS.has(currentChar)) {
352+
if (!this.VALID_CHARACTERS.has(currentChar)) {
342353
Lexer.throwError(`Invalid character: ${currentChar}`);
343354
}
344355
// Process it as character

0 commit comments

Comments
 (0)