From 786521ad4ab25a8b439cb6f07214ccde82e7143d Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 4 Oct 2023 21:03:32 -0400 Subject: [PATCH] Add affiliated keyword matching to the detect_* functions. --- src/parser/diary_sexp.rs | 4 ++-- src/parser/drawer.rs | 3 +-- src/parser/dynamic_block.rs | 3 +-- src/parser/element_parser.rs | 5 ++--- src/parser/fixed_width_area.rs | 4 ++-- src/parser/footnote_definition.rs | 4 ++-- src/parser/greater_block.rs | 3 +-- src/parser/horizontal_rule.rs | 3 +-- src/parser/keyword.rs | 11 +++-------- src/parser/latex_environment.rs | 3 +-- src/parser/paragraph.rs | 3 +-- src/parser/plain_list.rs | 4 ++-- src/parser/table.rs | 4 ++-- 13 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/parser/diary_sexp.rs b/src/parser/diary_sexp.rs index 1a82ff0..5edaad9 100644 --- a/src/parser/diary_sexp.rs +++ b/src/parser/diary_sexp.rs @@ -20,8 +20,7 @@ pub(crate) fn diary_sexp<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, DiarySexp<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; let (remaining, _clock) = tag("%%(")(input)?; let (remaining, _contents) = is_not("\r\n")(remaining)?; @@ -39,6 +38,7 @@ pub(crate) fn diary_sexp<'b, 'g, 'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn detect_diary_sexp<'s>(input: OrgSource<'s>) -> Res, ()> { + let (input, _) = many0(affiliated_keyword)(input)?; tuple((start_of_line, tag("%%(")))(input)?; Ok((input, ())) } diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index 630488e..7c30357 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -43,8 +43,7 @@ pub(crate) fn drawer<'b, 'g, 'r, 's>( "Cannot nest objects of the same element".into(), )))); } - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple(( diff --git a/src/parser/dynamic_block.rs b/src/parser/dynamic_block.rs index 1872c0f..85001d5 100644 --- a/src/parser/dynamic_block.rs +++ b/src/parser/dynamic_block.rs @@ -48,8 +48,7 @@ pub(crate) fn dynamic_block<'b, 'g, 'r, 's>( "Cannot nest objects of the same element".into(), )))); } - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs index 5349650..9fe87a1 100644 --- a/src/parser/element_parser.rs +++ b/src/parser/element_parser.rs @@ -71,7 +71,6 @@ fn _element<'b, 'g, 'r, 's>( let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context); let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context); let keyword_matcher = parser_with_context!(keyword)(context); - let affiliated_keyword_matcher = parser_with_context!(affiliated_keyword)(context); let babel_keyword_matcher = parser_with_context!(babel_call_keyword)(context); let paragraph_matcher = parser_with_context!(paragraph)(context); let latex_environment_matcher = parser_with_context!(latex_environment)(context); @@ -105,11 +104,11 @@ fn _element<'b, 'g, 'r, 's>( 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. - map(affiliated_keyword_matcher, Element::Keyword)(input) + map(affiliated_keyword, Element::Keyword)(input) } } } else { - map(affiliated_keyword_matcher, Element::Keyword)(input) + map(affiliated_keyword, Element::Keyword)(input) } } }?; diff --git a/src/parser/fixed_width_area.rs b/src/parser/fixed_width_area.rs index 0d67770..1cf0a74 100644 --- a/src/parser/fixed_width_area.rs +++ b/src/parser/fixed_width_area.rs @@ -27,8 +27,7 @@ pub(crate) fn fixed_width_area<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, FixedWidthArea<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; let fixed_width_area_line_matcher = parser_with_context!(fixed_width_area_line)(context); let exit_matcher = parser_with_context!(exit_matcher_parser)(context); let (remaining, _first_line) = fixed_width_area_line_matcher(input)?; @@ -63,6 +62,7 @@ fn fixed_width_area_line<'b, 'g, 'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn detect_fixed_width_area<'s>(input: OrgSource<'s>) -> Res, ()> { + let (input, _) = many0(affiliated_keyword)(input)?; tuple(( start_of_line, space0, diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index a7660ae..937d920 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -42,8 +42,7 @@ pub(crate) fn footnote_definition<'b, 'g, 'r, 's>( "Cannot nest objects of the same element".into(), )))); } - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; // Cannot be indented. let (remaining, (_, lbl, _, _, _)) = tuple(( @@ -123,6 +122,7 @@ fn footnote_definition_end<'b, 'g, 'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn detect_footnote_definition<'s>(input: OrgSource<'s>) -> Res, ()> { + let (input, _) = many0(affiliated_keyword)(input)?; tuple((start_of_line, tag_no_case("[fn:"), label, tag("]")))(input)?; Ok((input, ())) } diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index 47c0bf7..15e7637 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -46,8 +46,7 @@ pub(crate) fn greater_block<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Element<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name)) = tuple(( diff --git a/src/parser/horizontal_rule.rs b/src/parser/horizontal_rule.rs index a471dc2..f19c56e 100644 --- a/src/parser/horizontal_rule.rs +++ b/src/parser/horizontal_rule.rs @@ -22,8 +22,7 @@ pub(crate) fn horizontal_rule<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, HorizontalRule<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; let (remaining, rule) = recognize(tuple(( space0, diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index 4026ab3..4581d1a 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -93,16 +93,12 @@ pub(crate) fn keyword<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Keyword<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; filtered_keyword(regular_keyword_key)(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub(crate) fn affiliated_keyword<'b, 'g, 'r, 's>( - _context: RefContext<'b, 'g, 'r, 's>, - input: OrgSource<'s>, -) -> Res, Keyword<'s>> { +pub(crate) fn affiliated_keyword<'s>(input: OrgSource<'s>) -> Res, Keyword<'s>> { filtered_keyword(affiliated_key)(input) } @@ -111,8 +107,7 @@ pub(crate) fn babel_call_keyword<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, BabelCall<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; let (remaining, kw) = filtered_keyword(babel_call_key)(input)?; Ok(( remaining, diff --git a/src/parser/latex_environment.rs b/src/parser/latex_environment.rs index 97469a3..7e64567 100644 --- a/src/parser/latex_environment.rs +++ b/src/parser/latex_environment.rs @@ -31,8 +31,7 @@ pub(crate) fn latex_environment<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, LatexEnvironment<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_opening, name, _open_close_brace, _ws, _line_ending)) = tuple(( diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 591e4db..2576e35 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -28,8 +28,7 @@ pub(crate) fn paragraph<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Paragraph<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Gamma, diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index 164ed9c..92e6007 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -54,6 +54,7 @@ pub(crate) fn detect_plain_list<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, ()> { + let (input, _) = many0(affiliated_keyword)(input)?; if verify( tuple(( start_of_line, @@ -79,8 +80,7 @@ pub(crate) fn plain_list<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, PlainList<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; let contexts = [ ContextElement::Context("plain list"), diff --git a/src/parser/table.rs b/src/parser/table.rs index e9445b7..e1de64c 100644 --- a/src/parser/table.rs +++ b/src/parser/table.rs @@ -39,8 +39,7 @@ pub(crate) fn org_mode_table<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Table<'s>> { - let (input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?; start_of_line(input)?; peek(tuple((space0, tag("|"))))(input)?; @@ -80,6 +79,7 @@ pub(crate) fn org_mode_table<'b, 'g, 'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub(crate) fn detect_table<'s>(input: OrgSource<'s>) -> Res, ()> { + let (input, _) = many0(affiliated_keyword)(input)?; tuple((start_of_line, space0, tag("|")))(input)?; Ok((input, ())) }