#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Keyword {
Let,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Punct {
Add,
Sub,
Mul,
Div,
Pow,
Colon,
SemiColon,
Assign,
Comma,
OpenParen,
CloseParen,
OpenBracket,
CloseBracket,
OpenCurly,
CloseCurly,
Spread,
Range,
Dot,
Pipe,
Arrow,
Lambda,
}
impl Punct {
pub fn precedence(self) -> u8 {
use self::Punct::*;
match self {
Pow => 1,
Mul | Div => 2,
Add | Sub => 3,
_ => 9,
}
}
pub fn left_associative(self) -> bool {
use self::Punct::*;
match self {
Add | Sub | Mul | Div => true,
_ => false,
}
}
pub fn is_arithmetic(self) -> bool {
use self::Punct::*;
match self {
Add | Sub | Mul | Div | Pow => true,
_ => false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum TokenType {
Keyword(Keyword),
Ident { start: usize, end: usize },
Num(i32),
Str { start: usize, end: usize },
Bool(bool),
Comment { start: usize, end: usize },
Discard,
Punct(Punct),
Term,
}
impl TokenType {
pub fn type_eq(self, other: TokenType) -> bool {
use self::TokenType::*;
match (self, other) {
(Keyword(_), Keyword(_))
| (Ident { .. }, Ident { .. })
| (Num(_), Num(_))
| (Str { .. }, Str { .. })
| (Bool(_), Bool(_))
| (Comment { .. }, Comment { .. })
| (Punct(_), Punct(_))
| (Term, Term) => true,
_ => false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Token {
pub col: usize,
pub ln: usize,
pub r#type: TokenType,
}
impl Token {
pub fn new(ln: usize, col: usize, t: TokenType) -> Self {
Token { col, ln, r#type: t }
}
}