MCPcopy Create free account
hub / github.com/daisy/MathCAT / find_match

Method find_match

src/speech.rs:2201–2258  ·  view source on GitHub ↗
(&'r mut self, rule_vector: &[Box<SpeechPattern>], mathml: Element<'c>)

Source from the content-addressed store, hash-verified

2199 }
2200
2201 fn find_match<T:TreeOrString<'c, 'm, T>>(&'r mut self, rule_vector: &[Box<SpeechPattern>], mathml: Element<'c>) -> Result<Option<T>> {
2202 for pattern in rule_vector {
2203 // debug!("Pattern: {}", pattern);
2204 // pushing and popping around the is_match would be a little cleaner, but push/pop is relatively expensive, so we optimize
2205 if pattern.match_uses_var_defs {
2206 self.context_stack.push(pattern.var_defs.clone(), mathml)?;
2207 }
2208 if pattern.is_match(&self.context_stack.base, mathml)
2209 .chain_err(|| error_string(pattern, mathml) )? {
2210 if !pattern.match_uses_var_defs && pattern.var_defs.len() > 0 { // don't push them on twice
2211 self.context_stack.push(pattern.var_defs.clone(), mathml)?;
2212 }
2213 let result: Result<T> = pattern.replacements.replace(self, mathml);
2214 if pattern.var_defs.len() > 0 {
2215 self.context_stack.pop();
2216 }
2217 return match result {
2218 Ok(s) => {
2219 // for all except braille and navigation, nav_node_id will be an empty string and will not match
2220 if self.nav_node_id.is_empty() {
2221 Ok( Some(s) )
2222 } else {
2223 Ok ( Some(self.nav_node_adjust(s, mathml)) )
2224 }
2225 },
2226 Err(e) => Err( e.chain_err(||
2227 format!(
2228 "attempting replacement pattern: \"{}\" for \"{}\".\n\
2229 Replacement\n{}\n...due to matching the following MathML with the pattern\n{}\n\
2230 {}\
2231 The patterns are in {}.\n",
2232 pattern.pattern_name, pattern.tag_name,
2233 pattern.replacements.pretty_print_replacements(),pattern.pattern,
2234 mml_to_string(&mathml),
2235 pattern.file_name
2236 )
2237 ))
2238 }
2239 } else if pattern.match_uses_var_defs {
2240 self.context_stack.pop();
2241 }
2242 };
2243 return Ok(None); // no matches
2244
2245 fn error_string(pattern: &SpeechPattern, mathml: Element) -> String {
2246 return format!(
2247 "error during pattern match using: \"{}\" for \"{}\".\n\
2248 Pattern is \n{}\nMathML for the match:\n\
2249 {}\
2250 The patterns are in {}.\n",
2251 pattern.pattern_name, pattern.tag_name,
2252 pattern.pattern,
2253 mml_to_string(&mathml),
2254 pattern.file_name
2255 );
2256 }
2257
2258 }

Callers 1

match_patternMethod · 0.80

Calls 7

is_matchMethod · 0.80
lenMethod · 0.80
is_emptyMethod · 0.80
nav_node_adjustMethod · 0.80
pushMethod · 0.45
replaceMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected