mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-10-30 19:47:52 +00:00 
			
		
		
		
	 13ffb5bc19
			
		
	
	
		13ffb5bc19
		
			
		
	
	
	
	
		
			
			(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>
		
			
				
	
	
		
			299 lines
		
	
	
	
		
			12 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			299 lines
		
	
	
	
		
			12 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
| ---
 | |
| source: crates/ruff_python_parser/tests/fixtures.rs
 | |
| input_file: crates/ruff_python_parser/resources/valid/statement/delete.py
 | |
| ---
 | |
| ## AST
 | |
| 
 | |
| ```
 | |
| Module(
 | |
|     ModModule {
 | |
|         range: 0..122,
 | |
|         body: [
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 0..5,
 | |
|                     targets: [
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 4..5,
 | |
|                                 id: "x",
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 6..13,
 | |
|                     targets: [
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 11..12,
 | |
|                                 id: "x",
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 14..23,
 | |
|                     targets: [
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 18..19,
 | |
|                                 id: "a",
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 21..22,
 | |
|                                 id: "b",
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 24..40,
 | |
|                     targets: [
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 28..29,
 | |
|                                 id: "a",
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                         Tuple(
 | |
|                             ExprTuple {
 | |
|                                 range: 31..37,
 | |
|                                 elts: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 32..33,
 | |
|                                             id: "b",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 35..36,
 | |
|                                             id: "c",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 ctx: Del,
 | |
|                                 parenthesized: true,
 | |
|                             },
 | |
|                         ),
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 39..40,
 | |
|                                 id: "d",
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 41..51,
 | |
|                     targets: [
 | |
|                         List(
 | |
|                             ExprList {
 | |
|                                 range: 45..51,
 | |
|                                 elts: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 46..47,
 | |
|                                             id: "a",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 49..50,
 | |
|                                             id: "b",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 52..70,
 | |
|                     targets: [
 | |
|                         List(
 | |
|                             ExprList {
 | |
|                                 range: 56..70,
 | |
|                                 elts: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 57..58,
 | |
|                                             id: "a",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     List(
 | |
|                                         ExprList {
 | |
|                                             range: 60..66,
 | |
|                                             elts: [
 | |
|                                                 Name(
 | |
|                                                     ExprName {
 | |
|                                                         range: 61..62,
 | |
|                                                         id: "b",
 | |
|                                                         ctx: Del,
 | |
|                                                     },
 | |
|                                                 ),
 | |
|                                                 Name(
 | |
|                                                     ExprName {
 | |
|                                                         range: 64..65,
 | |
|                                                         id: "c",
 | |
|                                                         ctx: Del,
 | |
|                                                     },
 | |
|                                                 ),
 | |
|                                             ],
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 68..69,
 | |
|                                             id: "d",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 71..78,
 | |
|                     targets: [
 | |
|                         Attribute(
 | |
|                             ExprAttribute {
 | |
|                                 range: 75..78,
 | |
|                                 value: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 75..76,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 attr: Identifier {
 | |
|                                     id: "y",
 | |
|                                     range: 77..78,
 | |
|                                 },
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 79..87,
 | |
|                     targets: [
 | |
|                         Subscript(
 | |
|                             ExprSubscript {
 | |
|                                 range: 83..87,
 | |
|                                 value: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 83..84,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 slice: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 85..86,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 ctx: Del,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             Delete(
 | |
|                 StmtDelete {
 | |
|                     range: 88..121,
 | |
|                     targets: [
 | |
|                         Tuple(
 | |
|                             ExprTuple {
 | |
|                                 range: 92..121,
 | |
|                                 elts: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 98..99,
 | |
|                                             id: "x",
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Attribute(
 | |
|                                         ExprAttribute {
 | |
|                                             range: 105..108,
 | |
|                                             value: Name(
 | |
|                                                 ExprName {
 | |
|                                                     range: 105..106,
 | |
|                                                     id: "x",
 | |
|                                                     ctx: Load,
 | |
|                                                 },
 | |
|                                             ),
 | |
|                                             attr: Identifier {
 | |
|                                                 id: "y",
 | |
|                                                 range: 107..108,
 | |
|                                             },
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Subscript(
 | |
|                                         ExprSubscript {
 | |
|                                             range: 114..118,
 | |
|                                             value: Name(
 | |
|                                                 ExprName {
 | |
|                                                     range: 114..115,
 | |
|                                                     id: "x",
 | |
|                                                     ctx: Load,
 | |
|                                                 },
 | |
|                                             ),
 | |
|                                             slice: Name(
 | |
|                                                 ExprName {
 | |
|                                                     range: 116..117,
 | |
|                                                     id: "y",
 | |
|                                                     ctx: Load,
 | |
|                                                 },
 | |
|                                             ),
 | |
|                                             ctx: Del,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 ctx: Del,
 | |
|                                 parenthesized: true,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|         ],
 | |
|     },
 | |
| )
 | |
| ```
 |