465 lines
16 KiB
Rust
465 lines
16 KiB
Rust
use nom::branch::alt;
|
|
use nom::bytes::complete::tag;
|
|
use nom::character::complete::anychar;
|
|
use nom::character::complete::line_ending;
|
|
use nom::character::complete::multispace1;
|
|
use nom::character::complete::one_of;
|
|
use nom::character::complete::space0;
|
|
use nom::combinator::map;
|
|
use nom::combinator::not;
|
|
use nom::combinator::peek;
|
|
use nom::combinator::recognize;
|
|
use nom::combinator::verify;
|
|
use nom::multi::many_till;
|
|
use nom::sequence::terminated;
|
|
#[cfg(feature = "tracing")]
|
|
use tracing::span;
|
|
|
|
use super::object_parser::standard_set_object;
|
|
use super::org_source::OrgSource;
|
|
use super::radio_link::RematchObject;
|
|
use super::util::in_object_section;
|
|
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
|
use super::util::start_of_line;
|
|
use crate::context::parser_with_context;
|
|
use crate::context::ContextElement;
|
|
use crate::context::ContextMatcher;
|
|
use crate::context::ExitClass;
|
|
use crate::context::ExitMatcherNode;
|
|
use crate::context::RefContext;
|
|
use crate::error::CustomError;
|
|
use crate::error::MyError;
|
|
use crate::error::Res;
|
|
use crate::parser::radio_link::rematch_target;
|
|
use crate::parser::util::exit_matcher_parser;
|
|
use crate::parser::util::get_consumed;
|
|
use crate::parser::util::preceded_by_whitespace;
|
|
use crate::types::Bold;
|
|
use crate::types::Code;
|
|
use crate::types::Italic;
|
|
use crate::types::Object;
|
|
use crate::types::StrikeThrough;
|
|
use crate::types::Underline;
|
|
use crate::types::Verbatim;
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub(crate) fn text_markup<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
|
alt((
|
|
map(parser_with_context!(bold)(context), Object::Bold),
|
|
map(parser_with_context!(italic)(context), Object::Italic),
|
|
map(parser_with_context!(underline)(context), Object::Underline),
|
|
map(
|
|
parser_with_context!(strike_through)(context),
|
|
Object::StrikeThrough,
|
|
),
|
|
map(parser_with_context!(verbatim)(context), Object::Verbatim),
|
|
map(parser_with_context!(code)(context), Object::Code),
|
|
))(input)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn bold<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Bold<'s>> {
|
|
let (remaining, children) = text_markup_object("*")(context, input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Bold {
|
|
source: source.into(),
|
|
children,
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn italic<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Italic<'s>> {
|
|
let (remaining, children) = text_markup_object("/")(context, input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Italic {
|
|
source: source.into(),
|
|
children,
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn underline<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Underline<'s>> {
|
|
let (remaining, children) = text_markup_object("_")(context, input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Underline {
|
|
source: source.into(),
|
|
children,
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn strike_through<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, StrikeThrough<'s>> {
|
|
let (remaining, children) = text_markup_object("+")(context, input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
StrikeThrough {
|
|
source: source.into(),
|
|
children,
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn verbatim<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Verbatim<'s>> {
|
|
let (remaining, contents) = text_markup_string("=")(context, input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Verbatim {
|
|
source: source.into(),
|
|
contents: contents.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn code<'b, 'g, 'r, 's>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Code<'s>> {
|
|
let (remaining, contents) = text_markup_string("~")(context, input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Code {
|
|
source: source.into(),
|
|
contents: contents.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
fn text_markup_object<'c>(
|
|
marker_symbol: &'c str,
|
|
) -> impl for<'b, 'g, 'r, 's> Fn(
|
|
RefContext<'b, 'g, 'r, 's>,
|
|
OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>>
|
|
+ 'c {
|
|
move |context, input: OrgSource<'_>| _text_markup_object(context, input, marker_symbol)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn _text_markup_object<'b, 'g, 'r, 's, 'c>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
marker_symbol: &'c str,
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
|
if in_object_section(context, marker_symbol) {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Cannot nest objects of the same type".into(),
|
|
))));
|
|
}
|
|
|
|
let (remaining, _) = pre(context, input)?;
|
|
let (remaining, open) = tag(marker_symbol)(remaining)?;
|
|
let (remaining, _peek_not_whitespace) =
|
|
peek(verify(anychar, |c| !c.is_whitespace() && *c != '\u{200B}'))(remaining)?;
|
|
let text_markup_end_specialized = text_markup_end(open.into(), remaining.get_byte_offset());
|
|
let contexts = [
|
|
ContextElement::ContextObject(marker_symbol),
|
|
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Gamma,
|
|
exit_matcher: &text_markup_end_specialized,
|
|
}),
|
|
];
|
|
let parser_context = context.with_additional_node(&contexts[0]);
|
|
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
|
|
|
let (remaining, (children, _exit_contents)) = verify(
|
|
many_till(
|
|
parser_with_context!(standard_set_object)(&parser_context),
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
),
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
)(remaining)?;
|
|
|
|
{
|
|
#[cfg(feature = "tracing")]
|
|
let span = span!(tracing::Level::DEBUG, "Checking parent exit.");
|
|
#[cfg(feature = "tracing")]
|
|
let _enter = span.enter();
|
|
if exit_matcher_parser(context, remaining).is_ok() {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Parent exit matcher is triggering.".into(),
|
|
))));
|
|
}
|
|
}
|
|
|
|
let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
|
|
let (remaining, _trailing_whitespace) =
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
|
Ok((remaining, children))
|
|
}
|
|
|
|
fn text_markup_string<'c>(
|
|
marker_symbol: &'c str,
|
|
) -> impl for<'b, 'g, 'r, 's> Fn(
|
|
RefContext<'b, 'g, 'r, 's>,
|
|
OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>>
|
|
+ 'c {
|
|
move |context, input: OrgSource<'_>| _text_markup_string(context, input, marker_symbol)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn _text_markup_string<'b, 'g, 'r, 's, 'c>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
marker_symbol: &'c str,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
if in_object_section(context, marker_symbol) {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Cannot nest objects of the same type".into(),
|
|
))));
|
|
}
|
|
let (remaining, _) = pre(context, input)?;
|
|
let (remaining, open) = tag(marker_symbol)(remaining)?;
|
|
let (remaining, _peek_not_whitespace) =
|
|
peek(verify(anychar, |c| !c.is_whitespace() && *c != '\u{200B}'))(remaining)?;
|
|
let text_markup_end_specialized = text_markup_end(open.into(), remaining.get_byte_offset());
|
|
let contexts = [
|
|
ContextElement::ContextObject(marker_symbol),
|
|
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Gamma,
|
|
exit_matcher: &text_markup_end_specialized,
|
|
}),
|
|
];
|
|
let parser_context = context.with_additional_node(&contexts[0]);
|
|
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
|
|
|
let (remaining, contents) = recognize(verify(
|
|
many_till(
|
|
anychar,
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
),
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
))(remaining)?;
|
|
|
|
{
|
|
#[cfg(feature = "tracing")]
|
|
let span = span!(tracing::Level::DEBUG, "Checking parent exit.");
|
|
#[cfg(feature = "tracing")]
|
|
let _enter = span.enter();
|
|
if exit_matcher_parser(context, remaining).is_ok() {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Parent exit matcher is triggering.".into(),
|
|
))));
|
|
}
|
|
}
|
|
|
|
let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
|
|
let (remaining, _trailing_whitespace) =
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
|
Ok((remaining, contents))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn pre<'b, 'g, 'r, 's>(
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, ()> {
|
|
if start_of_line(input).is_ok() {
|
|
return Ok((input, ()));
|
|
}
|
|
if preceded_by_whitespace(true)(input).is_ok() {
|
|
return Ok((input, ()));
|
|
}
|
|
let preceding_character = input.get_preceding_character();
|
|
match preceding_character {
|
|
// If None, we are at the start of the file which is technically the beginning of a line.
|
|
Some('-') | Some('(') | Some('{') | Some('\'') | Some('"') => {}
|
|
Some(_) => {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Not a valid pre character for text markup.".into(),
|
|
))));
|
|
}
|
|
None => unreachable!(), // None is for start of file, which should already be handled by the start_of_line matcher above.
|
|
};
|
|
Ok((input, ()))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn post<'b, 'g, 'r, 's>(
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, ()> {
|
|
let (remaining, _) = alt((recognize(one_of(" \r\n\t-.,;:!?')}[\"\\")), line_ending))(input)?;
|
|
Ok((remaining, ()))
|
|
}
|
|
|
|
fn text_markup_end<'c>(
|
|
marker_symbol: &'c str,
|
|
contents_start_offset: usize,
|
|
) -> impl ContextMatcher + 'c {
|
|
move |context, input: OrgSource<'_>| {
|
|
_text_markup_end(context, input, marker_symbol, contents_start_offset)
|
|
}
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn _text_markup_end<'b, 'g, 'r, 's, 'c>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
marker_symbol: &'c str,
|
|
contents_start_offset: usize,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
if input.get_byte_offset() == contents_start_offset {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Text markup cannot be empty".into(),
|
|
))));
|
|
}
|
|
not(preceded_by_whitespace(false))(input)?;
|
|
let (remaining, _marker) = terminated(
|
|
tag(marker_symbol),
|
|
peek(parser_with_context!(post)(context)),
|
|
)(input)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((remaining, source))
|
|
}
|
|
|
|
impl<'x> RematchObject<'x> for Bold<'x> {
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn rematch_object<'b, 'g, 'r, 's>(
|
|
&'x self,
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
|
let (remaining, children) =
|
|
_rematch_text_markup_object(_context, input, "*", &self.children)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Object::Bold(Bold {
|
|
source: source.into(),
|
|
children,
|
|
}),
|
|
))
|
|
}
|
|
}
|
|
|
|
impl<'x> RematchObject<'x> for Italic<'x> {
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn rematch_object<'b, 'g, 'r, 's>(
|
|
&'x self,
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
|
let (remaining, children) =
|
|
_rematch_text_markup_object(_context, input, "/", &self.children)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Object::Italic(Italic {
|
|
source: source.into(),
|
|
children,
|
|
}),
|
|
))
|
|
}
|
|
}
|
|
|
|
impl<'x> RematchObject<'x> for Underline<'x> {
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn rematch_object<'b, 'g, 'r, 's>(
|
|
&'x self,
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
|
let (remaining, children) =
|
|
_rematch_text_markup_object(_context, input, "_", &self.children)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Object::Underline(Underline {
|
|
source: source.into(),
|
|
children,
|
|
}),
|
|
))
|
|
}
|
|
}
|
|
|
|
impl<'x> RematchObject<'x> for StrikeThrough<'x> {
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn rematch_object<'b, 'g, 'r, 's>(
|
|
&'x self,
|
|
_context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
|
let (remaining, children) =
|
|
_rematch_text_markup_object(_context, input, "+", &self.children)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Object::StrikeThrough(StrikeThrough {
|
|
source: source.into(),
|
|
children,
|
|
}),
|
|
))
|
|
}
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn _rematch_text_markup_object<'b, 'g, 'r, 's, 'x>(
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
marker_symbol: &'static str,
|
|
original_match_children: &'x Vec<Object<'x>>,
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
|
let (remaining, _) = pre(context, input)?;
|
|
let (remaining, open) = tag(marker_symbol)(remaining)?;
|
|
let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?;
|
|
let text_markup_end_specialized = text_markup_end(open.into(), remaining.get_byte_offset());
|
|
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Gamma,
|
|
exit_matcher: &text_markup_end_specialized,
|
|
});
|
|
let parser_context = context.with_additional_node(&parser_context);
|
|
|
|
let (remaining, children) =
|
|
// TODO: This doesn't really check the exit matcher between each object. I think it may be possible to construct an org document that parses incorrectly with the current code.
|
|
rematch_target(&parser_context, original_match_children, remaining)?;
|
|
|
|
{
|
|
#[cfg(feature = "tracing")]
|
|
let span = span!(tracing::Level::DEBUG, "Checking parent exit.");
|
|
#[cfg(feature = "tracing")]
|
|
let _enter = span.enter();
|
|
if exit_matcher_parser(context, remaining).is_ok() {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Parent exit matcher is triggering.".into(),
|
|
))));
|
|
}
|
|
}
|
|
|
|
let (remaining, _close) = text_markup_end_specialized(context, remaining)?;
|
|
let (remaining, _trailing_whitespace) = space0(remaining)?;
|
|
Ok((remaining, children))
|
|
}
|