Implement the horizontal rule parser.

This commit is contained in:
Tom Alexander 2023-04-21 22:23:59 -04:00
parent 3fb0de97cf
commit bfffde3fdb
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
7 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,3 @@
foo
-----
bar

View File

@ -16,6 +16,7 @@ use crate::parser::FixedWidthArea;
use crate::parser::FootnoteDefinition; use crate::parser::FootnoteDefinition;
use crate::parser::GreaterBlock; use crate::parser::GreaterBlock;
use crate::parser::Heading; use crate::parser::Heading;
use crate::parser::HorizontalRule;
use crate::parser::Paragraph; use crate::parser::Paragraph;
use crate::parser::PlainList; use crate::parser::PlainList;
use crate::parser::PlainListItem; use crate::parser::PlainListItem;
@ -221,6 +222,7 @@ fn compare_element<'s>(
Element::DiarySexp(obj) => compare_diary_sexp(source, emacs, obj), Element::DiarySexp(obj) => compare_diary_sexp(source, emacs, obj),
Element::Planning(obj) => compare_planning(source, emacs, obj), Element::Planning(obj) => compare_planning(source, emacs, obj),
Element::FixedWidthArea(obj) => compare_fixed_width_area(source, emacs, obj), Element::FixedWidthArea(obj) => compare_fixed_width_area(source, emacs, obj),
Element::HorizontalRule(obj) => compare_horizontal_rule(source, emacs, obj),
} }
} }
@ -778,3 +780,27 @@ fn compare_fixed_width_area<'s>(
children: child_status, children: child_status,
}) })
} }
fn compare_horizontal_rule<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s HorizontalRule<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let child_status = Vec::new();
let mut this_status = DiffStatus::Good;
let emacs_name = "horizontal-rule";
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: child_status,
})
}

View File

@ -11,6 +11,7 @@ use super::lesser_element::DiarySexp;
use super::lesser_element::ExampleBlock; use super::lesser_element::ExampleBlock;
use super::lesser_element::ExportBlock; use super::lesser_element::ExportBlock;
use super::lesser_element::FixedWidthArea; use super::lesser_element::FixedWidthArea;
use super::lesser_element::HorizontalRule;
use super::lesser_element::Paragraph; use super::lesser_element::Paragraph;
use super::lesser_element::Planning; use super::lesser_element::Planning;
use super::lesser_element::SrcBlock; use super::lesser_element::SrcBlock;
@ -38,6 +39,7 @@ pub enum Element<'s> {
DiarySexp(DiarySexp<'s>), DiarySexp(DiarySexp<'s>),
Planning(Planning<'s>), Planning(Planning<'s>),
FixedWidthArea(FixedWidthArea<'s>), FixedWidthArea(FixedWidthArea<'s>),
HorizontalRule(HorizontalRule<'s>),
} }
impl<'s> Source<'s> for Element<'s> { impl<'s> Source<'s> for Element<'s> {
@ -61,6 +63,7 @@ impl<'s> Source<'s> for Element<'s> {
Element::DiarySexp(obj) => obj.source, Element::DiarySexp(obj) => obj.source,
Element::Planning(obj) => obj.source, Element::Planning(obj) => obj.source,
Element::FixedWidthArea(obj) => obj.source, Element::FixedWidthArea(obj) => obj.source,
Element::HorizontalRule(obj) => obj.source,
} }
} }
} }

View File

@ -7,6 +7,7 @@ use super::element::Element;
use super::fixed_width_area::fixed_width_area; use super::fixed_width_area::fixed_width_area;
use super::footnote_definition::footnote_definition; use super::footnote_definition::footnote_definition;
use super::greater_block::greater_block; use super::greater_block::greater_block;
use super::horizontal_rule::horizontal_rule;
use super::lesser_block::comment_block; use super::lesser_block::comment_block;
use super::lesser_block::example_block; use super::lesser_block::example_block;
use super::lesser_block::export_block; use super::lesser_block::export_block;
@ -51,6 +52,7 @@ pub fn non_paragraph_element<'r, 's>(
let clock_matcher = parser_with_context!(clock)(context); let clock_matcher = parser_with_context!(clock)(context);
let diary_sexp_matcher = parser_with_context!(diary_sexp)(context); let diary_sexp_matcher = parser_with_context!(diary_sexp)(context);
let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context); let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context);
let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context);
alt(( alt((
map(plain_list_matcher, Element::PlainList), map(plain_list_matcher, Element::PlainList),
map(greater_block_matcher, Element::GreaterBlock), map(greater_block_matcher, Element::GreaterBlock),
@ -67,5 +69,6 @@ pub fn non_paragraph_element<'r, 's>(
map(clock_matcher, Element::Clock), map(clock_matcher, Element::Clock),
map(diary_sexp_matcher, Element::DiarySexp), map(diary_sexp_matcher, Element::DiarySexp),
map(fixed_width_area_matcher, Element::FixedWidthArea), map(fixed_width_area_matcher, Element::FixedWidthArea),
map(horizontal_rule_matcher, Element::HorizontalRule),
))(input) ))(input)
} }

View File

@ -0,0 +1,29 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many1_count;
use nom::sequence::tuple;
use super::Context;
use crate::error::Res;
use crate::parser::util::start_of_line;
use crate::parser::HorizontalRule;
#[tracing::instrument(ret, level = "debug")]
pub fn horizontal_rule<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, HorizontalRule<'s>> {
start_of_line(context, input)?;
let (remaining, rule) = recognize(tuple((
space0,
verify(many1_count(tag("-")), |dashes| *dashes >= 5),
space0,
alt((line_ending, eof)),
)))(input)?;
Ok((remaining, HorizontalRule { source: rule }))
}

View File

@ -79,6 +79,11 @@ pub struct FixedWidthArea<'s> {
pub source: &'s str, pub source: &'s str,
} }
#[derive(Debug)]
pub struct HorizontalRule<'s> {
pub source: &'s str,
}
impl<'s> Paragraph<'s> { impl<'s> Paragraph<'s> {
pub fn of_text(input: &'s str) -> Self { pub fn of_text(input: &'s str) -> Self {
let mut objects = Vec::with_capacity(1); let mut objects = Vec::with_capacity(1);
@ -157,3 +162,9 @@ impl<'s> Source<'s> for FixedWidthArea<'s> {
self.source self.source
} }
} }
impl<'s> Source<'s> for HorizontalRule<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}

View File

@ -11,6 +11,7 @@ mod fixed_width_area;
mod footnote_definition; mod footnote_definition;
mod greater_block; mod greater_block;
mod greater_element; mod greater_element;
mod horizontal_rule;
mod lesser_block; mod lesser_block;
mod lesser_element; mod lesser_element;
mod list; mod list;
@ -49,6 +50,7 @@ pub use lesser_element::DiarySexp;
pub use lesser_element::ExampleBlock; pub use lesser_element::ExampleBlock;
pub use lesser_element::ExportBlock; pub use lesser_element::ExportBlock;
pub use lesser_element::FixedWidthArea; pub use lesser_element::FixedWidthArea;
pub use lesser_element::HorizontalRule;
pub use lesser_element::Paragraph; pub use lesser_element::Paragraph;
pub use lesser_element::Planning; pub use lesser_element::Planning;
pub use lesser_element::SrcBlock; pub use lesser_element::SrcBlock;