use std::fmt;
#[derive(Debug)]
#[cfg_attr(
feature = "unstable_machinery_serde",
derive(serde::Serialize),
serde(tag = "name", content = "payload")
)]
pub enum Token<'a> {
TemplateData(&'a str),
VariableStart,
VariableEnd,
BlockStart,
BlockEnd,
Ident(&'a str),
Str(&'a str),
String(String),
Int(u64),
Int128(u128),
Float(f64),
Plus,
Minus,
Mul,
Div,
FloorDiv,
Pow,
Mod,
Dot,
Comma,
Colon,
Tilde,
Assign,
Pipe,
Eq,
Ne,
Gt,
Gte,
Lt,
Lte,
BracketOpen,
BracketClose,
ParenOpen,
ParenClose,
BraceOpen,
BraceClose,
}
impl<'a> fmt::Display for Token<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Token::TemplateData(_) => f.write_str("template-data"),
Token::VariableStart => f.write_str("start of variable block"),
Token::VariableEnd => f.write_str("end of variable block"),
Token::BlockStart => f.write_str("start of block"),
Token::BlockEnd => f.write_str("end of block"),
Token::Ident(_) => f.write_str("identifier"),
Token::Str(_) | Token::String(_) => f.write_str("string"),
Token::Int(_) | Token::Int128(_) => f.write_str("integer"),
Token::Float(_) => f.write_str("float"),
Token::Plus => f.write_str("`+`"),
Token::Minus => f.write_str("`-`"),
Token::Mul => f.write_str("`*`"),
Token::Div => f.write_str("`/`"),
Token::FloorDiv => f.write_str("`//`"),
Token::Pow => f.write_str("`**`"),
Token::Mod => f.write_str("`%`"),
Token::Dot => f.write_str("`.`"),
Token::Comma => f.write_str("`,`"),
Token::Colon => f.write_str("`:`"),
Token::Tilde => f.write_str("`~`"),
Token::Assign => f.write_str("`=`"),
Token::Pipe => f.write_str("`|`"),
Token::Eq => f.write_str("`==`"),
Token::Ne => f.write_str("`!=`"),
Token::Gt => f.write_str("`>`"),
Token::Gte => f.write_str("`>=`"),
Token::Lt => f.write_str("`<`"),
Token::Lte => f.write_str("`<=`"),
Token::BracketOpen => f.write_str("`[`"),
Token::BracketClose => f.write_str("`]`"),
Token::ParenOpen => f.write_str("`(`"),
Token::ParenClose => f.write_str("`)`"),
Token::BraceOpen => f.write_str("`{{`"),
Token::BraceClose => f.write_str("`}}`"),
}
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "unstable_machinery_serde", derive(serde::Serialize))]
pub struct Span {
pub start_line: u32,
pub start_col: u32,
pub start_offset: u32,
pub end_line: u32,
pub end_col: u32,
pub end_offset: u32,
}
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
" @ {}:{}-{}:{}",
self.start_line, self.start_col, self.end_line, self.end_col
)
}
}