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:1835–1933  ·  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

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