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

Method parse_nonlocal_statement

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

Parses a nonlocal statement. # Panics If the parser isn't positioned at a `nonlocal` token. See:

(&mut self)

Source from the content-addressed store, hash-verified

1011 ///
1012 /// See: <https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-nonlocal_stmt>
1013 fn parse_nonlocal_statement(&mut self) -> ast::StmtNonlocal {
1014 let start = self.node_start();
1015 self.bump(TokenKind::Nonlocal);
1016
1017 // test_err nonlocal_stmt_trailing_comma
1018 // def _():
1019 // nonlocal ,
1020 // nonlocal x,
1021 // nonlocal x, y,
1022
1023 // test_err nonlocal_stmt_expression
1024 // def _():
1025 // nonlocal x + 1
1026 let names = self.parse_comma_separated_list_into_vec(
1027 RecoveryContextKind::Identifiers,
1028 Parser::parse_identifier,
1029 );
1030
1031 if names.is_empty() {
1032 // test_err nonlocal_stmt_empty
1033 // def _():
1034 // nonlocal
1035 self.add_error(
1036 ParseErrorType::EmptyNonlocalNames,
1037 self.current_token_range(),
1038 );
1039 }
1040
1041 // test_ok nonlocal_stmt
1042 // def _():
1043 // nonlocal x
1044 // nonlocal x, y, z
1045 ast::StmtNonlocal {
1046 range: self.node_range(start),
1047 names,
1048 node_index: AtomicNodeIndex::NONE,
1049 }
1050 }
1051
1052 /// Parses a type alias statement.
1053 ///

Callers 1

Calls 7

node_startMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
is_emptyMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected