use crate::{token_name, Loc};
#[derive(Debug, Clone)]
pub enum TokenValue {
String(String),
InvalidString(Vec<u8>),
}
impl TokenValue {
pub fn to_string_lossy(&self) -> String {
match &self {
Self::String(s) => s.clone(),
Self::InvalidString(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
match &self {
Self::String(s) => s.as_bytes().to_vec(),
Self::InvalidString(bytes) => bytes.clone(),
}
}
pub fn into_string_lossy(self) -> String {
match self {
Self::String(s) => s,
Self::InvalidString(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
}
}
pub fn into_bytes(self) -> Vec<u8> {
match self {
Self::String(s) => s.into_bytes(),
Self::InvalidString(bytes) => bytes,
}
}
}
#[derive(Clone)]
pub struct Token {
pub token_type: i32,
pub token_value: TokenValue,
pub loc: Loc,
}
use std::fmt;
impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format!(
"[{}, {:?}, {}...{}]",
token_name(self.token_type),
self.token_value,
self.loc.begin,
self.loc.end
))
}
}
impl Token {
pub fn to_string_lossy(&self) -> String {
self.token_value.to_string_lossy()
}
pub fn to_bytes(&self) -> Vec<u8> {
self.token_value.to_bytes()
}
pub fn into_string_lossy(self) -> String {
self.token_value.into_string_lossy()
}
pub fn into_bytes(self) -> Vec<u8> {
self.token_value.into_bytes()
}
}