-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.rs
119 lines (98 loc) · 3.1 KB
/
parser.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::ast::{BinOp, Expr, ExprKind};
use crate::error::Error;
use crate::lex::{Token, TokenKind};
pub fn parse(tokens: &[Token]) -> Result<Expr, Error> {
let mut parser = Parser {
tokens,
idx: 0,
last_position: 1,
};
let expr = parser.parse(1)?;
if let Some(token) = parser.next() {
return Err(Error {
message: "Extra token",
position: token.position,
});
}
Ok(expr)
}
struct Parser<'a> {
tokens: &'a [Token],
idx: usize,
last_position: usize,
}
impl<'a> Parser<'a> {
fn parse_primary(&mut self, token: &Token) -> Result<Expr, Error> {
let kind = match token.kind {
TokenKind::LParen => {
let expr = self.parse(1)?;
match self.next().map(|token| &token.kind) {
Some(TokenKind::RParen) => return Ok(expr),
_ => return Err(self.error("Unclosed (")),
}
}
TokenKind::Sub => {
let left = Expr {
kind: ExprKind::Real(0.0),
position: token.position,
};
let right = self
.next()
.ok_or_else(|| self.error("Expected a token"))
.and_then(|token| self.parse_primary(token))?;
ExprKind::Bin(BinOp::Sub, left.into(), right.into())
}
TokenKind::Real(num) => ExprKind::Real(num),
TokenKind::Imag(num) => ExprKind::Imag(num),
TokenKind::Ident(ref ident) => ExprKind::Var(ident.clone()),
_ => return Err(self.error("Unexpected token")),
};
Ok(Expr {
kind,
position: token.position,
})
}
fn parse(&mut self, min_precedence: u32) -> Result<Expr, Error> {
let mut expr = self
.next()
.ok_or_else(|| self.error("Expected a token"))
.and_then(|token| self.parse_primary(token))?;
loop {
let op = match self.peek().and_then(|token| BinOp::from_token(&token.kind)) {
Some(op) => op,
None => break,
};
if op.precedence() < min_precedence {
return Ok(expr);
}
self.next();
let right = self.parse(op.precedence() + 1)?;
expr = Expr {
position: self.last_position,
kind: ExprKind::Bin(op, expr.into(), right.into()),
};
}
Ok(expr)
}
fn error(&self, message: &'static str) -> Error {
Error {
message,
position: self.last_position,
}
}
fn peek(&self) -> Option<&'a Token> {
if self.idx >= self.tokens.len() {
return None;
}
Some(&self.tokens[self.idx])
}
fn next(&mut self) -> Option<&'a Token> {
if self.idx >= self.tokens.len() {
return None;
}
let token = &self.tokens[self.idx];
self.last_position = token.position;
self.idx += 1;
Some(token)
}
}