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
// SPDX-FileCopyrightText: 2022 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
//! # thread-ast-engine
//!
//! **Core AST engine for Thread: parsing, matching, and transforming code using AST patterns.**
//!
//! ## Overview
//!
//! `thread-ast-engine` provides powerful tools for working with Abstract Syntax Trees (ASTs).
//! Forked from [`ast-grep-core`](https://github.com/ast-grep/ast-grep/), it offers language-agnostic
//! APIs for code analysis and transformation.
//!
//! ### What You Can Do
//!
//! - **Parse** source code into ASTs using [tree-sitter](https://tree-sitter.github.io/tree-sitter/)
//! - **Search** for code patterns using flexible meta-variables (like `$VAR`)
//! - **Transform** code by replacing matched patterns with new code
//! - **Navigate** AST nodes with intuitive tree traversal methods
//!
//! Perfect for building code linters, refactoring tools, and automated code modification systems.
//!
//! ## Quick Start
//!
//! Add to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! thread-ast-engine = { version = "0.1.0", features = ["parsing", "matching"] }
//! ```
//!
//! ### Basic Example: Find and Replace Variables
//!
//! ```rust,no_run
//! use thread_ast_engine::Language;
//! use thread_ast_engine::tree_sitter::LanguageExt;
//!
//! // Parse JavaScript/TypeScript code
//! let mut ast = Language::Tsx.ast_grep("var a = 1; var b = 2;");
//!
//! // Replace all 'var' declarations with 'let'
//! ast.replace("var $NAME = $VALUE", "let $NAME = $VALUE")?;
//!
//! // Get the transformed code
//! println!("{}", ast.generate());
//! // Output: "let a = 1; let b = 2;"
//! # Ok::<(), String>(())
//! ```
//!
//! ### Finding Code Patterns
//!
//! ```rust,no_run
//! use thread_ast_engine::matcher::MatcherExt;
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//!
//! let ast = Language::Tsx.ast_grep("function add(a, b) { return a + b; }");
//! let root = ast.root();
//!
//! // Find all function declarations
//! if let Some(func) = root.find("function $NAME($$$PARAMS) { $$$BODY }") {
//! println!("Function name: {}", func.get_env().get_match("NAME").unwrap().text());
//! }
//!
//! // Find all return statements
//! for ret_stmt in root.find_all("return $EXPR") {
//! println!("Returns: {}", ret_stmt.get_env().get_match("EXPR").unwrap().text());
//! }
//! ```
//!
//! ### Working with Meta-Variables
//!
//! Meta-variables capture parts of the matched code:
//!
//! - `$VAR` - Captures a single AST node
//! - `$$$ITEMS` - Captures multiple consecutive nodes (ellipsis)
//! - `$_` - Matches any node but doesn't capture it
//!
//! ```rust,no_run
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//! let ast = Language::Tsx.ast_grep("console.log('Hello', 'World', 123)");
//! let root = ast.root();
//!
//! if let Some(call) = root.find("console.log($$$ARGS)") {
//! let args = call.get_env().get_multiple_matches("ARGS");
//! println!("Found {} arguments", args.len()); // Output: Found 3 arguments
//! }
//! ```
//!
//! ## Core Components
//!
//! ### [`Node`] - AST Navigation
//! Navigate and inspect AST nodes with methods like [`Node::children`], [`Node::parent`], and [`Node::find`].
//!
//! ### [`Pattern`] - Code Matching
//! Match code structures using tree-sitter patterns with meta-variables.
//!
//! ### [`MetaVarEnv`] - Variable Capture
//! Store and retrieve captured meta-variables from pattern matches.
//!
//! ### [`Replacer`] - Code Transformation
//! Replace matched code with new content, supporting template-based replacement.
//!
//! ### [`Language`] - Language Support
//! Abstract interface for different programming languages via tree-sitter grammars.
//!
//! ## Feature Flags
//!
//! - **`parsing`** - Enables tree-sitter parsing (includes tree-sitter dependency)
//! - **`matching`** - Enables pattern matching and node replacement/transformation engine.
//!
//! Use `default-features = false` to opt out of all features and enable only what you need:
//!
//! ```toml
//! [dependencies]
//! thread-ast-engine = { version = "0.1.0", default-features = false, features = ["matching"] }
//! ```
//!
//! ## Advanced Examples
//!
//! ### Custom Pattern Matching
//!
//! ```rust,no_run
//! use thread_ast_engine::ops::Op;
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//!
//! // Combine multiple patterns with logical operators
//! let pattern = Op::either("let $VAR = $VALUE")
//! .or("const $VAR = $VALUE")
//! .or("var $VAR = $VALUE");
//!
//! let ast = Language::Tsx.ast_grep("const x = 42;");
//! let root = ast.root();
//!
//! if let Some(match_) = root.find(pattern) {
//! println!("Found variable declaration");
//! }
//! ```
//!
//! ### Tree Traversal
//!
//! ```rust,no_run
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//! let ast = Language::Tsx.ast_grep("if (condition) { doSomething(); } else { doOther(); }");
//! let root = ast.root();
//!
//! // Traverse all descendants
//! for node in root.dfs() {
//! if node.kind() == "identifier" {
//! println!("Identifier: {}", node.text());
//! }
//! }
//!
//! // Check relationships between nodes
//! if let Some(if_stmt) = root.find("if ($COND) { $$$THEN }") {
//! println!("If statement condition: {}",
//! if_stmt.get_env().get_match("COND").unwrap().text());
//! }
//! ```
//!
//! ## License
//!
//! Original ast-grep code is licensed under the [MIT license](./LICENSE-MIT),
//! all changes introduced in this project are licensed under the [AGPL-3.0-or-later](./LICENSE-AGPL-3.0-or-later).
//!
//! See [`VENDORED.md`](crates/ast-engine/VENDORED.md) for more information on our fork, changes, and reasons.
// Core AST functionality (always available)
pub use ;
pub use Doc;
// pub use matcher::types::{MatchStrictness, Pattern, PatternBuilder, PatternError, PatternNode};
// Feature-gated modules
// Everything but types feature gated behind "matching" in `matchers`
// Re-exports
// the bare types with no implementations
pub use ;
// implemented types
pub use ;
pub use MetaVarEnv;
pub use MatchStrictness;
pub use Language;
pub use Root;
pub type AstGrep<D> = ;