86 lines
2.6 KiB
Rust
86 lines
2.6 KiB
Rust
use nom::branch::alt;
|
|
use nom::bytes::complete::tag;
|
|
use nom::character::complete::anychar;
|
|
use nom::combinator::map;
|
|
use nom::combinator::peek;
|
|
use nom::combinator::recognize;
|
|
use nom::combinator::verify;
|
|
use nom::multi::many_till;
|
|
|
|
use super::object::PlainText;
|
|
use super::org_source::OrgSource;
|
|
use super::radio_link::RematchObject;
|
|
use super::Context;
|
|
use super::Object;
|
|
use crate::error::Res;
|
|
use crate::parser::object_parser::any_object_except_plain_text;
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
use crate::parser::util::exit_matcher_parser;
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
pub fn plain_text<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, PlainText<'s>> {
|
|
let (remaining, source) = recognize(verify(
|
|
many_till(
|
|
anychar,
|
|
peek(alt((
|
|
parser_with_context!(exit_matcher_parser)(context),
|
|
parser_with_context!(plain_text_end)(context),
|
|
))),
|
|
),
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
))(input)?;
|
|
|
|
Ok((
|
|
remaining,
|
|
PlainText {
|
|
source: source.into(),
|
|
},
|
|
))
|
|
}
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn plain_text_end<'r, 's>(
|
|
context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
|
recognize(parser_with_context!(any_object_except_plain_text)(context))(input)
|
|
}
|
|
|
|
impl<'x> RematchObject<'x> for PlainText<'x> {
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
|
fn rematch_object<'r, 's>(
|
|
&'x self,
|
|
_context: Context<'r, 's>,
|
|
input: OrgSource<'s>,
|
|
) -> Res<OrgSource<'s>, Object<'s>> {
|
|
map(tag(self.source), |s| {
|
|
Object::PlainText(PlainText {
|
|
source: Into::<&str>::into(s),
|
|
})
|
|
})(input)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use nom::combinator::map;
|
|
|
|
use super::*;
|
|
use crate::parser::parser_context::ContextTree;
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
use crate::parser::source::Source;
|
|
|
|
#[test]
|
|
fn plain_text_simple() {
|
|
let input = OrgSource::new("foobarbaz");
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
let plain_text_matcher = parser_with_context!(plain_text)(&initial_context);
|
|
let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap();
|
|
assert_eq!(Into::<&str>::into(remaining), "");
|
|
assert_eq!(result.get_source(), Into::<&str>::into(input));
|
|
}
|
|
}
|