Move integer and char parsing out of the parser and back into the lexer

This commit is contained in:
2025-10-10 14:45:08 -04:00
parent 0cbb800c19
commit b5d552376e
4 changed files with 114 additions and 37 deletions

View File

@@ -4,11 +4,55 @@ use crate::span::Span;
#[derive(Clone, Debug)]
pub struct Token {
pub lexeme: String,
pub lexeme: Lexeme,
pub kind: TKind,
pub span: Span,
}
#[derive(Clone, Debug)]
pub enum Lexeme {
String(String),
Integer(u128, u32),
Char(char),
}
impl Lexeme {
pub fn string(self) -> Option<String> {
match self {
Self::String(s) => Some(s),
_ => None,
}
}
pub fn str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
}
pub fn int(&self) -> Option<u128> {
match self {
Self::Integer(i, _) => Some(*i),
_ => None,
}
}
pub fn char(&self) -> Option<char> {
match self {
Self::Char(c) => Some(*c),
_ => None,
}
}
}
impl std::fmt::Display for Lexeme {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(v) => v.fmt(f),
Self::Integer(v, _) => v.fmt(f),
Self::Char(v) => v.fmt(f),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TKind {
Comment,
@@ -19,6 +63,7 @@ pub enum TKind {
Else,
False,
Fn,
For,
If,
Let,
Loop,