use std::fmt::Display;
use std::fmt::Formatter;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TokenKind {
Eof,
Error,
CodeComplete,
BareIdentifier,
AtIdentifier,
PercentIdentifier,
CaretIdentifier,
FloatLiteral,
Integer,
String,
IntType,
Arrow,
Colon,
SingleQuote,
Comma,
Dot,
Equal,
LParen,
RParen,
LBrace,
RBrace,
Minus,
Exclamation,
Greater,
Less,
Plus,
KwF16,
KwF32,
KwF64,
KwF80,
KwTrue,
KwType,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Location {
line: usize,
column: usize,
start: usize,
}
impl Display for Location {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "loc(:{}:{})", self.line, self.column)
}
}
impl Location {
pub fn new(line: usize, column: usize, start: usize) -> Self {
Self {
line,
column,
start,
}
}
pub fn line(&self) -> usize {
self.line
}
pub fn column(&self) -> usize {
self.column
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Token {
pub kind: TokenKind,
pub lexeme: String,
pub location: Location,
}
impl Token {
pub fn new(kind: TokenKind, lexeme: String, location: Location) -> Self {
Self {
kind,
lexeme,
location,
}
}
pub fn line(&self) -> usize {
self.location.line()
}
pub fn column(&self) -> usize {
self.location.column()
}
pub fn lexeme(&self) -> &str {
&self.lexeme
}
}
impl Display for Token {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?} \"{}\" {}", self.kind, self.lexeme, self.location)
}
}