From 399bd4f6a1b9e8220d04cb1f7364f43981f244ba Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 3 Oct 2023 01:21:47 +0200 Subject: [PATCH] Added lexer --- src/main.rs | 1 + src/syntax/mod.rs | 1 + src/syntax/token.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/syntax/mod.rs create mode 100644 src/syntax/token.rs diff --git a/src/main.rs b/src/main.rs index 113ec36..e25511a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // Rhea +mod syntax; mod utils; use std::io::{stdin, Read}; diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs new file mode 100644 index 0000000..79c66ba --- /dev/null +++ b/src/syntax/mod.rs @@ -0,0 +1 @@ +pub mod token; diff --git a/src/syntax/token.rs b/src/syntax/token.rs new file mode 100644 index 0000000..bbd8ad3 --- /dev/null +++ b/src/syntax/token.rs @@ -0,0 +1,68 @@ +use lasso::{Rodeo, Spur}; +use logos::Logos; + +#[derive(Default)] +pub struct Extras { + pub interner: Rodeo, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Logos)] +#[logos(extras = Extras)] +#[logos(skip r"[ \t\n\f]+")] +#[logos(skip r"\\.*")] +#[rustfmt::skip] +pub enum Token { + #[token("(")] LeftParen, + #[token(")")] RightParen, + #[token("{")] LeftCurly, + #[token("}")] RightCurly, + #[token(".")] Dot, + #[token(",")] Comma, + #[token(":")] Colon, + #[token(";")] Semicolon, + + #[token("←")] //______ + #[token("<-")] LArrow, + #[token("→")] //______ + #[token("->")] RArrow, + + #[token("+")] Plus, + #[token("-")] Minus, + #[token("*")] Star, + #[token("/")] Slash, + + #[token("=")] Equ, + #[token("≠")] //___ + #[token("/=")] Neq, + #[token("<")] Lt, + #[token(">")] Gt, + #[token("≤")] //____ + #[token("<=")] LtEq, + #[token("≥")] //____ + #[token(">=")] GtEq, + + #[token("func")] Func, + #[token("var")] Var, + #[token("const")] Const, + #[token("include")] Include, + #[token("switch")] Switch, + #[token("loop")] Loop, + + #[regex( + r"\p{XID_Start}\p{XID_Continue}*", + |l| l.extras.interner.get_or_intern(l.slice()), + )] Ident(Spur), + + #[regex( + "\"[^\"]*\"", + |l| { + let s = l.slice(); + l.extras.interner.get_or_intern(&s[1..s.len() - 1]) + }, + )] String(Spur), + + #[regex( + "[0-9]+", + |l| l.slice().parse::().ok() + )] Int(u64), +}