From 469629360dd8aad2f7ca6937cd266b1ab6901bab Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 18 Dec 2016 16:06:10 -0800 Subject: manually/naively strip comments from raw .mo files --- modelica-parser-lalrpop/examples/parse_file.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'modelica-parser-lalrpop/examples') 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 = 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)), } } } -- cgit v1.2.3