cl_token/
token.rs

1//! A [Token] contains a single unit of lexical information, and an optional bit of [TokenData]
2use super::{TokenData, TokenKind};
3
4/// Contains a single unit of lexical information,
5/// and an optional bit of [TokenData]
6#[derive(Clone, Debug, PartialEq)]
7pub struct Token {
8    pub ty: TokenKind,
9    pub data: TokenData,
10    pub line: u32,
11    pub col: u32,
12}
13impl Token {
14    /// Creates a new [Token] out of a [TokenKind], [TokenData], line, and column.
15    pub fn new(ty: TokenKind, data: impl Into<TokenData>, line: u32, col: u32) -> Self {
16        Self { ty, data: data.into(), line, col }
17    }
18    /// Casts this token to a new [TokenKind]
19    pub fn cast(self, ty: TokenKind) -> Self {
20        Self { ty, ..self }
21    }
22    /// Returns the [TokenKind] of this token
23    pub fn ty(&self) -> TokenKind {
24        self.ty
25    }
26    /// Returns a reference to this token's [TokenData]
27    pub fn data(&self) -> &TokenData {
28        &self.data
29    }
30    /// Converts this token into its inner [TokenData]
31    pub fn into_data(self) -> TokenData {
32        self.data
33    }
34    /// Returns the line where this token originated
35    pub fn line(&self) -> u32 {
36        self.line
37    }
38    /// Returns the column where this token originated
39    pub fn col(&self) -> u32 {
40        self.col
41    }
42}