diff --git a/parser/src/json/compiler.rs b/parser/src/json/compiler.rs index 17b230c..e03e4e6 100644 --- a/parser/src/json/compiler.rs +++ b/parser/src/json/compiler.rs @@ -81,7 +81,7 @@ struct Compiler { pending_definitions: Vec<(String, NodeRef)>, any_cache: Option, - lexeme_cache: HashMap, + lexeme_cache: HashMap<(String, bool), NodeRef>, } macro_rules! cache { @@ -173,7 +173,7 @@ impl Compiler { message: reason.to_string(), })), Schema::Null => Ok(self.builder.string("null")), - Schema::Boolean => Ok(self.lexeme(r"true|false")), + Schema::Boolean => Ok(self.lexeme(r"true|false", false)), Schema::Number { minimum, maximum, @@ -285,13 +285,14 @@ impl Compiler { } } - fn lexeme(&mut self, rx: &str) -> NodeRef { - if self.lexeme_cache.contains_key(rx) { - return self.lexeme_cache[rx]; - } - let r = self.builder.lexeme(mk_regex(rx), false); - self.lexeme_cache.insert(rx.to_string(), r); - r + fn lexeme(&mut self, rx: &str, json_quoted: bool) -> NodeRef { + let key = (rx.to_string(), json_quoted); + self.lexeme_cache + .entry(key) + .or_insert_with(|| + self.builder.lexeme(mk_regex(rx), json_quoted) + ) + .clone() } fn json_int( @@ -333,7 +334,7 @@ impl Compiler { minimum, maximum ) })?; - Ok(self.lexeme(&rx)) + Ok(self.lexeme(&rx, false)) } fn json_number( @@ -352,11 +353,11 @@ impl Compiler { minimum, maximum ) })?; - Ok(self.lexeme(&rx)) + Ok(self.lexeme(&rx, false)) } fn json_simple_string(&mut self) -> NodeRef { - self.lexeme(&format!("\"{}*\"", CHAR_REGEX)) + self.lexeme("(?s:.*)", true) } fn get_definition(&mut self, reference: &str) -> Result { @@ -568,12 +569,14 @@ impl Compiler { let node = self.builder.lexeme(mk_regex(regex), true); Ok(node) } else { - Ok(self.lexeme(&format!( - "\"{}{{{},{}}}\"", - CHAR_REGEX, - min_length, - max_length.map_or("".to_string(), |v| v.to_string()) - ))) + Ok(self.lexeme( + &format!( + "(?s:.{{{},{}}})", + min_length, + max_length.map_or("".to_string(), |v| v.to_string()) + ), + true, + )) } }