smt_lang/parser/
mod.rs

1pub mod parser;
2pub use parser::*;
3
4pub mod position;
5pub use position::*;
6
7use crate::error::*;
8use crate::problem::*;
9
10use std::fs;
11
12lalrpop_mod!(grammar, "/parser/grammar.rs");
13
14use line_col::LineColLookup;
15
16#[derive(Debug)]
17pub struct Identifier {
18    pub name: String,
19    pub position: Position,
20}
21
22impl Identifier {
23    pub fn new(file: &str, lookup: &LineColLookup, name: &str, offset: usize) -> Self {
24        let name = name.into();
25        let position = Position::new(file, lookup, offset);
26        Self { name, position }
27    }
28}
29
30pub fn parse_file(problem: &mut Problem, file: &str) -> Result<(), Error> {
31    let mut parser = Parser::new(problem);
32    parser.add(file);
33
34    loop {
35        match parser.next() {
36            None => return Ok(()),
37            Some(file) => match fs::read_to_string(&file) {
38                Ok(input) => {
39                    let lookup = LineColLookup::new(&input);
40                    match grammar::ProblemParser::new().parse(&lookup, &mut parser, &input) {
41                        Ok(_) => {}
42                        Err(e) => return Err(Error::new_parse(&file, &lookup, e)),
43                    }
44                }
45                Err(e) => {
46                    let e = Error::File {
47                        filename: file,
48                        message: format!("{:?}", e),
49                    };
50                    return Err(e);
51                }
52            },
53        }
54    }
55}