Skip to content

Commit 4ca0832

Browse files
committed
chore(parser): Simplify implementation of any_token_ws in favor of padded_by_ws
1 parent 5925cf0 commit 4ca0832

2 files changed

Lines changed: 7 additions & 144 deletions

File tree

rusty_parser/src/tokens/any_token_of.rs

Lines changed: 6 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,29 @@
11
use std::collections::HashSet;
22

3-
use rusty_pc::{ParseErr, ParseResult, Parser, Token, TokenKind};
3+
use rusty_pc::{ParseResult, Parser, Token, TokenKind};
44

55
use crate::ParseError;
66
use crate::tokens::TokenType;
77

8-
// TODO support "ignoring" mode for all tokens
9-
108
macro_rules! any_token_of {
119
// full options
1210
(
1311
types = $($token_type:expr),*
1412
;
1513
symbols = $($symbol:literal),*
1614
;
17-
ws = $ws:literal
18-
;
1915
mode = $match_mode:expr
2016
$(,)?
2117
) => {
2218
$crate::tokens::AnyTokenOf::new_multi(
2319
$crate::tokens::any_token(),
2420
&[ $($token_type),* ],
2521
&[$($symbol),*],
26-
$ws,
2722
$match_mode
2823
)
2924
};
3025

3126
// minus mode
32-
(
33-
types = $($token_type:expr),*
34-
;
35-
symbols = $($symbol:literal),*
36-
;
37-
ws = $ws:literal
38-
$(,)?
39-
) => {
40-
any_token_of!(
41-
types = $($token_type),*
42-
;
43-
symbols = $($symbol),*
44-
;
45-
ws = $ws
46-
;
47-
mode = $crate::tokens::MatchMode::Include
48-
)
49-
};
50-
51-
// minus ws
52-
(
53-
types = $($token_type:expr),*
54-
;
55-
symbols = $($symbol:literal),*
56-
;
57-
mode = $match_mode:expr
58-
$(,)?
59-
) => {
60-
any_token_of!(
61-
types = $($token_type),*
62-
;
63-
symbols = $($symbol),*
64-
;
65-
ws = false
66-
;
67-
mode = $match_mode
68-
)
69-
};
70-
71-
// minus ws and minus mode
7227
(
7328
$($token_type:expr),+
7429
;
@@ -80,8 +35,6 @@ macro_rules! any_token_of {
8035
;
8136
symbols = $($symbol),+
8237
;
83-
ws = false
84-
;
8538
mode = $crate::tokens::MatchMode::Include
8639
)
8740
};
@@ -98,8 +51,6 @@ macro_rules! any_token_of {
9851
;
9952
symbols =
10053
;
101-
ws = false
102-
;
10354
mode = $match_mode
10455
)
10556
};
@@ -113,7 +64,7 @@ macro_rules! any_token_of {
11364
;
11465
symbols =
11566
;
116-
ws = false
67+
mode = $crate::tokens::MatchMode::Include
11768
)
11869
};
11970
}
@@ -127,7 +78,7 @@ macro_rules! any_symbol_of {
12778
;
12879
symbols = $($symbol),+
12980
;
130-
ws = false
81+
mode = $crate::tokens::MatchMode::Include
13182
)
13283
};
13384
}
@@ -141,8 +92,8 @@ macro_rules! any_symbol_of_ws {
14192
;
14293
symbols = $($symbol),+
14394
;
144-
ws = true
145-
)
95+
mode = $crate::tokens::MatchMode::Include
96+
).padded_by_ws()
14697
};
14798
}
14899

@@ -165,9 +116,6 @@ pub struct AnyTokenOf<P> {
165116
/// The syntax error message to return for non-fatal errors.
166117
err_msg: String,
167118

168-
/// Are leading and trailing whitespaces allowed?
169-
ws: bool,
170-
171119
match_mode: MatchMode,
172120
}
173121

@@ -184,15 +132,13 @@ impl<P> AnyTokenOf<P> {
184132
token_kinds: HashSet<TokenKind>,
185133
symbols: HashSet<char>,
186134
err_msg: String,
187-
ws: bool,
188135
match_mode: MatchMode,
189136
) -> Self {
190137
Self {
191138
parser,
192139
token_kinds,
193140
symbols,
194141
err_msg,
195-
ws,
196142
match_mode,
197143
}
198144
}
@@ -201,7 +147,6 @@ impl<P> AnyTokenOf<P> {
201147
parser: P,
202148
token_types: &[TokenType],
203149
symbols: &[char],
204-
ws: bool,
205150
match_mode: MatchMode,
206151
) -> Self {
207152
let mut token_kinds: HashSet<TokenKind> = HashSet::new();
@@ -234,7 +179,6 @@ impl<P> AnyTokenOf<P> {
234179
token_kinds,
235180
symbols.iter().copied().collect(),
236181
err_msg,
237-
ws,
238182
match_mode,
239183
)
240184
}
@@ -249,45 +193,11 @@ where
249193
type Error = ParseError;
250194

251195
fn parse(&self, input: I) -> ParseResult<I, Self::Output, Self::Error> {
252-
if self.ws {
253-
self.parse_token_padded_by_ws(input)
254-
} else {
255-
self.parse_token(input)
256-
}
196+
self.parse_token(input)
257197
}
258198
}
259199

260200
impl<P> AnyTokenOf<P> {
261-
fn parse_token_padded_by_ws<I, C>(&self, input: I) -> ParseResult<I, Token, ParseError>
262-
where
263-
I: Clone,
264-
P: Parser<I, C, Output = Token, Error = ParseError>,
265-
{
266-
let original_input = input.clone();
267-
268-
// parse either the target token or (if self.ws is true) the leading whitespace
269-
let (input, opt_token) = self.parse_leading_ws_or_token(input)?;
270-
271-
let (input, token) = match opt_token {
272-
// we already found the token
273-
Some(token) => (input, token),
274-
_ => {
275-
// we only found the leading whitespace
276-
// parse the token, and if the error is not fatal, return the original input
277-
// i.e. undo parsing the leading whitespace
278-
match self.parse_token(input) {
279-
Ok((input, token)) => Ok((input, token)),
280-
Err((false, _, err)) => Err((false, original_input, err)),
281-
Err(err) => Err(err),
282-
}?
283-
}
284-
};
285-
286-
// parse the trailing whitespace
287-
let input = self.parse_trailing_ws(input)?;
288-
Ok((input, token))
289-
}
290-
291201
/// Parses the token we're looking for.
292202
fn parse_token<I, C>(&self, input: I) -> ParseResult<I, Token, ParseError>
293203
where
@@ -309,54 +219,6 @@ impl<P> AnyTokenOf<P> {
309219
}
310220
}
311221

312-
/// Perform the first parse, which can yield either the token we're looking for
313-
/// or an optional leading whitespace.
314-
fn parse_leading_ws_or_token<I, C>(&self, input: I) -> ParseResult<I, Option<Token>, ParseError>
315-
where
316-
I: Clone,
317-
P: Parser<I, C, Output = Token, Error = ParseError>,
318-
{
319-
let original_input = input.clone();
320-
match self.parser.parse(input) {
321-
Ok((input, token)) => {
322-
if self.accept_token(&token) {
323-
// found it
324-
Ok((input, Some(token)))
325-
} else if TokenType::Whitespace.get_index() == token.kind() {
326-
// found leading whitespace
327-
Ok((input, None))
328-
} else {
329-
self.to_syntax_err(original_input)
330-
}
331-
}
332-
Err((false, _, _)) => self.to_syntax_err(original_input),
333-
Err(err) => Err(err),
334-
}
335-
}
336-
337-
/// Parse the optional trailing whitespace.
338-
/// This method returns the input from which the next parser should continue.
339-
/// If the trailing whitespace was not found, then that is the original input.
340-
fn parse_trailing_ws<I, C>(&self, input: I) -> Result<I, ParseErr<I, ParseError>>
341-
where
342-
I: Clone,
343-
P: Parser<I, C, Output = Token, Error = ParseError>,
344-
{
345-
let original_input = input.clone();
346-
match self.parser.parse(input) {
347-
Ok((input, ws_token)) => {
348-
if ws_token.kind() == TokenType::Whitespace.get_index() {
349-
// ok accept trailing whitespace
350-
Ok(input)
351-
} else {
352-
Ok(original_input)
353-
}
354-
}
355-
Err((false, _, _)) => Ok(original_input),
356-
Err(err) => Err(err),
357-
}
358-
}
359-
360222
fn accept_token(&self, token: &Token) -> bool {
361223
match self.match_mode {
362224
MatchMode::Include => self.test_token(token),

rusty_parser/src/tokens/frequently_used.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rusty_pc::{Parser, Token};
22

33
use crate::ParseError;
44
use crate::input::RcStringView;
5+
use crate::pc_specific::SpecificTrait;
56
use crate::tokens::{TokenType, any_symbol_of, any_symbol_of_ws, any_token_of};
67

78
/// Equal sign, surrounded by optional whitespace.

0 commit comments

Comments
 (0)