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:2844–2904  ·  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

2842 /// - <https://docs.python.org/3/reference/compound_stmts.html#the-async-for-statement>
2843 /// - <https://docs.python.org/3/reference/compound_stmts.html#coroutine-function-definition>
2844 fn parse_async_statement(&mut self) -> Stmt {
2845 let async_start = self.node_start();
2846 self.bump(TokenKind::Async);
2847
2848 match self.current_token_kind() {
2849 // test_ok async_function_definition
2850 // async def foo(): ...
2851 TokenKind::Def => Stmt::FunctionDef(ast::StmtFunctionDef {
2852 is_async: true,
2853 ..self.parse_function_definition(DecoratorList::new(), async_start)
2854 }),
2855
2856 // test_ok async_with_statement
2857 // async with item: ...
2858 TokenKind::With => Stmt::With(ast::StmtWith {
2859 is_async: true,
2860 ..self.parse_with_statement(async_start)
2861 }),
2862
2863 // test_ok async_for_statement
2864 // async for target in iter: ...
2865 TokenKind::For => Stmt::For(ast::StmtFor {
2866 is_async: true,
2867 ..self.parse_for_statement(async_start)
2868 }),
2869
2870 kind => {
2871 // test_err async_unexpected_token
2872 // async class Foo: ...
2873 // async while test: ...
2874 // async x = 1
2875 // async async def foo(): ...
2876 // async match test:
2877 // case _: ...
2878 self.add_error(
2879 ParseErrorType::UnexpectedTokenAfterAsync(kind),
2880 self.current_token_range(),
2881 );
2882
2883 // Although this statement is not a valid `async` statement,
2884 // we still parse it. Guard the recursive recovery path so
2885 // `async async async ...` cannot overflow the parser stack.
2886 if let Some(stmt) = self.with_recursion(Self::parse_statement) {
2887 stmt
2888 } else {
2889 let range = self.node_range(async_start);
2890 self.add_error(ParseErrorType::RecursionLimitExceeded, range);
2891 Stmt::Expr(ast::StmtExpr {
2892 range,
2893 value: Box::new(Expr::Name(ast::ExprName {
2894 range,
2895 id: Name::new_static("async"),
2896 ctx: ExprContext::Invalid,
2897 node_index: AtomicNodeIndex::NONE,
2898 })),
2899 node_index: AtomicNodeIndex::NONE,
2900 })
2901 }

Callers 1

parse_statementMethod · 0.80

Calls 12

ExprEnum · 0.85
NameClass · 0.85
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
node_rangeMethod · 0.80
bumpMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected