Merge branch 'lesser_block'

This commit is contained in:
Tom Alexander 2023-04-21 18:15:08 -04:00
commit f4aaa43497
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
11 changed files with 552 additions and 1 deletions

View File

@ -0,0 +1,3 @@
#+begin_export
This would be LaTeX code.
#+end_export

View File

@ -0,0 +1,3 @@
#+begin_export latex html
This would be LaTeX code.
#+end_export

View File

@ -0,0 +1,19 @@
#+begin_comment
This is a comment.
#+end_comment
#+begin_example
This is an example.
#+end_example
#+begin_export latex
This would be LaTeX code.
#+end_export
#+begin_src sh
echo "this is a source block."
#+end_src
#+begin_verse
This is a verse block.
#+end_verse

View File

@ -0,0 +1,3 @@
#+begin_src
echo "this is a source block."
#+end_src

View File

@ -2,10 +2,13 @@ use super::sexp::Token;
use super::util::assert_bounds;
use super::util::assert_name;
use crate::parser::Comment;
use crate::parser::CommentBlock;
use crate::parser::Document;
use crate::parser::DocumentElement;
use crate::parser::Drawer;
use crate::parser::Element;
use crate::parser::ExampleBlock;
use crate::parser::ExportBlock;
use crate::parser::FootnoteDefinition;
use crate::parser::GreaterBlock;
use crate::parser::Heading;
@ -14,9 +17,11 @@ use crate::parser::PlainList;
use crate::parser::PlainListItem;
use crate::parser::PropertyDrawer;
use crate::parser::Section;
use crate::parser::SrcBlock;
use crate::parser::Table;
use crate::parser::TableCell;
use crate::parser::TableRow;
use crate::parser::VerseBlock;
use crate::DynamicBlock;
#[derive(Debug)]
@ -202,6 +207,11 @@ fn compare_element<'s>(
Element::Drawer(obj) => compare_drawer(source, emacs, obj),
Element::PropertyDrawer(obj) => compare_property_drawer(source, emacs, obj),
Element::Table(obj) => compare_table(source, emacs, obj),
Element::VerseBlock(obj) => compare_verse_block(source, emacs, obj),
Element::CommentBlock(obj) => compare_comment_block(source, emacs, obj),
Element::ExampleBlock(obj) => compare_example_block(source, emacs, obj),
Element::ExportBlock(obj) => compare_export_block(source, emacs, obj),
Element::SrcBlock(obj) => compare_src_block(source, emacs, obj),
}
}
@ -548,3 +558,122 @@ fn compare_table_cell<'s>(
children: child_status,
})
}
fn compare_verse_block<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s VerseBlock<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let children = emacs.as_list()?;
let mut child_status = Vec::new();
let mut this_status = DiffStatus::Good;
let emacs_name = "verse-block";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) {}
Ok(DiffResult {
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: child_status,
})
}
fn compare_comment_block<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s CommentBlock<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "comment-block";
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(),
})
}
fn compare_example_block<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s ExampleBlock<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "example-block";
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(),
})
}
fn compare_export_block<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s ExportBlock<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "export-block";
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(),
})
}
fn compare_src_block<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s SrcBlock<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "src-block";
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(),
})
}

View File

@ -5,7 +5,12 @@ use super::greater_element::PlainList;
use super::greater_element::PropertyDrawer;
use super::greater_element::Table;
use super::lesser_element::Comment;
use super::lesser_element::CommentBlock;
use super::lesser_element::ExampleBlock;
use super::lesser_element::ExportBlock;
use super::lesser_element::Paragraph;
use super::lesser_element::SrcBlock;
use super::lesser_element::VerseBlock;
use super::source::Source;
use super::Drawer;
@ -20,6 +25,11 @@ pub enum Element<'s> {
Drawer(Drawer<'s>),
PropertyDrawer(PropertyDrawer<'s>),
Table(Table<'s>),
VerseBlock(VerseBlock<'s>),
CommentBlock(CommentBlock<'s>),
ExampleBlock(ExampleBlock<'s>),
ExportBlock(ExportBlock<'s>),
SrcBlock(SrcBlock<'s>),
}
impl<'s> Source<'s> for Element<'s> {
@ -34,6 +44,11 @@ impl<'s> Source<'s> for Element<'s> {
Element::Drawer(obj) => obj.source,
Element::PropertyDrawer(obj) => obj.source,
Element::Table(obj) => obj.source,
Element::VerseBlock(obj) => obj.source,
Element::CommentBlock(obj) => obj.source,
Element::ExampleBlock(obj) => obj.source,
Element::ExportBlock(obj) => obj.source,
Element::SrcBlock(obj) => obj.source,
}
}
}

View File

@ -5,6 +5,11 @@ use super::element::Element;
use super::error::Res;
use super::footnote_definition::footnote_definition;
use super::greater_block::greater_block;
use super::lesser_block::comment_block;
use super::lesser_block::example_block;
use super::lesser_block::export_block;
use super::lesser_block::src_block;
use super::lesser_block::verse_block;
use super::paragraph::paragraph;
use super::plain_list::plain_list;
use super::Context;
@ -35,6 +40,11 @@ pub fn non_paragraph_element<'r, 's>(
let comment_matcher = parser_with_context!(comment)(context);
let drawer_matcher = parser_with_context!(drawer)(context);
let table_matcher = parser_with_context!(org_mode_table)(context);
let verse_block_matcher = parser_with_context!(verse_block)(context);
let comment_block_matcher = parser_with_context!(comment_block)(context);
let example_block_matcher = parser_with_context!(example_block)(context);
let export_block_matcher = parser_with_context!(export_block)(context);
let src_block_matcher = parser_with_context!(src_block)(context);
alt((
map(plain_list_matcher, Element::PlainList),
map(greater_block_matcher, Element::GreaterBlock),
@ -43,5 +53,10 @@ pub fn non_paragraph_element<'r, 's>(
map(comment_matcher, Element::Comment),
map(drawer_matcher, Element::Drawer),
map(table_matcher, Element::Table),
map(verse_block_matcher, Element::VerseBlock),
map(comment_block_matcher, Element::CommentBlock),
map(example_block_matcher, Element::ExampleBlock),
map(export_block_matcher, Element::ExportBlock),
map(src_block_matcher, Element::SrcBlock),
))(input)
}

View File

@ -37,7 +37,6 @@ pub fn greater_block<'r, 's>(
// TODO: Do I need to differentiate between different greater block types.
start_of_line(context, input)?;
let (remaining, _leading_whitespace) = space0(input)?;
// TODO: Not handling indentation before start of block
let (remaining, (_begin, name)) = tuple((
tag_no_case("#+begin_"),
verify(name, |name: &str| match name.to_lowercase().as_str() {

293
src/parser/lesser_block.rs Normal file
View File

@ -0,0 +1,293 @@
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::character::complete::space1;
use nom::combinator::consumed;
use nom::combinator::eof;
use nom::combinator::map;
use nom::combinator::opt;
use nom::combinator::verify;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::error::Res;
use super::Context;
use crate::parser::exiting::ExitClass;
use crate::parser::lesser_element::CommentBlock;
use crate::parser::lesser_element::ExampleBlock;
use crate::parser::lesser_element::ExportBlock;
use crate::parser::lesser_element::SrcBlock;
use crate::parser::lesser_element::VerseBlock;
use crate::parser::object::Object;
use crate::parser::object::PlainText;
use crate::parser::object_parser::standard_set_object;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ExitMatcherNode;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::plain_text::plain_text;
use crate::parser::util::blank_line;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
use crate::parser::util::start_of_line;
#[tracing::instrument(ret, level = "debug")]
pub fn verse_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, VerseBlock<'s>> {
let (remaining, name) = lesser_block_begin("verse")(context, input)?;
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let lesser_block_end_specialized = lesser_block_end("verse");
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context("lesser block"))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &lesser_block_end_specialized,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let object_matcher = parser_with_context!(standard_set_object)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
// Check for a completely empty block
let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) {
Ok((remaining, (whitespace, (_children, _exit_contents)))) => (
remaining,
vec![Object::PlainText(PlainText { source: whitespace })],
),
Err(_) => {
let (remaining, (children, _exit_contents)) =
many_till(object_matcher, exit_matcher)(remaining)?;
(remaining, children)
}
};
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
VerseBlock {
source,
name,
data: parameters,
children,
},
))
}
#[tracing::instrument(ret, level = "debug")]
pub fn comment_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, CommentBlock<'s>> {
let (remaining, name) = lesser_block_begin("comment")(context, input)?;
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let lesser_block_end_specialized = lesser_block_end("comment");
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context("lesser block"))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &lesser_block_end_specialized,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let (remaining, contents) = map(parser_with_context!(plain_text)(&parser_context), |obj| {
obj.source
})(remaining)?;
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
CommentBlock {
source,
name,
data: parameters,
contents,
},
))
}
#[tracing::instrument(ret, level = "debug")]
pub fn example_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, ExampleBlock<'s>> {
let (remaining, name) = lesser_block_begin("example")(context, input)?;
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let lesser_block_end_specialized = lesser_block_end("example");
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context("lesser block"))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &lesser_block_end_specialized,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let (remaining, contents) = map(parser_with_context!(plain_text)(&parser_context), |obj| {
obj.source
})(remaining)?;
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
ExampleBlock {
source,
name,
data: parameters,
contents,
},
))
}
#[tracing::instrument(ret, level = "debug")]
pub fn export_block<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, ExportBlock<'s>> {
let (remaining, name) = lesser_block_begin("export")(context, input)?;
// https://orgmode.org/worg/org-syntax.html#Blocks claims that export blocks must have a single word for data but testing shows no data and multi-word data still parses as an export block.
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let lesser_block_end_specialized = lesser_block_end("export");
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context("lesser block"))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &lesser_block_end_specialized,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let (remaining, contents) = map(parser_with_context!(plain_text)(&parser_context), |obj| {
obj.source
})(remaining)?;
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
ExportBlock {
source,
name,
data: parameters,
contents,
},
))
}
#[tracing::instrument(ret, level = "debug")]
pub fn src_block<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, 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, parameters) = opt(tuple((space1, data)))(remaining)?;
let (remaining, _nl) = line_ending(remaining)?;
let lesser_block_end_specialized = lesser_block_end("src");
let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
.with_additional_node(ContextElement::Context("lesser block"))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta,
exit_matcher: &lesser_block_end_specialized,
}));
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let (remaining, contents) = map(parser_with_context!(plain_text)(&parser_context), |obj| {
obj.source
})(remaining)?;
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
SrcBlock {
source,
name,
data: parameters,
contents,
},
))
}
#[tracing::instrument(ret, level = "debug")]
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not(" \t\r\n")(input)
}
#[tracing::instrument(ret, level = "debug")]
fn data<'s>(input: &'s str) -> Res<&'s str, &'s str> {
is_not("\r\n")(input)
}
fn lesser_block_end(
current_name: &str,
) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> {
let current_name_lower = current_name.to_lowercase();
move |context: Context, input: &str| {
start_of_line(context, input)?;
let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_begin, _name, _ws)) = tuple((
tag_no_case("#+end_"),
tag_no_case(current_name_lower.as_str()),
alt((eof, line_ending)),
))(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, source))
}
}
fn lesser_block_begin(
current_name: &str,
) -> impl for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str> {
let current_name_lower = current_name.to_lowercase();
move |context: Context, input: &str| {
start_of_line(context, input)?;
let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_begin, name)) = tuple((
tag_no_case("#+begin_"),
verify(name, |name: &str| {
name.to_lowercase().as_str() == current_name_lower
}),
))(remaining)?;
Ok((remaining, name))
}
}

View File

@ -19,6 +19,46 @@ pub struct TableCell<'s> {
pub children: Vec<Object<'s>>,
}
#[derive(Debug)]
pub struct VerseBlock<'s> {
pub source: &'s str,
pub name: &'s str,
pub data: Option<&'s str>,
pub children: Vec<Object<'s>>,
}
#[derive(Debug)]
pub struct CommentBlock<'s> {
pub source: &'s str,
pub name: &'s str,
pub data: Option<&'s str>,
pub contents: &'s str,
}
#[derive(Debug)]
pub struct ExampleBlock<'s> {
pub source: &'s str,
pub name: &'s str,
pub data: Option<&'s str>,
pub contents: &'s str,
}
#[derive(Debug)]
pub struct ExportBlock<'s> {
pub source: &'s str,
pub name: &'s str,
pub data: Option<&'s str>,
pub contents: &'s str,
}
#[derive(Debug)]
pub struct SrcBlock<'s> {
pub source: &'s str,
pub name: &'s str,
pub data: Option<&'s str>,
pub contents: &'s str,
}
impl<'s> Paragraph<'s> {
pub fn of_text(input: &'s str) -> Self {
let mut objects = Vec::with_capacity(1);
@ -47,3 +87,29 @@ impl<'s> Source<'s> for Comment<'s> {
self.source
}
}
impl<'s> Source<'s> for VerseBlock<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for CommentBlock<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for ExampleBlock<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for ExportBlock<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}
impl<'s> Source<'s> for SrcBlock<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}

View File

@ -9,6 +9,7 @@ mod exiting;
mod footnote_definition;
mod greater_block;
mod greater_element;
mod lesser_block;
mod lesser_element;
mod list;
mod object;
@ -38,7 +39,12 @@ pub use greater_element::PropertyDrawer;
pub use greater_element::Table;
pub use greater_element::TableRow;
pub use lesser_element::Comment;
pub use lesser_element::CommentBlock;
pub use lesser_element::ExampleBlock;
pub use lesser_element::ExportBlock;
pub use lesser_element::Paragraph;
pub use lesser_element::SrcBlock;
pub use lesser_element::TableCell;
pub use lesser_element::VerseBlock;
pub use source::Source;
type Context<'r, 's> = &'r parser_context::ContextTree<'r, 's>;