Skip to content

Commit

Permalink
Bump the identifier token
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 17, 2024
1 parent 243937d commit 3ca2ba3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 7 additions & 9 deletions crates/ruff_python_parser/src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,9 @@ impl<'src> Parser<'src> {
}

if self.current_token_kind().is_soft_keyword() {
return ast::Identifier {
id: self.current_token_kind().to_string(),
range: self.current_token_range(),
};
let id = self.src_text(range).to_string();
self.bump_any();
return ast::Identifier { id, range };
}

if self.current_token_kind().is_keyword() {
Expand All @@ -486,10 +485,9 @@ impl<'src> Parser<'src> {
range,
);

ast::Identifier {
id: self.current_token_kind().to_string(),
range: self.current_token_range(),
}
let id = self.src_text(range).to_string();
self.bump_any();
ast::Identifier { id, range }
} else {
self.add_error(
ParseErrorType::OtherError("Expected an identifier".into()),
Expand Down Expand Up @@ -576,7 +574,7 @@ impl<'src> Parser<'src> {
TokenKind::Lbrace => self.parse_set_or_dict_like_expression(),

kind => {
if kind.is_keyword() {
if kind.is_keyword() || kind.is_soft_keyword() {
Expr::Name(self.parse_name())
} else {
self.add_error(
Expand Down
11 changes: 11 additions & 0 deletions crates/ruff_python_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ impl<'src> Parser<'src> {
self.next_token();
}

/// Bumps the current token regardless of its kind and advances to the next token.
///
/// # Panics
///
/// If the parser is at end of file.
fn bump_any(&mut self) {
assert_ne!(self.current_token_kind(), TokenKind::EndOfFile);

self.next_token();
}

fn expect(&mut self, expected: TokenKind) -> bool {
if self.eat(expected) {
return true;
Expand Down

0 comments on commit 3ca2ba3

Please sign in to comment.