#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Syntax {
None,
Indent,
Dedent,
Bool(bool), Number, String(bool), Word, JS, Op, Semi, Colon, Comma, LParen, RParen, LCurly, RCurly, LStaple, RStaple, LCaret, RCaret, Slash, Equal,
Def,
Do,
Return,
If,
Then,
Else,
For,
While,
In,
Fn,
}
impl Syntax {
#[rustfmt::skip]
pub fn starts_expr(&self) -> bool {
matches!(self,
Syntax::String(..) | Syntax::Bool(..) | Syntax::Number | Syntax::Word | Syntax::Fn |
Syntax::Op | Syntax::LCaret | Syntax::LParen | Syntax::LStaple | Syntax::LCurly
)
}
pub fn is_word_or_keyword(&self) -> bool {
use Syntax::*;
match self {
Word | Def | Do | Return | If | Then | Else | For | While | In | Fn => true,
None | Indent | Dedent | Bool(..) | Number | String(..) | JS | Op | Semi | Colon
| Comma | LParen | RParen | LCurly | RCurly | LStaple | RStaple | LCaret | RCaret
| Slash | Equal => false,
}
}
}
pub trait SyntaxTrait {
fn is_word_char(&self) -> bool;
fn is_tag_opener(&self) -> bool;
fn is_op(&self) -> bool;
fn is_bracket(&self) -> bool;
}
impl SyntaxTrait for char {
fn is_word_char(&self) -> bool {
self.is_alphanumeric() || matches!(*self, '-' | '_' | '\'')
}
fn is_tag_opener(&self) -> bool {
self.is_alphabetic() || matches!(self, '#' | '.' | ':' | '@' | '/')
}
fn is_op(&self) -> bool {
!self.is_whitespace()
&& !self.is_alphanumeric()
&& !self.is_bracket()
&& !matches!(*self, '#' | '"' | '`' | '\'')
}
fn is_bracket(&self) -> bool {
matches!(self, '(' | ')' | '[' | ']' | '{' | '}')
}
}