@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.
128 lines • 4.21 kB
TypeScript
/**
* Represents a template file with its raw content before processing.
*/
export interface TemplateBase {
readonly filePath: string;
}
export type TemplatePass0 = TemplateBase & {
/**
* The raw content of the template file, split into lines.
*/
readonly raw: readonly string[];
};
export interface ParsedLine {
line: string;
codeReference: {
filePath: string;
lineNumber: number;
};
}
export type TemplatePass1 = TemplatePass0 & {
/**
* The content after pass 1 processing, including:
* - comments removal
* - #include expansion
* - #define expansion
*/
readonly pass1: readonly ParsedLine[];
};
export type TemplatePass2 = TemplateBase & {
/**
* The content after pass 2 processing, including:
* - codegen
*/
readonly generateResult: GenerateResult;
};
/**
* Represents the final content after all processing.
* The file content as a string.
*/
export type TemplateBuildResult = string;
export interface TemplateRepository<T> {
/**
* The base path of the template files in the repository.
*/
readonly basePath: string;
readonly templates: ReadonlyMap<string, T>;
}
export interface CodeSegment {
type: "raw" | "code" | "expression";
content: string;
}
export interface CodeSegmentArg {
type: CodePatternArgType;
code: CodeSegment[];
}
export interface CodeGenerator {
emit(code: CodeSegment[]): string;
param(name: string): string;
variable(name: string): string;
property(obj: string, propertyName: string): string;
function(name: string, args: CodeSegmentArg[]): string;
method(obj: string, methodName: string, args: CodeSegmentArg[]): string;
}
/**
* Defines the type of code pattern.
*/
export type CodePatternType = "control" | "param" | "variable" | "function" | "method" | "property";
export type CodePatternVariableType = "shader-variable";
export type CodePatternParamType = "int";
/**
* Defines the type of argument for a function or method.
*
* "expression" - The argument is an expression. Willbe emitted as is.
* "string" - The argument is a string. Will be emitted as a string literal.
*/
export type CodePatternArgType = "expression" | "string" | "auto";
export interface CodePattern {
readonly type: CodePatternType;
readonly pattern: string | RegExp;
readonly replace?: string | (string | null)[];
readonly variableType?: CodePatternVariableType;
readonly paramType?: CodePatternParamType;
readonly argTypes?: CodePatternArgType[];
}
export interface GenerateOptions {
preserveCodeReference?: boolean;
}
export interface GenerateResult {
code: string;
params: Map<string, NonNullable<CodePattern["paramType"]>>;
variables: Map<string, NonNullable<CodePattern["variableType"]>>;
hasMainFunction: boolean;
}
export interface Generator {
generate(filePath: string, repo: TemplateRepository<TemplatePass1>, generator: CodeGenerator, options?: GenerateOptions): GenerateResult;
generateDirectory(repo: TemplateRepository<TemplatePass1>, generator: CodeGenerator, options?: GenerateOptions): TemplateRepository<TemplatePass2>;
}
export interface LoadFromDirectoryOptions {
/**
* The file extension to look for when loading files.
* Defaults to '.wgsl.template'.
*/
ext?: string;
}
export interface Loader {
loadFromDirectory(directory: string, options?: LoadFromDirectoryOptions): Promise<TemplateRepository<TemplatePass0>>;
loadFromDirectories(directories: ({
path: string;
alias?: string;
} | string)[], options?: LoadFromDirectoryOptions): Promise<TemplateRepository<TemplatePass0>>;
}
export interface Parser {
parse(repo: TemplateRepository<TemplatePass0>): TemplateRepository<TemplatePass1>;
}
export interface SourceBuilderOptions {
/**
* The extension of template files.
*/
templateExt: string;
/**
* The prefix to use for include paths.
*/
includePathPrefix?: string;
}
export interface SourceBuilder {
build(repo: TemplateRepository<TemplatePass2>, options: SourceBuilderOptions): TemplateRepository<TemplateBuildResult>;
}
//# sourceMappingURL=types.d.ts.map