highlight: Group textmate tokenizer context
79edb80ac12f843dcc2e0320bf3df477152bbf5c
Replace long tokenizer helper argument lists with small context structs so the TextMate correctness changes satisfy the workspace clippy gate without suppressing lints.
1 parent
500bb0cf
crates/highlight/src/grammar/pattern.rs
+152 -120
| 141 | 141 | pub(super) spans: Vec<ScopeSpan>, |
|
| 142 | 142 | /// New rule pushed onto the parser stack, when this match opened one. |
|
| 143 | 143 | open_rule: Option<OpenRule>, |
|
| 144 | 144 | } |
|
| 145 | 145 | ||
| 146 | + | /// Shared inputs for finding the next pattern. |
|
| 147 | + | pub(super) struct PatternSearch<'line, 'sets, 'cache> { |
|
| 148 | + | /// Pattern sets in the compiled grammar. |
|
| 149 | + | sets: &'sets [PatternSet], |
|
| 150 | + | /// Line being searched. |
|
| 151 | + | line: &'line str, |
|
| 152 | + | /// Exclusive byte limit for the search. |
|
| 153 | + | limit: usize, |
|
| 154 | + | /// Cache for dynamically resolved end regexes. |
|
| 155 | + | cache: &'cache mut EndRegexCache, |
|
| 156 | + | /// Anchor availability for this search. |
|
| 157 | + | anchors: AnchorState, |
|
| 158 | + | } |
|
| 159 | + | ||
| 160 | + | /// Shared inputs for emitting capture spans. |
|
| 161 | + | pub(super) struct SpanContext<'line, 'sets, 'cache> { |
|
| 162 | + | /// Pattern sets in the compiled grammar. |
|
| 163 | + | sets: &'sets [PatternSet], |
|
| 164 | + | /// Line being tokenized. |
|
| 165 | + | line: &'line str, |
|
| 166 | + | /// Cache for dynamically resolved end regexes. |
|
| 167 | + | cache: &'cache mut EndRegexCache, |
|
| 168 | + | /// Anchor availability for nested capture searches. |
|
| 169 | + | anchors: AnchorState, |
|
| 170 | + | } |
|
| 171 | + | ||
| 172 | + | impl<'line, 'sets, 'cache> PatternSearch<'line, 'sets, 'cache> { |
|
| 173 | + | /// Creates a pattern search context. |
|
| 174 | + | pub(super) fn new( |
|
| 175 | + | sets: &'sets [PatternSet], |
|
| 176 | + | line: &'line str, |
|
| 177 | + | limit: usize, |
|
| 178 | + | cache: &'cache mut EndRegexCache, |
|
| 179 | + | anchors: AnchorState, |
|
| 180 | + | ) -> Self { |
|
| 181 | + | Self { |
|
| 182 | + | sets, |
|
| 183 | + | line, |
|
| 184 | + | limit, |
|
| 185 | + | cache, |
|
| 186 | + | anchors, |
|
| 187 | + | } |
|
| 188 | + | } |
|
| 189 | + | ||
| 190 | + | /// Creates a capture-span context from this search. |
|
| 191 | + | fn span_context(&mut self) -> SpanContext<'line, 'sets, '_> { |
|
| 192 | + | SpanContext { |
|
| 193 | + | sets: self.sets, |
|
| 194 | + | line: self.line, |
|
| 195 | + | cache: self.cache, |
|
| 196 | + | anchors: self.anchors, |
|
| 197 | + | } |
|
| 198 | + | } |
|
| 199 | + | } |
|
| 200 | + | ||
| 201 | + | impl<'line, 'sets, 'cache> SpanContext<'line, 'sets, 'cache> { |
|
| 202 | + | /// Creates a span emission context. |
|
| 203 | + | pub(super) fn new( |
|
| 204 | + | sets: &'sets [PatternSet], |
|
| 205 | + | line: &'line str, |
|
| 206 | + | cache: &'cache mut EndRegexCache, |
|
| 207 | + | anchors: AnchorState, |
|
| 208 | + | ) -> Self { |
|
| 209 | + | Self { |
|
| 210 | + | sets, |
|
| 211 | + | line, |
|
| 212 | + | cache, |
|
| 213 | + | anchors, |
|
| 214 | + | } |
|
| 215 | + | } |
|
| 216 | + | } |
|
| 217 | + | ||
| 146 | 218 | /// Raw regex hit: byte range plus the positions of any retained capture groups. |
|
| 147 | 219 | pub(super) struct RegexMatch { |
|
| 148 | 220 | /// Start byte of the full regex match. |
|
| 149 | 221 | pub(super) start: usize, |
|
| 150 | 222 | /// End byte of the full regex match. |
| 207 | 279 | let Some(name) = name else { return Self::None }; |
|
| 208 | 280 | let path = name |
|
| 209 | 281 | .split_whitespace() |
|
| 210 | 282 | .map(|scope| interner.intern(scope)) |
|
| 211 | 283 | .collect::<Vec<_>>(); |
|
| 284 | + | // A blank or whitespace-only name interns no scopes; treat it as unscoped. |
|
| 212 | 285 | if path.is_empty() { |
|
| 213 | 286 | return Self::None; |
|
| 214 | 287 | } |
|
| 215 | 288 | if name.starts_with("meta.structure.") { |
|
| 216 | 289 | Self::Structural(path) |
| 224 | 297 | !matches!(self, Self::None) |
|
| 225 | 298 | } |
|
| 226 | 299 | ||
| 227 | 300 | /// Pushes a span for this scope's region when it is visible and non-empty. |
|
| 228 | 301 | pub(super) fn push_visible(&self, spans: &mut Vec<ScopeSpan>, start: usize, end: usize) { |
|
| 229 | - | if let Self::Visible(path) = self { |
|
| 302 | + | if let Self::Visible(path) = self |
|
| 303 | + | && start < end |
|
| 304 | + | { |
|
| 230 | 305 | for scope in path { |
|
| 231 | - | if start < end { |
|
| 232 | - | spans.push(ScopeSpan { |
|
| 233 | - | start, |
|
| 234 | - | end, |
|
| 235 | - | scope: *scope, |
|
| 236 | - | }); |
|
| 237 | - | } |
|
| 306 | + | spans.push(ScopeSpan { |
|
| 307 | + | start, |
|
| 308 | + | end, |
|
| 309 | + | scope: *scope, |
|
| 310 | + | }); |
|
| 238 | 311 | } |
|
| 239 | 312 | } |
|
| 240 | 313 | } |
|
| 241 | 314 | ||
| 242 | 315 | /// Pushes visible name and content scopes for an inner rule region. |
| 255 | 328 | impl Capture { |
|
| 256 | 329 | /// Pushes this capture's direct and retokenized spans. |
|
| 257 | 330 | fn push_spans( |
|
| 258 | 331 | &self, |
|
| 259 | 332 | spans: &mut Vec<ScopeSpan>, |
|
| 260 | - | sets: &[PatternSet], |
|
| 261 | - | line: &str, |
|
| 262 | 333 | start: usize, |
|
| 263 | 334 | end: usize, |
|
| 264 | - | cache: &mut EndRegexCache, |
|
| 265 | - | anchors: AnchorState, |
|
| 335 | + | context: &mut SpanContext<'_, '_, '_>, |
|
| 266 | 336 | ) { |
|
| 267 | - | self.scope.push_visible(spans, start, end); |
|
| 268 | - | self.content_scope.push_visible(spans, start, end); |
|
| 337 | + | Scope::push_content_visible(&self.scope, &self.content_scope, spans, start, end); |
|
| 269 | 338 | let Some(nested) = self.nested else { |
|
| 270 | 339 | return; |
|
| 271 | 340 | }; |
|
| 341 | + | let mut search = PatternSearch::new( |
|
| 342 | + | context.sets, |
|
| 343 | + | context.line, |
|
| 344 | + | end, |
|
| 345 | + | context.cache, |
|
| 346 | + | context.anchors, |
|
| 347 | + | ); |
|
| 272 | 348 | let mut pos = start; |
|
| 273 | 349 | while pos < end { |
|
| 274 | - | let Some(matched) = nested.find_next(sets, line, pos, end, cache, anchors) else { |
|
| 350 | + | let Some(matched) = nested.find_next(&mut search, pos) else { |
|
| 275 | 351 | break; |
|
| 276 | 352 | }; |
|
| 353 | + | // The search is bounded at `end`, but a zero-width match can still |
|
| 354 | + | // land on the boundary; stop before emitting spans past the capture. |
|
| 277 | 355 | if matched.start >= end { |
|
| 278 | 356 | break; |
|
| 279 | 357 | } |
|
| 280 | - | let next_pos = matched.next_pos(line).min(end); |
|
| 358 | + | let next_pos = matched.next_pos(context.line).min(end); |
|
| 281 | 359 | spans.extend( |
|
| 282 | 360 | matched |
|
| 283 | 361 | .spans |
|
| 284 | 362 | .into_iter() |
|
| 285 | 363 | .filter_map(|span| clamp_scope_span(span, end)), |
| 320 | 398 | } |
|
| 321 | 399 | ||
| 322 | 400 | /// Finds the next pattern match in this set or its included sets. |
|
| 323 | 401 | pub(super) fn find_next( |
|
| 324 | 402 | self, |
|
| 325 | - | sets: &[PatternSet], |
|
| 326 | - | line: &str, |
|
| 403 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 327 | 404 | pos: usize, |
|
| 328 | - | limit: usize, |
|
| 329 | - | cache: &mut EndRegexCache, |
|
| 330 | - | anchors: AnchorState, |
|
| 331 | 405 | ) -> Option<Match> { |
|
| 332 | 406 | let mut active = Vec::new(); |
|
| 333 | - | self.find_next_inner(sets, line, pos, limit, cache, anchors, &mut active) |
|
| 407 | + | self.find_next_inner(search, pos, &mut active) |
|
| 334 | 408 | } |
|
| 335 | 409 | ||
| 336 | 410 | /// Finds the next pattern match while carrying the active include stack. |
|
| 337 | 411 | fn find_next_inner( |
|
| 338 | 412 | self, |
|
| 339 | - | sets: &[PatternSet], |
|
| 340 | - | line: &str, |
|
| 413 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 341 | 414 | pos: usize, |
|
| 342 | - | limit: usize, |
|
| 343 | - | cache: &mut EndRegexCache, |
|
| 344 | - | anchors: AnchorState, |
|
| 345 | 415 | active: &mut Vec<PatternSetId>, |
|
| 346 | 416 | ) -> Option<Match> { |
|
| 347 | 417 | if active.contains(&self) || active.len() > MAX_INCLUDE_DEPTH { |
|
| 348 | 418 | return None; |
|
| 349 | 419 | } |
|
| 350 | 420 | active.push(self); |
|
| 351 | - | let found = sets |
|
| 421 | + | let found = search |
|
| 422 | + | .sets |
|
| 352 | 423 | .get(self.index()) |
|
| 353 | - | .and_then(|set| set.find_next_inner(sets, line, pos, limit, cache, anchors, active)); |
|
| 424 | + | .and_then(|set| set.find_next_inner(search, pos, active)); |
|
| 354 | 425 | active.pop(); |
|
| 355 | 426 | found |
|
| 356 | 427 | } |
|
| 357 | 428 | } |
|
| 358 | 429 |
| 381 | 452 | } |
|
| 382 | 453 | ||
| 383 | 454 | /// Finds the next pattern match at or after `pos`. |
|
| 384 | 455 | fn find_next_inner( |
|
| 385 | 456 | &self, |
|
| 386 | - | sets: &[PatternSet], |
|
| 387 | - | line: &str, |
|
| 457 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 388 | 458 | pos: usize, |
|
| 389 | - | limit: usize, |
|
| 390 | - | cache: &mut EndRegexCache, |
|
| 391 | - | anchors: AnchorState, |
|
| 392 | 459 | active: &mut Vec<PatternSetId>, |
|
| 393 | 460 | ) -> Option<Match> { |
|
| 394 | - | if pos > limit || limit > line.len() || !line.is_char_boundary(pos) { |
|
| 461 | + | if pos > search.limit |
|
| 462 | + | || search.limit > search.line.len() |
|
| 463 | + | || !search.line.is_char_boundary(pos) |
|
| 464 | + | { |
|
| 395 | 465 | return None; |
|
| 396 | 466 | } |
|
| 397 | - | let mut found = self.find_next_local(sets, line, pos, limit, cache, anchors); |
|
| 467 | + | let mut found = self.find_next_local(search, pos); |
|
| 398 | 468 | for include in &self.includes { |
|
| 399 | - | let Some(candidate) = |
|
| 400 | - | include.find_next_inner(sets, line, pos, limit, cache, anchors, active) |
|
| 401 | - | else { |
|
| 469 | + | let Some(candidate) = include.find_next_inner(search, pos, active) else { |
|
| 402 | 470 | continue; |
|
| 403 | 471 | }; |
|
| 404 | 472 | found = match found { |
|
| 405 | 473 | Some(current) if current.start <= candidate.start => Some(current), |
|
| 406 | 474 | _ => Some(candidate), |
| 408 | 476 | } |
|
| 409 | 477 | found |
|
| 410 | 478 | } |
|
| 411 | 479 | ||
| 412 | 480 | /// Finds the next local pattern match at or after `pos`. |
|
| 413 | - | fn find_next_local( |
|
| 414 | - | &self, |
|
| 415 | - | sets: &[PatternSet], |
|
| 416 | - | line: &str, |
|
| 417 | - | pos: usize, |
|
| 418 | - | limit: usize, |
|
| 419 | - | cache: &mut EndRegexCache, |
|
| 420 | - | anchors: AnchorState, |
|
| 421 | - | ) -> Option<Match> { |
|
| 481 | + | fn find_next_local(&self, search: &mut PatternSearch<'_, '_, '_>, pos: usize) -> Option<Match> { |
|
| 422 | 482 | let regexes = self.regexes.as_ref()?; |
|
| 423 | 483 | let (entry, captures) = regexes.captures_with_options( |
|
| 424 | - | line, |
|
| 484 | + | search.line, |
|
| 425 | 485 | pos, |
|
| 426 | - | limit, |
|
| 486 | + | search.limit, |
|
| 427 | 487 | RegSetLead::Position, |
|
| 428 | - | anchors.search_options(pos), |
|
| 488 | + | search.anchors.search_options(pos), |
|
| 429 | 489 | )?; |
|
| 430 | 490 | let pattern = self.patterns.get(entry)?; |
|
| 431 | 491 | let regex_match = RegexMatch::from_captures(captures, pattern.needs_match_captures())?; |
|
| 432 | 492 | ||
| 433 | - | pattern.match_result(regex_match, sets, line, cache) |
|
| 493 | + | pattern.match_result(regex_match, search) |
|
| 434 | 494 | } |
|
| 435 | 495 | } |
|
| 436 | 496 | ||
| 437 | 497 | impl PatternCompiler<'_> { |
|
| 438 | 498 | /// Creates a compiler for a raw grammar. |
| 546 | 606 | _ => { |
|
| 547 | 607 | self.inline_patterns(pattern, compiled, includes)?; |
|
| 548 | 608 | return Ok(()); |
|
| 549 | 609 | } |
|
| 550 | 610 | }; |
|
| 611 | + | // Nested children are compiled โ and assigned their rule ids โ before |
|
| 612 | + | // this pattern, so every parent has a higher rule id than its children. |
|
| 551 | 613 | let nested = self.compile_nested_set(&pattern.patterns)?; |
|
| 552 | 614 | let id = self.next_rule; |
|
| 553 | 615 | self.next_rule += 1; |
|
| 554 | 616 | ||
| 555 | - | let matched = self.captures(pattern.captures.as_ref())?; |
|
| 556 | - | let begin = self.captures( |
|
| 557 | - | pattern |
|
| 558 | - | .begin_captures |
|
| 559 | - | .as_ref() |
|
| 560 | - | .or(pattern.captures.as_ref()), |
|
| 561 | - | )?; |
|
| 562 | - | let end = self.captures(pattern.end_captures.as_ref().or(pattern.captures.as_ref()))?; |
|
| 563 | - | let while_rule = self.captures( |
|
| 564 | - | pattern |
|
| 565 | - | .while_captures |
|
| 566 | - | .as_ref() |
|
| 567 | - | .or(pattern.captures.as_ref()), |
|
| 568 | - | )?; |
|
| 617 | + | // The slot-specific capture maps fall back to the shared `captures` map. |
|
| 618 | + | let shared = pattern.captures.as_ref(); |
|
| 619 | + | let matched = self.captures(shared)?; |
|
| 620 | + | let begin = self.captures(pattern.begin_captures.as_ref().or(shared))?; |
|
| 621 | + | let end = self.captures(pattern.end_captures.as_ref().or(shared))?; |
|
| 622 | + | let while_rule = self.captures(pattern.while_captures.as_ref().or(shared))?; |
|
| 569 | 623 | ||
| 570 | 624 | compiled.push(Pattern { |
|
| 571 | 625 | id, |
|
| 572 | 626 | scope, |
|
| 573 | 627 | content_scope: Scope::from_raw(pattern.content_name.as_deref(), &mut self.interner), |
| 649 | 703 | let mut captures = Captures::new(); |
|
| 650 | 704 | let Some(raw) = raw else { |
|
| 651 | 705 | return Ok(captures); |
|
| 652 | 706 | }; |
|
| 653 | 707 | for (index, capture) in raw { |
|
| 654 | - | let Some(scope_index) = index.parse().ok() else { |
|
| 708 | + | let Ok(scope_index) = index.parse() else { |
|
| 655 | 709 | continue; |
|
| 656 | 710 | }; |
|
| 657 | 711 | let nested = if capture.patterns.is_empty() { |
|
| 658 | 712 | None |
|
| 659 | 713 | } else { |
| 688 | 742 | locations |
|
| 689 | 743 | } |
|
| 690 | 744 | } |
|
| 691 | 745 | ||
| 692 | 746 | impl Pattern { |
|
| 747 | + | /// Pushes this pattern's name and content scopes over an inner-region range. |
|
| 748 | + | pub(super) fn push_content_visible( |
|
| 749 | + | &self, |
|
| 750 | + | spans: &mut Vec<ScopeSpan>, |
|
| 751 | + | start: usize, |
|
| 752 | + | end: usize, |
|
| 753 | + | ) { |
|
| 754 | + | Scope::push_content_visible(&self.scope, &self.content_scope, spans, start, end); |
|
| 755 | + | } |
|
| 756 | + | ||
| 693 | 757 | /// Returns the active end regex when reopening this pattern from line state. |
|
| 694 | 758 | pub(super) fn resume_end<'pat>( |
|
| 695 | 759 | &'pat self, |
|
| 696 | 760 | dynamic_end: Option<&str>, |
|
| 697 | 761 | cache: &mut EndRegexCache, |
| 712 | 776 | line, |
|
| 713 | 777 | 0, |
|
| 714 | 778 | !self.captures.while_rule.is_empty(), |
|
| 715 | 779 | anchors, |
|
| 716 | 780 | )?; |
|
| 781 | + | // The while pattern must anchor at column 0; a search from offset 0 can |
|
| 782 | + | // still return a later match, which does not count as a continuation. |
|
| 717 | 783 | (matched.start == 0).then_some(matched) |
|
| 718 | 784 | } |
|
| 719 | 785 | ||
| 720 | 786 | /// Returns true when nested patterns should win same-position end matches. |
|
| 721 | 787 | pub(super) fn apply_end_pattern_last(&self) -> bool { |
| 756 | 822 | pub(super) fn append_match_spans( |
|
| 757 | 823 | &self, |
|
| 758 | 824 | matched: &RegexMatch, |
|
| 759 | 825 | captures: &Captures, |
|
| 760 | 826 | spans: &mut Vec<ScopeSpan>, |
|
| 761 | - | sets: &[PatternSet], |
|
| 762 | - | line: &str, |
|
| 763 | - | cache: &mut EndRegexCache, |
|
| 764 | - | anchors: AnchorState, |
|
| 827 | + | context: &mut SpanContext<'_, '_, '_>, |
|
| 765 | 828 | ) { |
|
| 766 | 829 | self.scope.push_visible(spans, matched.start, matched.end); |
|
| 767 | - | self.append_capture_spans(matched, captures, spans, sets, line, cache, anchors); |
|
| 830 | + | self.append_capture_spans(matched, captures, spans, context); |
|
| 768 | 831 | spans.sort_by_key(|span| (span.start, span.end)); |
|
| 769 | 832 | } |
|
| 770 | 833 | ||
| 771 | 834 | /// Adds capture-group spans for one regex match. |
|
| 772 | 835 | pub(super) fn append_capture_spans( |
|
| 773 | 836 | &self, |
|
| 774 | 837 | matched: &RegexMatch, |
|
| 775 | 838 | captures: &Captures, |
|
| 776 | 839 | spans: &mut Vec<ScopeSpan>, |
|
| 777 | - | sets: &[PatternSet], |
|
| 778 | - | line: &str, |
|
| 779 | - | cache: &mut EndRegexCache, |
|
| 780 | - | anchors: AnchorState, |
|
| 840 | + | context: &mut SpanContext<'_, '_, '_>, |
|
| 781 | 841 | ) { |
|
| 782 | 842 | for (index, capture) in captures { |
|
| 783 | 843 | if let Some(Some((start, end))) = matched.captures.get(*index) |
|
| 784 | 844 | && start < end |
|
| 785 | 845 | { |
|
| 786 | - | capture.push_spans(spans, sets, line, *start, *end, cache, anchors); |
|
| 846 | + | capture.push_spans(spans, *start, *end, context); |
|
| 787 | 847 | } |
|
| 788 | 848 | } |
|
| 789 | 849 | } |
|
| 790 | 850 | ||
| 791 | 851 | /// Whether match-time regex captures must be retained. |
| 812 | 872 | ||
| 813 | 873 | /// Builds a [`Match`] for one regex hit, dispatching by body kind. |
|
| 814 | 874 | fn match_result( |
|
| 815 | 875 | &self, |
|
| 816 | 876 | regex_match: RegexMatch, |
|
| 817 | - | sets: &[PatternSet], |
|
| 818 | - | line: &str, |
|
| 819 | - | cache: &mut EndRegexCache, |
|
| 877 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 820 | 878 | ) -> Option<Match> { |
|
| 821 | 879 | match &self.body { |
|
| 822 | - | PatternBody::Match { .. } => Some(self.simple_match(regex_match, sets, line, cache)), |
|
| 823 | - | PatternBody::BeginEnd { end, .. } => { |
|
| 824 | - | self.begin_match(end, regex_match, sets, line, cache) |
|
| 825 | - | } |
|
| 826 | - | PatternBody::BeginWhile { .. } => { |
|
| 827 | - | Some(self.begin_while_match(regex_match, sets, line, cache)) |
|
| 828 | - | } |
|
| 880 | + | PatternBody::Match { .. } => Some(self.simple_match(regex_match, search)), |
|
| 881 | + | PatternBody::BeginEnd { end, .. } => self.begin_match(end, regex_match, search), |
|
| 882 | + | PatternBody::BeginWhile { .. } => Some(self.begin_while_match(regex_match, search)), |
|
| 829 | 883 | } |
|
| 830 | 884 | } |
|
| 831 | 885 | ||
| 832 | 886 | /// Builds a match for a single-line match rule. |
|
| 833 | 887 | fn simple_match( |
|
| 834 | 888 | &self, |
|
| 835 | 889 | regex_match: RegexMatch, |
|
| 836 | - | sets: &[PatternSet], |
|
| 837 | - | line: &str, |
|
| 838 | - | cache: &mut EndRegexCache, |
|
| 890 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 839 | 891 | ) -> Match { |
|
| 840 | 892 | let mut spans = Vec::new(); |
|
| 893 | + | let mut context = search.span_context(); |
|
| 841 | 894 | self.append_match_spans( |
|
| 842 | 895 | ®ex_match, |
|
| 843 | 896 | &self.captures.matched, |
|
| 844 | 897 | &mut spans, |
|
| 845 | - | sets, |
|
| 846 | - | line, |
|
| 847 | - | cache, |
|
| 848 | - | AnchorState::new(false), |
|
| 898 | + | &mut context, |
|
| 849 | 899 | ); |
|
| 850 | 900 | Match { |
|
| 851 | 901 | start: regex_match.start, |
|
| 852 | 902 | end: regex_match.end, |
|
| 853 | 903 | spans, |
| 858 | 908 | /// Builds a match for a begin/end rule, eagerly closing on the same line if possible. |
|
| 859 | 909 | fn begin_match( |
|
| 860 | 910 | &self, |
|
| 861 | 911 | end_pattern: &EndPattern, |
|
| 862 | 912 | begin: RegexMatch, |
|
| 863 | - | sets: &[PatternSet], |
|
| 864 | - | line: &str, |
|
| 865 | - | cache: &mut EndRegexCache, |
|
| 913 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 866 | 914 | ) -> Option<Match> { |
|
| 867 | - | let end_regex = end_pattern.resolve_for_begin(&begin, line, cache)?; |
|
| 915 | + | let end_regex = end_pattern.resolve_for_begin(&begin, search.line, search.cache)?; |
|
| 868 | 916 | let dynamic_end = end_regex.dynamic_source(); |
|
| 869 | 917 | let mut spans = Vec::new(); |
|
| 870 | - | self.append_match_spans( |
|
| 871 | - | &begin, |
|
| 872 | - | &self.captures.begin, |
|
| 873 | - | &mut spans, |
|
| 874 | - | sets, |
|
| 875 | - | line, |
|
| 876 | - | cache, |
|
| 877 | - | AnchorState::new(false), |
|
| 878 | - | ); |
|
| 918 | + | let mut context = search.span_context(); |
|
| 919 | + | self.append_match_spans(&begin, &self.captures.begin, &mut spans, &mut context); |
|
| 879 | 920 | ||
| 880 | 921 | Some(Match { |
|
| 881 | 922 | start: begin.start, |
|
| 882 | 923 | end: begin.end, |
|
| 883 | 924 | spans, |
| 891 | 932 | ||
| 892 | 933 | /// Builds a match for a begin/while rule. |
|
| 893 | 934 | fn begin_while_match( |
|
| 894 | 935 | &self, |
|
| 895 | 936 | begin: RegexMatch, |
|
| 896 | - | sets: &[PatternSet], |
|
| 897 | - | line: &str, |
|
| 898 | - | cache: &mut EndRegexCache, |
|
| 937 | + | search: &mut PatternSearch<'_, '_, '_>, |
|
| 899 | 938 | ) -> Match { |
|
| 900 | 939 | let mut spans = Vec::new(); |
|
| 901 | - | let end = begin.next_pos(line); |
|
| 940 | + | let end = begin.next_pos(search.line); |
|
| 902 | 941 | ||
| 903 | - | self.append_match_spans( |
|
| 904 | - | &begin, |
|
| 905 | - | &self.captures.begin, |
|
| 906 | - | &mut spans, |
|
| 907 | - | sets, |
|
| 908 | - | line, |
|
| 909 | - | cache, |
|
| 910 | - | AnchorState::new(false), |
|
| 911 | - | ); |
|
| 942 | + | let mut context = search.span_context(); |
|
| 943 | + | self.append_match_spans(&begin, &self.captures.begin, &mut spans, &mut context); |
|
| 912 | 944 | self.scope.push_visible(&mut spans, begin.start, end); |
|
| 913 | 945 | ||
| 914 | 946 | Match { |
|
| 915 | 947 | start: begin.start, |
|
| 916 | 948 | end, |
crates/highlight/src/grammar/tokenize.rs
+84 -92
| 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 | 7 | use super::end::{EndRegex, EndRegexCache}; |
|
| 8 | - | use super::pattern::{Match, OpenRule, Pattern, RegexMatch, Scope}; |
|
| 8 | + | use super::pattern::{Match, OpenRule, Pattern, PatternSearch, RegexMatch, SpanContext}; |
|
| 9 | 9 | use super::*; |
|
| 10 | 10 | ||
| 11 | 11 | /// Reusable line tokenizer for one compiled grammar. |
|
| 12 | 12 | /// |
|
| 13 | 13 | /// The tokenizer keeps runtime scratch that is not part of immutable grammar |
| 94 | 94 | line: &str, |
|
| 95 | 95 | pos: usize, |
|
| 96 | 96 | spans: &mut Vec<ScopeSpan>, |
|
| 97 | 97 | anchors: &mut AnchorState, |
|
| 98 | 98 | ) -> usize { |
|
| 99 | - | let Some(found) = self.grammar.patterns.find_next( |
|
| 99 | + | let mut search = PatternSearch::new( |
|
| 100 | 100 | &self.grammar.pattern_sets, |
|
| 101 | 101 | line, |
|
| 102 | - | pos, |
|
| 103 | 102 | line.len(), |
|
| 104 | 103 | &mut self.cache, |
|
| 105 | 104 | *anchors, |
|
| 106 | - | ) else { |
|
| 105 | + | ); |
|
| 106 | + | let Some(found) = self.grammar.patterns.find_next(&mut search, pos) else { |
|
| 107 | 107 | return line.len(); |
|
| 108 | 108 | }; |
|
| 109 | 109 | let next_pos = found.next_pos_after_open(line); |
|
| 110 | 110 | let opened = found.open_rule(); |
|
| 111 | 111 | let anchor = found.end(); |
| 134 | 134 | }; |
|
| 135 | 135 | let Some(end) = pattern.resume_end(open.dynamic_end.as_deref(), &mut self.cache) else { |
|
| 136 | 136 | return self.advance_inside_while_rule(state, line, pos, spans, pattern, anchors); |
|
| 137 | 137 | }; |
|
| 138 | 138 | let event = self.next_event_inside(pattern, &end, line, pos, *anchors); |
|
| 139 | - | let next_pos = match &event { |
|
| 140 | - | InsideEvent::Close(close) => close.end(), |
|
| 141 | - | InsideEvent::Nested(nested) => nested.next_pos_after_open(line), |
|
| 142 | - | InsideEvent::None => line.len(), |
|
| 143 | - | }; |
|
| 144 | - | for open in state.stack.iter().take(state.stack.len().saturating_sub(1)) { |
|
| 145 | - | if let Some(pattern) = self.grammar.pattern_by_rule(open.rule_id) { |
|
| 146 | - | Scope::push_content_visible( |
|
| 147 | - | &pattern.scope, |
|
| 148 | - | &pattern.content_scope, |
|
| 149 | - | spans, |
|
| 150 | - | pos, |
|
| 151 | - | next_pos, |
|
| 152 | - | ); |
|
| 139 | + | let next_pos = event.next_pos(line); |
|
| 140 | + | // Parent rules wrap the whole span consumed by this step; the current |
|
| 141 | + | // rule's own content scope is emitted per-event by `InsideEvent::apply`. |
|
| 142 | + | if let Some((_current, parents)) = state.stack.split_last() { |
|
| 143 | + | for open in parents { |
|
| 144 | + | if let Some(pattern) = self.grammar.pattern_by_rule(open.rule_id) { |
|
| 145 | + | pattern.push_content_visible(spans, pos, next_pos); |
|
| 146 | + | } |
|
| 153 | 147 | } |
|
| 154 | 148 | } |
|
| 155 | - | event.apply( |
|
| 149 | + | let mut context = EventContext { |
|
| 156 | 150 | state, |
|
| 157 | 151 | line, |
|
| 158 | - | pos, |
|
| 159 | 152 | spans, |
|
| 160 | 153 | pattern, |
|
| 161 | - | &self.grammar.pattern_sets, |
|
| 162 | - | &mut self.cache, |
|
| 154 | + | sets: &self.grammar.pattern_sets, |
|
| 155 | + | cache: &mut self.cache, |
|
| 163 | 156 | anchors, |
|
| 164 | - | ) |
|
| 157 | + | }; |
|
| 158 | + | event.apply(pos, next_pos, &mut context) |
|
| 165 | 159 | } |
|
| 166 | 160 | ||
| 167 | 161 | /// Checks active begin/while rules before scanning a continued line. |
|
| 168 | 162 | fn check_while_conditions( |
|
| 169 | 163 | &mut self, |
| 188 | 182 | break; |
|
| 189 | 183 | } |
|
| 190 | 184 | index += 1; |
|
| 191 | 185 | continue; |
|
| 192 | 186 | }; |
|
| 187 | + | let mut context = |
|
| 188 | + | SpanContext::new(&self.grammar.pattern_sets, line, &mut self.cache, *anchors); |
|
| 193 | 189 | pattern.append_capture_spans( |
|
| 194 | 190 | &matched, |
|
| 195 | 191 | &pattern.captures.while_rule, |
|
| 196 | 192 | spans, |
|
| 197 | - | &self.grammar.pattern_sets, |
|
| 198 | - | line, |
|
| 199 | - | &mut self.cache, |
|
| 200 | - | *anchors, |
|
| 193 | + | &mut context, |
|
| 201 | 194 | ); |
|
| 202 | 195 | anchors.set_begin_position(matched.end()); |
|
| 203 | 196 | pos = pos.max(matched.end()); |
|
| 204 | 197 | index += 1; |
|
| 205 | 198 | } |
| 215 | 208 | pos: usize, |
|
| 216 | 209 | spans: &mut Vec<ScopeSpan>, |
|
| 217 | 210 | pattern: &Pattern, |
|
| 218 | 211 | anchors: &mut AnchorState, |
|
| 219 | 212 | ) -> usize { |
|
| 220 | - | let nested = pattern.nested.find_next( |
|
| 213 | + | let mut search = PatternSearch::new( |
|
| 221 | 214 | &self.grammar.pattern_sets, |
|
| 222 | 215 | line, |
|
| 223 | - | pos, |
|
| 224 | 216 | line.len(), |
|
| 225 | 217 | &mut self.cache, |
|
| 226 | 218 | *anchors, |
|
| 227 | 219 | ); |
|
| 220 | + | let nested = pattern.nested.find_next(&mut search, pos); |
|
| 228 | 221 | let Some(nested) = nested else { |
|
| 229 | - | Scope::push_content_visible( |
|
| 230 | - | &pattern.scope, |
|
| 231 | - | &pattern.content_scope, |
|
| 232 | - | spans, |
|
| 233 | - | pos, |
|
| 234 | - | line.len(), |
|
| 235 | - | ); |
|
| 222 | + | pattern.push_content_visible(spans, pos, line.len()); |
|
| 236 | 223 | return line.len(); |
|
| 237 | 224 | }; |
|
| 238 | 225 | let next_pos = nested.next_pos_after_open(line); |
|
| 239 | 226 | let opened = nested.open_rule(); |
|
| 240 | 227 | let anchor = nested.end(); |
|
| 241 | 228 | ||
| 242 | - | Scope::push_content_visible(&pattern.scope, &pattern.content_scope, spans, pos, next_pos); |
|
| 229 | + | pattern.push_content_visible(spans, pos, next_pos); |
|
| 243 | 230 | spans.extend(nested.spans); |
|
| 244 | 231 | ||
| 245 | 232 | if let Some(opened) = opened { |
|
| 246 | 233 | state.stack.push(opened); |
|
| 247 | 234 | anchors.set_begin_position(anchor); |
| 273 | 260 | end: &EndRegex<'_>, |
|
| 274 | 261 | line: &str, |
|
| 275 | 262 | pos: usize, |
|
| 276 | 263 | anchors: AnchorState, |
|
| 277 | 264 | ) -> InsideEvent { |
|
| 278 | - | let nested = pattern.nested.find_next( |
|
| 265 | + | let mut search = PatternSearch::new( |
|
| 279 | 266 | &self.grammar.pattern_sets, |
|
| 280 | 267 | line, |
|
| 281 | - | pos, |
|
| 282 | 268 | line.len(), |
|
| 283 | 269 | &mut self.cache, |
|
| 284 | 270 | anchors, |
|
| 285 | 271 | ); |
|
| 272 | + | let nested = pattern.nested.find_next(&mut search, pos); |
|
| 286 | 273 | let close = pattern.find_end_match(end, line, pos, anchors); |
|
| 287 | 274 | ||
| 288 | 275 | match (nested, close) { |
|
| 289 | 276 | (Some(nested), Some(close)) |
|
| 290 | 277 | if nested.start < close.start |
| 305 | 292 | } |
|
| 306 | 293 | } |
|
| 307 | 294 | ||
| 308 | 295 | /// Trims token spans back to the caller-visible line range. |
|
| 309 | 296 | fn clamp_spans(spans: &mut Vec<ScopeSpan>, visible_len: usize) { |
|
| 310 | - | for span in spans.iter_mut() { |
|
| 297 | + | spans.retain_mut(|span| { |
|
| 311 | 298 | span.end = span.end.min(visible_len); |
|
| 312 | - | } |
|
| 313 | - | spans.retain(|span| span.start < span.end); |
|
| 299 | + | span.start < span.end |
|
| 300 | + | }); |
|
| 314 | 301 | } |
|
| 315 | 302 | ||
| 316 | 303 | /// One event found inside an open begin/end rule. |
|
| 317 | 304 | enum InsideEvent { |
|
| 318 | 305 | /// Nested pattern matched before the end pattern. |
| 322 | 309 | /// Neither nested nor end matched on the rest of this line. |
|
| 323 | 310 | None, |
|
| 324 | 311 | } |
|
| 325 | 312 | ||
| 326 | 313 | impl InsideEvent { |
|
| 327 | - | /// Emits the event's spans, updates the parser stack, and returns the next byte position. |
|
| 328 | - | fn apply( |
|
| 329 | - | self, |
|
| 330 | - | state: &mut LineState, |
|
| 331 | - | line: &str, |
|
| 332 | - | pos: usize, |
|
| 333 | - | spans: &mut Vec<ScopeSpan>, |
|
| 334 | - | pattern: &Pattern, |
|
| 335 | - | sets: &[PatternSet], |
|
| 336 | - | cache: &mut EndRegexCache, |
|
| 337 | - | anchors: &mut AnchorState, |
|
| 338 | - | ) -> usize { |
|
| 314 | + | /// Returns the byte position this event advances to. |
|
| 315 | + | fn next_pos(&self, line: &str) -> usize { |
|
| 316 | + | match self { |
|
| 317 | + | Self::Close(close) => close.end(), |
|
| 318 | + | Self::Nested(nested) => nested.next_pos_after_open(line), |
|
| 319 | + | Self::None => line.len(), |
|
| 320 | + | } |
|
| 321 | + | } |
|
| 322 | + | ||
| 323 | + | /// Emits the event's spans and updates the parser stack. |
|
| 324 | + | /// |
|
| 325 | + | /// `next_pos` is the position the event advances to, as returned by |
|
| 326 | + | /// [`InsideEvent::next_pos`]; it is also this function's return value. |
|
| 327 | + | fn apply(self, pos: usize, next_pos: usize, context: &mut EventContext<'_>) -> usize { |
|
| 339 | 328 | match self { |
|
| 340 | 329 | Self::Close(close) => { |
|
| 341 | - | Scope::push_content_visible( |
|
| 342 | - | &pattern.scope, |
|
| 343 | - | &pattern.content_scope, |
|
| 344 | - | spans, |
|
| 345 | - | pos, |
|
| 346 | - | close.start, |
|
| 347 | - | ); |
|
| 348 | - | pattern.append_match_spans( |
|
| 330 | + | context |
|
| 331 | + | .pattern |
|
| 332 | + | .push_content_visible(context.spans, pos, close.start); |
|
| 333 | + | let mut span_context = |
|
| 334 | + | SpanContext::new(context.sets, context.line, context.cache, *context.anchors); |
|
| 335 | + | context.pattern.append_match_spans( |
|
| 349 | 336 | &close, |
|
| 350 | - | &pattern.captures.end, |
|
| 351 | - | spans, |
|
| 352 | - | sets, |
|
| 353 | - | line, |
|
| 354 | - | cache, |
|
| 355 | - | *anchors, |
|
| 337 | + | &context.pattern.captures.end, |
|
| 338 | + | context.spans, |
|
| 339 | + | &mut span_context, |
|
| 356 | 340 | ); |
|
| 357 | - | if let Some(closed) = state.stack.pop() { |
|
| 358 | - | anchors.restore_begin_position(closed.anchor_position); |
|
| 341 | + | if let Some(closed) = context.state.stack.pop() { |
|
| 342 | + | context |
|
| 343 | + | .anchors |
|
| 344 | + | .restore_begin_position(closed.anchor_position); |
|
| 359 | 345 | } |
|
| 360 | - | close.end() |
|
| 361 | 346 | } |
|
| 362 | 347 | Self::Nested(nested) => { |
|
| 363 | - | let next_pos = nested.next_pos_after_open(line); |
|
| 364 | 348 | let anchor = nested.end(); |
|
| 365 | - | ||
| 366 | - | Scope::push_content_visible( |
|
| 367 | - | &pattern.scope, |
|
| 368 | - | &pattern.content_scope, |
|
| 369 | - | spans, |
|
| 370 | - | pos, |
|
| 371 | - | next_pos, |
|
| 372 | - | ); |
|
| 373 | 349 | let opened = nested.open_rule(); |
|
| 374 | 350 | ||
| 375 | - | spans.extend(nested.spans); |
|
| 351 | + | context |
|
| 352 | + | .pattern |
|
| 353 | + | .push_content_visible(context.spans, pos, next_pos); |
|
| 354 | + | context.spans.extend(nested.spans); |
|
| 376 | 355 | ||
| 377 | 356 | if let Some(opened) = opened { |
|
| 378 | - | state.stack.push(opened); |
|
| 379 | - | anchors.set_begin_position(anchor); |
|
| 357 | + | context.state.stack.push(opened); |
|
| 358 | + | context.anchors.set_begin_position(anchor); |
|
| 380 | 359 | } |
|
| 381 | - | next_pos |
|
| 382 | 360 | } |
|
| 383 | 361 | Self::None => { |
|
| 384 | - | Scope::push_content_visible( |
|
| 385 | - | &pattern.scope, |
|
| 386 | - | &pattern.content_scope, |
|
| 387 | - | spans, |
|
| 388 | - | pos, |
|
| 389 | - | line.len(), |
|
| 390 | - | ); |
|
| 391 | - | line.len() |
|
| 362 | + | context |
|
| 363 | + | .pattern |
|
| 364 | + | .push_content_visible(context.spans, pos, next_pos); |
|
| 392 | 365 | } |
|
| 393 | 366 | } |
|
| 367 | + | next_pos |
|
| 394 | 368 | } |
|
| 395 | 369 | } |
|
| 370 | + | ||
| 371 | + | /// Mutable context used while applying an inside-rule event. |
|
| 372 | + | struct EventContext<'a> { |
|
| 373 | + | /// Parser state being updated. |
|
| 374 | + | state: &'a mut LineState, |
|
| 375 | + | /// Line being tokenized. |
|
| 376 | + | line: &'a str, |
|
| 377 | + | /// Scope spans emitted for the current line. |
|
| 378 | + | spans: &'a mut Vec<ScopeSpan>, |
|
| 379 | + | /// Pattern whose body is currently active. |
|
| 380 | + | pattern: &'a Pattern, |
|
| 381 | + | /// Pattern sets in the compiled grammar. |
|
| 382 | + | sets: &'a [PatternSet], |
|
| 383 | + | /// Cache for dynamically resolved end regexes. |
|
| 384 | + | cache: &'a mut EndRegexCache, |
|
| 385 | + | /// Anchor availability for subsequent matches. |
|
| 386 | + | anchors: &'a mut AnchorState, |
|
| 387 | + | } |