mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
parser: map some operators to new precedence
This commit is contained in:
parent
860e16368b
commit
9b3fbd12a4
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user