-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
39 lines (34 loc) · 1.44 KB
/
mod.rs
File metadata and controls
39 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::lexical_analysis::model::token::Token;
use crate::syntax_analysis::model::expression_precedence::get_infix_operator_precedence;
use crate::syntax_analysis::model::syntax_tree_node::{Expression, InfixOperator};
use crate::syntax_analysis::SyntaxAnalysis;
impl SyntaxAnalysis<'_> {
pub(crate) fn parse_infix_expression(
&mut self,
left_hand: Expression,
) -> anyhow::Result<Expression> {
debug!("Parsing a infix expression.");
let token = self
.tokens
.next()
.ok_or_else(|| anyhow::anyhow!("No token to parse."))?;
let operator = match token {
Token::Plus => InfixOperator::Plus,
Token::Minus => InfixOperator::Minus,
Token::Multiply => InfixOperator::Multiply,
Token::Divide => InfixOperator::Divide,
Token::Equals => InfixOperator::Equals,
Token::NotEquals => InfixOperator::NotEquals,
Token::LesserThan => InfixOperator::LesserThan,
Token::GreaterThan => InfixOperator::GreaterThan,
_ => anyhow::bail!("Unknown infix operator token {:?}.", token),
};
let precedence = get_infix_operator_precedence(&operator);
self.get_expression(precedence)
.map(|right_hand| Expression::Infix {
left_hand: Box::new(left_hand),
operator,
right_hand: Box::new(right_hand),
})
}
}