mirror of
https://github.com/danog/parser.git
synced 2024-11-27 04:14:55 +01:00
parser: support basic math ops
This commit is contained in:
parent
53c7970f83
commit
be40cd61c4
@ -496,6 +496,17 @@ impl Lexer {
|
||||
TokenKind::Comment(buffer)
|
||||
}
|
||||
},
|
||||
'*' => {
|
||||
self.col += 1;
|
||||
|
||||
if let Some('*') = it.peek() {
|
||||
self.col += 1;
|
||||
it.next();
|
||||
TokenKind::Pow
|
||||
} else {
|
||||
TokenKind::Asterisk
|
||||
}
|
||||
},
|
||||
'|' => {
|
||||
self.col += 1;
|
||||
// TODO: Handle boolean or || tokens.
|
||||
|
@ -243,6 +243,8 @@ pub struct ArrayItem {
|
||||
pub enum InfixOp {
|
||||
Add,
|
||||
Sub,
|
||||
Div,
|
||||
Mul,
|
||||
LessThan,
|
||||
}
|
||||
|
||||
@ -251,6 +253,8 @@ impl From<TokenKind> for InfixOp {
|
||||
match k {
|
||||
TokenKind::Plus => Self::Add,
|
||||
TokenKind::Minus => Self::Sub,
|
||||
TokenKind::Asterisk => Self::Mul,
|
||||
TokenKind::Slash => Self::Div,
|
||||
TokenKind::LessThan => Self::LessThan,
|
||||
_ => unreachable!()
|
||||
}
|
||||
|
@ -911,6 +911,7 @@ fn infix(lhs: Expression, op: TokenKind, rhs: Expression) -> Expression {
|
||||
|
||||
fn infix_binding_power(t: &TokenKind) -> Option<(u8, u8)> {
|
||||
Some(match t {
|
||||
TokenKind::Asterisk | TokenKind::Slash => (13, 14),
|
||||
TokenKind::Plus | TokenKind::Minus => (11, 12),
|
||||
TokenKind::LessThan => (9, 10),
|
||||
TokenKind::Equals => (2, 1),
|
||||
@ -1011,6 +1012,14 @@ mod tests {
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! expr {
|
||||
($expr:expr) => {
|
||||
Statement::Expression {
|
||||
expr: $expr,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paren_expression() {
|
||||
assert_ast("<?php (1 + 2);", &[
|
||||
@ -1022,6 +1031,29 @@ mod tests {
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_precedence() {
|
||||
assert_ast("<?php 1 + 2 * 3 / 4 - 5;", &[
|
||||
expr!(Expression::Infix(
|
||||
Box::new(Expression::Infix(
|
||||
Box::new(Expression::Int(1)),
|
||||
InfixOp::Add,
|
||||
Box::new(Expression::Infix(
|
||||
Box::new(Expression::Infix(
|
||||
Box::new(Expression::Int(2)),
|
||||
InfixOp::Mul,
|
||||
Box::new(Expression::Int(3))
|
||||
)),
|
||||
InfixOp::Div,
|
||||
Box::new(Expression::Int(4))
|
||||
))
|
||||
)),
|
||||
InfixOp::Sub,
|
||||
Box::new(Expression::Int(5))
|
||||
))
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_fn() {
|
||||
assert_ast("<?php function foo() {}", &[
|
||||
|
Loading…
Reference in New Issue
Block a user