Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/ast-engine/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
//! ```rust,no_run
//! use thread_ast_engine::language::Language;
//!
//! let lang = Tsx {};
//! let pattern = lang.pre_process_pattern("var $A = $B");
//! let meta_var = lang.extract_meta_var("$A");
//! // Example usage for implementing Language
//! ```
#[allow(unused_imports)]
#[cfg(feature = "matching")]
Expand Down
67 changes: 34 additions & 33 deletions crates/ast-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
//! use thread_ast_engine::tree_sitter::LanguageExt;
//!
//! // Parse JavaScript/TypeScript code
//! let mut ast = Language::Tsx.ast_grep("var a = 1; var b = 2;");
//! // 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")?;
//! // ast.replace("var $NAME = $VALUE", "let $NAME = $VALUE")?;
//!
//! // Get the transformed code
//! println!("{}", ast.generate());
//! // println!("{}", ast.generate());
//! // Output: "let a = 1; let b = 2;"
//! # Ok::<(), String>(())
//! ```
Expand All @@ -55,18 +55,18 @@
//! # 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();
//! // 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());
//! }
//! // 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());
//! }
//! // for ret_stmt in root.find_all("return $EXPR") {
//! // println!("Returns: {}", ret_stmt.get_env().get_match("EXPR").unwrap().text());
//! // }
//! ```
//!
//! ### Working with Meta-Variables
Expand All @@ -81,13 +81,13 @@
//! # 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();
//! // 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
//! }
//! // 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
Expand Down Expand Up @@ -134,12 +134,13 @@
//! .or("const $VAR = $VALUE")
//! .or("var $VAR = $VALUE");
//!
//! let ast = Language::Tsx.ast_grep("const x = 42;");
//! let root = ast.root();
//! // Assuming `Language::Tsx` implements `LanguageExt` and `Language`
//! // let ast = Language::Tsx.ast_grep("const x = 42;");
//! // let root = ast.root();
//!
//! if let Some(match_) = root.find(pattern) {
//! println!("Found variable declaration");
//! }
//! // if let Some(match_) = root.find(pattern) {
//! // println!("Found variable declaration");
//! // }
//! ```
//!
//! ### Tree Traversal
Expand All @@ -148,21 +149,21 @@
//! # 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();
//! // 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());
//! }
//! }
//! // 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());
//! }
//! // if let Some(if_stmt) = root.find("if ($COND) { $$$THEN }") {
//! // println!("If statement condition: {}",
//! // if_stmt.get_env().get_match("COND").unwrap().text());
//! // }
//! ```
//!
//! ## License
Expand Down
28 changes: 14 additions & 14 deletions crates/ast-engine/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//! let ast = Language::Tsx.ast_grep("let x = 42;");
//! let root = ast.root();
//! // let ast = Language::Tsx.ast_grep("let x = 42;");
//! // let root = ast.root();
//!
//! // Find variable declarations
//! if let Some(decl) = root.find("let $VAR = $VALUE") {
//! let var_name = decl.get_env().get_match("VAR").unwrap();
//! let value = decl.get_env().get_match("VALUE").unwrap();
//! println!("Variable {} = {}", var_name.text(), value.text());
//! }
//! // if let Some(decl) = root.find("let $VAR = $VALUE") {
//! // let var_name = decl.get_env().get_match("VAR").unwrap();
//! // let value = decl.get_env().get_match("VALUE").unwrap();
//! // println!("Variable {} = {}", var_name.text(), value.text());
//! // }
//! ```
//!
//! ### Finding Multiple Matches
Expand All @@ -49,15 +49,15 @@
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//! let code = "let a = 1; let b = 2; let c = 3;";
//! let ast = Language::Tsx.ast_grep(code);
//! let root = ast.root();
//! // let code = "let a = 1; let b = 2; let c = 3;";
//! // let ast = Language::Tsx.ast_grep(code);
//! // let root = ast.root();
//!
//! // Find all variable declarations
//! for decl in root.find_all("let $VAR = $VALUE") {
//! let var_name = decl.get_env().get_match("VAR").unwrap();
//! println!("Found variable: {}", var_name.text());
//! }
//! // for decl in root.find_all("let $VAR = $VALUE") {
//! // let var_name = decl.get_env().get_match("VAR").unwrap();
//! // println!("Found variable: {}", var_name.text());
//! // }
//! ```
//!
//! ### `NodeMatch`
Expand Down
10 changes: 5 additions & 5 deletions crates/ast-engine/src/matchers/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ pub trait Matcher {
/// # 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();
/// // 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");
/// }
/// // if let Some(node_match) = root.find("const $VAR = $VALUE") {
/// // println!("Found constant declaration");
/// // }
/// ```
pub trait MatcherExt: Matcher {
fn match_node<'tree, D: Doc>(&self, node: Node<'tree, D>) -> Option<NodeMatch<'tree, D>>;
Expand Down
6 changes: 1 addition & 5 deletions crates/ast-engine/src/meta_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
//! ## Example
//!
//! ```rust,no_run
//! use thread_ast_engine::meta_var::{MetaVarEnv, MetaVariable, extract_meta_var};
//!
//! let mut env = MetaVarEnv::new();
//! env.insert("$A", node);
//! let meta = extract_meta_var("$A", '$');
//! // Note: exact APIs depend on `Doc` generics
//! ```
//!
//! See [`MetaVarEnv`](crates/ast-engine/src/meta_var.rs:48) for details on usage in AST matching and rewriting.
Expand Down
54 changes: 27 additions & 27 deletions crates/ast-engine/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::MatcherExt;
//! let ast = Language::Tsx.ast_grep("function foo() { return 42; }");
//! let root_node = ast.root();
//! // let ast = Language::Tsx.ast_grep("function foo() { return 42; }");
//! // let root_node = ast.root();
//!
//! // Navigate the tree
//! for child in root_node.children() {
//! println!("Child kind: {}", child.kind());
//! }
//! // for child in root_node.children() {
//! // println!("Child kind: {}", child.kind());
//! // }
//!
//! // Find specific patterns
//! if let Some(func) = root_node.find("function $NAME() { $$$BODY }") {
//! println!("Found function: {}", func.get_env().get_match("NAME").unwrap().text());
//! }
//! // if let Some(func) = root_node.find("function $NAME() { $$$BODY }") {
//! // println!("Found function: {}", func.get_env().get_match("NAME").unwrap().text());
//! // }
//! ```

use crate::Doc;
Expand Down Expand Up @@ -62,11 +62,11 @@ use std::borrow::Cow;
/// ```rust,no_run
/// # use thread_ast_engine::Language;
/// # use thread_ast_engine::tree_sitter::LanguageExt;
/// let ast = Language::Tsx.ast_grep("let x = 42;\nlet y = 24;");
/// let root = ast.root();
/// // let ast = Language::Tsx.ast_grep("let x = 42;\nlet y = 24;");
/// // let root = ast.root();
///
/// let start_pos = root.start_pos();
/// assert_eq!(start_pos.line(), 0);
/// // let start_pos = root.start_pos();
/// // assert_eq!(start_pos.line(), 0);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Position {
Expand Down Expand Up @@ -119,12 +119,12 @@ impl Position {
/// # use thread_ast_engine::Language;
/// # use thread_ast_engine::tree_sitter::LanguageExt;
/// # use thread_ast_engine::MatcherExt;
/// let mut ast = Language::Tsx.ast_grep("let x = 42;");
/// let root_node = ast.root();
/// // let mut ast = Language::Tsx.ast_grep("let x = 42;");
/// // let root_node = ast.root();
///
/// // Perform tree-wide replacements
/// ast.replace("let $VAR = $VALUE", "const $VAR = $VALUE");
/// println!("{}", ast.generate());
/// // ast.replace("let $VAR = $VALUE", "const $VAR = $VALUE");
/// // println!("{}", ast.generate());
/// ```
#[derive(Clone, Debug)]
pub struct Root<D: Doc> {
Expand Down Expand Up @@ -209,23 +209,23 @@ impl<D: Doc> Root<D> {
/// # use thread_ast_engine::Language;
/// # use thread_ast_engine::tree_sitter::LanguageExt;
/// # use thread_ast_engine::matcher::MatcherExt;
/// let ast = Language::Tsx.ast_grep("function hello() { return 'world'; }");
/// let root_node = ast.root();
/// // let ast = Language::Tsx.ast_grep("function hello() { return 'world'; }");
/// // let root_node = ast.root();
///
/// // Check the node type
/// println!("Root kind: {}", root_node.kind());
/// // println!("Root kind: {}", root_node.kind());
///
/// // Navigate to children
/// for child in root_node.children() {
/// println!("Child: {} at {}:{}", child.kind(),
/// child.start_pos().line(), child.start_pos().column(&child));
/// }
/// // for child in root_node.children() {
/// // println!("Child: {} at {}:{}", child.kind(),
/// // child.start_pos().line(), child.start_pos().column(&child));
/// // }
///
/// // Find specific patterns
/// if let Some(return_stmt) = root_node.find("return $VALUE") {
/// let value = return_stmt.get_env().get_match("VALUE").unwrap();
/// println!("Returns: {}", value.text());
/// }
/// // if let Some(return_stmt) = root_node.find("return $VALUE") {
/// // let value = return_stmt.get_env().get_match("VALUE").unwrap();
/// // println!("Returns: {}", value.text());
/// // }
/// ```
#[derive(Clone, Debug)]
pub struct Node<'r, D: Doc> {
Expand Down
18 changes: 9 additions & 9 deletions crates/ast-engine/src/replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//! let mut ast = Language::Tsx.ast_grep("var x = 42;");
//! // let mut ast = Language::Tsx.ast_grep("var x = 42;");
//!
//! // Replace using a template string
//! ast.replace("var $NAME = $VALUE", "const $NAME = $VALUE");
//! println!("{}", ast.generate()); // "const x = 42;"
//! // ast.replace("var $NAME = $VALUE", "const $NAME = $VALUE");
//! // println!("{}", ast.generate()); // "const x = 42;"
//! ```
//!
//! ### Structural Replacement
Expand All @@ -44,12 +44,12 @@
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # use thread_ast_engine::matcher::MatcherExt;
//! let mut target = Language::Tsx.ast_grep("old_function();");
//! let replacement = Language::Tsx.ast_grep("new_function(42)");
//! // let mut target = Language::Tsx.ast_grep("old_function();");
//! // let replacement = Language::Tsx.ast_grep("new_function(42)");
//!
//! // Replace with another AST
//! target.replace("old_function()", replacement);
//! println!("{}", target.generate()); // "new_function(42);"
//! // target.replace("old_function()", replacement);
//! // println!("{}", target.generate()); // "new_function(42);"
//! ```

use crate::matcher::Matcher;
Expand Down Expand Up @@ -88,10 +88,10 @@ pub use template::{TemplateFix, TemplateFixError};
/// # use thread_ast_engine::meta_var::Underlying;
/// struct CustomReplacer;
///
/// impl<D: Doc> Replacer<D> for CustomReplacer {
/// impl<D: Doc<Source = String>> Replacer<D> for CustomReplacer {
/// fn generate_replacement(&self, nm: &NodeMatch<'_, D>) -> Underlying<D> {
/// // Custom replacement logic here
/// "new_code".as_bytes().to_vec()
/// "new_code".into()
/// }
/// }
/// ```
Expand Down
5 changes: 4 additions & 1 deletion crates/ast-engine/src/tree_sitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
//!
//! ```rust,no_run
//! # use thread_ast_engine::tree_sitter::{StrDoc, LanguageExt};
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::{Language, Doc};
//! # #[derive(Clone, Debug)]
//! # struct Tsx;
//! # impl Language for Tsx {
//! # fn kind_to_id(&self, _: &str) -> u16 { 0 }
Expand Down Expand Up @@ -144,6 +145,8 @@ fn parse_lang(
///
/// ```rust,no_run
/// # use thread_ast_engine::tree_sitter::StrDoc;
/// # use thread_ast_engine::Doc;
/// # #[derive(Clone, Debug)]
/// # struct JavaScript;
/// # impl thread_ast_engine::Language for JavaScript {
/// # fn kind_to_id(&self, _: &str) -> u16 { 0 }
Expand Down
5 changes: 4 additions & 1 deletion crates/ast-engine/src/tree_sitter/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//! # use thread_ast_engine::tree_sitter::traversal::Visitor;
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # #[derive(Clone, Debug)]
//! # struct Tsx;
//! # impl thread_ast_engine::Language for Tsx {
//! # fn kind_to_id(&self, _: &str) -> u16 { 0 }
Expand Down Expand Up @@ -56,6 +57,7 @@
//! # use thread_ast_engine::tree_sitter::traversal::Visitor;
//! # use thread_ast_engine::Language;
//! # use thread_ast_engine::tree_sitter::LanguageExt;
//! # #[derive(Clone, Debug)]
//! # struct Tsx;
//! # impl thread_ast_engine::Language for Tsx {
//! # fn kind_to_id(&self, _: &str) -> u16 { 0 }
Expand All @@ -71,7 +73,7 @@
//! // Non-reentrant: only finds outer matches
//! let outer_only: Vec<_> = Visitor::new("$FUNC($$$)")
//! .reentrant(false)
//! .visit(root)
//! .visit(root.clone())
//! .collect();
//!
//! // Reentrant: finds all matches including nested ones
Expand Down Expand Up @@ -117,6 +119,7 @@ use std::marker::PhantomData;
/// # use thread_ast_engine::tree_sitter::traversal::Visitor;
/// # use thread_ast_engine::Language;
/// # use thread_ast_engine::tree_sitter::LanguageExt;
/// # #[derive(Clone, Debug)]
/// # struct Tsx;
/// # impl thread_ast_engine::Language for Tsx {
/// # fn kind_to_id(&self, _: &str) -> u16 { 0 }
Expand Down
Loading
Loading