Implement iterator for context.

This commit is contained in:
Tom Alexander
2023-09-02 20:46:17 -04:00
parent 22e9bc991f
commit 0d728510d7
9 changed files with 102 additions and 39 deletions

View File

@@ -399,10 +399,9 @@ fn time_range_rest_end<'r, 's>(
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
// We pop off the most recent context element to get a context tree with just the active/inactive_time_rest_end exit matcher (removing this function from the exit matcher chain) because the 2nd time in the range does not end when a "-TIME" pattern is found.
let parent_node = context.iter().next().expect("Two context elements are added to the tree when adding this exit matcher, so it should be impossible for this to return None.");
let parent_tree = ContextTree::branch_from(parent_node);
let parent_node = context.get_parent().expect("Two context elements are added to the tree when adding this exit matcher, so it should be impossible for this to return None.");
let exit_contents =
recognize(tuple((tag("-"), parser_with_context!(time)(&parent_tree))))(input);
recognize(tuple((tag("-"), parser_with_context!(time)(&parent_node))))(input);
exit_contents
}

View File

@@ -1,10 +1,14 @@
use std::collections::VecDeque;
use super::Element;
use super::Object;
use super::PlainListItem;
use super::TableCell;
use super::TableRow;
use crate::types::Document;
use crate::types::DocumentElement;
use crate::types::Element;
use crate::types::Heading;
use crate::types::Object;
use crate::types::PlainListItem;
use crate::types::Section;
use crate::types::TableCell;
use crate::types::TableRow;
pub enum Token<'r, 's> {
Document(&'r Document<'s>),

View File

@@ -14,7 +14,9 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::Context;
use crate::context::parser_with_context;
use crate::context::ContextElement;
use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::MyError;
use crate::error::Res;
@@ -35,7 +37,10 @@ pub fn in_section<'r, 's, 'x>(context: RefContext<'r, 's>, section_name: &'x str
}
/// Checks if we are currently an immediate child of the given section type
pub fn immediate_in_section<'r, 's, 'x>(context: RefContext<'r, 's>, section_name: &'x str) -> bool {
pub fn immediate_in_section<'r, 's, 'x>(
context: RefContext<'r, 's>,
section_name: &'x str,
) -> bool {
for thing in context.iter() {
match thing.get_data() {
ContextElement::Context(name) if *name == section_name => return true,