use crate::slice_file::Location;
use std::fmt;
pub type Token<'a> = (Location, TokenKind<'a>, Location);
pub type Error = (Location, ErrorKind, Location);
#[derive(Clone, Debug)]
pub enum TokenKind<'input> {
Identifier(&'input str),
StringLiteral(&'input str),
IntegerLiteral(&'input str),
DocComment(&'input str),
ModuleKeyword, StructKeyword, InterfaceKeyword, EnumKeyword, CustomKeyword, TypeAliasKeyword, ResultKeyword,
SequenceKeyword, DictionaryKeyword,
BoolKeyword, Int8Keyword, UInt8Keyword, Int16Keyword, UInt16Keyword, Int32Keyword, UInt32Keyword, VarInt32Keyword, VarUInt32Keyword, Int64Keyword, UInt64Keyword, VarInt62Keyword, VarUInt62Keyword, Float32Keyword, Float64Keyword, StringKeyword,
CompactKeyword, IdempotentKeyword, StreamKeyword, TagKeyword, UncheckedKeyword,
LeftParenthesis, RightParenthesis, LeftBracket, RightBracket, DoubleLeftBracket, DoubleRightBracket, LeftBrace, RightBrace, LeftChevron, RightChevron,
Comma, Colon, DoubleColon, Equals, QuestionMark, Arrow, Minus, }
impl fmt::Display for TokenKind<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Identifier(input) => input,
Self::IntegerLiteral(input) => input,
Self::StringLiteral(input) => input,
Self::DocComment(input) => input,
Self::ModuleKeyword => "module",
Self::StructKeyword => "struct",
Self::InterfaceKeyword => "interface",
Self::EnumKeyword => "enum",
Self::CustomKeyword => "custom",
Self::TypeAliasKeyword => "typealias",
Self::ResultKeyword => "Result",
Self::SequenceKeyword => "Sequence",
Self::DictionaryKeyword => "Dictionary",
Self::BoolKeyword => "bool",
Self::Int8Keyword => "int8",
Self::UInt8Keyword => "uint8",
Self::Int16Keyword => "int16",
Self::UInt16Keyword => "uint16",
Self::Int32Keyword => "int32",
Self::UInt32Keyword => "uint32",
Self::VarInt32Keyword => "varint32",
Self::VarUInt32Keyword => "varuint32",
Self::Int64Keyword => "int64",
Self::UInt64Keyword => "uint64",
Self::VarInt62Keyword => "varint62",
Self::VarUInt62Keyword => "varuint62",
Self::Float32Keyword => "float32",
Self::Float64Keyword => "float64",
Self::StringKeyword => "string",
Self::CompactKeyword => "compact",
Self::IdempotentKeyword => "idempotent",
Self::StreamKeyword => "stream",
Self::TagKeyword => "tag",
Self::UncheckedKeyword => "unchecked",
Self::LeftParenthesis => "(",
Self::RightParenthesis => ")",
Self::LeftBracket => "[",
Self::RightBracket => "]",
Self::DoubleLeftBracket => "[[",
Self::DoubleRightBracket => "]]",
Self::LeftBrace => "{",
Self::RightBrace => "}",
Self::LeftChevron => "<",
Self::RightChevron => ">",
Self::Comma => ",",
Self::Colon => ":",
Self::DoubleColon => "::",
Self::Equals => "=",
Self::QuestionMark => "?",
Self::Arrow => "->",
Self::Minus => "-",
})
}
}
#[derive(Clone, Debug)]
pub enum ErrorKind {
UnknownSymbol { symbol: String, suggestion: Option<String> },
UnterminatedStringLiteral,
UnterminatedBlockComment,
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownSymbol { symbol, suggestion } => match suggestion {
Some(s) => write!(f, "unknown symbol '{symbol}', try using '{s}' instead"),
None => write!(f, "unknown symbol '{symbol}'"),
},
Self::UnterminatedStringLiteral => f.write_str("unterminated string literal"),
Self::UnterminatedBlockComment => f.write_str("unterminated block comment"),
}
}
}