inline: mv raw format spec from span to event field

This commit is contained in:
Noah Hellman 2023-04-24 19:35:33 +02:00
parent 8be7c4c840
commit 657b47df12
2 changed files with 30 additions and 32 deletions

View file

@ -26,7 +26,7 @@ pub enum Atom {
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Container { pub enum Container<'s> {
Span, Span,
Subscript, Subscript,
Superscript, Superscript,
@ -36,8 +36,9 @@ pub enum Container {
Strong, Strong,
Mark, Mark,
Verbatim, Verbatim,
/// Span is the format. RawFormat {
RawFormat, format: &'s str,
},
InlineMath, InlineMath,
DisplayMath, DisplayMath,
ReferenceLink(CowStrIndex), ReferenceLink(CowStrIndex),
@ -57,9 +58,9 @@ pub enum QuoteType {
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum EventKind { pub enum EventKind<'s> {
Enter(Container), Enter(Container<'s>),
Exit(Container), Exit(Container<'s>),
Atom(Atom), Atom(Atom),
Str, Str,
Attributes { Attributes {
@ -72,8 +73,8 @@ pub enum EventKind {
type AttributesIndex = u32; type AttributesIndex = u32;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Event { pub struct Event<'s> {
pub kind: EventKind, pub kind: EventKind<'s>,
pub span: Span, pub span: Span,
} }
@ -218,7 +219,7 @@ pub struct Parser<'s> {
openers: Vec<(Opener, usize)>, openers: Vec<(Opener, usize)>,
/// Buffer queue for next events. Events are buffered until no modifications due to future /// Buffer queue for next events. Events are buffered until no modifications due to future
/// characters are needed. /// characters are needed.
events: std::collections::VecDeque<Event>, events: std::collections::VecDeque<Event<'s>>,
/// State if inside a verbatim container. /// State if inside a verbatim container.
verbatim: Option<VerbatimState>, verbatim: Option<VerbatimState>,
/// State if currently parsing potential attributes. /// State if currently parsing potential attributes.
@ -268,12 +269,12 @@ impl<'s> Parser<'s> {
self.store_attributes.clear(); self.store_attributes.clear();
} }
fn push_sp(&mut self, kind: EventKind, span: Span) -> Option<ControlFlow> { fn push_sp(&mut self, kind: EventKind<'s>, span: Span) -> Option<ControlFlow> {
self.events.push_back(Event { kind, span }); self.events.push_back(Event { kind, span });
Some(Continue) Some(Continue)
} }
fn push(&mut self, kind: EventKind) -> Option<ControlFlow> { fn push(&mut self, kind: EventKind<'s>) -> Option<ControlFlow> {
self.push_sp(kind, self.input.span) self.push_sp(kind, self.input.span)
} }
@ -310,17 +311,16 @@ impl<'s> Parser<'s> {
&& matches!(first.kind, lex::Kind::Seq(Sequence::Backtick)) && matches!(first.kind, lex::Kind::Seq(Sequence::Backtick))
{ {
let raw_format = self.input.ahead_raw_format(); let raw_format = self.input.ahead_raw_format();
let mut span_closer = self.input.span;
if let Some(span_format) = raw_format { if let Some(span_format) = raw_format {
self.events[event_opener].kind = EventKind::Enter(RawFormat); self.events[event_opener].kind = EventKind::Enter(RawFormat {
self.events[event_opener].span = span_format; format: span_format.of(self.input.src),
self.input.span = span_format.translate(1); });
span_closer = span_format; self.input.span = Span::new(self.input.span.start(), span_format.end() + 1);
}; };
let ty_opener = if let EventKind::Enter(ty) = self.events[event_opener].kind { let ty_opener = if let EventKind::Enter(ty) = self.events[event_opener].kind {
debug_assert!(matches!( debug_assert!(matches!(
ty, ty,
Verbatim | RawFormat | InlineMath | DisplayMath Verbatim | RawFormat { .. } | InlineMath | DisplayMath
)); ));
ty ty
} else { } else {
@ -330,7 +330,7 @@ impl<'s> Parser<'s> {
{ {
self.events.drain(*event_skip..); self.events.drain(*event_skip..);
} }
self.push_sp(EventKind::Exit(ty_opener), span_closer); self.push(EventKind::Exit(ty_opener));
self.verbatim = None; self.verbatim = None;
if raw_format.is_none() if raw_format.is_none()
&& self.input.peek().map_or(false, |t| { && self.input.peek().map_or(false, |t| {
@ -925,7 +925,7 @@ impl<'s> Parser<'s> {
self.push(EventKind::Atom(atom)) self.push(EventKind::Atom(atom))
} }
fn merge_str_events(&mut self, span_str: Span) -> Event { fn merge_str_events(&mut self, span_str: Span) -> Event<'s> {
let mut span = span_str; let mut span = span_str;
let should_merge = |e: &Event, span: Span| { let should_merge = |e: &Event, span: Span| {
matches!(e.kind, EventKind::Str | EventKind::Placeholder) matches!(e.kind, EventKind::Str | EventKind::Placeholder)
@ -952,7 +952,7 @@ impl<'s> Parser<'s> {
} }
} }
fn apply_word_attributes(&mut self, span_str: Span) -> Event { fn apply_word_attributes(&mut self, span_str: Span) -> Event<'s> {
if let Some(i) = span_str if let Some(i) = span_str
.of(self.input.src) .of(self.input.src)
.bytes() .bytes()
@ -1089,8 +1089,8 @@ impl Opener {
} }
} }
enum DelimEventKind { enum DelimEventKind<'s> {
Container(Container), Container(Container<'s>),
Span(SpanType), Span(SpanType),
Quote(QuoteType), Quote(QuoteType),
Link { Link {
@ -1100,7 +1100,7 @@ enum DelimEventKind {
}, },
} }
impl From<Opener> for DelimEventKind { impl<'s> From<Opener> for DelimEventKind<'s> {
fn from(d: Opener) -> Self { fn from(d: Opener) -> Self {
match d { match d {
Opener::Span(ty) => Self::Span(ty), Opener::Span(ty) => Self::Span(ty),
@ -1127,7 +1127,7 @@ impl From<Opener> for DelimEventKind {
} }
impl<'s> Iterator for Parser<'s> { impl<'s> Iterator for Parser<'s> {
type Item = Event; type Item = Event<'s>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
while self.events.is_empty() while self.events.is_empty()
@ -1158,7 +1158,7 @@ impl<'s> Iterator for Parser<'s> {
let ty_opener = if let EventKind::Enter(ty) = self.events[event_opener].kind { let ty_opener = if let EventKind::Enter(ty) = self.events[event_opener].kind {
debug_assert!(matches!( debug_assert!(matches!(
ty, ty,
Verbatim | RawFormat | InlineMath | DisplayMath Verbatim | RawFormat { .. } | InlineMath | DisplayMath
)); ));
ty ty
} else { } else {
@ -1336,16 +1336,16 @@ mod test {
fn raw_format() { fn raw_format() {
test_parse!( test_parse!(
"`raw`{=format}", "`raw`{=format}",
(Enter(RawFormat), "format"), (Enter(RawFormat { format: "format" }), "`"),
(Str, "raw"), (Str, "raw"),
(Exit(RawFormat), "format"), (Exit(RawFormat { format: "format" }), "`{=format}"),
); );
test_parse!( test_parse!(
"before `raw`{=format} after", "before `raw`{=format} after",
(Str, "before "), (Str, "before "),
(Enter(RawFormat), "format"), (Enter(RawFormat { format: "format" }), "`"),
(Str, "raw"), (Str, "raw"),
(Exit(RawFormat), "format"), (Exit(RawFormat { format: "format" }), "`{=format}"),
(Str, " after"), (Str, " after"),
); );
} }

View file

@ -779,9 +779,7 @@ impl<'s> Parser<'s> {
inline::Container::Verbatim => Container::Verbatim, inline::Container::Verbatim => Container::Verbatim,
inline::Container::InlineMath => Container::Math { display: false }, inline::Container::InlineMath => Container::Math { display: false },
inline::Container::DisplayMath => Container::Math { display: true }, inline::Container::DisplayMath => Container::Math { display: true },
inline::Container::RawFormat => Container::RawInline { inline::Container::RawFormat { format } => Container::RawInline { format },
format: inline.span.of(self.src),
},
inline::Container::Subscript => Container::Subscript, inline::Container::Subscript => Container::Subscript,
inline::Container::Superscript => Container::Superscript, inline::Container::Superscript => Container::Superscript,
inline::Container::Insert => Container::Insert, inline::Container::Insert => Container::Insert,