From f2c1f1c13772145acaecb1ebf9b4cafcacb98131 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 7 Jan 2017 21:46:22 -0800 Subject: parser: statically compile regex w/ lazy_static As per regex library's documentation --- modelica-parser-lalrpop/Cargo.toml | 1 + modelica-parser-lalrpop/src/lib.rs | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'modelica-parser-lalrpop') diff --git a/modelica-parser-lalrpop/Cargo.toml b/modelica-parser-lalrpop/Cargo.toml index 42c6357..6e4b196 100644 --- a/modelica-parser-lalrpop/Cargo.toml +++ b/modelica-parser-lalrpop/Cargo.toml @@ -18,6 +18,7 @@ name = "modelica_parser" lalrpop-util = "^0.12.4" colored = "1.3" regex = "^0.1" +lazy_static = "^0.2" [build-dependencies] lalrpop = "0.12" diff --git a/modelica-parser-lalrpop/src/lib.rs b/modelica-parser-lalrpop/src/lib.rs index b515d73..ed07ca2 100644 --- a/modelica-parser-lalrpop/src/lib.rs +++ b/modelica-parser-lalrpop/src/lib.rs @@ -2,6 +2,7 @@ extern crate lalrpop_util; extern crate colored; extern crate regex; +#[macro_use] extern crate lazy_static; mod parser; mod ast; @@ -20,12 +21,12 @@ pub use parser::{ }; 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, "") + lazy_static! { + static ref RE: Regex = Regex::new(r"(?m)(//.*)$").unwrap(); + } + RE.replace_all(&raw, "") } fn pp_segment(raw: &str, start: usize, end: usize) -> String { -- cgit v1.2.3