Move the affiliated keywords parser inside the specific element parsers.

We need access to the affiliated keywords to do things like set the name of the element, and only half the element parsers are allowed to have affiliated keywords, so it makes sense to move it inside the specific parsers.
This commit is contained in:
Tom Alexander
2023-10-04 20:01:09 -04:00
parent a26640355c
commit d8102b7bc2
32 changed files with 324 additions and 59 deletions

View File

@@ -1,8 +1,5 @@
use nom::branch::alt;
use nom::combinator::map;
use nom::combinator::not;
use nom::multi::many0;
use nom::sequence::tuple;
use super::clock::clock;
use super::comment::comment;
@@ -79,8 +76,6 @@ fn _element<'b, 'g, 'r, 's>(
let paragraph_matcher = parser_with_context!(paragraph)(context);
let latex_environment_matcher = parser_with_context!(latex_environment)(context);
// TODO: Affiliated keywords cannot be on comments, clocks, headings, inlinetasks, items, node properties, planning, property drawers, sections, and table rows
let (remaining, mut affiliated_keywords) = many0(affiliated_keyword_matcher)(input)?;
let (remaining, mut element) = match alt((
map(plain_list_matcher, Element::PlainList),
greater_block_matcher,
@@ -100,28 +95,20 @@ fn _element<'b, 'g, 'r, 's>(
map(horizontal_rule_matcher, Element::HorizontalRule),
map(latex_environment_matcher, Element::LatexEnvironment),
map(babel_keyword_matcher, Element::BabelCall),
map(
map(
tuple((not(affiliated_keyword_matcher), keyword_matcher)),
|(_, kw)| kw,
),
Element::Keyword,
),
))(remaining)
map(keyword_matcher, Element::Keyword),
))(input)
{
the_ok @ Ok(_) => the_ok,
Err(_) => {
if can_be_paragraph {
match map(paragraph_matcher, Element::Paragraph)(remaining) {
match map(paragraph_matcher, Element::Paragraph)(input) {
the_ok @ Ok(_) => the_ok,
Err(_) => {
// TODO: Because this function expects a single element, if there are multiple affiliated keywords before an element that cannot have affiliated keywords, we end up re-parsing the affiliated keywords many times.
affiliated_keywords.clear();
map(affiliated_keyword_matcher, Element::Keyword)(input)
}
}
} else {
affiliated_keywords.clear();
map(affiliated_keyword_matcher, Element::Keyword)(input)
}
}
@@ -149,6 +136,7 @@ fn _detect_element<'b, 'g, 'r, 's>(
input: OrgSource<'s>,
can_be_paragraph: bool,
) -> Res<OrgSource<'s>, ()> {
// TODO: What about affiliated keywords in the detect_* functions?
if alt((
parser_with_context!(detect_plain_list)(context),
detect_footnote_definition,