116 lines
3.7 KiB
Rust
116 lines
3.7 KiB
Rust
use nom::branch::alt;
|
|
use nom::bytes::complete::tag;
|
|
use nom::bytes::complete::tag_no_case;
|
|
use nom::bytes::complete::take_while1;
|
|
use nom::character::complete::anychar;
|
|
use nom::character::complete::line_ending;
|
|
use nom::character::complete::space0;
|
|
use nom::combinator::eof;
|
|
use nom::combinator::peek;
|
|
use nom::combinator::recognize;
|
|
use nom::multi::many_till;
|
|
use nom::sequence::tuple;
|
|
|
|
use super::org_source::OrgSource;
|
|
use super::util::get_consumed;
|
|
use super::Context;
|
|
use crate::error::Res;
|
|
use crate::parser::exiting::ExitClass;
|
|
use crate::parser::parser_context::ContextElement;
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
use crate::parser::util::exit_matcher_parser;
|
|
use crate::parser::util::start_of_line;
|
|
use crate::parser::LatexEnvironment;
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn latex_environment<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, LatexEnvironment<'s>> {
|
|
start_of_line(input)?;
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
|
let (remaining, (_opening, name, _open_close_brace, _ws, _line_ending)) = tuple((
|
|
tag_no_case(r#"\begin{"#),
|
|
name,
|
|
tag("}"),
|
|
space0,
|
|
line_ending,
|
|
))(remaining)?;
|
|
|
|
let latex_environment_end_specialized = latex_environment_end(name.into());
|
|
let parser_context =
|
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
class: ExitClass::Beta,
|
|
exit_matcher: &latex_environment_end_specialized,
|
|
}));
|
|
|
|
let (remaining, _contents) = contents(&latex_environment_end_specialized, context, remaining)?;
|
|
let (remaining, _end) = latex_environment_end_specialized(&parser_context, remaining)?;
|
|
|
|
let source = get_consumed(input, remaining);
|
|
Ok((
|
|
remaining,
|
|
LatexEnvironment {
|
|
source: source.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn name<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
take_while1(|c: char| c.is_alphanumeric() || c == '*')(input)
|
|
}
|
|
|
|
#[cfg_attr(
|
|
feature = "tracing",
|
|
tracing::instrument(ret, level = "debug", skip(end_matcher))
|
|
)]
|
|
pub fn contents<
|
|
'r,
|
|
's,
|
|
F: Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>,
|
|
>(
|
|
end_matcher: F,
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
let (remaining, source) = recognize(many_till(
|
|
anychar,
|
|
peek(alt((
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
parser_with_context!(end_matcher)(context),
|
|
))),
|
|
))(input)?;
|
|
|
|
Ok((remaining, source))
|
|
}
|
|
|
|
fn latex_environment_end(
|
|
current_name: &str,
|
|
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
let current_name_lower = current_name.to_lowercase();
|
|
move |context: Context, input: OrgSource<'_>| {
|
|
_latex_environment_end(context, input, current_name_lower.as_str())
|
|
}
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn _latex_environment_end<'r, 's, 'x>(
|
|
_context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
current_name_lower: &'x str,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
start_of_line(input)?;
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
|
let (remaining, (_begin, _name, _close_brace, _ws, _line_ending)) = tuple((
|
|
tag_no_case(r#"\end{"#),
|
|
tag_no_case(current_name_lower),
|
|
tag("}"),
|
|
space0,
|
|
alt((eof, line_ending)),
|
|
))(remaining)?;
|
|
let source = get_consumed(input, remaining);
|
|
Ok((remaining, source))
|
|
}
|