-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-pattern-impl.ts
More file actions
169 lines (161 loc) · 4.09 KB
/
code-pattern-impl.ts
File metadata and controls
169 lines (161 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { CodePattern } from "./types.js";
//
// The default patterns for code generation that are automatically included
//
export const DEFAULT_PATTERNS: CodePattern[] = [
{ 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: [string, CodePattern][] = [
[
"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: [string, CodePattern][] = [
[
".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<string, CodePattern>([
...BUILT_IN_PATTERNS,
...INDICES_HELPER_PATTERNS,
//TODO: add more patterns as needed
]);
export function lookupPattern(name: string): CodePattern | undefined {
return PATTERN_MAP.get(name);
}
export function createParamPattern(name: string): CodePattern {
// 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` };
}