Merge branch 'line_break'
All checks were successful
semver Build semver has succeeded
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded

This commit is contained in:
Tom Alexander 2023-07-22 00:44:03 -04:00
commit 537fc00fd3
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
9 changed files with 138 additions and 2 deletions

View File

@ -0,0 +1,6 @@
foo\\
bar
lorem\\\
ipsum

View File

@ -31,6 +31,7 @@ use crate::parser::Italic;
use crate::parser::Keyword;
use crate::parser::LatexEnvironment;
use crate::parser::LatexFragment;
use crate::parser::LineBreak;
use crate::parser::Object;
use crate::parser::OrgMacro;
use crate::parser::Paragraph;
@ -170,6 +171,7 @@ fn compare_object<'s>(
Object::CitationReference(obj) => compare_citation_reference(source, emacs, obj),
Object::InlineBabelCall(obj) => compare_inline_babel_call(source, emacs, obj),
Object::InlineSourceBlock(obj) => compare_inline_source_block(source, emacs, obj),
Object::LineBreak(obj) => compare_line_break(source, emacs, obj),
}
}
@ -1438,3 +1440,26 @@ fn compare_inline_source_block<'s>(
children: Vec::new(),
})
}
fn compare_line_break<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s LineBreak<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "line-break";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}

59
src/parser/line_break.rs Normal file
View File

@ -0,0 +1,59 @@
use nom::bytes::complete::tag;
use nom::character::complete::line_ending;
use nom::character::complete::one_of;
use nom::combinator::recognize;
use nom::multi::many0;
use super::Context;
use crate::error::CustomError;
use crate::error::MyError;
use crate::error::Res;
use crate::parser::util::get_consumed;
use crate::parser::util::get_current_line_before_position;
use crate::parser::util::get_one_before;
use crate::parser::LineBreak;
#[tracing::instrument(ret, level = "debug")]
pub fn line_break<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, LineBreak<'s>> {
let (remaining, _) = pre(context, input)?;
let (remaining, _) = tag(r#"\\"#)(remaining)?;
let (remaining, _) = recognize(many0(one_of(" \t")))(remaining)?;
let (remaining, _) = line_ending(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, LineBreak { source }))
}
#[tracing::instrument(ret, level = "debug")]
fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> {
let document_root = context.get_document_root().unwrap();
let preceding_character = get_one_before(document_root, input)
.map(|slice| slice.chars().next())
.flatten();
match preceding_character {
// If None, we are at the start of the file
None | Some('\\') => {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Not a valid pre character for line break.",
))));
}
_ => {}
};
let current_line = get_current_line_before_position(document_root, input);
match current_line {
Some(line) => {
let is_non_empty_line = line.chars().any(|c| !c.is_whitespace());
if !is_non_empty_line {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"Not a valid pre line for line break.",
))));
}
}
None => {
return Err(nom::Err::Error(CustomError::MyError(MyError(
"No preceding line for line break.",
))));
}
}
Ok((input, ()))
}

View File

@ -25,6 +25,7 @@ mod latex_environment;
mod latex_fragment;
mod lesser_block;
mod lesser_element;
mod line_break;
mod list;
mod object;
mod object_parser;
@ -87,6 +88,7 @@ pub use object::InlineBabelCall;
pub use object::InlineSourceBlock;
pub use object::Italic;
pub use object::LatexFragment;
pub use object::LineBreak;
pub use object::Object;
pub use object::OrgMacro;
pub use object::PlainLink;

View File

@ -23,6 +23,7 @@ pub enum Object<'s> {
CitationReference(CitationReference<'s>),
InlineBabelCall(InlineBabelCall<'s>),
InlineSourceBlock(InlineSourceBlock<'s>),
LineBreak(LineBreak<'s>),
}
#[derive(Debug, PartialEq)]
@ -149,6 +150,11 @@ pub struct InlineSourceBlock<'s> {
pub source: &'s str,
}
#[derive(Debug, PartialEq)]
pub struct LineBreak<'s> {
pub source: &'s str,
}
impl<'s> Source<'s> for Object<'s> {
fn get_source(&'s self) -> &'s str {
match self {
@ -173,6 +179,7 @@ impl<'s> Source<'s> for Object<'s> {
Object::CitationReference(obj) => obj.source,
Object::InlineBabelCall(obj) => obj.source,
Object::InlineSourceBlock(obj) => obj.source,
Object::LineBreak(obj) => obj.source,
}
}
}
@ -296,3 +303,9 @@ impl<'s> Source<'s> for InlineSourceBlock<'s> {
self.source
}
}
impl<'s> Source<'s> for LineBreak<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}

View File

@ -15,6 +15,7 @@ use crate::parser::footnote_reference::footnote_reference;
use crate::parser::inline_babel_call::inline_babel_call;
use crate::parser::inline_source_block::inline_source_block;
use crate::parser::latex_fragment::latex_fragment;
use crate::parser::line_break::line_break;
use crate::parser::object::Object;
use crate::parser::org_macro::org_macro;
use crate::parser::plain_link::plain_link;
@ -31,6 +32,7 @@ pub fn standard_set_object<'r, 's>(
not(|i| context.check_exit_matcher(i))(input)?;
alt((
map(parser_with_context!(line_break)(context), Object::LineBreak),
map(
parser_with_context!(inline_source_block)(context),
Object::InlineSourceBlock,
@ -96,6 +98,7 @@ pub fn any_object_except_plain_text<'r, 's>(
) -> Res<&'s str, Object<'s>> {
// Used for exit matchers so this does not check exit matcher condition.
alt((
map(parser_with_context!(line_break)(context), Object::LineBreak),
map(
parser_with_context!(inline_source_block)(context),
Object::InlineSourceBlock,

View File

@ -62,6 +62,7 @@ impl<'r, 's> Token<'r, 's> {
Object::CitationReference(_) => Box::new(std::iter::empty()), // TODO: Iterate over children
Object::InlineBabelCall(_) => Box::new(std::iter::empty()),
Object::InlineSourceBlock(_) => Box::new(std::iter::empty()),
Object::LineBreak(_) => Box::new(std::iter::empty()),
},
Token::Element(elem) => match elem {
Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)),

View File

@ -59,6 +59,32 @@ pub fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Optio
Some(&document[previous_character_offset..offset])
}
/// Get the line current_position is on up until current_position
pub fn get_current_line_before_position<'s>(
document: &'s str,
current_position: &'s str,
) -> Option<&'s str> {
assert!(is_slice_of(document, current_position));
if document.as_ptr() as usize == current_position.as_ptr() as usize {
return None;
}
let offset = current_position.as_ptr() as usize - document.as_ptr() as usize;
let mut previous_character_offset = offset;
loop {
let new_offset = document.floor_char_boundary(previous_character_offset - 1);
let new_line = &document[new_offset..offset];
let leading_char = new_line
.chars()
.next()
.expect("Impossible to not have at least 1 character to read.");
if "\r\n".contains(leading_char) || new_offset == 0 {
break;
}
previous_character_offset = new_offset;
}
Some(&document[previous_character_offset..offset])
}
/// Check if the child string slice is a slice of the parent string slice.
fn is_slice_of(parent: &str, child: &str) -> bool {
let parent_start = parent.as_ptr() as usize;

View File

@ -1,2 +1,3 @@
before src_foo{ipsum} after
src_bar[lorem]{ipsum}
foo bar baz
lorem ipsum