From 65c51f79d6dd3e755f6b31b3f9b8fd53bbf2c5d5 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 26 Nov 2022 18:22:41 -0500 Subject: [PATCH] Update the paragraph parser to work at the end of the file. --- src/parser/text.rs | 13 +++++++++---- src/parser/text_element_parser.rs | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/parser/text.rs b/src/parser/text.rs index 6e965631..5cae6265 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::rc::Rc; +use nom::branch::alt; /* hypothetical link: @@ -18,6 +19,7 @@ use nom::bytes::complete::tag; use nom::character::complete::alphanumeric1; use nom::character::complete::line_ending; use nom::character::complete::space1; +use nom::combinator::eof; use nom::combinator::map; use nom::combinator::recognize; use nom::error::VerboseError; @@ -130,10 +132,13 @@ pub fn link_end(input: &str) -> Res<&str, TextElement> { } pub fn paragraph_end(input: &str) -> Res<&str, &str> { - recognize(tuple(( - map(line_break, TextElement::LineBreak), - many1(blank_line), - )))(input) + alt(( + recognize(tuple(( + map(line_break, TextElement::LineBreak), + many1(blank_line), + ))), + eof, + ))(input) } pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 1b1dd7ef..7d2db6fc 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -23,11 +23,14 @@ use nom::branch::alt; use nom::combinator::map; use nom::combinator::not; use nom::combinator::recognize; +use nom::error::ErrorKind; +use nom::error::ParseError; use nom::error::VerboseError; use nom::multi::many_till; use nom::sequence::tuple; use nom::IResult; use tracing::instrument; +use tracing::trace; fn flat_text_element<'s, 'r>( i: &'s str, @@ -84,8 +87,18 @@ pub fn paragraph<'s, 'r>( i: &'s str, context: &'r OrgModeContext<'r>, ) -> Res<&'s str, (Vec>, &'s str)> { + // not(eof)(i)?; let paragraph_context = context.with_additional_fail_matcher(¶graph_end); let text_element_parser = parser_with_context!(flat_text_element)(¶graph_context); let ret = many_till(text_element_parser, paragraph_end)(i); + trace!("Returning from paragraph with {:#?}", ret); + if let Ok((remaining, (text_elements, end_of_paragraph))) = &ret { + if text_elements.len() == 0 { + return Err(nom::Err::Error(nom::error::VerboseError::from_error_kind( + remaining, + ErrorKind::ManyTill, + ))); + } + } ret }