My task is to add switch statement and remove mandatory colons from functions, classes, loops in Python. Maybe to add some other nice features from Coffeescript.
The .py files with custom syntax must be imported with python interpreter, than parsed with a custom parser (just like Coffeescript compiler does).
(I already had a little experience in writing Python-like "for" syntax to already created custom parser, corrected several bugs. But it takes a long time to read all code and get it. So I decided to ask advice first.)
I searched a long time through internet, found several helpful answers, but still don't know how to implement it better. Some from what I found:
- Parse a .py file, read the AST, modify it, then write back the modified source code
- Python's tokenize module
- Python's ast module
- Python's c-like preprocessor with import hook
What I think to do:
- Rewrite Coffeescript parser or Python parser into pure Python
- Make import hook to parse files to AST by my own parser.
- Continue import (compile AST and import it to module)
(like Coffeescript does it)
So I have such questions: - Is there a Python parser written in Python (not to rewrite all Coffeescript parser) ? - Maybe is there any way to make ast.AST class frow own parser not rewriting ast library from C into Python ? - How can I do it better and easier ? (except modifying Python's sources, all must be done in runtime and be totally compatible with all other Python interpreters) - Maybe there are already some libraries, that help modifying Python's syntax ?
Thank you very much. Best regards, Serj.