MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_simple_statements

Method parse_simple_statements

crates/ruff_python_parser/src/parser/statement.rs:198–264  ·  view source on GitHub ↗

Parses a sequence of simple statements. If there is more than one statement in this sequence, it is expected to be separated by a semicolon. The sequence can optionally end with a semicolon, but regardless of whether a semicolon is present or not, it is expected to end with a newline. Matches the `simple_stmts` rule in the [Python grammar]. [Python grammar]: https://docs.python.org/3/reference/

(&mut self)

Source from the content-addressed store, hash-verified

196 ///
197 /// [Python grammar]: https://docs.python.org/3/reference/grammar.html
198 fn parse_simple_statements(&mut self) -> Suite {
199 let stmts_snapshot = self.stmt_scratch.snapshot();
200 let mut progress = ParserProgress::default();
201
202 loop {
203 progress.assert_progressing(self);
204
205 let stmt = self.parse_simple_statement();
206 self.stmt_scratch.push(stmt);
207
208 if !self.eat(TokenKind::Semi) {
209 if self.at_simple_stmt() {
210 // test_err simple_stmts_on_same_line_in_block
211 // if True: break; continue pass; continue break
212 self.add_error(
213 ParseErrorType::SimpleStatementsOnSameLine,
214 self.current_token_range(),
215 );
216 } else {
217 // test_ok simple_stmts_in_block
218 // if True: pass
219 // if True: pass;
220 // if True: pass; continue
221 // if True: pass; continue;
222 // x = 1
223 break;
224 }
225 }
226
227 if !self.at_simple_stmt() {
228 break;
229 }
230 }
231
232 // Ideally, we should use `expect` here but we use `eat` for better error message. Later,
233 // if the parser isn't at the start of a compound statement, we'd `expect` a newline.
234 if !self.eat(TokenKind::Newline) {
235 if self.at_compound_stmt() {
236 // test_err simple_and_compound_stmt_on_same_line_in_block
237 // if True: pass if False: pass
238 // if True: pass; if False: pass
239 self.add_error(
240 ParseErrorType::SimpleAndCompoundStatementOnSameLine,
241 self.current_token_range(),
242 );
243 } else {
244 // test_err multiple_clauses_on_same_line
245 // if True: pass elif False: pass else: pass
246 // if True: pass; elif False: pass; else: pass
247 // for x in iter: break else: pass
248 // for x in iter: break; else: pass
249 // try: pass except exc: pass else: pass finally: pass
250 // try: pass; except exc: pass; else: pass; finally: pass
251 self.add_error(
252 ParseErrorType::ExpectedToken {
253 found: self.current_token_kind(),
254 expected: TokenKind::Newline,
255 },

Callers 1

parse_bodyMethod · 0.80

Calls 12

assert_progressingMethod · 0.80
at_simple_stmtMethod · 0.80
current_token_rangeMethod · 0.80
at_compound_stmtMethod · 0.80
current_token_kindMethod · 0.80
take_thin_vecMethod · 0.80
defaultFunction · 0.50
snapshotMethod · 0.45
pushMethod · 0.45
eatMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected