From f3c1b47a8a5fd3f84789068c8696837ab7ac11c7 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Apr 2023 21:19:49 -0400 Subject: [PATCH] First attempt at table parser. Table row not yet implemented. --- src/parser/element.rs | 5 +-- src/parser/greater_element.rs | 6 ++++ src/parser/table.rs | 63 ++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/parser/element.rs b/src/parser/element.rs index 9475c40..9f9f97f 100644 --- a/src/parser/element.rs +++ b/src/parser/element.rs @@ -19,7 +19,8 @@ use super::Context; use super::Drawer; use super::PlainListItem; use crate::parser::parser_with_context::parser_with_context; -use crate::parser::table::table; +use crate::parser::table; +use crate::parser::table::org_mode_table; use nom::branch::alt; use nom::combinator::map; @@ -133,7 +134,7 @@ pub fn non_paragraph_element<'r, 's>( let footnote_definition_matcher = parser_with_context!(footnote_definition)(context); let comment_matcher = parser_with_context!(comment)(context); let drawer_matcher = parser_with_context!(drawer)(context); - let table_matcher = parser_with_context!(table)(context); + let table_matcher = parser_with_context!(org_mode_table)(context); alt(( map(plain_list_matcher, Element::PlainList), map(greater_block_matcher, Element::GreaterBlock), diff --git a/src/parser/greater_element.rs b/src/parser/greater_element.rs index 96ee656..3054f38 100644 --- a/src/parser/greater_element.rs +++ b/src/parser/greater_element.rs @@ -59,4 +59,10 @@ pub struct NodeProperty<'s> { #[derive(Debug)] pub struct Table<'s> { pub source: &'s str, + pub children: Vec>, +} + +#[derive(Debug)] +pub struct TableRow<'s> { + pub source: &'s str, } diff --git a/src/parser/table.rs b/src/parser/table.rs index 05cde93..d83a4bf 100644 --- a/src/parser/table.rs +++ b/src/parser/table.rs @@ -1,8 +1,69 @@ +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 table<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Table<'s>> { +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)?; + + 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!() }