Fix handling of spaces between language, switches, and parameters.

This commit is contained in:
Tom Alexander 2023-10-04 18:57:51 -04:00
parent 8e70773b15
commit c877116540
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 31 additions and 48 deletions

View File

@ -1,6 +1,7 @@
use std::path::Path;
use crate::compare::diff::compare_document;
use crate::compare::diff::DiffResult;
use crate::compare::parse::emacs_parse_anonymous_org_document;
use crate::compare::parse::emacs_parse_file_org_document;
use crate::compare::parse::get_emacs_version;
@ -43,6 +44,12 @@ pub fn run_anonymous_compare_with_settings<P: AsRef<str>>(
if diff_result.is_bad() {
Err("Diff results do not match.")?;
} else {
println!(
"{color}Entire document passes.{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
);
}
Ok(())
@ -83,6 +90,12 @@ pub fn run_compare_on_file_with_settings<P: AsRef<Path>>(
if diff_result.is_bad() {
Err("Diff results do not match.")?;
} else {
println!(
"{color}Entire document passes.{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
);
}
Ok(())

View File

@ -235,7 +235,7 @@ impl<'b, 's> DiffResult<'b, 's> {
.any(|child| child.is_immediately_bad() || child.has_bad_children())
}
fn foreground_color(red: u8, green: u8, blue: u8) -> String {
pub(crate) fn foreground_color(red: u8, green: u8, blue: u8) -> String {
if DiffResult::should_use_color() {
format!(
"\x1b[38;2;{red};{green};{blue}m",
@ -249,7 +249,7 @@ impl<'b, 's> DiffResult<'b, 's> {
}
#[allow(dead_code)]
fn background_color(red: u8, green: u8, blue: u8) -> String {
pub(crate) fn background_color(red: u8, green: u8, blue: u8) -> String {
if DiffResult::should_use_color() {
format!(
"\x1b[48;2;{red};{green};{blue}m",
@ -262,7 +262,7 @@ impl<'b, 's> DiffResult<'b, 's> {
}
}
fn reset_color() -> &'static str {
pub(crate) fn reset_color() -> &'static str {
if DiffResult::should_use_color() {
"\x1b[0m"
} else {

View File

@ -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,