wesl 0.6.0-pre2

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 (110) hide show
  1. package/dist/index.cjs +2617 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.js +2617 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/linker/packages/linker/src/AbstractElems.d.ts +104 -0
  6. package/dist/linker/packages/linker/src/BindIdents.d.ts +16 -0
  7. package/dist/linker/packages/linker/src/CommentsGrammar.d.ts +6 -0
  8. package/dist/linker/packages/linker/src/FlattenTreeImport.d.ts +11 -0
  9. package/dist/linker/packages/linker/src/ImportGrammar.d.ts +13 -0
  10. package/dist/linker/packages/linker/src/ImportTree.d.ts +17 -0
  11. package/dist/linker/packages/linker/src/Linker.d.ts +26 -0
  12. package/dist/linker/packages/linker/src/LowerAndEmit.d.ts +25 -0
  13. package/dist/linker/packages/linker/src/ParseWESL.d.ts +36 -0
  14. package/dist/linker/packages/linker/src/ParsedRegistry.d.ts +26 -0
  15. package/dist/linker/packages/linker/src/PathUtil.d.ts +9 -0
  16. package/dist/linker/packages/linker/src/Scope.d.ts +55 -0
  17. package/dist/linker/packages/linker/src/Slicer.d.ts +26 -0
  18. package/dist/linker/packages/linker/src/StandardTypes.d.ts +6 -0
  19. package/dist/linker/packages/linker/src/Util.d.ts +26 -0
  20. package/dist/linker/packages/linker/src/WESLCollect.d.ts +29 -0
  21. package/dist/linker/packages/linker/src/WESLGrammar.d.ts +23 -0
  22. package/dist/linker/packages/linker/src/WESLTokens.d.ts +42 -0
  23. package/dist/linker/packages/linker/src/WgslBundle.d.ts +13 -0
  24. package/dist/linker/packages/linker/src/debug/ASTtoString.d.ts +3 -0
  25. package/dist/linker/packages/linker/src/debug/ImportToString.d.ts +2 -0
  26. package/dist/linker/packages/linker/src/debug/LineWrapper.d.ts +21 -0
  27. package/dist/linker/packages/linker/src/debug/ScopeToString.d.ts +4 -0
  28. package/dist/linker/packages/linker/src/index.d.ts +7 -0
  29. package/dist/linker/packages/linker/src/test/ErrorLogging.test.d.ts +1 -0
  30. package/dist/linker/packages/linker/src/test/Expression.test.d.ts +1 -0
  31. package/dist/linker/packages/linker/src/test/FlattenTreeImport.test.d.ts +1 -0
  32. package/dist/linker/packages/linker/src/test/ImportCases.test.d.ts +1 -0
  33. package/dist/linker/packages/linker/src/test/ImportSyntaxCases.test.d.ts +1 -0
  34. package/dist/linker/packages/linker/src/test/LinkGlob.test.d.ts +1 -0
  35. package/dist/linker/packages/linker/src/test/LinkPackage.test.d.ts +1 -0
  36. package/dist/linker/packages/linker/src/test/Linker.test.d.ts +1 -0
  37. package/dist/linker/packages/linker/src/test/MatchWgslD.test.d.ts +1 -0
  38. package/dist/linker/packages/linker/src/test/ParseComments.test.d.ts +1 -0
  39. package/dist/linker/packages/linker/src/test/ParseWESL.test.d.ts +1 -0
  40. package/dist/linker/packages/linker/src/test/PathUtil.test.d.ts +1 -0
  41. package/dist/linker/packages/linker/src/test/PrettyGrammar.test.d.ts +1 -0
  42. package/dist/linker/packages/linker/src/test/ScopeWESL.test.d.ts +1 -0
  43. package/dist/linker/packages/linker/src/test/Slicer.test.d.ts +1 -0
  44. package/dist/linker/packages/linker/src/test/TestSetup.d.ts +1 -0
  45. package/dist/linker/packages/linker/src/test/TestUtil.d.ts +15 -0
  46. package/dist/linker/packages/linker/src/test/Util.test.d.ts +1 -0
  47. package/dist/linker/packages/linker/src/test/WgslTests.d.ts +0 -0
  48. package/dist/linker/packages/linker/src/test/shared/StringUtil.d.ts +8 -0
  49. package/dist/linker/packages/linker/src/test/shared/test/StringUtil.test.d.ts +1 -0
  50. package/dist/minified.cjs +2 -0
  51. package/dist/minified.cjs.map +1 -0
  52. package/dist/minified.js +2617 -0
  53. package/dist/minified.js.map +1 -0
  54. package/dist/wesl-testsuite/src/test-cases/BulkTests.d.ts +4 -0
  55. package/dist/wesl-testsuite/src/test-cases/ImportCases.d.ts +3 -0
  56. package/dist/wesl-testsuite/src/test-cases/ImportSyntaxCases.d.ts +3 -0
  57. package/package.json +45 -0
  58. package/src/AbstractElems.ts +148 -0
  59. package/src/BindIdents.ts +277 -0
  60. package/src/CommentsGrammar.ts +44 -0
  61. package/src/FlattenTreeImport.ts +59 -0
  62. package/src/ImportGrammar.ts +142 -0
  63. package/src/ImportTree.ts +19 -0
  64. package/src/Linker.ts +151 -0
  65. package/src/LowerAndEmit.ts +143 -0
  66. package/src/ParseWESL.ts +106 -0
  67. package/src/ParsedRegistry.ts +97 -0
  68. package/src/PathUtil.ts +52 -0
  69. package/src/Scope.ts +100 -0
  70. package/src/Slicer.ts +127 -0
  71. package/src/StandardTypes.ts +66 -0
  72. package/src/Util.ts +112 -0
  73. package/src/WESLCollect.ts +336 -0
  74. package/src/WESLGrammar.ts +538 -0
  75. package/src/WESLTokens.ts +97 -0
  76. package/src/WgslBundle.ts +16 -0
  77. package/src/debug/ASTtoString.ts +149 -0
  78. package/src/debug/ImportToString.ts +21 -0
  79. package/src/debug/LineWrapper.ts +65 -0
  80. package/src/debug/ScopeToString.ts +51 -0
  81. package/src/index.ts +7 -0
  82. package/src/test/ErrorLogging.test.ts +14 -0
  83. package/src/test/Expression.test.ts +22 -0
  84. package/src/test/FlattenTreeImport.test.ts +56 -0
  85. package/src/test/ImportCases.test.ts +440 -0
  86. package/src/test/ImportSyntaxCases.test.ts +22 -0
  87. package/src/test/LinkGlob.test.ts +25 -0
  88. package/src/test/LinkPackage.test.ts +26 -0
  89. package/src/test/Linker.test.ts +120 -0
  90. package/src/test/MatchWgslD.test.ts +16 -0
  91. package/src/test/ParseComments.test.ts +74 -0
  92. package/src/test/ParseWESL.test.ts +902 -0
  93. package/src/test/PathUtil.test.ts +34 -0
  94. package/src/test/PrettyGrammar.test.ts +21 -0
  95. package/src/test/ScopeWESL.test.ts +272 -0
  96. package/src/test/Slicer.test.ts +103 -0
  97. package/src/test/TestSetup.ts +4 -0
  98. package/src/test/TestUtil.ts +52 -0
  99. package/src/test/Util.test.ts +22 -0
  100. package/src/test/WgslTests.ts +0 -0
  101. package/src/test/__snapshots__/ParseDirectives.test.ts.snap +25 -0
  102. package/src/test/__snapshots__/ParseWESL.test.ts.snap +119 -0
  103. package/src/test/__snapshots__/ParseWESL2.test.ts.snap +67 -0
  104. package/src/test/__snapshots__/RustDirective.test.ts.snap +359 -0
  105. package/src/test/shared/StringUtil.ts +59 -0
  106. package/src/test/shared/test/StringUtil.test.ts +32 -0
  107. package/src/test/wgsl_1/main.wgsl +3 -0
  108. package/src/test/wgsl_1/util.wgsl +1 -0
  109. package/src/test/wgsl_2/main2.wgsl +3 -0
  110. package/src/test/wgsl_2/util2.wgsl +1 -0
@@ -0,0 +1,149 @@
1
+ import { AbstractElem, ModuleElem } from "../AbstractElems.ts";
2
+ import { importToString } from "./ImportToString.ts";
3
+ import { LineWrapper } from "./LineWrapper.ts";
4
+
5
+ export function astToString(elem: AbstractElem, indent = 0): string {
6
+ const { kind, contents } = elem as ModuleElem;
7
+ const str = new LineWrapper(indent);
8
+ str.add(kind);
9
+ addElemFields(elem, str);
10
+ let childStrings: string[] = [];
11
+ if (contents) {
12
+ childStrings = contents.map(e => astToString(e, indent + 2));
13
+ }
14
+ if (childStrings.length) {
15
+ str.nl();
16
+ str.addBlock(childStrings.join("\n"), false);
17
+ }
18
+
19
+ return str.result;
20
+ }
21
+
22
+ export function elemToString(elem: AbstractElem): string {
23
+ const { kind } = elem as ModuleElem;
24
+ const str = new LineWrapper();
25
+ str.add(kind);
26
+ addElemFields(elem, str);
27
+ let childStrings: string[] = [];
28
+ if (childStrings.length) {
29
+ str.nl();
30
+ str.addBlock(childStrings.join("\n"), false);
31
+ }
32
+ return str.result;
33
+ }
34
+
35
+ function addElemFields(elem: AbstractElem, str: LineWrapper): void {
36
+ addTextFields(elem, str) ||
37
+ addVarishFields(elem, str) ||
38
+ addStructFields(elem, str) ||
39
+ addNameFields(elem, str) ||
40
+ addFnFields(elem, str) ||
41
+ addAliasFields(elem, str) ||
42
+ addImport(elem, str) ||
43
+ addRefIdent(elem, str) ||
44
+ addDeclIdent(elem, str);
45
+ }
46
+
47
+ function addVarishFields(
48
+ elem: AbstractElem,
49
+ str: LineWrapper,
50
+ ): true | undefined {
51
+ const { kind } = elem;
52
+ if (
53
+ kind === "var" ||
54
+ kind === "gvar" ||
55
+ kind === "const" ||
56
+ kind === "override"
57
+ ) {
58
+ str.add(" " + elem.name.ident.originalName);
59
+ if (elem.typeRef) {
60
+ str.add(":" + elem.typeRef.ident.originalName);
61
+ }
62
+ return true;
63
+ }
64
+ }
65
+
66
+ function addTextFields(
67
+ elem: AbstractElem,
68
+ str: LineWrapper,
69
+ ): true | undefined {
70
+ if (elem.kind === "text") {
71
+ const { srcModule, start, end } = elem;
72
+ str.add(` '${srcModule.src.slice(start, end)}'`);
73
+ return true;
74
+ }
75
+ }
76
+
77
+ function addRefIdent(elem: AbstractElem, str: LineWrapper): true | undefined {
78
+ if (elem.kind === "ref") {
79
+ str.add(" " + elem.ident.originalName);
80
+ return true;
81
+ }
82
+ }
83
+
84
+ function addDeclIdent(elem: AbstractElem, str: LineWrapper): true | undefined {
85
+ if (elem.kind === "decl") {
86
+ str.add(" %" + elem.ident.originalName);
87
+ return true;
88
+ }
89
+ }
90
+
91
+ function addAliasFields(
92
+ elem: AbstractElem,
93
+ str: LineWrapper,
94
+ ): true | undefined {
95
+ if (elem.kind === "alias") {
96
+ const { name, typeRef } = elem;
97
+ const prefix = name.ident.kind === "decl" ? "%" : "";
98
+ str.add(" " + prefix + name.ident.originalName);
99
+ str.add("=" + typeRef.ident.originalName);
100
+ return true;
101
+ }
102
+ }
103
+
104
+ function addStructFields(
105
+ elem: AbstractElem,
106
+ str: LineWrapper,
107
+ ): true | undefined {
108
+ if (elem.kind === "struct") {
109
+ str.add(" " + elem.name.ident.originalName);
110
+ return true;
111
+ }
112
+ }
113
+
114
+ function addImport(elem: AbstractElem, str: LineWrapper): true | undefined {
115
+ if (elem.kind === "import") {
116
+ str.add(" " + importToString(elem.imports));
117
+ return true;
118
+ }
119
+ }
120
+
121
+ function addNameFields(
122
+ elem: AbstractElem,
123
+ str: LineWrapper,
124
+ ): true | undefined {
125
+ if (elem.kind === "name") {
126
+ str.add(" " + elem.name);
127
+ return true;
128
+ }
129
+ }
130
+
131
+ function addFnFields(elem: AbstractElem, str: LineWrapper): true | undefined {
132
+ if (elem.kind === "fn") {
133
+ const { name, params, returnType } = elem;
134
+ str.add(" " + name.ident.originalName);
135
+
136
+ str.add("(");
137
+ const paramStrs = params
138
+ .map(p => p.name.ident.originalName + ": " + p.typeRef.ident.originalName)
139
+ .join(", ");
140
+ str.add(paramStrs);
141
+ str.add(")");
142
+
143
+ if (returnType) {
144
+ str.add(" -> " + returnType.ident.originalName);
145
+ }
146
+
147
+ return true;
148
+ }
149
+ }
@@ -0,0 +1,21 @@
1
+ import { ImportTree, PathSegment, SegmentList, SimpleSegment } from "../ImportTree.ts";
2
+
3
+ export function importToString(tree: ImportTree): string {
4
+ return tree.segments.map(s => segmentToString(s)).join("/");
5
+ }
6
+
7
+ function segmentToString(segment: PathSegment): string {
8
+ if (segment instanceof SimpleSegment) {
9
+ const { name, as, args } = segment;
10
+ const asMsg = as ? ` as ${as}` : "";
11
+ const argsMsg = args ? `(${args.join(", ")})` : "";
12
+ return `${name}${argsMsg}${asMsg}`;
13
+ }
14
+ if (segment instanceof SegmentList) {
15
+ return `{${segment.list.map(s => segmentToString(s)).join(", ")}}`;
16
+ }
17
+ if (segment instanceof ImportTree) {
18
+ return `(${importToString(segment)})`;
19
+ }
20
+ return `|unknown segment type ${(segment as any).constructor.name}|`;
21
+ }
@@ -0,0 +1,65 @@
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 class LineWrapper {
5
+ #fragments: string[] = [];
6
+ #column = 0;
7
+ #spc: string;
8
+ #oneLine = true;
9
+ #isHanging = false;
10
+ #hangingSpc: string;
11
+
12
+ constructor(
13
+ readonly indent = 0,
14
+ readonly maxWidth = 60,
15
+ readonly hangingIndent = 2,
16
+ ) {
17
+ this.#spc = " ".repeat(indent);
18
+ this.#hangingSpc = " ".repeat(hangingIndent);
19
+ }
20
+
21
+ /** add a new line to the constructed string */
22
+ nl() {
23
+ this.#fragments.push("\n");
24
+ this.#column = 0;
25
+ this.#oneLine = false;
26
+ this.#isHanging = false;
27
+ }
28
+
29
+ /** add a string, wrapping to the next line if necessary */
30
+ add(s: string) {
31
+ if (this.#column + s.length > this.maxWidth) {
32
+ this.hangingNl();
33
+ }
34
+ if (this.#column === 0) {
35
+ this.#fragments.push(this.#spc);
36
+ if (this.#isHanging) {
37
+ this.#fragments.push(this.#hangingSpc);
38
+ }
39
+ this.#column = this.indent;
40
+ }
41
+ this.#fragments.push(s);
42
+ this.#column += s.length;
43
+ }
44
+
45
+ /** add a raw block of text with no wrapping */
46
+ addBlock(s: string, andNewLine = true) {
47
+ this.#fragments.push(s);
48
+ if (andNewLine) this.nl();
49
+ }
50
+
51
+ /** @return the constructed string */
52
+ get result(): string {
53
+ return this.#fragments.join("");
54
+ }
55
+
56
+ /** true if the result contains no newlines */
57
+ get oneLine(): boolean {
58
+ return this.#oneLine;
59
+ }
60
+
61
+ private hangingNl() {
62
+ this.nl();
63
+ this.#isHanging = true;
64
+ }
65
+ }
@@ -0,0 +1,51 @@
1
+ import { LineWrapper } from "./LineWrapper.ts";
2
+ import { Ident, Scope } from "../Scope.ts";
3
+
4
+ /** A debugging print of the scope tree with identifiers in nested brackets */
5
+ export function scopeToString(scope: Scope, indent = 0): string {
6
+ const { children } = scope;
7
+ let childStrings: string[] = [];
8
+ if (children.length)
9
+ childStrings = children.map(c => scopeToString(c, indent + 2));
10
+
11
+ // list of identifiers, with decls prefixed with '%'
12
+ const identStrings = scope.idents.map(({ kind, originalName }) => {
13
+ const prefix = kind === "decl" ? "%" : "";
14
+ return `${prefix}${originalName}`;
15
+ });
16
+
17
+ const str = new LineWrapper(indent);
18
+ str.add("{ ");
19
+
20
+ const last = identStrings.length - 1;
21
+ identStrings.forEach((s, i) => {
22
+ const element = i < last ? s + ", " : s;
23
+ str.add(element);
24
+ });
25
+
26
+ if (childStrings.length) {
27
+ str.nl();
28
+ str.addBlock(childStrings.join("\n"));
29
+ }
30
+
31
+ if (str.oneLine) {
32
+ str.add(" }");
33
+ } else {
34
+ if (!childStrings.length) str.nl();
35
+ str.add("}");
36
+ }
37
+
38
+ return str.result;
39
+ }
40
+
41
+ export function identToString(ident?: Ident): string {
42
+ if (!ident) return JSON.stringify(ident);
43
+ const { kind, originalName } = ident;
44
+ const idStr = ident.id ? `#${ident.id}` : "";
45
+ if (kind === "ref") {
46
+ const ref = identToString(ident.refersTo!);
47
+ return `${originalName} ${idStr} -> ${ref}`;
48
+ } else {
49
+ return `%${originalName} ${idStr} (${ident.mangledName})`;
50
+ }
51
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./debug/ASTtoString.js";
2
+ export * from "./debug/ScopeToString.js";
3
+ export * from "./Linker.js";
4
+ export * from "./ParseWESL.js";
5
+ export * from "./PathUtil.js";
6
+ export * from "./Util.js";
7
+ export * from "./WgslBundle.js";
@@ -0,0 +1,14 @@
1
+ import { expect, test } from "vitest";
2
+ import { linkWithLog } from "./TestUtil.ts";
3
+
4
+ test("unresolved identifier", () => {
5
+ const src = `
6
+ fn main() { x = 7; }
7
+ `;
8
+ const { log } = linkWithLog(src);
9
+ expect(log).toMatchInlineSnapshot(`
10
+ "unresolved identifier in file: ./test.wesl
11
+ fn main() { x = 7; } Ln 2
12
+ ^^"
13
+ `);
14
+ });
@@ -0,0 +1,22 @@
1
+ import { eof, seq } from "mini-parse";
2
+ import { expect, test } from "vitest";
3
+ import { expression } from "../WESLGrammar.ts";
4
+ import { testAppParse } from "./TestUtil.ts";
5
+
6
+ test("parse number", () => {
7
+ const src = `3`;
8
+ const { parsed } = testAppParse(seq(expression, eof), src);
9
+ expect(parsed).not.toBeNull();
10
+ });
11
+
12
+ test("parse comparisons with && ||", () => {
13
+ const src = `a<3 && 4>(5)`;
14
+ const { parsed } = testAppParse(seq(expression, eof), src);
15
+ expect(parsed).not.toBeNull();
16
+ });
17
+
18
+ test("parse vec templated type", () => {
19
+ const src = `vec2<f32>`;
20
+ const { parsed } = testAppParse(seq(expression, eof), src);
21
+ expect(parsed).not.toBeNull();
22
+ });
@@ -0,0 +1,56 @@
1
+ import { expect, test } from "vitest";
2
+ import {
3
+ ImportTree,
4
+ SegmentList,
5
+ SimpleSegment,
6
+ } from "../ImportTree.ts";
7
+ import { flattenTreeImport } from "../FlattenTreeImport.ts";
8
+
9
+ test("complex tree import", () => {
10
+ const zap = new SimpleSegment("zap");
11
+ const foo = new SimpleSegment("foo", "bar"); // foo as bar
12
+ const doh = new SimpleSegment("doh");
13
+ const bib = new SimpleSegment("bib");
14
+ const bog = new SimpleSegment("bog");
15
+ const subtree = new ImportTree([bib, bog]);
16
+ const list = new SegmentList([foo, doh, subtree]);
17
+
18
+ const tree = new ImportTree([zap, list]);
19
+ const flattened = flattenTreeImport(tree);
20
+ expect(flattened).toMatchInlineSnapshot(`
21
+ [
22
+ {
23
+ "importPath": [
24
+ "zap",
25
+ "bar",
26
+ ],
27
+ "modulePath": [
28
+ "zap",
29
+ "foo",
30
+ ],
31
+ },
32
+ {
33
+ "importPath": [
34
+ "zap",
35
+ "doh",
36
+ ],
37
+ "modulePath": [
38
+ "zap",
39
+ "doh",
40
+ ],
41
+ },
42
+ {
43
+ "importPath": [
44
+ "zap",
45
+ "bib",
46
+ "bog",
47
+ ],
48
+ "modulePath": [
49
+ "zap",
50
+ "bib",
51
+ "bog",
52
+ ],
53
+ },
54
+ ]
55
+ `);
56
+ });