Fix handling of spaces between language, switches, and parameters.
This commit is contained in:
@@ -17,7 +17,6 @@ use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
use nom::multi::separated_list1;
|
||||
use nom::sequence::tuple;
|
||||
use nom::InputTake;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use crate::context::parser_with_context;
|
||||
@@ -240,8 +239,12 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
|
||||
) -> Res<OrgSource<'s>, 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, switches) = opt(tuple((space1, src_switches)))(remaining)?;
|
||||
let (remaining, parameters) = opt(map(tuple((space0, src_parameters)), |(_, parameters)| {
|
||||
let (remaining, language) = opt(map(tuple((space1, switch_word(true))), |(_, language)| {
|
||||
language
|
||||
}))(remaining)?;
|
||||
let (remaining, switches) =
|
||||
opt(map(tuple((space1, src_switches)), |(_, switches)| switches))(remaining)?;
|
||||
let (remaining, parameters) = opt(map(tuple((space1, src_parameters)), |(_, parameters)| {
|
||||
parameters
|
||||
}))(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
@@ -257,16 +260,11 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let switches = match switches {
|
||||
Some((_ws, switches)) => Some(switches),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let (remaining, contents) = content(&parser_context, remaining)?;
|
||||
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
let (switches, language, number_lines, retain_labels, use_labels, label_format) = {
|
||||
let (switches, number_lines, retain_labels, use_labels, label_format) = {
|
||||
if let Some(switches) = switches {
|
||||
(
|
||||
if switches.source.len() == 0 {
|
||||
@@ -274,21 +272,20 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
|
||||
} else {
|
||||
Some(switches.source)
|
||||
},
|
||||
switches.language,
|
||||
switches.number_lines,
|
||||
switches.retain_labels,
|
||||
switches.use_labels,
|
||||
switches.label_format,
|
||||
)
|
||||
} else {
|
||||
(None, None, None, RetainLabels::Yes, true, None)
|
||||
(None, None, RetainLabels::Yes, true, None)
|
||||
}
|
||||
};
|
||||
Ok((
|
||||
remaining,
|
||||
SrcBlock {
|
||||
source: source.into(),
|
||||
language,
|
||||
language: language.map(Into::<&str>::into),
|
||||
switches,
|
||||
parameters: parameters.map(Into::<&str>::into),
|
||||
number_lines,
|
||||
@@ -362,7 +359,6 @@ fn _lesser_block_begin<'b, 'g, 'r, 's, 'c>(
|
||||
#[derive(Debug)]
|
||||
struct ExampleSrcSwitches<'s> {
|
||||
source: &'s str,
|
||||
language: Option<&'s str>,
|
||||
number_lines: Option<SwitchNumberLines>,
|
||||
retain_labels: RetainLabels,
|
||||
use_labels: bool,
|
||||
@@ -384,27 +380,24 @@ fn src_parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn src_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
|
||||
example_src_switches(true, true)(input)
|
||||
example_src_switches(true)(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
|
||||
let (remaining, switches) = example_src_switches(false, false)(input)?;
|
||||
debug_assert!(switches.language.is_none());
|
||||
let (remaining, switches) = example_src_switches(false)(input)?;
|
||||
Ok((remaining, switches))
|
||||
}
|
||||
|
||||
fn example_src_switches(
|
||||
grab_language: bool,
|
||||
stop_at_parameters: bool,
|
||||
) -> impl for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
|
||||
move |input| _example_src_switches(input, grab_language, stop_at_parameters)
|
||||
move |input| _example_src_switches(input, stop_at_parameters)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _example_src_switches<'s>(
|
||||
input: OrgSource<'s>,
|
||||
grab_language: bool,
|
||||
stop_at_parameters: bool,
|
||||
) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
|
||||
let mut number_lines = None;
|
||||
@@ -412,32 +405,10 @@ fn _example_src_switches<'s>(
|
||||
let mut use_labels = true;
|
||||
let mut label_format = None;
|
||||
let mut saw_r = false;
|
||||
let mut language = None;
|
||||
|
||||
let remaining = if grab_language {
|
||||
let (remain, first_word) = opt(switch_word(stop_at_parameters))(input)?;
|
||||
language = first_word.map(Into::<&str>::into);
|
||||
remain
|
||||
} else {
|
||||
input
|
||||
};
|
||||
|
||||
let (remaining, (source, words)) = if language.is_none() {
|
||||
consumed(separated_list1(space1, switch_word(stop_at_parameters)))(remaining)?
|
||||
} else {
|
||||
let (remain, maybe_words) = opt(map(
|
||||
tuple((
|
||||
space1,
|
||||
consumed(separated_list1(space1, switch_word(stop_at_parameters))),
|
||||
)),
|
||||
|(_, words)| words,
|
||||
))(remaining)?;
|
||||
if let Some((source, words)) = maybe_words {
|
||||
(remain, (source, words))
|
||||
} else {
|
||||
(remain, (remaining.take(0), Vec::new()))
|
||||
}
|
||||
};
|
||||
let (remaining, words) = separated_list1(space1, switch_word(stop_at_parameters))(input)?;
|
||||
let (remaining, _post_spaces) = opt(tuple((space0, peek(line_ending))))(remaining)?;
|
||||
let source = input.get_until(remaining);
|
||||
|
||||
let mut state = SwitchState::Normal;
|
||||
for word in words {
|
||||
@@ -534,7 +505,6 @@ fn _example_src_switches<'s>(
|
||||
remaining,
|
||||
ExampleSrcSwitches {
|
||||
source: Into::<&str>::into(source),
|
||||
language,
|
||||
number_lines,
|
||||
retain_labels,
|
||||
use_labels,
|
||||
|
||||
Reference in New Issue
Block a user