@fs-eire/wgsl-template
Version:
A powerful template system for generating WGSL (WebGPU Shading Language) code with support for parameters, conditionals, and multiple output formats including C++ code generation.
159 lines • 4.59 kB
JavaScript
//
// The default patterns for code generation that are automatically included
//
export const DEFAULT_PATTERNS = [
{ type: "control", pattern: /\(/ },
{ type: "control", pattern: /\)/ },
{ type: "control", pattern: /,/ },
{ type: "control", pattern: /{/ },
{ type: "control", pattern: /}/ },
{ type: "control", pattern: /(?<![a-zA-Z0-9_])(\$MAIN\s*\{)/ },
];
//
// Built-in patterns for common code transformations
//
const BUILT_IN_PATTERNS = [
[
"guardAgainstOutOfBoundsWorkgroupSizes",
{
type: "function",
pattern: /\b(guardAgainstOutOfBoundsWorkgroupSizes)\s*\(/d,
replace: ["shader_helper.GuardAgainstOutOfBoundsWorkgroupSizes"],
argTypes: ["string"],
},
],
[
"getElementAt",
{
type: "function",
pattern: /\b(getElementAt)\s*\(/d,
replace: ["GetElementAt"],
argTypes: ["string", "auto", "expression", "expression"],
},
],
];
//
// Indices Helper Patterns
//
export const INDICES_HELPER_PATTERNS = [
[
".rank",
{
type: "property",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(rank)\b/d,
replace: [null, "Rank()"],
},
],
[
".numComponents",
{
type: "property",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(numComponents)\b/d,
replace: [null, "NumComponents()"],
},
],
[
".offsetToIndices",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(offsetToIndices)\s*\(/d,
replace: [null, "OffsetToIndices"],
argTypes: ["string"],
},
],
[
".indicesToOffset",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(indicesToOffset)\s*\(/d,
replace: [null, "IndicesToOffset"],
argTypes: ["string"],
},
],
[
".indicesSet",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(indicesSet)\s*\(/d,
replace: [null, "IndicesSet"],
argTypes: ["string", "auto", "auto"],
},
],
[
".indicesGet",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(indicesGet)\s*\(/d,
replace: [null, "IndicesGet"],
argTypes: ["string", "auto"],
},
],
[
".set",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(set)\s*\(/d,
replace: [null, "Set"],
},
],
[
".setByOffset",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(setByOffset)\s*\(/d,
replace: [null, "SetByOffset"],
},
],
[
".setByIndices",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(setByIndices)\s*\(/d,
replace: [null, "SetByIndices"],
argTypes: ["string", "string"],
},
],
[
".get",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(get)\s*\(/d,
replace: [null, "Get"],
},
],
[
".getByOffset",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(getByOffset)\s*\(/d,
replace: [null, "GetByOffset"],
},
],
[
".getByIndices",
{
type: "method",
pattern: /\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(getByIndices)\s*\(/d,
replace: [null, "GetByIndices"],
argTypes: ["string"],
},
],
];
const PATTERN_MAP = new Map([
...BUILT_IN_PATTERNS,
...INDICES_HELPER_PATTERNS,
//TODO: add more patterns as needed
]);
export function lookupPattern(name) {
return PATTERN_MAP.get(name);
}
export function createParamPattern(name) {
// Validate that the parameter name is a valid identifier
// Valid identifiers: start with letter or underscore, followed by letters, digits, or underscores
const identifierPattern = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
if (!identifierPattern.test(name)) {
throw new Error(`Invalid parameter identifier: "${name}". Parameter names must start with a letter or underscore and contain only letters, digits, and underscores.`);
}
return { type: "param", pattern: `\\b${name}\\b` };
}
//# sourceMappingURL=code-pattern-impl.js.map