mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-11-04 13:39:07 +00:00 
			
		
		
		
	(Supersedes #9152, authored by @LaBatata101) ## Summary This PR replaces the current parser generated from LALRPOP to a hand-written recursive descent parser. It also updates the grammar for [PEP 646](https://peps.python.org/pep-0646/) so that the parser outputs the correct AST. For example, in `data[*x]`, the index expression is now a tuple with a single starred expression instead of just a starred expression. Beyond the performance improvements, the parser is also error resilient and can provide better error messages. The behavior as seen by any downstream tools isn't changed. That is, the linter and formatter can still assume that the parser will _stop_ at the first syntax error. This will be updated in the following months. For more details about the change here, refer to the PR corresponding to the individual commits and the release blog post. ## Test Plan Write _lots_ and _lots_ of tests for both valid and invalid syntax and verify the output. ## Acknowledgements - @MichaReiser for reviewing 100+ parser PRs and continuously providing guidance throughout the project - @LaBatata101 for initiating the transition to a hand-written parser in #9152 - @addisoncrump for implementing the fuzzer which helped [catch](https://github.com/astral-sh/ruff/pull/10903) [a](https://github.com/astral-sh/ruff/pull/10910) [lot](https://github.com/astral-sh/ruff/pull/10966) [of](https://github.com/astral-sh/ruff/pull/10896) [bugs](https://github.com/astral-sh/ruff/pull/10877) --------- Co-authored-by: Victor Hugo Gomes <labatata101@linuxmail.org> Co-authored-by: Micha Reiser <micha@reiser.io>
		
			
				
	
	
		
			248 lines
		
	
	
	
		
			8.2 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			248 lines
		
	
	
	
		
			8.2 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
---
 | 
						|
source: crates/ruff_python_parser/tests/fixtures.rs
 | 
						|
input_file: crates/ruff_python_parser/resources/valid/statement/simple.py
 | 
						|
---
 | 
						|
## AST
 | 
						|
 | 
						|
```
 | 
						|
Module(
 | 
						|
    ModModule {
 | 
						|
        range: 0..172,
 | 
						|
        body: [
 | 
						|
            Continue(
 | 
						|
                StmtContinue {
 | 
						|
                    range: 61..69,
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Break(
 | 
						|
                StmtBreak {
 | 
						|
                    range: 70..75,
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            If(
 | 
						|
                StmtIf {
 | 
						|
                    range: 77..86,
 | 
						|
                    test: Name(
 | 
						|
                        ExprName {
 | 
						|
                            range: 80..81,
 | 
						|
                            id: "x",
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                    body: [
 | 
						|
                        Expr(
 | 
						|
                            StmtExpr {
 | 
						|
                                range: 83..86,
 | 
						|
                                value: EllipsisLiteral(
 | 
						|
                                    ExprEllipsisLiteral {
 | 
						|
                                        range: 83..86,
 | 
						|
                                    },
 | 
						|
                                ),
 | 
						|
                            },
 | 
						|
                        ),
 | 
						|
                    ],
 | 
						|
                    elif_else_clauses: [],
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            If(
 | 
						|
                StmtIf {
 | 
						|
                    range: 87..100,
 | 
						|
                    test: BooleanLiteral(
 | 
						|
                        ExprBooleanLiteral {
 | 
						|
                            range: 90..94,
 | 
						|
                            value: true,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                    body: [
 | 
						|
                        Pass(
 | 
						|
                            StmtPass {
 | 
						|
                                range: 96..100,
 | 
						|
                            },
 | 
						|
                        ),
 | 
						|
                    ],
 | 
						|
                    elif_else_clauses: [],
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 101..102,
 | 
						|
                    value: NumberLiteral(
 | 
						|
                        ExprNumberLiteral {
 | 
						|
                            range: 101..102,
 | 
						|
                            value: Int(
 | 
						|
                                1,
 | 
						|
                            ),
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 104..105,
 | 
						|
                    value: NumberLiteral(
 | 
						|
                        ExprNumberLiteral {
 | 
						|
                            range: 104..105,
 | 
						|
                            value: Int(
 | 
						|
                                2,
 | 
						|
                            ),
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Pass(
 | 
						|
                StmtPass {
 | 
						|
                    range: 107..111,
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 112..113,
 | 
						|
                    value: NumberLiteral(
 | 
						|
                        ExprNumberLiteral {
 | 
						|
                            range: 112..113,
 | 
						|
                            value: Int(
 | 
						|
                                1,
 | 
						|
                            ),
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 115..118,
 | 
						|
                    value: EllipsisLiteral(
 | 
						|
                        ExprEllipsisLiteral {
 | 
						|
                            range: 115..118,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 120..133,
 | 
						|
                    value: If(
 | 
						|
                        ExprIf {
 | 
						|
                            range: 120..133,
 | 
						|
                            test: Name(
 | 
						|
                                ExprName {
 | 
						|
                                    range: 125..126,
 | 
						|
                                    id: "b",
 | 
						|
                                    ctx: Load,
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            body: Name(
 | 
						|
                                ExprName {
 | 
						|
                                    range: 120..121,
 | 
						|
                                    id: "a",
 | 
						|
                                    ctx: Load,
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            orelse: Name(
 | 
						|
                                ExprName {
 | 
						|
                                    range: 132..133,
 | 
						|
                                    id: "c",
 | 
						|
                                    ctx: Load,
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            If(
 | 
						|
                StmtIf {
 | 
						|
                    range: 135..157,
 | 
						|
                    test: Name(
 | 
						|
                        ExprName {
 | 
						|
                            range: 138..139,
 | 
						|
                            id: "c",
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                    body: [
 | 
						|
                        Expr(
 | 
						|
                            StmtExpr {
 | 
						|
                                range: 141..142,
 | 
						|
                                value: Name(
 | 
						|
                                    ExprName {
 | 
						|
                                        range: 141..142,
 | 
						|
                                        id: "B",
 | 
						|
                                        ctx: Load,
 | 
						|
                                    },
 | 
						|
                                ),
 | 
						|
                            },
 | 
						|
                        ),
 | 
						|
                        Delete(
 | 
						|
                            StmtDelete {
 | 
						|
                                range: 144..149,
 | 
						|
                                targets: [
 | 
						|
                                    Name(
 | 
						|
                                        ExprName {
 | 
						|
                                            range: 148..149,
 | 
						|
                                            id: "A",
 | 
						|
                                            ctx: Del,
 | 
						|
                                        },
 | 
						|
                                    ),
 | 
						|
                                ],
 | 
						|
                            },
 | 
						|
                        ),
 | 
						|
                    ],
 | 
						|
                    elif_else_clauses: [
 | 
						|
                        ElifElseClause {
 | 
						|
                            range: 150..157,
 | 
						|
                            test: None,
 | 
						|
                            body: [
 | 
						|
                                Expr(
 | 
						|
                                    StmtExpr {
 | 
						|
                                        range: 156..157,
 | 
						|
                                        value: Name(
 | 
						|
                                            ExprName {
 | 
						|
                                                range: 156..157,
 | 
						|
                                                id: "C",
 | 
						|
                                                ctx: Load,
 | 
						|
                                            },
 | 
						|
                                        ),
 | 
						|
                                    },
 | 
						|
                                ),
 | 
						|
                            ],
 | 
						|
                        },
 | 
						|
                    ],
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            If(
 | 
						|
                StmtIf {
 | 
						|
                    range: 158..171,
 | 
						|
                    test: Name(
 | 
						|
                        ExprName {
 | 
						|
                            range: 161..162,
 | 
						|
                            id: "x",
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                    body: [
 | 
						|
                        Expr(
 | 
						|
                            StmtExpr {
 | 
						|
                                range: 164..171,
 | 
						|
                                value: Yield(
 | 
						|
                                    ExprYield {
 | 
						|
                                        range: 164..171,
 | 
						|
                                        value: Some(
 | 
						|
                                            Name(
 | 
						|
                                                ExprName {
 | 
						|
                                                    range: 170..171,
 | 
						|
                                                    id: "x",
 | 
						|
                                                    ctx: Load,
 | 
						|
                                                },
 | 
						|
                                            ),
 | 
						|
                                        ),
 | 
						|
                                    },
 | 
						|
                                ),
 | 
						|
                            },
 | 
						|
                        ),
 | 
						|
                    ],
 | 
						|
                    elif_else_clauses: [],
 | 
						|
                },
 | 
						|
            ),
 | 
						|
        ],
 | 
						|
    },
 | 
						|
)
 | 
						|
```
 |