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>
		
			
				
	
	
		
			461 lines
		
	
	
	
		
			19 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
			
		
		
	
	
			461 lines
		
	
	
	
		
			19 KiB
		
	
	
	
		
			Text
		
	
	
	
	
	
| ---
 | |
| source: crates/ruff_python_parser/tests/fixtures.rs
 | |
| input_file: crates/ruff_python_parser/resources/valid/statement/while.py
 | |
| ---
 | |
| ## AST
 | |
| 
 | |
| ```
 | |
| Module(
 | |
|     ModModule {
 | |
|         range: 0..314,
 | |
|         body: [
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 0..16,
 | |
|                     test: Name(
 | |
|                         ExprName {
 | |
|                             range: 6..7,
 | |
|                             id: "x",
 | |
|                             ctx: Load,
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 13..16,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 13..16,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [],
 | |
|                 },
 | |
|             ),
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 18..61,
 | |
|                     test: BoolOp(
 | |
|                         ExprBoolOp {
 | |
|                             range: 24..37,
 | |
|                             op: And,
 | |
|                             values: [
 | |
|                                 Compare(
 | |
|                                     ExprCompare {
 | |
|                                         range: 25..30,
 | |
|                                         left: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 25..26,
 | |
|                                                 id: "x",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         ops: [
 | |
|                                             Gt,
 | |
|                                         ],
 | |
|                                         comparators: [
 | |
|                                             NumberLiteral(
 | |
|                                                 ExprNumberLiteral {
 | |
|                                                     range: 29..30,
 | |
|                                                     value: Int(
 | |
|                                                         1,
 | |
|                                                     ),
 | |
|                                                 },
 | |
|                                             ),
 | |
|                                         ],
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 Name(
 | |
|                                     ExprName {
 | |
|                                         range: 36..37,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             ],
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Pass(
 | |
|                             StmtPass {
 | |
|                                 range: 43..47,
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 58..61,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 58..61,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 63..152,
 | |
|                     test: BoolOp(
 | |
|                         ExprBoolOp {
 | |
|                             range: 69..76,
 | |
|                             op: And,
 | |
|                             values: [
 | |
|                                 Name(
 | |
|                                     ExprName {
 | |
|                                         range: 69..70,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 Name(
 | |
|                                     ExprName {
 | |
|                                         range: 75..76,
 | |
|                                         id: "y",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             ],
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 82..85,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 82..85,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 90..111,
 | |
|                                 value: Call(
 | |
|                                     ExprCall {
 | |
|                                         range: 90..111,
 | |
|                                         func: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 90..95,
 | |
|                                                 id: "print",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         arguments: Arguments {
 | |
|                                             range: 95..111,
 | |
|                                             args: [
 | |
|                                                 StringLiteral(
 | |
|                                                     ExprStringLiteral {
 | |
|                                                         range: 96..110,
 | |
|                                                         value: StringLiteralValue {
 | |
|                                                             inner: Single(
 | |
|                                                                 StringLiteral {
 | |
|                                                                     range: 96..110,
 | |
|                                                                     value: "Hello World!",
 | |
|                                                                     flags: StringLiteralFlags {
 | |
|                                                                         quote_style: Single,
 | |
|                                                                         prefix: Empty,
 | |
|                                                                         triple_quoted: false,
 | |
|                                                                     },
 | |
|                                                                 },
 | |
|                                                             ),
 | |
|                                                         },
 | |
|                                                     },
 | |
|                                                 ),
 | |
|                                             ],
 | |
|                                             keywords: [],
 | |
|                                         },
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 123..144,
 | |
|                                 value: Call(
 | |
|                                     ExprCall {
 | |
|                                         range: 123..144,
 | |
|                                         func: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 123..128,
 | |
|                                                 id: "print",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         arguments: Arguments {
 | |
|                                             range: 128..144,
 | |
|                                             args: [
 | |
|                                                 StringLiteral(
 | |
|                                                     ExprStringLiteral {
 | |
|                                                         range: 129..143,
 | |
|                                                         value: StringLiteralValue {
 | |
|                                                             inner: Single(
 | |
|                                                                 StringLiteral {
 | |
|                                                                     range: 129..143,
 | |
|                                                                     value: "Olá, Mundo!",
 | |
|                                                                     flags: StringLiteralFlags {
 | |
|                                                                         quote_style: Single,
 | |
|                                                                         prefix: Empty,
 | |
|                                                                         triple_quoted: false,
 | |
|                                                                     },
 | |
|                                                                 },
 | |
|                                                             ),
 | |
|                                                         },
 | |
|                                                     },
 | |
|                                                 ),
 | |
|                                             ],
 | |
|                                             keywords: [],
 | |
|                                         },
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 149..152,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 149..152,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 154..171,
 | |
|                     test: Named(
 | |
|                         ExprNamed {
 | |
|                             range: 160..166,
 | |
|                             target: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 160..161,
 | |
|                                     id: "a",
 | |
|                                     ctx: Store,
 | |
|                                 },
 | |
|                             ),
 | |
|                             value: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 165..166,
 | |
|                                     id: "b",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 168..171,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 168..171,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [],
 | |
|                 },
 | |
|             ),
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 172..197,
 | |
|                     test: BoolOp(
 | |
|                         ExprBoolOp {
 | |
|                             range: 178..192,
 | |
|                             op: And,
 | |
|                             values: [
 | |
|                                 Named(
 | |
|                                     ExprNamed {
 | |
|                                         range: 179..185,
 | |
|                                         target: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 179..180,
 | |
|                                                 id: "a",
 | |
|                                                 ctx: Store,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                         value: Name(
 | |
|                                             ExprName {
 | |
|                                                 range: 184..185,
 | |
|                                                 id: "b",
 | |
|                                                 ctx: Load,
 | |
|                                             },
 | |
|                                         ),
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 Name(
 | |
|                                     ExprName {
 | |
|                                         range: 191..192,
 | |
|                                         id: "c",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             ],
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 194..197,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 194..197,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [],
 | |
|                 },
 | |
|             ),
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 198..220,
 | |
|                     test: Lambda(
 | |
|                         ExprLambda {
 | |
|                             range: 204..215,
 | |
|                             parameters: Some(
 | |
|                                 Parameters {
 | |
|                                     range: 211..212,
 | |
|                                     posonlyargs: [],
 | |
|                                     args: [
 | |
|                                         ParameterWithDefault {
 | |
|                                             range: 211..212,
 | |
|                                             parameter: Parameter {
 | |
|                                                 range: 211..212,
 | |
|                                                 name: Identifier {
 | |
|                                                     id: "x",
 | |
|                                                     range: 211..212,
 | |
|                                                 },
 | |
|                                                 annotation: None,
 | |
|                                             },
 | |
|                                             default: None,
 | |
|                                         },
 | |
|                                     ],
 | |
|                                     vararg: None,
 | |
|                                     kwonlyargs: [],
 | |
|                                     kwarg: None,
 | |
|                                 },
 | |
|                             ),
 | |
|                             body: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 214..215,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 217..220,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 217..220,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [],
 | |
|                 },
 | |
|             ),
 | |
|             While(
 | |
|                 StmtWhile {
 | |
|                     range: 221..239,
 | |
|                     test: Await(
 | |
|                         ExprAwait {
 | |
|                             range: 227..234,
 | |
|                             value: Name(
 | |
|                                 ExprName {
 | |
|                                     range: 233..234,
 | |
|                                     id: "x",
 | |
|                                     ctx: Load,
 | |
|                                 },
 | |
|                             ),
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         Expr(
 | |
|                             StmtExpr {
 | |
|                                 range: 236..239,
 | |
|                                 value: EllipsisLiteral(
 | |
|                                     ExprEllipsisLiteral {
 | |
|                                         range: 236..239,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     orelse: [],
 | |
|                 },
 | |
|             ),
 | |
|             If(
 | |
|                 StmtIf {
 | |
|                     range: 241..313,
 | |
|                     test: BooleanLiteral(
 | |
|                         ExprBooleanLiteral {
 | |
|                             range: 244..248,
 | |
|                             value: true,
 | |
|                         },
 | |
|                     ),
 | |
|                     body: [
 | |
|                         While(
 | |
|                             StmtWhile {
 | |
|                                 range: 254..298,
 | |
|                                 test: Name(
 | |
|                                     ExprName {
 | |
|                                         range: 260..261,
 | |
|                                         id: "x",
 | |
|                                         ctx: Load,
 | |
|                                     },
 | |
|                                 ),
 | |
|                                 body: [
 | |
|                                     Pass(
 | |
|                                         StmtPass {
 | |
|                                             range: 271..275,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                                 orelse: [
 | |
|                                     Pass(
 | |
|                                         StmtPass {
 | |
|                                             range: 294..298,
 | |
|                                         },
 | |
|                                     ),
 | |
|                                 ],
 | |
|                             },
 | |
|                         ),
 | |
|                     ],
 | |
|                     elif_else_clauses: [
 | |
|                         ElifElseClause {
 | |
|                             range: 299..313,
 | |
|                             test: None,
 | |
|                             body: [
 | |
|                                 Pass(
 | |
|                                     StmtPass {
 | |
|                                         range: 309..313,
 | |
|                                     },
 | |
|                                 ),
 | |
|                             ],
 | |
|                         },
 | |
|                     ],
 | |
|                 },
 | |
|             ),
 | |
|         ],
 | |
|     },
 | |
| )
 | |
| ```
 |