wip
This commit is contained in:
71
src/token.rs
Normal file
71
src/token.rs
Normal file
@ -0,0 +1,71 @@
|
||||
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,
|
||||
}
|
Reference in New Issue
Block a user