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 20, 2024
1 parent 0fbe59f commit c939908
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 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 @@ -470,7 +470,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 @@ -519,7 +519,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 @@ -538,7 +538,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 @@ -1418,7 +1418,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
8 changes: 4 additions & 4 deletions crates/ruff_python_parser/src/parser/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,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 @@ -429,7 +429,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 All @@ -443,7 +443,7 @@ impl<'src> Parser<'src> {
})
}
TokenKind::Name if self.peek() == TokenKind::Dot => {
let TokenValue::Name(name) = self.bump_value(TokenKind::Name) else {
let TokenValue::Name { name } = self.bump_value(TokenKind::Name) else {
unreachable!()
};
let id = Expr::Name(ast::ExprName {
Expand All @@ -460,7 +460,7 @@ impl<'src> Parser<'src> {
})
}
TokenKind::Name => {
let TokenValue::Name(name) = self.bump_value(TokenKind::Name) else {
let TokenValue::Name { name } = self.bump_value(TokenKind::Name) else {
unreachable!()
};
let range = self.node_range(start);
Expand Down

0 comments on commit c939908

Please sign in to comment.