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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Tcl and iRules: which braced arguments of a command hold a *script*.
//!
//! Neither dialect's grammar marks the difference. `{a b}` and
//! `{puts hi}` parse to one kind, so the role is recoverable only from
//! the enclosing command's leading word and the argument's position in
//! its list (grammar-dispatch §9). This module is that lookup: two
//! tables of documented Tcl 8.6 signatures, and the position arithmetic
//! [`Getter::is_value_braced_word`] and [`Getter::is_braced_literal_slot`]
//! read them through.
//!
//! It lives here rather than in `getter.rs` for the reason the module
//! above gives: the classifiers consult it, and all three of them —
//! `Getter::get_op_type_with_code`, `Checker::is_string_with_code` and
//! `Alterator::keeps_children` — must agree on the same bytes.
//!
//! [`Getter::is_value_braced_word`]: crate::getter::Getter::is_value_braced_word
//! [`Getter::is_braced_literal_slot`]: crate::getter::Getter::is_braced_literal_slot
use crate;
use crate;
/// The core Tcl-family commands that evaluate a braced argument as a
/// *script* and that neither dialect's grammar models with a node of its
/// own (#1318).
///
/// A command the grammar *does* model — `proc`, `if`, `while`,
/// `foreach`, `catch`, `try`, `namespace`, iRules' `when`, `for`,
/// `switch` and `dict for` / `dict update` / `dict with` — needs no
/// entry: its body is a child of that construct's own node rather than
/// of a generic `command`, which
/// `Getter::generic_argument_command` already answers `None` for.
/// `for` and `switch` appear here because the *Tcl* grammar models
/// neither (#467, #1264); the iRules grammar models both, so those two
/// rows are live for one dialect and inert for the other.
///
/// Each entry is a command whose documented syntax puts a script in a
/// braced argument, paired with *which* of its arguments that is. Only
/// `eval` and `for` evaluate all of them; the other six mix the two
/// roles in one argument list, and the second column is what
/// `Getter::is_braced_literal_slot` reads to tell the halves apart
/// (#1381 review). Syntax and slot are both from the Tcl 8.6 manual
/// page named in the last column.
///
/// | command | syntax | evaluated | page |
/// | --- | --- | --- | --- |
/// | `after` | `after ms ?script …?` | all but the first — argument 0 is a millisecond count or a `cancel` / `idle` / `info` subcommand word | `after(n)` |
/// | `eval` | `eval arg ?arg …?` | all | `eval(n)` |
/// | `for` | `for start test next body` | all four | `for(n)` |
/// | `on` | `on code varList script` | the last of exactly three | `try(n)` |
/// | `switch` | `switch ?options? string pattern body ?pattern body …?` | the arm bodies, or the single braced arm list | `switch(n)` |
/// | `time` | `time script ?count?` | argument 0 only | `time(n)` |
/// | `trap` | `trap pattern varList script` | the last of exactly three | `try(n)` |
/// | `uplevel` | `uplevel ?level? arg ?arg …?` | all but a leading level specifier | `uplevel(n)` |
///
/// `on` and `trap` are listed because the iRules grammar models
/// `on_handler` / `trap_handler` only *under* `try` (pinned by
/// `irules_try_handler_kinds_appear_only_under_try`), and the Tcl
/// grammar models neither a `trap` nor a second `on`; written outside
/// that shape they parse as generic commands. Their last argument is
/// the handler script. The pattern and variable list before it are
/// values, which only `Getter::is_braced_literal_slot` tells apart —
/// and since #1382 every classifier reads that answer, so their braces
/// quote rather than open. Both rows carry the clause's arity for the
/// reason [`ScriptSlots::LastOf`] gives: these are the two commands a
/// grammar that models neither can group a *following* clause into.
///
/// `after cancel {…}` and `after info {…}` are the one place the slot
/// column knowingly over-reports: those arguments identify a *pending*
/// script by its text rather than supplying one to run. Calling them
/// scripts costs nothing a reader would notice — the text is real code
/// either way — and telling them apart needs the subcommand dispatch
/// the paragraph below rules out.
///
/// `lmap varname list body` is deliberately **not** listed even though
/// its body is a script: the list is per-command, not per-argument, so
/// listing it would bill `lmap i {1 2 3} {…}`'s *list* as a block —
/// the same spelling sensitivity this rule exists to remove, and worse
/// than the one occurrence its body gives up. `for` and `switch` have
/// no such argument (`for`'s four are all evaluated, `switch`'s braced
/// argument is the arm list).
///
/// Subcommand-dispatched script takers (`dict for`, `interp eval`,
/// `trace add … {script}`) are absent for a related reason: the
/// leading word alone cannot tell `dict for` from `dict set`, and
/// admitting it would misclassify the far commoner value-taking
/// spellings. Tk callbacks (`bind`, `fileevent`, a `-command {…}`
/// option) are absent for the same reason as any user proc.
///
/// This list is the whole of the heuristic, and it is knowingly
/// incomplete — see `Getter::is_value_braced_word` for what an
/// unlisted command defaults to and why.
const SCRIPT_TAKING_COMMANDS: = ;
/// `on code varList script` and `trap pattern varList script` each take
/// exactly three arguments (Tcl 8.6 `try(n)`). See
/// [`ScriptSlots::LastOf`] for why the count is part of the rule rather
/// than a comment on it.
const HANDLER_CLAUSE_ARITY: usize = 3;
/// Named because two rules have to agree on it: `switch` takes a
/// script, and its *arm bodies* are scripts too even though the Tcl
/// grammar hangs them off a `command` named after the pattern
/// (`Getter::is_switch_arm`). Spelling it twice would let one drift.
pub const SWITCH_COMMAND: &str = "switch";
/// The `namespace` subcommands with a script argument, and which of
/// their arguments it is (Tcl 8.6 `namespace(n)`):
/// `namespace eval ns ?arg …?` evaluates everything after the namespace
/// name, `namespace inscope ns script ?arg …?` evaluates the second
/// argument and appends the rest to it as list elements, and
/// `namespace code script` has no name argument at all. Every
/// subcommand *not* listed takes values throughout — `export {pattern}`,
/// `path {ns …}`, `ensemble create -map {dict}` — which is the
/// [`ScriptSlots::NoneOfThem`] default [`namespace_script_slots`] falls
/// back to.
const NAMESPACE_SCRIPT_SUBCOMMANDS: = ;
/// The `switch` options that consume the word after them, so the
/// subject scan must step over it: `-matchvar varName` and
/// `-indexvar varName` (Tcl 8.6 `switch(n)`; both are `-regexp`-only).
/// The other four — `-exact`, `-glob`, `-regexp`, `-nocase` — are bare
/// flags and need no entry.
const SWITCH_OPTIONS_TAKING_A_WORD: = ;
/// `switch`'s end-of-options marker: whatever follows it is the
/// subject, even a subject that itself begins with `-`.
const SWITCH_OPTION_TERMINATOR: &str = "--";
/// Which of a Tcl-family construct's arguments the interpreter
/// *evaluates*. Positions count from the construct's first argument,
/// so the subcommand of a `namespace` — which shares the one
/// `word_list` with the arguments — is not one of them.
///
/// Every position a variant does not name holds a value: a millisecond
/// count, an iteration count, a namespace name, a `switch` subject or
/// pattern, a `try` handler's error code and variable list. The two
/// tables above pair one of these with each command whose signature
/// mixes the roles, and [`fills_script_slot`] applies it.
pub
/// One dialect's kind ids paired with the buffer the nodes under test
/// were parsed from.
///
/// The two are a unit: `BracedWordKinds` names ids in *this* grammar and
/// `code` must be the exact source *this* node came from, the same-parse
/// precondition [`Getter`] documents. Threading them as one value keeps
/// every rule below at one context parameter and, as with the kind table
/// itself, stops a caller pairing a node with the wrong buffer.
///
/// [`Getter`]: crate::getter::Getter
pub
/// A leading word with its global-namespace qualifier removed.
///
/// `::switch` *is* `switch`: a leading `::` names the global namespace,
/// and inside a `namespace eval` body it is the spelling that guarantees
/// the core command rather than a local proc shadowing it. Any rule that
/// resolves a leading word against a table of core command names has to
/// normalise through here, or the score moves with how the author spelled
/// a command that resolves identically. Four metrics read the leading
/// word without the slot table — cognitive, cyclomatic, ABC and nexits —
/// and each scored the qualified spelling as an ordinary call until this
/// was shared (#1381 review).
///
/// Only the *leading* qualifier is stripped. `ns::eval` is a different
/// command living in `ns` and must not be mistaken for the core one;
/// `BRACED_WORD_VALUE_CASES` in `src/metrics/halstead.rs` pins that
/// direction, and stripping every `::` segment would promote it.
/// The [`ScriptSlots`] of a generic command named `command`, or `None`
/// when no core command of that name evaluates a braced argument.
pub
/// The [`ScriptSlots`] of the subcommand a `namespace` construct's
/// `word_list` names — its first entry, before the arguments proper.
///
/// `None` for a subcommand that is not a plain word
/// (`namespace $sub {…}`), which is unresolvable and keeps the script
/// answer, exactly as an unresolvable command name does.
pub
/// One construct's argument layout: which slots hold a script, and
/// where in the shared `word_list` its arguments begin. The two always
/// travel together and are both plain scalars, so they ride in one
/// value rather than as two same-shaped parameters.
pub
/// Whether `arguments`' own slot sequence can be read positionally — the
/// precondition every rule below rests on (#1381 review).
///
/// The obvious spelling, `owner.has_error()`, is wrong and was measured
/// wrong: [`Node::has_error`] is transitive, so a stray `]` buried in one
/// argument's *script* withdrew the layout from every sibling.
/// `after {100} {puts ]}` lost `{100}`, and the dump then rendered it as
/// a fabricated `command` named `100` — the defect the slot table exists
/// to remove. An error *inside* an argument moves no sibling's index; an
/// `ERROR` or MISSING token holding a slot of its own moves every index
/// after it, and that is the only shape that makes a position
/// meaningless.
///
/// `has_error` stays as the `O(1)` pre-filter, so a well-formed command —
/// every command in a file that parses — never pays for the scan, and
/// the scan is bounded by the argument count when it does.
///
/// [`Node::has_error`]: crate::Node::has_error
pub
/// Whether the braced `word` fills a slot of `arguments` that `roles`
/// names as evaluated.
///
/// A word whose index cannot be resolved keeps the construct-wide
/// script answer, which is what every caller had before the slot table
/// existed.
pub
/// Whether a braced argument 0 of `uplevel` reads as a level specifier
/// rather than as the first word of the script.
///
/// `uplevel` decides this from the argument's *value*, so the test is
/// on the text between the braces: an optional `#` prefix and then an
/// integer, whitespace-tolerant because `Tcl_GetInt` is (Tcl 8.6
/// `uplevel(n)`). `uplevel {set x 2}` has no level and must stay whole.
///
/// Unreadable text and text that is not brace-delimited share one exit
/// because neither is a level and the caller cannot tell them apart
/// anyway: `node_text` answers `None` for non-UTF-8 bytes, which
/// `Ast::parse` accepts, and a caller reaching this with a node that is
/// not a `braced_word` has no level either.
/// Whether the argument at `index` of a `switch` command is one of the
/// arms it evaluates, rather than an option, an option's operand, the
/// subject, or a pattern.
/// The index of a `switch` command's subject among `arguments`: the
/// first word that is neither a leading option nor an option's operand,
/// or the word after a `--` terminator.
///
/// The scan stops at the first argument that is not a plain option
/// word, and a braced word never is one, so it ends at or before
/// whichever braced word the caller is asking about — its length is the
/// leading-option count, not the argument count. Every braced argument
/// of a wide one-line arm list asks, so an unbounded scan here would be
/// the quadratic `is_switch_arm_body` documents (#1381 review). It runs
/// over the caller's cursor for the same reason.
///
/// # The option list is resolved statically, and Tcl resolves it at run time
///
/// A word that is not a `simple_word` is taken to be the subject. That
/// is right for the overwhelmingly common `switch $v {…}` and for a
/// braced subject, and wrong for a legal but vanishingly rare computed
/// *option*: Tcl substitutes each word before dispatching, so
/// `switch "-exact" $v a {…}` and `switch $opt $v a {…}` really do pass
/// an option there. Taken as the subject, every later index shifts by
/// one and the pattern/body parity below inverts — the bodies read as
/// literals and the patterns as scripts. The same static-resolution
/// limit `command_leading_word` documents for a command *name*, and
/// answered the same way: guessing keeps the common form right, where
/// returning `None` would surrender `switch $v` to the construct-wide
/// script answer.