Fixing more errors.

This commit is contained in:
Tom Alexander 2023-09-03 00:27:50 -04:00
parent 15e8d1ab77
commit b54c6d366c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
7 changed files with 118 additions and 72 deletions

View File

@ -14,16 +14,21 @@ use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::Context; 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::CustomError; use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
use crate::error::Res; use crate::error::Res;
use crate::parser::greater_element::NodeProperty;
use crate::parser::greater_element::PropertyDrawer;
use crate::parser::util::get_consumed; use crate::parser::util::get_consumed;
use crate::parser::util::immediate_in_section; use crate::parser::util::immediate_in_section;
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::parser::util::start_of_line; use crate::parser::util::start_of_line;
use crate::types::NodeProperty;
use crate::types::PropertyDrawer;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn property_drawer<'r, 's>( pub fn property_drawer<'r, 's>(
@ -46,13 +51,18 @@ pub fn property_drawer<'r, 's>(
line_ending, line_ending,
))(input)?; ))(input)?;
let parser_context = context let contexts = [
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) ContextElement::ConsumeTrailingWhitespace(true),
.with_additional_node(ContextElement::Context("property-drawer")) ContextElement::Context("property-drawer"),
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Alpha, class: ExitClass::Alpha,
exit_matcher: &property_drawer_end, exit_matcher: &property_drawer_end,
})); }),
];
let parser_context = context
.with_additional_node(&contexts[0])
.with_additional_node(&contexts[1])
.with_additional_node(&contexts[2]);
let node_property_matcher = parser_with_context!(node_property)(&parser_context); let node_property_matcher = parser_with_context!(node_property)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
@ -135,11 +145,11 @@ fn node_property_name<'r, 's>(
context: RefContext<'_, 'r, 's>, context: RefContext<'_, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> { ) -> Res<OrgSource<'s>, OrgSource<'s>> {
let parser_context = let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Beta,
exit_matcher: &node_property_name_end, exit_matcher: &node_property_name_end,
})); });
let parser_context = context.with_additional_node(&parser_context);
let (remaining, name) = recognize(tuple(( let (remaining, name) = recognize(tuple((
verify( verify(

View File

@ -5,16 +5,22 @@ use nom::character::complete::space0;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many_till; use nom::multi::many_till;
use super::object_parser::minimal_set_object;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::exit_matcher_parser;
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
use super::Context; use crate::context::parser_with_context;
use super::Object; use crate::context::ContextElement;
use crate::context::ExitClass;
use crate::context::ExitMatcherNode;
use crate::context::RefContext;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
use crate::error::Res; use crate::error::Res;
use crate::parser::util::get_consumed; use crate::parser::util::get_consumed;
use crate::parser::RadioLink; use crate::types::Object;
use crate::parser::RadioTarget; use crate::types::RadioLink;
use crate::types::RadioTarget;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn radio_link<'r, 's>( pub fn radio_link<'r, 's>(
@ -23,7 +29,7 @@ pub fn radio_link<'r, 's>(
) -> Res<OrgSource<'s>, RadioLink<'s>> { ) -> Res<OrgSource<'s>, RadioLink<'s>> {
let radio_targets = context let radio_targets = context
.iter() .iter()
.filter_map(|context_element| match context_element.get_data() { .filter_map(|context_element| match context_element {
ContextElement::RadioTarget(targets) => Some(targets), ContextElement::RadioTarget(targets) => Some(targets),
_ => None, _ => None,
}) })
@ -84,11 +90,11 @@ pub fn radio_target<'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, RadioTarget<'s>> { ) -> Res<OrgSource<'s>, RadioTarget<'s>> {
let (remaining, _opening) = tag("<<<")(input)?; let (remaining, _opening) = tag("<<<")(input)?;
let parser_context = let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma, class: ExitClass::Gamma,
exit_matcher: &radio_target_end, exit_matcher: &radio_target_end,
})); });
let parser_context = context.with_additional_node(&parser_context);
let (remaining, (children, _exit_contents)) = verify( let (remaining, (children, _exit_contents)) = verify(
many_till( many_till(
@ -130,25 +136,28 @@ pub trait RematchObject<'x> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::context::Context;
use crate::context::GlobalSettings;
use crate::context::List;
use crate::parser::element_parser::element; use crate::parser::element_parser::element;
use crate::parser::parser_context::ContextElement; use crate::types::Bold;
use crate::parser::parser_context::ContextTree; use crate::types::Element;
use crate::parser::parser_with_context::parser_with_context; use crate::types::PlainText;
use crate::parser::source::Source; use crate::types::Source;
use crate::parser::Bold;
use crate::parser::PlainText;
#[test] #[test]
fn plain_text_radio_target() { fn plain_text_radio_target() {
let input = OrgSource::new("foo bar baz"); let input = OrgSource::new("foo bar baz");
let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })]; let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })];
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let global_settings = GlobalSettings::default();
let document_context = initial_context let initial_context = ContextElement::document_context();
.with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let document_context = ContextElement::RadioTarget(vec![&radio_target_match]);
let document_context = initial_context.with_additional_node(&document_context);
let paragraph_matcher = parser_with_context!(element(true))(&document_context); let paragraph_matcher = parser_with_context!(element(true))(&document_context);
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
let first_paragraph = match first_paragraph { let first_paragraph = match first_paragraph {
crate::parser::Element::Paragraph(paragraph) => paragraph, Element::Paragraph(paragraph) => paragraph,
_ => panic!("Should be a paragraph!"), _ => panic!("Should be a paragraph!"),
}; };
assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(Into::<&str>::into(remaining), "");
@ -173,14 +182,16 @@ mod tests {
source: "*bar*", source: "*bar*",
children: vec![Object::PlainText(PlainText { source: "bar" })], children: vec![Object::PlainText(PlainText { source: "bar" })],
})]; })];
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let global_settings = GlobalSettings::default();
let document_context = initial_context let initial_context = ContextElement::document_context();
.with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); let initial_context = Context::new(&global_settings, List::new(&initial_context));
let document_context = ContextElement::RadioTarget(vec![&radio_target_match]);
let document_context = initial_context.with_additional_node(&document_context);
let paragraph_matcher = parser_with_context!(element(true))(&document_context); let paragraph_matcher = parser_with_context!(element(true))(&document_context);
let (remaining, first_paragraph) = let (remaining, first_paragraph) =
paragraph_matcher(input.into()).expect("Parse first paragraph"); paragraph_matcher(input.into()).expect("Parse first paragraph");
let first_paragraph = match first_paragraph { let first_paragraph = match first_paragraph {
crate::parser::Element::Paragraph(paragraph) => paragraph, Element::Paragraph(paragraph) => paragraph,
_ => panic!("Should be a paragraph!"), _ => panic!("Should be a paragraph!"),
}; };
assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(Into::<&str>::into(remaining), "");

View File

@ -6,13 +6,19 @@ use nom::character::complete::one_of;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many_till; use nom::multi::many_till;
use super::object_parser::regular_link_description_object_set;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::exit_matcher_parser;
use super::util::get_consumed; use super::util::get_consumed;
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
use super::Context; use crate::context::parser_with_context;
use super::Object; use crate::context::ContextElement;
use super::RegularLink; use crate::context::ExitClass;
use crate::context::ExitMatcherNode;
use crate::context::RefContext;
use crate::error::Res; use crate::error::Res;
use crate::types::Object;
use crate::types::RegularLink;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn regular_link<'r, 's>( pub fn regular_link<'r, 's>(
@ -86,11 +92,11 @@ pub fn description<'r, 's>(
context: RefContext<'_, 'r, 's>, context: RefContext<'_, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> { ) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
let parser_context = let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Beta,
exit_matcher: &description_end, exit_matcher: &description_end,
})); });
let parser_context = context.with_additional_node(&parser_context);
let (remaining, (children, _exit_contents)) = verify( let (remaining, (children, _exit_contents)) = verify(
many_till( many_till(
parser_with_context!(regular_link_description_object_set)(&parser_context), parser_with_context!(regular_link_description_object_set)(&parser_context),

View File

@ -10,17 +10,23 @@ use nom::combinator::recognize;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many_till; use nom::multi::many_till;
use super::object_parser::standard_set_object;
use super::org_source::BracketDepth; use super::org_source::BracketDepth;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::util::exit_matcher_parser;
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
use super::Context; use crate::context::parser_with_context;
use super::Object; use crate::context::ContextElement;
use crate::context::ExitClass;
use crate::context::ExitMatcherNode;
use crate::context::RefContext;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
use crate::error::Res; use crate::error::Res;
use crate::parser::util::get_consumed; use crate::parser::util::get_consumed;
use crate::parser::Subscript; use crate::types::Object;
use crate::parser::Superscript; use crate::types::Subscript;
use crate::types::Superscript;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn subscript<'r, 's>( pub fn subscript<'r, 's>(
@ -151,11 +157,11 @@ fn script_with_braces<'r, 's>(
) -> Res<OrgSource<'s>, Vec<Object<'s>>> { ) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
let (remaining, _) = tag("{")(input)?; let (remaining, _) = tag("{")(input)?;
let exit_with_depth = script_with_braces_end(remaining.get_brace_depth()); let exit_with_depth = script_with_braces_end(remaining.get_brace_depth());
let parser_context = let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma, class: ExitClass::Gamma,
exit_matcher: &exit_with_depth, exit_matcher: &exit_with_depth,
})); });
let parser_context = context.with_additional_node(&parser_context);
let (remaining, (children, _exit_contents)) = many_till( let (remaining, (children, _exit_contents)) = many_till(
parser_with_context!(standard_set_object)(&parser_context), parser_with_context!(standard_set_object)(&parser_context),
@ -168,8 +174,9 @@ fn script_with_braces<'r, 's>(
fn script_with_braces_end( fn script_with_braces_end(
starting_brace_depth: BracketDepth, starting_brace_depth: BracketDepth,
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> { ) -> impl for<'b, 'r, 's> Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>
move |context: Context, input: OrgSource<'_>| { {
move |context, input: OrgSource<'_>| {
_script_with_braces_end(context, input, starting_brace_depth) _script_with_braces_end(context, input, starting_brace_depth)
} }
} }

View File

@ -12,14 +12,20 @@ use nom::multi::many1;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::object_parser::table_cell_set_object;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::Context; 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::error::Res;
use crate::parser::greater_element::TableRow;
use crate::parser::lesser_element::TableCell;
use crate::parser::util::get_consumed; use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line; use crate::parser::util::start_of_line;
use crate::parser::Table; use crate::types::Table;
use crate::types::TableCell;
use crate::types::TableRow;
/// Parse an org-mode-style table /// Parse an org-mode-style table
/// ///
@ -32,13 +38,18 @@ pub fn org_mode_table<'r, 's>(
start_of_line(input)?; start_of_line(input)?;
peek(tuple((space0, tag("|"))))(input)?; peek(tuple((space0, tag("|"))))(input)?;
let parser_context = context let contexts = [
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) ContextElement::ConsumeTrailingWhitespace(true),
.with_additional_node(ContextElement::Context("table")) ContextElement::Context("table"),
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Alpha, class: ExitClass::Alpha,
exit_matcher: &table_end, exit_matcher: &table_end,
})); }),
];
let parser_context = context
.with_additional_node(&contexts[0])
.with_additional_node(&contexts[1])
.with_additional_node(&contexts[2]);
let org_mode_table_row_matcher = parser_with_context!(org_mode_table_row)(&parser_context); 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 exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
@ -120,11 +131,11 @@ pub fn org_mode_table_cell<'r, 's>(
context: RefContext<'_, 'r, 's>, context: RefContext<'_, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, TableCell<'s>> { ) -> Res<OrgSource<'s>, TableCell<'s>> {
let parser_context = let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Beta,
exit_matcher: &org_mode_table_cell_end, exit_matcher: &org_mode_table_cell_end,
})); });
let parser_context = context.with_additional_node(&parser_context);
let table_cell_set_object_matcher = let table_cell_set_object_matcher =
parser_with_context!(table_cell_set_object)(&parser_context); parser_with_context!(table_cell_set_object)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);

View File

@ -28,7 +28,7 @@ pub const WORD_CONSTITUENT_CHARACTERS: &str =
#[allow(dead_code)] #[allow(dead_code)]
pub fn in_section<'r, 's, 'x>(context: RefContext<'_, 'r, 's>, section_name: &'x str) -> bool { pub fn in_section<'r, 's, 'x>(context: RefContext<'_, 'r, 's>, section_name: &'x str) -> bool {
for thing in context.iter() { for thing in context.iter() {
match thing.get_data() { match thing {
ContextElement::Context(name) if *name == section_name => return true, ContextElement::Context(name) if *name == section_name => return true,
_ => {} _ => {}
} }
@ -42,7 +42,7 @@ pub fn immediate_in_section<'r, 's, 'x>(
section_name: &'x str, section_name: &'x str,
) -> bool { ) -> bool {
for thing in context.iter() { for thing in context.iter() {
match thing.get_data() { match thing {
ContextElement::Context(name) if *name == section_name => return true, ContextElement::Context(name) if *name == section_name => return true,
ContextElement::Context(name) if *name != section_name => return false, ContextElement::Context(name) if *name != section_name => return false,
_ => {} _ => {}

View File

@ -17,6 +17,7 @@ pub use greater_element::PlainList;
pub use greater_element::PlainListItem; pub use greater_element::PlainListItem;
pub use greater_element::PropertyDrawer; pub use greater_element::PropertyDrawer;
pub use greater_element::Table; pub use greater_element::Table;
pub use greater_element::NodeProperty;
pub use greater_element::TableRow; pub use greater_element::TableRow;
pub use lesser_element::Clock; pub use lesser_element::Clock;
pub use lesser_element::Comment; pub use lesser_element::Comment;