2023-07-19 00:23:27 +00:00
|
|
|
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::space0;
|
|
|
|
use nom::combinator::eof;
|
|
|
|
use nom::combinator::peek;
|
|
|
|
use nom::combinator::recognize;
|
|
|
|
|
2023-08-23 04:30:26 +00:00
|
|
|
use super::org_source::OrgSource;
|
2023-07-19 00:23:27 +00:00
|
|
|
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::get_consumed;
|
|
|
|
|
2023-08-11 00:04:59 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 04:30:26 +00:00
|
|
|
pub fn entity<'r, 's>(
|
|
|
|
context: Context<'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Entity<'s>> {
|
2023-07-19 00:23:27 +00:00
|
|
|
let (remaining, _) = tag("\\")(input)?;
|
|
|
|
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 {
|
2023-08-23 04:30:26 +00:00
|
|
|
source: source.into(),
|
|
|
|
entity_name: entity_name.into(),
|
2023-07-19 00:23:27 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-08-11 00:04:59 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-23 04:30:26 +00:00
|
|
|
fn name<'r, 's>(
|
2023-08-24 23:43:41 +00:00
|
|
|
_context: Context<'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
2023-07-19 00:23:27 +00:00
|
|
|
// TODO: This should be defined by org-entities and optionally org-entities-user
|
|
|
|
|
|
|
|
// TODO: Add the rest of the entities, this is a very incomplete list
|
2023-08-25 08:28:24 +00:00
|
|
|
let (remaining, proto) = alt((alt((
|
|
|
|
tag_no_case("delta"),
|
|
|
|
tag_no_case("pi"),
|
|
|
|
tag_no_case("ast"),
|
2023-08-28 02:15:23 +00:00
|
|
|
tag_no_case("lt"),
|
|
|
|
tag_no_case("gt"),
|
2023-08-25 08:28:24 +00:00
|
|
|
)),))(input)?;
|
2023-07-19 00:23:27 +00:00
|
|
|
Ok((remaining, proto))
|
|
|
|
}
|
|
|
|
|
2023-08-11 00:04:59 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-08-24 23:43:41 +00:00
|
|
|
fn entity_end<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
2023-07-19 00:23:27 +00:00
|
|
|
let (remaining, _) = alt((eof, recognize(satisfy(|c| !c.is_alphabetic()))))(input)?;
|
|
|
|
|
|
|
|
Ok((remaining, ()))
|
|
|
|
}
|