mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-11-04 05:34:54 +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>
		
			
				
	
	
		
			227 lines
		
	
	
	
		
			9.2 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			227 lines
		
	
	
	
		
			9.2 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
---
 | 
						|
source: crates/ruff_python_parser/tests/fixtures.rs
 | 
						|
input_file: crates/ruff_python_parser/resources/valid/expressions/attribute.py
 | 
						|
---
 | 
						|
## AST
 | 
						|
 | 
						|
```
 | 
						|
Module(
 | 
						|
    ModModule {
 | 
						|
        range: 0..90,
 | 
						|
        body: [
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 0..10,
 | 
						|
                    value: Attribute(
 | 
						|
                        ExprAttribute {
 | 
						|
                            range: 0..10,
 | 
						|
                            value: Name(
 | 
						|
                                ExprName {
 | 
						|
                                    range: 0..5,
 | 
						|
                                    id: "value",
 | 
						|
                                    ctx: Load,
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            attr: Identifier {
 | 
						|
                                id: "attr",
 | 
						|
                                range: 6..10,
 | 
						|
                            },
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 11..23,
 | 
						|
                    value: Call(
 | 
						|
                        ExprCall {
 | 
						|
                            range: 11..23,
 | 
						|
                            func: Attribute(
 | 
						|
                                ExprAttribute {
 | 
						|
                                    range: 11..21,
 | 
						|
                                    value: Name(
 | 
						|
                                        ExprName {
 | 
						|
                                            range: 11..16,
 | 
						|
                                            id: "value",
 | 
						|
                                            ctx: Load,
 | 
						|
                                        },
 | 
						|
                                    ),
 | 
						|
                                    attr: Identifier {
 | 
						|
                                        id: "attr",
 | 
						|
                                        range: 17..21,
 | 
						|
                                    },
 | 
						|
                                    ctx: Load,
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            arguments: Arguments {
 | 
						|
                                range: 21..23,
 | 
						|
                                args: [],
 | 
						|
                                keywords: [],
 | 
						|
                            },
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 24..36,
 | 
						|
                    value: Attribute(
 | 
						|
                        ExprAttribute {
 | 
						|
                            range: 24..36,
 | 
						|
                            value: Call(
 | 
						|
                                ExprCall {
 | 
						|
                                    range: 24..31,
 | 
						|
                                    func: Name(
 | 
						|
                                        ExprName {
 | 
						|
                                            range: 24..29,
 | 
						|
                                            id: "value",
 | 
						|
                                            ctx: Load,
 | 
						|
                                        },
 | 
						|
                                    ),
 | 
						|
                                    arguments: Arguments {
 | 
						|
                                        range: 29..31,
 | 
						|
                                        args: [],
 | 
						|
                                        keywords: [],
 | 
						|
                                    },
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            attr: Identifier {
 | 
						|
                                id: "attr",
 | 
						|
                                range: 32..36,
 | 
						|
                            },
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 37..55,
 | 
						|
                    value: Attribute(
 | 
						|
                        ExprAttribute {
 | 
						|
                            range: 37..55,
 | 
						|
                            value: Call(
 | 
						|
                                ExprCall {
 | 
						|
                                    range: 37..51,
 | 
						|
                                    func: Attribute(
 | 
						|
                                        ExprAttribute {
 | 
						|
                                            range: 37..49,
 | 
						|
                                            value: Call(
 | 
						|
                                                ExprCall {
 | 
						|
                                                    range: 37..44,
 | 
						|
                                                    func: Name(
 | 
						|
                                                        ExprName {
 | 
						|
                                                            range: 37..42,
 | 
						|
                                                            id: "value",
 | 
						|
                                                            ctx: Load,
 | 
						|
                                                        },
 | 
						|
                                                    ),
 | 
						|
                                                    arguments: Arguments {
 | 
						|
                                                        range: 42..44,
 | 
						|
                                                        args: [],
 | 
						|
                                                        keywords: [],
 | 
						|
                                                    },
 | 
						|
                                                },
 | 
						|
                                            ),
 | 
						|
                                            attr: Identifier {
 | 
						|
                                                id: "attr",
 | 
						|
                                                range: 45..49,
 | 
						|
                                            },
 | 
						|
                                            ctx: Load,
 | 
						|
                                        },
 | 
						|
                                    ),
 | 
						|
                                    arguments: Arguments {
 | 
						|
                                        range: 49..51,
 | 
						|
                                        args: [],
 | 
						|
                                        keywords: [],
 | 
						|
                                    },
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            attr: Identifier {
 | 
						|
                                id: "foo",
 | 
						|
                                range: 52..55,
 | 
						|
                            },
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 56..70,
 | 
						|
                    value: Attribute(
 | 
						|
                        ExprAttribute {
 | 
						|
                            range: 56..70,
 | 
						|
                            value: Attribute(
 | 
						|
                                ExprAttribute {
 | 
						|
                                    range: 56..66,
 | 
						|
                                    value: Name(
 | 
						|
                                        ExprName {
 | 
						|
                                            range: 56..61,
 | 
						|
                                            id: "value",
 | 
						|
                                            ctx: Load,
 | 
						|
                                        },
 | 
						|
                                    ),
 | 
						|
                                    attr: Identifier {
 | 
						|
                                        id: "attr",
 | 
						|
                                        range: 62..66,
 | 
						|
                                    },
 | 
						|
                                    ctx: Load,
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            attr: Identifier {
 | 
						|
                                id: "foo",
 | 
						|
                                range: 67..70,
 | 
						|
                            },
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
            Expr(
 | 
						|
                StmtExpr {
 | 
						|
                    range: 71..89,
 | 
						|
                    value: Attribute(
 | 
						|
                        ExprAttribute {
 | 
						|
                            range: 71..89,
 | 
						|
                            value: Call(
 | 
						|
                                ExprCall {
 | 
						|
                                    range: 71..85,
 | 
						|
                                    func: Attribute(
 | 
						|
                                        ExprAttribute {
 | 
						|
                                            range: 71..83,
 | 
						|
                                            value: Name(
 | 
						|
                                                ExprName {
 | 
						|
                                                    range: 72..77,
 | 
						|
                                                    id: "value",
 | 
						|
                                                    ctx: Load,
 | 
						|
                                                },
 | 
						|
                                            ),
 | 
						|
                                            attr: Identifier {
 | 
						|
                                                id: "attr",
 | 
						|
                                                range: 79..83,
 | 
						|
                                            },
 | 
						|
                                            ctx: Load,
 | 
						|
                                        },
 | 
						|
                                    ),
 | 
						|
                                    arguments: Arguments {
 | 
						|
                                        range: 83..85,
 | 
						|
                                        args: [],
 | 
						|
                                        keywords: [],
 | 
						|
                                    },
 | 
						|
                                },
 | 
						|
                            ),
 | 
						|
                            attr: Identifier {
 | 
						|
                                id: "foo",
 | 
						|
                                range: 86..89,
 | 
						|
                            },
 | 
						|
                            ctx: Load,
 | 
						|
                        },
 | 
						|
                    ),
 | 
						|
                },
 | 
						|
            ),
 | 
						|
        ],
 | 
						|
    },
 | 
						|
)
 | 
						|
```
 |