Token - Helper methods to better handle identifiers

This commit is contained in:
Emmanuel BENOîT 2023-01-08 11:22:14 +01:00
parent 0cebffbf9b
commit 616565bb0d

View file

@ -69,4 +69,17 @@ impl Token {
| TokenType::Number(_)
)
}
/// Check whether a token is an identifier.
pub fn is_identifier(&self) -> bool {
matches!(self.token_type, TokenType::Identifier(_))
}
/// Get the name from an identifier token, returning an error otherwise.
pub fn as_identifier(&self) -> Result<&str, &'static str> {
match &self.token_type {
TokenType::Identifier(name) => Ok(name),
_ => Err("token is not an identifier"),
}
}
}