complexpr/complexpr/src/token.rs

110 lines
3.1 KiB
Rust
Raw Normal View History

2022-09-06 13:52:29 +00:00
use std::{fmt, rc::Rc};
use crate::Position;
#[derive(Clone)]
pub struct Token {
pub ty: TokenType,
pub text: String,
pub pos: Position
}
impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} @ {},{}", self.ty, self.pos.line, self.pos.col)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum TokenType {
2022-09-07 22:10:43 +00:00
Int(i64), Float(f64), ImFloat(f64), String(Rc<str>), Char(char),
2022-09-06 13:52:29 +00:00
Ident(Rc<str>),
Plus, Minus, Star, Slash, Percent, DoubleSlash, Caret,
Bang, DoubleAmper, DoublePipe,
Tilde, Amper, Pipe,
2022-09-06 13:52:29 +00:00
2022-09-22 12:58:59 +00:00
Dot, DoubleDot,
2022-09-21 03:38:58 +00:00
2022-09-06 13:52:29 +00:00
Equal, PlusEqual, MinusEqual, StarEqual, SlashEqual, PercentEqual, DoubleSlashEqual, CaretEqual,
DoubleEqual, BangEqual, Greater, GreaterEqual, Less, LessEqual, Spaceship,
2022-09-06 13:52:29 +00:00
PipeColon, PipePoint, PipeQuestion, PipeAmper,
PipeSlash, PipeBackslash, PipeDoubleSlash, PipeDoubleBackslash,
Backslash,
2022-09-06 13:52:29 +00:00
2022-09-07 22:10:43 +00:00
Comma, Semicolon, Colon,
2022-09-06 13:52:29 +00:00
LParen, RParen, LBrack, RBrack, LBrace, RBrace,
2022-09-11 05:01:53 +00:00
True, False, Nil,
If, Elif, Else, For, While,
Fn, Let, Struct,
Break, Continue, Return
2022-09-06 13:52:29 +00:00
}
impl TokenType {
pub fn get_op_type(&self) -> Option<OpType> {
match self {
Self::Plus | Self::Minus => Some(OpType::Additive),
Self::Star | Self::Slash | Self::DoubleSlash
| Self::Percent => Some(OpType::Multiplicative),
Self::Caret => Some(OpType::Exponential),
Self::PipeColon | Self::PipeAmper | Self::PipePoint | Self::PipeQuestion
| Self::PipeSlash | Self::PipeDoubleSlash | Self::PipeBackslash | Self::PipeDoubleBackslash => Some(OpType::Pipeline),
2022-09-06 13:52:29 +00:00
Self::Greater | Self::GreaterEqual | Self::Less | Self::LessEqual
| Self::DoubleEqual | Self::BangEqual | Self::Spaceship => Some(OpType::Comparison),
2022-09-06 13:52:29 +00:00
Self::Equal | Self::PlusEqual | Self::MinusEqual
| Self::StarEqual | Self::SlashEqual | Self::DoubleSlashEqual
| Self::CaretEqual | Self::PercentEqual => Some(OpType::Assignment),
Self::Amper => Some(OpType::BitwiseAnd),
Self::Pipe => Some(OpType::BitwiseOr),
Self::DoubleAmper => Some(OpType::LogicalAnd),
Self::DoublePipe => Some(OpType::LogicalOr),
2022-09-11 05:14:54 +00:00
2022-09-06 13:52:29 +00:00
_ => None
}
}
pub fn as_ident(self) -> Option<Rc<str>> {
match self {
Self::Ident(s) => Some(s),
_ => None
}
}
pub fn is_infix_op(&self) -> bool {
matches!(self.get_op_type(), Some(
OpType::Additive
| OpType::Multiplicative
| OpType::Exponential
| OpType::Comparison
| OpType::BitwiseAnd
| OpType::BitwiseOr
// TODO | OpType::Pipeline
))
}
2022-09-06 13:52:29 +00:00
}
2022-09-11 17:02:18 +00:00
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
2022-09-06 13:52:29 +00:00
pub enum OpType {
Assignment, Comparison, Pipeline,
Additive, Multiplicative, Exponential,
LogicalAnd, LogicalOr,
BitwiseAnd, BitwiseOr,
2022-09-06 13:52:29 +00:00
}
impl OpType {
pub fn is_right_associative(&self) -> bool {
matches!(self, OpType::Exponential | OpType::Assignment)
}
2022-09-11 17:02:18 +00:00
}