use std::fmt::Debug;
use super::{Keyword, Reserved, Text};
#[derive(Debug, PartialEq, Eq)]
pub struct Token<'s> {
pub ttype: TokenType<'s>,
pub loc: Location,
}
impl<'s> AsRef<Token<'s>> for Token<'s> {
fn as_ref(&self) -> &Token<'s> {
self
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum TokenType<'s> {
LeftParen,
RightParen,
Dot,
Comma,
Semicolon,
Plus,
Minus,
Star,
Slash,
At,
PipePipe,
Equal,
EqualGreater,
CaretEqual,
Less,
LessEqual,
LessGreater,
Greater,
GreaterEqual,
BangEqual,
QuestionMark,
Comment(Comment<'s>),
Integer(&'s str),
Float(&'s str),
Text(Text<'s>, NationalStyle),
Placeholder(Ident<'s>),
Identifier(Ident<'s>, Option<Reserved>),
Keyword(Keyword),
}
#[derive(Debug, PartialEq, Eq)]
pub struct Ident<'s>(pub &'s str, pub QuoteStyle);
impl<'s> std::fmt::Display for Ident<'s> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
crate::fmt::Display::fmt(self, f)
}
}
impl<'s> crate::fmt::Display for Ident<'s> {
fn fmt(&self, f: &mut impl crate::fmt::Formatter) -> std::fmt::Result {
match self.1 {
QuoteStyle::None => f.write_str(self.0),
QuoteStyle::Quoted => {
f.write_char('"')?;
f.write_str(self.0)?;
f.write_char('"')
}
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum QuoteStyle {
None,
Quoted,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum NationalStyle {
None,
National,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Comment<'s>(pub &'s str, pub CommentStyle);
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CommentStyle {
Line,
Block,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Location {
pub line: u32,
pub col: u32,
}
impl Ord for Location {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.line
.cmp(&other.line)
.then_with(|| self.col.cmp(&other.col))
}
}
impl PartialOrd for Location {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<(u32, u32)> for Location {
fn from((line, col): (u32, u32)) -> Self {
Self { line, col }
}
}
impl Location {
#[must_use]
pub(crate) fn with_cols_removed(&self, n: u32) -> Self {
Self {
line: self.line,
col: self.col.saturating_sub(n),
}
}
}
impl std::fmt::Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[line: {}, column: {}]", self.line, self.col)
}
}