#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Span {
pub file: String,
pub start_line: usize,
pub start_col: usize,
pub end_line: usize,
pub end_col: usize,
}
impl std::fmt::Display for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{}:{}-{}:{}",
self.file, self.start_line, self.start_col, self.end_line, self.end_col
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpannedToken {
pub token: Token,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Token {
Identifier(String),
StringLiteral(String),
Keyword(Keyword),
Colon, Semicolon, Comma, LeftBrace, RightBrace, LeftBracket, RightBracket, LeftParen, RightParen, At, Plus, Star, Slash,
Eof,
}
impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Token::Identifier(s) => write!(f, "identifier '{s}'"),
Token::StringLiteral(s) => write!(f, "string \"{s}\""),
Token::Keyword(kw) => write!(f, "keyword '{kw}'"),
Token::Colon => write!(f, "':'"),
Token::Semicolon => write!(f, "';'"),
Token::Comma => write!(f, "','"),
Token::LeftBrace => write!(f, "'{{'"),
Token::RightBrace => write!(f, "'}}'"),
Token::LeftBracket => write!(f, "'['"),
Token::RightBracket => write!(f, "']'"),
Token::LeftParen => write!(f, "'('"),
Token::RightParen => write!(f, "')'"),
Token::At => write!(f, "'@'"),
Token::Plus => write!(f, "'+'"),
Token::Star => write!(f, "'*'"),
Token::Slash => write!(f, "'/'"),
Token::Eof => write!(f, "end of file"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Keyword {
Namespace,
Uses,
Type,
Plural,
Required,
Optional,
String,
Int64,
Real64,
Bool,
Date,
Time,
DateTime,
}
impl std::fmt::Display for Keyword {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Keyword::Namespace => "namespace",
Keyword::Uses => "uses",
Keyword::Type => "type",
Keyword::Plural => "plural",
Keyword::Required => "required",
Keyword::Optional => "optional",
Keyword::String => "string",
Keyword::Int64 => "int64",
Keyword::Real64 => "real64",
Keyword::Bool => "bool",
Keyword::Date => "date",
Keyword::Time => "time",
Keyword::DateTime => "datetime",
};
write!(f, "{s}")
}
}