https://github.com/chlohal/python-parser
A parser for Python in *pure* Javascript
https://github.com/chlohal/python-parser
javascript parser python
Last synced: 10 months ago
JSON representation
A parser for Python in *pure* Javascript
- Host: GitHub
- URL: https://github.com/chlohal/python-parser
- Owner: chlohal
- License: mit
- Created: 2021-07-07T02:08:02.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-11-21T02:08:20.000Z (about 3 years ago)
- Last Synced: 2025-03-26T06:11:46.943Z (10 months ago)
- Topics: javascript, parser, python
- Language: PEG.js
- Homepage:
- Size: 38.1 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python Parser
This is a naive Python parser in pure Javascript: no WASM, shell calls, or any non-JS. It has *no* runtime dependencies.
If you want to parse Python in the browser, then this is your project! I made this with PegJS when I couldn't find any pure-JS Python parsers. It's designed to be as easily usable as possible.
## How to Use
### Using in the Browser
1) Add the [parser.js](./parser.js) file to your page
2) Call the `pythonParser.parse(/*...code...*/)` method.
### Using in NodeJS
1) `require("parser.js")`
2) Call the `pythonParser.parse(/*...code...*/)` method.
## Parser Output
I hope that the output object can be understood with a glance at the [test output](./test-output.json). You can also take a look at the [Python grammar](./python.pegjs) to see the exact format.
## PegJS
This project uses [PegJS](https://github.com/pegjs/pegjs) for parsing, which is [available under the MIT License](https://github.com/pegjs/pegjs/blob/master/LICENSE). The PegJS-generated parser is 1 Javascript file that doesn't depend on anything!
## Questions
**How is this different from [Skulpt](https://skulpt.org/)?**
Skulpt runs Python. This project *only* parses it and then gives you a syntax tree. You can use that for whatever you like: code formatting; static code analysis; even making your own interpreter!
**I'm getting syntax errors!**
Try adding newlines to the end of your input: e.g. instead of `pythonParser.parse(input)`, try `pythonParser.parse(input + "\n\n")`. If you're experiencing a larger error, please open an issue.