Skip to content

Commit 99588ed

Browse files
chore(tokenizer): cleanup
1 parent 099975a commit 99588ed

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/tokenizer.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,51 +58,47 @@ export function tokenize(code: string, index: number = 0): Token[] {
5858

5959
const tokens: Token[] = []
6060

61-
let value: string
62-
let prev: number = -1
61+
let value: string | undefined
62+
let start: number
6363
let char: number
6464

6565
while (index < code.length) {
66+
start = index
6667
char = code.charCodeAt(index++)
6768

6869
if (isSpace(char)) {
69-
const start = index - 1
7070
while (isSpace(code.charCodeAt(index))) index++
7171
value = code.slice(start, index)
7272
tokens.push({ type: 'whitespace', value })
7373
} else if (isDigit(char) || (char === DOT && isDigit(code.charCodeAt(index)))) {
74-
if ((value = matchAsPrefix(FLOAT_REGEX, code, index - 1)!)) {
74+
if ((value = matchAsPrefix(FLOAT_REGEX, code, start))) {
7575
index = FLOAT_REGEX.lastIndex
7676
tokens.push({ type: 'float', value })
77-
} else if ((value = matchAsPrefix(INT_REGEX, code, index - 1)!)) {
77+
} else if ((value = matchAsPrefix(INT_REGEX, code, start))) {
7878
index = INT_REGEX.lastIndex
7979
tokens.push({ type: 'int', value })
8080
}
8181
} else if (isIdent(char)) {
82-
const start = index - 1
8382
while (isIdent(code.charCodeAt(index))) index++
8483
value = code.slice(start, index)
8584
if (BOOL_REGEX.test(value)) tokens.push({ type: 'bool', value })
86-
else if (KEYWORDS_LIST.has(isMacro(prev) ? String.fromCharCode(prev) + value : value))
85+
else if (KEYWORDS_LIST.has(isMacro(code.charCodeAt(start - 1)) ? code[start - 1] + value : value))
8786
tokens.push({ type: 'keyword', value })
8887
else tokens.push({ type: 'identifier', value })
8988
} else if (char === SLASH && code.charCodeAt(index) === SLASH) {
90-
const start = index - 1
9189
while (index < code.length && code.charCodeAt(index) !== LF) index++
9290
value = code.slice(start, index)
9391
index++ // consume LF
9492
tokens.push({ type: 'comment', value })
9593
} else if (char === SLASH && code.charCodeAt(index) === STAR) {
96-
const start = index - 1
9794
while (index < code.length && (code.charCodeAt(index - 2) !== STAR || code.charCodeAt(index - 1) !== SLASH))
9895
index++
9996
value = code.slice(start, index)
10097
tokens.push({ type: 'comment', value })
101-
} else if ((value = matchAsPrefix(SYMBOLS_REGEX, code, index - 1)!)) {
98+
} else if ((value = matchAsPrefix(SYMBOLS_REGEX, code, start))) {
10299
index += value.length - 1
103100
tokens.push({ type: 'symbol', value })
104101
}
105-
prev = char
106102
}
107103

108104
return tokens

0 commit comments

Comments
 (0)