tree-sitter replaces ast-grep
This commit is contained in:
58
crates/g3-core/examples/inspect_ast.rs
Normal file
58
crates/g3-core/examples/inspect_ast.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
//! Inspect tree-sitter AST structure for Rust code
|
||||
|
||||
use tree_sitter::{Parser, Language};
|
||||
|
||||
fn print_tree(node: tree_sitter::Node, source: &str, indent: usize) {
|
||||
let indent_str = " ".repeat(indent);
|
||||
let node_text = &source[node.byte_range()];
|
||||
let preview = if node_text.len() > 50 {
|
||||
format!("{}...", &node_text[..50])
|
||||
} else {
|
||||
node_text.to_string()
|
||||
};
|
||||
|
||||
println!(
|
||||
"{}{} [{}:{}] '{}'",
|
||||
indent_str,
|
||||
node.kind(),
|
||||
node.start_position().row + 1,
|
||||
node.start_position().column + 1,
|
||||
preview.replace('\n', "\\n")
|
||||
);
|
||||
|
||||
let mut cursor = node.walk();
|
||||
for child in node.children(&mut cursor) {
|
||||
print_tree(child, source, indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let source_code = r#"
|
||||
pub async fn example_async() {
|
||||
println!("Hello");
|
||||
}
|
||||
|
||||
fn regular_function() {
|
||||
println!("Regular");
|
||||
}
|
||||
|
||||
pub async fn another_async(x: i32) -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
"#;
|
||||
|
||||
println!("Source code:");
|
||||
println!("{}", source_code);
|
||||
println!("\n{}", "=".repeat(80));
|
||||
println!("AST Structure:");
|
||||
println!("{}\n", "=".repeat(80));
|
||||
|
||||
let mut parser = Parser::new();
|
||||
let language: Language = tree_sitter_rust::language().into();
|
||||
parser.set_language(&language)?;
|
||||
|
||||
let tree = parser.parse(source_code, None).unwrap();
|
||||
print_tree(tree.root_node(), source_code, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
56
crates/g3-core/examples/inspect_python_ast.rs
Normal file
56
crates/g3-core/examples/inspect_python_ast.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
//! Inspect tree-sitter AST structure for Python code
|
||||
|
||||
use tree_sitter::{Parser, Language};
|
||||
|
||||
fn print_tree(node: tree_sitter::Node, source: &str, indent: usize) {
|
||||
let indent_str = " ".repeat(indent);
|
||||
let node_text = &source[node.byte_range()];
|
||||
let preview = if node_text.len() > 50 {
|
||||
format!("{}...", &node_text[..50])
|
||||
} else {
|
||||
node_text.to_string()
|
||||
};
|
||||
|
||||
println!(
|
||||
"{}{} [{}:{}] '{}'",
|
||||
indent_str,
|
||||
node.kind(),
|
||||
node.start_position().row + 1,
|
||||
node.start_position().column + 1,
|
||||
preview.replace('\n', "\\n")
|
||||
);
|
||||
|
||||
let mut cursor = node.walk();
|
||||
for child in node.children(&mut cursor) {
|
||||
print_tree(child, source, indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let source_code = r#"
|
||||
def regular_function():
|
||||
pass
|
||||
|
||||
async def async_function():
|
||||
pass
|
||||
|
||||
class MyClass:
|
||||
def method(self):
|
||||
pass
|
||||
"#;
|
||||
|
||||
println!("Source code:");
|
||||
println!("{}", source_code);
|
||||
println!("\n{}", "=".repeat(80));
|
||||
println!("AST Structure:");
|
||||
println!("{}\n", "=".repeat(80));
|
||||
|
||||
let mut parser = Parser::new();
|
||||
let language: Language = tree_sitter_python::language().into();
|
||||
parser.set_language(&language)?;
|
||||
|
||||
let tree = parser.parse(source_code, None).unwrap();
|
||||
print_tree(tree.root_node(), source_code, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
43
crates/g3-core/examples/test_python_query.rs
Normal file
43
crates/g3-core/examples/test_python_query.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
//! Test Python async query
|
||||
|
||||
use tree_sitter::{Parser, Query, QueryCursor, Language};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let source_code = r#"
|
||||
def regular_function():
|
||||
pass
|
||||
|
||||
async def async_function():
|
||||
pass
|
||||
"#;
|
||||
|
||||
let mut parser = Parser::new();
|
||||
let language: Language = tree_sitter_python::language().into();
|
||||
parser.set_language(&language)?;
|
||||
|
||||
let tree = parser.parse(source_code, None).unwrap();
|
||||
|
||||
// Try different queries
|
||||
let queries = vec![
|
||||
"(function_definition (async) name: (identifier) @name)",
|
||||
"(function_definition (async))",
|
||||
"(async)",
|
||||
];
|
||||
|
||||
for query_str in queries {
|
||||
println!("\nTrying query: {}", query_str);
|
||||
match Query::new(&language, query_str) {
|
||||
Ok(query) => {
|
||||
let mut cursor = QueryCursor::new();
|
||||
let matches = cursor.matches(&query, tree.root_node(), source_code.as_bytes());
|
||||
let count = matches.count();
|
||||
println!(" ✓ Valid query, found {} matches", count);
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" ✗ Invalid query: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user