From 6823db5c600706a418575bb84da4254db9f7bced Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 17:11:45 -0400 Subject: [PATCH 01/19] Initial commit. --- .gitignore | 1 + Cargo.toml | 8 ++++++++ src/main.rs | 3 +++ 3 files changed, 12 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fa58aae --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "nom_custom_input_test" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 2ec055af5ac03a931acaeec08040a948f0c2ab86 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 17:20:01 -0400 Subject: [PATCH 02/19] Very simple setup. --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 12 +++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d849343 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,32 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "nom_custom_input_test" +version = "0.1.0" +dependencies = [ + "nom", +] diff --git a/Cargo.toml b/Cargo.toml index fa58aae..2043c9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +nom = "7.1.3" diff --git a/src/main.rs b/src/main.rs index e7a11a9..63db4e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,13 @@ +use nom::bytes::complete::tag; + fn main() { - println!("Hello, world!"); + let input = "this is my test input".to_owned(); + + let output = tag::<_, _, (_, nom::error::ErrorKind)>("this")(input.as_str()).unwrap(); + + println!("{:#?}", output); +} + +struct WrappedInput<'s> { + contents: &'s str, } From 1e3dadd45866d2e2888ad99ffb231d1d4099e940 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 17:24:26 -0400 Subject: [PATCH 03/19] Wrap the input. --- src/main.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 63db4e1..b6359e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,20 @@ use nom::bytes::complete::tag; fn main() { let input = "this is my test input".to_owned(); + let wrapped_input = WrappedInput::new(input.as_str()); - let output = tag::<_, _, (_, nom::error::ErrorKind)>("this")(input.as_str()).unwrap(); + let output = tag::<_, _, (_, nom::error::ErrorKind)>("this")(wrapped_input).unwrap(); println!("{:#?}", output); } +#[derive(Debug)] struct WrappedInput<'s> { contents: &'s str, } + +impl<'s> WrappedInput<'s> { + pub fn new(input: &'s str) -> Self { + WrappedInput { contents: input } + } +} From e4656cddf602cdb6fffd48e23525db314d582e15 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 17:50:41 -0400 Subject: [PATCH 04/19] Implement slice, take, and compare. --- src/main.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index b6359e2..1ada841 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use nom::bytes::complete::tag; +use std::ops::RangeBounds; + +use nom::{bytes::complete::tag, Compare, InputTake, Slice}; fn main() { let input = "this is my test input".to_owned(); @@ -11,11 +13,86 @@ fn main() { #[derive(Debug)] struct WrappedInput<'s> { - contents: &'s str, + full_source: &'s str, + start: usize, + end: usize, //exclusive + preceding_line_break: Option, } impl<'s> WrappedInput<'s> { + /// Returns a wrapped string that keeps track of values we need for parsing org-mode. + /// + /// Only call this on the full original string. Calling this on a substring can result in invalid values. pub fn new(input: &'s str) -> Self { - WrappedInput { contents: input } + WrappedInput { + full_source: input, + start: 0, + end: input.len(), + preceding_line_break: None, + } + } +} + +impl<'s> InputTake for WrappedInput<'s> { + fn take(&self, count: usize) -> Self { + self.slice(..count) + } + + fn take_split(&self, count: usize) -> (Self, Self) { + (self.slice(count..), self.slice(..count)) + } +} + +impl<'s, O: Into<&'s str>> Compare for WrappedInput<'s> { + fn compare(&self, t: O) -> nom::CompareResult { + (&self.full_source[self.start..self.end]).compare(t.into()) + } + + fn compare_no_case(&self, t: O) -> nom::CompareResult { + (&self.full_source[self.start..self.end]).compare_no_case(t.into()) + } +} + +impl<'s> From<&'s str> for WrappedInput<'s> { + fn from(value: &'s str) -> Self { + WrappedInput::new(value) + } +} + +impl<'s> From<&WrappedInput<'s>> for &'s str { + fn from(value: &WrappedInput<'s>) -> Self { + &value.full_source[value.start..value.end] + } +} + +impl<'s, R> Slice for WrappedInput<'s> +where + R: RangeBounds, +{ + fn slice(&self, range: R) -> Self { + let new_start = match range.start_bound() { + std::ops::Bound::Included(idx) => self.start + idx, + std::ops::Bound::Excluded(idx) => self.start + idx - 1, + std::ops::Bound::Unbounded => self.start, + }; + let new_end = match range.end_bound() { + std::ops::Bound::Included(idx) => self.start + idx, + std::ops::Bound::Excluded(idx) => self.start + idx - 1, + std::ops::Bound::Unbounded => self.start, + }; + + // TODO: calculate updated values for WrappedInput + WrappedInput { + full_source: self.full_source, + start: new_start, + end: new_end, + preceding_line_break: None, + } + } +} + +impl<'s> std::fmt::Display for WrappedInput<'s> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + Into::<&str>::into(self).fmt(f) } } From bc29f1dfc0e60f08f4bafb8ac105f2aa7ce3a8b2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 21:38:50 -0400 Subject: [PATCH 05/19] Add slicing tests. --- src/main.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1ada841..4ed11e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,10 +76,16 @@ where std::ops::Bound::Unbounded => self.start, }; let new_end = match range.end_bound() { - std::ops::Bound::Included(idx) => self.start + idx, - std::ops::Bound::Excluded(idx) => self.start + idx - 1, - std::ops::Bound::Unbounded => self.start, + std::ops::Bound::Included(idx) => self.start + idx + 1, + std::ops::Bound::Excluded(idx) => self.start + idx, + std::ops::Bound::Unbounded => self.end, }; + if new_start < self.start { + panic!("Attempted to extend before the start of the WrappedInput.") + } + if new_end > self.end { + panic!("Attempted to extend past the end of the WrappedInput.") + } // TODO: calculate updated values for WrappedInput WrappedInput { @@ -96,3 +102,60 @@ impl<'s> std::fmt::Display for WrappedInput<'s> { Into::<&str>::into(self).fmt(f) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn range() { + let input = WrappedInput::new("foo bar baz"); + let output = input.slice(4..7); + assert_eq!(output.to_string(), "bar"); + } + + #[test] + fn range_to() { + let input = WrappedInput::new("foo bar baz"); + let output = input.slice(..7); + assert_eq!(output.to_string(), "foo bar"); + } + + #[test] + fn range_from() { + let input = WrappedInput::new("foo bar baz"); + let output = input.slice(4..); + assert_eq!(output.to_string(), "bar baz"); + } + + #[test] + fn full_range() { + let input = WrappedInput::new("foo bar baz"); + let output = input.slice(..); + assert_eq!(output.to_string(), "foo bar baz"); + } + + #[test] + fn nested_range() { + let input = WrappedInput::new("lorem foo bar baz ipsum"); + let first_cut = input.slice(6..17); + let output = first_cut.slice(4..7); + assert_eq!(first_cut.to_string(), "foo bar baz"); + assert_eq!(output.to_string(), "bar"); + } + + #[test] + #[should_panic] + fn out_of_bounds() { + let input = WrappedInput::new("lorem foo bar baz ipsum"); + input.slice(6..30); + } + + #[test] + #[should_panic] + fn out_of_nested_bounds() { + let input = WrappedInput::new("lorem foo bar baz ipsum"); + let first_cut = input.slice(6..17); + first_cut.slice(4..14); + } +} From edff1e089d61027d6deb3075822343cddb13cc09 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 22:18:44 -0400 Subject: [PATCH 06/19] Implement text since line break. --- src/main.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4ed11e4..7846d6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,12 @@ impl<'s> WrappedInput<'s> { preceding_line_break: None, } } + + /// Get the text since the line break preceding the start of this WrappedInput. + pub fn text_since_line_break(&self) -> &'s str { + let start = self.preceding_line_break.unwrap_or(0); + &self.full_source[start..self.start] + } } impl<'s> InputTake for WrappedInput<'s> { @@ -87,12 +93,17 @@ where panic!("Attempted to extend past the end of the WrappedInput.") } + let skipped_text = &self.full_source[self.start..new_start]; + let last_line_feed = skipped_text + .rfind('\n') + .map(|idx| idx + self.preceding_line_break.unwrap_or(0) + 1); + // TODO: calculate updated values for WrappedInput WrappedInput { full_source: self.full_source, start: new_start, end: new_end, - preceding_line_break: None, + preceding_line_break: last_line_feed, } } } @@ -158,4 +169,21 @@ mod tests { let first_cut = input.slice(6..17); first_cut.slice(4..14); } + + #[test] + fn line_break() { + let input = WrappedInput::new("lorem\nfoo\nbar\nbaz\nipsum"); + assert_eq!(input.slice(5..).preceding_line_break, None); + assert_eq!(input.slice(6..).preceding_line_break, Some(6)); + assert_eq!(input.slice(6..).slice(10..).preceding_line_break, Some(14)); + } + + #[test] + fn text_since_line_break() { + let input = WrappedInput::new("lorem\nfoo\nbar\nbaz\nipsum"); + assert_eq!(input.text_since_line_break(), ""); + assert_eq!(input.slice(5..).text_since_line_break(), "lorem"); + assert_eq!(input.slice(6..).text_since_line_break(), ""); + assert_eq!(input.slice(6..).slice(10..).text_since_line_break(), "ba"); + } } From 5a7f34b63eddabb08cab225119a1e38c260dc151 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 22:24:35 -0400 Subject: [PATCH 07/19] Prepare for merging into Organic. --- .gitignore | 1 - Cargo.lock | 32 ------------------------- Cargo.toml | 9 ------- src/{main.rs => input/wrapped_input.rs} | 11 +-------- 4 files changed, 1 insertion(+), 52 deletions(-) delete mode 100644 .gitignore delete mode 100644 Cargo.lock delete mode 100644 Cargo.toml rename src/{main.rs => input/wrapped_input.rs} (94%) diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index d849343..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,32 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - -[[package]] -name = "nom_custom_input_test" -version = "0.1.0" -dependencies = [ - "nom", -] diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 2043c9c..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "nom_custom_input_test" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -nom = "7.1.3" diff --git a/src/main.rs b/src/input/wrapped_input.rs similarity index 94% rename from src/main.rs rename to src/input/wrapped_input.rs index 7846d6b..5495fe7 100644 --- a/src/main.rs +++ b/src/input/wrapped_input.rs @@ -1,15 +1,6 @@ use std::ops::RangeBounds; -use nom::{bytes::complete::tag, Compare, InputTake, Slice}; - -fn main() { - let input = "this is my test input".to_owned(); - let wrapped_input = WrappedInput::new(input.as_str()); - - let output = tag::<_, _, (_, nom::error::ErrorKind)>("this")(wrapped_input).unwrap(); - - println!("{:#?}", output); -} +use nom::{Compare, InputTake, Slice}; #[derive(Debug)] struct WrappedInput<'s> { From cda49c628c6089c50bed33b6c02877b77d01f2c6 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 22:33:50 -0400 Subject: [PATCH 08/19] Move the wrapped input into the parser. --- src/parser/document.rs | 2 + src/parser/mod.rs | 1 + .../wrapped_input.rs => parser/org_source.rs} | 46 ++++++++++--------- 3 files changed, 27 insertions(+), 22 deletions(-) rename src/{input/wrapped_input.rs => parser/org_source.rs} (80%) diff --git a/src/parser/document.rs b/src/parser/document.rs index d4a129f..43c176e 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -16,6 +16,7 @@ use nom::sequence::tuple; use super::element::Element; use super::object::Object; +use super::org_source::OrgSource; use super::parser_with_context::parser_with_context; use super::source::Source; use super::token::AllTokensIterator; @@ -95,6 +96,7 @@ impl<'s> Source<'s> for Heading<'s> { #[allow(dead_code)] pub fn document(input: &str) -> Res<&str, Document> { let initial_context: ContextTree<'_, '_> = ContextTree::new(); + let wrapped_input = OrgSource::new(input); let document_context = initial_context.with_additional_node(ContextElement::DocumentRoot(input)); let (remaining, document) = _document(&document_context, input)?; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 62dd249..639af9a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -30,6 +30,7 @@ mod list; mod object; mod object_parser; mod org_macro; +mod org_source; mod paragraph; mod parser_context; mod parser_with_context; diff --git a/src/input/wrapped_input.rs b/src/parser/org_source.rs similarity index 80% rename from src/input/wrapped_input.rs rename to src/parser/org_source.rs index 5495fe7..8afd74b 100644 --- a/src/input/wrapped_input.rs +++ b/src/parser/org_source.rs @@ -1,21 +1,23 @@ use std::ops::RangeBounds; -use nom::{Compare, InputTake, Slice}; +use nom::Compare; +use nom::InputTake; +use nom::Slice; #[derive(Debug)] -struct WrappedInput<'s> { +pub struct OrgSource<'s> { full_source: &'s str, start: usize, end: usize, //exclusive preceding_line_break: Option, } -impl<'s> WrappedInput<'s> { +impl<'s> OrgSource<'s> { /// Returns a wrapped string that keeps track of values we need for parsing org-mode. /// /// Only call this on the full original string. Calling this on a substring can result in invalid values. pub fn new(input: &'s str) -> Self { - WrappedInput { + OrgSource { full_source: input, start: 0, end: input.len(), @@ -30,7 +32,7 @@ impl<'s> WrappedInput<'s> { } } -impl<'s> InputTake for WrappedInput<'s> { +impl<'s> InputTake for OrgSource<'s> { fn take(&self, count: usize) -> Self { self.slice(..count) } @@ -40,7 +42,7 @@ impl<'s> InputTake for WrappedInput<'s> { } } -impl<'s, O: Into<&'s str>> Compare for WrappedInput<'s> { +impl<'s, O: Into<&'s str>> Compare for OrgSource<'s> { fn compare(&self, t: O) -> nom::CompareResult { (&self.full_source[self.start..self.end]).compare(t.into()) } @@ -50,19 +52,19 @@ impl<'s, O: Into<&'s str>> Compare for WrappedInput<'s> { } } -impl<'s> From<&'s str> for WrappedInput<'s> { +impl<'s> From<&'s str> for OrgSource<'s> { fn from(value: &'s str) -> Self { - WrappedInput::new(value) + OrgSource::new(value) } } -impl<'s> From<&WrappedInput<'s>> for &'s str { - fn from(value: &WrappedInput<'s>) -> Self { +impl<'s> From<&OrgSource<'s>> for &'s str { + fn from(value: &OrgSource<'s>) -> Self { &value.full_source[value.start..value.end] } } -impl<'s, R> Slice for WrappedInput<'s> +impl<'s, R> Slice for OrgSource<'s> where R: RangeBounds, { @@ -90,7 +92,7 @@ where .map(|idx| idx + self.preceding_line_break.unwrap_or(0) + 1); // TODO: calculate updated values for WrappedInput - WrappedInput { + OrgSource { full_source: self.full_source, start: new_start, end: new_end, @@ -99,7 +101,7 @@ where } } -impl<'s> std::fmt::Display for WrappedInput<'s> { +impl<'s> std::fmt::Display for OrgSource<'s> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { Into::<&str>::into(self).fmt(f) } @@ -111,35 +113,35 @@ mod tests { #[test] fn range() { - let input = WrappedInput::new("foo bar baz"); + let input = OrgSource::new("foo bar baz"); let output = input.slice(4..7); assert_eq!(output.to_string(), "bar"); } #[test] fn range_to() { - let input = WrappedInput::new("foo bar baz"); + let input = OrgSource::new("foo bar baz"); let output = input.slice(..7); assert_eq!(output.to_string(), "foo bar"); } #[test] fn range_from() { - let input = WrappedInput::new("foo bar baz"); + let input = OrgSource::new("foo bar baz"); let output = input.slice(4..); assert_eq!(output.to_string(), "bar baz"); } #[test] fn full_range() { - let input = WrappedInput::new("foo bar baz"); + let input = OrgSource::new("foo bar baz"); let output = input.slice(..); assert_eq!(output.to_string(), "foo bar baz"); } #[test] fn nested_range() { - let input = WrappedInput::new("lorem foo bar baz ipsum"); + let input = OrgSource::new("lorem foo bar baz ipsum"); let first_cut = input.slice(6..17); let output = first_cut.slice(4..7); assert_eq!(first_cut.to_string(), "foo bar baz"); @@ -149,21 +151,21 @@ mod tests { #[test] #[should_panic] fn out_of_bounds() { - let input = WrappedInput::new("lorem foo bar baz ipsum"); + let input = OrgSource::new("lorem foo bar baz ipsum"); input.slice(6..30); } #[test] #[should_panic] fn out_of_nested_bounds() { - let input = WrappedInput::new("lorem foo bar baz ipsum"); + let input = OrgSource::new("lorem foo bar baz ipsum"); let first_cut = input.slice(6..17); first_cut.slice(4..14); } #[test] fn line_break() { - let input = WrappedInput::new("lorem\nfoo\nbar\nbaz\nipsum"); + let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum"); assert_eq!(input.slice(5..).preceding_line_break, None); assert_eq!(input.slice(6..).preceding_line_break, Some(6)); assert_eq!(input.slice(6..).slice(10..).preceding_line_break, Some(14)); @@ -171,7 +173,7 @@ mod tests { #[test] fn text_since_line_break() { - let input = WrappedInput::new("lorem\nfoo\nbar\nbaz\nipsum"); + let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum"); assert_eq!(input.text_since_line_break(), ""); assert_eq!(input.slice(5..).text_since_line_break(), "lorem"); assert_eq!(input.slice(6..).text_since_line_break(), ""); From 6d1675fa0000aed5b969b2011985d1e87a87b170 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 22:57:44 -0400 Subject: [PATCH 09/19] Lifetime issue. --- src/parser/org_source.rs | 91 +++++++++++++++++++++++++++++++++++++++- src/parser/util.rs | 10 +++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 8afd74b..c0e70f4 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -1,10 +1,14 @@ use std::ops::RangeBounds; use nom::Compare; +use nom::InputIter; +use nom::InputLength; use nom::InputTake; +use nom::InputTakeAtPosition; +use nom::Offset; use nom::Slice; -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub struct OrgSource<'s> { full_source: &'s str, start: usize, @@ -107,6 +111,91 @@ impl<'s> std::fmt::Display for OrgSource<'s> { } } +impl<'s> InputLength for OrgSource<'s> { + fn input_len(&self) -> usize { + todo!() + } +} + +impl<'s> InputIter for OrgSource<'s> { + type Item = <&'s str as InputIter>::Item; + + type Iter = <&'s str as InputIter>::Iter; + + type IterElem = <&'s str as InputIter>::IterElem; + + fn iter_indices(&self) -> Self::Iter { + todo!() + } + + fn iter_elements(&self) -> Self::IterElem { + todo!() + } + + fn position

(&self, predicate: P) -> Option + where + P: Fn(Self::Item) -> bool, + { + todo!() + } + + fn slice_index(&self, count: usize) -> Result { + todo!() + } +} + +impl<'s> Offset for OrgSource<'s> { + fn offset(&self, second: &Self) -> usize { + todo!() + } +} + +impl<'s> InputTakeAtPosition for OrgSource<'s> { + type Item = <&'s str as InputTakeAtPosition>::Item; + + fn split_at_position>( + &self, + predicate: P, + ) -> nom::IResult + where + P: Fn(Self::Item) -> bool, + { + todo!() + } + + fn split_at_position1>( + &self, + predicate: P, + e: nom::error::ErrorKind, + ) -> nom::IResult + where + P: Fn(Self::Item) -> bool, + { + todo!() + } + + fn split_at_position_complete>( + &self, + predicate: P, + ) -> nom::IResult + where + P: Fn(Self::Item) -> bool, + { + todo!() + } + + fn split_at_position1_complete>( + &self, + predicate: P, + e: nom::error::ErrorKind, + ) -> nom::IResult + where + P: Fn(Self::Item) -> bool, + { + todo!() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/parser/util.rs b/src/parser/util.rs index 47a64d4..b858a77 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -14,6 +14,7 @@ use nom::multi::many0; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::parser_context::ContextElement; use super::Context; use crate::error::CustomError; @@ -113,6 +114,15 @@ pub fn blank_line(input: &str) -> Res<&str, &str> { recognize(tuple((space0, alt((line_ending, eof)))))(input) } +/// A line containing only whitespace and then a line break +/// +/// It is up to the caller to ensure this is called at the start of a line. +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +pub fn new_blank_line<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { + not(eof)(input)?; + recognize(tuple((space0, alt((line_ending, eof)))))(input) +} + #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn element_trailing_whitespace<'r, 's>( context: Context<'r, 's>, From c475dce6daa5e4b5da108b1e54d8daeab574083e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 23:14:23 -0400 Subject: [PATCH 10/19] Fix lifetime issue. --- src/parser/org_source.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index c0e70f4..3f43c65 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -46,7 +46,7 @@ impl<'s> InputTake for OrgSource<'s> { } } -impl<'s, O: Into<&'s str>> Compare for OrgSource<'s> { +impl<'s, 'o, O: Into<&'o str>> Compare for OrgSource<'s> { fn compare(&self, t: O) -> nom::CompareResult { (&self.full_source[self.start..self.end]).compare(t.into()) } From b7a5dd48ea020fc5fd76cc7f18f01f419233ac11 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 23:32:27 -0400 Subject: [PATCH 11/19] Impl missing traits. --- src/parser/org_source.rs | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 3f43c65..b765366 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -12,7 +12,7 @@ use nom::Slice; pub struct OrgSource<'s> { full_source: &'s str, start: usize, - end: usize, //exclusive + end: usize, // exclusive preceding_line_break: Option, } @@ -113,7 +113,7 @@ impl<'s> std::fmt::Display for OrgSource<'s> { impl<'s> InputLength for OrgSource<'s> { fn input_len(&self) -> usize { - todo!() + self.end - self.start } } @@ -125,28 +125,28 @@ impl<'s> InputIter for OrgSource<'s> { type IterElem = <&'s str as InputIter>::IterElem; fn iter_indices(&self) -> Self::Iter { - todo!() + Into::<&str>::into(self).char_indices() } fn iter_elements(&self) -> Self::IterElem { - todo!() + Into::<&str>::into(self).iter_elements() } fn position

(&self, predicate: P) -> Option where P: Fn(Self::Item) -> bool, { - todo!() + Into::<&str>::into(self).position(predicate) } fn slice_index(&self, count: usize) -> Result { - todo!() + Into::<&str>::into(self).slice_index(count) } } impl<'s> Offset for OrgSource<'s> { fn offset(&self, second: &Self) -> usize { - todo!() + second.start - self.start } } @@ -160,7 +160,10 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + match Into::<&str>::into(self).position(predicate) { + Some(idx) => Ok(self.take_split(idx)), + None => Err(nom::Err::Incomplete(nom::Needed::new(1))), + } } fn split_at_position1>( @@ -171,7 +174,11 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + match Into::<&str>::into(self).position(predicate) { + Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))), + Some(idx) => Ok(self.take_split(idx)), + None => Err(nom::Err::Incomplete(nom::Needed::new(1))), + } } fn split_at_position_complete>( @@ -181,7 +188,10 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + match self.split_at_position(predicate) { + Err(nom::Err::Incomplete(_)) => Ok(self.take_split(self.input_len())), + res => res, + } } fn split_at_position1_complete>( @@ -192,7 +202,18 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + let window = Into::<&str>::into(self); + match window.position(predicate) { + Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))), + Some(n) => Ok(self.take_split(n)), + None => { + if window.input_len() == 0 { + Err(nom::Err::Error(E::from_error_kind(self.clone(), e))) + } else { + Ok(self.take_split(self.input_len())) + } + } + } } } From dab598e5e7bf9e4746956fdadcdcb78fcdf48a37 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 23 Aug 2023 00:30:26 -0400 Subject: [PATCH 12/19] Convert all functions to using the wrapped input type. --- src/error/error.rs | 1 + src/parser/angle_link.rs | 22 +++- src/parser/citation.rs | 45 ++++--- src/parser/citation_reference.rs | 44 +++++-- src/parser/clock.rs | 24 +++- src/parser/comment.rs | 20 ++- src/parser/diary_sexp.rs | 13 +- src/parser/document.rs | 67 +++++++--- src/parser/drawer.rs | 23 ++-- src/parser/dynamic_block.rs | 26 ++-- src/parser/element_parser.rs | 11 +- src/parser/entity.rs | 17 ++- src/parser/export_snippet.rs | 26 ++-- src/parser/fixed_width_area.rs | 16 ++- src/parser/footnote_definition.rs | 21 ++-- src/parser/footnote_reference.rs | 37 +++--- src/parser/greater_block.rs | 40 +++--- src/parser/horizontal_rule.rs | 12 +- src/parser/inline_babel_call.rs | 46 +++++-- src/parser/inline_source_block.rs | 50 ++++++-- src/parser/keyword.rs | 13 +- src/parser/latex_environment.rs | 34 +++-- src/parser/latex_fragment.rs | 68 ++++++---- src/parser/lesser_block.rs | 88 +++++++------ src/parser/line_break.rs | 23 ++-- src/parser/object_parser.rs | 17 +-- src/parser/org_macro.rs | 31 +++-- src/parser/org_source.rs | 36 ++++++ src/parser/paragraph.rs | 19 ++- src/parser/parser_context.rs | 22 ++-- src/parser/plain_link.rs | 33 +++-- src/parser/plain_list.rs | 57 ++++++--- src/parser/plain_text.rs | 26 +++- src/parser/planning.rs | 15 ++- src/parser/property_drawer.rs | 45 ++++--- src/parser/radio_link.rs | 37 ++++-- src/parser/regular_link.rs | 41 ++++-- src/parser/sexp.rs | 51 +++----- src/parser/statistics_cookie.rs | 27 ++-- src/parser/subscript_and_superscript.rs | 73 +++++++---- src/parser/table.rs | 65 +++++++--- src/parser/target.rs | 20 ++- src/parser/text_markup.rs | 147 ++++++++++++++++------ src/parser/timestamp.rs | 159 +++++++++++++++++------- src/parser/util.rs | 81 ++++++------ 45 files changed, 1217 insertions(+), 572 deletions(-) diff --git a/src/error/error.rs b/src/error/error.rs index 37e3804..ef61469 100644 --- a/src/error/error.rs +++ b/src/error/error.rs @@ -4,6 +4,7 @@ use nom::IResult; pub type Res = IResult>; +// TODO: MyError probably shouldn't be based on the same type as the input type since it's used exclusively with static strings right now. #[derive(Debug, PartialEq)] pub enum CustomError { MyError(MyError), diff --git a/src/parser/angle_link.rs b/src/parser/angle_link.rs index 5ee81b1..24664b9 100644 --- a/src/parser/angle_link.rs +++ b/src/parser/angle_link.rs @@ -4,6 +4,7 @@ use nom::combinator::peek; use nom::combinator::recognize; use nom::multi::many_till; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::exiting::ExitClass; @@ -16,7 +17,10 @@ use crate::parser::util::get_consumed; use crate::parser::AngleLink; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn angle_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, AngleLink<'s>> { +pub fn angle_link<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, AngleLink<'s>> { let (remaining, _) = tag("<")(input)?; let (remaining, proto) = protocol(context, remaining)?; let (remaining, _separator) = tag(":")(remaining)?; @@ -26,15 +30,18 @@ pub fn angle_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s Ok(( remaining, AngleLink { - source, - link_type: proto, - path, + source: source.into(), + link_type: proto.into(), + path: path.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn path_angle<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn path_angle<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -48,6 +55,9 @@ fn path_angle<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn path_angle_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn path_angle_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { tag(">")(input) } diff --git a/src/parser/citation.rs b/src/parser/citation.rs index 6d7e0a7..dae8b31 100644 --- a/src/parser/citation.rs +++ b/src/parser/citation.rs @@ -11,6 +11,7 @@ use nom::multi::many_till; use nom::multi::separated_list1; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::Res; @@ -29,7 +30,10 @@ use crate::parser::util::get_consumed; use crate::parser::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn citation<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Citation<'s>> { +pub fn citation<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Citation<'s>> { // TODO: Despite being a standard object, citations cannot exist inside the global prefix/suffix for other citations because citations must contain something that matches @key which is forbidden inside the global prefix/suffix. This TODO is to evaluate if its worth putting in an explicit check for this (which can be easily accomplished by checking the output of `get_bracket_depth()`). I suspect its not worth it because I expect, outside of intentionally crafted inputs, this parser will exit immediately inside a citation since it is unlikely to find the "[cite" substring inside a citation global prefix/suffix. let (remaining, _) = tag_no_case("[cite")(input)?; let (remaining, _) = opt(citestyle)(remaining)?; @@ -44,11 +48,16 @@ pub fn citation<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str let (remaining, _) = tag("]")(remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Citation { source })) + Ok(( + remaining, + Citation { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn citestyle<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { +fn citestyle<'r, 's>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { let (remaining, _) = tuple((tag("/"), style))(input)?; let (remaining, _) = opt(tuple((tag("/"), variant)))(remaining)?; let source = get_consumed(input, remaining); @@ -56,14 +65,14 @@ fn citestyle<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn style<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { +fn style<'r, 's>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { recognize(many1(verify(anychar, |c| { c.is_alphanumeric() || "_-".contains(*c) })))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn variant<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { +fn variant<'r, 's>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { recognize(many1(verify(anychar, |c| { c.is_alphanumeric() || "_-/".contains(*c) })))(input) @@ -72,8 +81,8 @@ fn variant<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn global_prefix<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Vec>> { + input: OrgSource<'s>, +) -> Res, Vec>> { // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. let parser_context = context .with_additional_node(ContextElement::CitationBracket(CitationBracket { @@ -96,12 +105,15 @@ fn global_prefix<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn global_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn global_prefix_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside a citation."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; @@ -116,7 +128,7 @@ fn global_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&' } } if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("]")(input); if close_bracket.is_ok() { return close_bracket; } @@ -130,8 +142,8 @@ fn global_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&' #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn global_suffix<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Vec>> { + input: OrgSource<'s>, +) -> Res, Vec>> { // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. let parser_context = context .with_additional_node(ContextElement::CitationBracket(CitationBracket { @@ -153,12 +165,15 @@ fn global_suffix<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn global_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn global_suffix_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside a citation."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; @@ -173,7 +188,7 @@ fn global_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&' } } if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("]")(input); if close_bracket.is_ok() { return close_bracket; } diff --git a/src/parser/citation_reference.rs b/src/parser/citation_reference.rs index 4c3d8fe..ed18a65 100644 --- a/src/parser/citation_reference.rs +++ b/src/parser/citation_reference.rs @@ -10,6 +10,7 @@ use nom::multi::many_till; use nom::sequence::preceded; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::Res; @@ -28,21 +29,26 @@ use crate::parser::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn citation_reference<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, CitationReference<'s>> { + input: OrgSource<'s>, +) -> Res, CitationReference<'s>> { let (remaining, _prefix) = opt(parser_with_context!(key_prefix)(context))(input)?; let (remaining, _key) = parser_with_context!(citation_reference_key)(context)(remaining)?; let (remaining, _suffix) = opt(parser_with_context!(key_suffix)(context))(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, CitationReference { source })) + Ok(( + remaining, + CitationReference { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn citation_reference_key<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, source) = recognize(tuple(( tag("@"), many1(verify( @@ -59,7 +65,10 @@ pub fn citation_reference_key<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn key_prefix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Vec>> { +fn key_prefix<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Vec>> { // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. let parser_context = context .with_additional_node(ContextElement::CitationBracket(CitationBracket { @@ -81,7 +90,10 @@ fn key_prefix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn key_suffix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Vec>> { +fn key_suffix<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Vec>> { // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. let parser_context = context .with_additional_node(ContextElement::CitationBracket(CitationBracket { @@ -114,12 +126,15 @@ pub fn get_bracket_depth<'r, 's>(context: Context<'r, 's>) -> Option<&'r Citatio } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn key_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn key_prefix_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside a citation reference."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; @@ -134,7 +149,7 @@ fn key_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s } } if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("]")(input); if close_bracket.is_ok() { return close_bracket; } @@ -146,12 +161,15 @@ fn key_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn key_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn key_suffix_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside a citation reference."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; @@ -166,7 +184,7 @@ fn key_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s } } if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("]")(input); if close_bracket.is_ok() { return close_bracket; } diff --git a/src/parser/clock.rs b/src/parser/clock.rs index ea8ce4a..36c84a7 100644 --- a/src/parser/clock.rs +++ b/src/parser/clock.rs @@ -11,6 +11,7 @@ use nom::combinator::recognize; use nom::combinator::verify; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::parser_with_context::parser_with_context; @@ -19,7 +20,10 @@ use crate::parser::util::start_of_line; use crate::parser::Clock; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn clock<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Clock<'s>> { +pub fn clock<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Clock<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _clock) = tag_no_case("clock:")(remaining)?; @@ -31,14 +35,19 @@ pub fn clock<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, C ))(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Clock { source })) + Ok(( + remaining, + Clock { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn inactive_timestamp_range_duration<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(tuple(( tag("["), is_not("\r\n]"), @@ -50,14 +59,17 @@ fn inactive_timestamp_range_duration<'r, 's>( space1, digit1, tag(":"), - verify(digit1, |mm: &str| mm.len() == 2), + verify(digit1, |mm: &OrgSource<'_>| mm.len() == 2), space0, alt((line_ending, eof)), )))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn inactive_timestamp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn inactive_timestamp<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(tuple(( tag("["), is_not("\r\n]"), diff --git a/src/parser/comment.rs b/src/parser/comment.rs index de75f1e..c301a5f 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -11,6 +11,7 @@ use nom::multi::many0; use nom::sequence::preceded; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::util::get_consumed; use super::Context; use crate::error::CustomError; @@ -24,10 +25,13 @@ use crate::parser::util::start_of_line; use crate::parser::Comment; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Comment<'s>> { +pub fn comment<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Comment<'s>> { if immediate_in_section(context, "comment") { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", + "Cannot nest objects of the same element".into(), )))); } let parser_context = context.with_additional_node(ContextElement::Context("comment")); @@ -38,11 +42,19 @@ pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Comment { source })) + Ok(( + remaining, + Comment { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn comment_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn comment_line<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let (remaining, _indent) = space0(input)?; let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple(( diff --git a/src/parser/diary_sexp.rs b/src/parser/diary_sexp.rs index 9f3b208..869311e 100644 --- a/src/parser/diary_sexp.rs +++ b/src/parser/diary_sexp.rs @@ -6,6 +6,7 @@ use nom::combinator::eof; use nom::combinator::recognize; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::sexp::sexp; use super::Context; use crate::error::Res; @@ -14,7 +15,10 @@ use crate::parser::util::start_of_line; use crate::parser::DiarySexp; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn diary_sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, DiarySexp<'s>> { +pub fn diary_sexp<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, DiarySexp<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _clock) = tag("%%")(remaining)?; @@ -24,5 +28,10 @@ pub fn diary_sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s recognize(tuple((space0, alt((line_ending, eof)))))(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, DiarySexp { source })) + Ok(( + remaining, + DiarySexp { + source: source.into(), + }, + )) } diff --git a/src/parser/document.rs b/src/parser/document.rs index 43c176e..09ddcd5 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -16,6 +16,7 @@ use nom::sequence::tuple; use super::element::Element; use super::object::Object; +use super::org_source::convert_error; use super::org_source::OrgSource; use super::parser_with_context::parser_with_context; use super::source::Source; @@ -99,7 +100,9 @@ pub fn document(input: &str) -> Res<&str, Document> { let wrapped_input = OrgSource::new(input); let document_context = initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let (remaining, document) = _document(&document_context, input)?; + let (remaining, document) = _document(&document_context, wrapped_input) + .map(|(rem, out)| (Into::<&str>::into(rem), out)) + .map_err(convert_error)?; { // If there are radio targets in this document then we need to parse the entire document again with the knowledge of the radio targets. let all_radio_targets: Vec<&Vec>> = document @@ -117,15 +120,20 @@ pub fn document(input: &str) -> Res<&str, Document> { if !all_radio_targets.is_empty() { let document_context = document_context .with_additional_node(ContextElement::RadioTarget(all_radio_targets)); - let (remaining, document) = _document(&document_context, input)?; - return Ok((remaining, document)); + let (remaining, document) = _document(&document_context, wrapped_input) + .map(|(rem, out)| (Into::<&str>::into(rem), out)) + .map_err(convert_error)?; + return Ok((remaining.into(), document)); } } - Ok((remaining, document)) + Ok((remaining.into(), document)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn _document<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Document<'s>> { +fn _document<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Document<'s>> { let zeroth_section_matcher = parser_with_context!(zeroth_section)(context); let heading_matcher = parser_with_context!(heading)(context); let (remaining, _blank_lines) = many0(blank_line)(input)?; @@ -135,7 +143,7 @@ pub fn _document<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st Ok(( remaining, Document { - source, + source: source.into(), zeroth_section, children, }, @@ -143,7 +151,10 @@ pub fn _document<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Section<'s>> { +fn zeroth_section<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Section<'s>> { // TODO: The zeroth section is specialized so it probably needs its own parser let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) @@ -184,11 +195,20 @@ fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Section { source, children })) + Ok(( + remaining, + Section { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn section<'r, 's>(context: Context<'r, 's>, mut input: &'s str) -> Res<&'s str, Section<'s>> { +fn section<'r, 's>( + context: Context<'r, 's>, + mut input: OrgSource<'s>, +) -> Res, Section<'s>> { // TODO: The zeroth section is specialized so it probably needs its own parser let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) @@ -225,17 +245,29 @@ fn section<'r, 's>(context: Context<'r, 's>, mut input: &'s str) -> Res<&'s str, maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Section { source, children })) + Ok(( + remaining, + Section { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn section_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let headline_matcher = parser_with_context!(headline)(context); recognize(headline_matcher)(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> { +fn heading<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Heading<'s>> { not(|i| context.check_exit_matcher(i))(input)?; let (remaining, (star_count, _ws, title)) = headline(context, input)?; let section_matcher = parser_with_context!(section)(context); @@ -251,7 +283,7 @@ fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Hea Ok(( remaining, Heading { - source, + source: source.into(), stars: star_count, title, children, @@ -262,8 +294,8 @@ fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Hea #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn headline<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, (usize, &'s str, Vec>)> { + input: OrgSource<'s>, +) -> Res, (usize, OrgSource<'s>, Vec>)> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Document, @@ -283,7 +315,10 @@ fn headline<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn headline_end<'r, 's>(_context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn headline_end<'r, 's>( + _context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { line_ending(input) } diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index 6f74617..4e6a842 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -10,6 +10,7 @@ use nom::combinator::recognize; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -31,10 +32,13 @@ use crate::parser::Element; use crate::parser::Paragraph; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> { +pub fn drawer<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Drawer<'s>> { if immediate_in_section(context, "drawer") { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", + "Cannot nest objects of the same element".into(), )))); } start_of_line(context, input)?; @@ -63,9 +67,9 @@ pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ))(remaining) { Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => { - let mut element = Element::Paragraph(Paragraph::of_text(first_line)); + let mut element = Element::Paragraph(Paragraph::of_text(first_line.into())); let source = get_consumed(remaining, remain); - element.set_source(source); + element.set_source(source.into()); (remain, vec![element]) } Err(_) => { @@ -81,20 +85,23 @@ pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Ok(( remaining, Drawer { - source, - name: drawer_name, + source: source.into(), + name: drawer_name.into(), children, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn drawer_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; recognize(tuple(( space0, diff --git a/src/parser/dynamic_block.rs b/src/parser/dynamic_block.rs index dd76b79..30739ff 100644 --- a/src/parser/dynamic_block.rs +++ b/src/parser/dynamic_block.rs @@ -11,6 +11,7 @@ use nom::combinator::recognize; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -33,12 +34,12 @@ use crate::parser::Element; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn dynamic_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, DynamicBlock<'s>> { + input: OrgSource<'s>, +) -> Res, DynamicBlock<'s>> { // TODO: Do I need to differentiate between different dynamic block types. if immediate_in_section(context, "dynamic block") { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", + "Cannot nest objects of the same element".into(), )))); } start_of_line(context, input)?; @@ -69,9 +70,9 @@ pub fn dynamic_block<'r, 's>( ))(remaining) { Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => { - let mut element = Element::Paragraph(Paragraph::of_text(first_line)); + let mut element = Element::Paragraph(Paragraph::of_text(first_line.into())); let source = get_consumed(remaining, remain); - element.set_source(source); + element.set_source(source.into()); (remain, vec![element]) } Err(_) => { @@ -86,26 +87,29 @@ pub fn dynamic_block<'r, 's>( Ok(( remaining, DynamicBlock { - source, - name, - parameters, + source: source.into(), + name: name.into(), + parameters: parameters.map(|val| val.into()), children, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not(" \t\r\n")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn parameters<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn parameters<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not("\r\n")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn dynamic_block_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn dynamic_block_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let (remaining, source) = recognize(tuple(( space0, diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs index 11a2b45..d781a7a 100644 --- a/src/parser/element_parser.rs +++ b/src/parser/element_parser.rs @@ -19,6 +19,7 @@ use super::lesser_block::example_block; use super::lesser_block::export_block; use super::lesser_block::src_block; use super::lesser_block::verse_block; +use super::org_source::OrgSource; use super::paragraph::paragraph; use super::plain_list::plain_list; use super::source::SetSource; @@ -31,16 +32,16 @@ use crate::parser::table::org_mode_table; pub fn element( can_be_paragraph: bool, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, Element<'s>> { - move |context: Context, input: &str| _element(context, input, can_be_paragraph) +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, Element<'s>> { + move |context: Context, input: OrgSource<'_>| _element(context, input, can_be_paragraph) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _element<'r, 's>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, can_be_paragraph: bool, -) -> Res<&'s str, Element<'s>> { +) -> Res, Element<'s>> { let plain_list_matcher = parser_with_context!(plain_list)(context); let greater_block_matcher = parser_with_context!(greater_block)(context); let dynamic_block_matcher = parser_with_context!(dynamic_block)(context); @@ -103,7 +104,7 @@ fn _element<'r, 's>( maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); - element.set_source(source); + element.set_source(source.into()); Ok((remaining, element)) } diff --git a/src/parser/entity.rs b/src/parser/entity.rs index 6289505..c375d8e 100644 --- a/src/parser/entity.rs +++ b/src/parser/entity.rs @@ -7,6 +7,7 @@ use nom::combinator::eof; use nom::combinator::peek; use nom::combinator::recognize; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::object::Entity; @@ -14,7 +15,10 @@ use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::get_consumed; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn entity<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Entity<'s>> { +pub fn entity<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Entity<'s>> { let (remaining, _) = tag("\\")(input)?; let (remaining, entity_name) = name(context, remaining)?; let (remaining, _) = alt(( @@ -27,14 +31,17 @@ pub fn entity<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Ok(( remaining, Entity { - source, - entity_name, + source: source.into(), + entity_name: entity_name.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn name<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { // TODO: This should be defined by org-entities and optionally org-entities-user // TODO: Add the rest of the entities, this is a very incomplete list @@ -43,7 +50,7 @@ fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn entity_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +fn entity_end<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let (remaining, _) = alt((eof, recognize(satisfy(|c| !c.is_alphabetic()))))(input)?; Ok((remaining, ())) diff --git a/src/parser/export_snippet.rs b/src/parser/export_snippet.rs index cfc0c53..c293eef 100644 --- a/src/parser/export_snippet.rs +++ b/src/parser/export_snippet.rs @@ -7,6 +7,7 @@ use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::exiting::ExitClass; @@ -20,8 +21,8 @@ use crate::parser::ExportSnippet; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn export_snippet<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, ExportSnippet<'s>> { + input: OrgSource<'s>, +) -> Res, ExportSnippet<'s>> { let (remaining, _) = tag("@@")(input)?; let (remaining, backend_name) = backend(context, remaining)?; let parser_context = @@ -38,15 +39,18 @@ pub fn export_snippet<'r, 's>( Ok(( remaining, ExportSnippet { - source, - backend: backend_name, - contents: backend_contents.map(|(_colon, backend_contents)| backend_contents), + source: source.into(), + backend: backend_name.into(), + contents: backend_contents.map(|(_colon, backend_contents)| backend_contents.into()), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn backend<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn backend<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, backend_name) = recognize(many1(verify(anychar, |c| c.is_alphanumeric() || *c == '-')))(input)?; @@ -54,7 +58,10 @@ fn backend<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn contents<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn contents<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, source) = recognize(verify( many_till(anychar, parser_with_context!(exit_matcher_parser)(context)), |(children, _exit_contents)| !children.is_empty(), @@ -63,6 +70,9 @@ fn contents<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &' } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn export_snippet_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn export_snippet_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { tag("@@")(input) } diff --git a/src/parser/fixed_width_area.rs b/src/parser/fixed_width_area.rs index 182dfec..068556d 100644 --- a/src/parser/fixed_width_area.rs +++ b/src/parser/fixed_width_area.rs @@ -11,6 +11,7 @@ use nom::multi::many0; use nom::sequence::preceded; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::parser_with_context::parser_with_context; @@ -22,8 +23,8 @@ use crate::parser::FixedWidthArea; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn fixed_width_area<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, FixedWidthArea<'s>> { + input: OrgSource<'s>, +) -> Res, FixedWidthArea<'s>> { let fixed_width_area_line_matcher = parser_with_context!(fixed_width_area_line)(context); let exit_matcher = parser_with_context!(exit_matcher_parser)(context); let (remaining, _first_line) = fixed_width_area_line_matcher(input)?; @@ -31,14 +32,19 @@ pub fn fixed_width_area<'r, 's>( many0(preceded(not(exit_matcher), fixed_width_area_line_matcher))(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, FixedWidthArea { source })) + Ok(( + remaining, + FixedWidthArea { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn fixed_width_area_line<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let (remaining, _indent) = space0(input)?; let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple(( diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 775701f..738c324 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -10,6 +10,7 @@ use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::util::WORD_CONSTITUENT_CHARACTERS; use super::Context; use crate::error::CustomError; @@ -31,11 +32,11 @@ use crate::parser::util::start_of_line; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn footnote_definition<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, FootnoteDefinition<'s>> { + input: OrgSource<'s>, +) -> Res, FootnoteDefinition<'s>> { if immediate_in_section(context, "footnote definition") { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", + "Cannot nest objects of the same element".into(), )))); } start_of_line(context, input)?; @@ -59,15 +60,15 @@ pub fn footnote_definition<'r, 's>( Ok(( remaining, FootnoteDefinition { - source, - label: lbl, + source: source.into(), + label: lbl.into(), children, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn label<'s>(input: &'s str) -> Res<&'s str, &'s str> { +pub fn label<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { alt(( digit1, take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c)), @@ -77,8 +78,8 @@ pub fn label<'s>(input: &'s str) -> Res<&'s str, &'s str> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn footnote_definition_end<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let start_of_line_matcher = parser_with_context!(start_of_line)(context); let allow_nesting_context = context.with_additional_node(ContextElement::Context("allow nesting footnotes")); @@ -98,7 +99,9 @@ fn footnote_definition_end<'r, 's>( ))), recognize(tuple(( start_of_line_matcher, - verify(many1(blank_line), |lines: &Vec<&str>| lines.len() >= 2), + verify(many1(blank_line), |lines: &Vec>| { + lines.len() >= 2 + }), ))), ))(input)?; diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index 5cb797e..10956a8 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -5,6 +5,7 @@ use nom::character::complete::space0; use nom::combinator::verify; use nom::multi::many_till; +use super::org_source::OrgSource; use super::parser_context::ContextElement; use super::Context; use crate::error::CustomError; @@ -24,8 +25,8 @@ use crate::parser::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn footnote_reference<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, FootnoteReference<'s>> { + input: OrgSource<'s>, +) -> Res, FootnoteReference<'s>> { alt(( parser_with_context!(anonymous_footnote)(context), parser_with_context!(footnote_reference_only)(context), @@ -36,8 +37,8 @@ pub fn footnote_reference<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn anonymous_footnote<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, FootnoteReference<'s>> { + input: OrgSource<'s>, +) -> Res, FootnoteReference<'s>> { let (remaining, _) = tag_no_case("[fn::")(input)?; let parser_context = context .with_additional_node(ContextElement::FootnoteReferenceDefinition( @@ -65,7 +66,7 @@ fn anonymous_footnote<'r, 's>( Ok(( remaining, FootnoteReference { - source, + source: source.into(), label: None, definition: children, }, @@ -75,8 +76,8 @@ fn anonymous_footnote<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn inline_footnote<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, FootnoteReference<'s>> { + input: OrgSource<'s>, +) -> Res, FootnoteReference<'s>> { let (remaining, _) = tag_no_case("[fn:")(input)?; let (remaining, label_contents) = label(remaining)?; let (remaining, _) = tag(":")(remaining)?; @@ -106,8 +107,8 @@ fn inline_footnote<'r, 's>( Ok(( remaining, FootnoteReference { - source, - label: Some(label_contents), + source: source.into(), + label: Some(label_contents.into()), definition: children, }, )) @@ -116,8 +117,8 @@ fn inline_footnote<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn footnote_reference_only<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, FootnoteReference<'s>> { + input: OrgSource<'s>, +) -> Res, FootnoteReference<'s>> { let (remaining, _) = tag_no_case("[fn:")(input)?; let (remaining, label_contents) = label(remaining)?; let (remaining, _) = tag("]")(remaining)?; @@ -126,28 +127,28 @@ fn footnote_reference_only<'r, 's>( Ok(( remaining, FootnoteReference { - source, - label: Some(label_contents), + source: source.into(), + label: Some(label_contents.into()), definition: Vec::with_capacity(0), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn definition<'s>(input: &'s str) -> Res<&'s str, Vec>> { +fn definition<'s>(input: OrgSource<'s>) -> Res, Vec>> { Ok((input, vec![])) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn footnote_definition_end<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside a footnote definition."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; @@ -164,7 +165,7 @@ fn footnote_definition_end<'r, 's>( if current_depth > 0 { // Its impossible for the next character to end the footnote reference definition if we're any amount of brackets deep return Err(nom::Err::Error(CustomError::MyError(MyError( - "NoFootnoteReferenceDefinitionEnd", + "NoFootnoteReferenceDefinitionEnd".into(), )))); } tag("]")(input) diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index 7164e9a..62c042c 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -11,6 +11,7 @@ use nom::combinator::verify; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -33,26 +34,28 @@ use crate::parser::Paragraph; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn greater_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, GreaterBlock<'s>> { + input: OrgSource<'s>, +) -> Res, GreaterBlock<'s>> { // TODO: Do I need to differentiate between different greater block types. start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name)) = tuple(( tag_no_case("#+begin_"), - verify(name, |name: &str| match name.to_lowercase().as_str() { - "comment" | "example" | "export" | "src" | "verse" => false, - _ => true, + verify(name, |name: &OrgSource<'_>| { + match Into::<&str>::into(name).to_lowercase().as_str() { + "comment" | "example" | "export" | "src" | "verse" => false, + _ => true, + } }), ))(remaining)?; - let context_name = match name.to_lowercase().as_str() { + let context_name = match Into::<&str>::into(name).to_lowercase().as_str() { "center" => "center block", "quote" => "quote block", _ => "greater block", }; if immediate_in_section(context, context_name) { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", + "Cannot nest objects of the same element".into(), )))); } let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?; @@ -60,7 +63,7 @@ pub fn greater_block<'r, 's>( let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) .with_additional_node(ContextElement::Context(context_name)) - .with_additional_node(ContextElement::GreaterBlock(name)) + .with_additional_node(ContextElement::GreaterBlock(name.into())) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &greater_block_end, @@ -80,9 +83,9 @@ pub fn greater_block<'r, 's>( ))(remaining) { Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => { - let mut element = Element::Paragraph(Paragraph::of_text(first_line)); + let mut element = Element::Paragraph(Paragraph::of_text(first_line.into())); let source = get_consumed(remaining, remain); - element.set_source(source); + element.set_source(source.into()); (remain, vec![element]) } Err(_) => { @@ -99,29 +102,32 @@ pub fn greater_block<'r, 's>( Ok(( remaining, GreaterBlock { - source, - name, - parameters, + source: source.into(), + name: name.into(), + parameters: parameters.map(|val| Into::<&str>::into(val)), children, }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not(" \t\r\n")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn parameters<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn parameters<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not("\r\n")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn greater_block_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn greater_block_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let current_name: &str = get_context_greater_block_name(context).ok_or(nom::Err::Error( - CustomError::MyError(MyError("Not inside a greater block")), + CustomError::MyError(MyError("Not inside a greater block".into())), ))?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _ws)) = tuple(( diff --git a/src/parser/horizontal_rule.rs b/src/parser/horizontal_rule.rs index 37fb99d..88aa468 100644 --- a/src/parser/horizontal_rule.rs +++ b/src/parser/horizontal_rule.rs @@ -8,6 +8,7 @@ use nom::combinator::verify; use nom::multi::many1_count; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::util::start_of_line; @@ -16,8 +17,8 @@ use crate::parser::HorizontalRule; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn horizontal_rule<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, HorizontalRule<'s>> { + input: OrgSource<'s>, +) -> Res, HorizontalRule<'s>> { start_of_line(context, input)?; let (remaining, rule) = recognize(tuple(( space0, @@ -25,5 +26,10 @@ pub fn horizontal_rule<'r, 's>( space0, alt((line_ending, eof)), )))(input)?; - Ok((remaining, HorizontalRule { source: rule })) + Ok(( + remaining, + HorizontalRule { + source: rule.into(), + }, + )) } diff --git a/src/parser/inline_babel_call.rs b/src/parser/inline_babel_call.rs index 4c412bf..3e25186 100644 --- a/src/parser/inline_babel_call.rs +++ b/src/parser/inline_babel_call.rs @@ -10,6 +10,7 @@ use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::exiting::ExitClass; @@ -24,8 +25,8 @@ use crate::parser::InlineBabelCall; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn inline_babel_call<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, InlineBabelCall<'s>> { + input: OrgSource<'s>, +) -> Res, InlineBabelCall<'s>> { let (remaining, _) = tag_no_case("call_")(input)?; let (remaining, _name) = name(context, remaining)?; let (remaining, _header1) = opt(parser_with_context!(header)(context))(remaining)?; @@ -33,11 +34,19 @@ pub fn inline_babel_call<'r, 's>( let (remaining, _header2) = opt(parser_with_context!(header)(context))(remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, InlineBabelCall { source })) + Ok(( + remaining, + InlineBabelCall { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn name<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -51,12 +60,18 @@ fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn name_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(one_of("[("))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn header<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("[")(input)?; let parser_context = context @@ -78,12 +93,15 @@ fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn header_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside an inline babel call header."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '(' => { current_depth += 1; @@ -101,7 +119,10 @@ fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn argument<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn argument<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("(")(input)?; let parser_context = context @@ -123,12 +144,15 @@ fn argument<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &' } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn argument_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn argument_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside an inline babel call argument."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; diff --git a/src/parser/inline_source_block.rs b/src/parser/inline_source_block.rs index 1b71faf..0d7bef7 100644 --- a/src/parser/inline_source_block.rs +++ b/src/parser/inline_source_block.rs @@ -11,6 +11,7 @@ use nom::multi::many_till; #[cfg(feature = "tracing")] use tracing::span; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::Res; @@ -26,19 +27,27 @@ use crate::parser::InlineSourceBlock; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn inline_source_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, InlineSourceBlock<'s>> { + input: OrgSource<'s>, +) -> Res, InlineSourceBlock<'s>> { let (remaining, _) = tag_no_case("src_")(input)?; let (remaining, _) = lang(context, remaining)?; let (remaining, _header1) = opt(parser_with_context!(header)(context))(remaining)?; let (remaining, _body) = body(context, remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, InlineSourceBlock { source })) + Ok(( + remaining, + InlineSourceBlock { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn lang<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn lang<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -52,12 +61,18 @@ fn lang<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn lang_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn lang_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(one_of("[{"))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn header<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("[")(input)?; let parser_context = context @@ -81,12 +96,15 @@ fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn header_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside an inline source block header."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '[' => { current_depth += 1; @@ -101,7 +119,7 @@ fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } } if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("]")(input); if close_bracket.is_ok() { return close_bracket; } @@ -111,7 +129,10 @@ fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn body<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn body<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("{")(input)?; let parser_context = context @@ -145,12 +166,15 @@ fn body<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn body_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn body_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside an inline source block body."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '{' => { current_depth += 1; @@ -176,7 +200,7 @@ fn body_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &' let _enter = span.enter(); if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("}")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("}")(input); if close_bracket.is_ok() { return close_bracket; } diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index d5a33e6..8114660 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -11,13 +11,17 @@ use nom::combinator::peek; use nom::combinator::recognize; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::util::start_of_line; use crate::parser::Keyword; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn keyword<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Keyword<'s>> { +pub fn keyword<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Keyword<'s>> { start_of_line(context, input)?; // TODO: When key is a member of org-element-parsed-keywords, value can contain the standard set objects, excluding footnote references. let (remaining, rule) = recognize(tuple(( @@ -30,5 +34,10 @@ pub fn keyword<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, alt((recognize(tuple((space1, is_not("\r\n")))), space0)), alt((line_ending, eof)), )))(input)?; - Ok((remaining, Keyword { source: rule })) + Ok(( + remaining, + Keyword { + source: rule.into(), + }, + )) } diff --git a/src/parser/latex_environment.rs b/src/parser/latex_environment.rs index fc90b18..84195c7 100644 --- a/src/parser/latex_environment.rs +++ b/src/parser/latex_environment.rs @@ -11,6 +11,7 @@ use nom::combinator::recognize; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::util::get_consumed; use super::Context; use crate::error::Res; @@ -25,8 +26,8 @@ use crate::parser::LatexEnvironment; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn latex_environment<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, LatexEnvironment<'s>> { + input: OrgSource<'s>, +) -> Res, LatexEnvironment<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_opening, name, _open_close_brace, _ws, _line_ending)) = tuple(( @@ -37,7 +38,7 @@ pub fn latex_environment<'r, 's>( line_ending, ))(remaining)?; - let latex_environment_end_specialized = latex_environment_end(name); + let latex_environment_end_specialized = latex_environment_end(name.into()); let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -48,11 +49,16 @@ pub fn latex_environment<'r, 's>( let (remaining, _end) = latex_environment_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, LatexEnvironment { source })) + Ok(( + remaining, + LatexEnvironment { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { take_while1(|c: char| c.is_alphanumeric() || c == '*')(input) } @@ -60,11 +66,15 @@ fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { feature = "tracing", tracing::instrument(ret, level = "debug", skip(end_matcher)) )] -pub fn contents<'r, 's, F: Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str>>( +pub fn contents< + 'r, + 's, + F: Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>>, +>( end_matcher: F, context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, source) = recognize(many_till( anychar, peek(alt(( @@ -78,9 +88,9 @@ pub fn contents<'r, 's, F: Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> fn latex_environment_end( current_name: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> { +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { let current_name_lower = current_name.to_lowercase(); - move |context: Context, input: &str| { + move |context: Context, input: OrgSource<'_>| { _latex_environment_end(context, input, current_name_lower.as_str()) } } @@ -88,9 +98,9 @@ fn latex_environment_end( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _latex_environment_end<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, current_name_lower: &'x str, -) -> Res<&'s str, &'s str> { +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _close_brace, _ws, _line_ending)) = tuple(( diff --git a/src/parser/latex_fragment.rs b/src/parser/latex_fragment.rs index 71f7b7c..75073eb 100644 --- a/src/parser/latex_fragment.rs +++ b/src/parser/latex_fragment.rs @@ -13,6 +13,7 @@ use nom::multi::many0; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -26,8 +27,8 @@ use crate::parser::LatexFragment; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn latex_fragment<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, LatexFragment<'s>> { + input: OrgSource<'s>, +) -> Res, LatexFragment<'s>> { let (remaining, _) = alt(( parser_with_context!(raw_latex_fragment)(context), parser_with_context!(escaped_parenthesis_fragment)(context), @@ -38,11 +39,19 @@ pub fn latex_fragment<'r, 's>( ))(input)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, LatexFragment { source })) + Ok(( + remaining, + LatexFragment { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn raw_latex_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn raw_latex_fragment<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("\\")(input)?; let (remaining, _) = name(context, remaining)?; let (remaining, _) = many0(parser_with_context!(brackets)(context))(remaining)?; @@ -52,12 +61,18 @@ fn raw_latex_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<& } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn name<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { alpha1(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn brackets<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn brackets<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, body) = alt(( recognize(tuple(( tag("["), @@ -88,8 +103,8 @@ fn brackets<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &' #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn escaped_parenthesis_fragment<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("\\(")(input)?; let (remaining, _) = recognize(many_till( anychar, @@ -107,8 +122,8 @@ fn escaped_parenthesis_fragment<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn escaped_bracket_fragment<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = tag("\\[")(input)?; let (remaining, _) = recognize(many_till( anychar, @@ -126,8 +141,8 @@ fn escaped_bracket_fragment<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn double_dollar_fragment<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { // TODO: The documentation on the dollar sign versions is incomplete. Test to figure out what the real requirements are. For example, can this span more than 3 lines and can this contain a single $ since its terminated by $$? let (remaining, _) = tag("$$")(input)?; let (remaining, _) = recognize(many_till( @@ -144,7 +159,10 @@ fn double_dollar_fragment<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn dollar_char_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn dollar_char_fragment<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (_, _) = pre(context, input)?; let (remaining, _) = tag("$")(input)?; let (remaining, _) = verify(none_of(".,?;\""), |c| !c.is_whitespace())(remaining)?; @@ -156,21 +174,21 @@ fn dollar_char_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +pub fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) .flatten(); if let Some('$') = preceding_character { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre character for dollar char fragment.", + "Not a valid pre character for dollar char fragment.".into(), )))); } Ok((input, ())) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn post<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +pub fn post<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { // TODO: What about eof? Test to find out. // TODO: Figure out which punctuation characters should be included. @@ -181,8 +199,8 @@ pub fn post<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, () #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn bordered_dollar_fragment<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (_, _) = pre(context, input)?; let (remaining, _) = tag("$")(input)?; // TODO: I'm assuming I should be peeking at the borders but the documentation is not clear. Test to figure out. @@ -197,7 +215,7 @@ fn bordered_dollar_fragment<'r, 's>( tag("$"), ))), )), - |body: &str| body.lines().take(4).count() <= 3, + |body: &OrgSource<'_>| Into::<&str>::into(body).lines().take(4).count() <= 3, )(remaining)?; let (_, _) = peek(parser_with_context!(close_border)(context))(remaining)?; @@ -209,12 +227,18 @@ fn bordered_dollar_fragment<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn open_border<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +pub fn open_border<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(verify(none_of(".,;$"), |c| !c.is_whitespace()))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn close_border<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +pub fn close_border<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -223,7 +247,7 @@ pub fn close_border<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s Some(c) if !c.is_whitespace() && !".,;$".contains(c) => Ok((input, ())), _ => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre character for dollar char fragment.", + "Not a valid pre character for dollar char fragment.".into(), )))); } } diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 42a1feb..7a60c6c 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -11,6 +11,7 @@ use nom::combinator::verify; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::exiting::ExitClass; @@ -34,8 +35,8 @@ use crate::parser::util::text_until_exit; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn verse_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, VerseBlock<'s>> { + input: OrgSource<'s>, +) -> Res, VerseBlock<'s>> { let (remaining, name) = lesser_block_begin("verse")(context, input)?; let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; @@ -58,7 +59,9 @@ pub fn verse_block<'r, 's>( let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) { Ok((remaining, (whitespace, (_children, _exit_contents)))) => ( remaining, - vec![Object::PlainText(PlainText { source: whitespace })], + vec![Object::PlainText(PlainText { + source: whitespace.into(), + })], ), Err(_) => { let (remaining, (children, _exit_contents)) = @@ -72,9 +75,9 @@ pub fn verse_block<'r, 's>( Ok(( remaining, VerseBlock { - source, - name, - data: parameters, + source: source.into(), + name: name.into(), + data: parameters.map(|parameters| Into::<&str>::into(parameters)), children, }, )) @@ -83,8 +86,8 @@ pub fn verse_block<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn comment_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, CommentBlock<'s>> { + input: OrgSource<'s>, +) -> Res, CommentBlock<'s>> { let (remaining, name) = lesser_block_begin("comment")(context, input)?; let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; @@ -108,10 +111,10 @@ pub fn comment_block<'r, 's>( Ok(( remaining, CommentBlock { - source, - name, - data: parameters, - contents, + source: source.into(), + name: name.into(), + data: parameters.map(|parameters| Into::<&str>::into(parameters)), + contents: contents.into(), }, )) } @@ -119,8 +122,8 @@ pub fn comment_block<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn example_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, ExampleBlock<'s>> { + input: OrgSource<'s>, +) -> Res, ExampleBlock<'s>> { let (remaining, name) = lesser_block_begin("example")(context, input)?; let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; @@ -144,10 +147,10 @@ pub fn example_block<'r, 's>( Ok(( remaining, ExampleBlock { - source, - name, - data: parameters, - contents, + source: source.into(), + name: source.into(), + data: parameters.map(|parameters| Into::<&str>::into(parameters)), + contents: contents.into(), }, )) } @@ -155,8 +158,8 @@ pub fn example_block<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn export_block<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, ExportBlock<'s>> { + input: OrgSource<'s>, +) -> Res, ExportBlock<'s>> { let (remaining, name) = lesser_block_begin("export")(context, input)?; // https://orgmode.org/worg/org-syntax.html#Blocks claims that export blocks must have a single word for data but testing shows no data and multi-word data still parses as an export block. let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; @@ -181,16 +184,19 @@ pub fn export_block<'r, 's>( Ok(( remaining, ExportBlock { - source, - name, - data: parameters, - contents, + source: source.into(), + name: name.into(), + data: parameters.map(|parameters| Into::<&str>::into(parameters)), + contents: contents.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn src_block<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, SrcBlock<'s>> { +pub fn src_block<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, SrcBlock<'s>> { let (remaining, name) = lesser_block_begin("src")(context, input)?; // https://orgmode.org/worg/org-syntax.html#Blocks claims that data is mandatory and must follow the LANGUAGE SWITCHES ARGUMENTS pattern but testing has shown that no data and incorrect data here will still parse to a src block. let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; @@ -215,29 +221,29 @@ pub fn src_block<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st Ok(( remaining, SrcBlock { - source, - name, - data: parameters, - contents, + source: source.into(), + name: name.into(), + data: parameters.map(|parameters| Into::<&str>::into(parameters)), + contents: contents.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not(" \t\r\n")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn data<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn data<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not("\r\n")(input) } fn lesser_block_end( current_name: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> { +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { let current_name_lower = current_name.to_lowercase(); - move |context: Context, input: &str| { + move |context: Context, input: OrgSource<'_>| { _lesser_block_end(context, input, current_name_lower.as_str()) } } @@ -245,9 +251,9 @@ fn lesser_block_end( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _lesser_block_end<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, current_name_lower: &'x str, -) -> Res<&'s str, &'s str> { +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _ws)) = tuple(( @@ -261,9 +267,9 @@ fn _lesser_block_end<'r, 's, 'x>( fn lesser_block_begin( current_name: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> { +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { let current_name_lower = current_name.to_lowercase(); - move |context: Context, input: &str| { + move |context: Context, input: OrgSource<'_>| { _lesser_block_begin(context, input, current_name_lower.as_str()) } } @@ -271,15 +277,15 @@ fn lesser_block_begin( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _lesser_block_begin<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, current_name_lower: &'x str, -) -> Res<&'s str, &'s str> { +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name)) = tuple(( tag_no_case("#+begin_"), - verify(name, |name: &str| { - name.to_lowercase().as_str() == current_name_lower + verify(name, |name: &OrgSource<'_>| { + Into::<&str>::into(name).to_lowercase().as_str() == current_name_lower }), ))(remaining)?; Ok((remaining, name)) diff --git a/src/parser/line_break.rs b/src/parser/line_break.rs index 635fd6b..6d359e5 100644 --- a/src/parser/line_break.rs +++ b/src/parser/line_break.rs @@ -4,6 +4,7 @@ use nom::character::complete::one_of; use nom::combinator::recognize; use nom::multi::many0; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -14,17 +15,25 @@ use crate::parser::util::get_one_before; use crate::parser::LineBreak; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn line_break<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, LineBreak<'s>> { +pub fn line_break<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, LineBreak<'s>> { let (remaining, _) = pre(context, input)?; let (remaining, _) = tag(r#"\\"#)(remaining)?; let (remaining, _) = recognize(many0(one_of(" \t")))(remaining)?; let (remaining, _) = line_ending(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, LineBreak { source })) + Ok(( + remaining, + LineBreak { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -33,7 +42,7 @@ fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { // If None, we are at the start of the file None | Some('\\') => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre character for line break.", + "Not a valid pre character for line break.".into(), )))); } _ => {} @@ -41,16 +50,16 @@ fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { let current_line = get_current_line_before_position(document_root, input); match current_line { Some(line) => { - let is_non_empty_line = line.chars().any(|c| !c.is_whitespace()); + let is_non_empty_line = Into::<&str>::into(line).chars().any(|c| !c.is_whitespace()); if !is_non_empty_line { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre line for line break.", + "Not a valid pre line for line break.".into(), )))); } } None => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "No preceding line for line break.", + "No preceding line for line break.".into(), )))); } } diff --git a/src/parser/object_parser.rs b/src/parser/object_parser.rs index 3176a60..37973a1 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -2,6 +2,7 @@ use nom::branch::alt; use nom::combinator::map; use nom::combinator::not; +use super::org_source::OrgSource; use super::parser_with_context::parser_with_context; use super::plain_text::plain_text; use super::regular_link::regular_link; @@ -31,8 +32,8 @@ use crate::parser::timestamp::timestamp; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn standard_set_object<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, +) -> Res, Object<'s>> { not(|i| context.check_exit_matcher(i))(input)?; alt(( @@ -90,8 +91,8 @@ pub fn standard_set_object<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn minimal_set_object<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, +) -> Res, Object<'s>> { not(|i| context.check_exit_matcher(i))(input)?; alt(( @@ -113,8 +114,8 @@ pub fn minimal_set_object<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn any_object_except_plain_text<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, +) -> Res, Object<'s>> { // Used for exit matchers so this does not check exit matcher condition. alt(( map(parser_with_context!(timestamp)(context), Object::Timestamp), @@ -170,8 +171,8 @@ pub fn any_object_except_plain_text<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn regular_link_description_object_set<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, +) -> Res, Object<'s>> { // TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] alt(( map( diff --git a/src/parser/org_macro.rs b/src/parser/org_macro.rs index 6a14d6e..e3a66a7 100644 --- a/src/parser/org_macro.rs +++ b/src/parser/org_macro.rs @@ -7,6 +7,7 @@ use nom::combinator::verify; use nom::multi::many0; use nom::multi::separated_list0; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::object::OrgMacro; @@ -15,7 +16,10 @@ use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn org_macro<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, OrgMacro<'s>> { +pub fn org_macro<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgMacro<'s>> { let (remaining, _) = tag("{{{")(input)?; let (remaining, macro_name) = org_macro_name(context, remaining)?; let (remaining, macro_args) = opt(parser_with_context!(org_macro_args)(context))(remaining)?; @@ -25,15 +29,22 @@ pub fn org_macro<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st Ok(( remaining, OrgMacro { - source, - macro_name, - macro_args: macro_args.unwrap_or_else(|| Vec::with_capacity(0)), + source: source.into(), + macro_name: macro_name.into(), + macro_args: macro_args + .unwrap_or_else(|| Vec::with_capacity(0)) + .into_iter() + .map(|arg| arg.into()) + .collect(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn org_macro_name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn org_macro_name<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _) = verify(anychar, |c| c.is_alphabetic())(input)?; let (remaining, _) = many0(verify(anychar, |c| { c.is_alphanumeric() || *c == '-' || *c == '_' @@ -43,7 +54,10 @@ fn org_macro_name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn org_macro_args<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Vec<&'s str>> { +fn org_macro_args<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Vec>> { let (remaining, _) = tag("(")(input)?; let (remaining, args) = separated_list0(tag(","), parser_with_context!(org_macro_arg)(context))(remaining)?; @@ -53,7 +67,10 @@ fn org_macro_args<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn org_macro_arg<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn org_macro_arg<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let mut remaining = input; let mut escaping: bool = false; loop { diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index b765366..a3ac2ac 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -8,6 +8,9 @@ use nom::InputTakeAtPosition; use nom::Offset; use nom::Slice; +use crate::error::CustomError; +use crate::error::MyError; + #[derive(Debug, Copy, Clone)] pub struct OrgSource<'s> { full_source: &'s str, @@ -34,6 +37,10 @@ impl<'s> OrgSource<'s> { let start = self.preceding_line_break.unwrap_or(0); &self.full_source[start..self.start] } + + pub fn len(&self) -> usize { + self.end - self.start + } } impl<'s> InputTake for OrgSource<'s> { @@ -68,6 +75,12 @@ impl<'s> From<&OrgSource<'s>> for &'s str { } } +impl<'s> From> for &'s str { + fn from(value: OrgSource<'s>) -> Self { + &value.full_source[value.start..value.end] + } +} + impl<'s, R> Slice for OrgSource<'s> where R: RangeBounds, @@ -217,6 +230,29 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { } } +pub fn convert_error(err: nom::Err>>) -> nom::Err> { + match err { + nom::Err::Incomplete(needed) => nom::Err::Incomplete(needed), + nom::Err::Error(err) => nom::Err::Error(err.into()), + nom::Err::Failure(err) => nom::Err::Failure(err.into()), + } +} + +impl<'s> From>> for CustomError<&'s str> { + fn from(value: CustomError>) -> Self { + match value { + CustomError::MyError(err) => CustomError::MyError(err.into()), + CustomError::Nom(input, error_kind) => CustomError::Nom(input.into(), error_kind), + } + } +} + +impl<'s> From>> for MyError<&'s str> { + fn from(value: MyError>) -> Self { + MyError(value.0.into()) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 4ce08c4..65f11b7 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -7,6 +7,7 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::lesser_element::Paragraph; +use super::org_source::OrgSource; use super::util::blank_line; use super::util::get_consumed; use super::Context; @@ -21,7 +22,10 @@ use crate::parser::util::exit_matcher_parser; use crate::parser::util::start_of_line; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> { +pub fn paragraph<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Paragraph<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -39,11 +43,20 @@ pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st let source = get_consumed(input, remaining); - Ok((remaining, Paragraph { source, children })) + Ok(( + remaining, + Paragraph { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn paragraph_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn paragraph_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let non_paragraph_element_matcher = parser_with_context!(element(false))(context); let start_of_line_matcher = parser_with_context!(start_of_line)(&context); alt(( diff --git a/src/parser/parser_context.rs b/src/parser/parser_context.rs index f0ff27e..7001072 100644 --- a/src/parser/parser_context.rs +++ b/src/parser/parser_context.rs @@ -5,6 +5,7 @@ use nom::IResult; use super::list::List; use super::list::Node; +use super::org_source::OrgSource; use super::Context; use super::Object; use crate::error::CustomError; @@ -12,7 +13,8 @@ use crate::error::MyError; use crate::error::Res; use crate::parser::exiting::ExitClass; -type Matcher = dyn for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str>; +type Matcher = + dyn for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>>; #[derive(Debug, Clone)] pub struct ContextTree<'r, 's> { @@ -47,8 +49,8 @@ impl<'r, 's> ContextTree<'r, 's> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn check_exit_matcher( &'r self, - i: &'s str, - ) -> IResult<&'s str, &'s str, CustomError<&'s str>> { + i: OrgSource<'s>, + ) -> IResult, OrgSource<'s>, CustomError>> { // Special check for EOF. We don't just make this a document-level exit matcher since the IgnoreParent ChainBehavior could cause early exit matchers to not run. let at_end_of_file = eof(i); if at_end_of_file.is_ok() { @@ -78,7 +80,9 @@ impl<'r, 's> ContextTree<'r, 's> { }; } // TODO: Make this a specific error instead of just a generic MyError - return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit")))); + return Err(nom::Err::Error(CustomError::MyError(MyError( + "NoExit".into(), + )))); } pub fn get_document_root(&self) -> Option<&'s str> { @@ -215,31 +219,31 @@ pub struct ExitMatcherNode<'r> { #[derive(Debug)] pub struct FootnoteReferenceDefinition<'s> { - pub position: &'s str, + pub position: OrgSource<'s>, pub depth: usize, } #[derive(Debug)] pub struct CitationBracket<'s> { - pub position: &'s str, + pub position: OrgSource<'s>, pub depth: usize, } #[derive(Debug)] pub struct BabelHeaderBracket<'s> { - pub position: &'s str, + pub position: OrgSource<'s>, pub depth: usize, } #[derive(Debug)] pub struct InlineSourceBlockBracket<'s> { - pub position: &'s str, + pub position: OrgSource<'s>, pub depth: usize, } #[derive(Debug)] pub struct SubscriptSuperscriptBrace<'s> { - pub position: &'s str, + pub position: OrgSource<'s>, pub depth: usize, } diff --git a/src/parser/plain_link.rs b/src/parser/plain_link.rs index 3882219..8c99a5d 100644 --- a/src/parser/plain_link.rs +++ b/src/parser/plain_link.rs @@ -9,6 +9,7 @@ use nom::combinator::peek; use nom::combinator::recognize; use nom::multi::many_till; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -24,7 +25,10 @@ use crate::parser::util::get_one_before; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn plain_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainLink<'s>> { +pub fn plain_link<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, PlainLink<'s>> { let (remaining, _) = pre(context, input)?; let (remaining, proto) = protocol(context, remaining)?; let (remaining, _separator) = tag(":")(remaining)?; @@ -34,15 +38,15 @@ pub fn plain_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s Ok(( remaining, PlainLink { - source, - link_type: proto, - path, + source: source.into(), + link_type: proto.into(), + path: path.into(), }, )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -54,7 +58,7 @@ fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { Some(_) => { // Not at start of line, cannot be a heading return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre character for plain link.", + "Not a valid pre character for plain link.".into(), )))); } }; @@ -62,13 +66,16 @@ fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn post<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +fn post<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let (remaining, _) = alt((eof, recognize(none_of(WORD_CONSTITUENT_CHARACTERS))))(input)?; Ok((remaining, ())) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn protocol<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +pub fn protocol<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { // TODO: This should be defined by org-link-parameters let (remaining, proto) = alt(( alt(( @@ -103,7 +110,10 @@ pub fn protocol<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn path_plain<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn path_plain<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { // TODO: "optionally containing parenthesis-wrapped non-whitespace non-bracket substrings up to a depth of two. The string must end with either a non-punctation non-whitespace character, a forwards slash, or a parenthesis-wrapped substring" let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { @@ -118,6 +128,9 @@ fn path_plain<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn path_plain_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn path_plain_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(one_of(" \t\r\n()[]<>"))(input) } diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index 1990bab..b3edfae 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -15,6 +15,7 @@ use nom::sequence::tuple; use super::greater_element::PlainList; use super::greater_element::PlainListItem; +use super::org_source::OrgSource; use super::parser_with_context::parser_with_context; use super::util::non_whitespace_character; use super::Context; @@ -32,7 +33,10 @@ use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainList<'s>> { +pub fn plain_list<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, PlainList<'s>> { let parser_context = context .with_additional_node(ContextElement::Context("plain list")) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { @@ -76,7 +80,7 @@ pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s Some(final_child) => final_child, None => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Plain lists require at least one element.", + "Plain lists require at least one element.".into(), )))); } }; @@ -93,7 +97,7 @@ pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s Ok(( remaining, PlainList { - source, + source: source.into(), children: children.into_iter().map(|(_start, item)| item).collect(), }, )) @@ -102,25 +106,27 @@ pub fn plain_list<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn plain_list_item<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, PlainListItem<'s>> { + input: OrgSource<'s>, +) -> Res, PlainListItem<'s>> { start_of_line(context, input)?; let (remaining, leading_whitespace) = space0(input)?; // It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09) let indent_level = leading_whitespace.len(); - let (remaining, bull) = - verify(bullet, |bull: &str| bull != "*" || indent_level > 0)(remaining)?; + let (remaining, bull) = verify(bullet, |bull: &OrgSource<'_>| { + Into::<&str>::into(bull) != "*" || indent_level > 0 + })(remaining)?; - let maybe_contentless_item: Res<&str, &str> = alt((eof, line_ending))(remaining); + let maybe_contentless_item: Res, OrgSource<'_>> = + alt((eof, line_ending))(remaining); match maybe_contentless_item { Ok((rem, _ws)) => { let source = get_consumed(input, rem); return Ok(( rem, PlainListItem { - source, + source: source.into(), indentation: indent_level, - bullet: bull, + bullet: bull.into(), children: Vec::new(), }, )); @@ -152,16 +158,16 @@ pub fn plain_list_item<'r, 's>( return Ok(( remaining, PlainListItem { - source, + source: source.into(), indentation: indent_level, - bullet: bull, + bullet: bull.into(), children, }, )); } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn bullet<'s>(i: &'s str) -> Res<&'s str, &'s str> { +fn bullet<'s>(i: OrgSource<'s>) -> Res, OrgSource<'s>> { alt(( tag("*"), tag("-"), @@ -171,21 +177,29 @@ fn bullet<'s>(i: &'s str) -> Res<&'s str, &'s str> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn counter<'s>(i: &'s str) -> Res<&'s str, &'s str> { +fn counter<'s>(i: OrgSource<'s>) -> Res, OrgSource<'s>> { alt((recognize(one_of("abcdefghijklmnopqrstuvwxyz")), digit1))(i) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn plain_list_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn plain_list_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let start_of_line_matcher = parser_with_context!(start_of_line)(context); recognize(tuple(( start_of_line_matcher, - verify(many1(blank_line), |lines: &Vec<&str>| lines.len() >= 2), + verify(many1(blank_line), |lines: &Vec>| { + lines.len() >= 2 + }), )))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn plain_list_item_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; recognize(tuple(( opt(blank_line), @@ -194,14 +208,17 @@ fn plain_list_item_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res< } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn line_indented_lte<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn line_indented_lte<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let current_item_indent_level: &usize = get_context_item_indent(context).ok_or(nom::Err::Error(CustomError::MyError(MyError( - "Not inside a plain list item", + "Not inside a plain list item".into(), ))))?; let matched = recognize(verify( - tuple((space0::<&str, _>, non_whitespace_character)), + tuple((space0::, _>, non_whitespace_character)), // It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09) |(_space0, _anychar)| _space0.len() <= *current_item_indent_level, ))(input)?; diff --git a/src/parser/plain_text.rs b/src/parser/plain_text.rs index 76755fe..bdc9deb 100644 --- a/src/parser/plain_text.rs +++ b/src/parser/plain_text.rs @@ -8,6 +8,7 @@ use nom::combinator::verify; use nom::multi::many_till; use super::object::PlainText; +use super::org_source::OrgSource; use super::radio_link::RematchObject; use super::Context; use super::Object; @@ -17,7 +18,10 @@ use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainText<'s>> { +pub fn plain_text<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, PlainText<'s>> { let (remaining, source) = recognize(verify( many_till( anychar, @@ -29,11 +33,19 @@ pub fn plain_text<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s |(children, _exit_contents)| !children.is_empty(), ))(input)?; - Ok((remaining, PlainText { source })) + Ok(( + remaining, + PlainText { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn plain_text_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn plain_text_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(parser_with_context!(any_object_except_plain_text)(context))(input) } @@ -42,10 +54,12 @@ impl<'x> RematchObject<'x> for PlainText<'x> { fn rematch_object<'r, 's>( &'x self, _context: Context<'r, 's>, - input: &'s str, - ) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, + ) -> Res, Object<'s>> { map(tag(self.source), |s| { - Object::PlainText(PlainText { source: s }) + Object::PlainText(PlainText { + source: Into::<&str>::into(s), + }) })(input) } } diff --git a/src/parser/planning.rs b/src/parser/planning.rs index faea297..bbaed9c 100644 --- a/src/parser/planning.rs +++ b/src/parser/planning.rs @@ -9,6 +9,7 @@ use nom::combinator::eof; use nom::multi::separated_list1; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::lesser_element::Planning; @@ -16,7 +17,10 @@ use crate::parser::util::get_consumed; use crate::parser::util::start_of_line; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn planning<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Planning<'s>> { +pub fn planning<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Planning<'s>> { start_of_line(context, input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _planning_parameters) = separated_list1(space1, planning_parameter)(remaining)?; @@ -24,11 +28,16 @@ pub fn planning<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str let source = get_consumed(input, remaining); - Ok((remaining, Planning { source })) + Ok(( + remaining, + Planning { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn planning_parameter<'s>(input: &'s str) -> Res<&'s str, &'s str> { +fn planning_parameter<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { let (remaining, _planning_type) = alt(( tag_no_case("DEADLINE"), tag_no_case("SCHEDULED"), diff --git a/src/parser/property_drawer.rs b/src/parser/property_drawer.rs index 7d63a31..3e78ecf 100644 --- a/src/parser/property_drawer.rs +++ b/src/parser/property_drawer.rs @@ -12,6 +12,7 @@ use nom::combinator::recognize; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -32,11 +33,11 @@ use crate::parser::util::start_of_line; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn property_drawer<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, PropertyDrawer<'s>> { + input: OrgSource<'s>, +) -> Res, PropertyDrawer<'s>> { if immediate_in_section(context, "property-drawer") { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Cannot nest objects of the same element", + "Cannot nest objects of the same element".into(), )))); } let ( @@ -68,11 +69,20 @@ pub fn property_drawer<'r, 's>( maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, PropertyDrawer { source, children })) + Ok(( + remaining, + PropertyDrawer { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn property_drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn property_drawer_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(tuple(( parser_with_context!(start_of_line)(context), space0, @@ -85,8 +95,8 @@ fn property_drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res< #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn node_property<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, NodeProperty<'s>> { + input: OrgSource<'s>, +) -> Res, NodeProperty<'s>> { let (remaining, (_start_of_line, _leading_whitespace, _open_colon, _name, _close_colon)) = tuple(( parser_with_context!(start_of_line)(context), @@ -95,13 +105,17 @@ fn node_property<'r, 's>( parser_with_context!(node_property_name)(context), tag(":"), ))(input)?; - match tuple((space0::<&str, nom::error::Error<&str>>, line_ending))(remaining) { + match tuple(( + space0::, nom::error::Error>>, + line_ending, + ))(remaining) + { Ok((remaining, _ws)) => { let source = get_consumed(input, remaining); Ok(( remaining, NodeProperty { - source, + source: source.into(), value: None, }, )) @@ -113,8 +127,8 @@ fn node_property<'r, 's>( Ok(( remaining, NodeProperty { - source, - value: Some(value), + source: source.into(), + value: Some(value.into()), }, )) } @@ -122,7 +136,10 @@ fn node_property<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn node_property_name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn node_property_name<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -141,7 +158,7 @@ fn node_property_name<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<& #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn node_property_name_end<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { alt((tag("+:"), tag(":")))(input) } diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index c1d4d45..8f72205 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -5,6 +5,7 @@ use nom::character::complete::space0; use nom::combinator::verify; use nom::multi::many_till; +use super::org_source::OrgSource; use super::Context; use super::Object; use crate::error::CustomError; @@ -21,7 +22,10 @@ use crate::parser::RadioLink; use crate::parser::RadioTarget; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn radio_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, RadioLink<'s>> { +pub fn radio_link<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, RadioLink<'s>> { let radio_targets = context .iter() .filter_map(|context_element| match context_element.get_data() { @@ -37,14 +41,14 @@ pub fn radio_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s return Ok(( remaining, RadioLink { - source, + source: source.into(), children: rematched_target, }, )); } } Err(nom::Err::Error(CustomError::MyError(MyError( - "NoRadioLink", + "NoRadioLink".into(), )))) } @@ -52,8 +56,8 @@ pub fn radio_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s pub fn rematch_target<'x, 'r, 's>( context: Context<'r, 's>, target: &'x Vec>, - input: &'s str, -) -> Res<&'s str, Vec>> { + input: OrgSource<'s>, +) -> Res, Vec>> { let mut remaining = input; let mut new_matches = Vec::with_capacity(target.len()); for original_object in target { @@ -71,7 +75,7 @@ pub fn rematch_target<'x, 'r, 's>( } _ => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "OnlyMinimalSetObjectsAllowed", + "OnlyMinimalSetObjectsAllowed".into(), )))); } }; @@ -82,8 +86,8 @@ pub fn rematch_target<'x, 'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn radio_target<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, RadioTarget<'s>> { + input: OrgSource<'s>, +) -> Res, RadioTarget<'s>> { let (remaining, _opening) = tag("<<<")(input)?; let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { @@ -102,11 +106,20 @@ pub fn radio_target<'r, 's>( let (remaining, _closing) = tag(">>>")(remaining)?; let (remaining, _trailing_whitespace) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, RadioTarget { source, children })) + Ok(( + remaining, + RadioTarget { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn radio_target_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn radio_target_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { alt((tag("<"), tag(">"), line_ending))(input) } @@ -114,8 +127,8 @@ pub trait RematchObject<'x> { fn rematch_object<'r, 's>( &'x self, context: Context<'r, 's>, - input: &'s str, - ) -> Res<&'s str, Object<'s>>; + input: OrgSource<'s>, + ) -> Res, Object<'s>>; } #[cfg(test)] diff --git a/src/parser/regular_link.rs b/src/parser/regular_link.rs index d5664ab..2441961 100644 --- a/src/parser/regular_link.rs +++ b/src/parser/regular_link.rs @@ -7,6 +7,7 @@ use nom::character::complete::space0; use nom::combinator::verify; use nom::multi::many_till; +use super::org_source::OrgSource; use super::parser_with_context::parser_with_context; use super::util::get_consumed; use super::Context; @@ -22,8 +23,8 @@ use crate::parser::util::exit_matcher_parser; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn regular_link<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, RegularLink<'s>> { + input: OrgSource<'s>, +) -> Res, RegularLink<'s>> { alt(( parser_with_context!(regular_link_without_description)(context), parser_with_context!(regular_link_with_description)(context), @@ -33,21 +34,26 @@ pub fn regular_link<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn regular_link_without_description<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, RegularLink<'s>> { + input: OrgSource<'s>, +) -> Res, RegularLink<'s>> { let (remaining, _opening_bracket) = tag("[[")(input)?; let (remaining, _path) = pathreg(context, remaining)?; let (remaining, _closing_bracket) = tag("]]")(remaining)?; let (remaining, _trailing_whitespace) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, RegularLink { source })) + Ok(( + remaining, + RegularLink { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn regular_link_with_description<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, RegularLink<'s>> { + input: OrgSource<'s>, +) -> Res, RegularLink<'s>> { let (remaining, _opening_bracket) = tag("[[")(input)?; let (remaining, _path) = pathreg(context, remaining)?; let (remaining, _closing_bracket) = tag("][")(remaining)?; @@ -55,11 +61,19 @@ pub fn regular_link_with_description<'r, 's>( let (remaining, _closing_bracket) = tag("]]")(remaining)?; let (remaining, _trailing_whitespace) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, RegularLink { source })) + Ok(( + remaining, + RegularLink { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn pathreg<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +pub fn pathreg<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, path) = escaped( take_till1(|c| match c { '\\' | ']' => true, @@ -74,8 +88,8 @@ pub fn pathreg<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn description<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Vec>> { + input: OrgSource<'s>, +) -> Res, Vec>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -93,6 +107,9 @@ pub fn description<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn description_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn description_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { tag("]]")(input) } diff --git a/src/parser/sexp.rs b/src/parser/sexp.rs index 1a7aa4c..410c2c7 100644 --- a/src/parser/sexp.rs +++ b/src/parser/sexp.rs @@ -16,6 +16,9 @@ use nom::sequence::delimited; use nom::sequence::preceded; use nom::sequence::tuple; +use super::org_source::convert_error; +use super::org_source::OrgSource; +use super::util::get_consumed; use crate::error::Res; #[derive(Debug)] @@ -28,9 +31,7 @@ pub enum Token<'s> { #[derive(Debug)] pub struct TextWithProperties<'s> { - #[allow(dead_code)] pub text: &'s str, - #[allow(dead_code)] pub properties: Vec>, } @@ -135,24 +136,25 @@ impl<'s> Token<'s> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn sexp_with_padding<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { let (remaining, _) = multispace0(input)?; - let (remaining, tkn) = token(remaining)?; + let remaining = OrgSource::new(remaining); + let (remaining, tkn) = token(remaining).map(|(rem, out)| (Into::<&str>::into(rem), out)).map_err(convert_error)?; let (remaining, _) = multispace0(remaining)?; Ok((remaining, tkn)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn sexp<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +pub fn sexp<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, tkn) = token(input)?; Ok((remaining, tkn)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn token<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn token<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { alt((list, vector, atom))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn list<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn list<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, _) = tag("(")(input)?; let (remaining, children) = delimited( multispace0, @@ -164,7 +166,7 @@ fn list<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn vector<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn vector<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, _) = tag("[")(input)?; let (remaining, children) = delimited( multispace0, @@ -176,7 +178,7 @@ fn vector<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn atom<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn atom<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { not(peek(one_of(")]")))(input)?; alt(( text_with_properties, @@ -187,16 +189,16 @@ fn atom<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn unquoted_atom<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn unquoted_atom<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, body) = take_till1(|c| match c { ' ' | '\t' | '\r' | '\n' | ')' | ']' => true, _ => false, })(input)?; - Ok((remaining, Token::Atom(body))) + Ok((remaining, Token::Atom(body.into()))) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn quoted_atom<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn quoted_atom<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, _) = tag(r#"""#)(input)?; let (remaining, _) = escaped( take_till1(|c| match c { @@ -208,11 +210,11 @@ fn quoted_atom<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { )(remaining)?; let (remaining, _) = tag(r#"""#)(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Token::Atom(source))) + Ok((remaining, Token::Atom(source.into()))) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn hash_notation<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn hash_notation<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, _) = tag("#<")(input)?; let (remaining, _body) = take_till1(|c| match c { '>' => true, @@ -220,10 +222,10 @@ fn hash_notation<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { })(remaining)?; let (remaining, _) = tag(">")(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Token::Atom(source))) + Ok((remaining, Token::Atom(source.into()))) } -fn text_with_properties<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { +fn text_with_properties<'s>(input: OrgSource<'s>) -> Res, Token<'s>> { let (remaining, _) = tag("#(")(input)?; let (remaining, (text, props)) = delimited( multispace0, @@ -246,25 +248,6 @@ fn text_with_properties<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { )) } -/// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser. -fn get_consumed<'s>(input: &'s str, remaining: &'s str) -> &'s str { - assert!(is_slice_of(input, remaining)); - let source = { - let offset = remaining.as_ptr() as usize - input.as_ptr() as usize; - &input[..offset] - }; - source -} - -/// Check if the child string slice is a slice of the parent string slice. -fn is_slice_of(parent: &str, child: &str) -> bool { - let parent_start = parent.as_ptr() as usize; - let parent_end = parent_start + parent.len(); - let child_start = child.as_ptr() as usize; - let child_end = child_start + child.len(); - child_start >= parent_start && child_end <= parent_end -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/parser/statistics_cookie.rs b/src/parser/statistics_cookie.rs index 30fdac7..92bc578 100644 --- a/src/parser/statistics_cookie.rs +++ b/src/parser/statistics_cookie.rs @@ -4,6 +4,7 @@ use nom::character::complete::space0; use nom::combinator::recognize; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::parser_with_context::parser_with_context; @@ -12,8 +13,8 @@ use crate::parser::StatisticsCookie; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn statistics_cookie<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, StatisticsCookie<'s>> { + input: OrgSource<'s>, +) -> Res, StatisticsCookie<'s>> { alt(( parser_with_context!(percent_statistics_cookie)(context), parser_with_context!(fraction_statistics_cookie)(context), @@ -23,19 +24,24 @@ pub fn statistics_cookie<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn percent_statistics_cookie<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, StatisticsCookie<'s>> { + input: OrgSource<'s>, +) -> Res, StatisticsCookie<'s>> { let (remaining, source) = recognize(tuple((tag("["), nom::character::complete::u64, tag("%]"))))(input)?; let (remaining, _) = space0(remaining)?; - Ok((remaining, StatisticsCookie { source })) + Ok(( + remaining, + StatisticsCookie { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn fraction_statistics_cookie<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, StatisticsCookie<'s>> { + input: OrgSource<'s>, +) -> Res, StatisticsCookie<'s>> { let (remaining, source) = recognize(tuple(( tag("["), nom::character::complete::u64, @@ -44,5 +50,10 @@ pub fn fraction_statistics_cookie<'r, 's>( tag("]"), )))(input)?; let (remaining, _) = space0(remaining)?; - Ok((remaining, StatisticsCookie { source })) + Ok(( + remaining, + StatisticsCookie { + source: source.into(), + }, + )) } diff --git a/src/parser/subscript_and_superscript.rs b/src/parser/subscript_and_superscript.rs index 05e0455..529dc76 100644 --- a/src/parser/subscript_and_superscript.rs +++ b/src/parser/subscript_and_superscript.rs @@ -11,6 +11,7 @@ use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; +use super::org_source::OrgSource; use super::Context; use super::Object; use crate::error::CustomError; @@ -29,32 +30,45 @@ use crate::parser::Subscript; use crate::parser::Superscript; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn subscript<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Subscript<'s>> { +pub fn subscript<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Subscript<'s>> { // We check for the underscore first before checking the pre-character as a minor optimization to avoid walking up the context tree to find the document root unnecessarily. let (remaining, _) = tag("_")(input)?; pre(context, input)?; let (remaining, _body) = script_body(context, remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Subscript { source })) + Ok(( + remaining, + Subscript { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn superscript<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Superscript<'s>> { + input: OrgSource<'s>, +) -> Res, Superscript<'s>> { // We check for the circumflex first before checking the pre-character as a minor optimization to avoid walking up the context tree to find the document root unnecessarily. let (remaining, _) = tag("^")(input)?; pre(context, input)?; let (remaining, _body) = script_body(context, remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Superscript { source })) + Ok(( + remaining, + Superscript { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -63,7 +77,7 @@ fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { Some(c) if !c.is_whitespace() => {} _ => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Must be preceded by a non-whitespace character.", + "Must be preceded by a non-whitespace character.".into(), )))); } }; @@ -77,27 +91,36 @@ enum ScriptBody<'s> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn script_body<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ScriptBody<'s>> { +fn script_body<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, ScriptBody<'s>> { alt(( map(parser_with_context!(script_asterisk)(context), |body| { - ScriptBody::Braceless(body) + ScriptBody::Braceless(body.into()) }), map(parser_with_context!(script_alphanum)(context), |body| { - ScriptBody::Braceless(body) + ScriptBody::Braceless(body.into()) }), map(parser_with_context!(script_with_braces)(context), |body| { - ScriptBody::WithBraces(body) + ScriptBody::WithBraces(body.into()) }), ))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn script_asterisk<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn script_asterisk<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { tag("*")(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn script_alphanum<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn script_alphanum<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, _sign) = opt(recognize(one_of("+-")))(input)?; let (remaining, _script) = many_till( parser_with_context!(script_alphanum_character)(context), @@ -110,8 +133,8 @@ fn script_alphanum<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn script_alphanum_character<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(verify(anychar, |c| { c.is_alphanumeric() || r#",.\"#.contains(*c) }))(input) @@ -120,8 +143,8 @@ fn script_alphanum_character<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn end_script_alphanum_character<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, final_char) = recognize(verify(anychar, |c| c.is_alphanumeric()))(input)?; peek(not(parser_with_context!(script_alphanum_character)( context, @@ -132,13 +155,13 @@ fn end_script_alphanum_character<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn script_with_braces<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Vec>> { + input: OrgSource<'s>, +) -> Res, Vec>> { let (remaining, _) = tag("{")(input)?; let parser_context = context .with_additional_node(ContextElement::SubscriptSuperscriptBrace( SubscriptSuperscriptBrace { - position: remaining, + position: remaining.into(), depth: 0, }, )) @@ -159,13 +182,13 @@ fn script_with_braces<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn script_with_braces_end<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let context_depth = get_bracket_depth(context) .expect("This function should only be called from inside a subscript or superscript."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; - for c in text_since_context_entry.chars() { + for c in Into::<&str>::into(text_since_context_entry).chars() { match c { '{' => { current_depth += 1; @@ -180,13 +203,13 @@ fn script_with_braces_end<'r, 's>( } } if current_depth == 0 { - let close_bracket = tag::<&str, &str, CustomError<&str>>("}")(input); + let close_bracket = tag::<&str, OrgSource<'_>, CustomError>>("}")(input); if close_bracket.is_ok() { return close_bracket; } } return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid end for subscript or superscript.", + "Not a valid end for subscript or superscript.".into(), )))); } diff --git a/src/parser/table.rs b/src/parser/table.rs index 286f555..01029d4 100644 --- a/src/parser/table.rs +++ b/src/parser/table.rs @@ -12,6 +12,7 @@ use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::exiting::ExitClass; @@ -31,7 +32,10 @@ use crate::parser::Table; /// /// This is not the table.el style. #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn org_mode_table<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Table<'s>> { +pub fn org_mode_table<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Table<'s>> { start_of_line(context, input)?; peek(tuple((space0, tag("|"))))(input)?; @@ -52,11 +56,20 @@ pub fn org_mode_table<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<& // TODO: Consume trailing formulas let source = get_consumed(input, remaining); - Ok((remaining, Table { source, children })) + Ok(( + remaining, + Table { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn table_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn table_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; recognize(tuple((space0, not(tag("|")))))(input) } @@ -64,8 +77,8 @@ fn table_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, & #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn org_mode_table_row<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, TableRow<'s>> { + input: OrgSource<'s>, +) -> Res, TableRow<'s>> { alt(( parser_with_context!(org_mode_table_row_rule)(context), parser_with_context!(org_mode_table_row_regular)(context), @@ -75,15 +88,15 @@ pub fn org_mode_table_row<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn org_mode_table_row_rule<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, TableRow<'s>> { + input: OrgSource<'s>, +) -> Res, TableRow<'s>> { start_of_line(context, input)?; let (remaining, _) = tuple((space0, tag("|-"), is_not("\r\n"), line_ending))(input)?; let source = get_consumed(input, remaining); Ok(( remaining, TableRow { - source, + source: source.into(), children: Vec::new(), }, )) @@ -92,22 +105,28 @@ pub fn org_mode_table_row_rule<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn org_mode_table_row_regular<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, TableRow<'s>> { + input: OrgSource<'s>, +) -> Res, TableRow<'s>> { start_of_line(context, input)?; let (remaining, _) = tuple((space0, tag("|")))(input)?; let (remaining, children) = many1(parser_with_context!(org_mode_table_cell)(context))(remaining)?; let (remaining, _tail) = recognize(tuple((space0, alt((line_ending, eof)))))(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, TableRow { source, children })) + Ok(( + remaining, + TableRow { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn org_mode_table_cell<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, TableCell<'s>> { + input: OrgSource<'s>, +) -> Res, TableCell<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -118,29 +137,37 @@ pub fn org_mode_table_cell<'r, 's>( let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till(table_cell_set_object_matcher, exit_matcher), - |(children, exit_contents)| !children.is_empty() || exit_contents.ends_with("|"), + |(children, exit_contents)| { + !children.is_empty() || Into::<&str>::into(exit_contents).ends_with("|") + }, )(input)?; let (remaining, _tail) = org_mode_table_cell_end(&parser_context, remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, TableCell { source, children })) + Ok(( + remaining, + TableCell { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn org_mode_table_cell_end<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(tuple((space0, alt((tag("|"), peek(line_ending))))))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn table_cell_set_object<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, +) -> Res, Object<'s>> { not(|i| context.check_exit_matcher(i))(input)?; parser_with_context!(minimal_set_object)(context)(input) diff --git a/src/parser/target.rs b/src/parser/target.rs index 6acd36e..02c5aa1 100644 --- a/src/parser/target.rs +++ b/src/parser/target.rs @@ -7,6 +7,7 @@ use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; +use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; use crate::error::MyError; @@ -21,7 +22,10 @@ use crate::parser::util::get_one_before; use crate::parser::Target; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn target<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Target<'s>> { +pub fn target<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Target<'s>> { let (remaining, _) = tag("<<")(input)?; let (remaining, _) = peek(verify(anychar, |c| { !c.is_whitespace() && !"<>\n".contains(*c) @@ -44,17 +48,25 @@ pub fn target<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, .expect("We cannot be at the start of the file because we are inside a target."); if preceding_character.is_whitespace() { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Targets cannot end with whitespace.", + "Targets cannot end with whitespace.".into(), )))); } let (remaining, _) = tag(">>")(remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Target { source })) + Ok(( + remaining, + Target { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn target_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn target_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(one_of("<>\n"))(input) } diff --git a/src/parser/text_markup.rs b/src/parser/text_markup.rs index 59a5d09..fc340be 100644 --- a/src/parser/text_markup.rs +++ b/src/parser/text_markup.rs @@ -15,6 +15,7 @@ use nom::sequence::terminated; #[cfg(feature = "tracing")] use tracing::span; +use super::org_source::OrgSource; use super::radio_link::RematchObject; use super::Context; use crate::error::CustomError; @@ -39,7 +40,10 @@ use crate::parser::Underline; use crate::parser::Verbatim; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Object<'s>> { +pub fn text_markup<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Object<'s>> { alt(( map(parser_with_context!(bold)(context), Object::Bold), map(parser_with_context!(italic)(context), Object::Italic), @@ -54,73 +58,126 @@ pub fn text_markup<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn bold<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Bold<'s>> { +pub fn bold<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Bold<'s>> { let text_markup_object_specialized = text_markup_object("*"); let (remaining, children) = text_markup_object_specialized(context, input)?; let source = get_consumed(input, remaining); - Ok((remaining, Bold { source, children })) + Ok(( + remaining, + Bold { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn italic<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Italic<'s>> { +pub fn italic<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Italic<'s>> { let text_markup_object_specialized = text_markup_object("/"); let (remaining, children) = text_markup_object_specialized(context, input)?; let source = get_consumed(input, remaining); - Ok((remaining, Italic { source, children })) + Ok(( + remaining, + Italic { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn underline<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Underline<'s>> { +pub fn underline<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Underline<'s>> { let text_markup_object_specialized = text_markup_object("_"); let (remaining, children) = text_markup_object_specialized(context, input)?; let source = get_consumed(input, remaining); - Ok((remaining, Underline { source, children })) + Ok(( + remaining, + Underline { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn strike_through<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, StrikeThrough<'s>> { + input: OrgSource<'s>, +) -> Res, StrikeThrough<'s>> { let text_markup_object_specialized = text_markup_object("+"); let (remaining, children) = text_markup_object_specialized(context, input)?; let source = get_consumed(input, remaining); - Ok((remaining, StrikeThrough { source, children })) + Ok(( + remaining, + StrikeThrough { + source: source.into(), + children, + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn verbatim<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Verbatim<'s>> { +pub fn verbatim<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Verbatim<'s>> { let text_markup_string_specialized = text_markup_string("="); let (remaining, contents) = text_markup_string_specialized(context, input)?; let source = get_consumed(input, remaining); - Ok((remaining, Verbatim { source, contents })) + Ok(( + remaining, + Verbatim { + source: source.into(), + contents: contents.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn code<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Code<'s>> { +pub fn code<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Code<'s>> { let text_markup_string_specialized = text_markup_string("~"); let (remaining, contents) = text_markup_string_specialized(context, input)?; let source = get_consumed(input, remaining); - Ok((remaining, Code { source, contents })) + Ok(( + remaining, + Code { + source: source.into(), + contents: contents.into(), + }, + )) } fn text_markup_object( marker_symbol: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, Vec>> { +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, Vec>> { let marker_symbol = marker_symbol.to_owned(); - move |context: Context, input: &str| _text_markup_object(context, input, marker_symbol.as_str()) + move |context: Context, input: OrgSource<'_>| { + _text_markup_object(context, input, marker_symbol.as_str()) + } } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _text_markup_object<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, marker_symbol: &'x str, -) -> Res<&'s str, Vec>> { +) -> Res, Vec>> { let (remaining, _) = pre(context, input)?; let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?; - let text_markup_end_specialized = text_markup_end(open); + let text_markup_end_specialized = text_markup_end(open.into()); let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -142,7 +199,7 @@ fn _text_markup_object<'r, 's, 'x>( let _enter = span.enter(); if exit_matcher_parser(context, remaining).is_ok() { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Parent exit matcher is triggering.", + "Parent exit matcher is triggering.".into(), )))); } } @@ -154,21 +211,23 @@ fn _text_markup_object<'r, 's, 'x>( fn text_markup_string( marker_symbol: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> { +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { let marker_symbol = marker_symbol.to_owned(); - move |context: Context, input: &str| _text_markup_string(context, input, marker_symbol.as_str()) + move |context: Context, input: OrgSource<'_>| { + _text_markup_string(context, input, marker_symbol.as_str()) + } } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _text_markup_string<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, marker_symbol: &'x str, -) -> Res<&'s str, &'s str> { +) -> Res, OrgSource<'s>> { let (remaining, _) = pre(context, input)?; let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?; - let text_markup_end_specialized = text_markup_end(open); + let text_markup_end_specialized = text_markup_end(open.into()); let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -190,7 +249,7 @@ fn _text_markup_string<'r, 's, 'x>( let _enter = span.enter(); if exit_matcher_parser(context, remaining).is_ok() { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Parent exit matcher is triggering.", + "Parent exit matcher is triggering.".into(), )))); } } @@ -201,7 +260,7 @@ fn _text_markup_string<'r, 's, 'x>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +pub fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -213,7 +272,7 @@ pub fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> Some(_) => { // Not at start of line, cannot be a heading return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre character for text markup.", + "Not a valid pre character for text markup.".into(), )))); } }; @@ -221,24 +280,26 @@ pub fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn post<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +pub fn post<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { let (remaining, _) = alt((recognize(one_of(" \r\n\t-.,;:!?')}[\">")), line_ending))(input)?; Ok((remaining, ())) } fn text_markup_end( marker_symbol: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> { +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { let marker_symbol = marker_symbol.to_owned(); - move |context: Context, input: &str| _text_markup_end(context, input, marker_symbol.as_str()) + move |context: Context, input: OrgSource<'_>| { + _text_markup_end(context, input, marker_symbol.as_str()) + } } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _text_markup_end<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, marker_symbol: &'x str, -) -> Res<&'s str, &'s str> { +) -> Res, OrgSource<'s>> { not(parser_with_context!(preceded_by_whitespace)(context))(input)?; let (remaining, _marker) = terminated( tag(marker_symbol), @@ -253,26 +314,32 @@ impl<'x> RematchObject<'x> for Bold<'x> { fn rematch_object<'r, 's>( &'x self, _context: Context<'r, 's>, - input: &'s str, - ) -> Res<&'s str, Object<'s>> { + input: OrgSource<'s>, + ) -> Res, Object<'s>> { let (remaining, children) = _rematch_text_markup_object(_context, input, "*", &self.children)?; let source = get_consumed(input, remaining); - Ok((remaining, Object::Bold(Bold { source, children }))) + Ok(( + remaining, + Object::Bold(Bold { + source: source.into(), + children, + }), + )) } } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn _rematch_text_markup_object<'r, 's, 'x>( context: Context<'r, 's>, - input: &'s str, + input: OrgSource<'s>, marker_symbol: &'static str, original_match_children: &'x Vec>, -) -> Res<&'s str, Vec>> { +) -> Res, Vec>> { let (remaining, _) = pre(context, input)?; let (remaining, open) = tag(marker_symbol)(remaining)?; let (remaining, _peek_not_whitespace) = peek(not(multispace1))(remaining)?; - let text_markup_end_specialized = text_markup_end(open); + let text_markup_end_specialized = text_markup_end(open.into()); let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -290,7 +357,7 @@ fn _rematch_text_markup_object<'r, 's, 'x>( let _enter = span.enter(); if exit_matcher_parser(context, remaining).is_ok() { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Parent exit matcher is triggering.", + "Parent exit matcher is triggering.".into(), )))); } } diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 2680836..7e48346 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -11,6 +11,7 @@ use nom::combinator::verify; use nom::multi::many_till; use nom::sequence::tuple; +use super::org_source::OrgSource; use super::Context; use crate::error::Res; use crate::parser::exiting::ExitClass; @@ -23,7 +24,10 @@ use crate::parser::util::get_consumed; use crate::parser::Timestamp; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn timestamp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Timestamp<'s>> { +pub fn timestamp<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { // TODO: This would be more efficient if we didn't throw away the parse result of the first half of an active/inactive date range timestamp if the parse fails (as in, the first thing active_date_range_timestamp parses is a active_timestamp but then we throw that away if it doesn't turn out to be a full active_date_range_timestamp despite the active_timestamp parse being completely valid). I am going with the simplest/cleanest approach for the first implementation. alt(( // Order matters here. If its a date range, we need to parse the entire date range instead of just the first timestamp. If its a time range, we need to make sure thats parsed as a time range instead of as the "rest" portion of a single timestamp. @@ -40,19 +44,27 @@ pub fn timestamp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn diary_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _) = tag("<%%(")(input)?; let (remaining, _body) = sexp(context, remaining)?; let (remaining, _) = tag(")>")(remaining)?; let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn sexp<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -71,15 +83,18 @@ fn sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn sexp_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn sexp_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { alt((tag(")>"), recognize(one_of(">\n"))))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn active_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _) = tag("<")(input)?; let (remaining, _date) = date(context, remaining)?; let time_context = @@ -100,14 +115,19 @@ fn active_timestamp<'r, 's>( let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn inactive_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _) = tag("[")(input)?; let (remaining, _date) = date(context, remaining)?; let time_context = @@ -128,14 +148,19 @@ fn inactive_timestamp<'r, 's>( let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn active_date_range_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _first_timestamp) = active_timestamp(context, input)?; // TODO: Does the space0 at the end of the active/inactive timestamp parsers cause this to be incorrect? I could use a look-behind to make sure the preceding character is not whitespace let (remaining, _separator) = tag("--")(remaining)?; @@ -144,14 +169,19 @@ fn active_date_range_timestamp<'r, 's>( let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn active_time_range_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _) = tag("<")(input)?; let (remaining, _date) = date(context, remaining)?; let time_context = @@ -179,14 +209,19 @@ fn active_time_range_timestamp<'r, 's>( let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn inactive_date_range_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _first_timestamp) = inactive_timestamp(context, input)?; // TODO: Does the space0 at the end of the active/inactive timestamp parsers cause this to be incorrect? I could use a look-behind to make sure the preceding character is not whitespace let (remaining, _separator) = tag("--")(remaining)?; @@ -195,14 +230,19 @@ fn inactive_date_range_timestamp<'r, 's>( let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn inactive_time_range_timestamp<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Timestamp<'s>> { + input: OrgSource<'s>, +) -> Res, Timestamp<'s>> { let (remaining, _) = tag("[")(input)?; let (remaining, _date) = date(context, remaining)?; let time_context = @@ -230,17 +270,26 @@ fn inactive_time_range_timestamp<'r, 's>( let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); - Ok((remaining, Timestamp { source })) + Ok(( + remaining, + Timestamp { + source: source.into(), + }, + )) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn date<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { - let (remaining, _year) = verify(digit1, |year: &str| year.len() == 4)(input)?; +fn date<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { + let (remaining, _year) = verify(digit1, |year: &OrgSource<'_>| year.len() == 4)(input)?; let (remaining, _) = tag("-")(remaining)?; - let (remaining, _month) = verify(digit1, |month: &str| month.len() == 2)(remaining)?; + let (remaining, _month) = verify(digit1, |month: &OrgSource<'_>| month.len() == 2)(remaining)?; let (remaining, _) = tag("-")(remaining)?; - let (remaining, _day_of_month) = - verify(digit1, |day_of_month: &str| day_of_month.len() == 2)(remaining)?; + let (remaining, _day_of_month) = verify(digit1, |day_of_month: &OrgSource<'_>| { + day_of_month.len() == 2 + })(remaining)?; let (remaining, _dayname) = opt(tuple((space1, parser_with_context!(dayname)(context))))(remaining)?; let source = get_consumed(input, remaining); @@ -248,7 +297,10 @@ fn date<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s st } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn dayname<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn dayname<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let parser_context = context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, @@ -267,25 +319,36 @@ fn dayname<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn dayname_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn dayname_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(verify(anychar, |c| { c.is_whitespace() || "+-]>0123456789\n".contains(*c) }))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn time<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { - let (remaining, _hour) = - verify(digit1, |hour: &str| hour.len() >= 1 && hour.len() <= 2)(input)?; +fn time<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { + let (remaining, _hour) = verify(digit1, |hour: &OrgSource<'_>| { + hour.len() >= 1 && hour.len() <= 2 + })(input)?; let (remaining, _) = tag(":")(remaining)?; - let (remaining, _minute) = verify(digit1, |minute: &str| minute.len() == 2)(remaining)?; + let (remaining, _minute) = + verify(digit1, |minute: &OrgSource<'_>| minute.len() == 2)(remaining)?; let (remaining, _time_rest) = opt(parser_with_context!(time_rest)(context))(remaining)?; let source = get_consumed(input, remaining); Ok((remaining, source)) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn time_rest<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn time_rest<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { let (remaining, body) = recognize(verify( many_till(anychar, parser_with_context!(exit_matcher_parser)(context)), |(body, _end_contents)| !body.is_empty(), @@ -295,7 +358,10 @@ fn time_rest<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, & } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn active_time_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn active_time_rest_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { alt(( recognize(verify(anychar, |c| ">\n".contains(*c))), recognize(tuple((space1, parser_with_context!(repeater)(context)))), @@ -309,8 +375,8 @@ fn active_time_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn inactive_time_rest_end<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { alt(( recognize(verify(anychar, |c| "]\n".contains(*c))), recognize(tuple((space1, parser_with_context!(repeater)(context)))), @@ -322,7 +388,10 @@ fn inactive_time_rest_end<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn time_range_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn time_range_rest_end<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, 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); @@ -332,7 +401,10 @@ fn time_range_rest_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res< } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn repeater<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn repeater<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { // + for cumulative type // ++ for catch-up type // .+ for restart type @@ -345,7 +417,10 @@ fn repeater<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &' } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn warning_delay<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +fn warning_delay<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { // - for all type // -- for first type let (remaining, _mark) = alt((tag("--"), tag("-")))(input)?; diff --git a/src/parser/util.rs b/src/parser/util.rs index b858a77..64ba6bf 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -50,7 +50,9 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name: } /// Get one character from before the current position. -pub fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Option<&'s str> { +pub fn get_one_before<'s>(document: &'s str, current_position: OrgSource<'s>) -> Option<&'s str> { + // TODO: This should be replaced with new logic now that we are wrapping the input type. + let current_position = Into::<&str>::into(¤t_position); assert!(is_slice_of(document, current_position)); if document.as_ptr() as usize == current_position.as_ptr() as usize { return None; @@ -63,8 +65,10 @@ pub fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Optio /// Get the line current_position is on up until current_position pub fn get_current_line_before_position<'s>( document: &'s str, - current_position: &'s str, -) -> Option<&'s str> { + current_position: OrgSource<'s>, +) -> Option> { + // TODO: This should be replaced with new logic now that we are wrapping the input type. + let current_position = Into::<&str>::into(¤t_position); assert!(is_slice_of(document, current_position)); if document.as_ptr() as usize == current_position.as_ptr() as usize { return None; @@ -83,7 +87,7 @@ pub fn get_current_line_before_position<'s>( } previous_character_offset = new_offset; } - Some(&document[previous_character_offset..offset]) + Some((&document[previous_character_offset..offset]).into()) } /// Check if the child string slice is a slice of the parent string slice. @@ -96,29 +100,23 @@ fn is_slice_of(parent: &str, child: &str) -> bool { } /// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser. -pub fn get_consumed<'s>(input: &'s str, remaining: &'s str) -> &'s str { +pub fn get_consumed<'s>(input: OrgSource<'s>, remaining: OrgSource<'s>) -> OrgSource<'s> { + // TODO: This should be replaced with new logic now that we are wrapping the input type. + let input = Into::<&str>::into(&input); + let remaining = Into::<&str>::into(&remaining); assert!(is_slice_of(input, remaining)); let source = { let offset = remaining.as_ptr() as usize - input.as_ptr() as usize; &input[..offset] }; - source + source.into() } /// A line containing only whitespace and then a line break /// /// It is up to the caller to ensure this is called at the start of a line. #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn blank_line(input: &str) -> Res<&str, &str> { - not(eof)(input)?; - recognize(tuple((space0, alt((line_ending, eof)))))(input) -} - -/// A line containing only whitespace and then a line break -/// -/// It is up to the caller to ensure this is called at the start of a line. -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn new_blank_line<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { +pub fn blank_line(input: OrgSource<'_>) -> Res, OrgSource<'_>> { not(eof)(input)?; recognize(tuple((space0, alt((line_ending, eof)))))(input) } @@ -126,8 +124,8 @@ pub fn new_blank_line<'s>(input: OrgSource<'s>) -> Res, OrgSource< #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn element_trailing_whitespace<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { start_of_line(context, input)?; alt((eof, recognize(many0(blank_line))))(input) } @@ -135,8 +133,8 @@ pub fn element_trailing_whitespace<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn maybe_consume_trailing_whitespace_if_not_exiting<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Option<&'s str>> { + input: OrgSource<'s>, +) -> Res, Option>> { if context.should_consume_trailing_whitespace() && exit_matcher_parser(context, input).is_err() { Ok(opt(parser_with_context!(element_trailing_whitespace)( @@ -150,8 +148,8 @@ pub fn maybe_consume_trailing_whitespace_if_not_exiting<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn maybe_consume_trailing_whitespace<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Option<&'s str>> { + input: OrgSource<'s>, +) -> Res, Option>> { if context.should_consume_trailing_whitespace() { Ok(opt(parser_with_context!(element_trailing_whitespace)( context, @@ -162,13 +160,16 @@ pub fn maybe_consume_trailing_whitespace<'r, 's>( } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn trailing_whitespace(input: &str) -> Res<&str, &str> { +pub fn trailing_whitespace(input: OrgSource<'_>) -> Res, OrgSource<'_>> { alt((eof, recognize(tuple((line_ending, many0(blank_line))))))(input) } /// Check that we are at the start of a line #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { +pub fn start_of_line<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -178,7 +179,7 @@ pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&' Some(_) => { // Not at start of line, cannot be a heading return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not at start of line", + "Not at start of line".into(), )))); } // If None, we are at the start of the file which allows for headings @@ -191,8 +192,8 @@ pub fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&' #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn preceded_by_whitespace<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, ()> { + input: OrgSource<'s>, +) -> Res, ()> { let document_root = context.get_document_root().unwrap(); let preceding_character = get_one_before(document_root, input) .map(|slice| slice.chars().next()) @@ -202,7 +203,7 @@ pub fn preceded_by_whitespace<'r, 's>( // If None, we are at the start of the file which is not allowed None | Some(_) => { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not preceded by whitespace.", + "Not preceded by whitespace.".into(), )))); } }; @@ -213,7 +214,7 @@ pub fn preceded_by_whitespace<'r, 's>( /// /// This function only operates on spaces, tabs, carriage returns, and line feeds. It does not handle fancy unicode whitespace. #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn non_whitespace_character(input: &str) -> Res<&str, char> { +pub fn non_whitespace_character(input: OrgSource<'_>) -> Res, char> { none_of(" \t\r\n")(input) } @@ -221,25 +222,31 @@ pub fn non_whitespace_character(input: &str) -> Res<&str, char> { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn exit_matcher_parser<'r, 's>( context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, &'s str> { + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { peek(|i| context.check_exit_matcher(i))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn always_fail<'r, 's>(_context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +pub fn always_fail<'r, 's>( + _context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { Err(nom::Err::Error(CustomError::MyError(MyError( - "Always fail", + "Always fail".into(), )))) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn whitespace_eof(input: &str) -> Res<&str, &str> { +pub fn whitespace_eof(input: OrgSource<'_>) -> Res, OrgSource<'_>> { recognize(tuple((multispace0, eof)))(input) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn text_until_exit<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { +pub fn text_until_exit<'r, 's>( + context: Context<'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { recognize(verify( many_till(anychar, parser_with_context!(exit_matcher_parser)(context)), |(children, _exit_contents)| !children.is_empty(), @@ -247,9 +254,9 @@ pub fn text_until_exit<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res< } #[allow(dead_code)] -pub fn not_yet_implemented() -> Res<&'static str, ()> { +pub fn not_yet_implemented() -> Res, ()> { return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not implemented yet.", + "Not implemented yet.".into(), )))); } From 720afa5d3227d90d0958a9a829484f7ea540ea44 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 16:55:56 -0400 Subject: [PATCH 13/19] Update getting the previous character and previous line. This can be done a lot more efficiently now that we are keeping track of this information in the wrapped input type instead of having to fetch to the original document out of the context tree. --- src/parser/latex_fragment.rs | 11 +--- src/parser/line_break.rs | 29 +++-------- src/parser/org_source.rs | 32 ++++++++++++ src/parser/plain_link.rs | 6 +-- src/parser/radio_link.rs | 7 +-- src/parser/subscript_and_superscript.rs | 6 +-- src/parser/target.rs | 7 +-- src/parser/text_markup.rs | 6 +-- src/parser/util.rs | 69 +++---------------------- 9 files changed, 59 insertions(+), 114 deletions(-) diff --git a/src/parser/latex_fragment.rs b/src/parser/latex_fragment.rs index 75073eb..65ef846 100644 --- a/src/parser/latex_fragment.rs +++ b/src/parser/latex_fragment.rs @@ -21,7 +21,6 @@ use crate::error::Res; 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::get_one_before; use crate::parser::LatexFragment; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -175,10 +174,7 @@ fn dollar_char_fragment<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); if let Some('$') = preceding_character { return Err(nom::Err::Error(CustomError::MyError(MyError( "Not a valid pre character for dollar char fragment.".into(), @@ -239,10 +235,7 @@ pub fn close_border<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); match preceding_character { Some(c) if !c.is_whitespace() && !".,;$".contains(c) => Ok((input, ())), _ => { diff --git a/src/parser/line_break.rs b/src/parser/line_break.rs index 6d359e5..cc08400 100644 --- a/src/parser/line_break.rs +++ b/src/parser/line_break.rs @@ -10,8 +10,6 @@ use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::util::get_consumed; -use crate::parser::util::get_current_line_before_position; -use crate::parser::util::get_one_before; use crate::parser::LineBreak; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -34,10 +32,7 @@ pub fn line_break<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); match preceding_character { // If None, we are at the start of the file None | Some('\\') => { @@ -47,21 +42,13 @@ fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res {} }; - let current_line = get_current_line_before_position(document_root, input); - match current_line { - Some(line) => { - let is_non_empty_line = Into::<&str>::into(line).chars().any(|c| !c.is_whitespace()); - if !is_non_empty_line { - return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not a valid pre line for line break.".into(), - )))); - } - } - None => { - return Err(nom::Err::Error(CustomError::MyError(MyError( - "No preceding line for line break.".into(), - )))); - } + + let current_line = input.text_since_line_break(); + let is_non_empty_line = current_line.chars().any(|c| !c.is_whitespace()); + if !is_non_empty_line { + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Not a valid pre line for line break.".into(), + )))); } Ok((input, ())) diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index a3ac2ac..5ebc037 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -17,6 +17,7 @@ pub struct OrgSource<'s> { start: usize, end: usize, // exclusive preceding_line_break: Option, + preceding_character: Option, } impl<'s> OrgSource<'s> { @@ -29,6 +30,7 @@ impl<'s> OrgSource<'s> { start: 0, end: input.len(), preceding_line_break: None, + preceding_character: None, } } @@ -41,6 +43,17 @@ impl<'s> OrgSource<'s> { pub fn len(&self) -> usize { self.end - self.start } + + pub fn get_preceding_character(&self) -> Option { + todo!() + } + + pub fn is_at_start_of_line(&self) -> bool { + self.start == 0 || match self.preceding_line_break { + Some(offset) => offset == self.start - 1, + None => false, + } + } } impl<'s> InputTake for OrgSource<'s> { @@ -114,6 +127,7 @@ where start: new_start, end: new_end, preceding_line_break: last_line_feed, + preceding_character: skipped_text.chars().last() } } } @@ -325,4 +339,22 @@ mod tests { assert_eq!(input.slice(6..).text_since_line_break(), ""); assert_eq!(input.slice(6..).slice(10..).text_since_line_break(), "ba"); } + + #[test] + fn preceding_character() { + let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum"); + assert_eq!(input.get_preceding_character(), None); + assert_eq!(input.slice(5..).get_preceding_character(), Some('m')); + assert_eq!(input.slice(6..).get_preceding_character(), Some('\n')); + assert_eq!(input.slice(6..).slice(10..).get_preceding_character(), Some('a')); + } + + #[test] + fn is_at_start_of_line() { + let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum"); + assert_eq!(input.is_at_start_of_line(), true); + assert_eq!(input.slice(5..).is_at_start_of_line(), false); + assert_eq!(input.slice(6..).is_at_start_of_line(), true); + assert_eq!(input.slice(6..).slice(10..).is_at_start_of_line(), false); + } } diff --git a/src/parser/plain_link.rs b/src/parser/plain_link.rs index 8c99a5d..534d6cb 100644 --- a/src/parser/plain_link.rs +++ b/src/parser/plain_link.rs @@ -21,7 +21,6 @@ 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::get_one_before; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -47,10 +46,7 @@ pub fn plain_link<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); match preceding_character { // If None, we are at the start of the file which is fine None => {} diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index 8f72205..1bef234 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -156,7 +156,7 @@ mod tests { crate::parser::Element::Paragraph(paragraph) => paragraph, _ => panic!("Should be a paragraph!"), }; - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(first_paragraph.get_source(), "foo bar baz"); assert_eq!(first_paragraph.children.len(), 3); assert_eq!( @@ -183,12 +183,13 @@ mod tests { .with_additional_node(ContextElement::DocumentRoot(input)) .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); 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.into()).expect("Parse first paragraph"); let first_paragraph = match first_paragraph { crate::parser::Element::Paragraph(paragraph) => paragraph, _ => panic!("Should be a paragraph!"), }; - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(first_paragraph.get_source(), "foo *bar* baz"); assert_eq!(first_paragraph.children.len(), 3); assert_eq!( diff --git a/src/parser/subscript_and_superscript.rs b/src/parser/subscript_and_superscript.rs index 529dc76..fa94468 100644 --- a/src/parser/subscript_and_superscript.rs +++ b/src/parser/subscript_and_superscript.rs @@ -25,7 +25,6 @@ use crate::parser::parser_context::SubscriptSuperscriptBrace; 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::get_one_before; use crate::parser::Subscript; use crate::parser::Superscript; @@ -69,10 +68,7 @@ pub fn superscript<'r, 's>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); match preceding_character { Some(c) if !c.is_whitespace() => {} _ => { diff --git a/src/parser/target.rs b/src/parser/target.rs index 02c5aa1..026b31f 100644 --- a/src/parser/target.rs +++ b/src/parser/target.rs @@ -18,7 +18,6 @@ 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::get_one_before; use crate::parser::Target; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -41,10 +40,8 @@ pub fn target<'r, 's>( parser_with_context!(exit_matcher_parser)(&parser_context), ))(remaining)?; - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, remaining) - .map(|slice| slice.chars().next()) - .flatten() + let preceding_character = remaining + .get_preceding_character() .expect("We cannot be at the start of the file because we are inside a target."); if preceding_character.is_whitespace() { return Err(nom::Err::Error(CustomError::MyError(MyError( diff --git a/src/parser/text_markup.rs b/src/parser/text_markup.rs index fc340be..3dd98c7 100644 --- a/src/parser/text_markup.rs +++ b/src/parser/text_markup.rs @@ -29,7 +29,6 @@ use crate::parser::parser_with_context::parser_with_context; use crate::parser::radio_link::rematch_target; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::util::get_one_before; use crate::parser::util::preceded_by_whitespace; use crate::parser::Bold; use crate::parser::Code; @@ -261,10 +260,7 @@ fn _text_markup_string<'r, 's, 'x>( #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn pre<'r, 's>(context: Context<'r, 's>, input: OrgSource<'s>) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); match preceding_character { // If None, we are at the start of the file which is technically the beginning of a line. None | Some('\r') | Some('\n') | Some(' ') | Some('\t') | Some('-') | Some('(') diff --git a/src/parser/util.rs b/src/parser/util.rs index 64ba6bf..a62197b 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -49,47 +49,6 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name: false } -/// Get one character from before the current position. -pub fn get_one_before<'s>(document: &'s str, current_position: OrgSource<'s>) -> Option<&'s str> { - // TODO: This should be replaced with new logic now that we are wrapping the input type. - let current_position = Into::<&str>::into(¤t_position); - assert!(is_slice_of(document, current_position)); - if document.as_ptr() as usize == current_position.as_ptr() as usize { - return None; - } - let offset = current_position.as_ptr() as usize - document.as_ptr() as usize; - let previous_character_offset = document.floor_char_boundary(offset - 1); - Some(&document[previous_character_offset..offset]) -} - -/// Get the line current_position is on up until current_position -pub fn get_current_line_before_position<'s>( - document: &'s str, - current_position: OrgSource<'s>, -) -> Option> { - // TODO: This should be replaced with new logic now that we are wrapping the input type. - let current_position = Into::<&str>::into(¤t_position); - assert!(is_slice_of(document, current_position)); - if document.as_ptr() as usize == current_position.as_ptr() as usize { - return None; - } - let offset = current_position.as_ptr() as usize - document.as_ptr() as usize; - let mut previous_character_offset = offset; - loop { - let new_offset = document.floor_char_boundary(previous_character_offset - 1); - let new_line = &document[new_offset..offset]; - let leading_char = new_line - .chars() - .next() - .expect("Impossible to not have at least 1 character to read."); - if "\r\n".contains(leading_char) || new_offset == 0 { - break; - } - previous_character_offset = new_offset; - } - Some((&document[previous_character_offset..offset]).into()) -} - /// Check if the child string slice is a slice of the parent string slice. fn is_slice_of(parent: &str, child: &str) -> bool { let parent_start = parent.as_ptr() as usize; @@ -170,22 +129,13 @@ pub fn start_of_line<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); - match preceding_character { - Some('\n') => {} - Some(_) => { - // Not at start of line, cannot be a heading - return Err(nom::Err::Error(CustomError::MyError(MyError( - "Not at start of line".into(), - )))); - } - // If None, we are at the start of the file which allows for headings - None => {} - }; - Ok((input, ())) + if input.is_at_start_of_line() { + Ok((input, ())) + } else { + Err(nom::Err::Error(CustomError::MyError(MyError( + "Not at start of line".into(), + )))) + } } /// Check that we are at the start of a line @@ -194,10 +144,7 @@ pub fn preceded_by_whitespace<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, ()> { - let document_root = context.get_document_root().unwrap(); - let preceding_character = get_one_before(document_root, input) - .map(|slice| slice.chars().next()) - .flatten(); + let preceding_character = input.get_preceding_character(); match preceding_character { Some('\n') | Some('\r') | Some(' ') | Some('\t') => {} // If None, we are at the start of the file which is not allowed From 3348807a059315f695a436fc53e48af8fde6a72b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 17:01:12 -0400 Subject: [PATCH 14/19] Eliminate the document root context element. --- src/parser/citation.rs | 9 +++------ src/parser/document.rs | 8 +++----- src/parser/parser_context.rs | 18 ------------------ 3 files changed, 6 insertions(+), 29 deletions(-) diff --git a/src/parser/citation.rs b/src/parser/citation.rs index dae8b31..e19938b 100644 --- a/src/parser/citation.rs +++ b/src/parser/citation.rs @@ -203,24 +203,21 @@ fn global_suffix_end<'r, 's>( mod tests { use super::*; use crate::parser::element_parser::element; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::source::Source; #[test] fn citation_simple() { - let input = "[cite:@foo]"; + let input = OrgSource::new("[cite:@foo]"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let paragraph_matcher = parser_with_context!(element(true))(&document_context); + let paragraph_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let first_paragraph = match first_paragraph { crate::parser::Element::Paragraph(paragraph) => 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.children.len(), 1); assert_eq!( diff --git a/src/parser/document.rs b/src/parser/document.rs index 09ddcd5..29dd89f 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -98,9 +98,7 @@ impl<'s> Source<'s> for Heading<'s> { pub fn document(input: &str) -> Res<&str, Document> { let initial_context: ContextTree<'_, '_> = ContextTree::new(); let wrapped_input = OrgSource::new(input); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - 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_err(convert_error)?; { @@ -118,9 +116,9 @@ pub fn document(input: &str) -> Res<&str, Document> { .map(|rt| &rt.children) .collect(); if !all_radio_targets.is_empty() { - let document_context = document_context + let initial_context = initial_context .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_err(convert_error)?; return Ok((remaining.into(), document)); diff --git a/src/parser/parser_context.rs b/src/parser/parser_context.rs index 7001072..1369cd9 100644 --- a/src/parser/parser_context.rs +++ b/src/parser/parser_context.rs @@ -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. /// /// Defaults to true. @@ -121,11 +108,6 @@ impl<'r, 's> ContextTree<'r, 's> { #[derive(Debug)] 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. ExitMatcherNode(ExitMatcherNode<'r>), Context(&'r str), From e84e2b5147cc04848f1a2cb11f7d1b322c387a52 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 17:15:24 -0400 Subject: [PATCH 15/19] Update tests to compile again. --- src/parser/comment.rs | 13 +++-- src/parser/footnote_definition.rs | 25 +++++----- src/parser/org_source.rs | 10 +++- src/parser/paragraph.rs | 10 ++-- src/parser/plain_list.rs | 81 +++++++++++++------------------ src/parser/plain_text.rs | 11 ++--- src/parser/radio_link.rs | 6 +-- src/parser/util.rs | 15 ------ 8 files changed, 71 insertions(+), 100 deletions(-) diff --git a/src/parser/comment.rs b/src/parser/comment.rs index c301a5f..8162877 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -69,22 +69,21 @@ fn comment_line<'r, 's>( #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; #[test] fn require_space_after_hash() { - let input = "# Comment line + let input = OrgSource::new( + "# Comment line #not a comment -# Comment again"; +# Comment again", + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let comment_matcher = parser_with_context!(comment)(&document_context); + let comment_matcher = parser_with_context!(comment)(&initial_context); let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment"); assert_eq!( - remaining, + Into::<&str>::into(remaining), r#"#not a comment # Comment again"# ); diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 738c324..91eceb2 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -111,27 +111,26 @@ fn footnote_definition_end<'r, 's>( #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::Source; #[test] fn two_paragraphs() { - let input = "[fn:1] A footnote. + let input = OrgSource::new( + "[fn:1] A footnote. [fn:2] A multi- -line footnote."; +line footnote.", + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let footnote_definition_matcher = parser_with_context!(element(true))(&document_context); + let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_footnote_definition) = footnote_definition_matcher(input).expect("Parse first footnote_definition"); let (remaining, second_footnote_definition) = footnote_definition_matcher(remaining).expect("Parse second footnote_definition."); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!( first_footnote_definition.get_source(), "[fn:1] A footnote. @@ -148,19 +147,19 @@ line footnote." #[test] fn multiline_break() { - let input = "[fn:2] A multi- + let input = OrgSource::new( + "[fn:2] A multi- line footnote. -not in the footnote."; +not in the footnote.", + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let footnote_definition_matcher = parser_with_context!(element(true))(&document_context); + let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_footnote_definition) = footnote_definition_matcher(input).expect("Parse first footnote_definition"); - assert_eq!(remaining, "not in the footnote."); + assert_eq!(Into::<&str>::into(remaining), "not in the footnote."); assert_eq!( first_footnote_definition.get_source(), "[fn:2] A multi- diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 5ebc037..1aecc01 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -45,7 +45,7 @@ impl<'s> OrgSource<'s> { } pub fn get_preceding_character(&self) -> Option { - todo!() + self.preceding_character } pub fn is_at_start_of_line(&self) -> bool { @@ -357,4 +357,12 @@ mod tests { assert_eq!(input.slice(6..).is_at_start_of_line(), true); assert_eq!(input.slice(6..).slice(10..).is_at_start_of_line(), false); } + + #[test] + fn preceding_character_unicode() { + let input = OrgSource::new("๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ"); + assert_eq!(input.get_preceding_character(), None); + assert_eq!(input.slice(8..).get_preceding_character(), Some('๐Ÿ’›')); + } + } diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 65f11b7..8f91e12 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -69,22 +69,20 @@ fn paragraph_end<'r, 's>( #[cfg(test)] mod tests { use crate::parser::element_parser::element; - use crate::parser::parser_context::ContextElement; + use crate::parser::org_source::OrgSource; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::source::Source; #[test] fn two_paragraphs() { - let input = "foo bar baz\n\nlorem ipsum"; + let input = OrgSource::new("foo bar baz\n\nlorem ipsum"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let paragraph_matcher = parser_with_context!(element(true))(&document_context); + let paragraph_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, second_paragraph) = paragraph_matcher(remaining).expect("Parse second paragraph."); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(first_paragraph.get_source(), "foo bar baz\n\n"); assert_eq!(second_paragraph.get_source(), "lorem ipsum"); } diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index b3edfae..810784e 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -239,67 +239,56 @@ fn get_context_item_indent<'r, 's>(context: Context<'r, 's>) -> Option<&'r usize #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::Source; #[test] fn plain_list_item_empty() { - let input = "1."; + let input = OrgSource::new("1."); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context); + let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context); let (remaining, result) = plain_list_item_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1."); } #[test] fn plain_list_item_simple() { - let input = "1. foo"; + let input = OrgSource::new("1. foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context); + let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context); let (remaining, result) = plain_list_item_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1. foo"); } #[test] fn plain_list_empty() { - let input = "1."; + let input = OrgSource::new("1."); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let (remaining, result) = plain_list_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1."); } #[test] fn plain_list_simple() { - let input = "1. foo"; + let input = OrgSource::new("1. foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let (remaining, result) = plain_list_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1. foo"); } #[test] fn plain_list_cant_start_line_with_asterisk() { // Plain lists with an asterisk bullet must be indented or else they would be a headline - let input = "* foo"; + let input = OrgSource::new("* foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let result = plain_list_matcher(input); assert!(result.is_err()); } @@ -307,32 +296,30 @@ mod tests { #[test] fn indented_can_start_line_with_asterisk() { // Plain lists with an asterisk bullet must be indented or else they would be a headline - let input = " * foo"; + let input = OrgSource::new(" * foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let result = plain_list_matcher(input); assert!(result.is_ok()); } #[test] fn two_blank_lines_ends_list() { - let input = r#"1. foo + let input = OrgSource::new( + r#"1. foo 2. bar baz 3. lorem ipsum -"#; +"#, + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(element(true))(&document_context); + let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, result) = plain_list_matcher(input).expect("Should parse the plain list successfully."); - assert_eq!(remaining, " ipsum\n"); + assert_eq!(Into::<&str>::into(remaining), " ipsum\n"); assert_eq!( result.get_source(), r#"1. foo @@ -347,18 +334,18 @@ mod tests { #[test] fn two_blank_lines_ends_nested_list() { - let input = r#"1. foo + let input = OrgSource::new( + r#"1. foo 1. bar -baz"#; +baz"#, + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(element(true))(&document_context); + let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, result) = plain_list_matcher(input).expect("Should parse the plain list successfully."); - assert_eq!(remaining, "baz"); + assert_eq!(Into::<&str>::into(remaining), "baz"); assert_eq!( result.get_source(), r#"1. foo @@ -371,7 +358,8 @@ baz"#; #[test] fn interior_trailing_whitespace() { - let input = r#"1. foo + let input = OrgSource::new( + r#"1. foo bar @@ -382,14 +370,13 @@ baz"#; ipsum -dolar"#; +dolar"#, + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(element(true))(&document_context); + let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, result) = plain_list_matcher(input).expect("Should parse the plain list successfully."); - assert_eq!(remaining, "dolar"); + assert_eq!(Into::<&str>::into(remaining), "dolar"); assert_eq!( result.get_source(), r#"1. foo diff --git a/src/parser/plain_text.rs b/src/parser/plain_text.rs index bdc9deb..7d0423d 100644 --- a/src/parser/plain_text.rs +++ b/src/parser/plain_text.rs @@ -69,20 +69,17 @@ mod tests { use nom::combinator::map; use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::source::Source; #[test] fn plain_text_simple() { - let input = "foobarbaz"; + let input = OrgSource::new("foobarbaz"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_text_matcher = parser_with_context!(plain_text)(&document_context); + let plain_text_matcher = parser_with_context!(plain_text)(&initial_context); let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap(); - assert_eq!(remaining, ""); - assert_eq!(result.get_source(), input); + assert_eq!(Into::<&str>::into(remaining), ""); + assert_eq!(result.get_source(), Into::<&str>::into(input)); } } diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index 1bef234..e52d270 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -144,11 +144,10 @@ mod tests { #[test] fn plain_text_radio_target() { - let input = "foo bar baz"; + let input = OrgSource::new("foo bar baz"); let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })]; let initial_context: ContextTree<'_, '_> = ContextTree::new(); let document_context = initial_context - .with_additional_node(ContextElement::DocumentRoot(input)) .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); let paragraph_matcher = parser_with_context!(element(true))(&document_context); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); @@ -173,14 +172,13 @@ mod tests { #[test] fn bold_radio_target() { - let input = "foo *bar* baz"; + let input = OrgSource::new("foo *bar* baz"); let radio_target_match = vec![Object::Bold(Bold { source: "*bar*", children: vec![Object::PlainText(PlainText { source: "bar" })], })]; let initial_context: ContextTree<'_, '_> = ContextTree::new(); let document_context = initial_context - .with_additional_node(ContextElement::DocumentRoot(input)) .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); let paragraph_matcher = parser_with_context!(element(true))(&document_context); let (remaining, first_paragraph) = diff --git a/src/parser/util.rs b/src/parser/util.rs index a62197b..2306cd5 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -206,18 +206,3 @@ pub fn not_yet_implemented() -> Res, ()> { "Not implemented yet.".into(), )))); } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn get_one_before_unicode() { - let input = "๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ"; - let (green_heart_index, _) = input.char_indices().skip(2).next().unwrap(); - let starting_with_green_heart = &input[green_heart_index..]; - let yellow_heart = get_one_before(input, starting_with_green_heart).unwrap(); - assert!(is_slice_of(input, yellow_heart)); - assert_eq!(yellow_heart, "๐Ÿ’›"); - } -} From 32071ce74d0bd1480d01590e9e50b6c7f08659ce Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 17:23:16 -0400 Subject: [PATCH 16/19] Fix handling of start of line in OrgSource. --- src/parser/inline_source_block.rs | 4 ++-- src/parser/org_source.rs | 33 +++++++++++++++---------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/parser/inline_source_block.rs b/src/parser/inline_source_block.rs index 0d7bef7..304894c 100644 --- a/src/parser/inline_source_block.rs +++ b/src/parser/inline_source_block.rs @@ -156,7 +156,7 @@ fn body<'r, 's>( let span = span!( tracing::Level::DEBUG, "outside end body", - remaining = remaining + remaining = Into::<&str>::into(remaining) ); #[cfg(feature = "tracing")] let _enter = span.enter(); @@ -193,7 +193,7 @@ fn body_end<'r, 's>( let span = span!( tracing::Level::DEBUG, "inside end body", - remaining = input, + remaining = Into::<&str>::into(input), current_depth = current_depth ); #[cfg(feature = "tracing")] diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 1aecc01..3562944 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -16,7 +16,7 @@ pub struct OrgSource<'s> { full_source: &'s str, start: usize, end: usize, // exclusive - preceding_line_break: Option, + start_of_line: usize, preceding_character: Option, } @@ -29,15 +29,14 @@ impl<'s> OrgSource<'s> { full_source: input, start: 0, end: input.len(), - preceding_line_break: None, + start_of_line: 0, preceding_character: None, } } /// Get the text since the line break preceding the start of this WrappedInput. pub fn text_since_line_break(&self) -> &'s str { - let start = self.preceding_line_break.unwrap_or(0); - &self.full_source[start..self.start] + &self.full_source[self.start_of_line..self.start] } pub fn len(&self) -> usize { @@ -49,10 +48,7 @@ impl<'s> OrgSource<'s> { } pub fn is_at_start_of_line(&self) -> bool { - self.start == 0 || match self.preceding_line_break { - Some(offset) => offset == self.start - 1, - None => false, - } + self.start == self.start_of_line } } @@ -117,17 +113,18 @@ where } let skipped_text = &self.full_source[self.start..new_start]; - let last_line_feed = skipped_text + let start_of_line = skipped_text .rfind('\n') - .map(|idx| idx + self.preceding_line_break.unwrap_or(0) + 1); + .map(|idx| self.start + idx + 1) + .unwrap_or(self.start_of_line); // TODO: calculate updated values for WrappedInput OrgSource { full_source: self.full_source, start: new_start, end: new_end, - preceding_line_break: last_line_feed, - preceding_character: skipped_text.chars().last() + start_of_line, + preceding_character: skipped_text.chars().last(), } } } @@ -326,9 +323,9 @@ mod tests { #[test] fn line_break() { let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum"); - assert_eq!(input.slice(5..).preceding_line_break, None); - assert_eq!(input.slice(6..).preceding_line_break, Some(6)); - assert_eq!(input.slice(6..).slice(10..).preceding_line_break, Some(14)); + assert_eq!(input.slice(5..).start_of_line, 0); + assert_eq!(input.slice(6..).start_of_line, 6); + assert_eq!(input.slice(6..).slice(10..).start_of_line, 14); } #[test] @@ -346,7 +343,10 @@ mod tests { assert_eq!(input.get_preceding_character(), None); assert_eq!(input.slice(5..).get_preceding_character(), Some('m')); assert_eq!(input.slice(6..).get_preceding_character(), Some('\n')); - assert_eq!(input.slice(6..).slice(10..).get_preceding_character(), Some('a')); + assert_eq!( + input.slice(6..).slice(10..).get_preceding_character(), + Some('a') + ); } #[test] @@ -364,5 +364,4 @@ mod tests { assert_eq!(input.get_preceding_character(), None); assert_eq!(input.slice(8..).get_preceding_character(), Some('๐Ÿ’›')); } - } From 64e34816603856839d6be2fa5b6b396dc26a279c Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 18:33:40 -0400 Subject: [PATCH 17/19] Update get_consumed to use the new wrapped input type. --- src/parser/org_source.rs | 7 ++++++- src/parser/util.rs | 19 +------------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 3562944..9789c8e 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -50,6 +50,12 @@ impl<'s> OrgSource<'s> { pub fn is_at_start_of_line(&self) -> bool { self.start == self.start_of_line } + + pub fn get_until(&self, other: OrgSource<'s>) -> OrgSource<'s> { + assert!(other.start >= self.start); + assert!(other.end <= self.end); + self.slice(..(other.start - self.start)) + } } impl<'s> InputTake for OrgSource<'s> { @@ -118,7 +124,6 @@ where .map(|idx| self.start + idx + 1) .unwrap_or(self.start_of_line); - // TODO: calculate updated values for WrappedInput OrgSource { full_source: self.full_source, start: new_start, diff --git a/src/parser/util.rs b/src/parser/util.rs index 2306cd5..f70685a 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -49,26 +49,9 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name: false } -/// Check if the child string slice is a slice of the parent string slice. -fn is_slice_of(parent: &str, child: &str) -> bool { - let parent_start = parent.as_ptr() as usize; - let parent_end = parent_start + parent.len(); - let child_start = child.as_ptr() as usize; - let child_end = child_start + child.len(); - child_start >= parent_start && child_end <= parent_end -} - /// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser. pub fn get_consumed<'s>(input: OrgSource<'s>, remaining: OrgSource<'s>) -> OrgSource<'s> { - // TODO: This should be replaced with new logic now that we are wrapping the input type. - let input = Into::<&str>::into(&input); - let remaining = Into::<&str>::into(&remaining); - assert!(is_slice_of(input, remaining)); - let source = { - let offset = remaining.as_ptr() as usize - input.as_ptr() as usize; - &input[..offset] - }; - source.into() + input.get_until(remaining) } /// A line containing only whitespace and then a line break From e5224cda632a493d9b3c0be4d3c9684e3478dff9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 18:39:55 -0400 Subject: [PATCH 18/19] Removing dead code. --- src/parser/footnote_reference.rs | 6 ------ src/parser/lesser_block.rs | 2 +- src/parser/util.rs | 21 --------------------- 3 files changed, 1 insertion(+), 28 deletions(-) diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index 10956a8..74849b7 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -20,7 +20,6 @@ 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::FootnoteReference; -use crate::parser::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn footnote_reference<'r, 's>( @@ -134,11 +133,6 @@ fn footnote_reference_only<'r, 's>( )) } -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn definition<'s>(input: OrgSource<'s>) -> Res, Vec>> { - Ok((input, vec![])) -} - #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn footnote_definition_end<'r, 's>( context: Context<'r, 's>, diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 7a60c6c..8725def 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -124,7 +124,7 @@ pub fn example_block<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, ExampleBlock<'s>> { - let (remaining, name) = lesser_block_begin("example")(context, input)?; + let (remaining, _name) = lesser_block_begin("example")(context, input)?; let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let lesser_block_end_specialized = lesser_block_end("example"); diff --git a/src/parser/util.rs b/src/parser/util.rs index f70685a..7340abb 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -1,7 +1,6 @@ use nom::branch::alt; use nom::character::complete::anychar; use nom::character::complete::line_ending; -use nom::character::complete::multispace0; use nom::character::complete::none_of; use nom::character::complete::space0; use nom::combinator::eof; @@ -101,11 +100,6 @@ pub fn maybe_consume_trailing_whitespace<'r, 's>( } } -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn trailing_whitespace(input: OrgSource<'_>) -> Res, OrgSource<'_>> { - alt((eof, recognize(tuple((line_ending, many0(blank_line))))))(input) -} - /// Check that we are at the start of a line #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn start_of_line<'r, 's>( @@ -157,21 +151,6 @@ pub fn exit_matcher_parser<'r, 's>( peek(|i| context.check_exit_matcher(i))(input) } -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn always_fail<'r, 's>( - _context: Context<'r, 's>, - input: OrgSource<'s>, -) -> Res, OrgSource<'s>> { - Err(nom::Err::Error(CustomError::MyError(MyError( - "Always fail".into(), - )))) -} - -#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn whitespace_eof(input: OrgSource<'_>) -> Res, OrgSource<'_>> { - recognize(tuple((multispace0, eof)))(input) -} - #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn text_until_exit<'r, 's>( context: Context<'r, 's>, From cf37bc4111c85cb5139e952752fc1578a3ca144c Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 19:29:00 -0400 Subject: [PATCH 19/19] Remove unnecessary context from some util functions. --- src/parser/clock.rs | 2 +- src/parser/comment.rs | 2 +- src/parser/diary_sexp.rs | 2 +- src/parser/document.rs | 3 +-- src/parser/drawer.rs | 4 ++-- src/parser/dynamic_block.rs | 4 ++-- src/parser/fixed_width_area.rs | 2 +- src/parser/footnote_definition.rs | 5 ++--- src/parser/greater_block.rs | 4 ++-- src/parser/horizontal_rule.rs | 2 +- src/parser/keyword.rs | 2 +- src/parser/latex_environment.rs | 4 ++-- src/parser/lesser_block.rs | 4 ++-- src/parser/paragraph.rs | 3 +-- src/parser/plain_list.rs | 7 +++---- src/parser/planning.rs | 2 +- src/parser/property_drawer.rs | 6 +++--- src/parser/table.rs | 8 ++++---- src/parser/text_markup.rs | 2 +- src/parser/util.rs | 25 ++++++------------------- 20 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/parser/clock.rs b/src/parser/clock.rs index 36c84a7..035c3e7 100644 --- a/src/parser/clock.rs +++ b/src/parser/clock.rs @@ -24,7 +24,7 @@ pub fn clock<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, Clock<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _clock) = tag_no_case("clock:")(remaining)?; let (remaining, _gap_whitespace) = space1(remaining)?; diff --git a/src/parser/comment.rs b/src/parser/comment.rs index 8162877..1144c4d 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -55,7 +55,7 @@ fn comment_line<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _indent) = space0(input)?; let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple(( tag("#"), diff --git a/src/parser/diary_sexp.rs b/src/parser/diary_sexp.rs index 869311e..5b3ec36 100644 --- a/src/parser/diary_sexp.rs +++ b/src/parser/diary_sexp.rs @@ -19,7 +19,7 @@ pub fn diary_sexp<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, DiarySexp<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _clock) = tag("%%")(remaining)?; let (remaining, _gap_whitespace) = space0(remaining)?; diff --git a/src/parser/document.rs b/src/parser/document.rs index 29dd89f..957fff2 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -300,10 +300,9 @@ fn headline<'r, 's>( exit_matcher: &headline_end, })); let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context); - let start_of_line_matcher = parser_with_context!(start_of_line)(&parser_context); let (remaining, (_sol, star_count, ws, title, _line_ending)) = tuple(( - start_of_line_matcher, + start_of_line, many1_count(tag("*")), space1, many1(standard_set_object_matcher), diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index 4e6a842..fe1de33 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -41,7 +41,7 @@ pub fn drawer<'r, 's>( "Cannot nest objects of the same element".into(), )))); } - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple(( tag(":"), @@ -102,7 +102,7 @@ fn drawer_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; recognize(tuple(( space0, tag_no_case(":end:"), diff --git a/src/parser/dynamic_block.rs b/src/parser/dynamic_block.rs index 30739ff..69f4cca 100644 --- a/src/parser/dynamic_block.rs +++ b/src/parser/dynamic_block.rs @@ -42,7 +42,7 @@ pub fn dynamic_block<'r, 's>( "Cannot nest objects of the same element".into(), )))); } - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name, parameters, _ws)) = tuple(( recognize(tuple((tag_no_case("#+begin:"), space1))), @@ -110,7 +110,7 @@ fn dynamic_block_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, source) = recognize(tuple(( space0, tag_no_case("#+end:"), diff --git a/src/parser/fixed_width_area.rs b/src/parser/fixed_width_area.rs index 068556d..1083928 100644 --- a/src/parser/fixed_width_area.rs +++ b/src/parser/fixed_width_area.rs @@ -45,7 +45,7 @@ fn fixed_width_area_line<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _indent) = space0(input)?; let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple(( tag(":"), diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 91eceb2..6b64314 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -39,7 +39,7 @@ pub fn footnote_definition<'r, 's>( "Cannot nest objects of the same element".into(), )))); } - start_of_line(context, input)?; + start_of_line(input)?; // Cannot be indented. let (remaining, (_lead_in, lbl, _lead_out, _ws)) = tuple((tag_no_case("[fn:"), label, tag("]"), space0))(input)?; @@ -80,7 +80,6 @@ fn footnote_definition_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - let start_of_line_matcher = parser_with_context!(start_of_line)(context); let allow_nesting_context = context.with_additional_node(ContextElement::Context("allow nesting footnotes")); let footnote_definition_matcher = parser_with_context!(footnote_definition)( @@ -98,7 +97,7 @@ fn footnote_definition_end<'r, 's>( footnote_definition_matcher, ))), recognize(tuple(( - start_of_line_matcher, + start_of_line, verify(many1(blank_line), |lines: &Vec>| { lines.len() >= 2 }), diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index 62c042c..fa1ca69 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -37,7 +37,7 @@ pub fn greater_block<'r, 's>( input: OrgSource<'s>, ) -> Res, GreaterBlock<'s>> { // TODO: Do I need to differentiate between different greater block types. - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name)) = tuple(( tag_no_case("#+begin_"), @@ -125,7 +125,7 @@ fn greater_block_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let current_name: &str = get_context_greater_block_name(context).ok_or(nom::Err::Error( CustomError::MyError(MyError("Not inside a greater block".into())), ))?; diff --git a/src/parser/horizontal_rule.rs b/src/parser/horizontal_rule.rs index 88aa468..ad46eff 100644 --- a/src/parser/horizontal_rule.rs +++ b/src/parser/horizontal_rule.rs @@ -19,7 +19,7 @@ pub fn horizontal_rule<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, HorizontalRule<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, rule) = recognize(tuple(( space0, verify(many1_count(tag("-")), |dashes| *dashes >= 5), diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index 8114660..a375a9c 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -22,7 +22,7 @@ pub fn keyword<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, Keyword<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; // TODO: When key is a member of org-element-parsed-keywords, value can contain the standard set objects, excluding footnote references. let (remaining, rule) = recognize(tuple(( space0, diff --git a/src/parser/latex_environment.rs b/src/parser/latex_environment.rs index 84195c7..3aff4b1 100644 --- a/src/parser/latex_environment.rs +++ b/src/parser/latex_environment.rs @@ -28,7 +28,7 @@ pub fn latex_environment<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, LatexEnvironment<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_opening, name, _open_close_brace, _ws, _line_ending)) = tuple(( tag_no_case(r#"\begin{"#), @@ -101,7 +101,7 @@ fn _latex_environment_end<'r, 's, 'x>( input: OrgSource<'s>, current_name_lower: &'x str, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _close_brace, _ws, _line_ending)) = tuple(( tag_no_case(r#"\end{"#), diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 8725def..64bb352 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -254,7 +254,7 @@ fn _lesser_block_end<'r, 's, 'x>( input: OrgSource<'s>, current_name_lower: &'x str, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _ws)) = tuple(( tag_no_case("#+end_"), @@ -280,7 +280,7 @@ fn _lesser_block_begin<'r, 's, 'x>( input: OrgSource<'s>, current_name_lower: &'x str, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, name)) = tuple(( tag_no_case("#+begin_"), diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 8f91e12..9591706 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -58,9 +58,8 @@ fn paragraph_end<'r, 's>( input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let non_paragraph_element_matcher = parser_with_context!(element(false))(context); - let start_of_line_matcher = parser_with_context!(start_of_line)(&context); alt(( - recognize(tuple((start_of_line_matcher, many1(blank_line)))), + recognize(tuple((start_of_line, many1(blank_line)))), recognize(non_paragraph_element_matcher), eof, ))(input) diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index 810784e..f301b41 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -108,7 +108,7 @@ pub fn plain_list_item<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, PlainListItem<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, leading_whitespace) = space0(input)?; // It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09) let indent_level = leading_whitespace.len(); @@ -186,9 +186,8 @@ fn plain_list_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - let start_of_line_matcher = parser_with_context!(start_of_line)(context); recognize(tuple(( - start_of_line_matcher, + start_of_line, verify(many1(blank_line), |lines: &Vec>| { lines.len() >= 2 }), @@ -200,7 +199,7 @@ fn plain_list_item_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; recognize(tuple(( opt(blank_line), parser_with_context!(line_indented_lte)(context), diff --git a/src/parser/planning.rs b/src/parser/planning.rs index bbaed9c..51069ac 100644 --- a/src/parser/planning.rs +++ b/src/parser/planning.rs @@ -21,7 +21,7 @@ pub fn planning<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, Planning<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _planning_parameters) = separated_list1(space1, planning_parameter)(remaining)?; let (remaining, _trailing_ws) = tuple((space0, alt((line_ending, eof))))(remaining)?; diff --git a/src/parser/property_drawer.rs b/src/parser/property_drawer.rs index 3e78ecf..02be168 100644 --- a/src/parser/property_drawer.rs +++ b/src/parser/property_drawer.rs @@ -44,7 +44,7 @@ pub fn property_drawer<'r, 's>( remaining, (_start_of_line, _leading_whitespace, _open_tag, _trailing_whitespace, _line_ending), ) = tuple(( - parser_with_context!(start_of_line)(context), + start_of_line, space0, tag_no_case(":PROPERTIES:"), space0, @@ -84,7 +84,7 @@ fn property_drawer_end<'r, 's>( input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { recognize(tuple(( - parser_with_context!(start_of_line)(context), + start_of_line, space0, tag_no_case(":end:"), space0, @@ -99,7 +99,7 @@ fn node_property<'r, 's>( ) -> Res, NodeProperty<'s>> { let (remaining, (_start_of_line, _leading_whitespace, _open_colon, _name, _close_colon)) = tuple(( - parser_with_context!(start_of_line)(context), + start_of_line, space0, tag(":"), parser_with_context!(node_property_name)(context), diff --git a/src/parser/table.rs b/src/parser/table.rs index 01029d4..465bf48 100644 --- a/src/parser/table.rs +++ b/src/parser/table.rs @@ -36,7 +36,7 @@ pub fn org_mode_table<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, Table<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; peek(tuple((space0, tag("|"))))(input)?; let parser_context = context @@ -70,7 +70,7 @@ fn table_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; recognize(tuple((space0, not(tag("|")))))(input) } @@ -90,7 +90,7 @@ pub fn org_mode_table_row_rule<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, TableRow<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _) = tuple((space0, tag("|-"), is_not("\r\n"), line_ending))(input)?; let source = get_consumed(input, remaining); Ok(( @@ -107,7 +107,7 @@ pub fn org_mode_table_row_regular<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, ) -> Res, TableRow<'s>> { - start_of_line(context, input)?; + start_of_line(input)?; let (remaining, _) = tuple((space0, tag("|")))(input)?; let (remaining, children) = many1(parser_with_context!(org_mode_table_cell)(context))(remaining)?; diff --git a/src/parser/text_markup.rs b/src/parser/text_markup.rs index 3dd98c7..7cb5bf7 100644 --- a/src/parser/text_markup.rs +++ b/src/parser/text_markup.rs @@ -296,7 +296,7 @@ fn _text_markup_end<'r, 's, 'x>( input: OrgSource<'s>, marker_symbol: &'x str, ) -> Res, OrgSource<'s>> { - not(parser_with_context!(preceded_by_whitespace)(context))(input)?; + not(preceded_by_whitespace)(input)?; let (remaining, _marker) = terminated( tag(marker_symbol), peek(parser_with_context!(post)(context)), diff --git a/src/parser/util.rs b/src/parser/util.rs index 7340abb..12f3f25 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -63,11 +63,8 @@ pub fn blank_line(input: OrgSource<'_>) -> Res, OrgSource<'_>> { } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn element_trailing_whitespace<'r, 's>( - context: Context<'r, 's>, - input: OrgSource<'s>, -) -> Res, OrgSource<'s>> { - start_of_line(context, input)?; +pub fn element_trailing_whitespace<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { + start_of_line(input)?; alt((eof, recognize(many0(blank_line))))(input) } @@ -78,9 +75,7 @@ pub fn maybe_consume_trailing_whitespace_if_not_exiting<'r, 's>( ) -> Res, Option>> { if context.should_consume_trailing_whitespace() && exit_matcher_parser(context, input).is_err() { - Ok(opt(parser_with_context!(element_trailing_whitespace)( - context, - ))(input)?) + Ok(opt(element_trailing_whitespace)(input)?) } else { Ok((input, None)) } @@ -92,9 +87,7 @@ pub fn maybe_consume_trailing_whitespace<'r, 's>( input: OrgSource<'s>, ) -> Res, Option>> { if context.should_consume_trailing_whitespace() { - Ok(opt(parser_with_context!(element_trailing_whitespace)( - context, - ))(input)?) + Ok(opt(element_trailing_whitespace)(input)?) } else { Ok((input, None)) } @@ -102,10 +95,7 @@ pub fn maybe_consume_trailing_whitespace<'r, 's>( /// Check that we are at the start of a line #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn start_of_line<'r, 's>( - context: Context<'r, 's>, - input: OrgSource<'s>, -) -> Res, ()> { +pub fn start_of_line<'s>(input: OrgSource<'s>) -> Res, ()> { if input.is_at_start_of_line() { Ok((input, ())) } else { @@ -117,10 +107,7 @@ pub fn start_of_line<'r, 's>( /// Check that we are at the start of a line #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -pub fn preceded_by_whitespace<'r, 's>( - context: Context<'r, 's>, - input: OrgSource<'s>, -) -> Res, ()> { +pub fn preceded_by_whitespace<'s>(input: OrgSource<'s>) -> Res, ()> { let preceding_character = input.get_preceding_character(); match preceding_character { Some('\n') | Some('\r') | Some(' ') | Some('\t') => {}