iowo/crates/lang/src/tokens.rs

74 lines
1.7 KiB
Rust

use logos::Logos;
#[derive(Logos, Debug, PartialEq, Eq, Clone)]
#[logos(skip r"[ \t\n\f]+")]
pub enum Token<'a> {
// hack!
// this isn't actually supposed to be in the language.
// i just can't figure out how to automatically choose between a top level declaration
// or a top level expression
// so a declaration needs the keyword def until i can figure this out
#[token("def")]
Def,
#[token("let")]
Let,
#[token("in")]
In,
#[regex("[\\d]+", |lex| lex.slice())]
Int(&'a str),
#[regex("[+-]?([\\d]+\\.[\\d]*|[\\d]*\\.[\\d]+)", |lex| lex.slice())]
Float(&'a str),
// TODO: more bigger better more complex string lexing
// TODO: templating?
#[regex(r#""([^"\\]|\\["\\bnfrt]|u[a-fA-F0-9]{4})*""#, |lex| lex.slice())]
String(&'a str),
#[token("+")]
Plus,
#[token("-")]
Minus,
#[token("*")]
Mult,
#[token("/")]
Div,
#[regex("[a-zA-Z_]+[a-zA-Z0-9_\\-]*", |lex| lex.slice())]
Word(&'a str),
#[regex("\\$[a-zA-Z0-9_\\-]+", |lex| &lex.slice()[1..])]
VarIdent(&'a str),
#[token("@..")]
InputSpread,
#[regex("\\@[a-zA-Z0-9_\\-]+", |lex| &lex.slice()[1..])]
InputIdent(&'a str),
#[token(",")]
Comma,
#[token("|")]
Pipe,
#[token("@|")]
MappingPipe,
#[token("!|")]
NullPipe,
#[token("@")]
At,
#[token(">")]
GreaterThan,
#[token("=")]
Equals,
#[token(":")]
Colon,
#[token(";")]
SemiColon,
#[token("[")]
BracketOpen,
#[token("]")]
BracketClose,
#[token("(")]
ParenOpen,
#[token(")")]
ParenClose,
#[token("{")]
BraceOpen,
#[token("}")]
BraceClose,
}
#[cfg(test)]
mod tests;