Skip to content

spacedragon/tree-sitter-query-macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tree-sitterquery by rust macro (WIP)

Example

#[derive(Debug)]
struct Cons {
    modifiers: String,
    name: String,
    parameters: String,
    body: String
}

fn tree_sitter_macro(source: &str,node: Node) -> Cons {
    let mut result = Cons {
        modifiers: String::new(),
        name: String::new(),
        parameters: String::new(),
        body: String::new(),
    };
    // capture with rust closure
    let mut modifiers = |node: Node| -> bool {
        result.modifiers = node.utf8_text(source.as_bytes()).unwrap().to_string();
        true
    };

    let mut name = |node: Node| -> bool {
        result.name = node.utf8_text(source.as_bytes()).unwrap().to_string();
        true
    };
    let mut parameters = |node: Node| -> bool {
        result.parameters = node.utf8_text(source.as_bytes()).unwrap().to_string();
        true
    };
    let mut body = |node: Node| -> bool {
        result.body = node.utf8_text(source.as_bytes()).unwrap().to_string();
        true
    };

    let mut query = make_query!{
        (program
            (class_declaration
                body: (class_body    
                    (constructor_declaration 
                        (modifiers) @modifiers
                        name: (identifier) @name
                        parameters: (formal_parameters) @parameters
                        body: (constructor_body) @body
                    )
                )
            )
        )
    };
    query(node);
    result
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published