mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-10-31 12:05:57 +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>
		
			
				
	
	
		
			613 lines
		
	
	
	
		
			25 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			613 lines
		
	
	
	
		
			25 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
| ---
 | |
| source: crates/ruff_python_parser/tests/fixtures.rs
 | |
| input_file: crates/ruff_python_parser/resources/valid/expressions/if.py
 | |
| ---
 | |
| ## AST
 | |
| 
 | |
| ```
 | |
| Module(
 | |
|     ModModule {
 | |
|         range: 0..423,
 | |
|         body: [
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 0..16,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 0..16,
 | |
|                             test: BooleanLiteral(
 | |
|                                 ExprBooleanLiteral {
 | |
|                                     range: 5..9,
 | |
|                                     value: true,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 0..1,
 | |
|                                     id: "a",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 15..16,
 | |
|                                     id: "b",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 17..35,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 17..35,
 | |
|                             test: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 24..25,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Call(
 | |
|                                 ExprCall {
 | |
|                                     range: 17..20,
 | |
|                                     func: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 17..18,
 | |
|                                             id: "f",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     arguments: Arguments {
 | |
|                                         range: 18..20,
 | |
|                                         args: [],
 | |
|                                         keywords: [],
 | |
|                                     },
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: NoneLiteral(
 | |
|                                 ExprNoneLiteral {
 | |
|                                     range: 31..35,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 36..61,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 36..61,
 | |
|                             test: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 41..42,
 | |
|                                     id: "b",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 36..37,
 | |
|                                     id: "a",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: If(
 | |
|                                 ExprIf {
 | |
|                                     range: 48..61,
 | |
|                                     test: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 53..54,
 | |
|                                             id: "d",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     body: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 48..49,
 | |
|                                             id: "c",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     orelse: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 60..61,
 | |
|                                             id: "e",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 62..84,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 62..84,
 | |
|                             test: Compare(
 | |
|                                 ExprCompare {
 | |
|                                     range: 71..76,
 | |
|                                     left: NumberLiteral(
 | |
|                                         ExprNumberLiteral {
 | |
|                                             range: 71..72,
 | |
|                                             value: Int(
 | |
|                                                 1,
 | |
|                                             ),
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     ops: [
 | |
|                                         Lt,
 | |
|                                     ],
 | |
|                                     comparators: [
 | |
|                                         NumberLiteral(
 | |
|                                             ExprNumberLiteral {
 | |
|                                                 range: 75..76,
 | |
|                                                 value: Int(
 | |
|                                                     0,
 | |
|                                                 ),
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     ],
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: BinOp(
 | |
|                                 ExprBinOp {
 | |
|                                     range: 62..67,
 | |
|                                     left: NumberLiteral(
 | |
|                                         ExprNumberLiteral {
 | |
|                                             range: 62..63,
 | |
|                                             value: Int(
 | |
|                                                 1,
 | |
|                                             ),
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     op: Add,
 | |
|                                     right: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 66..67,
 | |
|                                             id: "x",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: UnaryOp(
 | |
|                                 ExprUnaryOp {
 | |
|                                     range: 82..84,
 | |
|                                     op: USub,
 | |
|                                     operand: NumberLiteral(
 | |
|                                         ExprNumberLiteral {
 | |
|                                             range: 83..84,
 | |
|                                             value: Int(
 | |
|                                                 1,
 | |
|                                             ),
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 85..108,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 85..108,
 | |
|                             test: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 96..97,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: BoolOp(
 | |
|                                 ExprBoolOp {
 | |
|                                     range: 85..92,
 | |
|                                     op: And,
 | |
|                                     values: [
 | |
|                                         Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 85..86,
 | |
|                                                 id: "a",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 91..92,
 | |
|                                                 id: "b",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     ],
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: BooleanLiteral(
 | |
|                                 ExprBooleanLiteral {
 | |
|                                     range: 103..108,
 | |
|                                     value: false,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 109..127,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 109..127,
 | |
|                             test: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 119..120,
 | |
|                                     id: "y",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Compare(
 | |
|                                 ExprCompare {
 | |
|                                     range: 109..115,
 | |
|                                     left: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 109..110,
 | |
|                                             id: "x",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     ops: [
 | |
|                                         LtE,
 | |
|                                     ],
 | |
|                                     comparators: [
 | |
|                                         Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 114..115,
 | |
|                                                 id: "y",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     ],
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 126..127,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 128..154,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 128..154,
 | |
|                             test: BoolOp(
 | |
|                                 ExprBoolOp {
 | |
|                                     range: 136..143,
 | |
|                                     op: And,
 | |
|                                     values: [
 | |
|                                         Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 136..137,
 | |
|                                                 id: "a",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 142..143,
 | |
|                                                 id: "b",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     ],
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: BooleanLiteral(
 | |
|                                 ExprBooleanLiteral {
 | |
|                                     range: 128..132,
 | |
|                                     value: true,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: BooleanLiteral(
 | |
|                                 ExprBooleanLiteral {
 | |
|                                     range: 149..154,
 | |
|                                     value: false,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 155..171,
 | |
|                     value: Tuple(
 | |
|                         ExprTuple {
 | |
|                             range: 155..171,
 | |
|                             elts: [
 | |
|                                 NumberLiteral(
 | |
|                                     ExprNumberLiteral {
 | |
|                                         range: 155..156,
 | |
|                                         value: Int(
 | |
|                                             1,
 | |
|                                         ),
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 If(
 | |
|                                     ExprIf {
 | |
|                                         range: 158..171,
 | |
|                                         test: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 163..164,
 | |
|                                                 id: "a",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         body: NumberLiteral(
 | |
|                                             ExprNumberLiteral {
 | |
|                                                 range: 158..159,
 | |
|                                                 value: Int(
 | |
|                                                     1,
 | |
|                                                 ),
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         orelse: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 170..171,
 | |
|                                                 id: "c",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     },
 | |
|                                 ),
 | |
|                             ],
 | |
|                             ctx: Load,
 | |
|                             parenthesized: false,
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 214..240,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 214..240,
 | |
|                             test: BooleanLiteral(
 | |
|                                 ExprBooleanLiteral {
 | |
|                                     range: 219..223,
 | |
|                                     value: true,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 214..215,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Lambda(
 | |
|                                 ExprLambda {
 | |
|                                     range: 229..240,
 | |
|                                     parameters: Some(
 | |
|                                         Parameters {
 | |
|                                             range: 236..237,
 | |
|                                             posonlyargs: [],
 | |
|                                             args: [
 | |
|                                                 ParameterWithDefault {
 | |
|                                                     range: 236..237,
 | |
|                                                     parameter: Parameter {
 | |
|                                                         range: 236..237,
 | |
|                                                         name: Identifier {
 | |
|                                                             id: "y",
 | |
|                                                             range: 236..237,
 | |
|                                                         },
 | |
|                                                         annotation: None,
 | |
|                                                     },
 | |
|                                                     default: None,
 | |
|                                                 },
 | |
|                                             ],
 | |
|                                             vararg: None,
 | |
|                                             kwonlyargs: [],
 | |
|                                             kwarg: None,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     body: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 239..240,
 | |
|                                             id: "y",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 302..323,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 302..323,
 | |
|                             test: Yield(
 | |
|                                 ExprYield {
 | |
|                                     range: 308..315,
 | |
|                                     value: Some(
 | |
|                                         Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 314..315,
 | |
|                                                 id: "x",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 302..303,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 322..323,
 | |
|                                     id: "y",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 324..350,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 324..350,
 | |
|                             test: YieldFrom(
 | |
|                                 ExprYieldFrom {
 | |
|                                     range: 330..342,
 | |
|                                     value: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 341..342,
 | |
|                                             id: "x",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 324..325,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 349..350,
 | |
|                                     id: "y",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 351..376,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 351..376,
 | |
|                             test: Lambda(
 | |
|                                 ExprLambda {
 | |
|                                     range: 357..368,
 | |
|                                     parameters: Some(
 | |
|                                         Parameters {
 | |
|                                             range: 364..365,
 | |
|                                             posonlyargs: [],
 | |
|                                             args: [
 | |
|                                                 ParameterWithDefault {
 | |
|                                                     range: 364..365,
 | |
|                                                     parameter: Parameter {
 | |
|                                                         range: 364..365,
 | |
|                                                         name: Identifier {
 | |
|                                                             id: "x",
 | |
|                                                             range: 364..365,
 | |
|                                                         },
 | |
|                                                         annotation: None,
 | |
|                                                     },
 | |
|                                                     default: None,
 | |
|                                                 },
 | |
|                                             ],
 | |
|                                             vararg: None,
 | |
|                                             kwonlyargs: [],
 | |
|                                             kwarg: None,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                     body: Name(
 | |
|                                         ExprName {
 | |
|                                             range: 367..368,
 | |
|                                             id: "x",
 | |
|                                             ctx: Load,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 351..352,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 375..376,
 | |
|                                     id: "y",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|             Expr(
 | |
|                 StmtExpr {
 | |
|                     range: 408..423,
 | |
|                     value: If(
 | |
|                         ExprIf {
 | |
|                             range: 409..422,
 | |
|                             test: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 414..415,
 | |
|                                     id: "y",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 409..410,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                             orelse: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 421..422,
 | |
|                                     id: "z",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                 },
 | |
|             ),
 | |
|         ],
 | |
|     },
 | |
| )
 | |
| ```
 |