logos calculator example

This commit is contained in:
LunarAkai 2025-08-05 17:17:11 +02:00
commit 0c30f0022d
9 changed files with 414 additions and 22 deletions

57
src/tokens.rs Normal file
View file

@ -0,0 +1,57 @@
use logos::{Lexer, Logos};
#[derive(Logos, Debug, Clone, PartialEq)]
#[logos(skip r"[ \t\r\n\f]+")] // Skips whitespace
pub enum Token<'source> {
#[token("false", |_| false)]
#[token("true", |_| true)]
Bool(bool),
#[token("+")]
Add,
#[token("-")]
Substract,
#[token("*")]
Multiply,
#[token("/")]
Divide,
#[token("=")]
Equals,
#[token(":")]
Colon,
#[token("(")]
ParenBegin,
#[token(")")]
ParenEnd,
#[token("{")]
BraceBegin,
#[token("}")]
BraceEnd,
#[regex("[0-9]+", |lex| lex.slice().parse::<isize>().unwrap())]
Integer(isize),
#[regex(r"[_a-zA-Z][_0-9a-zA-Z]*")]
Ident(&'source str),
#[regex(r#""([^"\\\x00-\x1F]|\\(["\\bnfrt/]|u[a-fA-F0-9]{4}))*""#, |lex| lex.slice().to_owned())]
String(String),
#[token("class")]
#[token("fun")]
#[token("var")]
#[token("if")]
#[token("else")]
Keyword(&'source str),
}