Compare commits

...

37 Commits

Author SHA1 Message Date
Tom Alexander
360b2d963d
Merge branch 'compare_properties_the_other_nodes'
Some checks failed
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has failed
rust-foreign-document-test Build rust-foreign-document-test has failed
2023-10-10 02:10:09 -04:00
Tom Alexander
534c5ded3c
Fix table type is required. 2023-10-10 02:07:36 -04:00
Tom Alexander
bdaf90af03
compare_properties timestamp. 2023-10-10 02:05:31 -04:00
Tom Alexander
1a67aac502
compare_properties latex environment. 2023-10-10 01:39:47 -04:00
Tom Alexander
9f166278f4
compare_properties babel call. 2023-10-10 01:36:48 -04:00
Tom Alexander
c2222c9102
compare_properties keyword. 2023-10-10 01:32:36 -04:00
Tom Alexander
fc104680eb
compare_properties horizontal rule. 2023-10-10 01:29:00 -04:00
Tom Alexander
e40e3ff553
compare_properties fixed width area. 2023-10-10 01:27:37 -04:00
Tom Alexander
0b465fe290
compare_properties planning. 2023-10-10 01:24:44 -04:00
Tom Alexander
d5396e311b
compare_properties diary sexp. 2023-10-10 01:22:16 -04:00
Tom Alexander
384242af87
compare_properties clock. 2023-10-10 01:19:14 -04:00
Tom Alexander
ec755bae8b
compare_properties src block. 2023-10-10 01:13:09 -04:00
Tom Alexander
0b1e06f0d5
compare_properties export block. 2023-10-10 01:07:41 -04:00
Tom Alexander
bdfa050ee3
compare_properties example block. 2023-10-10 01:02:45 -04:00
Tom Alexander
ec98e1c3c5
compare_properties comment block. 2023-10-10 00:15:04 -04:00
Tom Alexander
0d0b1b2051
compare_properties verse block. 2023-10-10 00:12:40 -04:00
Tom Alexander
2215c32e57
compare_properties table cell. 2023-10-10 00:11:05 -04:00
Tom Alexander
cf257443b0
compare_properties table row. 2023-10-10 00:07:34 -04:00
Tom Alexander
62815621e4
compare_properties table. 2023-10-10 00:05:34 -04:00
Tom Alexander
7af5359e00
compare_properties node property. 2023-10-09 22:45:32 -04:00
Tom Alexander
d1184fa1d0
compare_properties property drawer. 2023-10-09 22:42:02 -04:00
Tom Alexander
5b146d7c07
compare_properties drawer. 2023-10-09 22:37:53 -04:00
Tom Alexander
926682d513
compare_properties comment. 2023-10-09 22:36:15 -04:00
Tom Alexander
4c89d6c813
compare_properties footnote definition. 2023-10-09 22:32:25 -04:00
Tom Alexander
62926bb91d
compare_properties dynamic block. 2023-10-09 22:28:32 -04:00
Tom Alexander
9ab649ebd4
compare_properties special block. 2023-10-09 22:20:20 -04:00
Tom Alexander
ca1b633a9f
compare_properties quote block. 2023-10-09 21:57:11 -04:00
Tom Alexander
f543caee00
compare_properties plain list item. 2023-10-09 21:55:29 -04:00
Tom Alexander
409a92333e
compare_properties plain list. 2023-10-09 21:55:28 -04:00
Tom Alexander
7a38d1ead3
compare_properties paragraph. 2023-10-09 21:55:28 -04:00
Tom Alexander
33c53a14ab
Switch back to the old compare heading function until we support additional properties. 2023-10-09 21:32:29 -04:00
Tom Alexander
65615c64d2
Implement a new compare_properties implementation of the heading comparison. 2023-10-09 21:27:18 -04:00
Tom Alexander
166e59b922
Implement a compare_property_numeric. 2023-10-09 21:04:41 -04:00
Tom Alexander
8e357ed3b6
compare_properties section. 2023-10-09 20:54:31 -04:00
Tom Alexander
45074e3be3
Add a test. 2023-10-09 20:50:19 -04:00
Tom Alexander
ee1d8ca321
Text markup uses confine context. 2023-10-09 20:36:38 -04:00
Tom Alexander
25531cc443
Accept eof as a disallowed character for plain links. 2023-10-09 20:24:43 -04:00
10 changed files with 1619 additions and 1413 deletions

View File

@ -0,0 +1,3 @@
- [ ] Foo
- [-] Bar
- [X] Baz

View File

@ -0,0 +1 @@
/*foo*/

View File

@ -1,4 +1,6 @@
use std::collections::BTreeSet;
use std::fmt::Debug;
use std::str::FromStr;
use super::diff::artificial_diff_scope;
use super::diff::compare_ast_node;
@ -7,9 +9,14 @@ use super::diff::DiffStatus;
use super::sexp::unquote;
use super::sexp::Token;
use super::util::get_property;
use super::util::get_property_numeric;
use super::util::get_property_quoted_string;
use super::util::get_property_unquoted_atom;
use crate::types::AstNode;
use crate::types::CharOffsetInLine;
use crate::types::LineNumber;
use crate::types::RetainLabels;
use crate::types::SwitchNumberLines;
#[derive(Debug)]
pub(crate) enum EmacsField<'s> {
@ -117,6 +124,38 @@ pub(crate) fn compare_property_unquoted_atom<'b, 's, 'x, R, RG: Fn(R) -> Option<
}
}
pub(crate) fn compare_property_numeric<
'b,
's,
'x,
R,
RV: FromStr + PartialEq + Debug,
RG: Fn(R) -> Option<RV>,
>(
_source: &'s str,
emacs: &'b Token<'s>,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error + 's>>
where
<RV as FromStr>::Err: std::error::Error,
<RV as FromStr>::Err: 's,
{
let value = get_property_numeric::<RV>(emacs, emacs_field)?;
let rust_value = rust_value_getter(rust_node);
if rust_value != value {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, value, rust_value
));
Ok(ComparePropertiesResult::SelfChange(this_status, message))
} else {
Ok(ComparePropertiesResult::NoChange)
}
}
pub(crate) fn compare_property_list_of_quoted_string<
'b,
's,
@ -174,6 +213,59 @@ pub(crate) fn compare_property_list_of_quoted_string<
Ok(ComparePropertiesResult::NoChange)
}
pub(crate) fn compare_property_set_of_quoted_string<
'a,
'b,
's,
'x,
R,
RV: AsRef<str> + std::fmt::Debug + Ord + 'a + ?Sized,
RI: Iterator<Item = &'a RV>,
RG: Fn(R) -> Option<RI>,
>(
_source: &'s str,
emacs: &'b Token<'s>,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
let value = get_property(emacs, emacs_field)?
.map(Token::as_list)
.map_or(Ok(None), |r| r.map(Some))?;
let empty = Vec::new();
let value = value.unwrap_or(&empty);
let rust_value = rust_value_getter(rust_node);
let rust_value = if let Some(rust_value) = rust_value {
let slices: BTreeSet<&str> = rust_value.map(|rv| rv.as_ref()).collect();
slices
} else {
BTreeSet::new()
};
let value: Vec<&str> = value
.iter()
.map(|e| e.as_atom())
.collect::<Result<Vec<_>, _>>()?;
let value: Vec<String> = value
.into_iter()
.map(unquote)
.collect::<Result<Vec<_>, _>>()?;
let value: BTreeSet<&str> = value.iter().map(|e| e.as_str()).collect();
let mismatched: Vec<_> = value
.symmetric_difference(&rust_value)
.map(|val| *val)
.collect();
if !mismatched.is_empty() {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch. Mismatched values: {}",
emacs_field,
mismatched.join(", ")
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
Ok(ComparePropertiesResult::NoChange)
}
pub(crate) fn compare_property_boolean<'b, 's, 'x, R, RG: Fn(R) -> bool>(
_source: &'s str,
emacs: &'b Token<'s>,
@ -196,6 +288,45 @@ pub(crate) fn compare_property_boolean<'b, 's, 'x, R, RG: Fn(R) -> bool>(
}
}
pub(crate) fn compare_property_single_ast_node<
'b,
's,
'x: 'b + 's,
R,
RV: std::fmt::Debug,
RG: Fn(R) -> Option<RV>,
>(
source: &'s str,
emacs: &'b Token<'s>,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>>
where
AstNode<'b, 's>: From<RV>,
{
let value = get_property(emacs, emacs_field)?;
let rust_value = rust_value_getter(rust_node);
match (value, rust_value) {
(None, None) => {}
(None, rv @ Some(_)) | (Some(_), rv @ None) => {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, value, rv
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
(Some(ev), Some(rv)) => {
let child_status: Vec<DiffEntry<'b, 's>> =
vec![compare_ast_node(source, ev, rv.into())?];
let diff_scope = artificial_diff_scope(emacs_field, child_status)?;
return Ok(ComparePropertiesResult::DiffEntry(diff_scope));
}
}
Ok(ComparePropertiesResult::NoChange)
}
pub(crate) fn compare_property_list_of_ast_nodes<
'b,
's,
@ -249,3 +380,115 @@ where
}
Ok(ComparePropertiesResult::NoChange)
}
pub(crate) fn compare_property_number_lines<
'b,
's,
'x,
'y,
R,
RG: Fn(R) -> Option<&'y SwitchNumberLines>,
>(
_source: &'s str,
emacs: &'b Token<'s>,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
let number_lines = get_property(emacs, emacs_field)?;
let rust_value = rust_value_getter(rust_node);
match (number_lines, &rust_value) {
(None, None) => {}
(Some(number_lines), Some(rust_number_lines)) => {
let token_list = number_lines.as_list()?;
let number_type = token_list
.get(0)
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.ok_or(":number-lines should have a type.")?;
let number_value = token_list
.get(2)
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.map(|val| val.parse::<LineNumber>())
.map_or(Ok(None), |r| r.map(Some))?
.ok_or(":number-lines should have a value.")?;
match (number_type, number_value, rust_number_lines) {
("new", emacs_val, SwitchNumberLines::New(rust_val)) if emacs_val == *rust_val => {}
("continued", emacs_val, SwitchNumberLines::Continued(rust_val))
if emacs_val == *rust_val => {}
_ => {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, number_lines, rust_value
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
}
}
_ => {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, number_lines, rust_value
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
};
Ok(ComparePropertiesResult::NoChange)
}
pub(crate) fn compare_property_retain_labels<'b, 's, 'x, 'y, R, RG: Fn(R) -> &'y RetainLabels>(
_source: &'s str,
emacs: &'b Token<'s>,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error + 's>> {
let rust_value = rust_value_getter(rust_node);
let retain_labels = get_property_unquoted_atom(emacs, ":retain-labels")?;
if let Some(retain_labels) = retain_labels {
if retain_labels == "t" {
match rust_value {
RetainLabels::Yes => {}
_ => {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, retain_labels, rust_value
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
}
} else {
let retain_labels: CharOffsetInLine = get_property_numeric(emacs, ":retain-labels")?.expect("Cannot be None or else the earlier get_property_unquoted_atom would have been None.");
match (retain_labels, rust_value) {
(e, RetainLabels::Keep(r)) if e == *r => {}
_ => {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, retain_labels, rust_value
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
}
}
} else {
match rust_value {
RetainLabels::No => {}
_ => {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, retain_labels, rust_value
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
}
}
Ok(ComparePropertiesResult::NoChange)
}

File diff suppressed because it is too large Load Diff

View File

@ -22,9 +22,6 @@ pub(crate) enum ContextElement<'r, 's> {
/// Stores the name of the current element to prevent directly nesting elements of the same type.
Context(&'r str),
/// Stores the name of the current object to prevent directly nesting elements of the same type.
ContextObject(&'r str),
/// Indicates if elements should consume the whitespace after them.
ConsumeTrailingWhitespace(bool),

View File

@ -16,6 +16,7 @@ use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use nom::InputTake;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
@ -160,7 +161,20 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
) -> Res<OrgSource<'s>, ExampleBlock<'s>> {
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
let (remaining, _) = lesser_block_begin("example")(context, remaining)?;
let (remaining, parameters) = opt(tuple((space1, example_switches)))(remaining)?;
let (remaining, parameters) = opt(alt((
map(tuple((space1, example_switches)), |(_, switches)| switches),
map(space1, |ws: OrgSource<'_>| {
let source = ws.take(0);
ExampleSrcSwitches {
source: source.into(),
number_lines: None,
retain_labels: RetainLabels::Yes,
preserve_indent: None,
use_labels: true,
label_format: None,
}
}),
)))(remaining)?;
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
let lesser_block_end_specialized = lesser_block_end("example");
let contexts = [
@ -174,7 +188,6 @@ pub(crate) fn example_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 parameters = parameters.map(|(_, parameters)| parameters);
let (remaining, contents) = content(&parser_context, remaining)?;
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
@ -185,11 +198,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
let (switches, number_lines, preserve_indent, retain_labels, use_labels, label_format) = {
if let Some(parameters) = parameters {
(
if parameters.source.len() == 0 {
None
} else {
Some(parameters.source)
},
Some(parameters.source),
parameters.number_lines,
parameters.preserve_indent,
parameters.retain_labels,

View File

@ -324,7 +324,7 @@ fn impl_path_plain_end<'b, 'g, 'r, 's>(
!" \t\r\n[]<>()/".contains(*c) && c.is_ascii_punctuation()
}))(input)?;
let disallowed_character = recognize(one_of(" \t\r\n[]<>"))(remaining);
let disallowed_character = alt((recognize(one_of(" \t\r\n[]<>")), eof))(remaining);
if disallowed_character.is_ok() {
return disallowed_character;
}

View File

@ -176,12 +176,13 @@ fn org_mode_table_cell<'b, 'g, 'r, 's>(
let table_cell_set_object_matcher =
parser_with_context!(table_cell_set_object)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, _) = space0(input)?;
let (remaining, (children, _exit_contents)) = verify(
many_till(table_cell_set_object_matcher, exit_matcher),
|(children, exit_contents)| {
!children.is_empty() || Into::<&str>::into(exit_contents).ends_with("|")
},
)(input)?;
)(remaining)?;
let (remaining, _tail) = org_mode_table_cell_end(&parser_context, remaining)?;

View File

@ -4,11 +4,14 @@ use nom::character::complete::anychar;
use nom::character::complete::multispace1;
use nom::character::complete::one_of;
use nom::character::complete::space0;
use nom::combinator::all_consuming;
use nom::combinator::map;
use nom::combinator::map_parser;
use nom::combinator::not;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::terminated;
#[cfg(feature = "tracing")]
@ -17,15 +20,18 @@ use tracing::span;
use super::object_parser::standard_set_object;
use super::org_source::OrgSource;
use super::radio_link::RematchObject;
use super::util::in_object_section;
use super::util::confine_context;
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
use super::util::org_line_ending;
use super::util::start_of_line;
use super::util::text_until_exit;
use crate::context::parser_with_context;
use crate::context::Context;
use crate::context::ContextElement;
use crate::context::ContextMatcher;
use crate::context::ExitClass;
use crate::context::ExitMatcherNode;
use crate::context::List;
use crate::context::RefContext;
use crate::error::CustomError;
use crate::error::MyError;
@ -196,33 +202,30 @@ fn _text_markup_object<'b, 'g, 'r, 's, 'c>(
input: OrgSource<'s>,
marker_symbol: &'c str,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
if in_object_section(context, marker_symbol) {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same type".into(),
))));
}
let (remaining, _) = pre(context, input)?;
let (remaining, open) = tag(marker_symbol)(remaining)?;
let (remaining, _peek_not_whitespace) =
peek(verify(anychar, |c| !c.is_whitespace() && *c != '\u{200B}'))(remaining)?;
let text_markup_end_specialized = text_markup_end(open.into(), remaining.get_byte_offset());
let contexts = [
ContextElement::ContextObject(marker_symbol),
ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &text_markup_end_specialized,
}),
];
let contexts = [ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &text_markup_end_specialized,
})];
let parser_context = context.with_additional_node(&contexts[0]);
let parser_context = parser_context.with_additional_node(&contexts[1]);
let (remaining, (children, _exit_contents)) = verify(
many_till(
parser_with_context!(standard_set_object)(&parser_context),
parser_with_context!(exit_matcher_parser)(&parser_context),
let initial_context = ContextElement::document_context();
let initial_context = Context::new(context.get_global_settings(), List::new(&initial_context));
let (remaining, children) = map_parser(
verify(
parser_with_context!(text_until_exit)(&parser_context),
|text| text.len() > 0,
),
|(children, _exit_contents)| !children.is_empty(),
confine_context(|i| {
all_consuming(many1(parser_with_context!(standard_set_object)(
&initial_context,
)))(i)
}),
)(remaining)?;
{
@ -262,25 +265,16 @@ fn _text_markup_string<'b, 'g, 'r, 's, 'c>(
input: OrgSource<'s>,
marker_symbol: &'c str,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
if in_object_section(context, marker_symbol) {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same type".into(),
))));
}
let (remaining, _) = pre(context, input)?;
let (remaining, open) = tag(marker_symbol)(remaining)?;
let (remaining, _peek_not_whitespace) =
peek(verify(anychar, |c| !c.is_whitespace() && *c != '\u{200B}'))(remaining)?;
let text_markup_end_specialized = text_markup_end(open.into(), remaining.get_byte_offset());
let contexts = [
ContextElement::ContextObject(marker_symbol),
ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &text_markup_end_specialized,
}),
];
let contexts = [ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &text_markup_end_specialized,
})];
let parser_context = context.with_additional_node(&contexts[0]);
let parser_context = parser_context.with_additional_node(&contexts[1]);
let (remaining, contents) = recognize(verify(
many_till(

View File

@ -57,20 +57,6 @@ pub(crate) fn immediate_in_section<'b, 'g, 'r, 's, 'x>(
false
}
/// Check if we are below a section of the given section type regardless of depth
pub(crate) fn in_object_section<'b, 'g, 'r, 's, 'x>(
context: RefContext<'b, 'g, 'r, 's>,
section_name: &'x str,
) -> bool {
for thing in context.iter() {
match thing {
ContextElement::ContextObject(name) if *name == section_name => return true,
_ => {}
}
}
false
}
/// 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(crate) fn get_consumed<'s>(input: OrgSource<'s>, remaining: OrgSource<'s>) -> OrgSource<'s> {
input.get_until(remaining)