nix-community / rnix-parser

A Nix parser written in Rust [maintainer=@oberblastmeister]
MIT License
366 stars 44 forks source link

Example of modifying an AST #162

Open abhillman opened 6 months ago

abhillman commented 6 months ago

Description

Some basic code that demonstrates modifying an AST.

Considered alternatives

Poking around, playing with various things, reading tests -- currently doing that.

Additional context

An example could look something like this:

fn main() {
    let parse: rnix::Parse<rnix::Root> = rnix::Root::parse("\"hello world!\"");
    if !parse.errors().is_empty() { panic!(); }
    let syntax_node: rnix::SyntaxNode = parse.syntax();
    // modify the ast and call `println!("Modified: {}", syntax_node_modified);`
    println!("Original: {}", syntax_node);
}
abhillman commented 6 months ago

Related https://github.com/nix-community/rnix-parser/issues/151#issuecomment-2099425010

oberblastmeister commented 6 months ago

rust-analyzer has a good example for this https://github.com/rust-lang/rust-analyzer/blob/master/crates/syntax/src/ted.rs

I wonder if we should add an interface like that.

An example to modify the syntax tree would also be nice.