use nom::bytes::complete::tag; use nom::character::complete::space0; use nom::combinator::not; use nom::combinator::peek; use nom::combinator::recognize; use nom::multi::many_till; use nom::sequence::tuple; use super::Context; use crate::parser::error::Res; use crate::parser::exiting::ExitClass; use crate::parser::greater_element::TableRow; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; use crate::parser::Table; /// Parse an org-mode-style table /// /// This is not the table.el style. #[tracing::instrument(ret, level = "debug")] pub fn org_mode_table<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Table<'s>> { start_of_line(context, input)?; peek(tuple((space0, tag("|"))))(input)?; let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) .with_additional_node(ContextElement::Context("table")) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &table_end, })); let org_mode_table_row_matcher = parser_with_context!(org_mode_table_row)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let (remaining, (children, _exit_contents)) = many_till(org_mode_table_row_matcher, exit_matcher)(input)?; // TODO: Consume trailing formulas let (remaining, _trailing_ws) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, Table { source, children } )) } #[tracing::instrument(ret, level = "debug")] fn table_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { start_of_line(context, input)?; recognize(tuple((space0, not(tag("|")))))(input) } #[tracing::instrument(ret, level = "debug")] pub fn org_mode_table_row<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, TableRow<'s>> { todo!() }