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:1851–1949  ·  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

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