Conlang/compiler/cl-token/src/token.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

//! A [Token] contains a single unit of lexical information, and an optional bit of [TokenData]
use super::{TokenData, TokenKind};
/// Contains a single unit of lexical information,
/// and an optional bit of [TokenData]
#[derive(Clone, Debug, PartialEq)]
2023-10-17 18:23:34 +00:00
pub struct Token {
pub ty: TokenKind,
pub data: TokenData,
pub line: u32,
pub col: u32,
2023-10-17 18:23:34 +00:00
}
impl Token {
/// Creates a new [Token] out of a [TokenKind], [TokenData], line, and column.
pub fn new(ty: TokenKind, data: impl Into<TokenData>, line: u32, col: u32) -> Self {
Self { ty, data: data.into(), line, col }
2023-10-17 18:23:34 +00:00
}
/// Casts this token to a new [TokenKind]
pub fn cast(self, ty: TokenKind) -> Self {
2023-10-17 18:23:34 +00:00
Self { ty, ..self }
}
/// Returns the [TokenKind] of this token
pub fn ty(&self) -> TokenKind {
self.ty
}
/// Returns a reference to this token's [TokenData]
pub fn data(&self) -> &TokenData {
&self.data
}
/// Converts this token into its inner [TokenData]
pub fn into_data(self) -> TokenData {
self.data
2023-10-17 18:23:34 +00:00
}
/// Returns the line where this token originated
pub fn line(&self) -> u32 {
2023-10-17 18:23:34 +00:00
self.line
}
/// Returns the column where this token originated
pub fn col(&self) -> u32 {
2023-10-17 18:23:34 +00:00
self.col
}
}