Skip to content

Commit

Permalink
fix: handle case where tokens remain after parsing nested inline
Browse files Browse the repository at this point in the history
It might be the case that some nested inline is detected and parser
starts the parsing. However, the closing token is never encountered.
This effectively leaves some tokens in parser stack, and they must be
parsed as a `Inline::Plain`. The rest of the parsed inlines (if any) are
stored into inline cache to be returned in next iteration.
  • Loading branch information
nfejzic committed Jun 11, 2022
1 parent 9c0b0c8 commit 401fd89
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion inline/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl ParserStack {
}
}

#[derive(Debug, Clone)]
pub struct Parser<'i> {
iter: TokenIterator<'i>,
stack: ParserStack,
Expand Down Expand Up @@ -305,7 +306,30 @@ impl Parser<'_> {
let next_token = self.next_token()?;

let inline = if next_token.opens() {
self.parse_nested_inline(next_token)
let parsed_inline = self.parse_nested_inline(next_token);

if !self.stack().is_empty() {
// cache parsed inline for next iteration

// return remaining tokens as plain inline
if let Some(content) = self
.stack_mut()
.data
.drain(..)
.map(InlineContent::from_token_as_plain)
.reduce(|mut accumulated_content, content| {
accumulated_content.append(content);
accumulated_content
})
{
self.inline_cache.push_front(parsed_inline);
Inline::new(content, TokenKind::Plain)
} else {
parsed_inline
}
} else {
parsed_inline
}
} else {
let kind = next_token.kind();

Expand Down

0 comments on commit 401fd89

Please sign in to comment.