able-script/src/lexer.rs

210 lines
3.6 KiB
Rust
Raw Normal View History

use logos::{Lexer, Logos};
use crate::variables::Abool;
#[derive(Logos, Debug, PartialEq, Clone)]
pub enum Token {
// Symbols
#[token("(")]
LeftParenthesis,
#[token(")")]
RightParenthesis,
#[token("[")]
LeftBracket,
#[token("]")]
RightBracket,
#[token("{")]
LeftCurly,
#[token("}")]
RightCurly,
#[token(";")]
Semicolon,
#[token(".")]
Dot,
#[token(",")]
Comma,
// Operators
#[token("+")]
Plus,
#[token("-")]
Minus,
#[token("*")]
Star,
#[token("/")]
FwdSlash,
#[token("=")]
Equal,
// Logical operators
#[token("<")]
LessThan,
#[token(">")]
GreaterThan,
#[token("==")]
EqualEqual,
#[token("!=")]
NotEqual,
#[token("&")]
And,
#[token("|")]
Or,
#[token("!|aint")] // also add aint as a not keyword
Not,
// Keywords
2021-04-27 06:48:56 -05:00
#[token("functio")]
Functio,
2021-04-27 06:48:56 -05:00
/// Brain fuck FFI
#[token("bff")]
BfFunction,
/// Variable bro
#[token("var")]
Variable,
/// Prints the preceding things
#[token("print")]
Print,
/// Ban the following variable from ever being used again
#[token("melo")]
Melo,
#[token("T-Dark")]
TDark,
// Control flow keywords
2021-04-27 06:48:56 -05:00
#[token("if")]
If,
#[token("loop")]
Loop,
2021-05-02 11:12:51 -05:00
#[token("break")]
Break,
/// HopBack hops on the back of loop - like `continue`
2021-05-02 11:12:51 -05:00
#[token("hopback")]
HopBack,
/// Crash with random error (see discussion #17)
#[token("rlyeh")]
Rlyeh,
// Literals
/// True, False
#[regex("true|false", get_bool)]
Bool(bool),
/// Always, Sometimes, Never
#[regex("always|sometimes|never", get_abool)]
Abool(Abool),
/// Base52 based character ('a')
#[token("'.*'")]
Char,
/// String
#[regex("\"(\\.|[^\"])*\"", get_string)]
String(String),
/// Integer
#[regex(r"[0-9]+", get_int)]
Integer(i32),
/// A C-complaint identifier
#[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_iden)]
Identifier(String),
2021-05-24 01:18:36 -05:00
#[regex(r"owo.*")]
Comment,
#[regex("nul")]
Nul,
#[regex(r"[ \t\n\f]+", logos::skip)]
#[error]
Error,
2021-04-11 17:22:06 -05:00
}
fn get_bool(lexer: &mut Lexer<Token>) -> Option<bool> {
lexer.slice().parse().ok()
}
fn get_int(lexer: &mut Lexer<Token>) -> Option<i32> {
lexer.slice().parse().ok()
}
fn get_string(lexer: &mut Lexer<Token>) -> String {
lexer.slice().trim_matches('"').to_owned()
}
fn get_abool(lexer: &mut Lexer<Token>) -> Option<Abool> {
match lexer.slice() {
"always" => Some(Abool::Always),
"sometimes" => Some(Abool::Sometimes),
"never" => Some(Abool::Never),
_ => None,
}
}
fn get_iden(lexer: &mut Lexer<Token>) -> String {
lexer.slice().to_owned()
}
#[cfg(test)]
mod tests {
use super::Token;
use super::Token::*;
use logos::Logos;
#[test]
fn simple_fn() {
let code = "functio test() { var a = 3; if a == 3 { a print } }";
let expected = &[
Functio,
Identifier("test".to_owned()),
LeftParenthesis,
RightParenthesis,
LeftCurly,
Variable,
Identifier("a".to_owned()),
Equal,
Integer(3),
Semicolon,
If,
Identifier("a".to_owned()),
EqualEqual,
Integer(3),
LeftCurly,
Identifier("a".to_owned()),
Print,
RightCurly,
RightCurly,
];
let lexer = Token::lexer(code);
let result: Vec<Token> = lexer.collect();
assert_eq!(result, expected);
}
2021-04-27 06:48:56 -05:00
}