2023-07-18 20:51:06 -04:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2023-07-18 21:50:29 -04:00
|
|
|
use nom::character::complete::alpha1;
|
|
|
|
use nom::character::complete::anychar;
|
|
|
|
use nom::character::complete::line_ending;
|
|
|
|
use nom::character::complete::one_of;
|
2023-07-18 20:51:06 -04:00
|
|
|
use nom::character::complete::space0;
|
2023-07-18 21:50:29 -04:00
|
|
|
use nom::combinator::opt;
|
2023-07-18 20:51:06 -04:00
|
|
|
use nom::combinator::peek;
|
|
|
|
use nom::combinator::recognize;
|
2023-07-18 21:50:29 -04:00
|
|
|
use nom::multi::many_till;
|
|
|
|
use nom::sequence::tuple;
|
2023-07-18 20:51:06 -04:00
|
|
|
|
|
|
|
use super::Context;
|
|
|
|
use crate::error::Res;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
2023-07-18 21:50:29 -04:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
2023-07-18 20:51:06 -04:00
|
|
|
use crate::parser::util::get_consumed;
|
|
|
|
use crate::parser::LatexFragment;
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
pub fn latex_fragment<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, LatexFragment<'s>> {
|
2023-07-18 22:02:38 -04:00
|
|
|
let (remaining, _) = alt((
|
|
|
|
parser_with_context!(raw_latex_fragment)(context),
|
|
|
|
parser_with_context!(escaped_parenthesis_fragment)(context),
|
|
|
|
parser_with_context!(escaped_bracket_fragment)(context),
|
|
|
|
))(input)?;
|
2023-07-18 21:50:29 -04:00
|
|
|
let (remaining, _) = space0(remaining)?;
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, LatexFragment { source }))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn raw_latex_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
2023-07-18 20:51:06 -04:00
|
|
|
let (remaining, _) = tag("\\")(input)?;
|
2023-07-18 21:50:29 -04:00
|
|
|
let (remaining, _) = name(context, remaining)?;
|
|
|
|
let (remaining, _) = opt(parser_with_context!(brackets)(context))(remaining)?;
|
|
|
|
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, source))
|
|
|
|
}
|
2023-07-18 20:51:06 -04:00
|
|
|
|
2023-07-18 21:50:29 -04:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
alpha1(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn brackets<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
|
|
|
let (remaining, body) = alt((
|
|
|
|
recognize(tuple((
|
|
|
|
tag("["),
|
|
|
|
many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
|
|
alt((recognize(one_of("{}[]")), line_ending)),
|
|
|
|
))),
|
|
|
|
),
|
|
|
|
tag("]"),
|
|
|
|
))),
|
|
|
|
recognize(tuple((
|
|
|
|
tag("{"),
|
|
|
|
many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
|
|
alt((recognize(one_of("{}")), line_ending)),
|
|
|
|
))),
|
|
|
|
),
|
|
|
|
tag("}"),
|
|
|
|
))),
|
|
|
|
))(input)?;
|
|
|
|
Ok((remaining, body))
|
2023-07-18 20:51:06 -04:00
|
|
|
}
|
2023-07-18 22:02:38 -04:00
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn escaped_parenthesis_fragment<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, &'s str> {
|
|
|
|
let (remaining, _) = tag("\\(")(input)?;
|
|
|
|
let (remaining, _) = recognize(many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
|
|
tag("\\)"),
|
|
|
|
))),
|
|
|
|
))(remaining)?;
|
|
|
|
let (remaining, _) = tag("\\)")(remaining)?;
|
|
|
|
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, source))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
|
|
|
fn escaped_bracket_fragment<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: &'s str,
|
|
|
|
) -> Res<&'s str, &'s str> {
|
|
|
|
let (remaining, _) = tag("\\[")(input)?;
|
|
|
|
let (remaining, _) = recognize(many_till(
|
|
|
|
anychar,
|
|
|
|
peek(alt((
|
|
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
|
|
tag("\\]"),
|
|
|
|
))),
|
|
|
|
))(remaining)?;
|
|
|
|
let (remaining, _) = tag("\\]")(remaining)?;
|
|
|
|
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((remaining, source))
|
|
|
|
}
|