implement TokenTypes::Captures

This commit is contained in:
liquidex 2024-03-11 23:43:39 +01:00
parent 5ab8ffdba2
commit ed893ccd0f
2 changed files with 36 additions and 2 deletions

View file

@ -24,7 +24,38 @@ impl CompiledSyntax {
break;
}
}
CompiledTokenTypes::Captures(types) => { /* TODO */ }
CompiledTokenTypes::Captures(types) => {
if let Some(captures) = pattern.regex.captures(&text[i..]) {
let whole_match = captures.get(0).unwrap();
let mut last_match_end = 0;
for (index, capture) in captures
.iter()
.skip(1)
.enumerate()
.filter_map(|(i, m)| m.map(|m| (i, m)))
{
let id = types
.captures
.get(index)
.copied()
.unwrap_or(TOKEN_ID_DEFAULT);
push_token(
&mut tokens,
types.default,
i + last_match_end..i + capture.range().start,
);
push_token(
&mut tokens,
id,
i + capture.range().start..i + capture.range().end,
);
last_match_end = capture.range().end;
}
i += whole_match.range().end;
had_match = true;
break;
}
}
}
}
@ -47,6 +78,10 @@ impl CompiledSyntax {
}
fn push_token(tokens: &mut Vec<Token>, id: TokenId, range: Range<usize>) {
if range.is_empty() {
return;
}
if let Some(previous_token) = tokens.last_mut() {
if previous_token.id == id {
previous_token.range.end = range.end;

View file

@ -23,7 +23,6 @@
//! HTML renderer that takes an iterator of events as input.
use std::borrow::Borrow;
use std::collections::HashMap;
use std::io;