organic/src/parser/document.rs

26 lines
852 B
Rust
Raw Normal View History

2022-07-16 03:26:49 +00:00
//! A single element of text.
use super::combinator::context_many1;
2022-12-18 07:59:41 +00:00
use super::error::Res;
2022-12-18 07:53:26 +00:00
use super::paragraph::paragraph;
2022-12-18 07:16:28 +00:00
use super::parser_context::ContextElement;
use super::parser_context::ContextTree;
use super::text::Paragraph;
2022-12-04 04:53:52 +00:00
use super::token::Token;
use super::Context;
2022-07-16 03:26:49 +00:00
use nom::IResult;
2022-10-15 00:17:48 +00:00
type UnboundMatcher<'r, 's, I, O, E> = dyn Fn(Context<'r, 's>, I) -> IResult<I, O, E>;
pub fn document(input: &str) -> Res<&str, Vec<Paragraph>> {
let initial_context: ContextTree<'_, '_> = ContextTree::new();
let (remaining, tokens) = context_many1(&initial_context, paragraph)(input)?;
let paragraphs = tokens
.into_iter()
.map(|token| match token {
Token::TextElement(_) => unreachable!(),
Token::Paragraph(paragraph) => paragraph,
})
.collect();
Ok((remaining, paragraphs))
}