lark-parser / Lark.js

Live port of Lark's standalone parser to Javascript
MIT License
71 stars 12 forks source link

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

Open machard opened 2 years ago

machard commented 2 years ago

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 commented 2 years ago

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

erezsh commented 2 years ago

(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.

machard commented 2 years ago

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 ?

erezsh commented 2 years ago

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