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

View file

@ -1,33 +0,0 @@
use logos::{Lexer, Logos};
#[derive(Logos, Debug, Clone, PartialEq)]
#[logos(skip r"[ \t\r\n\f]+")] // Skips whitespace
enum Token<'source> {
#[token("false", |_| false)]
#[token("true", |_| true)]
Bool(bool),
#[token("+")]
Add,
#[token("-")]
Substract,
#[token("*")]
Multiply,
#[token("/")]
Divide,
#[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),
}
fn float<'a>(lex: &mut Lexer<'a, Token<'a>>) -> Result<f64, ()> {
lex.slice().parse().map_err(|_| ())
}