Use a single function for finding all keywords.

This commit is contained in:
Tom Alexander
2023-09-04 19:17:23 -04:00
parent d38e198258
commit 275b4b53d1
7 changed files with 111 additions and 54 deletions

View File

@@ -18,6 +18,7 @@ use nom::multi::many_till;
use nom::multi::separated_list1;
use nom::sequence::tuple;
use super::in_buffer_settings::scan_for_in_buffer_settings;
use super::org_source::OrgSource;
use super::setup_file::scan_for_setup_file;
use super::token::AllTokensIterator;
@@ -92,6 +93,8 @@ pub fn parse_with_settings<'g, 's>(
/// Parse a full org-mode document.
///
/// Use this entry point when you want to have direct control over the starting context or if you want to use this integrated with other nom parsers. For general-purpose usage, the `parse` and `parse_with_settings` functions are a lot simpler.
///
/// This will not prevent additional settings from being learned during parsing, for example when encountering a "#+TODO".
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
#[allow(dead_code)]
pub fn document<'b, 'g, 'r, 's>(
@@ -109,24 +112,42 @@ fn document_org_source<'b, 'g, 'r, 's>(
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Document<'s>> {
let setup_file = scan_for_setup_file(input);
if setup_file.is_ok() {
let setup_file = if setup_file.is_ok() {
let (_remaining, setup_file) = setup_file.expect("If-statement proves this is okay.");
let setup_file_contents = context
.get_global_settings()
.file_access
.read_file(Into::<&str>::into(setup_file))
.map_err(|err| nom::Err::<CustomError<OrgSource<'_>>>::Failure(err.into()))?;
let parsed_setup_file = _document(context, setup_file_contents.as_str().into());
if parsed_setup_file.is_err() {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Failed to parse the setup file.".into(),
))));
}
let (_remaining, parsed_setup_file) =
parsed_setup_file.expect("The if-statement proves this is ok.");
println!("TODO: Process setup_file: {:#?}", parsed_setup_file);
Some(setup_file_contents)
} else {
None
};
let setup_file_settings = setup_file
.as_ref()
.map(|input| input.as_str().into())
.map(scan_for_in_buffer_settings)
.map_or(Ok(None), |r| r.map(Some))
.map_err(|_err| {
nom::Err::Error(CustomError::MyError(MyError(
"TODO: make this take an owned string so I can dump err.to_string() into it."
.into(),
)))
})?;
let (_, document_settings) = scan_for_in_buffer_settings(input)?;
let mut final_settings = Vec::with_capacity(
document_settings.len()
+ match setup_file_settings {
Some((_, ref setup_file_settings)) => setup_file_settings.len(),
None => 0,
},
);
if let Some((_, setup_file_settings)) = setup_file_settings {
final_settings.extend(setup_file_settings.into_iter());
}
// TODO: read the keywords into settings and apply them to the GlobalSettings.
let (remaining, document) =
_document(context, input).map(|(rem, out)| (Into::<&str>::into(rem), out))?;
{