Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessor? Regenerate source code #543

Closed
mattkanwisher opened this issue Nov 12, 2017 · 5 comments
Closed

Preprocessor? Regenerate source code #543

mattkanwisher opened this issue Nov 12, 2017 · 5 comments

Comments

@mattkanwisher
Copy link

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?

@YemSalat
Copy link

YemSalat commented Nov 13, 2017

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

@mattkanwisher
Copy link
Author

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?

@YemSalat
Copy link

YemSalat commented Nov 13, 2017

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)

@futagoza
Copy link
Member

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

@mattkanwisher
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants