Fix case where indentation was causing the plain list item matcher to match items it wouldn't use in other parts.

This commit is contained in:
Tom Alexander 2023-04-14 19:56:54 -04:00
parent 143ac49777
commit 8549a6b0db
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 64 additions and 41 deletions

View File

@ -9,7 +9,7 @@ cd "$DIR"
: ${org_dir:="$DIR/../org_mode_samples"}
: ${compare_bin:="$DIR/../target/debug/org_compare"}
test_files=$(find $org_dir -type f -name '*.org')
test_files=$(find $org_dir -type f -name '*.org' | sort)
cargo build --bin org_compare

View File

@ -29,6 +29,7 @@ use nom::combinator::verify;
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::tuple;
use tracing::span;
#[tracing::instrument(ret, level = "debug")]
pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainList<'s>> {
@ -55,48 +56,70 @@ pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
Don't consume, yes exit matcher
Don't consume, no additional item
Consume, additional item
*/
let last_item_then_exit = tuple((without_consume_matcher, exit_matcher))(remaining);
match last_item_then_exit {
Ok((remain, (item, _exit)))
if item.indentation == *first_item_indentation.get_or_insert(item.indentation) =>
{
remaining = remain;
children.push(item);
break;
}
Ok(_) | Err(_) => {}
};
*/
{
let span = span!(tracing::Level::DEBUG, "first");
let _enter = span.enter();
let not_last_item = tuple((with_consume_matcher, peek(without_consume_matcher)))(remaining);
match not_last_item {
Ok((remain, (item, _future_item)))
if item.indentation == *first_item_indentation.get_or_insert(item.indentation) =>
{
remaining = remain;
children.push(item);
continue;
}
Ok(_) | Err(_) => {}
};
let last_item_then_exit = tuple((without_consume_matcher, exit_matcher))(remaining);
match last_item_then_exit {
Ok((remain, (item, _exit)))
if item.indentation
== *first_item_indentation.get_or_insert(item.indentation) =>
{
remaining = remain;
children.push(item);
break;
}
Ok(_) | Err(_) => {}
};
}
// If its not (don't consume, exit) and its not (consume, see another item) then it must be (don't consume, no additional item)
let last_item_then_exit = without_consume_matcher(remaining);
match last_item_then_exit {
Ok((remain, item))
if item.indentation == *first_item_indentation.get_or_insert(item.indentation) =>
{
remaining = remain;
children.push(item);
break;
}
Ok(_) | Err(_) => {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Should be unreachable.",
))));
unreachable!();
}
};
{
let span = span!(tracing::Level::DEBUG, "second");
let _enter = span.enter();
let not_last_item =
tuple((with_consume_matcher, peek(without_consume_matcher)))(remaining);
match not_last_item {
Ok((remain, (item, future_item)))
if item.indentation
== *first_item_indentation.get_or_insert(item.indentation)
&& future_item.indentation
== *first_item_indentation.get_or_insert(item.indentation) =>
{
remaining = remain;
children.push(item);
continue;
}
Ok(_) | Err(_) => {}
};
}
{
let span = span!(tracing::Level::DEBUG, "third");
let _enter = span.enter();
// If its not (don't consume, exit) and its not (consume, see another item) then it must be (don't consume, no additional item)
let last_item_then_exit = without_consume_matcher(remaining);
match last_item_then_exit {
Ok((remain, item))
if item.indentation
== *first_item_indentation.get_or_insert(item.indentation) =>
{
remaining = remain;
children.push(item);
break;
}
Ok(_) | Err(_) => {
// TODO: Maybe this is reachable when there are no items at all.
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Should be unreachable.",
))));
unreachable!();
}
};
}
}
if children.is_empty() {