61 lines
2.1 KiB
Rust
61 lines
2.1 KiB
Rust
|
use crate::parser::parser_with_context::parser_with_context;
|
||
|
|
||
|
use super::combinator::context_many_till;
|
||
|
use super::error::CustomError;
|
||
|
use super::error::MyError;
|
||
|
use super::parser_context::ChainBehavior;
|
||
|
use super::parser_context::ContextElement;
|
||
|
use super::parser_context::ExitMatcherNode;
|
||
|
use super::text::symbol;
|
||
|
use super::text::Link;
|
||
|
use super::text::Res;
|
||
|
use super::text::TextElement;
|
||
|
use super::text_element_parser::_in_section;
|
||
|
use super::text_element_parser::flat_text_element;
|
||
|
use super::Context;
|
||
|
use nom::combinator::map;
|
||
|
use nom::combinator::recognize;
|
||
|
use nom::sequence::tuple;
|
||
|
|
||
|
pub fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link<'s>> {
|
||
|
let link_start = parser_with_context!(context_link_start)(&context);
|
||
|
let parser_context = context
|
||
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||
|
exit_matcher: ChainBehavior::AndParent(Some(&context_link_end)),
|
||
|
}))
|
||
|
.with_additional_node(ContextElement::Context("link"));
|
||
|
let (remaining, captured) = recognize(tuple((link_start, |i| {
|
||
|
context_many_till(&parser_context, flat_text_element, context_link_end)(i)
|
||
|
})))(i)?;
|
||
|
let ret = Link { contents: captured };
|
||
|
Ok((remaining, ret))
|
||
|
}
|
||
|
|
||
|
fn can_start_link<'s, 'r>(context: Context<'r, 's>) -> bool {
|
||
|
!_in_section(context, "link")
|
||
|
}
|
||
|
|
||
|
fn context_link_start<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||
|
if can_start_link(context) {
|
||
|
recognize(link_start)(input)
|
||
|
} else {
|
||
|
// TODO: Make this a specific error instead of just a generic MyError
|
||
|
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||
|
"Cannot start link",
|
||
|
))));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn context_link_end<'s, 'r>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||
|
let (remaining, actual_match) = recognize(link_end)(input)?;
|
||
|
Ok((remaining, actual_match))
|
||
|
}
|
||
|
|
||
|
fn link_start(input: &str) -> Res<&str, TextElement> {
|
||
|
map(symbol("["), TextElement::Symbol)(input)
|
||
|
}
|
||
|
|
||
|
fn link_end(input: &str) -> Res<&str, TextElement> {
|
||
|
map(symbol("]"), TextElement::Symbol)(input)
|
||
|
}
|