I am building extensions to an existing language as a preprocessor. I was unable to figure out if there is a way to get pegjs to regenerate the source code. Basically I want it to parse the source, I want to modify the AST then regenerate the source. Anyway this is possible?
@mattkanwisher sorry, I dont quite understand what you mean by "regenerate" the source.
Could you please clarify a bit? If you mean produce code based on the AST - you would have to write that logic yourself, either directly in the grammar or as a separate piece.
Yeah produce code based on the AST, so I have to generate a compiler back to original source if I want to preprocess using this?
Hmm, if you want the _original_ source - you would still have it w/o a need to generate it, right?
But if you want a modified version of it - you would have to write the code generation logic yourself.
(@Mingun @futagoza please correct me if I'm wrong)
@YemSalat is right, PEG.js is just a parser generator.
As you can see, PEG.js is only involved in the first step, so if you want to regenerate the source, you have to build a compiler yourself, or if a compiler or generator (toolchain) already exist's for the language, I suggest you either return AST compatible with the toolchain or add a transform that alters's the AST to be more compatible with the toolchain.
Closing, as this is not actually an issue with PEG.js but your welcome to continue the discussion.
So I figured it out. For anyone that lands on this bug post in the future. Just read the source code to "The super tiny compiler". After that I was able to use pegjs to build my preprocessor in a few hours
Most helpful comment
@YemSalat is right, PEG.js is just a parser generator.
As you can see, PEG.js is only involved in the first step, so if you want to regenerate the source, you have to build a compiler yourself, or if a compiler or generator (toolchain) already exist's for the language, I suggest you either return AST compatible with the toolchain or add a transform that alters's the AST to be more compatible with the toolchain.
Closing, as this is not actually an issue with PEG.js but your welcome to continue the discussion.