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
// SPDX-FileCopyrightText: 2025 Herrington Darkholme <2883231+HerringtonDarkholme@users.noreply.github.com>
// SPDX-FileCopyrightText: 2025 Knitli Inc. <knitli@knit.li>
// SPDX-FileContributor: Adam Poulemanos <adam@knit.li>
//
// SPDX-License-Identifier: AGPL-3.0-or-later AND MIT
//! # Core Pattern Matching Types
//!
//! Fundamental types and traits for AST pattern matching operations.
//!
//! ## Key Types
//!
//! - [`Matcher`] - Core trait for matching AST nodes
//! - [`Pattern`] - Structural pattern for matching AST shapes
//! - [`MatchStrictness`] - Controls how precisely patterns must match
//! - [`PatternNode`] - Internal representation of pattern structure
//!
//! ## Usage
//!
//! These types are available even without the `matching` feature flag enabled,
//! allowing API definitions that reference them without requiring full
//! implementation dependencies.
use crateDoc;
use crate;
use crateNode;
use BitSet;
use Cow;
use Error;
/// Core trait for matching AST nodes against patterns.
///
/// Implementors define how to match nodes, whether by structure, content,
/// kind, or other criteria. The matcher can also capture meta-variables
/// during the matching process.
///
/// # Type Parameters
///
/// The trait is generic over document types to support different source
/// encodings and language implementations.
///
/// # Example Implementation
///
/// ```rust,ignore
/// use thread_ast_engine::Matcher;
///
/// struct SimpleKindMatcher {
/// target_kind: String,
/// }
///
/// impl Matcher for SimpleKindMatcher {
/// fn match_node_with_env<'tree, D: Doc>(
/// &self,
/// node: Node<'tree, D>,
/// _env: &mut Cow<MetaVarEnv<'tree, D>>,
/// ) -> Option<Node<'tree, D>> {
/// if node.kind() == self.target_kind {
/// Some(node)
/// } else {
/// None
/// }
/// }
/// }
/// ```
/// Extension trait providing convenient utility methods for [`Matcher`] implementations.
///
/// Automatically implemented for all types that implement [`Matcher`]. Provides
/// higher-level operations like finding nodes and working with meta-variable environments.
///
/// # Important
///
/// You should not implement this trait manually - it's automatically implemented
/// for all [`Matcher`] types.
///
/// # Example
///
/// ```rust,no_run
/// # use thread_ast_engine::Language;
/// # use thread_ast_engine::tree_sitter::LanguageExt;
/// # use thread_ast_engine::MatcherExt;
/// let ast = Language::Tsx.ast_grep("const x = 42;");
/// let root = ast.root();
///
/// // Use MatcherExt methods
/// if let Some(node_match) = root.find("const $VAR = $VALUE") {
/// println!("Found constant declaration");
/// }
/// ```
/// Result of a successful pattern match containing the matched node and captured variables.
///
/// `NodeMatch` combines an AST node with the meta-variables captured during
/// pattern matching. It acts like a regular [`Node`] (through [`Deref`]) while
/// also providing access to captured variables through [`get_env`].
///
/// # Lifetime
///
/// The lifetime `'t` ties the match to its source document, ensuring memory safety.
///
/// # Usage Patterns
///
/// ```rust,ignore
/// // Use as a regular node
/// let text = node_match.text();
/// let position = node_match.start_pos();
///
/// // Access captured meta-variables
/// let env = node_match.get_env();
/// let captured_name = env.get_match("VAR_NAME").unwrap();
///
/// // Generate replacement code
/// let edit = node_match.replace_by("new code with $VAR_NAME");
/// ```
///
/// # Type Parameters
///
/// - `'t` - Lifetime tied to the source document
/// - `D: Doc` - Document type containing the source and language info
, pub );
/// Controls how precisely patterns must match AST structure.
///
/// Different strictness levels allow patterns to match with varying degrees
/// of precision, from exact CST matching to loose structural matching.
///
/// # Variants
///
/// - **`Cst`** - All nodes must match exactly (concrete syntax tree)
/// - **`Smart`** - Matches meaningful nodes, ignoring trivial syntax
/// - **`Ast`** - Only structural nodes matter (abstract syntax tree)
/// - **`Relaxed`** - Ignores comments and focuses on code structure
/// - **`Signature`** - Matches structure only, ignoring all text content
///
/// # Example
///
/// ```rust,ignore
/// // With Cst strictness, these would be different:
/// // "let x=42;" vs "let x = 42;"
/// //
/// // With Ast strictness, they match the same pattern:
/// // "let $VAR = $VALUE"
/// ```
/// Structural pattern for matching AST nodes based on their shape and content.
///
/// Patterns represent code structures with support for meta-variables (like `$VAR`)
/// that can capture parts of the matched code. They're built from source code strings
/// and compiled into efficient matching structures.
///
/// # Example
///
/// ```rust,ignore
/// // Pattern for variable declarations
/// let pattern = Pattern::new("let $NAME = $VALUE", language);
///
/// // Can match: "let x = 42", "let result = calculate()", etc.
/// ```
/// Builder for constructing patterns from source code.
///
/// Handles parsing pattern strings into [`Pattern`] structures,
/// with optional contextual information for more precise matching.
/// Internal representation of a pattern's structure.
///
/// Patterns are compiled into a tree of `PatternNode` elements that
/// efficiently represent the matching logic for different AST structures.