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
//! Tests for zero-copy token implementation.
//!
//! ## Root Cause
//!
//! Token creation at initial tokenization called `classify_split()` which internally
//! called `.to_owned()` on every token, allocating a new `String` per Identifier/Number/Unrecognized
//! token. With 5-15 tokens per command, this was 40-60% of parsing hot path time.
//!
//! ## Why Not Caught
//!
//! All token-related tests checked BEHAVIOR (parse results) not HOT-PATH ALLOCATION PATTERNS.
//! No test verified that the token classification path avoided `String` allocations.
//!
//! ## Fix Applied
//!
//! Changed `ZeroCopyTokenKind<'a>` to use `Cow<'a, str>` (was `&'a str`) to handle
//! both borrowed tokens (from input) and synthetic tokens (merge_value_context).
//! Changed `RichItem<'a>.kind` from `UnilangTokenKind` to `ZeroCopyTokenKind<'a>`.
//! The initial tokenization now stores `Cow::Borrowed` (zero-copy) for input tokens;
//! only synthetic tokens use `Cow::Owned`.
//!
//! ## Prevention
//!
//! Assert that parser_engine/mod.rs uses `ZeroCopyTokenKind` (not `UnilangTokenKind`)
//! for its internal token matching — any revert to owned types will fail this test.
//!
//! ## Pitfall
//!
//! `Cow<'a, str>` in match arms requires `.as_ref()` to get `&str`. Pattern matching
//! on `Cow` requires `ref` binding: `ZeroCopyTokenKind::Identifier(ref s)` gives `s: &Cow<'a, str>`.
//! Use `s.as_ref()` or `&**s` to get `&str`. Do not use `s.clone()` for owned value extraction;
//! use `s.into_owned()` instead.
/// bug_reproducer(parser-001)
///
/// Verifies that the parser engine hot path uses `ZeroCopyTokenKind` (no `String` allocation
/// per token) rather than `UnilangTokenKind` (allocates `String` per token).
///
/// RED state (pre-fix): parser_engine imports and matches on `UnilangTokenKind` (owned String).
/// GREEN state (post-fix): parser_engine imports and matches on `ZeroCopyTokenKind` (Cow<str>).