root parser

This commit is contained in:
Ponj 2024-11-09 17:59:49 -05:00
parent bdb42ac846
commit 315238f14d
Signed by: p6nj
GPG key ID: 6FED68D87C479A59

View file

@ -1,13 +1,16 @@
use std::fmt::{Debug, Display}; use std::{
fmt::{Debug, Display},
num::TryFromIntError,
};
use derive_new::new; use derive_new::new;
use nom::{ use nom::{
character::complete::one_of, character::complete::one_of,
combinator::all_consuming, combinator::all_consuming,
error::{context, convert_error, ContextError, ParseError, VerboseError}, error::{context, convert_error, ContextError, FromExternalError, ParseError, VerboseError},
multi::{many0, many1}, multi::{many0, many1},
sequence::{preceded, terminated}, sequence::{preceded, terminated},
Needed, Parser, IResult, Needed, Parser,
}; };
use serde::{ use serde::{
de::{self, Deserializer, Visitor}, de::{self, Deserializer, Visitor},
@ -50,10 +53,7 @@ impl<'de> Deserialize<'de> for Atoms {
if sheet.is_empty() { if sheet.is_empty() {
Ok(Default::default()) Ok(Default::default())
} else { } else {
all_consuming(terminated( root(&sheet, &notes)
many1(preceded(maybe_yml_str_space(), atom(&notes))),
maybe_yml_str_space(),
))(&sheet)
.map_err(|e: nom::Err<VerboseError<&str>>| match e { .map_err(|e: nom::Err<VerboseError<&str>>| match e {
nom::Err::Incomplete(Needed::Unknown) => "needed some more bytes".to_string(), nom::Err::Incomplete(Needed::Unknown) => "needed some more bytes".to_string(),
nom::Err::Incomplete(Needed::Size(n)) => format!("needed {} more bytes", n), nom::Err::Incomplete(Needed::Size(n)) => format!("needed {} more bytes", n),
@ -77,3 +77,16 @@ where
{ {
context("yml white space", many0(one_of(" \t\r\n"))) context("yml white space", many0(one_of(" \t\r\n")))
} }
fn root<'a, E>(i: &'a str, notes: &'a str) -> IResult<&'a str, Vec<FlatAtom>, E>
where
E: ParseError<&'a str>
+ ContextError<&'a str>
+ FromExternalError<&'a str, TryFromIntError>
+ FromExternalError<&'a str, E>,
{
all_consuming(terminated(
many1(preceded(maybe_yml_str_space(), atom(notes))),
maybe_yml_str_space(),
))(i)
}