Token - Helper methods to better handle identifiers
This commit is contained in:
parent
0cebffbf9b
commit
616565bb0d
1 changed files with 13 additions and 0 deletions
|
@ -69,4 +69,17 @@ impl Token {
|
||||||
| TokenType::Number(_)
|
| 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"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue