From c79b8c7833290a12d4245d2177bf2141474e295b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 18 Jul 2023 21:50:29 -0400 Subject: [PATCH] Implement the first form of LaTeX fragment. --- src/parser/latex_fragment.rs | 79 +++++++++++++++++++++++++----------- src/parser/object.rs | 1 - 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/parser/latex_fragment.rs b/src/parser/latex_fragment.rs index 8b3113f..84dfae8 100644 --- a/src/parser/latex_fragment.rs +++ b/src/parser/latex_fragment.rs @@ -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)) } diff --git a/src/parser/object.rs b/src/parser/object.rs index 5cd1bc0..7aa01f5 100644 --- a/src/parser/object.rs +++ b/src/parser/object.rs @@ -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> {