inline: extract ahead_container_attributes

This commit is contained in:
Noah Hellman 2023-02-21 17:36:44 +01:00
parent 1eb3bd12d0
commit 418bb38f82

View file

@ -493,41 +493,25 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
} }
let mut ahead = self.lexer.chars(); if let Some((non_empty, span)) = self.ahead_attributes() {
let (mut attr_len, mut has_attr) = attr::valid(&mut ahead); if non_empty {
if attr_len > 0 {
let span_closer = self.span;
self.span = self.span.empty_after();
while attr_len > 0 {
self.span = self.span.extend(attr_len);
self.lexer = lex::Lexer::new(ahead.clone());
let (l, non_empty) = attr::valid(&mut ahead);
has_attr |= non_empty;
attr_len = l;
}
if has_attr {
self.events[e_attr] = Event { self.events[e_attr] = Event {
kind: EventKind::Attributes, kind: EventKind::Attributes,
span: self.span, span,
}; };
} }
if event_closer.is_none() { if event_closer.is_none() {
if has_attr { self.events[e_opener].kind = EventKind::Enter(Container::Span);
self.events[e_opener].kind = EventKind::Enter(Container::Span);
}
event_closer = Some(Event { event_closer = Some(Event {
kind: if has_attr { kind: EventKind::Exit(Container::Span),
EventKind::Exit(Container::Span) span: self.span,
} else {
EventKind::Str
},
span: span_closer,
}); });
} }
self.span = span;
} }
event_closer event_closer
}) })
.or_else(|| { .or_else(|| {
@ -574,6 +558,25 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
}) })
} }
fn ahead_attributes(&mut self) -> Option<(bool, Span)> {
let mut span = self.span.empty_after();
let mut ahead = self.lexer.chars();
let (mut attr_len, mut has_attr) = attr::valid(&mut ahead);
if attr_len > 0 {
while attr_len > 0 {
span = span.extend(attr_len);
self.lexer = lex::Lexer::new(ahead.clone());
let (l, non_empty) = attr::valid(&mut ahead);
has_attr |= non_empty;
attr_len = l;
}
Some((has_attr, span))
} else {
None
}
}
fn post_span(&mut self, ty: SpanType, opener_event: usize) -> Option<Event> { fn post_span(&mut self, ty: SpanType, opener_event: usize) -> Option<Event> {
let mut ahead = self.lexer.chars(); let mut ahead = self.lexer.chars();
match ahead.next() { match ahead.next() {