organic/src/parser/table.rs

179 lines
5.9 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::eof;
use nom::combinator::not;
use nom::combinator::peek;
use nom::combinator::recognize;
2023-04-20 02:09:53 +00:00
use nom::combinator::verify;
2023-09-09 02:47:07 +00:00
use nom::multi::many0;
2023-04-20 01:57:08 +00:00
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::tuple;
2023-09-09 02:47:07 +00:00
use super::keyword::table_formula_keyword;
2023-09-03 04:27:50 +00:00
use super::object_parser::table_cell_set_object;
use super::org_source::OrgSource;
2023-09-03 04:27:50 +00:00
use super::util::exit_matcher_parser;
use crate::context::parser_with_context;
use crate::context::ContextElement;
use crate::context::ExitClass;
use crate::context::ExitMatcherNode;
use crate::context::RefContext;
use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
2023-09-03 04:27:50 +00:00
use crate::types::Table;
use crate::types::TableCell;
use crate::types::TableRow;
2023-04-20 00:59:58 +00:00
/// Parse an org-mode-style table
///
/// This is not the table.el style.
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 17:11:08 +00:00
fn org_mode_table<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Table<'s>> {
start_of_line(input)?;
peek(tuple((space0, tag("|"))))(input)?;
2023-09-03 04:27:50 +00:00
let contexts = [
ContextElement::ConsumeTrailingWhitespace(true),
ContextElement::Context("table"),
ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Alpha,
exit_matcher: &table_end,
2023-09-03 04:27:50 +00:00
}),
];
2023-09-03 16:28:45 +00:00
let parser_context = context.with_additional_node(&contexts[0]);
let parser_context = parser_context.with_additional_node(&contexts[1]);
let parser_context = parser_context.with_additional_node(&contexts[2]);
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-09-09 03:05:04 +00:00
let (remaining, formulas) =
2023-09-09 02:47:07 +00:00
many0(parser_with_context!(table_formula_keyword)(context))(remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
Table {
source: source.into(),
2023-09-09 03:05:04 +00:00
formulas,
children,
},
))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 17:11:08 +00:00
fn detect_table<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
tuple((start_of_line, space0, tag("|")))(input)?;
Ok((input, ()))
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn table_end<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
start_of_line(input)?;
recognize(tuple((space0, not(tag("|")))))(input)
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 17:11:08 +00:00
fn org_mode_table_row<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, TableRow<'s>> {
2023-04-20 01:28:35 +00:00
alt((
parser_with_context!(org_mode_table_row_rule)(context),
parser_with_context!(org_mode_table_row_regular)(context),
))(input)
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 17:11:08 +00:00
fn org_mode_table_row_rule<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, TableRow<'s>> {
start_of_line(input)?;
2023-04-20 01:28:35 +00:00
let (remaining, _) = tuple((space0, tag("|-"), is_not("\r\n"), line_ending))(input)?;
let source = get_consumed(input, remaining);
2023-04-20 01:57:08 +00:00
Ok((
remaining,
TableRow {
source: source.into(),
2023-04-20 01:57:08 +00:00
children: Vec::new(),
},
))
2023-04-20 01:28:35 +00:00
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 17:11:08 +00:00
fn org_mode_table_row_regular<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, TableRow<'s>> {
start_of_line(input)?;
2023-04-20 01:57:08 +00:00
let (remaining, _) = tuple((space0, tag("|")))(input)?;
let (remaining, children) =
many1(parser_with_context!(org_mode_table_cell)(context))(remaining)?;
let (remaining, _tail) = recognize(tuple((space0, alt((line_ending, eof)))))(remaining)?;
2023-04-20 01:57:08 +00:00
let source = get_consumed(input, remaining);
Ok((
remaining,
TableRow {
source: source.into(),
children,
},
))
2023-04-20 01:57:08 +00:00
}
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
2023-09-11 17:11:08 +00:00
fn org_mode_table_cell<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, TableCell<'s>> {
2023-09-03 04:27:50 +00:00
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &org_mode_table_cell_end,
});
let parser_context = context.with_additional_node(&parser_context);
let table_cell_set_object_matcher =
parser_with_context!(table_cell_set_object)(&parser_context);
2023-04-20 02:09:53 +00:00
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, (children, _exit_contents)) = verify(
many_till(table_cell_set_object_matcher, exit_matcher),
|(children, exit_contents)| {
!children.is_empty() || Into::<&str>::into(exit_contents).ends_with("|")
},
2023-04-20 02:09:53 +00:00
)(input)?;
2023-04-21 19:36:45 +00:00
let (remaining, _tail) = org_mode_table_cell_end(&parser_context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
TableCell {
source: source.into(),
children,
},
))
2023-04-20 00:59:58 +00:00
}
2023-04-20 01:57:08 +00:00
2023-08-11 00:04:59 +00:00
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn org_mode_table_cell_end<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
2023-04-21 19:36:45 +00:00
recognize(tuple((space0, alt((tag("|"), peek(line_ending))))))(input)
2023-04-20 01:57:08 +00:00
}