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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// What: `fn group_body_start(s: &str) -> Option<usize>` returns the
// byte offset of the first character of a group's body.
// For `(body)` it is `1`; for `(?:body)` and `(?P<name>body)`
// it is the offset just after the opener metadata.
// Why: Recursion into a group body must skip the opener itself
// (`(`, `(?:`, `(?P<name>`, `(?<name>`) so the recursive
// walker sees only the body's regex syntax.
// TS map: `function groupBodyStart(s: string): number | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function groupBodyStart(s: string): number | null {
// if (s[0] !== "(") return null;
// if (s[1] !== "?") return 1;
// if (s[2] === ":") return 3;
// if (s[2] === "P" && s[3] === "<") return s.indexOf(">", 4) + 1;
// if (s[2] === "<") return s.indexOf(">", 3) + 1;
// return 1; // fallback: best-effort
// }
// ```
// What: `fn find_matching_close_paren(s: &str) -> Option<usize>`
// returns the byte index of the `)` matching the leading `(`
// in `s`. Handles nested parens, character classes (which
// don't nest but contain literal `)` as a regular char), and
// `\X` escapes.
// Why: Group skipping needs the right closing paren to advance
// past the whole group, including any nested parens.
// TS map: `function findMatchingCloseParen(s: string): number | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function findMatchingCloseParen(s: string): number | null {
// if (s[0] !== "(") return null;
// let depth = 1, i = 1;
// while (i < s.length) {
// const c = s[i];
// if (c === "\\") { i += 2; continue; }
// if (c === "[") { /* skip class */ }
// else if (c === "(") depth += 1;
// else if (c === ")") { depth -= 1; if (depth === 0) return i; }
// i += 1;
// }
// return null;
// }
// ```
// What: `fn skip_any_quantifier(s: &str) -> &str` advances past one
// leading quantifier (required OR optional) and returns the
// remainder. If no quantifier is present, returns `s`.
// Why: The new atom-skipper needs to advance past quantifiers
// regardless of required-vs-optional: the body-extracted
// literal is contributed only when the quantifier is required,
// but the walker still has to skip past optional quantifiers
// in either case so it can keep going.
// TS map: `function skipAnyQuantifier(s: string): string`.
//
// In TS you'd write (pseudocode):
// ```ts
// function skipAnyQuantifier(s: string): string { /* ... */ }
// ```
// What: `fn quantifier_is_required(s: &str) -> bool` returns true
// if the head of `s` is a quantifier whose lower bound is
// >= 1 (or there is no quantifier, treating "exactly one"
// as required).
// Why: Decides whether the body of a preceding group is required
// to appear in any match. Optional quantifiers (`?`, `*`,
// `{0}`, `{0,N}`, `{0,}`) make the group body matchable zero
// times, so its literal contributes nothing.
// TS map: `function quantifierIsRequired(s: string): boolean`.
//
// In TS you'd write (pseudocode):
// ```ts
// function quantifierIsRequired(s: string): boolean { /* ... */ }
// ```
// What: `fn skip_class_body(s: &str) -> Option<&str>` skips a single
// bracketed character class starting at the leading `[` of
// `s` and returns the remainder after the closing `]`.
// Handles a leading `^` negation and a leading `]` treated as
// a literal character (e.g. `[]abc]` matches `]ab` etc.).
// Skips `\X` escape sequences inside the class without
// interpreting them.
// Why: Resharp accepts character classes with the same syntax as
// PCRE/regex_syntax; skipping one body is a flat scan with
// no nesting (regex character classes don't nest, except via
// the resharp set-algebra `[A&&B]` form -- which this scan
// handles correctly because `&&` doesn't open a new class).
// TS map: `function skipClassBody(s: string): string | null`.
//
// In TS you'd write (pseudocode):
// ```ts
// function skipClassBody(s: string): string | null {
// // walk past `[`, optional `^`, optional immediate `]`-as-literal,
// // then characters and `\X` escapes until the matching `]`.
// }
// ```