Skip to content

Instantly share code, notes, and snippets.

@lylythechosenone
Created January 16, 2023 14:08
Show Gist options
  • Save lylythechosenone/390db31734ec147a57a1ded5c7fbbb93 to your computer and use it in GitHub Desktop.
Save lylythechosenone/390db31734ec147a57a1ded5c7fbbb93 to your computer and use it in GitHub Desktop.
My lexer in logos#277
use logos::Logos;
#[derive(Logos, PartialEq, Eq, Debug)]
pub enum Token<'a> {
#[regex(r"-?[0-9][0-9_]*(\.[0-9_]+)?([eE][\+-][0-9_]+)?([fui][0-9]+)?")]
Number(&'a str),
#[regex(r#""(\\.|[^"\\])*""#, |lex| &lex.slice()[1..lex.slice().len() - 1])]
#[regex(r#"#(\\.|[^#\\])*#"#, |lex| &lex.slice()[1..lex.slice().len() - 1])]
String(&'a str),
#[regex(r"'(\\.|[^'\\])'", |lex| lex.slice().chars().nth(1).unwrap())]
Char(char),
#[regex(r"///[^\n]*", |lex| lex.slice()[3..].trim())]
#[regex(r"/\*\*([^\*]|\*[^\*/]|\*\*[^/])*\*?\*/", |lex| lex.slice()[3..lex.slice().len() - 3].trim())]
Doc(&'a str),
#[regex(r"//[^\n]*", logos::skip)]
#[regex(r"/\*([^\*]|\*[^/])*\*/", logos::skip)]
Comment,
#[regex(r"#\[.+\]", |lex| &lex.slice()[2..lex.slice().len() - 1])]
Attrib(&'a str),
#[regex(r"[a-zA-Z_][a-zA-Z0-9_]*", |lex| lex.slice())]
Ident(&'a str),
#[token("(")]
LeftParen,
#[token(")")]
RightParen,
#[token("[")]
LeftBracket,
#[token("]")]
RightBracket,
#[token("{")]
LeftBrace,
#[token("}")]
RightBrace,
#[token(",")]
Comma,
#[token(".")]
Dot,
#[token(":")]
Colon,
#[token(";")]
Semicolon,
#[token("+")]
Plus,
#[token("-")]
Minus,
#[token("*")]
Star,
#[token("/")]
Slash,
#[token("%")]
Percent,
#[token("&")]
And,
#[token("|")]
Or,
#[token("^")]
Xor,
#[token("==")]
EqEq,
#[token("!=")]
BangEq,
#[token("<")]
Lt,
#[token("<=")]
LtEq,
#[token(">")]
Gt,
#[token(">=")]
GtEq,
#[token("&&")]
AndAnd,
#[token("||")]
OrOr,
#[token("!")]
Bang,
#[token(":=")]
ColonEq,
#[token("=>")]
FatArrow,
#[token("->")]
ThinArrow,
#[token("..")]
Ellipsis,
#[token("<<")]
LtLt,
#[token(">>")]
GtGt,
#[token("<<=")]
LtLtEq,
#[token(">>=")]
GtGtEq,
#[token("+=")]
PlusEq,
#[token("-=")]
MinusEq,
#[token("*=")]
StarEq,
#[token("/=")]
SlashEq,
#[token("%=")]
PercentEq,
#[token("&=")]
AndEq,
#[token("|=")]
OrEq,
#[token("^=")]
XorEq,
#[regex("(true|false)", |lex| lex.slice().parse())]
Bool(bool),
#[token("if")]
If,
#[token("else")]
Else,
#[token("while")]
While,
#[token("for")]
For,
#[token("in")]
In,
#[token("break")]
Break,
#[token("continue")]
Continue,
#[token("return")]
Return,
#[token("let")]
Let,
#[token("mut")]
Mut,
#[token("own")]
Own,
#[token("const")]
Const,
#[token("fn")]
Fn,
#[token("struct")]
Struct,
#[token("enum")]
Enum,
#[token("impl")]
Impl,
#[token("trait")]
Trait,
#[token("type")]
Type,
#[token("use")]
Use,
#[token("mod")]
Mod,
#[token("pub")]
Pub,
#[token("extern")]
Extern,
#[token("crate")]
Crate,
#[token("self")]
Self_,
#[token("super")]
Super,
#[token("as")]
As,
#[token("where")]
Where,
#[token("match")]
Match,
#[token("async")]
Async,
#[token("await")]
Await,
#[token("box")]
Box,
#[token("dyn")]
Dyn,
#[token("static")]
Static,
#[token("union")]
Union,
#[token("unsafe")]
Unsafe,
#[token("abstract")]
Macro,
#[token("override")]
#[error]
#[regex(r"[ \t\n\f]+", logos::skip)]
Error,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment