196 lines
6.9 KiB
Rust
196 lines
6.9 KiB
Rust
use nom::branch::alt;
|
|
use nom::bytes::complete::tag;
|
|
use nom::character::complete::anychar;
|
|
use nom::character::complete::one_of;
|
|
use nom::combinator::map;
|
|
use nom::combinator::not;
|
|
use nom::combinator::opt;
|
|
use nom::combinator::peek;
|
|
use nom::combinator::recognize;
|
|
use nom::combinator::verify;
|
|
use nom::multi::many_till;
|
|
|
|
use super::org_source::BracketDepth;
|
|
use super::org_source::OrgSource;
|
|
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
|
use super::Context;
|
|
use super::Object;
|
|
use crate::error::CustomError;
|
|
use crate::error::MyError;
|
|
use crate::error::Res;
|
|
use crate::parser::util::get_consumed;
|
|
use crate::parser::Subscript;
|
|
use crate::parser::Superscript;
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn subscript<'r, 's>(
|
|
context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Subscript<'s>> {
|
|
// We check for the underscore first before checking the pre-character as a minor optimization to avoid walking up the context tree to find the document root unnecessarily.
|
|
let (remaining, _) = tag("_")(input)?;
|
|
pre(context, input)?;
|
|
let (remaining, _body) = script_body(context, remaining)?;
|
|
let (remaining, _trailing_whitespace) =
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Subscript {
|
|
source: source.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn superscript<'r, 's>(
|
|
context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Superscript<'s>> {
|
|
// We check for the circumflex first before checking the pre-character as a minor optimization to avoid walking up the context tree to find the document root unnecessarily.
|
|
let (remaining, _) = tag("^")(input)?;
|
|
pre(context, input)?;
|
|
let (remaining, _body) = script_body(context, remaining)?;
|
|
let (remaining, _trailing_whitespace) =
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
Superscript {
|
|
source: source.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn pre<'r, 's>(_context: RefContext<'_, 'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
|
let preceding_character = input.get_preceding_character();
|
|
match preceding_character {
|
|
Some(c) if !c.is_whitespace() => {}
|
|
_ => {
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Must be preceded by a non-whitespace character.".into(),
|
|
))));
|
|
}
|
|
};
|
|
Ok((input, ()))
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum ScriptBody<'s> {
|
|
Braceless(&'s str),
|
|
WithBraces(Vec<Object<'s>>),
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn script_body<'r, 's>(
|
|
context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, ScriptBody<'s>> {
|
|
alt((
|
|
map(parser_with_context!(script_asterisk)(context), |body| {
|
|
ScriptBody::Braceless(body.into())
|
|
}),
|
|
map(parser_with_context!(script_alphanum)(context), |body| {
|
|
ScriptBody::Braceless(body.into())
|
|
}),
|
|
map(parser_with_context!(script_with_braces)(context), |body| {
|
|
ScriptBody::WithBraces(body.into())
|
|
}),
|
|
))(input)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn script_asterisk<'r, 's>(
|
|
_context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
tag("*")(input)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn script_alphanum<'r, 's>(
|
|
context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
let (remaining, _sign) = opt(recognize(one_of("+-")))(input)?;
|
|
let (remaining, _script) = many_till(
|
|
parser_with_context!(script_alphanum_character)(context),
|
|
parser_with_context!(end_script_alphanum_character)(context),
|
|
)(remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((remaining, source))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn script_alphanum_character<'r, 's>(
|
|
_context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
recognize(verify(anychar, |c| {
|
|
c.is_alphanumeric() || r#",.\"#.contains(*c)
|
|
}))(input)
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn end_script_alphanum_character<'r, 's>(
|
|
context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
let (remaining, final_char) = recognize(verify(anychar, |c| c.is_alphanumeric()))(input)?;
|
|
peek(not(parser_with_context!(script_alphanum_character)(
|
|
context,
|
|
)))(remaining)?;
|
|
Ok((remaining, final_char))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn script_with_braces<'r, 's>(
|
|
context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
|
let (remaining, _) = tag("{")(input)?;
|
|
let exit_with_depth = script_with_braces_end(remaining.get_brace_depth());
|
|
let parser_context =
|
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Gamma,
|
|
exit_matcher: &exit_with_depth,
|
|
}));
|
|
|
|
let (remaining, (children, _exit_contents)) = many_till(
|
|
parser_with_context!(standard_set_object)(&parser_context),
|
|
parser_with_context!(exit_matcher_parser)(&parser_context),
|
|
)(remaining)?;
|
|
|
|
let (remaining, _) = tag("}")(remaining)?;
|
|
Ok((remaining, children))
|
|
}
|
|
|
|
fn script_with_braces_end(
|
|
starting_brace_depth: BracketDepth,
|
|
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
move |context: Context, input: OrgSource<'_>| {
|
|
_script_with_braces_end(context, input, starting_brace_depth)
|
|
}
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn _script_with_braces_end<'r, 's>(
|
|
_context: RefContext<'_, 'r, 's>,
|
|
input: OrgSource<'s>,
|
|
starting_brace_depth: BracketDepth,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
let current_depth = input.get_brace_depth() - starting_brace_depth;
|
|
if current_depth > 0 {
|
|
// Its impossible for the next character to end the subscript or superscript if we're any amount of braces deep
|
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
"Not a valid end for subscript or superscript.".into(),
|
|
))));
|
|
}
|
|
if current_depth < 0 {
|
|
// This shouldn't be possible because if depth is 0 then a closing brace should end the subscript or superscript.
|
|
unreachable!("Exceeded subscript or superscript brace depth.")
|
|
}
|
|
tag("}")(input)
|
|
}
|