Remove unnecessary closures in plain lists.
This commit is contained in:
parent
44f7412a5c
commit
59cb3c2bbf
@ -25,6 +25,7 @@ use super::org_source::OrgSource;
|
|||||||
use super::util::include_input;
|
use super::util::include_input;
|
||||||
use super::util::indentation_level;
|
use super::util::indentation_level;
|
||||||
use super::util::non_whitespace_character;
|
use super::util::non_whitespace_character;
|
||||||
|
use crate::context::bind_context;
|
||||||
use crate::context::parser_with_context;
|
use crate::context::parser_with_context;
|
||||||
use crate::context::ContextElement;
|
use crate::context::ContextElement;
|
||||||
use crate::context::ContextMatcher;
|
use crate::context::ContextMatcher;
|
||||||
@ -120,7 +121,7 @@ where
|
|||||||
// While #3 is the most slow, it also seems to cleanest and involves the least manual mutation of already-parsed objects so I am going with #3 for now, but we should revisit #1 or #2 when the parser is more developed.
|
// While #3 is the most slow, it also seems to cleanest and involves the least manual mutation of already-parsed objects so I am going with #3 for now, but we should revisit #1 or #2 when the parser is more developed.
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let list_item = parser_with_context!(plain_list_item)(&parser_context)(remaining);
|
let list_item = plain_list_item(&parser_context, remaining);
|
||||||
match (&first_item_list_type, &list_item) {
|
match (&first_item_list_type, &list_item) {
|
||||||
(None, Ok((_remain, (list_type, _item)))) => {
|
(None, Ok((_remain, (list_type, _item)))) => {
|
||||||
let _ = first_item_list_type.insert(*list_type);
|
let _ = first_item_list_type.insert(*list_type);
|
||||||
@ -140,7 +141,7 @@ where
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let maybe_exit = parser_with_context!(exit_matcher_parser)(&parser_context)(remaining);
|
let maybe_exit = exit_matcher_parser(&parser_context, remaining);
|
||||||
if maybe_exit.is_ok() {
|
if maybe_exit.is_ok() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -157,7 +158,7 @@ where
|
|||||||
let final_item_context = ContextElement::ConsumeTrailingWhitespace(false);
|
let final_item_context = ContextElement::ConsumeTrailingWhitespace(false);
|
||||||
let final_item_context = parser_context.with_additional_node(&final_item_context);
|
let final_item_context = parser_context.with_additional_node(&final_item_context);
|
||||||
let (remaining, (_, reparsed_final_item)) =
|
let (remaining, (_, reparsed_final_item)) =
|
||||||
parser_with_context!(plain_list_item)(&final_item_context)(final_child_start)?;
|
plain_list_item(&final_item_context, final_child_start)?;
|
||||||
children.push((final_child_start, reparsed_final_item));
|
children.push((final_child_start, reparsed_final_item));
|
||||||
|
|
||||||
let (remaining, _trailing_ws) =
|
let (remaining, _trailing_ws) =
|
||||||
@ -187,10 +188,10 @@ fn plain_list_item<'b, 'g, 'r, 's>(
|
|||||||
) -> Res<OrgSource<'s>, (PlainListType, PlainListItem<'s>)> {
|
) -> Res<OrgSource<'s>, (PlainListType, PlainListItem<'s>)> {
|
||||||
start_of_line(input)?;
|
start_of_line(input)?;
|
||||||
let (remaining, (indent_level, _leading_whitespace)) = indentation_level(context, input)?;
|
let (remaining, (indent_level, _leading_whitespace)) = indentation_level(context, input)?;
|
||||||
let (remaining, (bullet_type, bull)) = verify(
|
let (remaining, (bullet_type, bull)) =
|
||||||
parser_with_context!(bullet)(context),
|
verify(bind_context!(bullet, context), |(_bullet_type, bull)| {
|
||||||
|(_bullet_type, bull)| !Into::<&str>::into(bull).starts_with('*') || indent_level > 0,
|
!Into::<&str>::into(bull).starts_with('*') || indent_level > 0
|
||||||
)(remaining)?;
|
})(remaining)?;
|
||||||
|
|
||||||
let (remaining, maybe_counter_set) =
|
let (remaining, maybe_counter_set) =
|
||||||
opt(tuple((space1, tag("[@"), counter_set_value, tag("]"))))(remaining)?;
|
opt(tuple((space1, tag("[@"), counter_set_value, tag("]"))))(remaining)?;
|
||||||
@ -199,7 +200,7 @@ fn plain_list_item<'b, 'g, 'r, 's>(
|
|||||||
let (remaining, maybe_checkbox) = opt(tuple((space1, item_checkbox)))(remaining)?;
|
let (remaining, maybe_checkbox) = opt(tuple((space1, item_checkbox)))(remaining)?;
|
||||||
|
|
||||||
let (remaining, maybe_tag) = if let BulletType::Unordered = bullet_type {
|
let (remaining, maybe_tag) = if let BulletType::Unordered = bullet_type {
|
||||||
opt(tuple((space1, parser_with_context!(item_tag)(context))))(remaining)?
|
opt(tuple((space1, bind_context!(item_tag, context))))(remaining)?
|
||||||
} else {
|
} else {
|
||||||
(remaining, None)
|
(remaining, None)
|
||||||
};
|
};
|
||||||
@ -221,9 +222,8 @@ fn plain_list_item<'b, 'g, 'r, 's>(
|
|||||||
let parser_context = context.with_additional_node(&contexts[0]);
|
let parser_context = context.with_additional_node(&contexts[0]);
|
||||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||||
|
|
||||||
let maybe_contentless_item: Res<OrgSource<'_>, ()> = peek(parser_with_context!(
|
let maybe_contentless_item: Res<OrgSource<'_>, ()> =
|
||||||
detect_contentless_item_contents
|
detect_contentless_item_contents(&parser_context, remaining);
|
||||||
)(&parser_context))(remaining);
|
|
||||||
if let Ok((_rem, _ws)) = maybe_contentless_item {
|
if let Ok((_rem, _ws)) = maybe_contentless_item {
|
||||||
let (remaining, _trailing_ws) = if context.should_consume_trailing_whitespace() {
|
let (remaining, _trailing_ws) = if context.should_consume_trailing_whitespace() {
|
||||||
recognize(alt((recognize(many1(blank_line)), eof)))(remaining)?
|
recognize(alt((recognize(many1(blank_line)), eof)))(remaining)?
|
||||||
@ -257,8 +257,8 @@ fn plain_list_item<'b, 'g, 'r, 's>(
|
|||||||
.count();
|
.count();
|
||||||
|
|
||||||
let (mut remaining, (mut children, _exit_contents)) = many_till(
|
let (mut remaining, (mut children, _exit_contents)) = many_till(
|
||||||
include_input(parser_with_context!(element(true))(&parser_context)),
|
include_input(bind_context!(element(true), &parser_context)),
|
||||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
bind_context!(exit_matcher_parser, &parser_context),
|
||||||
)(remaining)?;
|
)(remaining)?;
|
||||||
|
|
||||||
if !children.is_empty() && !context.should_consume_trailing_whitespace() {
|
if !children.is_empty() && !context.should_consume_trailing_whitespace() {
|
||||||
@ -267,9 +267,8 @@ fn plain_list_item<'b, 'g, 'r, 's>(
|
|||||||
let (final_child_start, _original_final_child) = children
|
let (final_child_start, _original_final_child) = children
|
||||||
.pop()
|
.pop()
|
||||||
.expect("if-statement already checked that children was non-empty.");
|
.expect("if-statement already checked that children was non-empty.");
|
||||||
let (remain, reparsed_final_element) = include_input(parser_with_context!(element(true))(
|
let (remain, reparsed_final_element) =
|
||||||
&final_item_context,
|
include_input(bind_context!(element(true), &final_item_context))(final_child_start)?;
|
||||||
))(final_child_start)?;
|
|
||||||
remaining = remain;
|
remaining = remain;
|
||||||
children.push(reparsed_final_element);
|
children.push(reparsed_final_element);
|
||||||
}
|
}
|
||||||
@ -322,7 +321,7 @@ fn bullet<'b, 'g, 'r, 's>(
|
|||||||
map(tag("+"), |bull| (BulletType::Unordered, bull)),
|
map(tag("+"), |bull| (BulletType::Unordered, bull)),
|
||||||
map(
|
map(
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
parser_with_context!(counter)(context),
|
bind_context!(counter, context),
|
||||||
alt((tag("."), tag(")"))),
|
alt((tag("."), tag(")"))),
|
||||||
))),
|
))),
|
||||||
|bull| (BulletType::Ordered, bull),
|
|bull| (BulletType::Ordered, bull),
|
||||||
@ -412,7 +411,7 @@ fn _plain_list_item_end<'b, 'g, 'r, 's>(
|
|||||||
start_of_line(input)?;
|
start_of_line(input)?;
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
opt(blank_line),
|
opt(blank_line),
|
||||||
parser_with_context!(line_indented_lte_matcher)(context),
|
bind_context!(line_indented_lte_matcher, context),
|
||||||
)))(input)
|
)))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,7 +430,7 @@ fn _line_indented_lte<'b, 'g, 'r, 's>(
|
|||||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||||
let matched = recognize(verify(
|
let matched = recognize(verify(
|
||||||
tuple((
|
tuple((
|
||||||
parser_with_context!(indentation_level)(context),
|
bind_context!(indentation_level, context),
|
||||||
non_whitespace_character,
|
non_whitespace_character,
|
||||||
)),
|
)),
|
||||||
// It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09)
|
// It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09)
|
||||||
@ -457,8 +456,8 @@ fn item_tag<'b, 'g, 'r, 's>(
|
|||||||
let (remaining, (children, _exit_contents)) = verify(
|
let (remaining, (children, _exit_contents)) = verify(
|
||||||
many_till(
|
many_till(
|
||||||
// TODO: Should this be using a different set like the minimal set?
|
// TODO: Should this be using a different set like the minimal set?
|
||||||
parser_with_context!(standard_set_object)(&parser_context),
|
bind_context!(standard_set_object, &parser_context),
|
||||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
bind_context!(exit_matcher_parser, &parser_context),
|
||||||
),
|
),
|
||||||
|(children, _exit_contents)| !children.is_empty(),
|
|(children, _exit_contents)| !children.is_empty(),
|
||||||
)(input)?;
|
)(input)?;
|
||||||
@ -508,7 +507,7 @@ fn item_tag_post_gap<'b, 'g, 'r, 's>(
|
|||||||
alt((
|
alt((
|
||||||
peek(recognize(not(blank_line))),
|
peek(recognize(not(blank_line))),
|
||||||
peek(recognize(tuple((many0(blank_line), eof)))),
|
peek(recognize(tuple((many0(blank_line), eof)))),
|
||||||
parser_with_context!(exit_matcher_parser)(context),
|
bind_context!(exit_matcher_parser, context),
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
))),
|
))),
|
||||||
@ -538,7 +537,7 @@ fn detect_contentless_item_contents<'b, 'g, 'r, 's>(
|
|||||||
) -> Res<OrgSource<'s>, ()> {
|
) -> Res<OrgSource<'s>, ()> {
|
||||||
let (remaining, _) = recognize(many_till(
|
let (remaining, _) = recognize(many_till(
|
||||||
blank_line,
|
blank_line,
|
||||||
parser_with_context!(exit_matcher_parser)(context),
|
bind_context!(exit_matcher_parser, context),
|
||||||
))(input)?;
|
))(input)?;
|
||||||
Ok((remaining, ()))
|
Ok((remaining, ()))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user