organic/src/parser/object_parser.rs

296 lines
10 KiB
Rust
Raw Normal View History

use super::org_source::OrgSource;
use super::plain_text::plain_text;
use super::regular_link::regular_link;
use super::subscript_and_superscript::detect_subscript_or_superscript;
2023-09-03 12:07:51 -04:00
use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::MyError;
2023-04-22 20:02:51 -04:00
use crate::error::Res;
2023-07-13 22:42:42 -04:00
use crate::parser::angle_link::angle_link;
use crate::parser::citation::citation;
2023-07-18 20:05:39 -04:00
use crate::parser::entity::entity;
2023-07-19 00:37:51 -04:00
use crate::parser::export_snippet::export_snippet;
use crate::parser::footnote_reference::footnote_reference;
use crate::parser::inline_babel_call::inline_babel_call;
use crate::parser::inline_source_block::inline_source_block;
2023-07-18 20:51:06 -04:00
use crate::parser::latex_fragment::latex_fragment;
2023-07-22 00:00:53 -04:00
use crate::parser::line_break::line_break;
use crate::parser::macros::element;
2023-07-14 04:28:56 +00:00
use crate::parser::org_macro::org_macro;
2023-07-13 18:18:07 -04:00
use crate::parser::plain_link::plain_link;
use crate::parser::radio_link::radio_link;
use crate::parser::radio_link::radio_target;
use crate::parser::statistics_cookie::statistics_cookie;
use crate::parser::subscript_and_superscript::subscript;
use crate::parser::subscript_and_superscript::superscript;
2023-07-22 01:40:14 -04:00
use crate::parser::target::target;
2023-04-22 20:02:51 -04:00
use crate::parser::text_markup::text_markup;
2023-07-24 17:34:07 -04:00
use crate::parser::timestamp::timestamp;
2023-09-03 12:07:51 -04:00
use crate::types::Object;
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
pub(crate) fn standard_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
element!(standard_set_object_sans_plain_text, context, input);
element!(
plain_text(detect_standard_set_object_sans_plain_text),
context,
input,
Object::PlainText
);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
pub(crate) fn minimal_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
element!(minimal_set_object_sans_plain_text, context, input);
element!(
plain_text(detect_minimal_set_object_sans_plain_text),
context,
input,
Object::PlainText
);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn standard_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
element!(timestamp, context, input, Object::Timestamp);
element!(subscript, context, input, Object::Subscript);
element!(superscript, context, input, Object::Superscript);
element!(statistics_cookie, context, input, Object::StatisticsCookie);
element!(target, context, input, Object::Target);
element!(line_break, context, input, Object::LineBreak);
element!(
inline_source_block,
context,
input,
Object::InlineSourceBlock
);
element!(inline_babel_call, context, input, Object::InlineBabelCall);
element!(citation, context, input, Object::Citation);
element!(
footnote_reference,
context,
input,
Object::FootnoteReference
);
element!(export_snippet, context, input, Object::ExportSnippet);
element!(entity, context, input, Object::Entity);
element!(latex_fragment, context, input, Object::LatexFragment);
element!(radio_link, context, input, Object::RadioLink);
element!(radio_target, context, input, Object::RadioTarget);
element!(text_markup, context, input);
element!(regular_link, context, input, Object::RegularLink);
element!(plain_link, context, input, Object::PlainLink);
element!(angle_link, context, input, Object::AngleLink);
element!(org_macro, context, input, Object::OrgMacro);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn minimal_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
element!(subscript, context, input, Object::Subscript);
element!(superscript, context, input, Object::Superscript);
element!(entity, context, input, Object::Entity);
element!(latex_fragment, context, input, Object::LatexFragment);
element!(text_markup, context, input);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-04-22 19:46:27 -04:00
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
pub(crate) fn detect_standard_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
if detect_subscript_or_superscript(input).is_ok() {
return Ok((input, ()));
}
if standard_set_object_sans_plain_text(context, input).is_ok() {
return Ok((input, ()));
}
return Err(nom::Err::Error(CustomError::MyError(MyError(
2023-10-16 17:58:52 -04:00
"No object detected.",
))));
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn detect_minimal_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
if detect_subscript_or_superscript(input).is_ok() {
return Ok((input, ()));
}
if minimal_set_object_sans_plain_text(context, input).is_ok() {
return Ok((input, ()));
}
return Err(nom::Err::Error(CustomError::MyError(MyError(
2023-10-16 17:58:52 -04:00
"No object detected.",
))));
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
pub(crate) fn regular_link_description_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
// TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
element!(
regular_link_description_set_object_sans_plain_text,
context,
input
);
element!(
plain_text(detect_regular_link_description_set_object_sans_plain_text),
context,
input,
Object::PlainText
);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
fn regular_link_description_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
// TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
element!(export_snippet, context, input, Object::ExportSnippet);
element!(statistics_cookie, context, input, Object::StatisticsCookie);
element!(
inline_source_block,
context,
input,
Object::InlineSourceBlock
);
element!(inline_babel_call, context, input, Object::InlineBabelCall);
element!(org_macro, context, input, Object::OrgMacro);
element!(minimal_set_object_sans_plain_text, context, input);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
fn detect_regular_link_description_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
if detect_subscript_or_superscript(input).is_ok() {
return Ok((input, ()));
}
if regular_link_description_set_object_sans_plain_text(context, input).is_ok() {
return Ok((input, ()));
}
2023-10-16 17:58:52 -04:00
Err(nom::Err::Error(CustomError::MyError(MyError(
"No object detected.",
))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
pub(crate) fn table_cell_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
element!(table_cell_set_object_sans_plain_text, context, input);
element!(
plain_text(detect_table_cell_set_object_sans_plain_text),
context,
input,
Object::PlainText
);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
2023-04-22 19:46:27 -04:00
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
fn table_cell_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> {
element!(citation, context, input, Object::Citation);
element!(export_snippet, context, input, Object::ExportSnippet);
element!(
footnote_reference,
context,
input,
Object::FootnoteReference
);
element!(radio_link, context, input, Object::RadioLink);
element!(regular_link, context, input, Object::RegularLink);
element!(plain_link, context, input, Object::PlainLink);
element!(angle_link, context, input, Object::AngleLink);
element!(org_macro, context, input, Object::OrgMacro);
element!(radio_target, context, input, Object::RadioTarget);
element!(target, context, input, Object::Target);
element!(timestamp, context, input, Object::Timestamp);
element!(minimal_set_object_sans_plain_text, context, input);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
}
2023-10-09 19:52:32 -04:00
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
)]
2023-09-11 13:13:28 -04:00
fn detect_table_cell_set_object_sans_plain_text<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
if detect_subscript_or_superscript(input).is_ok() {
return Ok((input, ()));
}
if table_cell_set_object_sans_plain_text(context, input).is_ok() {
return Ok((input, ()));
}
return Err(nom::Err::Error(CustomError::MyError(MyError(
2023-10-16 17:58:52 -04:00
"No object detected.",
))));
}