inline: use lex kinds

import Sequence and use Delimiter, Symbol consistently
This commit is contained in:
Noah Hellman 2023-02-15 17:27:50 +01:00
parent f192ea2aa6
commit 5d6d0e0840

View file

@ -3,6 +3,7 @@ use crate::lex;
use crate::Span; use crate::Span;
use lex::Delimiter; use lex::Delimiter;
use lex::Sequence;
use lex::Symbol; use lex::Symbol;
use Atom::*; use Atom::*;
@ -148,11 +149,11 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
fn parse_verbatim(&mut self, first: &lex::Token) -> Option<()> { fn parse_verbatim(&mut self, first: &lex::Token) -> Option<()> {
let (mut kind, opener_len) = match first.kind { let (mut kind, opener_len) = match first.kind {
lex::Kind::Seq(lex::Sequence::Dollar) => { lex::Kind::Seq(Sequence::Dollar) => {
let math_opt = (first.len <= 2) let math_opt = (first.len <= 2)
.then(|| { .then(|| {
if let Some(lex::Token { if let Some(lex::Token {
kind: lex::Kind::Seq(lex::Sequence::Backtick), kind: lex::Kind::Seq(Sequence::Backtick),
len, len,
}) = self.peek() }) = self.peek()
{ {
@ -174,7 +175,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
math_opt math_opt
} }
lex::Kind::Seq(lex::Sequence::Backtick) => Some((Verbatim, first.len)), lex::Kind::Seq(Sequence::Backtick) => Some((Verbatim, first.len)),
_ => None, _ => None,
}?; }?;
@ -190,7 +191,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
let mut non_whitespace_last = None; let mut non_whitespace_last = None;
while let Some(t) = self.eat() { while let Some(t) = self.eat() {
if matches!(t.kind, lex::Kind::Seq(lex::Sequence::Backtick)) && t.len == opener_len { if matches!(t.kind, lex::Kind::Seq(Sequence::Backtick)) && t.len == opener_len {
if matches!(kind, Verbatim) if matches!(kind, Verbatim)
&& matches!( && matches!(
self.lexer.peek().map(|t| &t.kind), self.lexer.peek().map(|t| &t.kind),
@ -242,10 +243,10 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
self.reset_span(); self.reset_span();
} }
if let Some((lex::Kind::Seq(lex::Sequence::Backtick), pos)) = non_whitespace_first { if let Some((lex::Kind::Seq(Sequence::Backtick), pos)) = non_whitespace_first {
span_inner = span_inner.with_start(pos); span_inner = span_inner.with_start(pos);
} }
if let Some((lex::Kind::Seq(lex::Sequence::Backtick), pos)) = non_whitespace_last { if let Some((lex::Kind::Seq(Sequence::Backtick), pos)) = non_whitespace_last {
span_inner = span_inner.with_end(pos); span_inner = span_inner.with_end(pos);
} }
@ -298,10 +299,9 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
self.events.drain(i..); self.events.drain(i..);
self.push(EventKind::Attributes); self.push(EventKind::Attributes);
self.push_sp(EventKind::Enter(Container::Span), span_str.empty_before()); self.push_sp(EventKind::Enter(Span), span_str.empty_before());
self.push_sp(EventKind::Str, span_str); self.push_sp(EventKind::Str, span_str);
return self.push_sp(EventKind::Exit(Span), span_str.empty_after());
return self.push_sp(EventKind::Exit(Container::Span), span_str.empty_after());
} else { } else {
return self.push_sp(EventKind::Placeholder, self.span.empty_before()); return self.push_sp(EventKind::Placeholder, self.span.empty_before());
} }
@ -447,9 +447,8 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
self.push(EventKind::Exit(cont)) self.push(EventKind::Exit(cont))
} }
DelimEventKind::Quote(ty) => { DelimEventKind::Quote(ty) => {
self.events[e_opener].kind = self.events[e_opener].kind = EventKind::Atom(Quote { ty, left: true });
EventKind::Atom(Atom::Quote { ty, left: true }); self.push(EventKind::Atom(Quote { ty, left: false }))
self.push(EventKind::Atom(Atom::Quote { ty, left: false }))
} }
DelimEventKind::Span(ty) => self.post_span(ty, e_opener), DelimEventKind::Span(ty) => self.post_span(ty, e_opener),
}; };
@ -460,7 +459,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
if event_closer.span.is_empty() if event_closer.span.is_empty()
&& matches!( && matches!(
event_closer.kind, event_closer.kind,
EventKind::Exit(Container::ReferenceLink | Container::ReferenceImage) EventKind::Exit(ReferenceLink | ReferenceImage)
) )
{ {
event_closer.span = inner_span; event_closer.span = inner_span;
@ -581,62 +580,59 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
fn parse_atom(&mut self, first: &lex::Token) -> Option<()> { fn parse_atom(&mut self, first: &lex::Token) -> Option<()> {
let atom = let atom = match first.kind {
match first.kind { lex::Kind::Newline => Softbreak,
lex::Kind::Newline => Softbreak, lex::Kind::Hardbreak => Hardbreak,
lex::Kind::Hardbreak => Hardbreak, lex::Kind::Escape => Escape,
lex::Kind::Escape => Escape, lex::Kind::Nbsp => Nbsp,
lex::Kind::Nbsp => Nbsp, lex::Kind::Seq(Sequence::Period) if first.len >= 3 => {
lex::Kind::Seq(lex::Sequence::Period) if first.len >= 3 => { while self.span.len() > 3 {
while self.span.len() > 3 { self.push_sp(EventKind::Atom(Ellipsis), self.span.with_len(3));
self.push_sp(EventKind::Atom(Ellipsis), self.span.with_len(3)); self.span = self.span.skip(3);
self.span = self.span.skip(3);
}
if self.span.len() == 3 {
Ellipsis
} else {
return self.push(EventKind::Str);
}
} }
lex::Kind::Seq(lex::Sequence::Hyphen) if first.len >= 2 => { if self.span.len() == 3 {
let (m, n) = if first.len % 3 == 0 { Ellipsis
(first.len / 3, 0) } else {
} else if first.len % 2 == 0 { return self.push(EventKind::Str);
(0, first.len / 2)
} else {
let n = (1..).find(|n| (first.len - 2 * n) % 3 == 0).unwrap();
((first.len - 2 * n) / 3, n)
};
std::iter::repeat(EmDash)
.take(m)
.chain(std::iter::repeat(EnDash).take(n))
.for_each(|atom| {
let l = if matches!(atom, EnDash) { 2 } else { 3 };
self.push_sp(EventKind::Atom(atom), self.span.with_len(l));
self.span = self.span.skip(l);
});
return Some(());
} }
lex::Kind::Open(lex::Delimiter::BraceQuote1) => Quote { }
ty: QuoteType::Single, lex::Kind::Seq(Sequence::Hyphen) if first.len >= 2 => {
left: true, let (m, n) = if first.len % 3 == 0 {
}, (first.len / 3, 0)
lex::Kind::Sym(lex::Symbol::Quote1) } else if first.len % 2 == 0 {
| lex::Kind::Close(lex::Delimiter::BraceQuote1) => Quote { (0, first.len / 2)
ty: QuoteType::Single, } else {
left: false, let n = (1..).find(|n| (first.len - 2 * n) % 3 == 0).unwrap();
}, ((first.len - 2 * n) / 3, n)
lex::Kind::Open(lex::Delimiter::BraceQuote2) => Quote { };
ty: QuoteType::Double, std::iter::repeat(EmDash)
left: true, .take(m)
}, .chain(std::iter::repeat(EnDash).take(n))
lex::Kind::Sym(lex::Symbol::Quote2) .for_each(|atom| {
| lex::Kind::Close(lex::Delimiter::BraceQuote2) => Quote { let l = if matches!(atom, EnDash) { 2 } else { 3 };
ty: QuoteType::Double, self.push_sp(EventKind::Atom(atom), self.span.with_len(l));
left: false, self.span = self.span.skip(l);
}, });
_ => return None, return Some(());
}; }
lex::Kind::Open(Delimiter::BraceQuote1) => Quote {
ty: QuoteType::Single,
left: true,
},
lex::Kind::Sym(Symbol::Quote1) | lex::Kind::Close(Delimiter::BraceQuote1) => Quote {
ty: QuoteType::Single,
left: false,
},
lex::Kind::Open(Delimiter::BraceQuote2) => Quote {
ty: QuoteType::Double,
left: true,
},
lex::Kind::Sym(Symbol::Quote2) | lex::Kind::Close(Delimiter::BraceQuote2) => Quote {
ty: QuoteType::Double,
left: false,
},
_ => return None,
};
self.push(EventKind::Atom(atom)) self.push(EventKind::Atom(atom))
} }