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

Method parse_async_statement

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

Parses a statement that is valid after an `async` token. If the statement is not a valid `async` statement, an error will be reported and it will be parsed as a statement. See: - - - <https://docs.python.org/3/reference/compound_stmt

(&mut self)

Source from the content-addressed store, hash-verified

2872 /// - <https://docs.python.org/3/reference/compound_stmts.html#the-async-for-statement>
2873 /// - <https://docs.python.org/3/reference/compound_stmts.html#coroutine-function-definition>
2874 fn parse_async_statement(&mut self) -> Stmt {
2875 let async_start = self.node_start();
2876 self.bump(TokenKind::Async);
2877
2878 match self.current_token_kind() {
2879 // test_ok async_function_definition
2880 // async def foo(): ...
2881 TokenKind::Def => Stmt::FunctionDef(ast::StmtFunctionDef {
2882 is_async: true,
2883 ..self.parse_function_definition(DecoratorList::new(), async_start)
2884 }),
2885
2886 // test_ok async_with_statement
2887 // async with item: ...
2888 TokenKind::With => Stmt::With(ast::StmtWith {
2889 is_async: true,
2890 ..self.parse_with_statement(async_start)
2891 }),
2892
2893 // test_ok async_for_statement
2894 // async for target in iter: ...
2895 TokenKind::For => Stmt::For(ast::StmtFor {
2896 is_async: true,
2897 ..self.parse_for_statement(async_start)
2898 }),
2899
2900 kind => {
2901 // test_err async_unexpected_token
2902 // async class Foo: ...
2903 // async while test: ...
2904 // async x = 1
2905 // async async def foo(): ...
2906 // async match test:
2907 // case _: ...
2908 self.add_error(
2909 ParseErrorType::UnexpectedTokenAfterAsync(kind),
2910 self.current_token_range(),
2911 );
2912
2913 // Although this statement is not a valid `async` statement,
2914 // we still parse it. Guard the recursive recovery path so
2915 // `async async async ...` cannot overflow the parser stack.
2916 self.with_recursion(Self::parse_statement)
2917 }
2918 }
2919 }
2920
2921 /// Parses a decorator list followed by a class, function or async function definition.
2922 ///

Callers 1

parse_statementMethod · 0.80

Calls 9

node_startMethod · 0.80
current_token_kindMethod · 0.80
parse_with_statementMethod · 0.80
parse_for_statementMethod · 0.80
current_token_rangeMethod · 0.80
with_recursionMethod · 0.80
bumpMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected