Identified the problem.

The issue is plain text is eating the line break so paragraph is failing since it expects a line break at the end.
This commit is contained in:
Tom Alexander 2023-03-27 18:08:17 -04:00
parent e1fbe36297
commit 028946ec90
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
8 changed files with 31 additions and 6 deletions

View File

@ -83,8 +83,7 @@ pub fn document(input: &str) -> Res<&str, Document> {
let section_matcher = parser_with_context!(section)(&document_context); let section_matcher = parser_with_context!(section)(&document_context);
let heading_matcher = parser_with_context!(heading)(&document_context); let heading_matcher = parser_with_context!(heading)(&document_context);
let (remaining, zeroth_section) = opt(section_matcher)(input)?; let (remaining, zeroth_section) = opt(section_matcher)(input)?;
// let (remaining, children) = many0(heading_matcher)(remaining)?; let (remaining, children) = many0(heading_matcher)(remaining)?;
let children = Vec::new();
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok(( Ok((
remaining, remaining,

View File

@ -24,6 +24,7 @@ impl<'s> Source<'s> for Element<'s> {
} }
} }
#[tracing::instrument(ret, level = "debug")]
pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> { pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> {
let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context); let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context);
let paragraph_matcher = parser_with_context!(paragraph)(context); let paragraph_matcher = parser_with_context!(paragraph)(context);

View File

@ -39,6 +39,7 @@ impl<'s> Source<'s> for Object<'s> {
} }
} }
#[tracing::instrument(ret, level = "debug")]
pub fn standard_set_object<'r, 's>( pub fn standard_set_object<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: &'s str,

View File

@ -19,6 +19,7 @@ use super::util::get_consumed;
use super::util::trailing_whitespace; use super::util::trailing_whitespace;
use super::Context; use super::Context;
#[tracing::instrument(ret, level = "debug")]
pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> { pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> {
let parser_context = let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
@ -35,6 +36,7 @@ pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st
Ok((remaining, Paragraph { source, children })) Ok((remaining, Paragraph { source, children }))
} }
#[tracing::instrument(ret, level = "debug")]
fn paragraph_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn paragraph_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let non_paragraph_element_matcher = parser_with_context!(non_paragraph_element)(context); let non_paragraph_element_matcher = parser_with_context!(non_paragraph_element)(context);
alt(( alt((

View File

@ -8,6 +8,7 @@ use super::error::MyError;
use super::error::Res; use super::error::Res;
use super::list::List; use super::list::List;
use super::list::Node; use super::list::Node;
use super::util::always_fail;
use super::Context; use super::Context;
type Matcher = dyn for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str>; type Matcher = dyn for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str>;
@ -64,20 +65,25 @@ impl<'r, 's> ContextTree<'r, 's> {
return at_end_of_file; return at_end_of_file;
} }
let blocked_context =
self.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
exit_matcher: ChainBehavior::IgnoreParent(Some(&always_fail)),
}));
for current_node in self.iter() { for current_node in self.iter() {
let context_element = current_node.get_data(); let context_element = current_node.get_data();
match context_element { match context_element {
ContextElement::ExitMatcherNode(exit_matcher) => { ContextElement::ExitMatcherNode(exit_matcher) => {
match exit_matcher.exit_matcher { match exit_matcher.exit_matcher {
ChainBehavior::AndParent(Some(matcher)) => { ChainBehavior::AndParent(Some(matcher)) => {
let local_result = matcher(self, i); let local_result = matcher(&blocked_context, i);
if local_result.is_ok() { if local_result.is_ok() {
return local_result; return local_result;
} }
} }
ChainBehavior::AndParent(None) => {} ChainBehavior::AndParent(None) => {}
ChainBehavior::IgnoreParent(Some(matcher)) => { ChainBehavior::IgnoreParent(Some(matcher)) => {
let local_result = matcher(self, i); let local_result = matcher(&blocked_context, i);
if local_result.is_ok() { if local_result.is_ok() {
return local_result; return local_result;
} }

View File

@ -25,7 +25,7 @@ use nom::multi::many0;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
#[allow(dead_code)] #[tracing::instrument(ret, level = "debug")]
pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainList<'s>> { pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainList<'s>> {
let (remaining, first_item) = plain_list_item(context, input)?; let (remaining, first_item) = plain_list_item(context, input)?;
let plain_list_item_matcher = parser_with_context!(plain_list_item)(context); let plain_list_item_matcher = parser_with_context!(plain_list_item)(context);
@ -41,7 +41,7 @@ pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
Ok((remaining, PlainList { source, children })) Ok((remaining, PlainList { source, children }))
} }
#[allow(dead_code)] #[tracing::instrument(ret, level = "debug")]
pub fn plain_list_item<'r, 's>( pub fn plain_list_item<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: &'s str,
@ -72,6 +72,7 @@ pub fn plain_list_item<'r, 's>(
)) ))
} }
#[tracing::instrument(ret, level = "debug")]
fn bullet<'s>(i: &'s str) -> Res<&'s str, &'s str> { fn bullet<'s>(i: &'s str) -> Res<&'s str, &'s str> {
alt(( alt((
tag("*"), tag("*"),
@ -81,10 +82,12 @@ fn bullet<'s>(i: &'s str) -> Res<&'s str, &'s str> {
))(i) ))(i)
} }
#[tracing::instrument(ret, level = "debug")]
fn counter<'s>(i: &'s str) -> Res<&'s str, &'s str> { fn counter<'s>(i: &'s str) -> Res<&'s str, &'s str> {
alt((recognize(one_of("abcdefghijklmnopqrstuvwxyz")), digit1))(i) alt((recognize(one_of("abcdefghijklmnopqrstuvwxyz")), digit1))(i)
} }
#[tracing::instrument(ret, level = "debug")]
fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let plain_list_item_matcher = parser_with_context!(plain_list_item)(context); let plain_list_item_matcher = parser_with_context!(plain_list_item)(context);
let line_indented_lte_matcher = parser_with_context!(line_indented_lte)(context); let line_indented_lte_matcher = parser_with_context!(line_indented_lte)(context);
@ -95,6 +98,7 @@ fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<
))(input) ))(input)
} }
#[tracing::instrument(ret, level = "debug")]
fn line_indented_lte<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn line_indented_lte<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let current_item_indent_level: &usize = let current_item_indent_level: &usize =
get_context_item_indent(context).ok_or(nom::Err::Error(CustomError::MyError(MyError( get_context_item_indent(context).ok_or(nom::Err::Error(CustomError::MyError(MyError(

View File

@ -7,6 +7,7 @@ use super::error::Res;
use super::object::PlainText; use super::object::PlainText;
use super::Context; use super::Context;
#[tracing::instrument(ret, level = "debug")]
pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainText<'s>> { pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainText<'s>> {
if input.len() == 0 { if input.len() == 0 {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(

View File

@ -71,16 +71,19 @@ pub fn get_consumed<'s>(input: &'s str, remaining: &'s str) -> &'s str {
/// A line containing only whitespace and then a line break /// A line containing only whitespace and then a line break
/// ///
/// It is up to the caller to ensure this is called at the start of a line. /// It is up to the caller to ensure this is called at the start of a line.
#[tracing::instrument(ret, level = "debug")]
pub fn blank_line(input: &str) -> Res<&str, &str> { pub fn blank_line(input: &str) -> Res<&str, &str> {
not(eof)(input)?; not(eof)(input)?;
recognize(tuple((space0, alt((line_ending, eof)))))(input) recognize(tuple((space0, alt((line_ending, eof)))))(input)
} }
#[tracing::instrument(ret, level = "debug")]
pub fn trailing_whitespace(input: &str) -> Res<&str, &str> { pub fn trailing_whitespace(input: &str) -> Res<&str, &str> {
alt((eof, recognize(tuple((line_ending, many0(blank_line))))))(input) alt((eof, recognize(tuple((line_ending, many0(blank_line))))))(input)
} }
/// Check that we are at the start of a line /// Check that we are at the start of a line
#[tracing::instrument(ret, level = "debug")]
pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> {
let document_root = context.get_document_root().unwrap(); let document_root = context.get_document_root().unwrap();
let preceding_character = get_one_before(document_root, input) let preceding_character = get_one_before(document_root, input)
@ -103,6 +106,7 @@ pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
/// Pull one non-whitespace character. /// Pull one non-whitespace character.
/// ///
/// This function only operates on spaces, tabs, carriage returns, and line feeds. It does not handle fancy unicode whitespace. /// This function only operates on spaces, tabs, carriage returns, and line feeds. It does not handle fancy unicode whitespace.
#[tracing::instrument(ret, level = "debug")]
pub fn non_whitespace_character(input: &str) -> Res<&str, char> { pub fn non_whitespace_character(input: &str) -> Res<&str, char> {
none_of(" \t\r\n")(input) none_of(" \t\r\n")(input)
} }
@ -116,6 +120,13 @@ pub fn exit_matcher_parser<'r, 's>(
peek(|i| context.check_exit_matcher(i))(input) peek(|i| context.check_exit_matcher(i))(input)
} }
#[tracing::instrument(ret, level = "debug")]
pub fn always_fail<'r, 's>(_context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
Err(nom::Err::Error(CustomError::MyError(MyError(
"Always fail",
))))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;