span: add empty_{before,after}, after, between

This commit is contained in:
Noah Hellman 2023-01-27 17:50:50 +01:00
parent f7ead5a69f
commit 8237384077
2 changed files with 32 additions and 17 deletions

View file

@ -103,7 +103,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
fn reset_span(&mut self) { fn reset_span(&mut self) {
self.span = Span::empty_at(self.span.end()); self.span = self.span.empty_after();
} }
fn parse_event(&mut self) -> Option<Event> { fn parse_event(&mut self) -> Option<Event> {
@ -164,7 +164,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
span: self.span, span: self.span,
}); });
let mut span_inner = Span::empty_at(self.span.end()); let mut span_inner = self.span.empty_after();
let mut span_outer = None; let mut span_outer = None;
let mut non_whitespace_first = None; let mut non_whitespace_first = None;
@ -203,7 +203,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
}) })
); );
self.lexer = lex::Lexer::new(ahead); self.lexer = lex::Lexer::new(ahead);
let span_format = Span::by_len(self.span.end(), len); let span_format = self.span.after(len);
kind = RawFormat; kind = RawFormat;
self.events[opener_event].kind = EventKind::Enter(kind); self.events[opener_event].kind = EventKind::Enter(kind);
self.events[opener_event].span = span_format; self.events[opener_event].span = span_format;
@ -269,10 +269,9 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
.iter() .iter()
.rposition(|e| e.kind != EventKind::Str) .rposition(|e| e.kind != EventKind::Str)
.map_or(0, |i| i + 1); .map_or(0, |i| i + 1);
let span_str = Span::new( let span_str = self.events[i]
self.events[i].span.start(), .span
self.events[self.events.len() - 1].span.end(), .union(self.events[self.events.len() - 1].span);
);
self.events.drain(i..); self.events.drain(i..);
self.events.push_back(Event { self.events.push_back(Event {
@ -281,7 +280,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
}); });
self.events.push_back(Event { self.events.push_back(Event {
kind: EventKind::Enter(Container::Span), kind: EventKind::Enter(Container::Span),
span: Span::empty_at(span_str.start()), span: span_str.empty_before(),
}); });
self.events.push_back(Event { self.events.push_back(Event {
kind: EventKind::Str, kind: EventKind::Str,
@ -290,12 +289,12 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
Event { Event {
kind: EventKind::Exit(Container::Span), kind: EventKind::Exit(Container::Span),
span: Span::empty_at(span_str.end()), span: span_str.empty_after(),
} }
} else { } else {
Event { Event {
kind: EventKind::Placeholder, kind: EventKind::Placeholder,
span: Span::empty_at(self.span.start()), span: self.span.empty_before(),
} }
}) })
} else { } else {
@ -328,12 +327,12 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
kind: EventKind::Enter(Autolink), kind: EventKind::Enter(Autolink),
span: self.span, span: self.span,
}); });
self.span = Span::by_len(self.span.end(), len); self.span = self.span.after(len);
self.events.push_back(Event { self.events.push_back(Event {
kind: EventKind::Str, kind: EventKind::Str,
span: self.span, span: self.span,
}); });
self.span = Span::by_len(self.span.end(), 1); self.span = self.span.after(1);
Event { Event {
kind: EventKind::Exit(Autolink), kind: EventKind::Exit(Autolink),
span: self.span, span: self.span,
@ -377,12 +376,12 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
.count(); .count();
end.then(|| { end.then(|| {
self.lexer = lex::Lexer::new(ahead); self.lexer = lex::Lexer::new(ahead);
self.span = Span::by_len(self.span.end(), len); self.span = self.span.after(len);
let ev = Event { let ev = Event {
kind: EventKind::Atom(FootnoteReference), kind: EventKind::Atom(FootnoteReference),
span: self.span, span: self.span,
}; };
self.span = Span::by_len(self.span.end(), 1); self.span = self.span.after(1);
ev ev
}) })
} else { } else {
@ -417,7 +416,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
return None; return None;
} }
let inner_span = Span::new(self.events[e_opener].span.end(), self.span.start()); let inner_span = self.events[e_opener].span.between(self.span);
let mut event_closer = match Container::try_from(d) { let mut event_closer = match Container::try_from(d) {
Ok(cont) => { Ok(cont) => {
self.events[e_opener].kind = EventKind::Enter(cont); self.events[e_opener].kind = EventKind::Enter(cont);
@ -448,7 +447,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
let (mut attr_len, mut has_attr) = attr::valid(&mut ahead); let (mut attr_len, mut has_attr) = attr::valid(&mut ahead);
if attr_len > 0 { if attr_len > 0 {
let span_closer = self.span; let span_closer = self.span;
self.span = Span::empty_at(self.span.end()); self.span = self.span.empty_after();
while attr_len > 0 { while attr_len > 0 {
self.span = self.span.extend(attr_len); self.span = self.span.extend(attr_len);
self.lexer = lex::Lexer::new(ahead.clone()); self.lexer = lex::Lexer::new(ahead.clone());
@ -530,7 +529,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
}) })
.count(); .count();
end.then(|| { end.then(|| {
let span = Span::by_len(self.span.end() + 1, len); let span = self.span.after(len).translate(1);
(kind, span) (kind, span)
}) })
} }

View file

@ -22,6 +22,14 @@ impl Span {
Self::by_len(start, 0) Self::by_len(start, 0)
} }
pub fn empty_before(self) -> Self {
Self::empty_at(self.start())
}
pub fn empty_after(self) -> Self {
Self::empty_at(self.end())
}
pub fn with_start(self, start: usize) -> Self { pub fn with_start(self, start: usize) -> Self {
Self::new(start, self.end()) Self::new(start, self.end())
} }
@ -34,10 +42,18 @@ impl Span {
Self::by_len(self.start(), len) Self::by_len(self.start(), len)
} }
pub fn after(self, len: usize) -> Self {
Self::by_len(self.end(), len)
}
pub fn union(self, span: Self) -> Self { pub fn union(self, span: Self) -> Self {
Self::new(self.start(), span.end()) Self::new(self.start(), span.end())
} }
pub fn between(self, span: Self) -> Self {
Self::new(self.end(), span.start())
}
pub fn skip(self, n: usize) -> Self { pub fn skip(self, n: usize) -> Self {
Self::new(self.start() + n, self.end()) Self::new(self.start() + n, self.end())
} }