diff --git a/org_mode_samples/object/subscript_and_superscript/with_parenthesis.org b/org_mode_samples/object/subscript_and_superscript/with_parenthesis.org index ecf84c2..8551490 100644 --- a/org_mode_samples/object/subscript_and_superscript/with_parenthesis.org +++ b/org_mode_samples/object/subscript_and_superscript/with_parenthesis.org @@ -7,3 +7,7 @@ foo_(b{ar) foo_{b(ar} foo_(b(a)r) + +foo_b(a)r + +foo_(b+ar) diff --git a/src/parser/plain_link.rs b/src/parser/plain_link.rs index d92fd0f..b1a909a 100644 --- a/src/parser/plain_link.rs +++ b/src/parser/plain_link.rs @@ -257,13 +257,6 @@ fn _path_plain_parenthesis_end<'s>( starting_parenthesis_depth: BracketDepth, ) -> Res, OrgSource<'s>> { let current_depth = input.get_parenthesis_depth() - starting_parenthesis_depth; - eprintln!( - "current_depth: {}, starting: {}, now: {}, remaining input: {}", - current_depth, - starting_parenthesis_depth, - input.get_parenthesis_depth(), - input - ); if current_depth < 0 { // This shouldn't be possible because if depth is 0 then a closing parenthesis should end the link. unreachable!("Exceeded plain link parenthesis depth.") diff --git a/src/parser/subscript_and_superscript.rs b/src/parser/subscript_and_superscript.rs index f0eb6ba..c1b2829 100644 --- a/src/parser/subscript_and_superscript.rs +++ b/src/parser/subscript_and_superscript.rs @@ -23,6 +23,7 @@ use crate::context::ContextElement; use crate::context::ContextMatcher; use crate::context::ExitClass; use crate::context::ExitMatcherNode; +use crate::context::Matcher; use crate::context::RefContext; use crate::error::CustomError; use crate::error::MyError; @@ -112,6 +113,10 @@ fn script_body<'b, 'g, 'r, 's>( map(parser_with_context!(script_with_braces)(context), |body| { ScriptBody::WithBraces(body.into()) }), + map( + parser_with_context!(script_with_parenthesis)(context), + |body| ScriptBody::Braceless(body.into()), + ), ))(input) } @@ -199,3 +204,49 @@ fn _script_with_braces_end<'b, 'g, 'r, 's>( } tag("}")(input) } + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +fn script_with_parenthesis<'b, 'g, 'r, 's>( + context: RefContext<'b, 'g, 'r, 's>, + input: OrgSource<'s>, +) -> Res, OrgSource<'s>> { + let (remaining, _) = tag("(")(input)?; + let exit_with_depth = script_with_parenthesis_end(remaining.get_parenthesis_depth()); + + let (remaining, _) = many_till( + anychar, + alt(( + peek(exit_with_depth), + parser_with_context!(exit_matcher_parser)(context), + )), + )(remaining)?; + + let (remaining, _) = tag(")")(remaining)?; + let source = get_consumed(input, remaining); + Ok((remaining, source)) +} + +fn script_with_parenthesis_end(starting_parenthesis_depth: BracketDepth) -> impl Matcher { + move |input: OrgSource<'_>| _script_with_parenthesis_end(input, starting_parenthesis_depth) +} + +#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] +fn _script_with_parenthesis_end<'s>( + input: OrgSource<'s>, + starting_parenthesis_depth: BracketDepth, +) -> Res, OrgSource<'s>> { + let current_depth = input.get_parenthesis_depth() - starting_parenthesis_depth; + if current_depth < 0 { + // This shouldn't be possible because if depth is 0 then a closing bracket should end the citation. + unreachable!("Exceeded citation key suffix bracket depth.") + } + if current_depth == 0 { + let close_parenthesis = tag::<&str, OrgSource<'_>, CustomError>>(")")(input); + if close_parenthesis.is_ok() { + return close_parenthesis; + } + } + Err(nom::Err::Error(CustomError::MyError(MyError( + "No script parenthesis end.".into(), + )))) +}