aboutsummaryrefslogtreecommitdiffstats
path: root/modelica-parser-lalrpop/examples/parse_file.rs
blob: 53da2855c3b03e146f54cd4d116b2e5d15eb2c57 (plain)
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

extern crate modelica_parser;
extern crate regex;

use std::env;
use std::io::Read;
use std::fs::File;
use std::process::exit;
use regex::Regex;


fn main() {

    let args: Vec<String> = env::args().collect();
    // Match on comments:
    //      (?m) sets multi-line mode
    let comment_re = Regex::new(r"(?m)(//.*)$").unwrap();

    if args.len() <= 1 {
        println!("I'm a modelica parser! Pass me one or more files to parse...");
        exit(-1);
    }

    for input in &args[1..] {
        let mut raw = String::new();
        if let Err(err) = File::open(input).and_then(|mut f| f.read_to_string(&mut raw)) {
            println!("=== {}: I/O Error {}",
                     input, err);
            continue;
        }

        // Strip comments manually
        let striped = comment_re.replace_all(&raw, "");

        let result = modelica_parser::parser::parse_file(&striped);

        match result {
            Ok(_) => println!("=== {}: OK", input),
            Err(err) => println!("=== {}: ERROR\n{}", input, modelica_parser::pp_parseerror(&striped, err)),
        }
    }
}