From f29720e5b97bf419702adcf13139577edae9b30c Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 29 Aug 2023 11:18:15 -0400 Subject: [PATCH] Switch to using a type for bracket depth. This is to make changing the type easier in the future. --- src/parser/citation.rs | 9 +++++---- src/parser/citation_reference.rs | 9 +++++---- src/parser/footnote_reference.rs | 5 +++-- src/parser/inline_babel_call.rs | 9 +++++---- src/parser/inline_source_block.rs | 9 +++++---- src/parser/org_source.rs | 14 ++++++++------ src/parser/subscript_and_superscript.rs | 5 +++-- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/parser/citation.rs b/src/parser/citation.rs index 0abb625..13acca0 100644 --- a/src/parser/citation.rs +++ b/src/parser/citation.rs @@ -12,6 +12,7 @@ use nom::multi::separated_list1; use nom::sequence::tuple; use super::citation_reference::must_balance_bracket; +use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; @@ -102,7 +103,7 @@ fn global_prefix<'r, 's>( } fn global_prefix_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _global_prefix_end(context, input, starting_bracket_depth) @@ -113,7 +114,7 @@ fn global_prefix_end( fn _global_prefix_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth < 0 { @@ -154,7 +155,7 @@ fn global_suffix<'r, 's>( } fn global_suffix_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _global_suffix_end(context, input, starting_bracket_depth) @@ -165,7 +166,7 @@ fn global_suffix_end( fn _global_suffix_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth < 0 { diff --git a/src/parser/citation_reference.rs b/src/parser/citation_reference.rs index ff560cf..0024fc3 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::BracketDepth; use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; @@ -109,7 +110,7 @@ fn key_suffix<'r, 's>( } fn key_prefix_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _key_prefix_end(context, input, starting_bracket_depth) @@ -120,7 +121,7 @@ fn key_prefix_end( fn _key_prefix_end<'r, 's>( context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth < 0 { @@ -140,7 +141,7 @@ fn _key_prefix_end<'r, 's>( } fn key_suffix_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _key_suffix_end(context, input, starting_bracket_depth) @@ -151,7 +152,7 @@ fn key_suffix_end( fn _key_suffix_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth < 0 { diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index a26610c..2de9a12 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::BracketDepth; use super::org_source::OrgSource; use super::parser_context::ContextElement; use super::Context; @@ -121,7 +122,7 @@ fn footnote_reference_only<'r, 's>( } fn footnote_definition_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _footnote_definition_end(context, input, starting_bracket_depth) @@ -132,7 +133,7 @@ fn footnote_definition_end( fn _footnote_definition_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth > 0 { diff --git a/src/parser/inline_babel_call.rs b/src/parser/inline_babel_call.rs index c4f2200..1f17b96 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::BracketDepth; use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; @@ -91,7 +92,7 @@ fn header<'r, 's>( } fn header_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _header_end(context, input, starting_bracket_depth) @@ -102,7 +103,7 @@ fn header_end( fn _header_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth > 0 { @@ -141,7 +142,7 @@ fn argument<'r, 's>( } fn argument_end( - starting_parenthesis_depth: i16, + starting_parenthesis_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _argument_end(context, input, starting_parenthesis_depth) @@ -152,7 +153,7 @@ fn argument_end( fn _argument_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_parenthesis_depth: i16, + starting_parenthesis_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_parenthesis_depth() - starting_parenthesis_depth; if current_depth > 0 { diff --git a/src/parser/inline_source_block.rs b/src/parser/inline_source_block.rs index e07d67d..bde53ee 100644 --- a/src/parser/inline_source_block.rs +++ b/src/parser/inline_source_block.rs @@ -12,6 +12,7 @@ use nom::multi::many_till; #[cfg(feature = "tracing")] use tracing::span; +use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::Context; use crate::error::CustomError; @@ -92,7 +93,7 @@ fn header<'r, 's>( } fn header_end( - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _header_end(context, input, starting_bracket_depth) @@ -103,7 +104,7 @@ fn header_end( fn _header_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_bracket_depth: i16, + starting_bracket_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_bracket_depth() - starting_bracket_depth; if current_depth > 0 { @@ -152,7 +153,7 @@ fn body<'r, 's>( } fn body_end( - starting_brace_depth: i16, + starting_brace_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| _body_end(context, input, starting_brace_depth) } @@ -161,7 +162,7 @@ fn body_end( fn _body_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_brace_depth: i16, + starting_brace_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_brace_depth() - starting_brace_depth; if current_depth > 0 { diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 877c556..231cb10 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -11,15 +11,17 @@ use nom::Slice; use crate::error::CustomError; use crate::error::MyError; +pub type BracketDepth = i16; + #[derive(Copy, Clone)] pub struct OrgSource<'s> { full_source: &'s str, start: usize, end: usize, // exclusive start_of_line: usize, - bracket_depth: i16, // [] - brace_depth: i16, // {} - parenthesis_depth: i16, // () + bracket_depth: BracketDepth, // [] + brace_depth: BracketDepth, // {} + parenthesis_depth: BracketDepth, // () preceding_character: Option, } @@ -71,15 +73,15 @@ impl<'s> OrgSource<'s> { self.slice(..(other.start - self.start)) } - pub fn get_bracket_depth(&self) -> i16 { + pub fn get_bracket_depth(&self) -> BracketDepth { self.bracket_depth } - pub fn get_brace_depth(&self) -> i16 { + pub fn get_brace_depth(&self) -> BracketDepth { self.brace_depth } - pub fn get_parenthesis_depth(&self) -> i16 { + pub fn get_parenthesis_depth(&self) -> BracketDepth { self.parenthesis_depth } } diff --git a/src/parser/subscript_and_superscript.rs b/src/parser/subscript_and_superscript.rs index 93361ab..52d518a 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::BracketDepth; use super::org_source::OrgSource; use super::Context; use super::Object; @@ -170,7 +171,7 @@ fn script_with_braces<'r, 's>( } fn script_with_braces_end( - starting_brace_depth: i16, + starting_brace_depth: BracketDepth, ) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { move |context: Context, input: OrgSource<'_>| { _script_with_braces_end(context, input, starting_brace_depth) @@ -181,7 +182,7 @@ fn script_with_braces_end( fn _script_with_braces_end<'r, 's>( _context: Context<'r, 's>, input: OrgSource<'s>, - starting_brace_depth: i16, + starting_brace_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_brace_depth() - starting_brace_depth; if current_depth > 0 {