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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Rust-only utility (NOT a port — lives outside `src/ported/` by design).
//!
//! C resolves the quoting inside a `[...]` subscript by RE-LEXING the
//! subscript text: `getindex` (Src/params.c:2022) calls
//! `parse_subscript(s, scanflags & SCANPM_DQUOTED, ']')` at c:2029, which
//! untokenizes the text and pushes it back through the lexer
//! (`dquote_parse`, Src/lex.c:1751-1769). zshrs has no equivalent step on
//! that path — its `lex::parse_subscript` port throws away the tokenized
//! text C copies back at c:Src/lex.c:1772, and re-entering the real lexer
//! from inside paramsubst (and from the compiler, which resolves literal
//! assignment keys at compile time) would mean re-entrant lexer state on
//! the hottest expansion path.
//!
//! So the three C stages that decide what a backslash inside a subscript
//! MEANS — `dquote_parse`'s backslash arm, `getarg`'s marker disposition,
//! and the `remnulargs` / `parsestr` + `singsub` round that follows — are
//! expressed here as the one string transform they add up to. Each rule
//! cites the C line it comes from; see [`subscript_unescape`].
//!
//! Both callers are the "what key is this?" sites:
//! * `ported::subst::paramsubst` — `${A[\[k\]]}` (read)
//! * `extensions::compile_zsh::compile_assign` — `A[\[k\]]=v` (store)
use crate;
/// Backslash disposition inside a `[...]` subscript — the net effect of
/// the re-lex C runs on a subscript's SOURCE text.
///
/// `getindex` NEVER reads the subscript the way the outer lexer left it.
/// It calls `parse_subscript(s, scanflags & SCANPM_DQUOTED, ']')`
/// (c:Src/params.c:2029), and `parse_subscript` untokenizes the text and
/// re-lexes it through `dquote_parse(']', sub)`
/// (c:Src/lex.c:1751-1769 — `untokenize(t = dupstring_wlen(s, l));
/// inpush(t, 0, NULL); … err = dquote_parse(endchar, sub);`). That
/// re-lex is where a backslash inside a subscript acquires its meaning:
///
/// ```text
/// c:Src/lex.c:1497-1512
/// if (c != '\n') {
/// if (c == '$' || c == '\\' || (c == '}' && !intick && bct) ||
/// c == endchar || c == '`' ||
/// (endchar == ']' && (c == '[' || c == ']' ||
/// c == '(' || c == ')' ||
/// c == '{' || c == '}' ||
/// (c == '"' && sub))))
/// add(Bnull);
/// else {
/// /* lexstop is implicitly handled here */
/// add('\\');
/// goto cont;
/// }
/// } else if (sub || unset(CSHJUNKIEQUOTES) || endchar != '"')
/// continue;
/// ```
///
/// With `endchar == ']'` a backslash before one of ``$ \ ` ] [ ( ) { }``
/// (plus `"` when the subscript is inside double quotes, `sub`) becomes
/// the `Bnull` marker + the literal char; a backslash before ANY other
/// char stays a literal backslash. That asymmetry is exactly why
/// `A[\[k\]]` keys on `[k]` while `A[a\ b]` / `A[a\*b]` keep theirs.
/// Backslash-newline is dropped outright (c:1513).
///
/// `getarg` then disposes of the markers (c:Src/params.c:1538-1551):
///
/// ```text
/// if (inull(c)) {
/// c = t[1];
/// if (c == '[' || c == ']' || c == '(' || c == ')' ||
/// c == '{' || c == '}') {
/// if (ishash && i) *t = ztokens[*t - Pound];
/// needtok = 1; ++t;
/// } else if (c != '"')
/// *t = ztokens[*t - Pound];
/// continue;
/// }
/// ```
///
/// — a marker before a bracket/paren/brace (or before `"`) is KEPT and
/// later DELETED by `remnulargs` (c:1583-1584, hash key path), so the
/// escaped bracket reaches the hash table bare. Every other marker is
/// untokenized back to a literal `\` (`ztokens[Bnull - Pound]` is `\`,
/// c:Src/lex.c:38), which the `parsestr` + `singsub` round at
/// c:1585-1593 re-marks and drops one stage later — so ``\$``, `\\` and
/// ``\` `` also lose their backslash, just further down the pipeline.
///
/// zshrs has no equivalent re-lex step on this path (its
/// `lex::parse_subscript` discards the tokenized text C copies back at
/// c:Src/lex.c:1772), so a source-literal backslash reached the assoc
/// key verbatim: `A[\[k\]]=v` stored the 5-char key `\[k\]` where zsh
/// stores `[k]`. This function is that missing step, expressed as the
/// composite string transform the three C stages add up to.
///
/// * `sub` — C's `SCANPM_DQUOTED`: the subscript sits inside `"…"`.
/// * `resolve_dollar` — the caller has NO `parsestr`/`singsub` round
/// after this call (compile-time literal key), so apply that stage's
/// share of the work here as well.
///
/// Returns the rewritten text and whether an UNESCAPED `$` / `` ` ``
/// (i.e. a live expansion, which C resolves in `singsub` at c:1592)
/// is still present.
/// Same C stages as [`subscript_unescape`], stopped one step earlier and
/// re-encoded for a caller that still has to run C's `parsestr` + `singsub`
/// round (c:Src/params.c:1585-1592) through the word compiler.
///
/// [`subscript_unescape`] returns PLAIN text, which is right for a key the
/// caller stores verbatim but wrong for a key that still holds a live
/// expansion: its resolved `$` would be re-expanded by the word compiler and
/// its now-bare `[` would be read as a glob. C never has that problem because
/// its intermediate text is MARKED — `getarg` keeps the `Bnull` before a
/// bracket (c:1541-1548) and writes a literal `\` before the others
/// (c:1549-1550), and `parsestr` re-marks those (c:1588) before `singsub`
/// expands what is left. zshrs's word compiler consumes the same lexer
/// encoding, so emit the marker directly and let it do `singsub`'s job:
///
/// | source | C intermediate | emitted here |
/// |--------|---------------------------------------|--------------|
/// | `\[` `\]` `\(` `\)` `\{` `\}` (and `\"` when `sub`) | marker kept (c:1547), deleted by `remnulargs` (c:1583) | `Bnull` + char |
/// | `\$` `\\` `` \` `` | marker → `\` (c:1550), re-marked by `parsestr` (c:1588), dropped by `singsub`'s `prefork`/`remnulargs` (c:Src/subst.c:169) | `Bnull` + char |
/// | any other `\X` | ordinary text (c:Src/lex.c:1510 `add('\\')`) — survives BOTH re-lexes because the second one runs with `endchar == '\0'` and never marks `X` | `\` + char |
/// | `\` + newline | dropped (c:Src/lex.c:1513) | — |
/// | everything else | untouched | verbatim |
///
/// An unescaped `$` / `` ` `` is deliberately left live — that is exactly the
/// work c:1592 `singsub` still has to do.
///
/// * `sub` — C's `SCANPM_DQUOTED`: the subscript sits inside `"…"`.
/// !!! WARNING: RUST-ONLY HELPER !!!
/// C has no separate function here: this is the scan loop that OPENS
/// `getarg` (c:Src/params.c:1533-1541), lifted out because the Rust
/// paramsubst expands the whole subscript in one place and then needs
/// to know where C would have cut it.
///
/// c:Src/params.c:1533-1541 —
/// for (t = s, i = 0;
/// (c = *t) &&
/// ((c != Outbrack && (ishash || c != ',')) || i || inpar);
/// t++) {
/// /* Untokenize inull() except before brackets and double-quotes */
/// if (inull(c)) { c = t[1]; if (c == '[' || … ) { … ++t; } … continue; }
/// if (c == '[' || c == Inbrack) i++;
/// else if (c == ']' || c == Outbrack) i--;
/// if (c == '(' || c == Inpar) inpar++;
/// else if (c == ')' || c == Outpar) inpar--;
/// …
/// }
///
/// Returns the two RAW (still unexpanded) argument texts when the scan
/// stopped on a top-level range comma, else None (one argument only).
/// `ishash` mirrors C's `ishash` gate: for a hash, `,` is an ordinary
/// key byte and never terminates the argument.
/// !!! WARNING: RUST-ONLY HELPER !!!
/// Resolve "is this subscript a range, and what are its bounds?" using
/// the parse-time decision recorded by `subscript_arg_split` when one is
/// available (c:Src/params.c:1533-1536 — C splits BEFORE expanding), and
/// falling back to a depth-0 comma scan of the already-expanded text for
/// the reference paths that do not record one.
/// !!! WARNING: RUST-ONLY HELPER !!!
/// Inverse of [`subscript_unescape`]'s marked set, for the one place the port
/// has to hand an ALREADY-EXPANDED key back through a text subscript.
///
/// c:Src/subst.c:3312-3316 — the `${name[key]=value}` family assigns with
/// *idend = '\0';
/// Param pm = setsparam(idbeg, ztrdup(val));
/// i.e. C re-parses the flat `name[key]` text too. That is sound in C because
/// `idbeg` still holds the LEXER's spelling, where a `]` inside the key is a
/// `Bnull`-marked byte and cannot close the subscript. zshrs's paramsubst has
/// already resolved the subscript to plain text by then (`expand_sub_arg`), so
/// the rebuilt string `B[\\]]` re-parsed as key `\` — the assignment landed on
/// the wrong key and the read-back came up empty (D06subscript.ztst
/// "Associative array substitution-assignment with reverse pattern subscript
/// key"). Re-apply the escaping the re-parse will strip, exactly over the set
/// c:Src/lex.c:1501-1506 marks for `endchar == ']'`, so the round trip is the
/// identity.
///
/// Returns the input untouched for a FLAG-GROUP subscript (`(r)pat`), whose
/// parentheses are structure rather than data.
/// !!! WARNING: RUST-ONLY HELPER !!!
/// Classification of ONE subscript operand — a range bound (`${a[lo,hi]}`)
/// or a chained subscript (`${a[lo,hi][SUB]}`) — whose text may open with a
/// `(...)` flag group.
///
/// C has no such function: `getarg` (c:Src/params.c:1367) parses the flags,
/// runs the search and returns the index all in one pass, writing its
/// side-effects back through `Value *v` / `int *inv` out-parameters. zshrs's
/// `ported::params::getarg` returns the matched ELEMENT for `r`/`R` and the
/// INDEX for `i`/`I` (see `getarg_out`), and it has no `Value` to record
/// `v->isarr |= SCANPM_WANTVALS` in — so the two facts every bound consumer
/// needs (the match POSITION and whether WANTVALS was raised) are recovered
/// here in one place instead of being re-derived at each call site.
/// !!! WARNING: RUST-ONLY HELPER !!!
/// Classify one subscript operand against `arr` — see [`SubscriptBound`].
///
/// c:Src/params.c getindex — a bound with a search-flag subscript
/// (`(r)pat`/`(i)pat`) yields the INDEX of the match (the `*inv`/`*w` path),
/// not the value: `${a[(r)3,(r)5]}` slices between the matched positions.
/// `getarg` returns the value for `r`/`R` but the index for `i`/`I`. `r` is a
/// FORWARD first-match (c:1411 `down = 0`), `R` a REVERSE last-match (c:1416
/// `down = 1`), so map `r`→`i` / `R`→`I` to get the matching index in the SAME
/// direction — preserving forward/reverse for duplicate matches and the
/// no-match returns (forward no-match → len+1, reverse → 0).