Skip to content

Commit 1f1a7d0

Browse files
committed
feat(pc): Support different context type for the inner parser of flatten
1 parent c57f68f commit 1f1a7d0

6 files changed

Lines changed: 30 additions & 20 deletions

File tree

rusty_parser/src/core/dim_name.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ mod type_definition {
185185
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier";
186186
}
187187

188-
OrParser::new(parsers)
189-
.with_expected_message(expected_message)
190-
.no_context()
188+
OrParser::new(parsers).with_expected_message(expected_message)
191189
})
192190
.flatten()
193191
}

rusty_parser/src/core/opt_second_expression.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ pub(super) fn conditionally_opt_whitespace()
7575
whitespace()
7676
.map(Some)
7777
.and_then_err(move |err| if allow_none { Ok(None) } else { Err(err) })
78-
.no_context()
7978
})
8079
.flatten()
8180
}

rusty_parser/src/core/param_name.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ fn extended_type()
106106
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier",
107107
)
108108
.boxed()
109-
.no_context()
110109
} else {
111-
built_in_extended_type().boxed().no_context()
110+
built_in_extended_type().boxed()
112111
}
113112
})
114113
.flatten()

rusty_parser/src/core/statements.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ fn ctx_demand_separator_p()
9292
ctx_parser()
9393
.map(|last_statement_was_comment| {
9494
if last_statement_was_comment {
95-
comment_separator().boxed().no_context()
95+
comment_separator().boxed()
9696
} else {
97-
common_separator().boxed().no_context()
97+
common_separator().boxed()
9898
}
9999
})
100100
.flatten()

rusty_pc/src/flatten.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1+
use std::marker::PhantomData;
2+
13
use crate::{InputTrait, Parser, SetContext};
24

3-
pub struct FlattenParser<P> {
5+
pub struct FlattenParser<P, CIn> {
46
parser: P,
7+
_marker: PhantomData<CIn>,
58
}
6-
impl<P> FlattenParser<P> {
9+
10+
impl<P, CIn> FlattenParser<P, CIn> {
711
pub(crate) fn new(parser: P) -> Self {
8-
Self { parser }
12+
Self {
13+
parser,
14+
_marker: PhantomData,
15+
}
916
}
1017
}
11-
impl<I, C, P> Parser<I, C> for FlattenParser<P>
18+
19+
impl<I, COut, CIn, P> Parser<I, COut> for FlattenParser<P, CIn>
1220
where
1321
I: InputTrait,
14-
P: Parser<I, C>,
15-
P::Output: Parser<I, C, Error = P::Error>,
22+
P: Parser<I, COut>,
23+
P::Output: Parser<I, CIn, Error = P::Error>,
1624
{
17-
type Output = <P::Output as Parser<I, C>>::Output;
25+
type Output = <P::Output as Parser<I, CIn>>::Output;
1826
type Error = P::Error;
1927
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
2028
match self.parser.parse(input) {
@@ -23,11 +31,11 @@ where
2331
}
2432
}
2533
}
26-
impl<C, P> SetContext<C> for FlattenParser<P>
34+
impl<P, COut, CIn> SetContext<COut> for FlattenParser<P, CIn>
2735
where
28-
P: SetContext<C>,
36+
P: SetContext<COut>,
2937
{
30-
fn set_context(&mut self, ctx: C) {
38+
fn set_context(&mut self, ctx: COut) {
3139
self.parser.set_context(ctx)
3240
}
3341
}

rusty_pc/src/parser.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ where
154154
// Boxed
155155
// =======================================================================
156156

157+
/// Boxes the given parser, erasing its type.
157158
fn boxed(self) -> BoxedParser<I, C, Self::Output, Self::Error>
158159
where
159160
Self: Sized + 'static,
@@ -242,10 +243,15 @@ where
242243
// Flatten
243244
// =======================================================================
244245

245-
fn flatten(self) -> FlattenParser<Self>
246+
/// Similar to a flat map operation,
247+
/// if the current parser's output is a parser,
248+
/// it returns the result of that inner parser.
249+
/// Typically used together with ctx_parser and map.
250+
/// The context type of the inner parser can be different (`CIn` type).
251+
fn flatten<CIn>(self) -> FlattenParser<Self, CIn>
246252
where
247253
Self: Sized,
248-
Self::Output: Parser<I, C, Error = Self::Error>,
254+
Self::Output: Parser<I, CIn, Error = Self::Error>,
249255
{
250256
FlattenParser::new(self)
251257
}

0 commit comments

Comments
 (0)