diff options
Diffstat (limited to 'modelica-parser-lalrpop/examples')
-rw-r--r-- | modelica-parser-lalrpop/examples/parse_file.rs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/modelica-parser-lalrpop/examples/parse_file.rs b/modelica-parser-lalrpop/examples/parse_file.rs index b3d6913..53da285 100644 --- a/modelica-parser-lalrpop/examples/parse_file.rs +++ b/modelica-parser-lalrpop/examples/parse_file.rs @@ -1,15 +1,20 @@ 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..."); @@ -17,18 +22,21 @@ fn main() { } for input in &args[1..] { - let mut s = String::new(); - if let Err(err) = File::open(input).and_then(|mut f| f.read_to_string(&mut s)) { + 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; } - let result = modelica_parser::parser::parse_file(&s); + // 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(&s, err)), + Err(err) => println!("=== {}: ERROR\n{}", input, modelica_parser::pp_parseerror(&striped, err)), } } } |