From a8f277efe5834df7143f6aad1d389473637fa455 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 4 Sep 2023 12:48:59 -0400 Subject: [PATCH] Scan for setupfile at the beginning of a parse. --- src/parser/document.rs | 6 ++++++ src/parser/mod.rs | 1 + src/parser/setup_file.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/parser/setup_file.rs diff --git a/src/parser/document.rs b/src/parser/document.rs index 3e9cbe4..4e7084d 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -19,6 +19,7 @@ use nom::multi::separated_list1; use nom::sequence::tuple; use super::org_source::OrgSource; +use super::setup_file::scan_for_setup_file; use super::token::AllTokensIterator; use super::token::Token; use super::util::exit_matcher_parser; @@ -78,6 +79,11 @@ fn document_org_source<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Document<'s>> { + let setup_file = scan_for_setup_file(input); + if setup_file.is_ok() { + let (_remaining, setup_file) = setup_file.expect("If-statement proves this is okay."); + println!("TODO: Process setup_file: {}", setup_file); + } let (remaining, document) = _document(context, input).map(|(rem, out)| (Into::<&str>::into(rem), out))?; { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index eae8a04..124fdb6 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -33,6 +33,7 @@ mod planning; mod property_drawer; mod radio_link; mod regular_link; +mod setup_file; pub mod sexp; mod statistics_cookie; mod subscript_and_superscript; diff --git a/src/parser/setup_file.rs b/src/parser/setup_file.rs new file mode 100644 index 0000000..e422a38 --- /dev/null +++ b/src/parser/setup_file.rs @@ -0,0 +1,31 @@ +use nom::branch::alt; +use nom::bytes::complete::is_not; +use nom::bytes::complete::tag_no_case; +use nom::character::complete::anychar; +use nom::character::complete::line_ending; +use nom::character::complete::space1; +use nom::combinator::eof; +use nom::multi::many_till; +use nom::sequence::tuple; + +use super::OrgSource; +use crate::error::Res; +use crate::parser::util::start_of_line; + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +pub fn scan_for_setup_file<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { + let (remaining, setup) = many_till(anychar, setup_file)(input) + .map(|(remaining, (_, setup_file))| (remaining, setup_file))?; + Ok((remaining, setup)) +} + +fn setup_file<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { + let (remaining, (_, _, _, setup_file, _)) = tuple(( + start_of_line, + tag_no_case("#+SETUPFILE:"), + space1, + is_not("\r\n"), + alt((line_ending, eof)), + ))(input)?; + Ok((remaining, setup_file)) +}