inline: get rid of verbatim state
This commit is contained in:
parent
8957907141
commit
81a4edb884
2 changed files with 82 additions and 97 deletions
119
src/inline.rs
119
src/inline.rs
|
@ -63,8 +63,6 @@ pub struct Parser<I> {
|
||||||
lexer: lex::Lexer<I>,
|
lexer: lex::Lexer<I>,
|
||||||
/// Span of current event.
|
/// Span of current event.
|
||||||
span: Span,
|
span: Span,
|
||||||
/// The kind, opener_len and opener_event of the current verbatim container if within one.
|
|
||||||
verbatim: Option<(Container, usize, usize)>,
|
|
||||||
/// Stack with kind and index of _potential_ openers for typesetting containers.
|
/// Stack with kind and index of _potential_ openers for typesetting containers.
|
||||||
typesets: Vec<(Container, usize)>,
|
typesets: Vec<(Container, usize)>,
|
||||||
/// Stack with index of _potential_ span/link openers.
|
/// Stack with index of _potential_ span/link openers.
|
||||||
|
@ -80,7 +78,6 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
|
||||||
Self {
|
Self {
|
||||||
lexer: lex::Lexer::new(chars),
|
lexer: lex::Lexer::new(chars),
|
||||||
span: Span::new(0, 0),
|
span: Span::new(0, 0),
|
||||||
verbatim: None,
|
|
||||||
typesets: Vec::new(),
|
typesets: Vec::new(),
|
||||||
spans: Vec::new(),
|
spans: Vec::new(),
|
||||||
events: std::collections::VecDeque::new(),
|
events: std::collections::VecDeque::new(),
|
||||||
|
@ -118,54 +115,6 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_verbatim(&mut self, first: &lex::Token) -> Option<Event> {
|
fn parse_verbatim(&mut self, first: &lex::Token) -> Option<Event> {
|
||||||
self.verbatim
|
|
||||||
.map(|(kind, opener_len, opener_event)| {
|
|
||||||
assert_eq!(self.events[opener_event].kind, EventKind::Enter(kind));
|
|
||||||
let kind = if matches!(first.kind, lex::Kind::Seq(lex::Sequence::Backtick))
|
|
||||||
&& first.len == opener_len
|
|
||||||
{
|
|
||||||
self.verbatim = None;
|
|
||||||
let kind = if matches!(kind, Verbatim)
|
|
||||||
&& matches!(
|
|
||||||
self.lexer.peek().map(|t| &t.kind),
|
|
||||||
Some(lex::Kind::Open(Delimiter::BraceEqual))
|
|
||||||
) {
|
|
||||||
let mut ahead = self.lexer.inner().clone();
|
|
||||||
let mut end = false;
|
|
||||||
let len = (&mut ahead)
|
|
||||||
.take_while(|c| {
|
|
||||||
if *c == '{' {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if *c == '}' {
|
|
||||||
end = true;
|
|
||||||
};
|
|
||||||
!end && !c.is_whitespace()
|
|
||||||
})
|
|
||||||
.count();
|
|
||||||
if len > 0 && end {
|
|
||||||
self.lexer = lex::Lexer::new(ahead);
|
|
||||||
let span_format = Span::by_len(self.span.end() + "{=".len(), len);
|
|
||||||
self.events[opener_event].kind = EventKind::Enter(RawFormat);
|
|
||||||
self.events[opener_event].span = span_format;
|
|
||||||
self.span = span_format;
|
|
||||||
RawFormat
|
|
||||||
} else {
|
|
||||||
Verbatim
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
kind
|
|
||||||
};
|
|
||||||
EventKind::Exit(kind)
|
|
||||||
} else {
|
|
||||||
EventKind::Str
|
|
||||||
};
|
|
||||||
Event {
|
|
||||||
kind,
|
|
||||||
span: self.span,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.or_else(|| {
|
|
||||||
match first.kind {
|
match first.kind {
|
||||||
lex::Kind::Seq(lex::Sequence::Dollar) => {
|
lex::Kind::Seq(lex::Sequence::Dollar) => {
|
||||||
let math_opt = (first.len <= 2)
|
let math_opt = (first.len <= 2)
|
||||||
|
@ -196,13 +145,61 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
|
||||||
lex::Kind::Seq(lex::Sequence::Backtick) => Some((Verbatim, first.len)),
|
lex::Kind::Seq(lex::Sequence::Backtick) => Some((Verbatim, first.len)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
.map(|(kind, opener_len)| {
|
.map(|(mut kind, opener_len)| {
|
||||||
self.verbatim = Some((kind, opener_len, self.events.len()));
|
let opener_event = self.events.len();
|
||||||
Event {
|
self.events.push_back(Event {
|
||||||
kind: EventKind::Enter(kind),
|
kind: EventKind::Enter(kind),
|
||||||
span: self.span,
|
span: self.span,
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut span_inner = Span::empty_at(self.span.end());
|
||||||
|
|
||||||
|
while let Some(t) = self.eat() {
|
||||||
|
if matches!(t.kind, lex::Kind::Seq(lex::Sequence::Backtick)) && t.len == opener_len
|
||||||
|
{
|
||||||
|
if matches!(kind, Verbatim)
|
||||||
|
&& matches!(
|
||||||
|
self.lexer.peek().map(|t| &t.kind),
|
||||||
|
Some(lex::Kind::Open(Delimiter::BraceEqual))
|
||||||
|
)
|
||||||
|
{
|
||||||
|
let mut ahead = self.lexer.inner().clone();
|
||||||
|
let mut end = false;
|
||||||
|
let len = (&mut ahead)
|
||||||
|
.take_while(|c| {
|
||||||
|
if *c == '{' {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
if *c == '}' {
|
||||||
|
end = true;
|
||||||
|
};
|
||||||
|
!end && !c.is_whitespace()
|
||||||
})
|
})
|
||||||
|
.count();
|
||||||
|
if len > 0 && end {
|
||||||
|
self.lexer = lex::Lexer::new(ahead);
|
||||||
|
let span_format = Span::by_len(self.span.end() + "{=".len(), len);
|
||||||
|
kind = RawFormat;
|
||||||
|
self.events[opener_event].kind = EventKind::Enter(kind);
|
||||||
|
self.events[opener_event].span = span_format;
|
||||||
|
self.span = span_format;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
span_inner = span_inner.extend(t.len);
|
||||||
|
self.reset_span();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.events.push_back(Event {
|
||||||
|
kind: EventKind::Str,
|
||||||
|
span: span_inner,
|
||||||
|
});
|
||||||
|
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Exit(kind),
|
||||||
|
span: self.span,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,7 +336,6 @@ impl<I: Iterator<Item = char> + Clone> Iterator for Parser<I> {
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
while self.events.is_empty()
|
while self.events.is_empty()
|
||||||
|| !self.typesets.is_empty()
|
|| !self.typesets.is_empty()
|
||||||
|| self.verbatim.is_some() // might be raw format
|
|
||||||
|| self // for merge
|
|| self // for merge
|
||||||
.events
|
.events
|
||||||
.back()
|
.back()
|
||||||
|
@ -352,9 +348,7 @@ impl<I: Iterator<Item = char> + Clone> Iterator for Parser<I> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.events
|
self.events.pop_front().map(|e| {
|
||||||
.pop_front()
|
|
||||||
.map(|e| {
|
|
||||||
if matches!(e.kind, EventKind::Str) {
|
if matches!(e.kind, EventKind::Str) {
|
||||||
// merge str events
|
// merge str events
|
||||||
let mut span = e.span;
|
let mut span = e.span;
|
||||||
|
@ -375,15 +369,6 @@ impl<I: Iterator<Item = char> + Clone> Iterator for Parser<I> {
|
||||||
e
|
e
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.or_else(|| {
|
|
||||||
self.verbatim.map(|(kind, _, _)| {
|
|
||||||
self.verbatim = None;
|
|
||||||
Event {
|
|
||||||
kind: EventKind::Exit(kind),
|
|
||||||
span: self.span,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -502,7 +502,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn raw_inline() {
|
fn raw_inline() {
|
||||||
test_parse!(
|
test_parse!(
|
||||||
"`raw\nraw`{=format}",
|
"``raw\nraw``{=format}",
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Start(RawInline { format: "format" }, Attributes::none()),
|
Start(RawInline { format: "format" }, Attributes::none()),
|
||||||
Str("raw\nraw"),
|
Str("raw\nraw"),
|
||||||
|
|
Loading…
Reference in a new issue