From 9e4bf553d3681a359e1d2f41eeaf8cefe6462fb1 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 15 Apr 2023 17:56:07 -0400 Subject: [PATCH] First attempt at implementing drawers. --- org_mode_samples/drawer/Makefile | 23 ++++++++++ org_mode_samples/drawer/simple.org | 9 ++++ src/parser/drawer.rs | 70 +++++++++++++++++++++++++++++- src/parser/greater_element.rs | 2 +- 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 org_mode_samples/drawer/Makefile create mode 100644 org_mode_samples/drawer/simple.org diff --git a/org_mode_samples/drawer/Makefile b/org_mode_samples/drawer/Makefile new file mode 100644 index 0000000..c47a86c --- /dev/null +++ b/org_mode_samples/drawer/Makefile @@ -0,0 +1,23 @@ +SHELL := bash +.ONESHELL: +.SHELLFLAGS := -eu -o pipefail -c +.DELETE_ON_ERROR: +MAKEFLAGS += --warn-undefined-variables +MAKEFLAGS += --no-builtin-rules +SRCFILES := $(wildcard *.org) +OUTFILES := $(patsubst %.org,%.tree.txt,$(SRCFILES)) + +ifeq ($(origin .RECIPEPREFIX), undefined) + $(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later) +endif +.RECIPEPREFIX = > + +.PHONY: all +all: $(OUTFILES) + +.PHONY: clean +clean: +> rm -rf $(OUTFILES) + +%.tree.txt: %.org ../common.el ../dump_org_ast.bash +> ../dump_org_ast.bash $< $@ diff --git a/org_mode_samples/drawer/simple.org b/org_mode_samples/drawer/simple.org new file mode 100644 index 0000000..50adaa3 --- /dev/null +++ b/org_mode_samples/drawer/simple.org @@ -0,0 +1,9 @@ +* Headline +before +:candle: +inside + +the drawer +:end: + +after diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index 1443521..2c417e8 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -1,8 +1,76 @@ +use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::bytes::complete::take_while; +use nom::character::complete::line_ending; +use nom::character::complete::space0; +use nom::combinator::eof; +use nom::combinator::recognize; +use nom::multi::many_till; +use nom::sequence::tuple; + use super::Context; +use crate::parser::element::element; use crate::parser::error::Res; +use crate::parser::parser_context::ChainBehavior; +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; +use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; use crate::parser::Drawer; #[tracing::instrument(ret, level = "debug")] pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> { - todo!() + start_of_line(context, input)?; + let (remaining, _leading_whitespace) = space0(input)?; + let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple(( + tag(":"), + name, + tag(":"), + recognize(tuple((space0, line_ending))), + ))(remaining)?; + + let parser_context = context + .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) + .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + exit_matcher: ChainBehavior::AndParent(Some(&drawer_end)), + })); + + let element_matcher = parser_with_context!(element)(&parser_context); + let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); + let (remaining, (children, _exit_contents)) = + many_till(element_matcher, exit_matcher)(remaining)?; + let (remaining, _end) = drawer_end(&parser_context, remaining)?; + + let (remaining, _trailing_ws) = + maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; + let source = get_consumed(input, remaining); + + Ok(( + remaining, + Drawer { + source, + name: drawer_name, + children, + }, + )) +} + +#[tracing::instrument(ret, level = "debug")] +fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { + take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c))(input) +} + +#[tracing::instrument(ret, level = "debug")] +fn drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { + start_of_line(context, input)?; + recognize(tuple(( + space0, + tag(":end:"), + space0, + alt((line_ending, eof)), + )))(input) } diff --git a/src/parser/greater_element.rs b/src/parser/greater_element.rs index 2d1f92a..c9e5975 100644 --- a/src/parser/greater_element.rs +++ b/src/parser/greater_element.rs @@ -32,6 +32,6 @@ pub struct FootnoteDefinition<'s> { #[derive(Debug)] pub struct Drawer<'s> { pub source: &'s str, - pub label: &'s str, + pub name: &'s str, pub children: Vec>, }