Skip to content

Commit 3f3bbee

Browse files
chore: cleanup
1 parent 4182b4d commit 3f3bbee

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/parser.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,18 @@ const TYPE_REGEX = /^(void|bool|float|u?int|[uib]?vec\d|mat\d(x\d)?)$/
123123
const QUALIFIER_REGEX = /^(const|uniform|in|out|inout|centroid|flat|smooth|invariant|lowp|mediump|highp)$/
124124
const VARIABLE_REGEX = new RegExp(`${TYPE_REGEX.source}|${QUALIFIER_REGEX.source}|layout`)
125125

126-
const isDeclaration = /* @__PURE__ */ RegExp.prototype.test.bind(VARIABLE_REGEX)
127-
128-
const isOpen = /* @__PURE__ */ RegExp.prototype.test.bind(/^[\(\[\{]$/)
129-
const isClose = /* @__PURE__ */ RegExp.prototype.test.bind(/^[\)\]\}]$/)
130-
126+
const SCOPE_DELTAS: Record<string, number> = {
127+
// Open
128+
'(': 1,
129+
'[': 1,
130+
'{': 1,
131+
// Close
132+
')': -1,
133+
']': -1,
134+
'}': -1,
135+
}
131136
function getScopeDelta(token: Token): number {
132-
if (isOpen(token.value)) return 1
133-
if (isClose(token.value)) return -1
134-
return 0
137+
return SCOPE_DELTAS[token.value] ?? 0
135138
}
136139

137140
function consume(tokens: Token[], expected?: string): Token {
@@ -616,7 +619,7 @@ function parseStatements(tokens: Token[]): Statement[] {
616619
else if (token.value === 'do') statement = parseDoWhile(tokens)
617620
else if (token.value === 'switch') statement = parseSwitch(tokens)
618621
else if (token.value === 'precision') statement = parsePrecision(tokens)
619-
else if (isDeclaration(token.value) && tokens[1].value !== '[') statement = parseIndeterminate(tokens)
622+
else if (VARIABLE_REGEX.test(token.value) && tokens[1].value !== '[') statement = parseIndeterminate(tokens)
620623
else {
621624
const expression = parseExpression(tokens)
622625
consume(tokens, ';')

src/tokenizer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export interface Token<T = TokenType, V = string> {
88
}
99

1010
// Checks for WGSL-specific `fn foo(`, `var bar =`, `let baz =`, `const qux =`
11-
const isWGSL = /* @__PURE__ */ RegExp.prototype.test.bind(/\bfn\s+\w+\s*\(|\b(var|let|const)\s+\w+\s*[:=]/)
11+
const WGSL_REGEX = /\bfn\s+\w+\s*\(|\b(var|let|const)\s+\w+\s*[:=]/
1212

13-
const isFloat = /* @__PURE__ */ RegExp.prototype.test.bind(/^(\d+\.\d*|\d*\.\d+)([eEpP][-+]?\d+)?[fFhH]?$/)
14-
const isInt = /* @__PURE__ */ RegExp.prototype.test.bind(/^(0[xX][\w\d]+|\d+)[iIuU]?$/)
15-
const isBool = /* @__PURE__ */ RegExp.prototype.test.bind(/^(true|false)$/)
13+
const FLOAT_REGEX = /^(\d+\.\d*|\d*\.\d+)([eEpP][-+]?\d+)?[fFhH]?$/
14+
const INT_REGEX = /^(0[xX][\w\d]+|\d+)[iIuU]?$/
15+
const BOOL_REGEX = /^(true|false)$/
1616

1717
const ZERO = 48
1818
const NINE = 57
@@ -43,7 +43,7 @@ export function tokenize(code: string, index: number = 0): Token[] {
4343
const tokens: Token[] = []
4444

4545
let prev: number = -1
46-
const [KEYWORDS, SYMBOLS] = isWGSL(code) ? [WGSL_KEYWORDS, WGSL_SYMBOLS] : [GLSL_KEYWORDS, GLSL_SYMBOLS]
46+
const [KEYWORDS, SYMBOLS] = WGSL_REGEX.test(code) ? [WGSL_KEYWORDS, WGSL_SYMBOLS] : [GLSL_KEYWORDS, GLSL_SYMBOLS]
4747
while (index < code.length) {
4848
let value = code[index]
4949
const char = code.charCodeAt(index++)
@@ -52,12 +52,12 @@ export function tokenize(code: string, index: number = 0): Token[] {
5252
while (isSpace(code.charCodeAt(index))) value += code[index++]
5353
tokens.push({ type: 'whitespace', value })
5454
} else if (isDigit(char) || (char === DOT && isDigit(code.charCodeAt(index)))) {
55-
while (isFloat(value + code[index]) || isInt(value + code[index])) value += code[index++]
56-
if (isFloat(value)) tokens.push({ type: 'float', value })
55+
while (FLOAT_REGEX.test(value + code[index]) || INT_REGEX.test(value + code[index])) value += code[index++]
56+
if (FLOAT_REGEX.test(value)) tokens.push({ type: 'float', value })
5757
else tokens.push({ type: 'int', value })
5858
} else if (isIdent(char)) {
5959
while (isIdent(code.charCodeAt(index))) value += code[index++]
60-
if (isBool(value)) tokens.push({ type: 'bool', value })
60+
if (BOOL_REGEX.test(value)) tokens.push({ type: 'bool', value })
6161
else if (KEYWORDS.includes(isMacro(prev) ? String.fromCharCode(prev) + value : value))
6262
tokens.push({ type: 'keyword', value })
6363
else tokens.push({ type: 'identifier', value })

0 commit comments

Comments
 (0)