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
package/src/Util.ts ADDED
@@ -0,0 +1,194 @@
1
+ import { Span } from "mini-parse";
2
+
3
+ export function multiKeySet<A, B, V>(
4
+ m: Map<A, Map<B, V>>,
5
+ a: A,
6
+ b: B,
7
+ v: V,
8
+ ): void {
9
+ const bMap = m.get(a) || new Map();
10
+ m.set(a, bMap);
11
+ bMap.set(b, v);
12
+ }
13
+
14
+ const tokenRegex = /\b(\w+)\b/gi;
15
+ /** replace strings in a text according to a relacement map
16
+ * replaced strings must be 'tokens', surrounded by spaces or punctuation
17
+ */
18
+ export function replaceWords(
19
+ text: string,
20
+ replace: Record<string, string>,
21
+ ): string {
22
+ return text.replaceAll(tokenRegex, s => (s in replace ? replace[s] : s));
23
+ }
24
+
25
+ /** return an array partitioned into possibly overlapping groups */
26
+ export function grouped<T>(a: T[], size: number, stride = size): T[][] {
27
+ const groups = [];
28
+ for (let i = 0; i < a.length; i += stride) {
29
+ groups.push(a.slice(i, i + size));
30
+ }
31
+ return groups;
32
+ }
33
+
34
+ /** group an array into subarrays by a key function */
35
+ export function groupBy<T, K>(a: T[], key: (t: T) => K): Map<K, T[]> {
36
+ const groups = new Map<K, T[]>();
37
+ for (const t of a) {
38
+ const k = key(t);
39
+ const group = groups.get(k) || [];
40
+ group.push(t);
41
+ groups.set(k, group);
42
+ }
43
+ return groups;
44
+ }
45
+
46
+ /** partition an array into two parts by a discriminator function */
47
+ export function partition<T>(a: T[], partFn: (t: T) => boolean): [T[], T[]] {
48
+ const yesPart: T[] = [];
49
+ const noPart: T[] = [];
50
+ for (const t of a) {
51
+ if (partFn(t)) yesPart.push(t);
52
+ else noPart.push(t);
53
+ }
54
+ return [yesPart, noPart];
55
+ }
56
+
57
+ /** run an carrying function over every element in an array,
58
+ * i.e. an inclusive prefix scan */
59
+ export function scan<T, U>(array: T[], fn: (a: T, b: U) => U, zero: U): U[] {
60
+ const result = [zero];
61
+
62
+ let current = zero;
63
+ for (let i = 0; i < array.length; i++) {
64
+ current = fn(array[i], current);
65
+ result.push(current);
66
+ }
67
+ return result;
68
+ }
69
+
70
+ /** return a new record by replacing values in 'a' with 'b' as a map.
71
+ * values in 'a' that are not in 'b' are unchanged.
72
+ * e.g. {a: "b", x: 9}, {b: 1} yields {a: 1, x: 9}
73
+ */
74
+ export function mapForward(
75
+ a: Record<string, string>,
76
+ b: Record<string, any>,
77
+ ): Record<string, any> {
78
+ const combined = Object.entries(a).map(([key, value]) => {
79
+ const mappedValue = value in b ? b[value] : value;
80
+ return [key, mappedValue];
81
+ });
82
+ return Object.fromEntries(combined);
83
+ }
84
+
85
+ /** return the last element of an array or undefined */
86
+ export function last<T>(a: T[]): T | undefined {
87
+ return a[a.length - 1];
88
+ }
89
+
90
+ /**
91
+ * Overlap two arrays, returning the tail of b if a is a prefix of b.
92
+ * Otherwise, return undefined.
93
+ */
94
+ export function overlapTail<T>(a: T[], b: T[]): T[] | undefined {
95
+ let overlapSize = Math.min(a.length, b.length);
96
+
97
+ while (overlapSize > 0) {
98
+ const suffix = a.slice(-overlapSize);
99
+ const prefix = b.slice(0, overlapSize);
100
+ if (arrayEquals(suffix, prefix)) {
101
+ break;
102
+ } else {
103
+ overlapSize--;
104
+ }
105
+ }
106
+
107
+ if (overlapSize) {
108
+ return b.slice(overlapSize);
109
+ }
110
+ }
111
+
112
+ function arrayEquals(a: any[], b: any[]): boolean {
113
+ return a.length === b.length && a.every((val, index) => val === b[index]);
114
+ }
115
+
116
+ /** filter an array, returning the truthy results of the filter function */
117
+ export function filterMap<T, U>(arr: T[], fn: (t: T) => U | undefined): U[] {
118
+ const out: U[] = [];
119
+ for (const t of arr) {
120
+ const u = fn(t);
121
+ if (u) out.push(u);
122
+ }
123
+ return out;
124
+ }
125
+
126
+ /** filters an array, returns the first truthy result of the filter function */
127
+ export function findMap<T, U>(
128
+ arr: T[],
129
+ fn: (t: T) => U | undefined,
130
+ ): U | undefined {
131
+ for (const t of arr) {
132
+ const u = fn(t);
133
+ if (u) return u;
134
+ }
135
+ return undefined;
136
+ }
137
+
138
+ /** Run a function over the values in a Record
139
+ * @return a new Record with mapped values. */
140
+ export function mapValues<T, U>(
141
+ obj: Record<string, T>,
142
+ fn: (v: T) => U,
143
+ ): Record<string, U> {
144
+ return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, fn(v)]));
145
+ }
146
+
147
+ /**
148
+ * Maps an index to a 1-indexed line number, and 1-indexed column.
149
+ */
150
+ export function offsetToLineNumber(
151
+ offset: number,
152
+ text: string,
153
+ ): [lineNum: number, linePos: number] {
154
+ offset = Math.min(text.length, Math.max(0, offset));
155
+ let lineStartOffset = 0;
156
+ let lineNum = 1;
157
+ while (true) {
158
+ // LATER: Does this "line break" actually match the spec? I think not
159
+ const lineEnd = text.indexOf("\n", lineStartOffset);
160
+ if (lineEnd === -1 || offset <= lineEnd) {
161
+ // Last relevant line
162
+ const linePos = 1 + (offset - lineStartOffset);
163
+ return [lineNum, linePos];
164
+ } else {
165
+ // Go to the next line
166
+ lineStartOffset = lineEnd + 1;
167
+ lineNum += 1;
168
+ }
169
+ }
170
+ }
171
+
172
+ /** Highlights an error.
173
+ *
174
+ * Returns a string with the line, and a string with the ^^^^ carets
175
+ */
176
+ export function errorHighlight(source: string, span: Span): [string, string] {
177
+ let lineStartOffset = source.lastIndexOf("\n", span[0]);
178
+ if (lineStartOffset === -1) {
179
+ lineStartOffset = 0;
180
+ }
181
+ let lineEndOffset = source.indexOf("\n", span[0]);
182
+ if (lineEndOffset === -1) {
183
+ lineEndOffset = source.length;
184
+ }
185
+
186
+ // LATER Handle multiline spans
187
+ const errorLength = span[1] - span[0];
188
+ const caretCount = Math.max(1, errorLength);
189
+ const linePos = span[0] - lineStartOffset;
190
+ return [
191
+ source.slice(lineStartOffset, lineEndOffset),
192
+ " ".repeat(linePos) + "^".repeat(caretCount),
193
+ ];
194
+ }