Skip to content

Commit 37895b7

Browse files
committed
chore(pc): Removed and_opt parser, adding one_of_p
1 parent d51f421 commit 37895b7

7 files changed

Lines changed: 48 additions & 43 deletions

File tree

rusty_parser/src/core/dim_name.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,15 @@ mod type_definition {
212212
}
213213

214214
fn built_in_string() -> impl Parser<StringView, Output = DimType, Error = ParserError> {
215-
keyword(Keyword::String).and_opt(
216-
star_ws().and_keep_right(expression_pos_p().or_expected("string length after *")),
217-
|_, opt_len| match opt_len {
215+
keyword(Keyword::String)
216+
.and_keep_right(star_and_length().to_option())
217+
.map(|opt_len| match opt_len {
218218
Some(len) => DimType::FixedLengthString(len, 0),
219219
_ => DimType::BuiltIn(TypeQualifier::DollarString, BuiltInStyle::Extended),
220-
},
221-
)
220+
})
221+
}
222+
223+
fn star_and_length() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
224+
star_ws().and_keep_right(expression_pos_p().or_expected("string length after *"))
222225
}
223226
}

rusty_parser/src/core/global_statement.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,9 @@ pub fn program_parser_p() -> impl Parser<StringView, Output = Program, Error = P
151151
}
152152

153153
fn main_program() -> impl Parser<StringView, Output = Program, Error = ParserError> {
154-
global_statement_pos_p().and_opt(next_statements(), |first, opt_next| {
154+
global_statement_pos_p().and(next_statements(), |first, mut opt_next| {
155155
let mut program = vec![first];
156-
if let Some(next) = opt_next {
157-
program.extend(next);
158-
}
156+
program.append(&mut opt_next);
159157
program
160158
})
161159
}

rusty_parser/src/core/if_block.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ fn single_line_if_else_p() -> impl Parser<
5050
Output = (Statements, Vec<ConditionalBlock>, Option<Statements>),
5151
Error = ParserError,
5252
> {
53-
single_line_non_comment_statements_p().and_opt(
53+
single_line_non_comment_statements_p().and(
5454
// comment or ELSE
55-
whitespace_ignoring()
56-
.and(comment_p().with_pos(), |_, s| vec![s])
57-
.or(single_line_else_p()),
55+
single_line_comment_p().or(single_line_else_p()).to_option(),
5856
|l, r| (l, vec![], r),
5957
)
6058
}
6159

60+
fn single_line_comment_p() -> impl Parser<StringView, Output = Statements, Error = ParserError> {
61+
whitespace_ignoring()
62+
.to_option()
63+
.and(comment_p().with_pos(), |_, s| vec![s])
64+
}
65+
6266
fn single_line_else_p() -> impl Parser<StringView, Output = Statements, Error = ParserError> {
6367
whitespace_ignoring()
6468
.and(keyword(Keyword::Else), IgnoringBothCombiner)

rusty_parser/src/core/implementation.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rusty_pc::and::{IgnoringBothCombiner, opt_and};
1+
use rusty_pc::and::IgnoringBothCombiner;
22
use rusty_pc::*;
33

44
use crate::core::declaration::{function_declaration_p, sub_declaration_p};
@@ -56,14 +56,13 @@ fn static_declaration_p<P, T>(
5656
where
5757
P: Parser<StringView, Output = T, Error = ParserError>,
5858
{
59-
parser.and_opt(
60-
opt_and(
61-
whitespace_ignoring(),
62-
keyword(Keyword::Static),
63-
IgnoringBothCombiner,
64-
),
65-
|l, r: Option<()>| (l, r.is_some()),
66-
)
59+
parser.and(ws_static().to_option(), |l, r: Option<()>| (l, r.is_some()))
60+
}
61+
62+
fn ws_static() -> impl Parser<StringView, Output = (), Error = ParserError> {
63+
whitespace_ignoring()
64+
.to_option()
65+
.and(keyword(Keyword::Static), IgnoringBothCombiner)
6766
}
6867

6968
#[cfg(test)]

rusty_parser/src/tokens/any_token.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn eol() -> impl Parser<StringView, Output = Token, Error = ParserError> {
5353

5454
fn cr_or_crlf() -> impl Parser<StringView, Output = Token, Error = ParserError> {
5555
one_p('\r')
56-
.and_opt(one_p('\n'), StringCombiner)
56+
.and(one_p('\n').to_option(), StringCombiner)
5757
.to_token(TokenType::Eol)
5858
}
5959

@@ -62,18 +62,20 @@ fn lf() -> impl Parser<StringView, Output = Token, Error = ParserError> {
6262
}
6363

6464
fn gt_or_ge() -> impl Parser<StringView, Output = Token, Error = ParserError> {
65-
one_p('>').and_opt(one_p('='), StringCombiner).map(|text| {
66-
if text.len() == 1 {
67-
Token::new(TokenType::Greater.get_index(), text)
68-
} else {
69-
Token::new(TokenType::GreaterEquals.get_index(), text)
70-
}
71-
})
65+
one_p('>')
66+
.and(one_p('=').to_option(), StringCombiner)
67+
.map(|text| {
68+
if text.len() == 1 {
69+
Token::new(TokenType::Greater.get_index(), text)
70+
} else {
71+
Token::new(TokenType::GreaterEquals.get_index(), text)
72+
}
73+
})
7274
}
7375

7476
fn lt_or_le_or_ne() -> impl Parser<StringView, Output = Token, Error = ParserError> {
7577
one_p('<')
76-
.and_opt(one_p('>').or(one_p('=')), StringCombiner)
78+
.and(one_of_p(&['>', '=']).to_option(), StringCombiner)
7779
.map(|text| {
7880
if text.len() == 1 {
7981
Token::new(TokenType::Less.get_index(), text)

rusty_pc/src/parser.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,6 @@ where
6666
self.and(right, KeepRightCombiner)
6767
}
6868

69-
/// Parses the left side and optionally the right side.
70-
/// The combiner function maps the left and (optional) right output to the final result.
71-
/// TODO #[deprecated]
72-
fn and_opt<R, F, O>(self, right: R, combiner: F) -> AndParser<Self, ToOptionParser<R>, F, O>
73-
where
74-
Self: Sized,
75-
R: Parser<I, C, Error = Self::Error>,
76-
F: Combiner<Self::Output, Option<R::Output>, O>,
77-
{
78-
self.and(right.to_option(), combiner)
79-
}
80-
8169
// =======================================================================
8270
// AndThen
8371
// =======================================================================

rusty_pc/src/top_level.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,14 @@ where
7474
{
7575
read_p().filter(move |x: &O| *x == needle)
7676
}
77+
78+
/// Parses one of the given elements.
79+
/// Note that the operation has O(n) complexity.
80+
pub fn one_of_p<I, O, E>(needles: &[O]) -> impl Parser<I, Output = O, Error = E>
81+
where
82+
I: InputTrait<Output = O>,
83+
O: PartialEq,
84+
E: ParserErrorTrait,
85+
{
86+
read_p().filter(move |x: &O| needles.contains(x))
87+
}

0 commit comments

Comments
 (0)