Eliminate the document root context element.

This commit is contained in:
Tom Alexander 2023-08-24 17:01:12 -04:00
parent 720afa5d32
commit 3348807a05
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 6 additions and 29 deletions

View File

@ -203,24 +203,21 @@ fn global_suffix_end<'r, 's>(
mod tests { mod tests {
use super::*; use super::*;
use crate::parser::element_parser::element; use crate::parser::element_parser::element;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ContextTree; use crate::parser::parser_context::ContextTree;
use crate::parser::parser_with_context::parser_with_context; use crate::parser::parser_with_context::parser_with_context;
use crate::parser::source::Source; use crate::parser::source::Source;
#[test] #[test]
fn citation_simple() { fn citation_simple() {
let input = "[cite:@foo]"; let input = OrgSource::new("[cite:@foo]");
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
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, crate::parser::Element::Paragraph(paragraph) => paragraph,
_ => panic!("Should be a paragraph!"), _ => panic!("Should be a paragraph!"),
}; };
assert_eq!(remaining, ""); assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(first_paragraph.get_source(), "[cite:@foo]"); assert_eq!(first_paragraph.get_source(), "[cite:@foo]");
assert_eq!(first_paragraph.children.len(), 1); assert_eq!(first_paragraph.children.len(), 1);
assert_eq!( assert_eq!(

View File

@ -98,9 +98,7 @@ impl<'s> Source<'s> for Heading<'s> {
pub fn document(input: &str) -> Res<&str, Document> { pub fn document(input: &str) -> Res<&str, Document> {
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let wrapped_input = OrgSource::new(input); let wrapped_input = OrgSource::new(input);
let document_context = let (remaining, document) = _document(&initial_context, wrapped_input)
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let (remaining, document) = _document(&document_context, wrapped_input)
.map(|(rem, out)| (Into::<&str>::into(rem), out)) .map(|(rem, out)| (Into::<&str>::into(rem), out))
.map_err(convert_error)?; .map_err(convert_error)?;
{ {
@ -118,9 +116,9 @@ pub fn document(input: &str) -> Res<&str, Document> {
.map(|rt| &rt.children) .map(|rt| &rt.children)
.collect(); .collect();
if !all_radio_targets.is_empty() { if !all_radio_targets.is_empty() {
let document_context = document_context let initial_context = initial_context
.with_additional_node(ContextElement::RadioTarget(all_radio_targets)); .with_additional_node(ContextElement::RadioTarget(all_radio_targets));
let (remaining, document) = _document(&document_context, wrapped_input) let (remaining, document) = _document(&initial_context, wrapped_input)
.map(|(rem, out)| (Into::<&str>::into(rem), out)) .map(|(rem, out)| (Into::<&str>::into(rem), out))
.map_err(convert_error)?; .map_err(convert_error)?;
return Ok((remaining.into(), document)); return Ok((remaining.into(), document));

View File

@ -85,19 +85,6 @@ impl<'r, 's> ContextTree<'r, 's> {
)))); ))));
} }
pub fn get_document_root(&self) -> Option<&'s str> {
for current_node in self.iter() {
let context_element = current_node.get_data();
match context_element {
ContextElement::DocumentRoot(body) => {
return Some(body);
}
_ => {}
}
}
None
}
/// Indicates if elements should consume the whitespace after them. /// Indicates if elements should consume the whitespace after them.
/// ///
/// Defaults to true. /// Defaults to true.
@ -121,11 +108,6 @@ impl<'r, 's> ContextTree<'r, 's> {
#[derive(Debug)] #[derive(Debug)]
pub enum ContextElement<'r, 's> { pub enum ContextElement<'r, 's> {
/// Stores a reference to the entire org-mode document being parsed.
///
/// This is used for look-behind.
DocumentRoot(&'s str),
/// Stores a parser that indicates that children should exit upon matching an exit matcher. /// Stores a parser that indicates that children should exit upon matching an exit matcher.
ExitMatcherNode(ExitMatcherNode<'r>), ExitMatcherNode(ExitMatcherNode<'r>),
Context(&'r str), Context(&'r str),