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

Method parse_for_statement

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

Parses a `for` statement. The given `start` offset is the start of either the `for` token or the `async` token if it's an async for statement. # Panics If the parser isn't positioned at a `for` token. See:

(&mut self, start: TextSize)

Source from the content-addressed store, hash-verified

1844 ///
1845 /// See: <https://docs.python.org/3/reference/compound_stmts.html#the-for-statement>
1846 fn parse_for_statement(&mut self, start: TextSize) -> ast::StmtFor {
1847 self.bump(TokenKind::For);
1848
1849 // test_err for_stmt_missing_target
1850 // for in x: ...
1851
1852 // test_ok for_in_target_valid_expr
1853 // for d[x in y] in target: ...
1854 // for (x in y)[0] in iter: ...
1855 // for (x in y).attr in iter: ...
1856
1857 // test_err for_stmt_invalid_target_in_keyword
1858 // for d(x in y) in target: ...
1859 // for (x in y)() in iter: ...
1860 // for (x in y) in iter: ...
1861 // for (x in y, z) in iter: ...
1862 // for [x in y, z] in iter: ...
1863 // for {x in y, z} in iter: ...
1864
1865 // test_err for_stmt_invalid_target_binary_expr
1866 // for x not in y in z: ...
1867 // for x == y in z: ...
1868 // for x or y in z: ...
1869 // for -x in y: ...
1870 // for not x in y: ...
1871 // for x | y in z: ...
1872 let mut target =
1873 self.parse_expression_list(ExpressionContext::starred_conditional().with_in_excluded());
1874
1875 helpers::set_expr_ctx(&mut target.expr, ExprContext::Store);
1876
1877 // test_err for_stmt_invalid_target
1878 // for 1 in x: ...
1879 // for "a" in x: ...
1880 // for *x and y in z: ...
1881 // for *x | y in z: ...
1882 // for await x in z: ...
1883 // for yield x in y: ...
1884 // for [x, 1, y, *["a"]] in z: ...
1885 self.validate_assignment_target(&target.expr);
1886
1887 // test_err for_stmt_missing_in_keyword
1888 // for a b: ...
1889 // for a: ...
1890 self.expect(TokenKind::In);
1891
1892 // test_err for_stmt_missing_iter
1893 // for x in:
1894 // a = 1
1895
1896 // test_err for_stmt_invalid_iter_expr
1897 // for x in *a and b: ...
1898 // for x in yield a: ...
1899 // for target in x := 1: ...
1900 let iter = self.parse_expression_list(ExpressionContext::starred_bitwise_or());
1901
1902 // test_ok for_iter_unpack_py39
1903 // # parse_options: {"target-version": "3.9"}

Callers 2

parse_statementMethod · 0.80
parse_async_statementMethod · 0.80

Calls 10

set_expr_ctxFunction · 0.85
parse_expression_listMethod · 0.80
with_in_excludedMethod · 0.80
expectMethod · 0.80
check_tuple_unpackingMethod · 0.80
parse_bodyMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
eatMethod · 0.45

Tested by

no test coverage detected