Skip to content

Commit 504ed2e

Browse files
committed
Created dedicated MapSoftErrParser
1 parent ff94030 commit 504ed2e

5 files changed

Lines changed: 49 additions & 25 deletions

File tree

rusty_parser/src/pc_specific/or_expected.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rusty_pc::map_err::{MapErrParser, SoftErrorOverrider};
1+
use rusty_pc::map_soft_err::MapSoftErrParser;
22
use rusty_pc::{Parser, ParserErrorTrait};
33

44
use crate::error::ParserError;
@@ -11,7 +11,7 @@ where
1111
/// Demands a successful result or returns a fatal syntax error
1212
/// with an error message like "Expected: " followed by the
1313
/// given expectation message.
14-
fn or_expected(self, expectation: &str) -> MapErrParser<Self, SoftErrorOverrider<Self::Error>> {
14+
fn or_expected(self, expectation: &str) -> MapSoftErrParser<Self, Self::Error> {
1515
self.or_fail(ParserError::expected(expectation).to_fatal())
1616
}
1717
}

rusty_pc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod map;
1717
pub mod map_ctx;
1818
pub mod map_decorator;
1919
pub mod map_err;
20+
pub mod map_soft_err;
2021
pub mod no_context;
2122
mod or;
2223
pub mod or_default;

rusty_pc/src/map_err.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,6 @@ where
4242
fn map(&self, err: E) -> E;
4343
}
4444

45-
/// Overrides a soft error with the given value.
46-
pub struct SoftErrorOverrider<E>(E);
47-
48-
impl<E> SoftErrorOverrider<E> {
49-
pub fn new(err: E) -> Self {
50-
Self(err)
51-
}
52-
}
53-
54-
impl<E> ErrorMapper<E> for SoftErrorOverrider<E>
55-
where
56-
E: ParserErrorTrait,
57-
{
58-
fn map(&self, err: E) -> E {
59-
if err.is_soft() { self.0.clone() } else { err }
60-
}
61-
}
62-
6345
/// Overrides a fatal error with the given value.
6446
pub struct FatalErrorOverrider<E>(E);
6547

rusty_pc/src/map_soft_err.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::map_decorator::{MapDecorator, MapDecoratorMarker};
2+
use crate::{InputTrait, Parser, ParserErrorTrait};
3+
4+
pub struct MapSoftErrParser<P, E> {
5+
parser: P,
6+
err: E,
7+
}
8+
9+
impl<P, E> MapSoftErrParser<P, E> {
10+
pub(crate) fn new(parser: P, err: E) -> Self {
11+
Self { parser, err }
12+
}
13+
}
14+
15+
impl<I, C, P, E> MapDecorator<I, C> for MapSoftErrParser<P, E>
16+
where
17+
I: InputTrait,
18+
P: Parser<I, C, Error = E>,
19+
E: ParserErrorTrait,
20+
{
21+
type OriginalOutput = P::Output;
22+
type Output = P::Output;
23+
type Error = E;
24+
25+
fn decorated(
26+
&mut self,
27+
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
28+
&mut self.parser
29+
}
30+
31+
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
32+
Ok(ok)
33+
}
34+
35+
fn map_soft_error(&self, _err: Self::Error) -> Result<Self::Output, Self::Error> {
36+
Err(self.err.clone())
37+
}
38+
}
39+
40+
impl<P, E> MapDecoratorMarker for MapSoftErrParser<P, E> {}

rusty_pc/src/parser.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::flatten::FlattenParser;
1010
use crate::many::{ManyCombiner, ManyParser, VecManyCombiner};
1111
use crate::map::{MapParser, MapToUnitParser};
1212
use crate::map_ctx::MapCtxParser;
13-
use crate::map_err::{ErrorMapper, FatalErrorOverrider, MapErrParser, SoftErrorOverrider};
13+
use crate::map_err::{ErrorMapper, FatalErrorOverrider, MapErrParser};
14+
use crate::map_soft_err::MapSoftErrParser;
1415
use crate::no_context::NoContextParser;
1516
use crate::or_default::OrDefaultParser;
1617
use crate::peek::PeekParser;
@@ -289,7 +290,7 @@ where
289290
}
290291

291292
// =======================================================================
292-
// MapErr
293+
// MapSoftErr
293294
// =======================================================================
294295

295296
/// Maps the error of this parser.
@@ -306,16 +307,16 @@ where
306307

307308
/// If this parser returns a soft error, the soft error will be replaced by
308309
/// the given error (which might be soft or fatal).
309-
fn with_soft_err(self, err: Self::Error) -> MapErrParser<Self, SoftErrorOverrider<Self::Error>>
310+
fn with_soft_err(self, err: Self::Error) -> MapSoftErrParser<Self, Self::Error>
310311
where
311312
Self: Sized,
312313
{
313-
self.map_err(SoftErrorOverrider::new(err))
314+
MapSoftErrParser::new(self, err)
314315
}
315316

316317
/// If this parser returns a soft error, the soft error will be replaced by
317318
/// the given error, which must be fatal.
318-
fn or_fail(self, err: Self::Error) -> MapErrParser<Self, SoftErrorOverrider<Self::Error>>
319+
fn or_fail(self, err: Self::Error) -> MapSoftErrParser<Self, Self::Error>
319320
where
320321
Self: Sized,
321322
{

0 commit comments

Comments
 (0)