Skip to content

Commit 44f9af7

Browse files
committed
cleanup
1 parent 6851145 commit 44f9af7

2 files changed

Lines changed: 17 additions & 41 deletions

File tree

src/preprocessor/index.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,13 @@ import {
44
preprocessComments,
55
PreprocessorOptions,
66
visitPreprocessedAst,
7-
initExpressionParser,
87
} from './preprocessor.js';
9-
import { formatError, GlslSyntaxError } from '../error.js';
10-
import type { SyntaxError } from '../error.js';
8+
import { formatError } from '../error.js';
119

1210
// This index file is currently only for package publishing, where the whole
1311
// library exists in the dist/ folder, so the below import is relative to dist/
1412
import * as parser from './preprocessor-parser.js';
1513

16-
// Register the constant_expression parser so preprocessor.ts can parse #if
17-
// expression strings after macro expansion.
18-
initExpressionParser((src) => {
19-
try {
20-
return (parser.parse as any)(src, {
21-
grammarSource: 'expression',
22-
startRule: 'constant_expression',
23-
});
24-
} catch (e) {
25-
if (e instanceof parser.SyntaxError) {
26-
throw new GlslSyntaxError(src, 'expression', e as SyntaxError);
27-
}
28-
throw e;
29-
}
30-
});
31-
3214
/**
3315
* This is the main entry point for the preprocessor. It parses the source
3416
* code and returns an AST. It protects the user from the horrific peggy

src/preprocessor/preprocessor.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ import {
33
PreprocessorAstNode,
44
PreprocessorElseIfNode,
55
PreprocessorIdentifierNode,
6-
PreprocessorIfNode,
76
PreprocessorLiteralNode,
87
PreprocessorSegmentNode,
98
} from './preprocessor-node.js';
109
import generate from './generator.js';
1110

11+
import * as parser from './preprocessor-parser.js';
12+
import { formatError } from '../error.js';
13+
14+
// Register the constant_expression parser so preprocessor.ts can parse #if
15+
// expression strings after macro expansion.
16+
const expressionParser = (src: string): PreprocessorAstNode =>
17+
formatError(parser)(src, {
18+
grammarSource: 'expression',
19+
startRule: 'constant_expression',
20+
});
21+
1222
export type PreprocessorProgram = {
1323
type: string;
1424
program: PreprocessorSegmentNode[];
@@ -479,40 +489,24 @@ export type PreprocessorOptions = {
479489
grammarSource?: string;
480490
};
481491

482-
// Parser for constant_expression strings, registered by index.ts at startup.
483-
// Must be set before preprocessAst() is called on any program that uses #if.
484-
let _exprParser: ((src: string) => PreprocessorAstNode) | undefined;
485-
export const initExpressionParser = (
486-
fn: (src: string) => PreprocessorAstNode
487-
) => {
488-
_exprParser = fn;
489-
};
490-
491492
// Evaluate a raw #if / #elif expression string against the current macros.
492493
// Handles defined() before macro expansion (spec-correct order), then
493494
// parses the result as a constant_expression AST and evaluates it.
494495
const evaluateExpressionString = (expr: string, macros: Macros): any => {
495-
if (!_exprParser) {
496-
throw new Error(
497-
'No expression parser registered. Import from preprocessor/index.js, or call initExpressionParser() first.'
498-
);
499-
}
500496
// Strip inline comments so "defined/**/A" is treated as "defined A"
501497
const stripped = preprocessComments(expr);
502498
// Step 1: evaluate defined(X) / defined X before any macro expansion
503499
const withDefined = stripped
504-
.replace(
505-
/defined\s*\(\s*([A-Za-z_][A-Za-z_0-9]*)\s*\)/g,
506-
(_, id) => (id in macros ? '1' : '0')
500+
.replace(/defined\s*\(\s*([A-Za-z_][A-Za-z_0-9]*)\s*\)/g, (_, id) =>
501+
id in macros ? '1' : '0'
507502
)
508-
.replace(
509-
/defined\s+([A-Za-z_][A-Za-z_0-9]*)/g,
510-
(_, id) => (id in macros ? '1' : '0')
503+
.replace(/defined\s+([A-Za-z_][A-Za-z_0-9]*)/g, (_, id) =>
504+
id in macros ? '1' : '0'
511505
);
512506
// Step 2: expand all macros in the resulting string
513507
const expanded = expandMacros(withDefined, macros);
514508
// Step 3: parse the expanded string as a constant_expression and evaluate
515-
return evaluteExpression(_exprParser(expanded.trim()), macros);
509+
return evaluteExpression(expressionParser(expanded.trim()), macros);
516510
};
517511

518512
// Remove escaped newlines, rather than try to handle them in the grammar

0 commit comments

Comments
 (0)