organic/src/parser/element_parser.rs

94 lines
4.2 KiB
Rust
Raw Normal View History

use super::clock::clock;
2023-04-21 20:10:56 +00:00
use super::comment::comment;
2023-04-22 00:22:31 +00:00
use super::diary_sexp::diary_sexp;
2023-04-21 20:10:56 +00:00
use super::drawer::drawer;
use super::dynamic_block::dynamic_block;
use super::element::Element;
2023-04-22 02:04:22 +00:00
use super::fixed_width_area::fixed_width_area;
2023-04-21 20:10:56 +00:00
use super::footnote_definition::footnote_definition;
use super::greater_block::greater_block;
2023-04-22 02:23:59 +00:00
use super::horizontal_rule::horizontal_rule;
2023-04-22 02:33:10 +00:00
use super::keyword::keyword;
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;
2023-04-21 20:10:56 +00:00
use super::paragraph::paragraph;
use super::plain_list::plain_list;
2023-04-22 03:36:38 +00:00
use super::util::start_of_line;
2023-04-21 20:10:56 +00:00
use super::Context;
2023-04-21 22:36:01 +00:00
use crate::error::Res;
2023-04-21 20:10:56 +00:00
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::table::org_mode_table;
use nom::branch::alt;
use nom::combinator::map;
2023-04-22 03:36:38 +00:00
use nom::multi::many0;
2023-04-21 20:10:56 +00:00
#[tracing::instrument(ret, level = "debug")]
pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> {
let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context);
2023-04-22 03:36:38 +00:00
let paragraph_matcher = parser_with_context!(paragraph_element)(context);
2023-04-21 20:10:56 +00:00
2023-04-22 03:36:38 +00:00
alt((non_paragraph_matcher, paragraph_matcher))(input)
2023-04-21 20:10:56 +00:00
}
2023-04-22 03:36:38 +00:00
pub fn paragraph_element<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, Element<'s>> {
let paragraph_matcher = parser_with_context!(paragraph)(context);
let keyword_matcher = parser_with_context!(keyword)(context);
let (remaining, affiliated_keywords) = many0(keyword_matcher)(input)?;
map(paragraph_matcher, Element::Paragraph)(remaining)
}
2023-04-21 20:10:56 +00:00
pub fn non_paragraph_element<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, Element<'s>> {
let plain_list_matcher = parser_with_context!(plain_list)(context);
let greater_block_matcher = parser_with_context!(greater_block)(context);
let dynamic_block_matcher = parser_with_context!(dynamic_block)(context);
let footnote_definition_matcher = parser_with_context!(footnote_definition)(context);
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);
let clock_matcher = parser_with_context!(clock)(context);
2023-04-22 00:22:31 +00:00
let diary_sexp_matcher = parser_with_context!(diary_sexp)(context);
2023-04-22 02:04:22 +00:00
let fixed_width_area_matcher = parser_with_context!(fixed_width_area)(context);
2023-04-22 02:23:59 +00:00
let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context);
2023-04-22 02:33:10 +00:00
let keyword_matcher = parser_with_context!(keyword)(context);
2023-04-22 03:36:38 +00:00
let (remaining, mut affiliated_keywords) = many0(keyword_matcher)(input)?;
let (remaining, element) = match alt((
2023-04-21 20:10:56 +00:00
map(plain_list_matcher, Element::PlainList),
map(greater_block_matcher, Element::GreaterBlock),
map(dynamic_block_matcher, Element::DynamicBlock),
map(footnote_definition_matcher, Element::FootnoteDefinition),
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),
map(clock_matcher, Element::Clock),
2023-04-22 00:22:31 +00:00
map(diary_sexp_matcher, Element::DiarySexp),
2023-04-22 02:04:22 +00:00
map(fixed_width_area_matcher, Element::FixedWidthArea),
2023-04-22 02:23:59 +00:00
map(horizontal_rule_matcher, Element::HorizontalRule),
2023-04-22 03:36:38 +00:00
))(remaining)
{
the_ok @ Ok(_) => the_ok,
Err(_) => {
affiliated_keywords.clear();
map(keyword_matcher, Element::Keyword)(input)
}
}?;
Ok((remaining, element))
2023-04-21 20:10:56 +00:00
}