Traverse Pest ast multiple times

Hey, I'm quite new to rust and I'm wondering if I'm missing something here.

I'm in the middle of trying to create a compiler in rust, using https://pest.rs/ for defining the grammar and parsing the input.

I want to be able to traverse the tree pest creates multiple times. ( first time to generate a symbol table, second to do code generation).

I think I understand the problem I have (although it would be good to check I understand correctly).

To traverse the tree I have code like this

    for line in ast.into_inner() {
        match line.as_rule() {
            Rule::EOI => Ok(()),
            Rule::line => doStuff,
            _ => unreachable!(),
        }?;
    }

into_inner doesn't return a reference, but a concrete type, as I understand assignment in rust, is move by default, so into_inner effectively removes this node from the tree.

If I want to traverse the tree multiple times (reading only!) the only option i can see is to clone the whole tree for each pass.

I'm not sure if this is an intentional property of the language/pest or if I'm missing something?

This is not a language limitation, it is up to each API to provide a non-consuming iterator, when that's desirable. So look for other methods in the Pest API that don't consume the tree, such as:
https://docs.rs/pest/latest/pest/iterators/struct.Pair.html#method.as_span

1 Like

Hey, yep thanks. I understand, Good to know I'm not missing something. Your response got me to what I think the answer is (For others that come after). It looks like the way to do it is to transform the pest tree using https://github.com/pest-parser/ast/tree/master/derive. Then you can traverse the tree using built in iterators.
Thanks

1 Like