#![doc(html_root_url = "https://docs.rs/kparse")]
#![warn(absolute_paths_not_starting_with_crate)]
#![warn(elided_lifetimes_in_paths)]
#![warn(explicit_outlives_requirements)]
#![warn(keyword_idents)]
#![warn(macro_use_extern_crate)]
#![warn(meta_variable_misuse)]
#![warn(missing_abi)]
#![warn(missing_docs)]
#![warn(non_ascii_idents)]
#![warn(noop_method_call)]
#![warn(pointer_structural_match)]
#![warn(semicolon_in_expressions_from_macros)]
#![warn(trivial_casts)]
#![warn(trivial_numeric_casts)]
#![warn(unreachable_pub)]
#![warn(unstable_features)]
#![warn(unused_import_braces)]
#![warn(unused_lifetimes)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::type_complexity)]
use nom::AsBytes;
use std::fmt::{Debug, Display};
pub mod combinators;
pub mod error;
pub mod spans;
pub mod test;
pub mod tracker;
mod context;
mod debug;
pub use crate::context::Context;
pub use crate::error::ParserError;
pub mod prelude {
pub use crate::error::AppendParserError;
pub use crate::spans::{Fragment, LocatedSpanExt, SpanExt};
pub use crate::tracker::{FindTracker, TrackError};
pub use crate::{ResultWithSpan, WithCode, WithSpan};
}
pub trait Code: Copy + Display + Debug + Eq {
const NOM_ERROR: Self;
}
pub trait WithSpan<C: Code, I, E> {
fn with_span(self, code: C, span: I) -> nom::Err<E>;
}
pub trait ResultWithSpan<C: Code, I, R> {
fn with_span(self, code: C, span: I) -> R;
}
pub trait WithCode<C: Code, R> {
fn with_code(self, code: C) -> R;
}
impl<C, I, Y> WithSpan<C, I, ParserError<C, I, Y>> for std::num::ParseIntError
where
C: Code,
I: AsBytes + Copy,
Y: Copy,
{
fn with_span(self, code: C, span: I) -> nom::Err<ParserError<C, I, Y>> {
nom::Err::Failure(ParserError::new(code, span))
}
}
impl<C, I, Y> WithSpan<C, I, ParserError<C, I, Y>> for std::num::ParseFloatError
where
C: Code,
I: AsBytes + Copy,
Y: Copy,
{
fn with_span(self, code: C, span: I) -> nom::Err<ParserError<C, I, Y>> {
nom::Err::Failure(ParserError::new(code, span))
}
}
impl<C, I, Y> WithSpan<C, I, ParserError<C, I, Y>> for nom::error::Error<I>
where
I: AsBytes + Copy,
C: Code,
Y: Copy,
{
fn with_span(self, code: C, span: I) -> nom::Err<ParserError<C, I, Y>> {
nom::Err::Error(ParserError::new(code, span).with_nom(self.input, self.code))
}
}
impl<C, I, Y> WithSpan<C, I, ParserError<C, I, Y>> for nom::Err<nom::error::Error<I>>
where
I: AsBytes + Copy,
C: Code,
Y: Copy,
{
fn with_span(self, code: C, span: I) -> nom::Err<ParserError<C, I, Y>> {
match self {
nom::Err::Incomplete(e) => nom::Err::Incomplete(e),
nom::Err::Error(e) => {
nom::Err::Error(ParserError::new(code, span).with_nom(e.input, e.code))
}
nom::Err::Failure(e) => {
nom::Err::Failure(ParserError::new(code, span).with_nom(e.input, e.code))
}
}
}
}
impl<C, I, Y> WithCode<C, ParserError<C, I, Y>> for nom::error::Error<I>
where
I: AsBytes + Copy,
C: Code,
Y: Copy,
{
fn with_code(self, code: C) -> ParserError<C, I, Y> {
ParserError::new(code, self.input).with_nom(self.input, self.code)
}
}
impl<C, I, Y> From<ParserError<C, I, Y>> for nom::Err<ParserError<C, I, Y>>
where
C: Code,
I: AsBytes + Copy,
Y: Copy,
{
fn from(e: ParserError<C, I, Y>) -> Self {
nom::Err::Error(e)
}
}
impl<C, I, Y> WithCode<C, ParserError<C, I, Y>> for ParserError<C, I, Y>
where
I: AsBytes + Copy,
C: Code,
Y: Copy,
{
fn with_code(self, code: C) -> ParserError<C, I, Y> {
ParserError::with_code(self, code)
}
}
impl<C, I, E, Y> WithCode<C, nom::Err<ParserError<C, I, Y>>> for nom::Err<E>
where
C: Code,
I: AsBytes + Copy,
E: WithCode<C, ParserError<C, I, Y>>,
Y: Copy,
{
fn with_code(self, code: C) -> nom::Err<ParserError<C, I, Y>> {
match self {
nom::Err::Incomplete(e) => nom::Err::Incomplete(e),
nom::Err::Error(e) => {
let p_err: ParserError<C, I, Y> = e.with_code(code);
nom::Err::Error(p_err)
}
nom::Err::Failure(e) => {
let p_err: ParserError<C, I, Y> = e.with_code(code);
nom::Err::Failure(p_err)
}
}
}
}
impl<C, I, O, E, Y> ResultWithSpan<C, I, Result<O, nom::Err<ParserError<C, I, Y>>>> for Result<O, E>
where
E: WithSpan<C, I, ParserError<C, I, Y>>,
C: Code,
I: AsBytes + Copy,
Y: Copy,
{
fn with_span(self, code: C, span: I) -> Result<O, nom::Err<ParserError<C, I, Y>>> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.with_span(code, span)),
}
}
}
impl<C, I, O, E, Y> WithCode<C, Result<(I, O), nom::Err<ParserError<C, I, Y>>>>
for Result<(I, O), E>
where
E: WithCode<C, nom::Err<ParserError<C, I, Y>>>,
C: Code,
I: AsBytes + Copy,
Y: Copy,
{
fn with_code(self, code: C) -> Result<(I, O), nom::Err<ParserError<C, I, Y>>> {
match self {
Ok(v) => Ok(v),
Err(e) => {
let p_err: nom::Err<ParserError<C, I, Y>> = e.with_code(code);
Err(p_err)
}
}
}
}