First attempt at table parser. Table row not yet implemented.

This commit is contained in:
Tom Alexander 2023-04-19 21:19:49 -04:00
parent 17ab8727d1
commit f3c1b47a8a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 71 additions and 3 deletions

View File

@ -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),

View File

@ -59,4 +59,10 @@ pub struct NodeProperty<'s> {
#[derive(Debug)]
pub struct Table<'s> {
pub source: &'s str,
pub children: Vec<TableRow<'s>>,
}
#[derive(Debug)]
pub struct TableRow<'s> {
pub source: &'s str,
}

View File

@ -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!()
}