-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rs
More file actions
37 lines (26 loc) · 859 Bytes
/
parser.rs
File metadata and controls
37 lines (26 loc) · 859 Bytes
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
//! Parsing source code in to expressions
use lalrpop_util::lalrpop_mod;
use crate::{
ast::{self, add_type_to_expr_parse},
errors::{ExprResult, SyntaxError},
lexer::lex,
parser::grammar::ExprParser,
};
lalrpop_mod!(grammar);
/// Parse source code in to an [`ast::Expr`].
pub fn parse(source: &str) -> ExprResult<ast::Expr> {
let tokens = lex(source);
let mut errs = vec![];
let expr_parser = ExprParser::new();
let mut parser_errors = Vec::new();
let mut expr = match expr_parser.parse(source, &mut parser_errors, tokens) {
Ok(ast) => ast,
Err(err) => {
errs.push(SyntaxError::from_parser_error(err, source));
ast::Expr::Error
}
};
add_type_to_expr_parse(&mut expr);
errs.extend(parser_errors);
if errs.is_empty() { Ok(expr) } else { Err(errs) }
}