wesl 0.6.0-pre10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +31 -0
  2. package/dist/index.js +4468 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/minified.js +3426 -0
  5. package/dist/minified.js.map +1 -0
  6. package/dist/tools/packages/wesl/src/AbstractElems.d.ts +322 -0
  7. package/dist/tools/packages/wesl/src/Assertions.d.ts +27 -0
  8. package/dist/tools/packages/wesl/src/BindIdents.d.ts +70 -0
  9. package/dist/tools/packages/wesl/src/Conditions.d.ts +6 -0
  10. package/dist/tools/packages/wesl/src/FlattenTreeImport.d.ts +11 -0
  11. package/dist/tools/packages/wesl/src/LinkedWesl.d.ts +50 -0
  12. package/dist/tools/packages/wesl/src/Linker.d.ts +87 -0
  13. package/dist/tools/packages/wesl/src/LinkerUtil.d.ts +3 -0
  14. package/dist/tools/packages/wesl/src/LiveDeclarations.d.ts +12 -0
  15. package/dist/tools/packages/wesl/src/LowerAndEmit.d.ts +31 -0
  16. package/dist/tools/packages/wesl/src/Mangler.d.ts +39 -0
  17. package/dist/tools/packages/wesl/src/ParseWESL.d.ts +60 -0
  18. package/dist/tools/packages/wesl/src/ParsedRegistry.d.ts +29 -0
  19. package/dist/tools/packages/wesl/src/PathUtil.d.ts +6 -0
  20. package/dist/tools/packages/wesl/src/RawEmit.d.ts +6 -0
  21. package/dist/tools/packages/wesl/src/Reflection.d.ts +45 -0
  22. package/dist/tools/packages/wesl/src/Scope.d.ts +81 -0
  23. package/dist/tools/packages/wesl/src/StandardTypes.d.ts +13 -0
  24. package/dist/tools/packages/wesl/src/TransformBindingStructs.d.ts +52 -0
  25. package/dist/tools/packages/wesl/src/Util.d.ts +43 -0
  26. package/dist/tools/packages/wesl/src/WESLCollect.d.ts +94 -0
  27. package/dist/tools/packages/wesl/src/WeslBundle.d.ts +13 -0
  28. package/dist/tools/packages/wesl/src/WeslDevice.d.ts +25 -0
  29. package/dist/tools/packages/wesl/src/debug/ASTtoString.d.ts +5 -0
  30. package/dist/tools/packages/wesl/src/debug/ImportToString.d.ts +2 -0
  31. package/dist/tools/packages/wesl/src/debug/LineWrapper.d.ts +21 -0
  32. package/dist/tools/packages/wesl/src/debug/ScopeToString.d.ts +6 -0
  33. package/dist/tools/packages/wesl/src/index.d.ts +11 -0
  34. package/dist/tools/packages/wesl/src/parse/ImportGrammar.d.ts +5 -0
  35. package/dist/tools/packages/wesl/src/parse/Keywords.d.ts +4 -0
  36. package/dist/tools/packages/wesl/src/parse/WeslBaseGrammar.d.ts +5 -0
  37. package/dist/tools/packages/wesl/src/parse/WeslExpression.d.ts +13 -0
  38. package/dist/tools/packages/wesl/src/parse/WeslGrammar.d.ts +80 -0
  39. package/dist/tools/packages/wesl/src/parse/WeslStream.d.ts +44 -0
  40. package/dist/tools/packages/wesl/src/test/BindWESL.test.d.ts +1 -0
  41. package/dist/tools/packages/wesl/src/test/ConditionLinking.test.d.ts +1 -0
  42. package/dist/tools/packages/wesl/src/test/ConditionalTranslationCases.test.d.ts +1 -0
  43. package/dist/tools/packages/wesl/src/test/ErrorLogging.test.d.ts +1 -0
  44. package/dist/tools/packages/wesl/src/test/Expression.test.d.ts +1 -0
  45. package/dist/tools/packages/wesl/src/test/FlattenTreeImport.test.d.ts +1 -0
  46. package/dist/tools/packages/wesl/src/test/ImportCases.test.d.ts +1 -0
  47. package/dist/tools/packages/wesl/src/test/ImportSyntaxCases.test.d.ts +1 -0
  48. package/dist/tools/packages/wesl/src/test/LinkGlob.test.d.ts +1 -0
  49. package/dist/tools/packages/wesl/src/test/LinkPackage.test.d.ts +1 -0
  50. package/dist/tools/packages/wesl/src/test/Linker.test.d.ts +1 -0
  51. package/dist/tools/packages/wesl/src/test/Mangling.test.d.ts +1 -0
  52. package/dist/tools/packages/wesl/src/test/ParseComments.test.d.ts +1 -0
  53. package/dist/tools/packages/wesl/src/test/ParseConditions.test.d.ts +1 -0
  54. package/dist/tools/packages/wesl/src/test/ParseError.test.d.ts +1 -0
  55. package/dist/tools/packages/wesl/src/test/ParseWESL.test.d.ts +1 -0
  56. package/dist/tools/packages/wesl/src/test/PathUtil.test.d.ts +1 -0
  57. package/dist/tools/packages/wesl/src/test/PrettyGrammar.test.d.ts +1 -0
  58. package/dist/tools/packages/wesl/src/test/Reflection.test.d.ts +1 -0
  59. package/dist/tools/packages/wesl/src/test/ScopeWESL.test.d.ts +1 -0
  60. package/dist/tools/packages/wesl/src/test/TestLink.d.ts +21 -0
  61. package/dist/tools/packages/wesl/src/test/TestSetup.d.ts +1 -0
  62. package/dist/tools/packages/wesl/src/test/TestUtil.d.ts +40 -0
  63. package/dist/tools/packages/wesl/src/test/Tokenizer.test.d.ts +1 -0
  64. package/dist/tools/packages/wesl/src/test/TransformBindingStructs.test.d.ts +1 -0
  65. package/dist/tools/packages/wesl/src/test/Util.test.d.ts +1 -0
  66. package/dist/tools/packages/wesl/src/test/VirtualModules.test.d.ts +1 -0
  67. package/dist/tools/packages/wesl/src/test/WeslDevice.test.d.ts +1 -0
  68. package/dist/tools/packages/wesl/src/test/WgslTests.d.ts +0 -0
  69. package/dist/tools/packages/wesl/src/vlq/vlq.d.ts +11 -0
  70. package/package.json +46 -0
  71. package/src/AbstractElems.ts +446 -0
  72. package/src/Assertions.ts +51 -0
  73. package/src/BindIdents.ts +523 -0
  74. package/src/Conditions.ts +74 -0
  75. package/src/FlattenTreeImport.ts +55 -0
  76. package/src/LinkedWesl.ts +184 -0
  77. package/src/Linker.ts +284 -0
  78. package/src/LinkerUtil.ts +29 -0
  79. package/src/LiveDeclarations.ts +31 -0
  80. package/src/LowerAndEmit.ts +413 -0
  81. package/src/Mangler.ts +94 -0
  82. package/src/ParseWESL.ts +157 -0
  83. package/src/ParsedRegistry.ts +120 -0
  84. package/src/PathUtil.ts +31 -0
  85. package/src/RawEmit.ts +102 -0
  86. package/src/Reflection.ts +334 -0
  87. package/src/Scope.ts +162 -0
  88. package/src/StandardTypes.ts +97 -0
  89. package/src/TransformBindingStructs.ts +319 -0
  90. package/src/Util.ts +194 -0
  91. package/src/WESLCollect.ts +614 -0
  92. package/src/WeslBundle.ts +16 -0
  93. package/src/WeslDevice.ts +209 -0
  94. package/src/debug/ASTtoString.ts +290 -0
  95. package/src/debug/ImportToString.ts +29 -0
  96. package/src/debug/LineWrapper.ts +70 -0
  97. package/src/debug/ScopeToString.ts +79 -0
  98. package/src/index.ts +11 -0
  99. package/src/parse/ImportGrammar.ts +157 -0
  100. package/src/parse/Keywords.ts +26 -0
  101. package/src/parse/WeslBaseGrammar.ts +8 -0
  102. package/src/parse/WeslExpression.ts +207 -0
  103. package/src/parse/WeslGrammar.ts +856 -0
  104. package/src/parse/WeslStream.ts +279 -0
  105. package/src/test/BindWESL.test.ts +57 -0
  106. package/src/test/ConditionLinking.test.ts +91 -0
  107. package/src/test/ConditionalTranslationCases.test.ts +56 -0
  108. package/src/test/ErrorLogging.test.ts +30 -0
  109. package/src/test/Expression.test.ts +22 -0
  110. package/src/test/FlattenTreeImport.test.ts +74 -0
  111. package/src/test/ImportCases.test.ts +56 -0
  112. package/src/test/ImportSyntaxCases.test.ts +24 -0
  113. package/src/test/LinkGlob.test.ts +25 -0
  114. package/src/test/LinkPackage.test.ts +26 -0
  115. package/src/test/Linker.test.ts +125 -0
  116. package/src/test/Mangling.test.ts +45 -0
  117. package/src/test/ParseComments.test.ts +36 -0
  118. package/src/test/ParseConditions.test.ts +183 -0
  119. package/src/test/ParseError.test.ts +36 -0
  120. package/src/test/ParseWESL.test.ts +1572 -0
  121. package/src/test/PathUtil.test.ts +34 -0
  122. package/src/test/PrettyGrammar.test.ts +20 -0
  123. package/src/test/Reflection.test.ts +172 -0
  124. package/src/test/ScopeWESL.test.ts +462 -0
  125. package/src/test/TestLink.ts +82 -0
  126. package/src/test/TestSetup.ts +4 -0
  127. package/src/test/TestUtil.ts +126 -0
  128. package/src/test/Tokenizer.test.ts +135 -0
  129. package/src/test/TransformBindingStructs.test.ts +230 -0
  130. package/src/test/Util.test.ts +22 -0
  131. package/src/test/VirtualModules.test.ts +37 -0
  132. package/src/test/WeslDevice.test.ts +265 -0
  133. package/src/test/WgslTests.ts +0 -0
  134. package/src/test/__snapshots__/ParseDirectives.test.ts.snap +25 -0
  135. package/src/test/__snapshots__/ParseWESL.test.ts.snap +119 -0
  136. package/src/test/__snapshots__/RustDirective.test.ts.snap +359 -0
  137. package/src/test/wgsl_1/main.wgsl +3 -0
  138. package/src/test/wgsl_1/util.wgsl +1 -0
  139. package/src/test/wgsl_2/main2.wgsl +3 -0
  140. package/src/test/wgsl_2/util2.wgsl +1 -0
  141. package/src/vlq/vlq.ts +94 -0
@@ -0,0 +1,60 @@
1
+ import { AppState, ParseError, SrcMap } from '../../mini-parse/src';
2
+ import { ConstAssertElem, ImportStatement, ModuleElem } from './AbstractElems.ts';
3
+ import { FlatImport } from './FlattenTreeImport.ts';
4
+ import { Scope, SrcModule } from './Scope.ts';
5
+ import { OpenElem } from './WESLCollect.ts';
6
+ /** result of a parse for one wesl module (e.g. one .wesl file)
7
+ *
8
+ * The parser constructs the AST constructed into three sections
9
+ * for convenient access by the binding stage.
10
+ * - import statements
11
+ * - language elements (fn, struct, etc)
12
+ * - scopes
13
+ *
14
+ */
15
+ export interface WeslAST {
16
+ /** source text for this module */
17
+ srcModule: SrcModule;
18
+ /** root module element */
19
+ moduleElem: ModuleElem;
20
+ /** root scope for this module */
21
+ rootScope: Scope;
22
+ /** imports found in this module */
23
+ imports: ImportStatement[];
24
+ /** module level const_assert statements */
25
+ moduleAsserts?: ConstAssertElem[];
26
+ }
27
+ /** an extended version of the AST */
28
+ export interface BindingAST extends WeslAST {
29
+ _flatImports?: FlatImport[];
30
+ }
31
+ /** stable and unstable state used during parsing */
32
+ export interface WeslParseState extends AppState<WeslParseContext, StableState> {
33
+ context: WeslParseContext;
34
+ stable: StableState;
35
+ }
36
+ /** stable values used or accumulated during parsing */
37
+ export type StableState = WeslAST;
38
+ /** unstable values used during parse collection */
39
+ export interface WeslParseContext {
40
+ scope: Scope;
41
+ openElems: OpenElem[];
42
+ }
43
+ /**
44
+ * An error when parsing WESL fails. Designed to be human-readable.
45
+ */
46
+ export declare class WeslParseError extends Error {
47
+ position: number;
48
+ src: SrcModule;
49
+ constructor(opts: {
50
+ cause: ParseError;
51
+ src: SrcModule;
52
+ });
53
+ }
54
+ /** Parse a WESL file. Throws on error. */
55
+ export declare function parseSrcModule(srcModule: SrcModule, srcMap?: SrcMap): WeslAST;
56
+ export declare function parseWESL(src: string, srcMap?: SrcMap): WeslAST;
57
+ export declare function blankWeslParseState(srcModule: SrcModule): WeslParseState;
58
+ export declare function syntheticWeslParseState(): WeslParseState;
59
+ /** @return a flattened form of the import tree for convenience in binding idents. */
60
+ export declare function flatImports(ast: BindingAST): FlatImport[];
@@ -0,0 +1,29 @@
1
+ import { WeslBundle } from '.';
2
+ import { WeslAST } from './ParseWESL.ts';
3
+ export interface ParsedRegistry {
4
+ modules: Record<string, WeslAST>;
5
+ }
6
+ export declare function parsedRegistry(): ParsedRegistry;
7
+ /** for debug */
8
+ export declare function registryToString(registry: ParsedRegistry): string;
9
+ /**
10
+ * Parse WESL each src module (file) into AST elements and a Scope tree.
11
+ * @param src keys are module paths, values are wesl src strings
12
+ */
13
+ export declare function parseWeslSrc(src: Record<string, string>): ParsedRegistry;
14
+ /** Look up a module with a flexible selector.
15
+ * :: separated module path, package::util
16
+ * / separated file path ./util.wesl (or ./util)
17
+ * - note: a file path should not include a weslRoot prefix, e.g. not ./shaders/util.wesl
18
+ * simpleName util
19
+ */
20
+ export declare function selectModule(parsed: ParsedRegistry, selectPath: string, packageName?: string): WeslAST | undefined;
21
+ /**
22
+ * @param srcFiles map of source strings by file path
23
+ * key is '/' separated relative path (relative to srcRoot, not absolute file path )
24
+ * value is wesl source string
25
+ * @param registry add parsed modules to this registry
26
+ * @param packageName name of package
27
+ */
28
+ export declare function parseIntoRegistry(srcFiles: Record<string, string>, registry: ParsedRegistry, packageName?: string, debugWeslRoot?: string): void;
29
+ export declare function parseLibsIntoRegistry(libs: WeslBundle[], registry: ParsedRegistry): void;
@@ -0,0 +1,6 @@
1
+ /** simplistic path manipulation utilities */
2
+ /** return path with ./ and foo/.. elements removed */
3
+ export declare function normalize(path: string): string;
4
+ /** return path w/o a suffix.
5
+ * e.g. /foo/bar.wgsl => /foo/bar */
6
+ export declare function noSuffix(path: string): string;
@@ -0,0 +1,6 @@
1
+ import { AttributeElem, NameElem, StuffElem, TranslateTimeExpressionElem, TypeRefElem, TypeTemplateParameter, UnknownExpressionElem } from './AbstractElems.ts';
2
+ export declare function attributeToString(e: AttributeElem): string;
3
+ export declare function typeListToString(params: TypeTemplateParameter[]): string;
4
+ export declare function typeParamToString(param?: TypeTemplateParameter): string;
5
+ export declare function typeRefToString(t?: TypeRefElem): string;
6
+ export declare function contentsToString(elem: TranslateTimeExpressionElem | UnknownExpressionElem | NameElem | StuffElem): string;
@@ -0,0 +1,45 @@
1
+ import { BindingStructElem } from './AbstractElems.ts';
2
+ import { TransformedAST, WeslJsPlugin } from './Linker.ts';
3
+ export type BindingStructReportFn = (structs: BindingStructElem[]) => void;
4
+ export declare const textureStorage: RegExp;
5
+ export declare function reportBindingStructsPlugin(fn: BindingStructReportFn): WeslJsPlugin;
6
+ /**
7
+ * Linker plugin that generates TypeScript strings for GPUBindingGroupLayouts
8
+ * based on the binding structs in the WESL source
9
+ *
10
+ * requires the enableBindingStructs() transform to be enabled
11
+ *
12
+ * @param fn a function that will be called with the binding structs
13
+ * (Normally the caller will pass a function that uses bindingGroupLayoutTs()
14
+ * to generate the TypeScript)
15
+ *
16
+ * The generated TypeScript looks looks roughly like this
17
+
18
+ export function MyBindingLayout(device: GPUDevice): GPUBindGroupLayout {
19
+ return device.createBindGroupLayout({
20
+ entries: [
21
+ {
22
+ binding: 0,
23
+ visibility: GPUShaderStage.COMPUTE,
24
+ buffer: {
25
+ type: "storage",
26
+ },
27
+ },
28
+ ],
29
+ });
30
+ }
31
+ */
32
+ export declare function reportBindingStructs(fn: BindingStructReportFn): (ast: TransformedAST) => TransformedAST;
33
+ /**
34
+ * @return a string containing a generated TypeScript function that creates
35
+ * a GPUBindingGroupLayout instance to align with the binding structures
36
+ * in wesl source.
37
+ */
38
+ export declare function bindingGroupLayoutTs(struct: BindingStructElem, typeScript?: boolean): string;
39
+ export declare function formatToTextureSampleType(format: GPUTextureFormat, float32Filterable?: boolean): GPUTextureSampleType;
40
+ export type WgslTexelType = "f32" | "u32" | "i32";
41
+ /** return the wgsl element type for a given texture format */
42
+ export declare function formatToTexelType(format: GPUTextureFormat): WgslTexelType;
43
+ /** @return the webgpu GPUTextureSampleType from the wgsl texel type */
44
+ export declare function texelTypeToSampleType(type: WgslTexelType): GPUTextureSampleType;
45
+ export declare function accessMode(access: string): GPUStorageTextureAccess;
@@ -0,0 +1,81 @@
1
+ import { DeclarationElem, IfAttribute, RefIdentElem } from './AbstractElems.ts';
2
+ import { WeslAST } from './ParseWESL.ts';
3
+ export interface SrcModule {
4
+ /** module path "rand_pkg::sub::foo", or "package::main" */
5
+ modulePath: string;
6
+ /** file path to the module for user error reporting e.g "rand_pkg:sub/foo.wesl", or "./sub/foo.wesl" */
7
+ debugFilePath: string;
8
+ /** original src for module */
9
+ src: string;
10
+ }
11
+ /** a src declaration or reference to an ident */
12
+ export type Ident = DeclIdent | RefIdent;
13
+ /** LATER change this to a Map, so that `toString` isn't accidentally a condition */
14
+ export type Conditions = Record<string, boolean>;
15
+ interface IdentBase {
16
+ originalName: string;
17
+ id?: number;
18
+ }
19
+ export interface RefIdent extends IdentBase {
20
+ kind: "ref";
21
+ refersTo?: Ident;
22
+ std?: true;
23
+ ast: WeslAST;
24
+ refIdentElem: RefIdentElem;
25
+ }
26
+ export interface DeclIdent extends IdentBase {
27
+ kind: "decl";
28
+ mangledName?: string;
29
+ declElem?: DeclarationElem;
30
+ scope: Scope;
31
+ isGlobal: boolean;
32
+ srcModule: SrcModule;
33
+ }
34
+ /** tree of ident references, organized by lexical scope and partialScope . */
35
+ export type Scope = LexicalScope | PartialScope;
36
+ /** A wgsl scope */
37
+ export interface LexicalScope extends ScopeBase {
38
+ kind: "scope";
39
+ /** @if condition for conditionally translating this scope */
40
+ ifAttribute?: IfAttribute;
41
+ /**
42
+ * Efficient access to declarations in this scope.
43
+ * constructed on demand, for module root scopes only */ scopeDecls?: Map<string, DeclIdent>;
44
+ }
45
+ /** A synthetic partial scope to contain @if conditioned idents.
46
+ * PartialScope idents are considered to be in the wgsl lexical scope of their parent. */
47
+ export interface PartialScope extends ScopeBase {
48
+ kind: "partial";
49
+ /** @if condition for conditionally translating this scope */
50
+ ifAttribute?: IfAttribute;
51
+ }
52
+ /** common scope elements */
53
+ interface ScopeBase {
54
+ /** id for debugging */
55
+ id: number;
56
+ /** null for root scope in a module */
57
+ parent: Scope | null;
58
+ contents: (Ident | Scope)[];
59
+ /** @if conditions for conditionally translating this scope */
60
+ ifAttribute?: IfAttribute;
61
+ }
62
+ /** Combine two scope siblings.
63
+ * The first scope is mutated to append the contents of the second. */
64
+ export declare function mergeScope(a: Scope, b: Scope): void;
65
+ /** reset scope and ident debugging ids */
66
+ export declare function resetScopeIds(): void;
67
+ export declare function nextIdentId(): number;
68
+ /** make a new Scope object */
69
+ export declare function emptyScope(parent: Scope | null, kind?: Scope["kind"]): Omit<Scope, "ifAttribute">;
70
+ /** For debugging,
71
+ * @return true if a scope is in the rootScope tree somewhere */
72
+ export declare function containsScope(rootScope: Scope, scope: Scope): boolean;
73
+ /** @returns true if the provided element of a Scope
74
+ * is itself a Scope (and not an Ident) */
75
+ export declare function childScope(child: Scope | Ident): child is Scope;
76
+ /** @returns true if the provided element of a Scope
77
+ * is an Ident (and not a child Scope) */
78
+ export declare function childIdent(child: Scope | Ident): child is Ident;
79
+ /** find a public declaration with the given original name */
80
+ export declare function publicDecl(scope: Scope, name: string, conditions: Conditions): DeclIdent | undefined;
81
+ export {};
@@ -0,0 +1,13 @@
1
+ export declare const stdFns: string[];
2
+ export declare const sampledTextureTypes = "\n texture_1d texture_2d texture_2d_array texture_3d \n texture_cube texture_cube_array\n";
3
+ export declare const multisampledTextureTypes = "\n texture_multisampled_2d texture_depth_multisampled_2d\n";
4
+ export declare const textureStorageTypes = "\n texture_storage_1d texture_storage_2d texture_storage_2d_array \n texture_storage_3d\n";
5
+ export declare const stdTypes: string[];
6
+ /** https://www.w3.org/TR/WGSL/#predeclared-enumerants */
7
+ export declare const stdEnumerants: string[];
8
+ /** return true if the name is for a built in type (not a user struct) */
9
+ export declare function stdType(name: string): boolean;
10
+ /** return true if the name is for a built in fn (not a user function) */
11
+ export declare function stdFn(name: string): boolean;
12
+ /** return true if the name is for a built in enumerant */
13
+ export declare function stdEnumerant(name: string): boolean;
@@ -0,0 +1,52 @@
1
+ import { BindingStructElem, DeclarationElem, ModuleElem, SimpleMemberRef, StructElem, SyntheticElem } from './AbstractElems.ts';
2
+ import { TransformedAST, WeslJsPlugin } from './Linker.ts';
3
+ export declare function bindingStructsPlugin(): WeslJsPlugin;
4
+ /**
5
+ * Transform binding structures into binding variables by mutating the AST.
6
+ *
7
+ * First we find all the binding structs:
8
+ * . find all the structs in the module by filtering the moduleElem.contents
9
+ * . for each struct:
10
+ * . mark any structs with that contain @group or @binding annotations as 'binding structs' and save them in a list
11
+ * . (later) create reverse links from structs to struct members
12
+ * . (later) visit all the binding structs and traverse to referencing structs, marking the referencing structs as binding structs too
13
+ * Generate synethic AST nodes for binding variables
14
+ *
15
+ * Find all references to binding struct members
16
+ * . find the componound idents by traversing moduleElem.contents
17
+ * . filter to find the compound idents that refer to 'binding structs'
18
+ * . go from each ident to its declaration,
19
+ * . declaration to typeRef reference
20
+ * . typeRef to type declaration
21
+ * . check type declaration to see if it's a binding struct
22
+ * . record the intermediate declaration (e.g. a fn param b:Bindings from 'fn(b:Bindings)' )
23
+ * rewrite references to binding struct members as synthetic elements
24
+ *
25
+ * Remove the binding structs from the AST
26
+ * Remove the intermediate fn param declarations from the AST
27
+ * Add the new binding variables to the AST
28
+ *
29
+ * @return the binding structs and the mutated AST
30
+ */
31
+ export declare function lowerBindingStructs(ast: TransformedAST): TransformedAST;
32
+ export declare function markEntryTypes(moduleElem: ModuleElem, bindingStructs: BindingStructElem[]): void;
33
+ /** mutate the AST, marking StructElems as bindingStructs
34
+ * (if they contain ptrs with @group @binding annotations)
35
+ * @return the binding structs
36
+ */
37
+ export declare function markBindingStructs(moduleElem: ModuleElem): BindingStructElem[];
38
+ /** convert each member of the binding struct into a synthetic global variable */
39
+ export declare function transformBindingStruct(s: StructElem, globalNames: Set<string>): SyntheticElem[];
40
+ interface MemberRefToStruct extends StructTrace {
41
+ memberRef: SimpleMemberRef;
42
+ }
43
+ interface StructTrace {
44
+ struct: StructElem;
45
+ intermediates: DeclarationElem[];
46
+ }
47
+ /** find all simple member references in the module that refer to binding structs */
48
+ export declare function findRefsToBindingStructs(moduleElem: ModuleElem): MemberRefToStruct[];
49
+ /** Mutate the member reference elem to instead contain synthetic elem text.
50
+ * The new text is the mangled var name of the struct member that the memberRef refers to. */
51
+ export declare function transformBindingReference(memberRef: SimpleMemberRef, struct: StructElem): SyntheticElem;
52
+ export {};
@@ -0,0 +1,43 @@
1
+ import { Span } from '../../mini-parse/src';
2
+ export declare function multiKeySet<A, B, V>(m: Map<A, Map<B, V>>, a: A, b: B, v: V): void;
3
+ /** replace strings in a text according to a relacement map
4
+ * replaced strings must be 'tokens', surrounded by spaces or punctuation
5
+ */
6
+ export declare function replaceWords(text: string, replace: Record<string, string>): string;
7
+ /** return an array partitioned into possibly overlapping groups */
8
+ export declare function grouped<T>(a: T[], size: number, stride?: number): T[][];
9
+ /** group an array into subarrays by a key function */
10
+ export declare function groupBy<T, K>(a: T[], key: (t: T) => K): Map<K, T[]>;
11
+ /** partition an array into two parts by a discriminator function */
12
+ export declare function partition<T>(a: T[], partFn: (t: T) => boolean): [T[], T[]];
13
+ /** run an carrying function over every element in an array,
14
+ * i.e. an inclusive prefix scan */
15
+ export declare function scan<T, U>(array: T[], fn: (a: T, b: U) => U, zero: U): U[];
16
+ /** return a new record by replacing values in 'a' with 'b' as a map.
17
+ * values in 'a' that are not in 'b' are unchanged.
18
+ * e.g. {a: "b", x: 9}, {b: 1} yields {a: 1, x: 9}
19
+ */
20
+ export declare function mapForward(a: Record<string, string>, b: Record<string, any>): Record<string, any>;
21
+ /** return the last element of an array or undefined */
22
+ export declare function last<T>(a: T[]): T | undefined;
23
+ /**
24
+ * Overlap two arrays, returning the tail of b if a is a prefix of b.
25
+ * Otherwise, return undefined.
26
+ */
27
+ export declare function overlapTail<T>(a: T[], b: T[]): T[] | undefined;
28
+ /** filter an array, returning the truthy results of the filter function */
29
+ export declare function filterMap<T, U>(arr: T[], fn: (t: T) => U | undefined): U[];
30
+ /** filters an array, returns the first truthy result of the filter function */
31
+ export declare function findMap<T, U>(arr: T[], fn: (t: T) => U | undefined): U | undefined;
32
+ /** Run a function over the values in a Record
33
+ * @return a new Record with mapped values. */
34
+ export declare function mapValues<T, U>(obj: Record<string, T>, fn: (v: T) => U): Record<string, U>;
35
+ /**
36
+ * Maps an index to a 1-indexed line number, and 1-indexed column.
37
+ */
38
+ export declare function offsetToLineNumber(offset: number, text: string): [lineNum: number, linePos: number];
39
+ /** Highlights an error.
40
+ *
41
+ * Returns a string with the line, and a string with the ^^^^ carets
42
+ */
43
+ export declare function errorHighlight(source: string, span: Span): [string, string];
@@ -0,0 +1,94 @@
1
+ import { CollectContext, CollectPair } from '../../mini-parse/src';
2
+ import { AbstractElem, AliasElem, AttributeElem, ConstAssertElem, ConstElem, ContainerElem, DeclIdentElem, DirectiveElem, FnElem, FnParamElem, GlobalVarElem, LetElem, ModuleElem, NameElem, OverrideElem, RefIdentElem, SimpleMemberRef, StatementElem, StructMemberElem, SwitchClauseElem, TypedDeclElem, TypeRefElem, VarElem } from './AbstractElems.ts';
3
+ import { Scope } from './Scope.ts';
4
+ export declare function importElem(cc: CollectContext): void;
5
+ /** create reference Ident and add to context */
6
+ export declare function refIdent(cc: CollectContext): RefIdentElem;
7
+ /** create declaration Ident and add to context */
8
+ export declare function declCollect(cc: CollectContext): DeclIdentElem;
9
+ /** create global declaration Ident and add to context */
10
+ export declare function globalDeclCollect(cc: CollectContext): DeclIdentElem;
11
+ export declare const typedDecl: CollectPair<TypedDeclElem>;
12
+ export type OpenElem<T extends ContainerElem = ContainerElem> = Pick<T, "kind" | "contents">;
13
+ export type PartElem<T extends ContainerElem = ContainerElem> = Pick<T, "kind" | "start" | "end" | "contents">;
14
+ type VarLikeElem = GlobalVarElem | VarElem | LetElem | ConstElem | OverrideElem;
15
+ export declare function collectVarLike<E extends VarLikeElem>(kind: E["kind"]): CollectPair<E>;
16
+ export declare const aliasCollect: CollectPair<AliasElem>;
17
+ /**
18
+ * Collect a FnElem and associated scopes.
19
+ *
20
+ * Scope definition is a bit complicated in wgsl and wesl for fns.
21
+ * Here's what we collect for scopes for this example function:
22
+ * @if(true) fn foo(a: u32) -> @location(x) R { let y = a; }
23
+ *
24
+ * -{ // partial scope in case the whole shebang is prefixed by an `@if`
25
+ * %foo
26
+ *
27
+ * {<=%foo // foo decl references this header+returnType+body scope (for tracing dependencies from decls)
28
+ * x // for @location(x) (contains no decls, so ok to merge for tracing)
29
+ * %a u32 // merged from header scope
30
+ * R // merged from return type (contains no decls, so ok to merge for tracing)
31
+ * %y a // merged body scope
32
+ * }
33
+ * }
34
+ */
35
+ export declare const fnCollect: CollectPair<FnElem>;
36
+ export declare const collectFnParam: CollectPair<FnParamElem>;
37
+ export declare const collectStruct: CollectPair<{
38
+ name: DeclIdentElem;
39
+ attributes: AttributeElem[];
40
+ members: StructMemberElem[];
41
+ start: number;
42
+ end: number;
43
+ kind: "struct";
44
+ contents: AbstractElem[];
45
+ }>;
46
+ export declare const collectStructMember: CollectPair<{
47
+ name: any;
48
+ attributes: AttributeElem[];
49
+ typeRef: any;
50
+ start: number;
51
+ end: number;
52
+ kind: "member";
53
+ contents: AbstractElem[];
54
+ }>;
55
+ export declare const specialAttribute: CollectPair<AttributeElem>;
56
+ /** debug routine to log tags at collect() */
57
+ export declare function logCollect(msg?: string): (cc: CollectContext) => void;
58
+ export declare const assertCollect: CollectPair<ConstAssertElem>;
59
+ export declare const statementCollect: CollectPair<StatementElem>;
60
+ export declare const switchClauseCollect: CollectPair<SwitchClauseElem>;
61
+ export declare const collectAttribute: CollectPair<AttributeElem>;
62
+ export declare const typeRefCollect: CollectPair<TypeRefElem>;
63
+ export declare const expressionCollect: CollectPair<{
64
+ start: number;
65
+ end: number;
66
+ kind: "expression";
67
+ contents: AbstractElem[];
68
+ }>;
69
+ export declare function globalAssertCollect(cc: CollectContext): void;
70
+ export declare const stuffCollect: CollectPair<{
71
+ start: number;
72
+ end: number;
73
+ kind: "stuff";
74
+ contents: AbstractElem[];
75
+ }>;
76
+ export declare const memberRefCollect: CollectPair<SimpleMemberRef>;
77
+ export declare function nameCollect(cc: CollectContext): NameElem;
78
+ export declare const collectModule: CollectPair<ModuleElem>;
79
+ export declare function directiveCollect(cc: CollectContext): DirectiveElem;
80
+ /**
81
+ * Collect a LexicalScope.
82
+ *
83
+ * The scope starts encloses all idents and subscopes inside the parser to which
84
+ * .collect is attached
85
+ */
86
+ export declare const scopeCollect: CollectPair<Scope>;
87
+ /**
88
+ * Collect a PartialScope.
89
+ *
90
+ * The scope starts encloses all idents and subscopes inside the parser to which
91
+ * .collect is attached
92
+ */
93
+ export declare const partialScopeCollect: CollectPair<Scope>;
94
+ export {};
@@ -0,0 +1,13 @@
1
+ export interface WeslBundle {
2
+ /** name of the package, e.g. random_wgsl */
3
+ name: string;
4
+ /** wesl edition of the code e.g. wesl_unstable_2024_1 */
5
+ edition: string;
6
+ /** map of wesl/wgsl modules:
7
+ * keys are file paths, relative to package root (e.g. "./lib.wgsl")
8
+ * values are wgsl/wesl code strings
9
+ */
10
+ modules: Record<string, string>;
11
+ /** packages referenced by this package */
12
+ dependencies?: WeslBundle[];
13
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * A {@link GPUDevice} with extensions for WESL. Created with {@link makeWeslDevice}.
3
+ * Used to make error reporting point at the orignal WESL sources.
4
+ */
5
+ export interface WeslDevice extends GPUDevice {
6
+ /**
7
+ * Attaches an error to the current error scope (created by {@link GPUDevice.pushErrorScope}).
8
+ * If there is no error scope, it reports the error as a `'uncapturederror'`
9
+ */
10
+ injectError(type: GPUErrorFilter, error: Promise<GPUError | null>): void;
11
+ }
12
+ /**
13
+ * Mutates a {@link GPUDevice} for usage with WESL. Does not impact your existing code, wherever a {@link GPUDevice} can be used, a {@link WeslDevice} is also valid.
14
+ *
15
+ * WESL uses this to display errors pointing at the WESL source instead of pointing at generated code.
16
+ */
17
+ export declare function makeWeslDevice(device: GPUDevice): WeslDevice;
18
+ export declare function throwClickableError({ url, text, lineNumber, lineColumn, length, error, }: {
19
+ url: string;
20
+ text: string | null;
21
+ lineNumber: number;
22
+ lineColumn: number;
23
+ length: number;
24
+ error: Error;
25
+ }): void;
@@ -0,0 +1,5 @@
1
+ import { AbstractElem, Attribute, StuffElem } from '../AbstractElems.ts';
2
+ export declare function astToString(elem: AbstractElem, indent?: number): string;
3
+ /** @return string representation of an attribute (for test/debug) */
4
+ export declare function attributeToString(attr: Attribute): string;
5
+ export declare function debugContentsToString(elem: StuffElem): string;
@@ -0,0 +1,2 @@
1
+ import { ImportStatement } from '../AbstractElems';
2
+ export declare function importToString(tree: ImportStatement): string;
@@ -0,0 +1,21 @@
1
+ /** debug utility for constructing strings that wrap at a fixed column width
2
+ * text beyond the column width is wrapped to start on the next line
3
+ */
4
+ export declare class LineWrapper {
5
+ #private;
6
+ readonly indent: number;
7
+ readonly maxWidth: number;
8
+ readonly hangingIndent: number;
9
+ constructor(indent?: number, maxWidth?: number, hangingIndent?: number);
10
+ /** add a new line to the constructed string */
11
+ nl(): void;
12
+ /** add a string, wrapping to the next line if necessary */
13
+ add(s: string): void;
14
+ /** add a raw block of text with no wrapping */
15
+ addBlock(s: string, andNewLine?: boolean): void;
16
+ /** @return the constructed string */
17
+ get result(): string;
18
+ /** true if the result contains no newlines */
19
+ get oneLine(): boolean;
20
+ private hangingNl;
21
+ }
@@ -0,0 +1,6 @@
1
+ import { Ident, Scope } from '../Scope.ts';
2
+ /** A debugging print of the scope tree with identifiers in nested brackets */
3
+ export declare function scopeToString(scope: Scope, indent?: number, shortIdents?: boolean): string;
4
+ /** A debug print of the scope tree with identifiers in long form in nested brackets */
5
+ export declare function scopeToStringLong(scope: Scope): string;
6
+ export declare function identToString(ident?: Ident): string;
@@ -0,0 +1,11 @@
1
+ export * from './debug/ASTtoString.js';
2
+ export * from './debug/ScopeToString.js';
3
+ export * from './LinkedWesl.js';
4
+ export * from './Linker.js';
5
+ export { WeslStream } from './parse/WeslStream.js';
6
+ export * from './ParsedRegistry.js';
7
+ export * from './ParseWESL.js';
8
+ export * from './PathUtil.js';
9
+ export * from './TransformBindingStructs.js';
10
+ export * from './WeslBundle.js';
11
+ export * from './WeslDevice.js';
@@ -0,0 +1,5 @@
1
+ import { Parser, Stream } from '../../../mini-parse/src';
2
+ import { ImportElem } from '../AbstractElems.js';
3
+ import { WeslToken } from './WeslStream.js';
4
+ /** parse a WESL style wgsl import statement. */
5
+ export declare const weslImports: Parser<Stream<WeslToken>, ImportElem[]>;
@@ -0,0 +1,4 @@
1
+ /** https://www.w3.org/TR/WGSL/#keyword-summary */
2
+ export declare const keywords: string[];
3
+ /** https://www.w3.org/TR/WGSL/#reserved-words */
4
+ export declare const reservedWords: string[];
@@ -0,0 +1,5 @@
1
+ import { WeslTokenKind } from './WeslStream';
2
+ export declare const word: import('../../../mini-parse/src').Parser<import('../../../mini-parse/src').Stream<import('../../../mini-parse/src').TypedToken<WeslTokenKind>>, string>;
3
+ export declare const keyword: import('../../../mini-parse/src').Parser<import('../../../mini-parse/src').Stream<import('../../../mini-parse/src').TypedToken<WeslTokenKind>>, string>;
4
+ export declare const qualified_ident: import('../../../mini-parse/src').Parser<import('../../../mini-parse/src').ParserStream | import('../../../mini-parse/src').Stream<import('../../../mini-parse/src').TypedToken<WeslTokenKind>>, string[]>;
5
+ export declare const number: import('../../../mini-parse/src').Parser<import('../../../mini-parse/src').Stream<import('../../../mini-parse/src').TypedToken<WeslTokenKind>>, string>;
@@ -0,0 +1,13 @@
1
+ import { Parser, Stream } from '../../../mini-parse/src';
2
+ import { WeslToken } from './WeslStream';
3
+ export declare const opt_template_list: Parser<import('../../../mini-parse/src').ParserStream | Stream<WeslToken>, [WeslToken<import('./WeslStream').WeslTokenKind> & {
4
+ kind: "symbol";
5
+ }, any[], WeslToken<import('./WeslStream').WeslTokenKind> & {
6
+ kind: "symbol";
7
+ }] | null>;
8
+ export declare const component_or_swizzle: import('../../../mini-parse/src/CombinatorTypes').ParserFromRepeatArg<import('../../../mini-parse/src/CombinatorTypes').OrParser<[Parser<import('../../../mini-parse/src').ParserStream | Stream<import('../../../mini-parse/src').TypedToken<import('./WeslStream').WeslTokenKind>>, string>, Parser<import('../../../mini-parse/src').ParserStream | Stream<WeslToken>, any>]>>;
9
+ /** parse simple struct.member style references specially, for binding struct lowering */
10
+ export declare const simple_component_reference: Parser<import('../../../mini-parse/src').ParserStream, [string[], [string, string], any[] | null]>;
11
+ export declare const expression: Parser<Stream<WeslToken>, any>;
12
+ export declare const type_specifier: Parser<Stream<WeslToken>, any>;
13
+ export declare const argument_expression_list: import('../../../mini-parse/src/CombinatorTypes').SeqParser<["(", Parser<import('../../../mini-parse/src').ParserStream | Stream<WeslToken>, any[]>, import('../../../mini-parse/src/CombinatorTypes').ParserFromArg<")">]>;
@@ -0,0 +1,80 @@
1
+ import { Parser, Span, Stream } from '../../../mini-parse/src';
2
+ import { BuiltinAttribute, DiagnosticAttribute, DiagnosticDirective, EnableDirective, IfAttribute, InterpolateAttribute, RequiresDirective, StandardAttribute } from '../AbstractElems.ts';
3
+ import { WeslToken } from './WeslStream.ts';
4
+ export declare const weslRoot: Parser<import('../../../mini-parse/src').ParserStream | Stream<WeslToken>, [import('../AbstractElems.ts').ImportElem[], [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
5
+ value: any;
6
+ span: Span;
7
+ }[]] | [string, {
8
+ value: any;
9
+ span: Span;
10
+ }[] | null])[], DiagnosticDirective | EnableDirective | RequiresDirective][], (string | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
11
+ value: any;
12
+ span: Span;
13
+ }[]] | [string, {
14
+ value: any;
15
+ span: Span;
16
+ }[] | null])[], string, string, [string, [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
17
+ value: any;
18
+ span: Span;
19
+ }[]] | [string, {
20
+ value: any;
21
+ span: Span;
22
+ }[] | null])[], string, string, any][], string]] | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
23
+ value: any;
24
+ span: Span;
25
+ }[]] | [string, {
26
+ value: any;
27
+ span: Span;
28
+ }[] | null])[], string, any, string] | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
29
+ value: any;
30
+ span: Span;
31
+ }[]] | [string, {
32
+ value: any;
33
+ span: Span;
34
+ }[] | null])[], string, string, [[string, [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
35
+ value: any;
36
+ span: Span;
37
+ }[]] | [string, {
38
+ value: any;
39
+ span: Span;
40
+ }[] | null])[], string, [string, any] | null][], string], [string, (IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
41
+ value: any;
42
+ span: Span;
43
+ }[]] | [string, {
44
+ value: any;
45
+ span: Span;
46
+ }[] | null])[], any] | null, [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
47
+ value: any;
48
+ span: Span;
49
+ }[]] | [string, {
50
+ value: any;
51
+ span: Span;
52
+ }[] | null])[], string, any[], string]]] | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
53
+ value: any;
54
+ span: Span;
55
+ }[]] | [string, {
56
+ value: any;
57
+ span: Span;
58
+ }[] | null])[], string, [string, [string, any] | null], [[string, any] | null], string] | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
59
+ value: any;
60
+ span: Span;
61
+ }[]] | [string, {
62
+ value: any;
63
+ span: Span;
64
+ }[] | null])[], string, [string, [string, any] | null], string, [any], string] | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
65
+ value: any;
66
+ span: Span;
67
+ }[]] | [string, {
68
+ value: any;
69
+ span: Span;
70
+ }[] | null])[], string, string, string, any, string] | [(IfAttribute | StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | [string, {
71
+ value: any;
72
+ span: Span;
73
+ }[]] | [string, {
74
+ value: any;
75
+ span: Span;
76
+ }[] | null])[], [string, [WeslToken<import('./WeslStream.ts').WeslTokenKind> & {
77
+ kind: "symbol";
78
+ }, any[], WeslToken<import('./WeslStream.ts').WeslTokenKind> & {
79
+ kind: "symbol";
80
+ }] | null, [string, [string, any] | null], [string, any] | null], string])[], true]>;