sable-markdown/src/parser/util.rs@main
raw
1use std::{collections::HashMap, sync::LazyLock};
2
3use nom::{
4 IResult, Parser,
5 branch::alt,
6 character::complete::{anychar, line_ending, not_line_ending, space0},
7 combinator::{eof, not, recognize},
8 multi::{many0, many1},
9 sequence::{preceded, terminated},
10};
11
12pub(super) static ENTITIES: LazyLock<HashMap<String, &'static entities::Entity>> =
13 LazyLock::new(|| {
14 let mut map = HashMap::new();
15 for entity in &entities::ENTITIES {
16 map.insert(entity.entity.to_string(), entity);
17 }
18 map
19 });
20
21pub(super) fn eof_or_eol(input: &str) -> IResult<&str, &str> {
22 alt((line_ending, eof)).parse(input)
23}
24
25pub(super) fn many_empty_lines0(input: &str) -> IResult<&str, Vec<&str>> {
26 many0(preceded(space0, eof_or_eol)).parse(input)
27}
28
29pub(super) fn not_eof_or_eol1(input: &str) -> IResult<&str, &str> {
30 recognize(many1(preceded(not(eof_or_eol), anychar))).parse(input)
31}
32
33pub(super) fn not_eof_or_eol0(input: &str) -> IResult<&str, &str> {
34 alt((not_line_ending, eof)).parse(input)
35}
36
37pub(super) fn line_terminated<'a, O, P>(
38 inner: P,
39) -> impl Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>
40where
41 P: Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>,
42{
43 terminated(inner, eof_or_eol)
44}
45
46