Lark.js icon indicating copy to clipboard operation
Lark.js copied to clipboard

How to I go from an AST tree to a file?

Open machard opened this issue 3 years ago • 4 comments

Hi, Let's say I use https://github.com/crytic/amarna/blob/main/amarna/grammars/cairo.lark and https://github.com/lark-parser/Lark.js to get the AST tree of a cairo file. What do I use after to generate a cairo file from the AST ? Thank you

machard avatar Aug 18 '22 15:08 machard

Unless I did not understand properly, I want to do the other side : write the cairo file after I played with the AST

machard avatar Aug 18 '22 15:08 machard

(Yes, sorry, I misunderstood your question at first)

There is no direct way to do that. You'll have to write a transformer that generates a cairo file from an AST.

You can use the line/column information to restore the same whitespace, and it's also possible to collect the ignore tokens into an array, using lexer_callbacks.

erezsh avatar Aug 18 '22 15:08 erezsh

thank you. I am a bit new to this, it's called a generator or serializer ? I think https://babeljs.io/docs/en/babel-generator is turning js ast to js code, do you have maybe other example of such tool ?

machard avatar Aug 18 '22 15:08 machard

You can call it a code generator. I don't think serializer is the same thing.

erezsh avatar Aug 18 '22 15:08 erezsh