Implement the first form of LaTeX fragment.

This commit is contained in:
Tom Alexander 2023-07-18 21:50:29 -04:00
parent 5ad8fdf4b2
commit c79b8c7833
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 56 additions and 24 deletions

View File

@ -1,18 +1,21 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::satisfy;
use nom::character::complete::alpha1;
use nom::character::complete::anychar;
use nom::character::complete::line_ending;
use nom::character::complete::one_of;
use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::Context;
use crate::error::Res;
use crate::parser::object::Entity;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::not_yet_implemented;
use crate::parser::LatexFragment;
#[tracing::instrument(ret, level = "debug")]
@ -20,22 +23,52 @@ pub fn latex_fragment<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, LatexFragment<'s>> {
not_yet_implemented()?;
let (remaining, _) = tag("\\")(input)?;
todo!()
// let (remaining, entity_name) = name(context, remaining)?;
// let (remaining, _) = alt((
// tag("{}"),
// peek(recognize(parser_with_context!(entity_end)(context))),
// ))(remaining)?;
// let (remaining, _) = space0(remaining)?;
// let source = get_consumed(input, remaining);
// Ok((
// remaining,
// Entity {
// source,
// entity_name,
// },
// ))
let (remaining, _) = alt((parser_with_context!(raw_latex_fragment)(context),))(input)?;
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> {
let (remaining, _) = tag("\\")(input)?;
let (remaining, _) = name(context, remaining)?;
let (remaining, _) = opt(parser_with_context!(brackets)(context))(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, source))
}
#[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))
}

View File

@ -107,7 +107,6 @@ pub struct Entity<'s> {
#[derive(Debug, PartialEq)]
pub struct LatexFragment<'s> {
pub source: &'s str,
pub entity_name: &'s str,
}
impl<'s> Source<'s> for Object<'s> {