Skip to content

Commit

Permalink
Use struct fields in TokenValue enum
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 27, 2024
1 parent 306f702 commit 820d872
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
35 changes: 24 additions & 11 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ impl<'src> Lexer<'src> {
let text = self.token_text();

if !is_ascii {
self.value = TokenValue::Name(text.nfkc().collect::<String>().into_boxed_str());
self.value = TokenValue::Name {
name: text.nfkc().collect::<String>().into_boxed_str(),
};
return TokenKind::Name;
}

Expand Down Expand Up @@ -239,7 +241,9 @@ impl<'src> Lexer<'src> {
"with" => TokenKind::With,
"yield" => TokenKind::Yield,
_ => {
self.value = TokenValue::Name(text.to_string().into_boxed_str());
self.value = TokenValue::Name {
name: text.to_string().into_boxed_str(),
};
TokenKind::Name
}
}
Expand Down Expand Up @@ -286,7 +290,7 @@ impl<'src> Lexer<'src> {
));
}
};
self.value = TokenValue::Int(value);
self.value = TokenValue::Int { value };
TokenKind::Int
}

Expand Down Expand Up @@ -354,7 +358,7 @@ impl<'src> Lexer<'src> {
};
TokenKind::Complex
} else {
self.value = TokenValue::Float(value);
self.value = TokenValue::Float { value };
TokenKind::Float
}
} else {
Expand Down Expand Up @@ -386,7 +390,7 @@ impl<'src> Lexer<'src> {
))
}
};
self.value = TokenValue::Int(value);
self.value = TokenValue::Int { value };
TokenKind::Int
}
}
Expand Down Expand Up @@ -1516,14 +1520,23 @@ pub(crate) enum TokenValue {
#[default]
None,
/// Token value for a name, commonly known as an identifier.
///
/// Unicode names are NFKC-normalized by the lexer, matching
/// [the behaviour of Python's lexer](https://docs.python.org/3/reference/lexical_analysis.html#identifiers)
Name(Box<str>),
Name {
/// The name value.
///
/// Unicode names are NFKC-normalized by the lexer,
/// matching [the behaviour of Python's lexer](https://docs.python.org/3/reference/lexical_analysis.html#identifiers)
name: Box<str>,
},
/// Token value for an integer.
Int(Int),
Int {
/// The integer value.
value: Int,
},
/// Token value for a floating point number.
Float(f64),
Float {
/// The float value.
value: f64,
},
/// Token value for a complex number.
Complex {
/// The real part of the complex number.
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_parser/src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ impl<'src> Parser<'src> {
let range = self.current_token_range();

if self.at(TokenKind::Name) {
let TokenValue::Name(name) = self.bump_value(TokenKind::Name) else {
let TokenValue::Name { name } = self.bump_value(TokenKind::Name) else {
unreachable!();
};
return ast::Identifier {
Expand Down Expand Up @@ -524,7 +524,7 @@ impl<'src> Parser<'src> {

let lhs = match self.current_token_kind() {
TokenKind::Float => {
let TokenValue::Float(value) = self.bump_value(TokenKind::Float) else {
let TokenValue::Float { value } = self.bump_value(TokenKind::Float) else {
unreachable!()
};

Expand All @@ -543,7 +543,7 @@ impl<'src> Parser<'src> {
})
}
TokenKind::Int => {
let TokenValue::Int(value) = self.bump_value(TokenKind::Int) else {
let TokenValue::Int { value } = self.bump_value(TokenKind::Int) else {
unreachable!()
};
Expr::NumberLiteral(ast::ExprNumberLiteral {
Expand Down Expand Up @@ -1423,7 +1423,7 @@ impl<'src> Parser<'src> {
let conversion = if self.eat(TokenKind::Exclamation) {
let conversion_flag_range = self.current_token_range();
if self.at(TokenKind::Name) {
let TokenValue::Name(name) = self.bump_value(TokenKind::Name) else {
let TokenValue::Name { name } = self.bump_value(TokenKind::Name) else {
unreachable!();
};
match &*name {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_parser/src/parser/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl<'src> Parser<'src> {
})
}
TokenKind::Int => {
let TokenValue::Int(value) = self.bump_value(TokenKind::Int) else {
let TokenValue::Int { value } = self.bump_value(TokenKind::Int) else {
unreachable!()
};
let range = self.node_range(start);
Expand All @@ -426,7 +426,7 @@ impl<'src> Parser<'src> {
})
}
TokenKind::Float => {
let TokenValue::Float(value) = self.bump_value(TokenKind::Float) else {
let TokenValue::Float { value } = self.bump_value(TokenKind::Float) else {
unreachable!()
};
let range = self.node_range(start);
Expand Down

0 comments on commit 820d872

Please sign in to comment.