kimun 0.34.1

Code metrics tool — health score, complexity, duplication, hotspots, ownership
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
/// Per-language tokenization rules for Halstead complexity metrics.
///
/// Each language defines three token categories:
/// - **operator_keywords**: control flow keywords that represent decisions
///   or actions (e.g. `if`, `return`, `for`). These count as operators in
///   Halstead's model because they direct program execution.
/// - **operator_symbols**: symbolic operators ordered longest-first so that
///   multi-character operators (`>>=`, `&&`) are matched before their
///   single-character prefixes (`>`, `&`). Longest-match ordering is
///   critical for correct tokenization.
/// - **ignored_keywords**: declarations, modifiers, and type names that
///   are structural rather than computational. These are excluded from
///   both operator and operand counts to focus the metric on logic
///   complexity rather than syntactic boilerplate.
///
/// The classification of a keyword as operator vs. ignored depends on
/// the language semantics. For example, `as` is an operator in Rust
/// (type cast — a computation) but ignored in Python (aliasing — a
/// declaration). Each language's doc comment explains such distinctions.
use super::tokenizer::TokenRules;

// ── Language rules ──────────────────────────────────────────────────────

/// Rust tokenization rules.
///
/// `as` is classified as an operator because it performs a type cast (a
/// runtime computation), unlike Python where `as` merely creates an alias.
/// `loop` is an operator (unconditional looping control flow). Lifetime
/// and ownership keywords (`mut`, `ref`, `move`) are ignored because they
/// are declarative annotations rather than executable operations.
pub static RUST: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "else", "match", "for", "while", "loop", "return", "break", "continue", "as", "in",
    ],
    operator_symbols: &[
        "..=", "...", "=>", "->", "&&", "||", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=", "%=",
        "&=", "|=", "^=", "<<=", ">>=", "<<", ">>", "::", "..", "+", "-", "*", "/", "%", "&", "|",
        "^", "!", "<", ">", "=", ";", ",", ".", ":", "(", ")", "[", "]", "{", "}", "?", "#",
    ],
    ignored_keywords: &[
        "fn", "let", "mut", "const", "static", "extern", "unsafe", "async", "await", "move", "ref",
        "impl", "struct", "enum", "trait", "type", "use", "mod", "where", "pub", "crate", "super",
        "self", "Self", "dyn", "i8", "i16", "i32", "i64", "i128", "isize", "u8", "u16", "u32",
        "u64", "u128", "usize", "f32", "f64", "bool", "char", "str", "String", "Vec", "Option",
        "Result", "Box",
    ],
};

/// Python tokenization rules.
///
/// `as` is ignored here (unlike Rust) because Python's `as` is used for
/// aliasing in imports (`import x as y`) and context managers (`with x as y`),
/// which are declarative rather than computational. Boolean operators
/// (`and`, `or`, `not`, `is`) are keywords rather than symbols in Python.
/// `with` is an operator because it controls resource management flow.
pub static PYTHON: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "elif", "else", "for", "while", "return", "yield", "try", "except", "finally",
        "raise", "assert", "del", "break", "continue", "in", "not", "and", "or", "is", "with",
    ],
    operator_symbols: &[
        "**=", "//=", ">>=", "<<=", "**", "//", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=",
        "%=", "&=", "|=", "^=", "<<", ">>", "->", "+", "-", "*", "/", "%", "&", "|", "^", "~", "<",
        ">", "=", ";", ",", ".", ":", "(", ")", "[", "]", "{", "}", "@",
    ],
    ignored_keywords: &[
        "def", "class", "import", "from", "as", "lambda", "global", "nonlocal", "async", "await",
        "pass", "int", "float", "str", "bool", "list", "dict", "tuple", "set", "type",
    ],
};

/// JavaScript/TypeScript tokenization rules.
///
/// Shared between JS and TS since their operator sets are identical.
/// `typeof` and `instanceof` are operators (runtime type checks).
/// `yield` is an operator (generator control flow). The nullish
/// coalescing operator `??` is included alongside `&&` and `||`.
pub static JAVASCRIPT: TokenRules = TokenRules {
    operator_keywords: &[
        "if",
        "else",
        "for",
        "while",
        "do",
        "switch",
        "case",
        "default",
        "return",
        "break",
        "continue",
        "throw",
        "try",
        "catch",
        "finally",
        "new",
        "delete",
        "typeof",
        "instanceof",
        "in",
        "of",
        "yield",
    ],
    operator_symbols: &[
        "===", "!==", ">>>", "**=", ">>=", "<<=", "=>", "&&", "||", "??", "==", "!=", "<=", ">=",
        "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "**", "<<", ">>", "++", "--", "+", "-",
        "*", "/", "%", "&", "|", "^", "~", "!", "<", ">", "=", ";", ",", ".", ":", "(", ")", "[",
        "]", "{", "}", "?",
    ],
    ignored_keywords: &[
        "function",
        "var",
        "let",
        "const",
        "class",
        "extends",
        "import",
        "export",
        "async",
        "await",
        "undefined",
        "void",
    ],
};

/// Go tokenization rules.
///
/// `go` (goroutine launch), `defer` (deferred execution), and `select`
/// (channel multiplexing) are operators because they control execution
/// flow. `range` is an operator (iteration control). Go has no
/// ternary operator, and `:=` (short variable declaration) is included
/// as an operator symbol because it combines declaration with assignment.
pub static GO: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "else", "for", "switch", "case", "default", "return", "break", "continue", "go",
        "defer", "select", "range",
    ],
    operator_symbols: &[
        ":=", "&&", "||", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=",
        "<<=", ">>=", "<<", ">>", "<-", "++", "--", "+", "-", "*", "/", "%", "&", "|", "^", "!",
        "<", ">", "=", ";", ",", ".", ":", "(", ")", "[", "]", "{", "}",
    ],
    ignored_keywords: &[
        "func",
        "var",
        "const",
        "type",
        "struct",
        "interface",
        "import",
        "package",
        "map",
        "chan",
        "int",
        "int8",
        "int16",
        "int32",
        "int64",
        "uint",
        "uint8",
        "uint16",
        "uint32",
        "uint64",
        "float32",
        "float64",
        "bool",
        "string",
        "byte",
        "rune",
        "error",
    ],
};

/// C-family tokenization rules (C, C++, Java, C#, Objective-C, PHP, Dart).
///
/// Uses a shared operator/keyword set covering the common subset. The
/// `goto` keyword is included for C/C++ but is harmless for languages
/// that don't support it — an unrecognized keyword simply won't match
/// any source tokens. `sizeof` is an operator (compile-time computation).
/// The `->` (member access) and `::` (scope resolution) symbols are
/// included because they represent operand access operations.
pub static C_FAMILY: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "else", "for", "while", "do", "switch", "case", "default", "return", "break",
        "continue", "goto", "sizeof", "new", "delete", "throw", "try", "catch",
    ],
    operator_symbols: &[
        "->", "::", "&&", "||", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=", "%=", "&=", "|=",
        "^=", "<<=", ">>=", "<<", ">>", "++", "--", "+", "-", "*", "/", "%", "&", "|", "^", "~",
        "!", "<", ">", "=", ";", ",", ".", ":", "(", ")", "[", "]", "{", "}", "?", "#",
    ],
    ignored_keywords: &[
        "typedef",
        "struct",
        "union",
        "enum",
        "class",
        "namespace",
        "using",
        "template",
        "virtual",
        "override",
        "int",
        "long",
        "short",
        "char",
        "float",
        "double",
        "void",
        "unsigned",
        "signed",
        "bool",
        "auto",
        "const",
        "static",
        "extern",
        "register",
        "volatile",
        "inline",
        "public",
        "private",
        "protected",
    ],
};

/// Ruby tokenization rules.
///
/// Block delimiters (`do`, `end`, `begin`) are operators because they
/// demarcate executable blocks, unlike braces in C-family languages which
/// are purely syntactic. `yield` passes control to a block (operator).
/// `unless` and `until` are operators (negated conditionals/loops).
/// The spaceship operator `<=>` and case equality `===` are included.
pub static RUBY: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "elsif", "else", "unless", "case", "when", "while", "until", "for", "do", "begin",
        "rescue", "ensure", "raise", "return", "break", "next", "yield", "end", "then", "in",
        "and", "or", "not",
    ],
    operator_symbols: &[
        "<=>", "===", "**=", "&&=", "||=", "=>", "&&", "||", "==", "!=", "<=", ">=", "+=", "-=",
        "*=", "/=", "%=", "**", "<<", ">>", "..", "+", "-", "*", "/", "%", "&", "|", "^", "~", "!",
        "<", ">", "=", ";", ",", ".", ":", "(", ")", "[", "]", "{", "}", "?", "@",
    ],
    ignored_keywords: &[
        "def",
        "class",
        "module",
        "require",
        "include",
        "extend",
        "attr_reader",
        "attr_writer",
        "attr_accessor",
    ],
};

/// Kotlin tokenization rules.
///
/// `as` is an operator (type cast, like Rust). `when` replaces `switch`
/// and is an operator (pattern-matching control flow). `is` performs
/// runtime type checks (operator). Kotlin visibility modifiers (`internal`,
/// `open`, `sealed`, `companion`) are ignored as declarative annotations.
/// The Elvis operator `?:` is included as a null-handling operator.
pub static KOTLIN: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "else", "when", "for", "while", "do", "return", "break", "continue", "throw", "try",
        "catch", "finally", "is", "as", "in",
    ],
    operator_symbols: &[
        "?:", "&&", "||", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=", "%=", "->", "::", "++",
        "--", "..", "+", "-", "*", "/", "%", "!", "<", ">", "=", ";", ",", ".", ":", "(", ")", "[",
        "]", "{", "}", "?", "@",
    ],
    ignored_keywords: &[
        "fun",
        "val",
        "var",
        "class",
        "object",
        "interface",
        "import",
        "package",
        "private",
        "public",
        "protected",
        "internal",
        "open",
        "override",
        "abstract",
        "data",
        "sealed",
        "companion",
        "Int",
        "Long",
        "Short",
        "Byte",
        "Float",
        "Double",
        "Boolean",
        "Char",
        "String",
        "Unit",
        "Any",
        "Nothing",
    ],
};

/// Swift tokenization rules.
///
/// `as` is an operator (type cast). `guard` is an operator (early exit
/// pattern — enforces conditions at function boundaries). `repeat` is
/// Swift's do-while equivalent. The half-open range `..<` and closed
/// range `...` operators are included. `fileprivate` is ignored as a
/// visibility modifier with no computational effect.
pub static SWIFT: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "else", "guard", "switch", "case", "default", "for", "while", "repeat", "return",
        "break", "continue", "throw", "try", "catch", "as", "is", "in",
    ],
    operator_symbols: &[
        "&&", "||", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=", "%=", "->", "..<", "...", "??",
        "+", "-", "*", "/", "%", "&", "|", "^", "~", "!", "<", ">", "=", ";", ",", ".", ":", "(",
        ")", "[", "]", "{", "}", "?", "@",
    ],
    ignored_keywords: &[
        "func",
        "let",
        "var",
        "class",
        "struct",
        "enum",
        "protocol",
        "extension",
        "import",
        "where",
        "private",
        "public",
        "internal",
        "open",
        "fileprivate",
        "override",
        "mutating",
        "static",
        "Int",
        "Float",
        "Double",
        "Bool",
        "String",
        "Character",
        "Void",
        "Any",
        "Self",
    ],
};

/// Shell (Bash/Zsh/sh) tokenization rules.
///
/// Shell control structures use keyword pairs (`if/then/fi`,
/// `for/do/done`, `case/esac`) — all keywords in each pair are operators
/// because they jointly control execution flow. The `$` symbol is an
/// operator because it triggers variable expansion (a runtime operation).
/// `;;` (case terminator) is a shell-specific operator symbol.
pub static SHELL: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "then", "elif", "else", "fi", "for", "while", "until", "do", "done", "case", "esac",
        "in", "return", "exit", "break", "continue",
    ],
    operator_symbols: &[
        "&&", "||", "==", "!=", "<=", ">=", ">>", "<<", ";;", "|", "&", ";", "<", ">", "=", "(",
        ")", "[", "]", "{", "}", "!", "$",
    ],
    ignored_keywords: &[
        "function", "local", "export", "readonly", "declare", "eval", "exec", "source",
    ],
};

/// Kaikai tokenization rules.
///
/// Functional language with algebraic effects. Control flow keywords
/// (`if`, `match`, `case`, `when`, `handle`, `with`) and boolean keywords
/// (`and`, `or`, `not`) are operators. `as` is an operator (capability/import
/// alias binding). Pipeline operators (`|>`, `||`, `|?`, `|`) drive
/// dataflow. `:=` is mutable cell write, `++` is concat, `=>` lambda arrow,
/// `->` type/pattern arrow. Declarations (`fn`, `let`, `var`, `type`,
/// `effect`, `protocol`, `impl`, `import`, `use`, visibility) are ignored.
pub static KAIKAI: TokenRules = TokenRules {
    operator_keywords: &[
        "if", "else", "match", "case", "when", "handle", "with", "and", "or", "not", "as",
    ],
    operator_symbols: &[
        "|>", "|?", "...", "++", "==", "!=", "<=", ">=", ":=", "=>", "->", "||", "..", "+", "-",
        "*", "/", "%", "^", "|", "!", "<", ">", "=", ";", ",", ".", ":", "(", ")", "[", "]", "{",
        "}", "@", "?",
    ],
    ignored_keywords: &[
        "fn", "let", "var", "const", "type", "effect", "protocol", "impl", "import", "use", "pub",
        "priv", "extern", "test", "bench", "axiom", "check", "where", "requires", "ensures",
        "assert", "unit", "true", "false", "Int", "Real", "Bool", "String", "Char", "Unit",
    ],
};

/// Look up the tokenization rules for a language by name.
/// Returns `None` for unsupported languages (no Halstead analysis available).
pub fn rules_for(language: &str) -> Option<&'static TokenRules> {
    match language {
        "Rust" => Some(&RUST),
        "Python" => Some(&PYTHON),
        "JavaScript" | "TypeScript" => Some(&JAVASCRIPT),
        "Go" => Some(&GO),
        "C" | "C++" | "C#" | "Java" | "Objective-C" | "PHP" | "Dart" => Some(&C_FAMILY),
        "Ruby" => Some(&RUBY),
        "Kotlin" => Some(&KOTLIN),
        "Swift" => Some(&SWIFT),
        "Bourne Shell" | "Bourne Again Shell" | "Zsh" => Some(&SHELL),
        "Kaikai" => Some(&KAIKAI),
        _ => None,
    }
}