add inline State

This commit is contained in:
Noah Hellman 2022-12-11 09:26:55 +01:00
parent 3c5093f4db
commit 7c53d1a093
2 changed files with 60 additions and 16 deletions

View file

@ -21,7 +21,6 @@ pub enum Atom {
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Container { pub enum Container {
Span, Span,
Attributes,
// typesetting // typesetting
Subscript, Subscript,
Superscript, Superscript,
@ -38,6 +37,10 @@ pub enum Container {
RawFormat, RawFormat,
InlineMath, InlineMath,
DisplayMath, DisplayMath,
// Links
ReferenceLink,
InlineLink,
AutoLink,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -46,6 +49,7 @@ pub enum EventKind {
Exit(Container), Exit(Container),
Atom(Atom), Atom(Atom),
Str, Str,
Attributes,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -54,11 +58,36 @@ pub struct Event {
pub span: Span, pub span: Span,
} }
#[derive(Debug, Clone, Copy)] /// Current parsing state of elements that are not recursive, i.e. may not contain arbitrary inline
pub enum Dir { /// elements, can only be one of these at a time.
Open, enum State {
Close, None,
Both, /// Within a verbatim element, e.g. '$`xxxxx'
Verbatim {
kind: Container,
opener_len: usize,
},
/// Potentially within an attribute list, e.g. '{a=b '.
Attributes {
comment: bool,
},
/// Potentially within an autolink URL or an inline link URL, e.g. '<https://' or
/// '[text](https://'.
Url {
auto: bool,
},
/// Potentially within a reference link tag, e.g. '[text][tag '
ReferenceLinkTag,
}
impl State {
fn verbatim(&self) -> Option<(Container, usize)> {
if let Self::Verbatim { kind, opener_len } = self {
Some((*kind, *opener_len))
} else {
None
}
}
} }
pub struct Parser<'s> { pub struct Parser<'s> {
@ -68,7 +97,7 @@ pub struct Parser<'s> {
lexer: lex::Lexer<'s>, lexer: lex::Lexer<'s>,
verbatim: Option<(Container, usize)>, state: State,
last: bool, last: bool,
} }
@ -81,7 +110,7 @@ impl<'s> Parser<'s> {
lexer: lex::Lexer::new(""), lexer: lex::Lexer::new(""),
verbatim: None, state: State::None,
last: false, last: false,
} }
} }
@ -142,13 +171,18 @@ impl<'s> Parser<'s> {
} }
fn parse_verbatim(&mut self, first: &lex::Token) -> Option<Event> { fn parse_verbatim(&mut self, first: &lex::Token) -> Option<Event> {
self.verbatim self.state
.verbatim()
.map(|(kind, opener_len)| { .map(|(kind, opener_len)| {
let kind = if matches!(first.kind, lex::Kind::Seq(lex::Sequence::Backtick)) let kind = if matches!(first.kind, lex::Kind::Seq(lex::Sequence::Backtick))
&& first.len == opener_len && first.len == opener_len
{ {
self.verbatim = None; self.state = State::None;
EventKind::Exit(kind) if matches!(kind, Container::Span) {
todo!()
} else {
EventKind::Exit(kind)
}
} else { } else {
EventKind::Str EventKind::Str
}; };
@ -191,7 +225,7 @@ impl<'s> Parser<'s> {
_ => None, _ => None,
} }
.map(|(kind, opener_len)| { .map(|(kind, opener_len)| {
self.verbatim = Some((kind, opener_len)); self.state = State::Verbatim { kind, opener_len };
Event { Event {
kind: EventKind::Enter(kind), kind: EventKind::Enter(kind),
span: self.span, span: self.span,
@ -201,6 +235,12 @@ impl<'s> Parser<'s> {
} }
fn parse_container(&mut self, first: &lex::Token) -> Option<Event> { fn parse_container(&mut self, first: &lex::Token) -> Option<Event> {
enum Dir {
Open,
Close,
Both,
}
match first.kind { match first.kind {
lex::Kind::Sym(Symbol::Asterisk) => Some((Strong, Dir::Both)), lex::Kind::Sym(Symbol::Asterisk) => Some((Strong, Dir::Both)),
lex::Kind::Sym(Symbol::Underscore) => Some((Emphasis, Dir::Both)), lex::Kind::Sym(Symbol::Underscore) => Some((Emphasis, Dir::Both)),
@ -294,9 +334,12 @@ impl<'s> Iterator for Parser<'s> {
}) })
.or_else(|| { .or_else(|| {
if self.last { if self.last {
self.verbatim.take().map(|(kind, _)| Event { self.state.verbatim().map(|(kind, _)| {
kind: EventKind::Exit(kind), self.state = State::None;
span: self.span, Event {
kind: EventKind::Exit(kind),
span: self.span,
}
}) })
} else { } else {
None None

View file

@ -166,7 +166,7 @@ impl<'s> Container<'s> {
pub enum LinkType { pub enum LinkType {
Inline, Inline,
Reference, Reference,
Autolink, AutoLink,
Email, Email,
} }
@ -262,6 +262,7 @@ impl<'s> Event<'s> {
inline::Atom::Escape => Atom::Escape, inline::Atom::Escape => Atom::Escape,
}), }),
inline::EventKind::Str => Self::Str(content), inline::EventKind::Str => Self::Str(content),
inline::EventKind::Attributes => todo!(),
} }
} }
} }