Skip to content

Commit 46fabae

Browse files
committed
chore(pc): Moved IifParser to its own module and renamed to IifCtxParser
1 parent d6d2760 commit 46fabae

6 files changed

Lines changed: 52 additions & 43 deletions

File tree

rusty_parser/src/core/print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn print_args_parser() -> impl Parser<StringView, Output = Vec<PrintArg>, Error
161161
}
162162

163163
fn print_arg_parser() -> impl Parser<StringView, bool, Output = PrintArg, Error = ParserError> {
164-
IifParser::new(delimiter_print_arg(), any_print_arg())
164+
IifCtxParser::new(delimiter_print_arg(), any_print_arg())
165165
}
166166

167167
fn any_print_arg() -> impl Parser<StringView, Output = PrintArg, Error = ParserError> {

rusty_parser/src/expr/binary_expression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn is_keyword_op(op: &Positioned<Operator>) -> bool {
4444
fn expr_after_binary_operator()
4545
-> impl Parser<StringView, bool, Output = ExpressionPos, Error = ParserError> {
4646
// boxed breaks apart the recursive type evaluation
47-
IifParser::new(
47+
IifCtxParser::new(
4848
// the previous operator is a keyword op, must have whitespace or parenthesis
4949
ws_expr_pos_p().boxed(),
5050
// the previous operator is a symbol, whitespace is optional
@@ -71,7 +71,7 @@ fn non_bin_expr() -> impl Parser<StringView, Output = ExpressionPos, Error = Par
7171
/// parenthesis. If that is the case, leading whitespace is not required for
7272
/// keyword based operators.
7373
fn operator() -> impl Parser<StringView, bool, Output = Positioned<Operator>, Error = ParserError> {
74-
IifParser::new(
74+
IifCtxParser::new(
7575
// no whitespace needed
7676
lead_opt_ws(operator_p()),
7777
// whitespace needed

rusty_parser/src/pc_specific/whitespace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rusty_pc::{IifParser, Parser, SurroundMode, surround};
1+
use rusty_pc::{IifCtxParser, Parser, SurroundMode, surround};
22

33
use crate::ParserError;
44
use crate::input::StringView;
@@ -84,7 +84,7 @@ where
8484
/// * `1 + 2AND` the lack of whitespace before `AND` is an error
8585
pub fn conditionally_opt_whitespace()
8686
-> impl Parser<StringView, bool, Output = (), Error = ParserError> {
87-
IifParser::new(
87+
IifCtxParser::new(
8888
// allow none
8989
opt_ws(),
9090
// whitespace is required

rusty_pc/src/ctx.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,3 @@ where
4444
self.0 = Some(ctx.clone());
4545
}
4646
}
47-
/// Based on the boolean context, parses using the left or the right parser.
48-
pub struct IifParser<L, R> {
49-
left: L,
50-
right: R,
51-
context: bool,
52-
}
53-
54-
impl<L, R> IifParser<L, R> {
55-
pub fn new(left: L, right: R) -> Self {
56-
Self {
57-
left,
58-
right,
59-
context: false,
60-
}
61-
}
62-
}
63-
64-
impl<L, R, I> Parser<I, bool> for IifParser<L, R>
65-
where
66-
I: InputTrait,
67-
L: Parser<I>,
68-
R: Parser<I, Output = L::Output, Error = L::Error>,
69-
{
70-
type Output = L::Output;
71-
type Error = L::Error;
72-
73-
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
74-
if self.context {
75-
self.left.parse(input)
76-
} else {
77-
self.right.parse(input)
78-
}
79-
}
80-
81-
fn set_context(&mut self, ctx: &bool) {
82-
self.context = *ctx;
83-
}
84-
}

rusty_pc/src/iif_ctx.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::{InputTrait, Parser};
2+
3+
/// Based on the boolean context, parses using the left or the right parser.
4+
pub struct IifCtxParser<L, R> {
5+
left: L,
6+
right: R,
7+
context: bool,
8+
}
9+
10+
impl<L, R> IifCtxParser<L, R> {
11+
pub fn new<I>(left: L, right: R) -> Self
12+
where
13+
I: InputTrait,
14+
L: Parser<I>,
15+
R: Parser<I, Output = L::Output, Error = L::Error>,
16+
{
17+
Self {
18+
left,
19+
right,
20+
context: false,
21+
}
22+
}
23+
}
24+
25+
impl<L, R, I> Parser<I, bool> for IifCtxParser<L, R>
26+
where
27+
I: InputTrait,
28+
L: Parser<I>,
29+
R: Parser<I, Output = L::Output, Error = L::Error>,
30+
{
31+
type Output = L::Output;
32+
type Error = L::Error;
33+
34+
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
35+
if self.context {
36+
self.left.parse(input)
37+
} else {
38+
self.right.parse(input)
39+
}
40+
}
41+
42+
fn set_context(&mut self, ctx: &bool) {
43+
self.context = *ctx;
44+
}
45+
}

rusty_pc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod delimited;
99
pub mod filter;
1010
pub mod filter_map;
1111
pub mod flatten;
12+
mod iif_ctx;
1213
mod lazy;
1314
pub mod many;
1415
pub mod many_ctx;
@@ -30,6 +31,7 @@ mod token;
3031
mod top_level;
3132

3233
pub use ctx::*;
34+
pub use iif_ctx::*;
3335
pub use lazy::*;
3436
pub use or::*;
3537
pub use parser::*;

0 commit comments

Comments
 (0)