highlight: Simplify tokenizer pattern internals
0d1fe9a9cfde319d43b2425b83eae391c517b33d
The grammar tokenizer carried three parallel structures that duplicated each other. Assisted-by: Claude:claude-fable-5
1 parent
180c0cb2
crates/highlight/src/grammar/end.rs
+37 -48
| 3 | 3 | //! TextMate end patterns can be static (a regex known at compile time) or |
|
| 4 | 4 | //! dynamic (numeric backrefs into the begin match resolved per occurrence). |
|
| 5 | 5 | //! Resolved dynamic end regexes are cached so that a multi-line construct does |
|
| 6 | 6 | //! not recompile its end regex on every line. |
|
| 7 | 7 | ||
| 8 | + | use std::borrow::Cow; |
|
| 8 | 9 | use std::collections::BTreeMap; |
|
| 9 | 10 | use std::sync::Arc; |
|
| 10 | 11 | ||
| 11 | 12 | use onig::Regex; |
|
| 12 | 13 |
| 22 | 23 | /// End regex compiled at grammar-compile time. |
|
| 23 | 24 | Static { |
|
| 24 | 25 | /// Original regex source. |
|
| 25 | 26 | source: String, |
|
| 26 | 27 | /// Compiled regex. |
|
| 27 | - | regex: Regex, |
|
| 28 | + | regex: Arc<Regex>, |
|
| 28 | 29 | }, |
|
| 29 | 30 | /// End regex deferred until begin-match captures are known. |
|
| 30 | 31 | Dynamic { |
|
| 31 | 32 | /// Regex source containing numeric backrefs. |
|
| 32 | 33 | source: String, |
|
| 33 | 34 | }, |
|
| 34 | 35 | } |
|
| 35 | 36 | ||
| 36 | 37 | /// End regex resolved against a specific begin match. |
|
| 37 | 38 | /// |
|
| 38 | - | /// Static end patterns borrow their regex from the compiled grammar, while |
|
| 39 | - | /// dynamic patterns own a shared handle into the [`EndRegexCache`]. Both |
|
| 40 | - | /// variants expose the same [`Self::source`] and [`Self::regex`] accessors so |
|
| 41 | - | /// the tokenizer can treat them uniformly. |
|
| 42 | - | pub(super) enum EndRegex<'pat> { |
|
| 43 | - | /// End regex compiled with the grammar. |
|
| 44 | - | Static { |
|
| 45 | - | /// Source string for tokenizer heuristics (e.g. escaped-quote skip). |
|
| 46 | - | source: &'pat str, |
|
| 47 | - | /// Compiled regex. |
|
| 48 | - | regex: &'pat Regex, |
|
| 49 | - | }, |
|
| 50 | - | /// End regex resolved from begin captures and shared via the cache. |
|
| 51 | - | Dynamic { |
|
| 52 | - | /// Resolved source string (after backref substitution). |
|
| 53 | - | source: String, |
|
| 54 | - | /// Compiled regex held jointly with the cache. |
|
| 55 | - | regex: Arc<Regex>, |
|
| 56 | - | }, |
|
| 39 | + | /// Static end patterns borrow their source from the compiled grammar, while |
|
| 40 | + | /// dynamic patterns own the source resolved from the begin captures; the |
|
| 41 | + | /// compiled regex is shared with the grammar or the [`EndRegexCache`]. |
|
| 42 | + | pub(super) struct EndRegex<'pat> { |
|
| 43 | + | /// Source string, owned when resolved from a dynamic end pattern. |
|
| 44 | + | source: Cow<'pat, str>, |
|
| 45 | + | /// Compiled regex used for searching. |
|
| 46 | + | regex: Arc<Regex>, |
|
| 57 | 47 | } |
|
| 58 | 48 | ||
| 59 | 49 | impl EndPattern { |
|
| 60 | 50 | /// Compiles an end pattern, deferring numeric-backref patterns until match time. |
|
| 61 | 51 | pub(super) fn compile(source: &str) -> Option<Self> { |
|
| 62 | 52 | if has_numeric_backrefs(source) { |
|
| 63 | 53 | return Some(Self::Dynamic { |
|
| 64 | 54 | source: source.to_owned(), |
|
| 65 | 55 | }); |
|
| 66 | 56 | } |
|
| 67 | - | let regex = regex(source).ok()?; |
|
| 57 | + | let regex = Arc::new(regex(source).ok()?); |
|
| 68 | 58 | ||
| 69 | 59 | Some(Self::Static { |
|
| 70 | 60 | source: source.to_owned(), |
|
| 71 | 61 | regex, |
|
| 72 | 62 | }) |
| 77 | 67 | &'pat self, |
|
| 78 | 68 | begin: &RegexMatch, |
|
| 79 | 69 | line: &str, |
|
| 80 | 70 | cache: &mut EndRegexCache, |
|
| 81 | 71 | ) -> Option<EndRegex<'pat>> { |
|
| 82 | - | match self { |
|
| 83 | - | Self::Static { source, regex } => Some(EndRegex::Static { source, regex }), |
|
| 84 | - | Self::Dynamic { source } => { |
|
| 85 | - | let resolved = begin.expand_backrefs(source, line); |
|
| 86 | - | let regex = lookup_or_compile(cache, &resolved)?; |
|
| 87 | - | ||
| 88 | - | Some(EndRegex::Dynamic { |
|
| 89 | - | source: resolved, |
|
| 90 | - | regex, |
|
| 91 | - | }) |
|
| 92 | - | } |
|
| 93 | - | } |
|
| 72 | + | let resolved = match self { |
|
| 73 | + | Self::Static { .. } => None, |
|
| 74 | + | Self::Dynamic { source } => Some(begin.expand_backrefs(source, line)), |
|
| 75 | + | }; |
|
| 76 | + | self.resolve(resolved, cache) |
|
| 94 | 77 | } |
|
| 95 | 78 | ||
| 96 | 79 | /// Resumes this end pattern using a dynamic source previously stored in line state. |
|
| 97 | 80 | pub(super) fn resume<'pat>( |
|
| 98 | 81 | &'pat self, |
|
| 99 | 82 | dynamic_source: Option<&str>, |
|
| 100 | 83 | cache: &mut EndRegexCache, |
|
| 84 | + | ) -> Option<EndRegex<'pat>> { |
|
| 85 | + | self.resolve(dynamic_source.map(str::to_owned), cache) |
|
| 86 | + | } |
|
| 87 | + | ||
| 88 | + | /// Builds the resolved end regex, compiling and caching a dynamic source. |
|
| 89 | + | fn resolve<'pat>( |
|
| 90 | + | &'pat self, |
|
| 91 | + | dynamic_source: Option<String>, |
|
| 92 | + | cache: &mut EndRegexCache, |
|
| 101 | 93 | ) -> Option<EndRegex<'pat>> { |
|
| 102 | 94 | match self { |
|
| 103 | - | Self::Static { source, regex } => Some(EndRegex::Static { source, regex }), |
|
| 95 | + | Self::Static { source, regex } => Some(EndRegex { |
|
| 96 | + | source: Cow::Borrowed(source), |
|
| 97 | + | regex: Arc::clone(regex), |
|
| 98 | + | }), |
|
| 104 | 99 | Self::Dynamic { .. } => { |
|
| 105 | 100 | let source = dynamic_source?; |
|
| 106 | - | let regex = lookup_or_compile(cache, source)?; |
|
| 101 | + | let regex = lookup_or_compile(cache, &source)?; |
|
| 107 | 102 | ||
| 108 | - | Some(EndRegex::Dynamic { |
|
| 109 | - | source: source.to_owned(), |
|
| 103 | + | Some(EndRegex { |
|
| 104 | + | source: Cow::Owned(source), |
|
| 110 | 105 | regex, |
|
| 111 | 106 | }) |
|
| 112 | 107 | } |
|
| 113 | 108 | } |
|
| 114 | 109 | } |
| 120 | 115 | } |
|
| 121 | 116 | ||
| 122 | 117 | impl EndRegex<'_> { |
|
| 123 | 118 | /// End-regex source string. |
|
| 124 | 119 | pub(super) fn source(&self) -> &str { |
|
| 125 | - | match self { |
|
| 126 | - | Self::Static { source, .. } => source, |
|
| 127 | - | Self::Dynamic { source, .. } => source.as_str(), |
|
| 128 | - | } |
|
| 120 | + | &self.source |
|
| 129 | 121 | } |
|
| 130 | 122 | ||
| 131 | 123 | /// Compiled regex used for searching. |
|
| 132 | 124 | pub(super) fn regex(&self) -> &Regex { |
|
| 133 | - | match self { |
|
| 134 | - | Self::Static { regex, .. } => regex, |
|
| 135 | - | Self::Dynamic { regex, .. } => regex, |
|
| 136 | - | } |
|
| 125 | + | &self.regex |
|
| 137 | 126 | } |
|
| 138 | 127 | ||
| 139 | 128 | /// Returns the resolved dynamic source that should be stored in line state. |
|
| 140 | 129 | pub(super) fn dynamic_source(&self) -> Option<String> { |
|
| 141 | - | match self { |
|
| 142 | - | Self::Static { .. } => None, |
|
| 143 | - | Self::Dynamic { source, .. } => Some(source.clone()), |
|
| 130 | + | match &self.source { |
|
| 131 | + | Cow::Owned(source) => Some(source.clone()), |
|
| 132 | + | Cow::Borrowed(_) => None, |
|
| 144 | 133 | } |
|
| 145 | 134 | } |
|
| 146 | 135 | } |
|
| 147 | 136 | ||
| 148 | 137 | /// Returns the cached regex for `source`, compiling and inserting it on first use. |
crates/highlight/src/grammar/pattern.rs
+62 -89
| 173 | 173 | pub(super) spans: Vec<ScopeSpan>, |
|
| 174 | 174 | /// New rule pushed onto the parser stack, when this match opened one. |
|
| 175 | 175 | pub(super) open_rule: Option<OpenRule>, |
|
| 176 | 176 | } |
|
| 177 | 177 | ||
| 178 | - | /// Shared inputs for finding the next pattern. |
|
| 178 | + | /// Shared inputs for finding the next pattern and emitting capture spans. |
|
| 179 | 179 | pub(super) struct PatternSearch<'line, 'sets, 'cache> { |
|
| 180 | 180 | /// Pattern sets in the compiled grammar. |
|
| 181 | 181 | sets: &'sets [PatternSet], |
|
| 182 | 182 | /// Line being searched. |
|
| 183 | 183 | line: &'line str, |
| 187 | 187 | cache: &'cache mut EndRegexCache, |
|
| 188 | 188 | /// Anchor availability for this search. |
|
| 189 | 189 | anchors: AnchorState, |
|
| 190 | 190 | } |
|
| 191 | 191 | ||
| 192 | - | /// Shared inputs for emitting capture spans. |
|
| 193 | - | pub(super) struct SpanContext<'line, 'sets, 'cache> { |
|
| 194 | - | /// Pattern sets in the compiled grammar. |
|
| 195 | - | sets: &'sets [PatternSet], |
|
| 196 | - | /// Line being tokenized. |
|
| 197 | - | line: &'line str, |
|
| 198 | - | /// Cache for dynamically resolved end regexes. |
|
| 199 | - | cache: &'cache mut EndRegexCache, |
|
| 200 | - | /// Anchor availability for nested capture searches. |
|
| 201 | - | anchors: AnchorState, |
|
| 202 | - | } |
|
| 203 | - | ||
| 204 | 192 | impl<'line, 'sets, 'cache> PatternSearch<'line, 'sets, 'cache> { |
|
| 205 | 193 | /// Creates a pattern search context. |
|
| 206 | 194 | pub(super) fn new( |
|
| 207 | 195 | sets: &'sets [PatternSet], |
|
| 208 | 196 | line: &'line str, |
| 216 | 204 | limit, |
|
| 217 | 205 | cache, |
|
| 218 | 206 | anchors, |
|
| 219 | 207 | } |
|
| 220 | 208 | } |
|
| 221 | - | ||
| 222 | - | /// Creates a capture-span context from this search. |
|
| 223 | - | fn span_context(&mut self) -> SpanContext<'line, 'sets, '_> { |
|
| 224 | - | SpanContext { |
|
| 225 | - | sets: self.sets, |
|
| 226 | - | line: self.line, |
|
| 227 | - | cache: self.cache, |
|
| 228 | - | anchors: self.anchors, |
|
| 229 | - | } |
|
| 230 | - | } |
|
| 231 | - | } |
|
| 232 | - | ||
| 233 | - | impl<'line, 'sets, 'cache> SpanContext<'line, 'sets, 'cache> { |
|
| 234 | - | /// Creates a span emission context. |
|
| 235 | - | pub(super) fn new( |
|
| 236 | - | sets: &'sets [PatternSet], |
|
| 237 | - | line: &'line str, |
|
| 238 | - | cache: &'cache mut EndRegexCache, |
|
| 239 | - | anchors: AnchorState, |
|
| 240 | - | ) -> Self { |
|
| 241 | - | Self { |
|
| 242 | - | sets, |
|
| 243 | - | line, |
|
| 244 | - | cache, |
|
| 245 | - | anchors, |
|
| 246 | - | } |
|
| 247 | - | } |
|
| 248 | 209 | } |
|
| 249 | 210 | ||
| 250 | 211 | /// Raw regex hit: byte range plus the positions of any retained capture groups. |
|
| 251 | 212 | pub(super) struct RegexMatch { |
|
| 252 | 213 | /// Start byte of the full regex match. |
| 424 | 385 | fn push_spans( |
|
| 425 | 386 | &self, |
|
| 426 | 387 | spans: &mut Vec<ScopeSpan>, |
|
| 427 | 388 | start: usize, |
|
| 428 | 389 | end: usize, |
|
| 429 | - | context: &mut SpanContext<'_, '_, '_>, |
|
| 390 | + | context: &mut PatternSearch<'_, '_, '_>, |
|
| 430 | 391 | ) { |
|
| 431 | 392 | Scope::push_content_visible(&self.scope, &self.content_scope, spans, start, end); |
|
| 432 | 393 | if let Some(nested) = self.nested { |
|
| 433 | 394 | retokenize_capture(nested, spans, start, end, context); |
|
| 434 | 395 | } |
|
| 435 | 396 | } |
|
| 436 | 397 | } |
|
| 437 | 398 | ||
| 438 | - | /// One event found while retokenizing a captured region. |
|
| 439 | - | enum CaptureEvent { |
|
| 440 | - | /// A pattern matched inside the region, possibly opening a begin/end rule. |
|
| 441 | - | Match(Match), |
|
| 442 | - | /// The innermost open rule's end pattern matched. |
|
| 399 | + | /// One event found inside an open begin/end rule. |
|
| 400 | + | pub(super) enum InsideEvent { |
|
| 401 | + | /// Nested pattern matched before the end pattern. |
|
| 402 | + | Nested(Match), |
|
| 403 | + | /// End pattern closed the open rule. |
|
| 443 | 404 | Close(RegexMatch), |
|
| 444 | - | /// Nothing further matches inside the region. |
|
| 405 | + | /// Neither nested nor end matched on the rest of the searched region. |
|
| 445 | 406 | None, |
|
| 446 | 407 | } |
|
| 447 | 408 | ||
| 409 | + | impl InsideEvent { |
|
| 410 | + | /// Returns the byte position this event advances to. |
|
| 411 | + | pub(super) fn next_pos(&self, line: &str) -> usize { |
|
| 412 | + | match self { |
|
| 413 | + | Self::Close(close) => close.end, |
|
| 414 | + | Self::Nested(nested) => nested.next_pos_after_open(line), |
|
| 415 | + | Self::None => line.len(), |
|
| 416 | + | } |
|
| 417 | + | } |
|
| 418 | + | } |
|
| 419 | + | ||
| 448 | 420 | /// Returns the pattern for a rule id by scanning the pattern sets. |
|
| 449 | 421 | fn pattern_by_rule(sets: &[PatternSet], rule_id: usize) -> Option<&Pattern> { |
|
| 450 | 422 | sets.iter() |
|
| 451 | 423 | .find_map(|set| set.patterns.iter().find(|pattern| pattern.id == rule_id)) |
|
| 452 | 424 | } |
| 461 | 433 | fn retokenize_capture( |
|
| 462 | 434 | root: PatternSetId, |
|
| 463 | 435 | spans: &mut Vec<ScopeSpan>, |
|
| 464 | 436 | start: usize, |
|
| 465 | 437 | end: usize, |
|
| 466 | - | context: &mut SpanContext<'_, '_, '_>, |
|
| 438 | + | context: &mut PatternSearch<'_, '_, '_>, |
|
| 467 | 439 | ) { |
|
| 468 | 440 | let sets = context.sets; |
|
| 469 | 441 | let line = context.line; |
|
| 470 | 442 | let mut search = PatternSearch::new(sets, &line[..end], end, context.cache, context.anchors); |
|
| 471 | 443 | let emit_open_content = |
| 489 | 461 | let Some(end_regex) = pattern.resume_end(open.dynamic_rule.as_deref(), search.cache) |
|
| 490 | 462 | else { |
|
| 491 | 463 | stack.pop(); |
|
| 492 | 464 | continue; |
|
| 493 | 465 | }; |
|
| 494 | - | let nested = pattern.nested.find_next(&mut search, pos); |
|
| 495 | - | let close = pattern |
|
| 496 | - | .find_end_match(&end_regex, line, pos, search.anchors) |
|
| 497 | - | .filter(|close| close.start <= end); |
|
| 498 | - | match (nested, close) { |
|
| 499 | - | (Some(nested), Some(close)) |
|
| 500 | - | if nested.start < close.start |
|
| 501 | - | || (pattern.apply_end_pattern_last() && nested.start == close.start) => |
|
| 502 | - | { |
|
| 503 | - | CaptureEvent::Match(nested) |
|
| 504 | - | } |
|
| 505 | - | (_, Some(close)) => CaptureEvent::Close(close), |
|
| 506 | - | (Some(nested), None) => CaptureEvent::Match(nested), |
|
| 507 | - | (None, None) => CaptureEvent::None, |
|
| 508 | - | } |
|
| 466 | + | pattern.next_event_inside(&end_regex, line, pos, &mut search) |
|
| 509 | 467 | } else { |
|
| 510 | 468 | match root.find_next(&mut search, pos) { |
|
| 511 | - | Some(matched) => CaptureEvent::Match(matched), |
|
| 512 | - | None => CaptureEvent::None, |
|
| 469 | + | Some(matched) => InsideEvent::Nested(matched), |
|
| 470 | + | None => InsideEvent::None, |
|
| 513 | 471 | } |
|
| 514 | 472 | }; |
|
| 515 | 473 | ||
| 516 | 474 | let previous_depth = stack.len(); |
|
| 517 | 475 | let next_pos = match event { |
|
| 518 | - | CaptureEvent::Match(matched) => { |
|
| 476 | + | InsideEvent::Nested(matched) => { |
|
| 519 | 477 | let next_pos = matched.next_pos_after_open(line).min(end); |
|
| 520 | 478 | emit_open_content(spans, &stack, pos, next_pos); |
|
| 521 | 479 | spans.extend(matched.spans.into_iter().filter_map(|mut span| { |
|
| 522 | 480 | span.end = span.end.min(end); |
|
| 523 | 481 | (span.start < span.end).then_some(span) |
| 525 | 483 | if let Some(opened) = matched.open_rule { |
|
| 526 | 484 | stack.push(opened); |
|
| 527 | 485 | } |
|
| 528 | 486 | next_pos |
|
| 529 | 487 | } |
|
| 530 | - | CaptureEvent::Close(close) => { |
|
| 488 | + | InsideEvent::Close(close) => { |
|
| 531 | 489 | emit_open_content(spans, &stack, pos, close.start); |
|
| 532 | 490 | if let Some(pattern) = stack |
|
| 533 | 491 | .last() |
|
| 534 | 492 | .and_then(|open| pattern_by_rule(sets, open.rule_id)) |
|
| 535 | 493 | { |
|
| 536 | - | let mut context = |
|
| 537 | - | SpanContext::new(sets, search.line, search.cache, search.anchors); |
|
| 538 | - | pattern.append_capture_spans( |
|
| 539 | - | &close, |
|
| 540 | - | &pattern.captures.end, |
|
| 541 | - | spans, |
|
| 542 | - | &mut context, |
|
| 543 | - | ); |
|
| 494 | + | pattern.append_capture_spans(&close, &pattern.captures.end, spans, &mut search); |
|
| 544 | 495 | } |
|
| 545 | 496 | stack.pop(); |
|
| 546 | 497 | close.end.min(end) |
|
| 547 | 498 | } |
|
| 548 | - | CaptureEvent::None => { |
|
| 499 | + | InsideEvent::None => { |
|
| 549 | 500 | emit_open_content(spans, &stack, pos, end); |
|
| 550 | 501 | break; |
|
| 551 | 502 | } |
|
| 552 | 503 | }; |
|
| 553 | 504 | // A match either advances or opens a rule; otherwise skip one byte. |
| 1196 | 1147 | } |
|
| 1197 | 1148 | return Some(matched); |
|
| 1198 | 1149 | } |
|
| 1199 | 1150 | } |
|
| 1200 | 1151 | ||
| 1152 | + | /// Finds the next event inside this open rule: a nested pattern match, the |
|
| 1153 | + | /// end pattern closing the rule, or neither within the search limit. |
|
| 1154 | + | /// |
|
| 1155 | + | /// The end pattern is searched on `line`, which may extend past the search |
|
| 1156 | + | /// limit; an end match counts only when it starts inside the limit. |
|
| 1157 | + | pub(super) fn next_event_inside( |
|
| 1158 | + | &self, |
|
| 1159 | + | end: &EndRegex<'_>, |
|
| 1160 | + | line: &str, |
|
| 1161 | + | pos: usize, |
|
| 1162 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 1163 | + | ) -> InsideEvent { |
|
| 1164 | + | let nested = self.nested.find_next(search, pos); |
|
| 1165 | + | let close = self |
|
| 1166 | + | .find_end_match(end, line, pos, search.anchors) |
|
| 1167 | + | .filter(|close| close.start <= search.limit); |
|
| 1168 | + | ||
| 1169 | + | match (nested, close) { |
|
| 1170 | + | (Some(nested), Some(close)) |
|
| 1171 | + | if nested.start < close.start |
|
| 1172 | + | || (self.apply_end_pattern_last() && nested.start == close.start) => |
|
| 1173 | + | { |
|
| 1174 | + | InsideEvent::Nested(nested) |
|
| 1175 | + | } |
|
| 1176 | + | (_, Some(close)) => InsideEvent::Close(close), |
|
| 1177 | + | (Some(nested), None) => InsideEvent::Nested(nested), |
|
| 1178 | + | (None, None) => InsideEvent::None, |
|
| 1179 | + | } |
|
| 1180 | + | } |
|
| 1181 | + | ||
| 1201 | 1182 | /// Adds capture-group spans plus the pattern's own scope span for one regex match. |
|
| 1202 | 1183 | pub(super) fn append_match_spans( |
|
| 1203 | 1184 | &self, |
|
| 1204 | 1185 | matched: &RegexMatch, |
|
| 1205 | 1186 | captures: &Captures, |
|
| 1206 | 1187 | spans: &mut Vec<ScopeSpan>, |
|
| 1207 | - | context: &mut SpanContext<'_, '_, '_>, |
|
| 1188 | + | context: &mut PatternSearch<'_, '_, '_>, |
|
| 1208 | 1189 | ) { |
|
| 1209 | 1190 | self.scope.push_visible(spans, matched.start, matched.end); |
|
| 1210 | 1191 | self.append_capture_spans(matched, captures, spans, context); |
|
| 1211 | 1192 | spans.sort_by_key(|span| (span.start, span.end)); |
|
| 1212 | 1193 | } |
| 1215 | 1196 | pub(super) fn append_capture_spans( |
|
| 1216 | 1197 | &self, |
|
| 1217 | 1198 | matched: &RegexMatch, |
|
| 1218 | 1199 | captures: &Captures, |
|
| 1219 | 1200 | spans: &mut Vec<ScopeSpan>, |
|
| 1220 | - | context: &mut SpanContext<'_, '_, '_>, |
|
| 1201 | + | context: &mut PatternSearch<'_, '_, '_>, |
|
| 1221 | 1202 | ) { |
|
| 1222 | 1203 | for (index, capture) in captures { |
|
| 1223 | 1204 | if let Some(Some((start, end))) = matched.captures.get(*index) |
|
| 1224 | 1205 | && *start < *end |
|
| 1225 | 1206 | { |
| 1270 | 1251 | &self, |
|
| 1271 | 1252 | regex_match: RegexMatch, |
|
| 1272 | 1253 | search: &mut PatternSearch<'_, '_, '_>, |
|
| 1273 | 1254 | ) -> Match { |
|
| 1274 | 1255 | let mut spans = Vec::new(); |
|
| 1275 | - | let mut context = search.span_context(); |
|
| 1276 | - | self.append_match_spans( |
|
| 1277 | - | ®ex_match, |
|
| 1278 | - | &self.captures.matched, |
|
| 1279 | - | &mut spans, |
|
| 1280 | - | &mut context, |
|
| 1281 | - | ); |
|
| 1256 | + | self.append_match_spans(®ex_match, &self.captures.matched, &mut spans, search); |
|
| 1282 | 1257 | Match { |
|
| 1283 | 1258 | start: regex_match.start, |
|
| 1284 | 1259 | end: regex_match.end, |
|
| 1285 | 1260 | spans, |
|
| 1286 | 1261 | open_rule: None, |
| 1295 | 1270 | search: &mut PatternSearch<'_, '_, '_>, |
|
| 1296 | 1271 | ) -> Option<Match> { |
|
| 1297 | 1272 | let end_regex = end_pattern.resolve_for_begin(&begin, search.line, search.cache)?; |
|
| 1298 | 1273 | let dynamic_rule = end_regex.dynamic_source(); |
|
| 1299 | 1274 | let mut spans = Vec::new(); |
|
| 1300 | - | let mut context = search.span_context(); |
|
| 1301 | - | self.append_match_spans(&begin, &self.captures.begin, &mut spans, &mut context); |
|
| 1275 | + | self.append_match_spans(&begin, &self.captures.begin, &mut spans, search); |
|
| 1302 | 1276 | ||
| 1303 | 1277 | Some(Match { |
|
| 1304 | 1278 | start: begin.start, |
|
| 1305 | 1279 | end: begin.end, |
|
| 1306 | 1280 | spans, |
| 1323 | 1297 | }; |
|
| 1324 | 1298 | let while_regex = while_rule.resolve_for_begin(&begin, search.line, search.cache); |
|
| 1325 | 1299 | let dynamic_rule = while_regex.and_then(|while_regex| while_regex.dynamic_source()); |
|
| 1326 | 1300 | let mut spans = Vec::new(); |
|
| 1327 | 1301 | ||
| 1328 | - | let mut context = search.span_context(); |
|
| 1329 | - | self.append_match_spans(&begin, &self.captures.begin, &mut spans, &mut context); |
|
| 1302 | + | self.append_match_spans(&begin, &self.captures.begin, &mut spans, search); |
|
| 1330 | 1303 | self.scope.push_visible(&mut spans, begin.start, begin.end); |
|
| 1331 | 1304 | ||
| 1332 | 1305 | Match { |
|
| 1333 | 1306 | start: begin.start, |
|
| 1334 | 1307 | end: begin.end, |
crates/highlight/src/grammar/tokenize.rs
+18 -61
| 2 | 2 | //! |
|
| 3 | 3 | //! Each step picks the next event in the line — a fresh root-pattern match, a |
|
| 4 | 4 | //! nested match inside the currently open begin/end rule, or that rule's end — |
|
| 5 | 5 | //! emits the spans it produces, and updates the parser state accordingly. |
|
| 6 | 6 | ||
| 7 | - | use super::end::{EndRegex, EndRegexCache}; |
|
| 8 | - | use super::pattern::{Match, OpenRule, Pattern, PatternSearch, RegexMatch, SpanContext}; |
|
| 7 | + | use super::end::EndRegexCache; |
|
| 8 | + | use super::pattern::{InsideEvent, Match, OpenRule, Pattern, PatternSearch}; |
|
| 9 | 9 | use super::selector::InjectionPriority; |
|
| 10 | 10 | use super::*; |
|
| 11 | 11 | ||
| 12 | 12 | /// Reusable line tokenizer for one compiled grammar. |
|
| 13 | 13 | /// |
| 129 | 129 | return pos; |
|
| 130 | 130 | }; |
|
| 131 | 131 | let Some(end) = pattern.resume_end(open.dynamic_rule.as_deref(), &mut self.cache) else { |
|
| 132 | 132 | return self.advance_inside_while_rule(state, line, pos, spans, pattern, anchors); |
|
| 133 | 133 | }; |
|
| 134 | - | let event = self.next_event_inside(pattern, &end, line, pos, *anchors); |
|
| 134 | + | let mut search = PatternSearch::new( |
|
| 135 | + | &self.grammar.pattern_sets, |
|
| 136 | + | line, |
|
| 137 | + | line.len(), |
|
| 138 | + | &mut self.cache, |
|
| 139 | + | *anchors, |
|
| 140 | + | ); |
|
| 141 | + | let event = pattern.next_event_inside(&end, line, pos, &mut search); |
|
| 135 | 142 | let next_pos = event.next_pos(line); |
|
| 136 | 143 | // Parent rules wrap the whole span consumed by this step; the current |
|
| 137 | 144 | // rule's own content scope is emitted below for each event. |
|
| 138 | 145 | if let Some((_current, parents)) = state.stack.split_last() { |
|
| 139 | 146 | for open in parents { |
| 144 | 151 | } |
|
| 145 | 152 | ||
| 146 | 153 | match event { |
|
| 147 | 154 | InsideEvent::Close(close) => { |
|
| 148 | 155 | pattern.push_content_visible(spans, pos, close.start); |
|
| 149 | - | let mut span_context = |
|
| 150 | - | SpanContext::new(&self.grammar.pattern_sets, line, &mut self.cache, *anchors); |
|
| 151 | - | pattern.append_match_spans(&close, &pattern.captures.end, spans, &mut span_context); |
|
| 156 | + | pattern.append_match_spans(&close, &pattern.captures.end, spans, &mut search); |
|
| 152 | 157 | if let Some(closed) = state.stack.pop() { |
|
| 153 | 158 | anchors.restore_begin_position(closed.anchor_position); |
|
| 154 | 159 | } |
|
| 155 | 160 | } |
|
| 156 | 161 | InsideEvent::Nested(nested) => { |
| 201 | 206 | break; |
|
| 202 | 207 | } |
|
| 203 | 208 | index += 1; |
|
| 204 | 209 | continue; |
|
| 205 | 210 | }; |
|
| 206 | - | let mut context = |
|
| 207 | - | SpanContext::new(&self.grammar.pattern_sets, line, &mut self.cache, *anchors); |
|
| 211 | + | let mut context = PatternSearch::new( |
|
| 212 | + | &self.grammar.pattern_sets, |
|
| 213 | + | line, |
|
| 214 | + | line.len(), |
|
| 215 | + | &mut self.cache, |
|
| 216 | + | *anchors, |
|
| 217 | + | ); |
|
| 208 | 218 | pattern.append_capture_spans( |
|
| 209 | 219 | &matched, |
|
| 210 | 220 | &pattern.captures.while_rule, |
|
| 211 | 221 | spans, |
|
| 212 | 222 | &mut context, |
| 316 | 326 | break; |
|
| 317 | 327 | } |
|
| 318 | 328 | } |
|
| 319 | 329 | } |
|
| 320 | 330 | ||
| 321 | - | /// Finds the next event (nested match, end match, or end of line) inside an open rule. |
|
| 322 | - | fn next_event_inside( |
|
| 323 | - | &mut self, |
|
| 324 | - | pattern: &Pattern, |
|
| 325 | - | end: &EndRegex<'_>, |
|
| 326 | - | line: &str, |
|
| 327 | - | pos: usize, |
|
| 328 | - | anchors: AnchorState, |
|
| 329 | - | ) -> InsideEvent { |
|
| 330 | - | let mut search = PatternSearch::new( |
|
| 331 | - | &self.grammar.pattern_sets, |
|
| 332 | - | line, |
|
| 333 | - | line.len(), |
|
| 334 | - | &mut self.cache, |
|
| 335 | - | anchors, |
|
| 336 | - | ); |
|
| 337 | - | let nested = pattern.nested.find_next(&mut search, pos); |
|
| 338 | - | let close = pattern.find_end_match(end, line, pos, anchors); |
|
| 339 | - | ||
| 340 | - | match (nested, close) { |
|
| 341 | - | (Some(nested), Some(close)) |
|
| 342 | - | if nested.start < close.start |
|
| 343 | - | || (pattern.apply_end_pattern_last() && nested.start == close.start) => |
|
| 344 | - | { |
|
| 345 | - | InsideEvent::Nested(nested) |
|
| 346 | - | } |
|
| 347 | - | (_, Some(close)) => InsideEvent::Close(close), |
|
| 348 | - | (Some(nested), None) => InsideEvent::Nested(nested), |
|
| 349 | - | (None, None) => InsideEvent::None, |
|
| 350 | - | } |
|
| 351 | - | } |
|
| 352 | - | ||
| 353 | 331 | /// Returns the number of cached dynamic end regexes (test helper). |
|
| 354 | 332 | #[cfg(test)] |
|
| 355 | 333 | pub(super) fn end_regex_cache_len(&self) -> usize { |
|
| 356 | 334 | self.cache.len() |
|
| 357 | 335 | } |
| 362 | 340 | spans.retain_mut(|span| { |
|
| 363 | 341 | span.end = span.end.min(visible_len); |
|
| 364 | 342 | span.start < span.end |
|
| 365 | 343 | }); |
|
| 366 | 344 | } |
|
| 367 | - | ||
| 368 | - | /// One event found inside an open begin/end rule. |
|
| 369 | - | enum InsideEvent { |
|
| 370 | - | /// Nested pattern matched before the end pattern. |
|
| 371 | - | Nested(Match), |
|
| 372 | - | /// End pattern closed the open rule. |
|
| 373 | - | Close(RegexMatch), |
|
| 374 | - | /// Neither nested nor end matched on the rest of this line. |
|
| 375 | - | None, |
|
| 376 | - | } |
|
| 377 | - | ||
| 378 | - | impl InsideEvent { |
|
| 379 | - | /// Returns the byte position this event advances to. |
|
| 380 | - | fn next_pos(&self, line: &str) -> usize { |
|
| 381 | - | match self { |
|
| 382 | - | Self::Close(close) => close.end, |
|
| 383 | - | Self::Nested(nested) => nested.next_pos_after_open(line), |
|
| 384 | - | Self::None => line.len(), |
|
| 385 | - | } |
|
| 386 | - | } |
|
| 387 | - | } |