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:1844–1942  ·  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

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