//! End-pattern resolution for begin/end rules. //! //! TextMate end patterns can be static (a regex known at compile time) or //! dynamic (numeric backrefs into the begin match resolved per occurrence). //! Resolved dynamic end regexes are cached so that a multi-line construct does //! not recompile its end regex on every line. use std::borrow::Cow; use std::collections::BTreeMap; use std::sync::Arc; use onig::Regex; use super::pattern::RegexMatch; use super::regex; /// Cache of regexes for resolved dynamic end patterns. pub(super) type EndRegexCache = BTreeMap>; /// End-pattern definition stored on a compiled begin/end rule. #[derive(Debug)] pub(super) enum EndPattern { /// End regex compiled at grammar-compile time. Static { /// Original regex source. source: String, /// Compiled regex. regex: Arc, }, /// End regex deferred until begin-match captures are known. Dynamic { /// Regex source containing numeric backrefs. source: String, }, } /// End regex resolved against a specific begin match. /// /// Static end patterns borrow their source from the compiled grammar, while /// dynamic patterns own the source resolved from the begin captures; the /// compiled regex is shared with the grammar or the [`EndRegexCache`]. pub(super) struct EndRegex<'pat> { /// Source string, owned when resolved from a dynamic end pattern. source: Cow<'pat, str>, /// Compiled regex used for searching. regex: Arc, } impl EndPattern { /// Compiles an end pattern, deferring numeric-backref patterns until match time. pub(super) fn compile(source: &str) -> Option { if has_numeric_backrefs(source) { return Some(Self::Dynamic { source: source.to_owned(), }); } let regex = Arc::new(regex(source).ok()?); Some(Self::Static { source: source.to_owned(), regex, }) } /// Resolves this end pattern when its begin/end rule first opens. pub(super) fn resolve_for_begin<'pat>( &'pat self, begin: &RegexMatch, line: &str, cache: &mut EndRegexCache, ) -> Option> { let resolved = match self { Self::Static { .. } => None, Self::Dynamic { source } => Some(begin.expand_backrefs(source, line)), }; self.resolve(resolved, cache) } /// Resumes this end pattern using a dynamic source previously stored in line state. pub(super) fn resume<'pat>( &'pat self, dynamic_source: Option<&str>, cache: &mut EndRegexCache, ) -> Option> { self.resolve(dynamic_source.map(str::to_owned), cache) } /// Builds the resolved end regex, compiling and caching a dynamic source. fn resolve<'pat>( &'pat self, dynamic_source: Option, cache: &mut EndRegexCache, ) -> Option> { match self { Self::Static { source, regex } => Some(EndRegex { source: Cow::Borrowed(source), regex: Arc::clone(regex), }), Self::Dynamic { .. } => { let source = dynamic_source?; let regex = lookup_or_compile(cache, &source)?; Some(EndRegex { source: Cow::Owned(source), regex, }) } } } /// Returns true when this end pattern depends on begin-match captures. pub(super) fn is_dynamic(&self) -> bool { matches!(self, Self::Dynamic { .. }) } } impl EndRegex<'_> { /// End-regex source string. pub(super) fn source(&self) -> &str { &self.source } /// Compiled regex used for searching. pub(super) fn regex(&self) -> &Regex { &self.regex } /// Returns the resolved dynamic source that should be stored in line state. pub(super) fn dynamic_source(&self) -> Option { match &self.source { Cow::Owned(source) => Some(source.clone()), Cow::Borrowed(_) => None, } } } /// Returns the cached regex for `source`, compiling and inserting it on first use. fn lookup_or_compile(cache: &mut EndRegexCache, source: &str) -> Option> { if let Some(regex) = cache.get(source) { return Some(Arc::clone(regex)); } let regex = Arc::new(regex(source).ok()?); cache.insert(source.to_owned(), Arc::clone(®ex)); Some(regex) } /// Returns true when `source` contains an unescaped numeric backreference. fn has_numeric_backrefs(source: &str) -> bool { let mut backslashes = 0_usize; for byte in source.bytes() { if byte == b'\\' { backslashes += 1; } else { if byte.is_ascii_digit() && backslashes % 2 == 1 { return true; } backslashes = 0; } } false }