test_treesitter.py•3.57 kB
#!/usr/bin/env python3
"""Working Tree-sitter implementation using direct tree_sitter module"""
import tree_sitter
import ctypes
import os
import site
# Load the languages shared library
tsl_path = os.path.join(site.getusersitepackages(), 'tree_sitter_languages', 'languages.so')
lib = ctypes.CDLL(tsl_path)
# Get language functions from the shared library
def get_language(name):
"""Get a tree-sitter Language object for the given language"""
symbol_name = f'tree_sitter_{name}'
lang_func = getattr(lib, symbol_name)
lang_func.restype = ctypes.c_void_p
# Create a Language object from the pointer
lang_ptr = lang_func()
return tree_sitter.Language(lang_ptr)
# Test parsing for our three target languages
def test_language(lang_name, code):
"""Test parsing for a specific language"""
print(f"\n{'='*50}")
print(f"Testing {lang_name}...")
print(f"{'='*50}")
# Get the language
language = get_language(lang_name)
print(f"✅ Got {lang_name} language object")
# Create parser and set language
parser = tree_sitter.Parser()
parser.language = language
print(f"✅ Parser configured for {lang_name}")
# Parse the code
tree = parser.parse(code)
print(f"✅ Parsed successfully!")
# Show the tree structure
print(f"\nRoot node type: {tree.root_node.type}")
print(f"Root node children: {tree.root_node.child_count}")
# Walk the tree and find functions
def find_functions(node, depth=0):
"""Recursively find function nodes"""
indent = " " * depth
# Check for function-like nodes
if 'function' in node.type or 'method' in node.type:
# Try to get the name
for child in node.children:
if 'identifier' in child.type or child.type == 'property_identifier':
name = code[child.start_byte:child.end_byte].decode('utf-8')
print(f"{indent}Found {node.type}: {name}")
break
# Recurse into children
for child in node.children:
find_functions(child, depth + 1)
print("\nFunctions found:")
find_functions(tree.root_node)
return tree
# Test Python
python_code = b'''
def hello(name: str) -> str:
"""Say hello"""
return f"Hello {name}"
class Greeter:
def __init__(self, prefix: str):
self.prefix = prefix
def greet(self, name: str) -> str:
return f"{self.prefix} {name}"
'''
test_language('python', python_code)
# Test JavaScript
javascript_code = b'''
function hello(name) {
return `Hello ${name}`;
}
const greet = (name) => {
return `Greetings ${name}`;
};
class Greeter {
constructor(prefix) {
this.prefix = prefix;
}
greet(name) {
return `${this.prefix} ${name}`;
}
}
'''
test_language('javascript', javascript_code)
# Test TypeScript
typescript_code = b'''
function hello(name: string): string {
return `Hello ${name}`;
}
interface Greeting {
prefix: string;
greet(name: string): string;
}
class Greeter implements Greeting {
constructor(public prefix: string) {}
greet(name: string): string {
return `${this.prefix} ${name}`;
}
}
const asyncGreet = async (name: string): Promise<string> => {
return Promise.resolve(`Async hello ${name}`);
};
'''
test_language('typescript', typescript_code)
print("\n" + "="*50)
print("🎉 All languages parsed successfully!")
print("Tree-sitter is working correctly!")
print("="*50)