use super::position::Position;
use std::fmt::Display;
#[derive(Clone, Debug)]
pub struct Token {
pub token_type: usize,
pub token_name: String,
pub start: Position,
pub stop: Position,
pub channel: usize,
pub text: String,
pub token_index: usize,
pub char_start_index: usize,
pub char_stop_index: usize,
}
impl Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[@{}, {}:{}='{}', <{}>, <{}>, start: <{}>, stop: <{}>]",
self.token_index,
self.char_start_index,
self.char_stop_index,
self.text,
self.token_name,
self.token_type,
self.start,
self.stop
)
}
}
impl Token {
pub fn start(channel: usize) -> Self {
Token {
token_type: 0,
token_name: "_START".to_owned(),
start: Position { line: 0, char_position: 0 },
stop: Position { line: 0, char_position: 0 },
channel,
text: "_START".to_owned(),
token_index: 0,
char_start_index: 0,
char_stop_index: 0,
}
}
pub fn new(token_type: usize, token_name: &str, text: &str,
start: Position, stop: Position, token_index: usize, channel:
usize, char_start_index: usize, char_stop_index: usize) -> Self {
Token {
token_type,
token_name: token_name.to_owned(),
start,
stop,
token_index, channel, char_start_index, char_stop_index,
text: text.to_owned(),
}
}
pub fn to_string(&self) -> String {
format!("[@{}, {}:{}='{}', <{}>, <{}>, start: <{}>, stop: <{}>]",
self.token_index,
self.char_start_index,
self.char_stop_index,
self.text,
self.token_name,
self.token_type,
self.start,
self.stop
)
}
}