parser: map some operators to new precedence

This commit is contained in:
Ryan Chandler 2022-09-12 17:22:06 +01:00
parent 860e16368b
commit 9b3fbd12a4
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 54 additions and 1 deletions

View File

@ -617,7 +617,14 @@ impl Lexer {
if let Some(b'*') = self.peek {
self.col += 1;
self.next();
TokenKind::Pow
if let Some(b'=') = self.peek {
self.col += 1;
self.next();
TokenKind::PowEquals
} else {
TokenKind::Pow
}
} else if let Some(b'=') = self.peek {
self.col += 1;
self.next();
@ -753,6 +760,12 @@ impl Lexer {
self.col += 1;
TokenKind::GreaterThanEquals
} else if let Some(b'>') = self.peek {
self.next();
self.col += 1;
TokenKind::RightShift
} else {
TokenKind::GreaterThan
}

View File

@ -107,6 +107,7 @@ pub enum TokenKind {
LeftBracket,
LeftParen,
LeftShift,
RightShift,
LessThan,
LessThanEquals,
Match,
@ -122,6 +123,7 @@ pub enum TokenKind {
Plus,
PlusEquals,
Pow,
PowEquals,
Private,
Protected,
Public,

View File

@ -1,3 +1,5 @@
use trunk_lexer::TokenKind;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
CloneNew,
@ -27,6 +29,42 @@ pub enum Precedence {
KeyOr,
}
impl Precedence {
pub fn prefix(kind: &TokenKind) -> Self {
use TokenKind::*;
match kind {
Bang => Self::Bang,
Clone | New => Self::CloneNew,
_ => Self::Prefix,
}
}
pub fn infix(kind: &TokenKind) -> Self {
use TokenKind::*;
match kind {
Instanceof => Self::Instanceof,
Asterisk | Slash | Percent => Self::MulDivMod,
Plus | Minus => Self::AddSub,
LeftShift | RightShift => Self::BitShift,
Dot => Self::Concat,
LessThan | LessThanEquals | GreaterThan | GreaterThanEquals => Self::LtGt,
DoubleEquals | BangEquals | TripleEquals | BangDoubleEquals => Self::Equality,
Ampersand => Self::BitwiseAnd,
Caret => Self::BitwiseXor,
Pipe => Self::BitwiseOr,
BooleanAnd => Self::And,
BooleanOr => Self::Or,
Coalesce => Self::NullCoalesce,
Question => Self::Ternary,
Equals | PlusEquals | MinusEquals | AsteriskEqual | PowEquals | SlashEquals | DotEquals | AndEqual | CoalesceEqual => Self::Assignment,
Yield => Self::Yield,
_ => unimplemented!("precedence for op {:?}", kind)
}
}
}
pub enum Associativity {
None,
Left,