Merge branch 'paragraph_without_affiliated_keyword'
rustfmt Build rustfmt has succeeded Details
rust-build Build rust-build has failed Details
rust-test Build rust-test has failed Details
rust-foreign-document-test Build rust-foreign-document-test has failed Details

This commit is contained in:
Tom Alexander 2023-10-12 17:46:56 -04:00
commit 1d9f91cdd2
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
22 changed files with 421 additions and 218 deletions

View File

@ -0,0 +1,3 @@
1. foo
#+NAME: bar
2. baz

View File

@ -0,0 +1,5 @@
* Overwrite
#+NAME: foo
:PROPERTIES:
:header-args: :var foo="lorem"
:END:

View File

@ -0,0 +1,3 @@
|foo|
#+NAME: bar
|baz |

View File

@ -0,0 +1,3 @@
#+NAME: foo
* bar
#+NAME: baz

View File

@ -25,12 +25,15 @@ use crate::types::AffiliatedKeywordValue;
use crate::types::AffiliatedKeywords;
use crate::types::Keyword;
pub(crate) fn parse_affiliated_keywords<'g, 's>(
pub(crate) fn parse_affiliated_keywords<'g, 's, AK>(
global_settings: &'g GlobalSettings<'g, 's>,
input: Vec<Keyword<'s>>,
) -> AffiliatedKeywords<'s> {
input: AK,
) -> AffiliatedKeywords<'s>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let mut ret = BTreeMap::new();
for kw in input.into_iter() {
for kw in input {
let translated_name = translate_name(global_settings, kw.key);
if is_single_string_keyword(global_settings, translated_name.as_str()) {
ret.insert(

View File

@ -9,13 +9,11 @@ use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use nom::InputTake;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::BracketDepth;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use super::util::start_of_line;
@ -28,17 +26,21 @@ use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::org_line_ending;
use crate::types::BabelCall;
use crate::types::Keyword;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn babel_call<'b, 'g, 'r, 's>(
pub(crate) fn babel_call<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, BabelCall<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, BabelCall<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
start_of_line(remaining)?;
let (remaining, _) = tuple((space0, tag("#+"), tag_no_case("call"), tag(":")))(remaining)?;

View File

@ -14,16 +14,21 @@ use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
use crate::types::DiarySexp;
use crate::types::Keyword;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn diary_sexp<'b, 'g, 'r, 's>(
pub(crate) fn diary_sexp<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, DiarySexp<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, DiarySexp<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
start_of_line(remaining)?;
let (remaining, value) = recognize(tuple((tag("%%("), is_not("\r\n"))))(remaining)?;
let (remaining, _eol) = org_line_ending(remaining)?;

View File

@ -7,12 +7,10 @@ use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::not;
use nom::combinator::recognize;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::context::parser_with_context;
@ -32,6 +30,7 @@ use crate::parser::util::start_of_line;
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
use crate::types::Drawer;
use crate::types::Element;
use crate::types::Keyword;
use crate::types::Paragraph;
use crate::types::SetSource;
@ -39,16 +38,20 @@ use crate::types::SetSource;
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn drawer<'b, 'g, 'r, 's>(
pub(crate) fn drawer<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Drawer<'s>> {
) -> Res<OrgSource<'s>, Drawer<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
if immediate_in_section(context, "drawer") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
))));
}
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
start_of_line(remaining)?;
let (remaining, _leading_whitespace) = space0(remaining)?;
let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple((

View File

@ -18,7 +18,6 @@ use nom::sequence::preceded;
use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::context::parser_with_context;
@ -37,6 +36,7 @@ use crate::parser::util::immediate_in_section;
use crate::parser::util::start_of_line;
use crate::types::DynamicBlock;
use crate::types::Element;
use crate::types::Keyword;
use crate::types::Paragraph;
use crate::types::SetSource;
@ -44,16 +44,20 @@ use crate::types::SetSource;
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn dynamic_block<'b, 'g, 'r, 's>(
pub(crate) fn dynamic_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, DynamicBlock<'s>> {
) -> Res<OrgSource<'s>, DynamicBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
if immediate_in_section(context, "dynamic block") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
))));
}
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
start_of_line(remaining)?;
let (remaining, _leading_whitespace) = space0(remaining)?;

View File

@ -1,8 +1,5 @@
use nom::branch::alt;
use nom::combinator::map;
use nom::combinator::opt;
use nom::combinator::peek;
use nom::sequence::tuple;
use nom::multi::many0;
#[cfg(feature = "tracing")]
use tracing::span;
@ -21,7 +18,6 @@ use super::footnote_definition::footnote_definition;
use super::greater_block::greater_block;
use super::horizontal_rule::horizontal_rule;
use super::keyword::affiliated_keyword;
use super::keyword::affiliated_keyword_as_regular_keyword;
use super::keyword::keyword;
use super::latex_environment::latex_environment;
use super::lesser_block::comment_block;
@ -39,6 +35,8 @@ use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::MyError;
use crate::error::Res;
use crate::parser::macros::ak_element;
use crate::parser::macros::element;
use crate::parser::table::org_mode_table;
use crate::types::Element;
@ -60,117 +58,204 @@ fn _element<'b, 'g, 'r, 's>(
input: OrgSource<'s>,
can_be_paragraph: bool,
) -> Res<OrgSource<'s>, Element<'s>> {
let plain_list_matcher = parser_with_context!(plain_list)(context);
let greater_block_matcher = parser_with_context!(greater_block)(context);
let dynamic_block_matcher = parser_with_context!(dynamic_block)(context);
let footnote_definition_matcher = parser_with_context!(footnote_definition)(context);
let comment_matcher = parser_with_context!(comment)(context);
let drawer_matcher = parser_with_context!(drawer)(context);
let table_matcher = parser_with_context!(org_mode_table)(context);
let verse_block_matcher = parser_with_context!(verse_block)(context);
let comment_block_matcher = parser_with_context!(comment_block)(context);
let example_block_matcher = parser_with_context!(example_block)(context);
let export_block_matcher = parser_with_context!(export_block)(context);
let src_block_matcher = parser_with_context!(src_block)(context);
let clock_matcher = parser_with_context!(clock)(context);
let diary_sexp_matcher = parser_with_context!(diary_sexp)(context);
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 babel_keyword_matcher = parser_with_context!(babel_call)(context);
let paragraph_matcher = parser_with_context!(paragraph)(context);
let latex_environment_matcher = parser_with_context!(latex_environment)(context);
let (post_affiliated_keywords_input, affiliated_keywords) = many0(affiliated_keyword)(input)?;
let (mut remaining, mut maybe_element) = {
#[cfg(feature = "tracing")]
let span = span!(tracing::Level::DEBUG, "Main element block");
#[cfg(feature = "tracing")]
let _enter = span.enter();
let mut affiliated_keywords = affiliated_keywords.into_iter();
opt(alt((
map(plain_list_matcher, Element::PlainList),
greater_block_matcher,
map(dynamic_block_matcher, Element::DynamicBlock),
map(footnote_definition_matcher, Element::FootnoteDefinition),
map(comment_matcher, Element::Comment),
map(drawer_matcher, Element::Drawer),
map(table_matcher, Element::Table),
map(verse_block_matcher, Element::VerseBlock),
map(comment_block_matcher, Element::CommentBlock),
map(example_block_matcher, Element::ExampleBlock),
map(export_block_matcher, Element::ExportBlock),
map(src_block_matcher, Element::SrcBlock),
map(clock_matcher, Element::Clock),
map(diary_sexp_matcher, Element::DiarySexp),
map(fixed_width_area_matcher, Element::FixedWidthArea),
map(horizontal_rule_matcher, Element::HorizontalRule),
map(latex_environment_matcher, Element::LatexEnvironment),
map(babel_keyword_matcher, Element::BabelCall),
map(keyword_matcher, Element::Keyword),
)))(input)?
};
ak_element!(
plain_list,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::PlainList
);
if maybe_element.is_none() && can_be_paragraph {
#[cfg(feature = "tracing")]
let span = span!(tracing::Level::DEBUG, "Paragraph with affiliated keyword.");
#[cfg(feature = "tracing")]
let _enter = span.enter();
ak_element!(
greater_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input
);
let (remain, paragraph_with_affiliated_keyword) = opt(map(
tuple((
peek(affiliated_keyword),
map(paragraph_matcher, Element::Paragraph),
)),
|(_, paragraph)| paragraph,
))(remaining)?;
if paragraph_with_affiliated_keyword.is_some() {
remaining = remain;
maybe_element = paragraph_with_affiliated_keyword;
}
}
ak_element!(
dynamic_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::DynamicBlock
);
if maybe_element.is_none() {
#[cfg(feature = "tracing")]
let span = span!(
tracing::Level::DEBUG,
"Affiliated keyword as regular keyword."
ak_element!(
footnote_definition,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::FootnoteDefinition
);
element!(comment, context, input, Element::Comment);
ak_element!(
drawer,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::Drawer
);
ak_element!(
org_mode_table,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::Table
);
ak_element!(
verse_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::VerseBlock
);
ak_element!(
comment_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::CommentBlock
);
ak_element!(
example_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::ExampleBlock
);
ak_element!(
export_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::ExportBlock
);
ak_element!(
src_block,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::SrcBlock
);
element!(clock, context, input, Element::Clock);
ak_element!(
diary_sexp,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::DiarySexp
);
ak_element!(
fixed_width_area,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::FixedWidthArea
);
ak_element!(
horizontal_rule,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::HorizontalRule
);
ak_element!(
latex_environment,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::LatexEnvironment
);
ak_element!(
babel_call,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::BabelCall
);
// Keyword with affiliated keywords
ak_element!(
keyword,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::Keyword
);
if can_be_paragraph {
// Paragraph with affiliated keyword
ak_element!(
paragraph,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input,
Element::Paragraph
);
#[cfg(feature = "tracing")]
let _enter = span.enter();
let (remain, kw) = opt(map(
parser_with_context!(affiliated_keyword_as_regular_keyword)(context),
Element::Keyword,
))(remaining)?;
if kw.is_some() {
maybe_element = kw;
remaining = remain;
}
}
if maybe_element.is_none() && can_be_paragraph {
#[cfg(feature = "tracing")]
let span = span!(
tracing::Level::DEBUG,
"Paragraph without affiliated keyword."
// Keyword without affiliated keywords
ak_element!(
keyword,
std::iter::empty(),
input,
context,
input,
Element::Keyword
);
if can_be_paragraph {
// Paragraph without affiliated keyword
ak_element!(
paragraph,
std::iter::empty(),
input,
context,
input,
Element::Paragraph
);
#[cfg(feature = "tracing")]
let _enter = span.enter();
let (remain, paragraph_without_affiliated_keyword) =
map(paragraph_matcher, Element::Paragraph)(remaining)?;
remaining = remain;
maybe_element = Some(paragraph_without_affiliated_keyword);
}
if maybe_element.is_none() {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"No element.",
))));
}
let element = maybe_element.expect("The above if-statement ensures this is Some().");
Ok((remaining, element))
Err(nom::Err::Error(CustomError::MyError(MyError(
"No element.",
))))
}
pub(crate) const fn detect_element(

View File

@ -21,16 +21,21 @@ use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
use crate::types::FixedWidthArea;
use crate::types::Keyword;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn fixed_width_area<'b, 'g, 'r, 's>(
pub(crate) fn fixed_width_area<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, FixedWidthArea<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, FixedWidthArea<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
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(remaining)?;

View File

@ -33,21 +33,26 @@ use crate::parser::util::immediate_in_section;
use crate::parser::util::maybe_consume_trailing_whitespace;
use crate::parser::util::start_of_line;
use crate::types::FootnoteDefinition;
use crate::types::Keyword;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn footnote_definition<'b, 'g, 'r, 's>(
pub(crate) fn footnote_definition<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, FootnoteDefinition<'s>> {
) -> Res<OrgSource<'s>, FootnoteDefinition<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
if immediate_in_section(context, "footnote definition") {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element".into(),
))));
}
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
start_of_line(remaining)?;
// Cannot be indented.
let (remaining, (_, lbl, _, _, _)) = tuple((

View File

@ -18,7 +18,6 @@ use nom::sequence::preceded;
use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::in_section;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
@ -48,12 +47,15 @@ use crate::types::SpecialBlock;
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn greater_block<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
pub(crate) fn greater_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Element<'s>> {
let pre_affiliated_keywords_input = input;
let (input, affiliated_keywords) = many0(affiliated_keyword)(input)?;
context: RefContext<'b, 'g, 'r, 's>,
pre_affiliated_keywords_input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Element<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
start_of_line(input)?;
let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_begin, name)) = tuple((
@ -93,12 +95,15 @@ pub(crate) fn greater_block<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn center_block<'b, 'g, 'r, 's>(
fn center_block<'b, 'g, 'r, 's, AK>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
pre_affiliated_keywords_input: OrgSource<'s>,
affiliated_keywords: Vec<Keyword<'s>>,
) -> Res<OrgSource<'s>, Element<'s>> {
affiliated_keywords: AK,
) -> Res<OrgSource<'s>, Element<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, (source, children)) = greater_block_body(
context,
input,
@ -123,12 +128,15 @@ fn center_block<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn quote_block<'b, 'g, 'r, 's>(
fn quote_block<'b, 'g, 'r, 's, AK>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
pre_affiliated_keywords_input: OrgSource<'s>,
affiliated_keywords: Vec<Keyword<'s>>,
) -> Res<OrgSource<'s>, Element<'s>> {
affiliated_keywords: AK,
) -> Res<OrgSource<'s>, Element<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, (source, children)) = greater_block_body(
context,
input,
@ -149,15 +157,18 @@ fn quote_block<'b, 'g, 'r, 's>(
))
}
fn special_block<'s>(
fn special_block<'s, AK>(
name: &'s str,
) -> impl for<'b, 'g, 'r> Fn(
RefContext<'b, 'g, 'r, 's>,
OrgSource<'s>,
OrgSource<'s>,
Vec<Keyword<'s>>,
AK,
) -> Res<OrgSource<'s>, Element<'s>>
+ 's {
+ 's
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let context_name = format!("special block {}", name);
move |context, input, pre_affiliated_keywords_input, affiliated_keywords| {
_special_block(
@ -175,14 +186,17 @@ fn special_block<'s>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn _special_block<'c, 'b, 'g, 'r, 's>(
fn _special_block<'c, 'b, 'g, 'r, 's, AK>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
pre_affiliated_keywords_input: OrgSource<'s>,
name: &'s str,
context_name: &'c str,
affiliated_keywords: Vec<Keyword<'s>>,
) -> Res<OrgSource<'s>, Element<'s>> {
affiliated_keywords: AK,
) -> Res<OrgSource<'s>, Element<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, parameters) = opt(tuple((space1, parameters)))(input)?;
let (remaining, (source, children)) = greater_block_body(
context,

View File

@ -5,12 +5,10 @@ use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many1_count;
use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::get_consumed;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
@ -18,16 +16,21 @@ use crate::context::RefContext;
use crate::error::Res;
use crate::parser::util::start_of_line;
use crate::types::HorizontalRule;
use crate::types::Keyword;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn horizontal_rule<'b, 'g, 'r, 's>(
pub(crate) fn horizontal_rule<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, HorizontalRule<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, HorizontalRule<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
start_of_line(remaining)?;
let (remaining, _rule) = recognize(tuple((
space0,

View File

@ -13,7 +13,6 @@ use nom::combinator::not;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
@ -94,11 +93,15 @@ fn _filtered_keyword<'s, F: Matcher>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn keyword<'b, 'g, 'r, 's>(
pub(crate) fn keyword<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Keyword<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, Keyword<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, mut kw) = filtered_keyword(regular_keyword_key)(remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
@ -109,22 +112,6 @@ pub(crate) fn keyword<'b, 'g, 'r, 's>(
Ok((remaining, kw))
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn affiliated_keyword_as_regular_keyword<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Keyword<'s>> {
let (remaining, mut kw) = affiliated_keyword(input)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
kw.source = Into::<&str>::into(source);
Ok((remaining, kw))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn affiliated_keyword<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, Keyword<'s>> {
filtered_keyword(affiliated_key)(input)

View File

@ -8,12 +8,10 @@ use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::get_consumed;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
@ -26,17 +24,22 @@ use crate::context::RefContext;
use crate::error::Res;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::start_of_line;
use crate::types::Keyword;
use crate::types::LatexEnvironment;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn latex_environment<'b, 'g, 'r, 's>(
pub(crate) fn latex_environment<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, LatexEnvironment<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, LatexEnvironment<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let value_start = remaining;
start_of_line(remaining)?;
let (remaining, _leading_whitespace) = space0(remaining)?;

View File

@ -13,13 +13,11 @@ use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use nom::InputTake;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::context::parser_with_context;
@ -41,6 +39,7 @@ use crate::types::CharOffsetInLine;
use crate::types::CommentBlock;
use crate::types::ExampleBlock;
use crate::types::ExportBlock;
use crate::types::Keyword;
use crate::types::LineNumber;
use crate::types::Object;
use crate::types::PlainText;
@ -53,11 +52,15 @@ use crate::types::VerseBlock;
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn verse_block<'b, 'g, 'r, 's>(
pub(crate) fn verse_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, VerseBlock<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, VerseBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, _) = lesser_block_begin("verse")(context, remaining)?;
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
@ -117,11 +120,15 @@ pub(crate) fn verse_block<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn comment_block<'b, 'g, 'r, 's>(
pub(crate) fn comment_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, CommentBlock<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, CommentBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, _) = lesser_block_begin("comment")(context, remaining)?;
let (remaining, _parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
@ -161,11 +168,15 @@ pub(crate) fn comment_block<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn example_block<'b, 'g, 'r, 's>(
pub(crate) fn example_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ExampleBlock<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, ExampleBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, _) = lesser_block_begin("example")(context, remaining)?;
let (remaining, parameters) = opt(alt((
map(tuple((space1, example_switches)), |(_, switches)| switches),
@ -238,11 +249,15 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn export_block<'b, 'g, 'r, 's>(
pub(crate) fn export_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ExportBlock<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, ExportBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, _) = lesser_block_begin("export")(context, remaining)?;
// https://orgmode.org/worg/org-syntax.html#Blocks claims that export blocks must have a single word for data but testing shows no data and multi-word data still parses as an export block.
let (remaining, export_type) = opt(map(
@ -290,11 +305,15 @@ pub(crate) fn export_block<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn src_block<'b, 'g, 'r, 's>(
pub(crate) fn src_block<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, SrcBlock<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, SrcBlock<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let (remaining, _) = lesser_block_begin("src")(context, remaining)?;
// https://orgmode.org/worg/org-syntax.html#Blocks claims that data is mandatory and must follow the LANGUAGE SWITCHES ARGUMENTS pattern but testing has shown that no data and incorrect data here will still parse to a src block.
let (remaining, language) =

40
src/parser/macros.rs Normal file
View File

@ -0,0 +1,40 @@
/// Parse an element that has affiliated keywords.
macro_rules! ak_element {
($parser:ident, $affiliated_keywords:expr, $post_affiliated_keywords_input: expr, $context: expr, $input: expr, $wrapper: expr) => {
if let Ok((remaining, ele)) = $parser(
$affiliated_keywords,
$post_affiliated_keywords_input,
$context,
$input,
) {
return Ok((remaining, $wrapper(ele)));
}
};
($parser:ident, $affiliated_keywords:expr, $post_affiliated_keywords_input: expr, $context: expr, $input: expr) => {
if let Ok((remaining, ele)) = $parser(
$affiliated_keywords,
$post_affiliated_keywords_input,
$context,
$input,
) {
return Ok((remaining, ele));
}
};
}
pub(crate) use ak_element;
macro_rules! element {
($parser:ident, $context: expr, $input: expr, $wrapper: expr) => {
if let Ok((remaining, ele)) = $parser($context, $input) {
return Ok((remaining, $wrapper(ele)));
}
};
($parser:ident, $context: expr, $input: expr) => {
if let Ok((remaining, ele)) = $parser($context, $input) {
return Ok((remaining, ele));
}
};
}
pub(crate) use element;

View File

@ -27,6 +27,7 @@ mod latex_environment;
mod latex_fragment;
mod lesser_block;
mod line_break;
mod macros;
mod object_parser;
mod org_macro;
mod org_source;

View File

@ -2,14 +2,12 @@ use nom::branch::alt;
use nom::combinator::eof;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::affiliated_keyword::parse_affiliated_keywords;
use super::element_parser::detect_element;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::blank_line;
use super::util::get_consumed;
@ -23,17 +21,22 @@ use crate::error::Res;
use crate::parser::object_parser::standard_set_object;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::start_of_line;
use crate::types::Keyword;
use crate::types::Paragraph;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn paragraph<'b, 'g, 'r, 's>(
pub(crate) fn paragraph<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Paragraph<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, Paragraph<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let contexts = [ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &paragraph_end,

View File

@ -43,6 +43,7 @@ use crate::parser::util::org_space;
use crate::parser::util::start_of_line;
use crate::types::CheckboxType;
use crate::types::IndentationLevel;
use crate::types::Keyword;
use crate::types::Object;
use crate::types::PlainList;
use crate::types::PlainListItem;
@ -83,12 +84,15 @@ pub(crate) fn detect_plain_list<'b, 'g, 'r, 's>(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn plain_list<'b, 'g, 'r, 's>(
pub(crate) fn plain_list<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PlainList<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, PlainList<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
let contexts = [
ContextElement::Context("plain list"),
ContextElement::ConsumeTrailingWhitespace(true),
@ -580,8 +584,8 @@ mod tests {
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
let (remaining, result) = plain_list_matcher(input).unwrap();
let (remaining, result) =
plain_list(std::iter::empty(), input, &initial_context, input).unwrap();
assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(result.get_standard_properties().get_source(), "1.");
}
@ -592,8 +596,8 @@ mod tests {
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
let (remaining, result) = plain_list_matcher(input).unwrap();
let (remaining, result) =
plain_list(std::iter::empty(), input, &initial_context, input).unwrap();
assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(result.get_standard_properties().get_source(), "1. foo");
}
@ -605,8 +609,7 @@ mod tests {
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
let result = plain_list_matcher(input);
let result = plain_list(std::iter::empty(), input, &initial_context, input);
assert!(result.is_err());
}
@ -617,8 +620,7 @@ mod tests {
let global_settings = GlobalSettings::default();
let initial_context = ContextElement::document_context();
let initial_context = Context::new(&global_settings, List::new(&initial_context));
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
let result = plain_list_matcher(input);
let result = plain_list(std::iter::empty(), input, &initial_context, input);
assert!(result.is_ok());
}

View File

@ -29,6 +29,7 @@ use crate::context::RefContext;
use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
use crate::types::Keyword;
use crate::types::Table;
use crate::types::TableCell;
use crate::types::TableRow;
@ -40,11 +41,15 @@ use crate::types::TableRow;
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
pub(crate) fn org_mode_table<'b, 'g, 'r, 's>(
pub(crate) fn org_mode_table<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Table<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
) -> Res<OrgSource<'s>, Table<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
start_of_line(remaining)?;
peek(tuple((space0, tag("|"))))(remaining)?;