Conlang/libconlang/src/token.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

//! # Token
//!
//! Stores a component of a file as a [Type], some [Data], and a line and column number
2023-10-17 18:23:34 +00:00
pub mod token_data;
pub mod token_type;
pub mod preamble {
//! Common imports for working with [tokens](super)
pub use super::{
token_data::Data,
token_type::{Keyword, Type},
Token,
};
2023-10-17 18:23:34 +00:00
}
use token_data::Data;
use token_type::Type;
/// Contains a single unit of lexical information,
/// and an optional bit of [data](TokenData)
#[derive(Clone, Debug, PartialEq)]
2023-10-17 18:23:34 +00:00
pub struct Token {
ty: Type,
data: Data,
line: u32,
col: u32,
2023-10-17 18:23:34 +00:00
}
impl Token {
/// 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 {
Self { ty, data: data.into(), line, col }
2023-10-17 18:23:34 +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 }
}
/// Returns the [Type] of this token
pub fn ty(&self) -> Type {
self.ty
}
/// Returns a reference to this token's [Data]
pub fn data(&self) -> &Data {
&self.data
}
/// Converts this token into its inner [Data]
pub fn into_data(self) -> Data {
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
}
}