From 616565bb0d28a6198c9ba3229880e11bfbf3f672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 8 Jan 2023 11:22:14 +0100 Subject: [PATCH] Token - Helper methods to better handle identifiers --- src/tokens.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tokens.rs b/src/tokens.rs index 98a413d..0bd7c0b 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -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"), + } + } }