Skip to content

Commit 7938fd6

Browse files
committed
chore: Removed AccumulateParser
It was only used in two places and it can implemented as `first.and(second.zero_or_more())`
1 parent 9974252 commit 7938fd6

5 files changed

Lines changed: 50 additions & 80 deletions

File tree

rusty_parser/src/pc/accumulate.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

rusty_parser/src/pc/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! base module contains functionality that can be extracted into a library,
22
//! it's generic and not specific to QBasic
3-
mod accumulate;
43
mod allow_none_if;
54
mod and;
65
mod and_without_undo;
@@ -31,7 +30,6 @@ pub mod supplier;
3130
mod to_option;
3231
mod tokenizers;
3332

34-
pub use accumulate::*;
3533
pub use allow_none_if::*;
3634
pub use and::*;
3735
pub use and_without_undo::AndWithoutUndo;

rusty_parser/src/specific/built_ins/close.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ pub fn parse() -> impl Parser<RcStringView, Output = Statement> {
1818
}
1919

2020
fn file_handles() -> impl Parser<RcStringView, Output = Expressions> {
21-
AccumulateParser::new(
22-
guarded_file_handle_or_expression_p(),
23-
comma().and_without_undo_keep_right(file_handle_or_expression_p()),
24-
)
25-
.or_default()
21+
guarded_file_handle_or_expression_p()
22+
.map(|first| vec![first])
23+
.and(
24+
comma()
25+
.and_without_undo_keep_right(file_handle_or_expression_p())
26+
.zero_or_more(),
27+
|mut l, mut r| {
28+
l.append(&mut r);
29+
l
30+
},
31+
)
32+
.or_default()
2633
}
2734

2835
fn file_handle_or_expression_p() -> impl Parser<RcStringView, Output = ExpressionPos> {

rusty_parser/src/specific/core/expression.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,16 @@ pub fn csv_expressions_non_opt(
469469
/// Missing expressions are not allowed.
470470
/// The first expression needs to be preceded by space or surrounded in parenthesis.
471471
pub fn csv_expressions_first_guarded() -> impl Parser<RcStringView, Output = Expressions> {
472-
AccumulateParser::new(
473-
ws_expr_pos_p(),
474-
comma().and_without_undo_keep_right(
475-
expression_pos_p().or_syntax_error("Expected: expression after comma"),
476-
),
472+
ws_expr_pos_p().map(|first| vec![first]).and(
473+
comma()
474+
.and_without_undo_keep_right(
475+
expression_pos_p().or_syntax_error("Expected: expression after comma"),
476+
)
477+
.zero_or_more(),
478+
|mut l, mut r| {
479+
l.append(&mut r);
480+
l
481+
},
477482
)
478483
}
479484

rusty_parser/src/specific/pc_specific/recognizers_impl.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,25 @@ fn eol() -> impl Parser<RcStringView, Output = Token> {
9696

9797
fn crlf() -> impl Parser<RcStringView, Output = Token> {
9898
char_parsers::specific('\r')
99-
.append_char(char_parsers::specific('\n'))
99+
.concat(char_parsers::specific('\n'))
100100
.to_token(TokenType::Eol)
101101
}
102102

103103
fn greater_or_equal() -> impl Parser<RcStringView, Output = Token> {
104104
char_parsers::specific('>')
105-
.append_char(char_parsers::specific('='))
105+
.concat(char_parsers::specific('='))
106106
.to_token(TokenType::GreaterEquals)
107107
}
108108

109109
fn less_or_equal() -> impl Parser<RcStringView, Output = Token> {
110110
char_parsers::specific('<')
111-
.append_char(char_parsers::specific('='))
111+
.concat(char_parsers::specific('='))
112112
.to_token(TokenType::LessEquals)
113113
}
114114

115115
fn not_equal() -> impl Parser<RcStringView, Output = Token> {
116116
char_parsers::specific('<')
117-
.append_char(char_parsers::specific('>'))
117+
.concat(char_parsers::specific('>'))
118118
.to_token(TokenType::NotEquals)
119119
}
120120

@@ -153,16 +153,20 @@ where
153153
F: Fn(&char) -> bool,
154154
{
155155
char_parsers::filter(predicate)
156-
.concatenate()
156+
.many_to_str()
157157
.to_token(token_type)
158158
}
159159

160160
fn specific(token_type: TokenType, needle: char) -> impl Parser<RcStringView, Output = Token> {
161-
char_parsers::specific(needle).to_str().to_token(token_type)
161+
char_parsers::specific(needle)
162+
.one_to_str()
163+
.to_token(token_type)
162164
}
163165

164166
fn unknown() -> impl Parser<RcStringView, Output = Token> {
165-
char_parsers::any().to_str().to_token(TokenType::Unknown)
167+
char_parsers::any()
168+
.one_to_str()
169+
.to_token(TokenType::Unknown)
166170
}
167171

168172
fn oct_digits() -> impl Parser<RcStringView, Output = Token> {
@@ -182,19 +186,16 @@ where
182186
F: Fn(&char) -> bool,
183187
{
184188
char_parsers::specific('&')
185-
.append_char(char_parsers::specific(radix))
189+
.concat(char_parsers::specific(radix))
186190
.and(
187-
char_parsers::specific('-').to_option().and(
188-
char_parsers::filter(predicate).concatenate(),
189-
|opt_minus, mut digits| {
190-
match opt_minus {
191-
Some(minus) => {
192-
// TODO prevent insert
193-
digits.insert(0, minus);
194-
digits
195-
}
196-
_ => digits,
191+
char_parsers::specific('-').one_to_str().to_option().and(
192+
char_parsers::filter(predicate).many_to_str(),
193+
|opt_minus, digits| match opt_minus {
194+
Some(mut minus) => {
195+
minus.push_str(&digits);
196+
minus
197197
}
198+
_ => digits,
198199
},
199200
),
200201
|mut left, right| {
@@ -249,14 +250,14 @@ mod string_parsers {
249250
use super::*;
250251

251252
pub trait CharToStringParser<I> {
252-
fn concatenate(self) -> impl Parser<I, Output = String>;
253+
/// Reads as many chars possible from the underlying parser and returns them as a string.
254+
fn many_to_str(self) -> impl Parser<I, Output = String>;
253255

254-
fn to_str(self) -> impl Parser<I, Output = String>;
256+
/// Reads one char possible from the underlying parser and converts it into a string.
257+
fn one_to_str(self) -> impl Parser<I, Output = String>;
255258

256-
fn append_char(
257-
self,
258-
other: impl Parser<I, Output = char>,
259-
) -> impl Parser<I, Output = String>
259+
/// A parser that reads two chars together and returns them as a string.
260+
fn concat(self, other: impl Parser<I, Output = char>) -> impl Parser<I, Output = String>
260261
where
261262
I: Clone;
262263
}
@@ -266,21 +267,18 @@ mod string_parsers {
266267
I: Clone,
267268
P: Parser<I, Output = char>,
268269
{
269-
fn concatenate(self) -> impl Parser<I, Output = String> {
270+
fn many_to_str(self) -> impl Parser<I, Output = String> {
270271
self.many(String::from, |mut s: String, c| {
271272
s.push(c);
272273
s
273274
})
274275
}
275276

276-
fn to_str(self) -> impl Parser<I, Output = String> {
277+
fn one_to_str(self) -> impl Parser<I, Output = String> {
277278
self.map(String::from)
278279
}
279280

280-
fn append_char(
281-
self,
282-
other: impl Parser<I, Output = char>,
283-
) -> impl Parser<I, Output = String> {
281+
fn concat(self, other: impl Parser<I, Output = char>) -> impl Parser<I, Output = String> {
284282
self.and(other, |l, r| {
285283
let mut s = String::from(l);
286284
s.push(r);

0 commit comments

Comments
 (0)