2024-03-01 01:36:06 +00:00
|
|
|
//! A [Token] contains a single unit of lexical information, and an optional bit of [Data]
|
|
|
|
use super::{Data, Type};
|
2023-10-22 23:28:20 +00:00
|
|
|
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Contains a single unit of lexical information,
|
2023-10-24 02:01:32 +00:00
|
|
|
/// and an optional bit of [Data]
|
2023-10-22 23:28:20 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2023-10-17 18:23:34 +00:00
|
|
|
pub struct Token {
|
|
|
|
ty: Type,
|
2023-10-24 00:43:16 +00:00
|
|
|
data: Data,
|
2023-10-20 20:34:54 +00:00
|
|
|
line: u32,
|
|
|
|
col: u32,
|
2023-10-17 18:23:34 +00:00
|
|
|
}
|
|
|
|
impl Token {
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Creates a new [Token] out of a [Type], [Data], line, and column.
|
|
|
|
pub fn new(ty: Type, data: impl Into<Data>, line: u32, col: u32) -> Self {
|
2023-10-22 23:28:20 +00:00
|
|
|
Self { ty, data: data.into(), line, col }
|
2023-10-17 18:23:34 +00:00
|
|
|
}
|
2023-10-22 23:28:20 +00:00
|
|
|
/// Casts this token to a new [Type]
|
2023-10-17 18:23:34 +00:00
|
|
|
pub fn cast(self, ty: Type) -> Self {
|
|
|
|
Self { ty, ..self }
|
|
|
|
}
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Returns the [Type] of this token
|
2023-10-22 23:28:20 +00:00
|
|
|
pub fn ty(&self) -> Type {
|
|
|
|
self.ty
|
|
|
|
}
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Returns a reference to this token's [Data]
|
|
|
|
pub fn data(&self) -> &Data {
|
2023-10-22 23:28:20 +00:00
|
|
|
&self.data
|
|
|
|
}
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Converts this token into its inner [Data]
|
|
|
|
pub fn into_data(self) -> Data {
|
2023-10-22 23:28:20 +00:00
|
|
|
self.data
|
2023-10-17 18:23:34 +00:00
|
|
|
}
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Returns the line where this token originated
|
2023-10-20 20:34:54 +00:00
|
|
|
pub fn line(&self) -> u32 {
|
2023-10-17 18:23:34 +00:00
|
|
|
self.line
|
|
|
|
}
|
2023-10-24 00:43:16 +00:00
|
|
|
/// Returns the column where this token originated
|
2023-10-20 20:34:54 +00:00
|
|
|
pub fn col(&self) -> u32 {
|
2023-10-17 18:23:34 +00:00
|
|
|
self.col
|
|
|
|
}
|
2023-10-19 19:40:03 +00:00
|
|
|
}
|