Pegjs: Preprocessor? Regenerate source code

Created on 12 Nov 2017  ·  5Comments  ·  Source: pegjs/pegjs

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?

discussion question

Most helpful comment

@YemSalat is right, PEG.js is just a parser generator.

  1. Build a parser using PEG.js (ensure actions return AST)
  2. Parse your language with the generated parser to build an AST
  3. Modify the AST using transform pass's
  4. Regenerate the source code

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.

All 5 comments

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

  1. Build a parser using PEG.js (ensure actions return AST)
  2. Parse your language with the generated parser to build an AST
  3. Modify the AST using transform pass's
  4. Regenerate the source code

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

https://github.com/thejameskyle/the-super-tiny-compiler

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gatsbyz picture gatsbyz  ·  3Comments

brettz9 picture brettz9  ·  8Comments

futagoza picture futagoza  ·  6Comments

StoneCypher picture StoneCypher  ·  8Comments

emmenko picture emmenko  ·  15Comments