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:197–263  ·  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

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

Callers 1

parse_bodyMethod · 0.80

Calls 11

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

Tested by

no test coverage detected