mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-10-31 03:55:09 +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>
		
			
				
	
	
		
			490 lines
		
	
	
	
		
			18 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			490 lines
		
	
	
	
		
			18 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
| ---
 | |
| source: crates/ruff_python_parser/tests/fixtures.rs
 | |
| input_file: crates/ruff_python_parser/resources/valid/statement/raise.py
 | |
| ---
 | |
| ## AST
 | |
| 
 | |
| ```
 | |
| Module(
 | |
|     ModModule {
 | |
|         range: 0..289,
 | |
|         body: [
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 8..13,
 | |
|                     exc: None,
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 14..21,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 20..21,
 | |
|                                 id: "a",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 22..34,
 | |
|                     exc: Some(
 | |
|                         Tuple(
 | |
|                             ExprTuple {
 | |
|                                 range: 28..34,
 | |
|                                 elts: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 29..30,
 | |
|                                             id: "a",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 32..33,
 | |
|                                             id: "b",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 ctx: Load,
 | |
|                                 parenthesized: true,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 35..46,
 | |
|                     exc: Some(
 | |
|                         Compare(
 | |
|                             ExprCompare {
 | |
|                                 range: 41..46,
 | |
|                                 left: NumberLiteral(
 | |
|                                     ExprNumberLiteral {
 | |
|                                         range: 41..42,
 | |
|                                         value: Int(
 | |
|                                             1,
 | |
|                                         ),
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 ops: [
 | |
|                                     Lt,
 | |
|                                 ],
 | |
|                                 comparators: [
 | |
|                                     NumberLiteral(
 | |
|                                         ExprNumberLiteral {
 | |
|                                             range: 45..46,
 | |
|                                             value: Int(
 | |
|                                                 2,
 | |
|                                             ),
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 47..60,
 | |
|                     exc: Some(
 | |
|                         BoolOp(
 | |
|                             ExprBoolOp {
 | |
|                                 range: 53..60,
 | |
|                                 op: And,
 | |
|                                 values: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 53..54,
 | |
|                                             id: "a",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 59..60,
 | |
|                                             id: "b",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 61..78,
 | |
|                     exc: Some(
 | |
|                         Lambda(
 | |
|                             ExprLambda {
 | |
|                                 range: 67..78,
 | |
|                                 parameters: Some(
 | |
|                                     Parameters {
 | |
|                                         range: 74..75,
 | |
|                                         posonlyargs: [],
 | |
|                                         args: [
 | |
|                                             ParameterWithDefault {
 | |
|                                                 range: 74..75,
 | |
|                                                 parameter: Parameter {
 | |
|                                                     range: 74..75,
 | |
|                                                     name: Identifier {
 | |
|                                                         id: "x",
 | |
|                                                         range: 74..75,
 | |
|                                                     },
 | |
|                                                     annotation: None,
 | |
|                                                 },
 | |
|                                                 default: None,
 | |
|                                             },
 | |
|                                         ],
 | |
|                                         vararg: None,
 | |
|                                         kwonlyargs: [],
 | |
|                                         kwarg: None,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 body: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 77..78,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 79..92,
 | |
|                     exc: Some(
 | |
|                         Await(
 | |
|                             ExprAwait {
 | |
|                                 range: 85..92,
 | |
|                                 value: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 91..92,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 93..115,
 | |
|                     exc: Some(
 | |
|                         If(
 | |
|                             ExprIf {
 | |
|                                 range: 99..115,
 | |
|                                 test: BooleanLiteral(
 | |
|                                     ExprBooleanLiteral {
 | |
|                                         range: 104..108,
 | |
|                                         value: true,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 body: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 99..100,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 orelse: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 114..115,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: None,
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 138..152,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 144..145,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 151..152,
 | |
|                                 id: "a",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 153..172,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 159..160,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         Tuple(
 | |
|                             ExprTuple {
 | |
|                                 range: 166..172,
 | |
|                                 elts: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 167..168,
 | |
|                                             id: "a",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 170..171,
 | |
|                                             id: "b",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 ctx: Load,
 | |
|                                 parenthesized: true,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 173..191,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 179..180,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         Compare(
 | |
|                             ExprCompare {
 | |
|                                 range: 186..191,
 | |
|                                 left: NumberLiteral(
 | |
|                                     ExprNumberLiteral {
 | |
|                                         range: 186..187,
 | |
|                                         value: Int(
 | |
|                                             1,
 | |
|                                         ),
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 ops: [
 | |
|                                     Lt,
 | |
|                                 ],
 | |
|                                 comparators: [
 | |
|                                     NumberLiteral(
 | |
|                                         ExprNumberLiteral {
 | |
|                                             range: 190..191,
 | |
|                                             value: Int(
 | |
|                                                 2,
 | |
|                                             ),
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 192..212,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 198..199,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         BoolOp(
 | |
|                             ExprBoolOp {
 | |
|                                 range: 205..212,
 | |
|                                 op: And,
 | |
|                                 values: [
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 205..206,
 | |
|                                             id: "a",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     Name(
 | |
|                                         ExprName {
 | |
|                                             range: 211..212,
 | |
|                                             id: "b",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 213..237,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 219..220,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         Lambda(
 | |
|                             ExprLambda {
 | |
|                                 range: 226..237,
 | |
|                                 parameters: Some(
 | |
|                                     Parameters {
 | |
|                                         range: 233..234,
 | |
|                                         posonlyargs: [],
 | |
|                                         args: [
 | |
|                                             ParameterWithDefault {
 | |
|                                                 range: 233..234,
 | |
|                                                 parameter: Parameter {
 | |
|                                                     range: 233..234,
 | |
|                                                     name: Identifier {
 | |
|                                                         id: "x",
 | |
|                                                         range: 233..234,
 | |
|                                                     },
 | |
|                                                     annotation: None,
 | |
|                                                 },
 | |
|                                                 default: None,
 | |
|                                             },
 | |
|                                         ],
 | |
|                                         vararg: None,
 | |
|                                         kwonlyargs: [],
 | |
|                                         kwarg: None,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 body: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 236..237,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 238..258,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 244..245,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         Await(
 | |
|                             ExprAwait {
 | |
|                                 range: 251..258,
 | |
|                                 value: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 257..258,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Raise(
 | |
|                 StmtRaise {
 | |
|                     range: 259..288,
 | |
|                     exc: Some(
 | |
|                         Name(
 | |
|                             ExprName {
 | |
|                                 range: 265..266,
 | |
|                                 id: "x",
 | |
|                                 ctx: Load,
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                     cause: Some(
 | |
|                         If(
 | |
|                             ExprIf {
 | |
|                                 range: 272..288,
 | |
|                                 test: BooleanLiteral(
 | |
|                                     ExprBooleanLiteral {
 | |
|                                         range: 277..281,
 | |
|                                         value: true,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 body: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 272..273,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 orelse: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 287..288,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|         ],
 | |
|     },
 | |
| )
 | |
| ```
 |