use nom::bytes::complete::tag; use nom::character::complete::line_ending; use nom::character::complete::one_of; use nom::combinator::recognize; use nom::multi::many0; use super::Context; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::util::get_consumed; use crate::parser::util::get_current_line_before_position; use crate::parser::util::get_one_before; use crate::parser::LineBreak; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn line_break<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, LineBreak<'s>> { let (remaining, _) = pre(context, input)?; let (remaining, _) = tag(r#"\\"#)(remaining)?; let (remaining, _) = recognize(many0(one_of(" \t")))(remaining)?; let (remaining, _) = line_ending(remaining)?; let source = get_consumed(input, remaining); Ok((remaining, LineBreak { source })) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) .flatten(); match preceding_character { // If None, we are at the start of the file None | Some('\\') => { return Err(nom::Err::Error(CustomError::MyError(MyError( "Not a valid pre character for line break.", )))); } _ => {} }; let current_line = get_current_line_before_position(document_root, input); match current_line { Some(line) => { let is_non_empty_line = line.chars().any(|c| !c.is_whitespace()); if !is_non_empty_line { return Err(nom::Err::Error(CustomError::MyError(MyError( "Not a valid pre line for line break.", )))); } } None => { return Err(nom::Err::Error(CustomError::MyError(MyError( "No preceding line for line break.", )))); } } Ok((input, ())) }