Files
ts-parser/src/token.rs
2025-05-27 23:46:52 +02:00

72 lines
1.2 KiB
Rust

use anyhow::anyhow;
#[derive(Debug)]
pub struct Token {
pub kind: TokenKind,
pub location: TokenLocation,
}
#[derive(Debug)]
pub enum TokenKind {
Identifier(String),
Literal(LiteralKind),
Keyword(KeywordKind),
Comment(CommentKind, String),
LeftParen,
RightParen,
LeftCurly,
RightCurly,
Comma,
Colon,
Semicolon,
Period,
NewLine,
EndOfFile,
}
#[derive(Debug)]
pub enum CommentKind {
Line,
Block,
}
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub enum KeywordKind {
function,
string,
number,
}
impl TryFrom<&str> for KeywordKind {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"function" => Ok(Self::function),
"string" => Ok(Self::string),
"number" => Ok(Self::number),
_ => Err(anyhow!("unknown keyword")),
}
}
}
#[derive(Debug)]
pub enum LiteralKind {
String(String),
Number(Number),
}
#[derive(Debug)]
pub enum Number {
Integer(usize),
Float(f64),
}
#[derive(Debug)]
pub struct TokenLocation {
pub file: Option<String>,
pub line: usize,
pub column: usize,
}