organic/src/parser/table.rs

90 lines
3.1 KiB
Rust
Raw Normal View History

2023-04-20 01:28:35 +00:00
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
2023-04-20 01:28:35 +00:00
use nom::character::complete::line_ending;
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;
2023-04-20 00:59:58 +00:00
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;
2023-04-20 00:59:58 +00:00
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)?;
2023-04-20 01:21:33 +00:00
// TODO: Consume trailing formulas
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
2023-04-20 01:28:35 +00:00
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)
}
2023-04-20 00:59:58 +00:00
#[tracing::instrument(ret, level = "debug")]
pub fn org_mode_table_row<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
2023-04-20 01:28:35 +00:00
) -> Res<&'s str, TableRow<'s>> {
alt((
parser_with_context!(org_mode_table_row_rule)(context),
parser_with_context!(org_mode_table_row_regular)(context),
))(input)
}
#[tracing::instrument(ret, level = "debug")]
pub fn org_mode_table_row_rule<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, TableRow<'s>> {
start_of_line(context, input)?;
let (remaining, _) = tuple((space0, tag("|-"), is_not("\r\n"), line_ending))(input)?;
let source = get_consumed(input, remaining);
Ok((remaining, TableRow { source }))
}
#[tracing::instrument(ret, level = "debug")]
pub fn org_mode_table_row_regular<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, TableRow<'s>> {
2023-04-20 00:59:58 +00:00
todo!()
}