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

Method parse_statement

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

Parses a compound or a single simple statement. See: - -

(&mut self)

Source from the content-addressed store, hash-verified

110 /// - <https://docs.python.org/3/reference/compound_stmts.html>
111 /// - <https://docs.python.org/3/reference/simple_stmts.html>
112 pub(super) fn parse_statement(&mut self) -> Stmt {
113 let start = self.node_start();
114
115 match self.current_token_kind() {
116 TokenKind::If => Stmt::If(self.parse_if_statement()),
117 TokenKind::For => Stmt::For(self.parse_for_statement(start)),
118 TokenKind::While => Stmt::While(self.parse_while_statement()),
119 TokenKind::Def => {
120 Stmt::FunctionDef(self.parse_function_definition(DecoratorList::new(), start))
121 }
122 TokenKind::Class => {
123 Stmt::ClassDef(self.parse_class_definition(DecoratorList::new(), start))
124 }
125 TokenKind::Try => Stmt::Try(self.parse_try_statement()),
126 TokenKind::With => Stmt::With(self.parse_with_statement(start)),
127 TokenKind::At => self.parse_decorators(),
128 TokenKind::Async => self.parse_async_statement(),
129 token => {
130 if token == TokenKind::Match {
131 // Match is considered a soft keyword, so we will treat it as an identifier if
132 // it's followed by an unexpected token.
133
134 match self.classify_match_token() {
135 MatchTokenKind::Keyword => {
136 return Stmt::Match(self.parse_match_statement());
137 }
138 MatchTokenKind::KeywordOrIdentifier => {
139 if let Some(match_stmt) = self.try_parse_match_statement() {
140 return Stmt::Match(match_stmt);
141 }
142 }
143 MatchTokenKind::Identifier => {}
144 }
145 }
146
147 self.parse_single_simple_statement()
148 }
149 }
150 }
151
152 /// Parses a single simple statement.
153 ///

Callers 2

parse_moduleMethod · 0.80
parse_blockMethod · 0.80

Calls 15

MatchEnum · 0.85
node_startMethod · 0.80
current_token_kindMethod · 0.80
parse_if_statementMethod · 0.80
parse_for_statementMethod · 0.80
parse_while_statementMethod · 0.80
parse_try_statementMethod · 0.80
parse_with_statementMethod · 0.80
parse_decoratorsMethod · 0.80
parse_async_statementMethod · 0.80

Tested by

no test coverage detected