Bold not getting detected.

This commit is contained in:
Tom Alexander 2022-11-25 18:23:51 -05:00
parent cdb35edd64
commit 9c54689bd9
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 16 additions and 4 deletions

View File

@ -10,6 +10,7 @@ use nom::error::ParseError;
use nom::error::VerboseError; use nom::error::VerboseError;
use nom::IResult; use nom::IResult;
use nom::Parser; use nom::Parser;
use tracing::trace;
type Link<'r, T> = Option<&'r Node<'r, T>>; type Link<'r, T> = Option<&'r Node<'r, T>>;
type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>; type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
@ -67,6 +68,7 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContext<'r> {
} }
fn match_fail<'s>(&'r self, i: &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> { fn match_fail<'s>(&'r self, i: &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> {
trace!("Checking for fail with \"{}\"", i);
let mut current_node = self.head.as_ref(); let mut current_node = self.head.as_ref();
while current_node.is_some() { while current_node.is_some() {
let current_node_unwrapped = current_node let current_node_unwrapped = current_node
@ -76,6 +78,7 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContext<'r> {
ChainBehavior::AndParent(Some(matcher)) => { ChainBehavior::AndParent(Some(matcher)) => {
let local_result = matcher(i); let local_result = matcher(i);
if local_result.is_ok() { if local_result.is_ok() {
trace!("FAIL MATCHED");
return local_result; return local_result;
} }
} }
@ -83,6 +86,7 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContext<'r> {
ChainBehavior::IgnoreParent(Some(matcher)) => { ChainBehavior::IgnoreParent(Some(matcher)) => {
let local_result = matcher(i); let local_result = matcher(i);
if local_result.is_ok() { if local_result.is_ok() {
trace!("FAIL MATCHED");
return local_result; return local_result;
} }
// TODO: Make this a custom error // TODO: Make this a custom error
@ -100,3 +104,9 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContext<'r> {
unreachable!() unreachable!()
} }
} }
impl<'r, T> std::fmt::Debug for ContextTree<'r, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ContextTree")
}
}

View File

@ -134,6 +134,7 @@ pub fn link_end(input: &str) -> Res<&str, TextElement> {
map(symbol("]"), TextElement::Symbol)(input) map(symbol("]"), TextElement::Symbol)(input)
} }
#[instrument]
pub fn paragraph_end(input: &str) -> Res<&str, &str> { pub fn paragraph_end(input: &str) -> Res<&str, &str> {
recognize(tuple((map(line_break, TextElement::LineBreak), blank_line)))(input) recognize(tuple((map(line_break, TextElement::LineBreak), blank_line)))(input)
} }

View File

@ -24,6 +24,7 @@ use nom::error::VerboseError;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use nom::IResult; use nom::IResult;
use tracing::instrument;
fn flat_text_element<'s, 'r>( fn flat_text_element<'s, 'r>(
i: &'s str, i: &'s str,
@ -44,21 +45,21 @@ fn flat_text_element<'s, 'r>(
))(i) ))(i)
} }
#[instrument]
fn recognize_bold_end(input: &str) -> Res<&str, &str> { fn recognize_bold_end(input: &str) -> Res<&str, &str> {
recognize(bold_end)(input) recognize(bold_end)(input)
} }
#[instrument]
fn flat_bold<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str, TextElement<'s>> { fn flat_bold<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str, TextElement<'s>> {
let new_context = context.with_additional_fail_matcher(&recognize_bold_end); let new_context = context.with_additional_fail_matcher(&recognize_bold_end);
let text_element_parser = parser_with_context!(flat_text_element)(&new_context); let text_element_parser = parser_with_context!(flat_text_element)(&new_context);
let (remaining, captured) = recognize(tuple(( let (remaining, captured) = recognize(tuple((
bold_start, bold_start,
many_till(text_element_parser, bold_end), many_till(text_element_parser, bold_end),
bold_end bold_end,
)))(i)?; )))(i)?;
let ret = TextElement::Bold(Bold { let ret = TextElement::Bold(Bold { contents: captured });
contents: captured
});
Ok((remaining, ret)) Ok((remaining, ret))
} }