Move integer and char parsing out of the parser and back into the lexer
This commit is contained in:
47
src/token.rs
47
src/token.rs
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user