You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This directory has the code for python lexing, parsing and generating Abstract Syntax Trees (AST).
4
+
5
+
The steps are:
6
+
- Lexical analysis: splits the source code into tokens.
7
+
- Parsing and generating the AST: transforms those tokens into an AST. Uses `LALRPOP`, a Rust parser generator framework.
8
+
9
+
This crate is published on [https://docs.rs/rustpython-parser](https://docs.rs/rustpython-parser).
10
+
11
+
We wrote [a blog post](https://rustpython.github.io/featured/2020/03/11/thing-explainer-parser.html) with screenshots and an explanation to help you understand the steps by seeing them in action.
12
+
13
+
For more information on LALRPOP, here is a link to the [LALRPOP book](https://github.com/lalrpop/lalrpop).
14
+
15
+
There is a readme in the `src` folder with the details of each file.
16
+
17
+
## How to use
18
+
19
+
For example, one could do this:
20
+
```
21
+
use rustpython_parser::{parser, ast};
22
+
let python_source = "print('Hello world')";
23
+
let python_ast = parser::parse_expression(python_source).unwrap();
This module takes care of lexing python source text. This means source code is translated into separate tokens.
8
+
9
+
**parser.rs**
10
+
A python parsing module. Use this module to parse python code into an AST. There are three ways to parse python code. You could parse a whole program, a single statement, or a single expression.
11
+
12
+
**ast.rs**
13
+
Implements abstract syntax tree (AST) nodes for the python language. Roughly equivalent to [the python AST](https://docs.python.org/3/library/ast.html).
14
+
15
+
**python.lalrpop**
16
+
Python grammar.
17
+
18
+
**token.rs**
19
+
Different token definitions. Loosely based on token.h from CPython source.
20
+
21
+
**errors.rs**
22
+
Define internal parse error types. The goal is to provide a matching and a safe error API, masking errors from LALR.
23
+
24
+
**fstring.rs**
25
+
Format strings.
26
+
27
+
**function.rs**
28
+
Collection of functions for parsing parameters, arguments.
29
+
30
+
**location.rs**
31
+
Datatypes to support source location information.
32
+
33
+
**mode.rs**
34
+
Execution mode check. Allowed modes are `exec`, `eval` or `single`.
0 commit comments