Skip to content

Commit 9bf667f

Browse files
committed
Rewrite group parser without macros
1 parent 0fa76ab commit 9bf667f

File tree

2 files changed

+21
-69
lines changed

2 files changed

+21
-69
lines changed

src/fallback.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -895,28 +895,27 @@ named!(token_kind -> TokenTree, alt!(
895895
symbol
896896
));
897897

898-
named!(group -> Group, alt!(
899-
do_parse!(
900-
punct!("(") >>
901-
ts: token_stream >>
902-
punct!(")") >>
903-
(Group::new(Delimiter::Parenthesis, ts))
904-
)
905-
|
906-
do_parse!(
907-
punct!("[") >>
908-
ts: token_stream >>
909-
punct!("]") >>
910-
(Group::new(Delimiter::Bracket, ts))
911-
)
912-
|
913-
do_parse!(
914-
punct!("{") >>
915-
ts: token_stream >>
916-
punct!("}") >>
917-
(Group::new(Delimiter::Brace, ts))
918-
)
919-
));
898+
fn group(input: Cursor) -> PResult<Group> {
899+
let input = skip_whitespace(input);
900+
if let Ok(input) = input.expect("(") {
901+
let (input, ts) = token_stream(input)?;
902+
let input = skip_whitespace(input);
903+
let input = input.expect(")")?;
904+
Ok((input, Group::new(Delimiter::Parenthesis, ts)))
905+
} else if let Ok(input) = input.expect("[") {
906+
let (input, ts) = token_stream(input)?;
907+
let input = skip_whitespace(input);
908+
let input = input.expect("]")?;
909+
Ok((input, Group::new(Delimiter::Bracket, ts)))
910+
} else if let Ok(input) = input.expect("{") {
911+
let (input, ts) = token_stream(input)?;
912+
let input = skip_whitespace(input);
913+
let input = input.expect("}")?;
914+
Ok((input, Group::new(Delimiter::Brace, ts)))
915+
} else {
916+
Err(LexError)
917+
}
918+
}
920919

921920
fn symbol(input: Cursor) -> PResult<TokenTree> {
922921
let raw = input.starts_with("r#");

src/strnom.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -214,59 +214,12 @@ macro_rules! alt {
214214
};
215215
}
216216

217-
macro_rules! do_parse {
218-
($i:expr, ( $($rest:expr),* )) => {
219-
Ok(($i, ( $($rest),* )))
220-
};
221-
222-
($i:expr, $e:ident >> $($rest:tt)*) => {
223-
do_parse!($i, call!($e) >> $($rest)*)
224-
};
225-
226-
($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
227-
match $submac!($i, $($args)*) {
228-
Err(LexError) => Err(LexError),
229-
Ok((i, _)) => do_parse!(i, $($rest)*),
230-
}
231-
};
232-
233-
($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
234-
do_parse!($i, $field: call!($e) >> $($rest)*)
235-
};
236-
237-
($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
238-
match $submac!($i, $($args)*) {
239-
Err(LexError) => Err(LexError),
240-
Ok((i, o)) => {
241-
let $field = o;
242-
do_parse!(i, $($rest)*)
243-
},
244-
}
245-
};
246-
}
247-
248217
macro_rules! call {
249218
($i:expr, $fun:expr $(, $args:expr)*) => {
250219
$fun($i $(, $args)*)
251220
};
252221
}
253222

254-
macro_rules! punct {
255-
($i:expr, $punct:expr) => {
256-
$crate::strnom::punct($i, $punct)
257-
};
258-
}
259-
260-
/// Do not use directly. Use `punct!`.
261-
pub(crate) fn punct<'a>(input: Cursor<'a>, token: &'static str) -> PResult<'a, ()> {
262-
let input = skip_whitespace(input);
263-
if input.starts_with(token) {
264-
Ok((input.advance(token.len()), ()))
265-
} else {
266-
Err(LexError)
267-
}
268-
}
269-
270223
macro_rules! map {
271224
($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
272225
match $submac!($i, $($args)*) {

0 commit comments

Comments
 (0)