big_code_analysis_ast/getter/irules.rs
1//! `Getter` implementation for iRules.
2#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
3
4use super::*;
5
6use crate::lang_helpers::irules::BRACED_WORD_KINDS;
7
8impl Getter for IrulesCode {
9 // The mapping is the `opens` annotations of `roles/irules.rs`
10 // (#1696). The rows read no bytes and no chain, so the byte-less
11 // spelling forwards with none.
12 impl_byte_less_space_kind!(irules);
13
14 fn get_space_kind_with_code<'a>(
15 node: &Node<'a>,
16 code: &[u8],
17 ancestors: Ancestors<'a, '_>,
18 ) -> SpaceKind {
19 crate::roles::irules::opens_space(node, code, ancestors)
20 }
21
22 keyword_roles!(irules);
23
24 paying_roles!(irules);
25
26 fn get_op_type<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> TokenRole {
27 match node.kind_id().into() {
28 // The braced-word rule (#1314, #1354 / #1317) — the twin of
29 // the Tcl arm, which carries the derivation and every
30 // caveat, over the shared
31 // `Getter::is_subsumed_braced_word`. The same split holds
32 // at this grammar's own ids: handler and `if` bodies are
33 // `BracedWord` (132), conditions are `Expr` (141), and only
34 // the value form is `BracedWordSimple` (133), which admits
35 // the same six child kinds Tcl's does.
36 _ if Self::is_subsumed_braced_word(node, ancestors, &BRACED_WORD_KINDS) => {
37 TokenRole::Unknown
38 }
39 // Anonymous keyword tokens (the `*2` aliases are the keyword
40 // literals; the unsuffixed high-id variants are the statement
41 // nodes counted by the branching metrics, not here). The
42 // declaration keywords (`proc`, `when`, `namespace`, `set`,
43 // `global`) are not here: `roles/irules.rs` declares them
44 // and `get_keyword_role` bills them (#1693).
45 Irules::On
46 | Irules::Off
47 | Irules::Trap
48 | Irules::If2
49 | Irules::Elseif2
50 | Irules::Else2
51 | Irules::While2
52 | Irules::For2
53 | Irules::Foreach2
54 | Irules::Switch2
55 | Irules::Try2
56 | Irules::Catch2
57 | Irules::Finally2
58 | Irules::Regexp2
59 | Irules::Expr2
60 | Irules::Dict
61 | Irules::Update
62 | Irules::With
63 // The comparison and short-circuit operators — symbolic,
64 // the string comparators (`eq`, `contains`, `matches`, …) and
65 // the word forms `and` / `or` — are not here: the `Comparison`
66 // and `ShortCircuit` rows of `roles/irules.rs` pay them and
67 // `get_paying_role` bills them (#1695).
68 // Structural punctuation. Only the *opening* delimiter is an
69 // operator (the pair folds to one glyph in
70 // `get_operator_id_as_str`); counting the matching closer too
71 // (former `RBRACE`/`RBRACK`/`RPAREN` arms) double-counted every
72 // balanced pair, inflating n1/N1 (#695).
73 // `LPAREN2` is defensive — the runtime collapses it to `LPAREN`
74 // before `kind_id()`, so it never fires (#768; see the Cpp note).
75 | Irules::LBRACE
76 | Irules::LBRACK
77 | Irules::LPAREN
78 | Irules::LPAREN2
79 | Irules::SEMI
80 | Irules::COLON
81 | Irules::COLONCOLON
82 | Irules::NsDelim
83 // Arithmetic / exponent operators.
84 | Irules::PLUS
85 | Irules::DASH
86 | Irules::STAR
87 | Irules::SLASH
88 | Irules::PERCENT
89 | Irules::STARSTAR
90 // Bitwise operators.
91 | Irules::AMP
92 | Irules::PIPE
93 | Irules::CARET
94 | Irules::TILDE
95 | Irules::LTLT
96 | Irules::GTGT
97 // Logical negation, symbolic and keyword. The ternary `?` is
98 // the `Ternary` row's and `get_paying_role` bills it (#1695).
99 | Irules::BANG
100 | Irules::Not => TokenRole::Operator,
101
102 // `Id` (named, id 49) is a standalone identifier operand — e.g.
103 // a `set` target (`set s …` → `s`). But it is ALSO the inner
104 // leaf of every `variable_substitution` (`$s` → `(variable_
105 // substitution (id "s"))`). The `VariableSubstitution` node is
106 // already the operand for the reference, so counting its inner
107 // `Id` too would double-count every `$var` (inflating n2/N2 and
108 // every derived Halstead value). Exclude `Id` exactly in that
109 // position. NB: Tcl has the identical shape — its anonymous
110 // `Id2` token is emitted both as the `set` target and as the
111 // var-sub leaf, so it needs this same parent guard; the blanket
112 // `Id2` kind exclusion it used to carry dropped every `set`
113 // target from n2/N2 (#1294). (`Id2` here is a different,
114 // non-surfacing token.)
115 Irules::Id => {
116 if ancestors.parent_has_kind(node, Irules::VariableSubstitution as u16) {
117 TokenRole::Unknown
118 } else {
119 TokenRole::Operand
120 }
121 }
122
123 // Operands: identifiers and literals.
124 //
125 // `BracedWord` (132) reaches this arm only when the guard
126 // above let it through, which since #1354 means only when
127 // it holds no command — the reason the Tcl twin spells
128 // out. A `when` handler body is the largest instance of the
129 // over-count that removed: its operand text was the entire
130 // event handler.
131 //
132 // `ArrayIndex` is deliberately absent, as in the Tcl twin:
133 // the grammar makes `(k)` a pure wrapper around one of the
134 // six word kinds, and its parens are already the `()`
135 // operator, so listing it billed `$arr(k)` three times —
136 // the reference, the wrapper and the index (grammar-
137 // dispatch §5). The reference and the index are the two
138 // operands, the index being walked because Tcl substitutes
139 // inside the parens (`$arr($i)`), unlike inside braces.
140 // role-bypass: allow(StringLit) — the operand classification of the braced words
141 // and of the substitution the row `wraps`, a projection the row does not answer
142 Irules::SimpleWord
143 | Irules::Number
144 | Irules::Boolean
145 | Irules::EventName
146 | Irules::BracedWord
147 | Irules::BracedWordSimple
148 | Irules::VariableSubstitution => TokenRole::Operand,
149
150 // Double-quoted strings count as a single operand when inert
151 // (`"hello world"`). When they carry a `$var` or `[cmd]`
152 // interpolation child, the inner substitution nodes are walked
153 // separately and contribute their own operands; counting the
154 // wrapping `QuotedWord` too would double-count `N2` (same pattern
155 // as Tcl #277 / Bash #180 / C# #183 / PHP #184).
156 // role-bypass: allow(StringLit) — the operand classification of the literals
157 // under the row's `wraps` list, a projection the row does not answer
158 Irules::QuotedWord => {
159 Self::string_operand_type(node, crate::roles::irules::string_lit::WRAPS)
160 }
161
162 _ => TokenRole::Unknown,
163 }
164 }
165
166 // The byte-reading half of the braced-word rule (#1318) — the twin
167 // of the Tcl override, which carries the derivation. This grammar
168 // models more of the script positions than Tcl's does (`for`,
169 // `switch` with `switch_arm` children, `when`, and the three `dict`
170 // loops all have nodes of their own), so fewer braced words reach
171 // the command-name recognition here; the ones that do — `eval`,
172 // `uplevel`, `after`, `time` and every user proc — behave exactly
173 // as they do in Tcl. Two entries in that list exist for this
174 // dialect alone: `on_handler` / `trap_handler` are emitted only
175 // under `try` (pinned by
176 // `irules_try_handler_kinds_appear_only_under_try`), so a
177 // statement-level `on error {…}` or `trap {…}` is a generic
178 // command and needs its name recognised.
179 fn get_op_type_with_code<'a>(
180 node: &Node<'a>,
181 code: &[u8],
182 ancestors: Ancestors<'a, '_>,
183 ) -> TokenRole {
184 Self::braced_word_op_type(node, code, ancestors, &BRACED_WORD_KINDS)
185 }
186
187 get_operator!(Irules);
188}