From aefce10b8f05dc52017beb1d422ce7b2b97bd6a5 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Mon, 19 Dec 2016 00:46:53 -0800 Subject: parser: make strip comments a function --- modelica-parser-lalrpop/examples/parse_file.rs | 9 +-------- modelica-parser-lalrpop/src/lib.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'modelica-parser-lalrpop') diff --git a/modelica-parser-lalrpop/examples/parse_file.rs b/modelica-parser-lalrpop/examples/parse_file.rs index 53da285..fe87ef0 100644 --- a/modelica-parser-lalrpop/examples/parse_file.rs +++ b/modelica-parser-lalrpop/examples/parse_file.rs @@ -1,20 +1,15 @@ 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..."); @@ -29,9 +24,7 @@ fn main() { continue; } - // Strip comments manually - let striped = comment_re.replace_all(&raw, ""); - + let striped = modelica_parser::strip_comments(&raw); let result = modelica_parser::parser::parse_file(&striped); match result { diff --git a/modelica-parser-lalrpop/src/lib.rs b/modelica-parser-lalrpop/src/lib.rs index a109607..0b94c35 100644 --- a/modelica-parser-lalrpop/src/lib.rs +++ b/modelica-parser-lalrpop/src/lib.rs @@ -1,13 +1,23 @@ extern crate lalrpop_util; extern crate colored; +extern crate regex; pub mod parser; pub mod ast; use colored::*; use lalrpop_util::ParseError; +use regex::Regex; +pub fn strip_comments(raw: &str) -> String { + // TODO: shouldn't recompile regex on every function call + + // Match on comments: + // (?m) sets multi-line mode + let comment_re = Regex::new(r"(?m)(//.*)$").unwrap(); + comment_re.replace_all(&raw, "") +} fn pp_segment(raw: &str, start: usize, end: usize) -> String { let mut line_start = 0; -- cgit v1.2.3