2023-07-18 20:23:27 -04:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::character::complete::satisfy;
|
|
|
|
use nom::combinator::eof;
|
2023-10-08 18:14:48 -04:00
|
|
|
use nom::combinator::map;
|
2023-07-18 20:23:27 -04:00
|
|
|
use nom::combinator::peek;
|
|
|
|
use nom::combinator::recognize;
|
2023-10-08 23:41:41 -04:00
|
|
|
use nom::combinator::verify;
|
2023-09-14 04:27:50 -04:00
|
|
|
use nom::sequence::tuple;
|
2023-07-18 20:23:27 -04:00
|
|
|
|
2023-08-23 00:30:26 -04:00
|
|
|
use super::org_source::OrgSource;
|
2023-08-31 15:44:44 -04:00
|
|
|
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
2023-10-08 18:06:56 -04:00
|
|
|
use crate::context::EntityDefinition;
|
2023-09-03 12:23:18 -04:00
|
|
|
use crate::context::RefContext;
|
2023-08-29 15:10:27 -04:00
|
|
|
use crate::error::CustomError;
|
|
|
|
use crate::error::MyError;
|
2023-07-18 20:23:27 -04:00
|
|
|
use crate::error::Res;
|
|
|
|
use crate::parser::util::get_consumed;
|
2023-09-03 12:23:18 -04:00
|
|
|
use crate::types::Entity;
|
2023-07-18 20:23:27 -04:00
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 13:13:28 -04:00
|
|
|
pub(crate) fn entity<'b, 'g, 'r, 's>(
|
2023-09-03 15:44:18 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Entity<'s>> {
|
2023-07-18 20:23:27 -04:00
|
|
|
let (remaining, _) = tag("\\")(input)?;
|
2023-10-08 18:14:48 -04:00
|
|
|
let (remaining, (entity_definition, entity_name, use_brackets)) = name(context, remaining)?;
|
2023-09-14 04:27:50 -04:00
|
|
|
|
2023-08-31 15:44:44 -04:00
|
|
|
let (remaining, _trailing_whitespace) =
|
|
|
|
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-07-18 20:23:27 -04:00
|
|
|
|
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
Entity {
|
2023-08-23 00:30:26 -04:00
|
|
|
source: source.into(),
|
2023-10-08 17:24:03 -04:00
|
|
|
name: entity_name.into(),
|
2023-10-08 18:06:56 -04:00
|
|
|
latex_math_mode: entity_definition.latex_math_mode,
|
|
|
|
latex: entity_definition.latex,
|
|
|
|
html: entity_definition.html,
|
|
|
|
ascii: entity_definition.ascii,
|
|
|
|
utf8: entity_definition.utf8,
|
2023-10-08 18:14:48 -04:00
|
|
|
use_brackets,
|
2023-07-18 20:23:27 -04:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-03 15:44:18 -04:00
|
|
|
fn name<'b, 'g, 'r, 's>(
|
2023-10-08 18:01:42 -04:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 00:30:26 -04:00
|
|
|
input: OrgSource<'s>,
|
2023-10-08 18:14:48 -04:00
|
|
|
) -> Res<OrgSource<'s>, (&'g EntityDefinition<'s>, OrgSource<'s>, bool)> {
|
2023-10-08 18:01:42 -04:00
|
|
|
for entity in context.get_global_settings().entities {
|
2023-09-14 04:27:50 -04:00
|
|
|
let result = tuple((
|
2023-10-08 18:01:42 -04:00
|
|
|
tag::<_, _, CustomError<_>>(entity.name),
|
2023-10-08 18:14:48 -04:00
|
|
|
alt((
|
2023-10-08 23:41:41 -04:00
|
|
|
verify(map(tag("{}"), |_| true), |_| !entity.name.ends_with(" ")),
|
2023-10-08 18:14:48 -04:00
|
|
|
map(peek(recognize(entity_end)), |_| false),
|
|
|
|
)),
|
2023-09-14 04:27:50 -04:00
|
|
|
))(input);
|
2023-08-29 15:10:27 -04:00
|
|
|
match result {
|
2023-10-08 18:14:48 -04:00
|
|
|
Ok((remaining, (ent, use_brackets))) => {
|
|
|
|
return Ok((remaining, (entity, ent, use_brackets)));
|
2023-08-29 15:10:27 -04:00
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 20:23:27 -04:00
|
|
|
|
2023-08-29 15:10:27 -04:00
|
|
|
Err(nom::Err::Error(CustomError::MyError(MyError(
|
|
|
|
"NoEntity".into(),
|
|
|
|
))))
|
2023-07-18 20:23:27 -04:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:04:59 -04:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-29 15:10:27 -04:00
|
|
|
fn entity_end<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
2023-07-18 20:23:27 -04:00
|
|
|
let (remaining, _) = alt((eof, recognize(satisfy(|c| !c.is_alphabetic()))))(input)?;
|
|
|
|
|
|
|
|
Ok((remaining, ()))
|
|
|
|
}
|