Skip to content

Commit bc81dca

Browse files
bors[bot]Veykril
andauthored
Merge #11159
11159: minor: Simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 8da5f46 + a0e0e45 commit bc81dca

File tree

4 files changed

+47
-59
lines changed

4 files changed

+47
-59
lines changed

crates/mbe/src/lib.rs

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ use crate::{
2727
pub use ::parser::TopEntryPoint;
2828
pub use tt::{Delimiter, DelimiterKind, Punct};
2929

30+
pub use crate::{
31+
syntax_bridge::{
32+
parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
33+
syntax_node_to_token_tree_censored, token_tree_to_syntax_node,
34+
},
35+
token_map::TokenMap,
36+
};
37+
3038
#[derive(Debug, PartialEq, Eq, Clone)]
3139
pub enum ParseError {
3240
UnexpectedToken(String),
@@ -70,14 +78,6 @@ impl fmt::Display for ExpandError {
7078
}
7179
}
7280

73-
pub use crate::{
74-
syntax_bridge::{
75-
parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
76-
syntax_node_to_token_tree_censored, token_tree_to_syntax_node,
77-
},
78-
token_map::TokenMap,
79-
};
80-
8181
/// This struct contains AST for a single `macro_rules` definition. What might
8282
/// be very confusing is that AST has almost exactly the same shape as
8383
/// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
@@ -121,11 +121,9 @@ impl Shift {
121121
}
122122
}
123123
tt::TokenTree::Leaf(leaf) => {
124-
let id = match leaf {
125-
tt::Leaf::Literal(it) => it.id,
126-
tt::Leaf::Punct(it) => it.id,
127-
tt::Leaf::Ident(it) => it.id,
128-
};
124+
let &(tt::Leaf::Ident(tt::Ident { id, .. })
125+
| tt::Leaf::Punct(tt::Punct { id, .. })
126+
| tt::Leaf::Literal(tt::Literal { id, .. })) = leaf;
129127

130128
(id != tt::TokenId::unspecified()).then(|| id.0)
131129
}
@@ -138,15 +136,15 @@ impl Shift {
138136
pub fn shift_all(self, tt: &mut tt::Subtree) {
139137
for t in &mut tt.token_trees {
140138
match t {
141-
tt::TokenTree::Leaf(leaf) => match leaf {
142-
tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id),
143-
tt::Leaf::Punct(punct) => punct.id = self.shift(punct.id),
144-
tt::Leaf::Literal(lit) => lit.id = self.shift(lit.id),
145-
},
139+
tt::TokenTree::Leaf(
140+
tt::Leaf::Ident(tt::Ident { id, .. })
141+
| tt::Leaf::Punct(tt::Punct { id, .. })
142+
| tt::Leaf::Literal(tt::Literal { id, .. }),
143+
) => *id = self.shift(*id),
146144
tt::TokenTree::Subtree(tt) => {
147145
if let Some(it) = tt.delimiter.as_mut() {
148146
it.id = self.shift(it.id);
149-
};
147+
}
150148
self.shift_all(tt)
151149
}
152150
}
@@ -155,9 +153,10 @@ impl Shift {
155153

156154
pub fn shift(self, id: tt::TokenId) -> tt::TokenId {
157155
if id == tt::TokenId::unspecified() {
158-
return id;
156+
id
157+
} else {
158+
tt::TokenId(id.0 + self.0)
159159
}
160-
tt::TokenId(id.0 + self.0)
161160
}
162161

163162
pub fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
@@ -190,8 +189,8 @@ impl DeclarativeMacro {
190189
}
191190
}
192191

193-
for rule in &rules {
194-
validate(&rule.lhs)?;
192+
for Rule { lhs, .. } in &rules {
193+
validate(lhs)?;
195194
}
196195

197196
Ok(DeclarativeMacro { rules, shift: Shift::new(tt) })
@@ -220,12 +219,13 @@ impl DeclarativeMacro {
220219
cov_mark::hit!(parse_macro_def_simple);
221220
let rule = Rule::parse(&mut src, false)?;
222221
if src.len() != 0 {
223-
return Err(ParseError::Expected("remain tokens in macro def".to_string()));
222+
return Err(ParseError::Expected("remaining tokens in macro def".to_string()));
224223
}
225224
rules.push(rule);
226225
}
227-
for rule in &rules {
228-
validate(&rule.lhs)?;
226+
227+
for Rule { lhs, .. } in &rules {
228+
validate(lhs)?;
229229
}
230230

231231
Ok(DeclarativeMacro { rules, shift: Shift::new(tt) })
@@ -281,28 +281,18 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
281281
Op::Repeat { tokens: subtree, separator, .. } => {
282282
// Checks that no repetition which could match an empty token
283283
// https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558
284-
285-
if separator.is_none()
286-
&& subtree.iter().all(|child_op| {
287-
match child_op {
288-
Op::Var { kind, .. } => {
289-
// vis is optional
290-
if kind.as_ref().map_or(false, |it| it == "vis") {
291-
return true;
292-
}
293-
}
294-
Op::Repeat { kind, .. } => {
295-
return matches!(
296-
kind,
297-
parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne
298-
)
299-
}
300-
Op::Leaf(_) => {}
301-
Op::Subtree { .. } => {}
302-
}
303-
false
304-
})
305-
{
284+
let lsh_is_empty_seq = separator.is_none() && subtree.iter().all(|child_op| {
285+
match child_op {
286+
// vis is optional
287+
Op::Var { kind: Some(kind), .. } => kind == "vis",
288+
Op::Repeat {
289+
kind: parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne,
290+
..
291+
} => true,
292+
_ => false,
293+
}
294+
});
295+
if lsh_is_empty_seq {
306296
return Err(ParseError::RepetitionEmptyTokenTree);
307297
}
308298
validate(subtree)?

crates/mbe/src/parser.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ impl PartialEq for Separator {
7676
use Separator::*;
7777

7878
match (self, other) {
79-
(Ident(ref a), Ident(ref b)) => a.text == b.text,
80-
(Literal(ref a), Literal(ref b)) => a.text == b.text,
81-
(Puncts(ref a), Puncts(ref b)) if a.len() == b.len() => {
79+
(Ident(a), Ident(b)) => a.text == b.text,
80+
(Literal(a), Literal(b)) => a.text == b.text,
81+
(Puncts(a), Puncts(b)) if a.len() == b.len() => {
8282
let a_iter = a.iter().map(|a| a.char);
8383
let b_iter = b.iter().map(|b| b.char);
8484
a_iter.eq(b_iter)
@@ -131,9 +131,7 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul
131131
Op::Repeat { tokens, separator, kind }
132132
}
133133
tt::TokenTree::Leaf(leaf) => match leaf {
134-
tt::Leaf::Punct(_) => {
135-
return Err(ParseError::Expected("ident".to_string()));
136-
}
134+
tt::Leaf::Punct(_) => return Err(ParseError::Expected("ident".to_string())),
137135
tt::Leaf::Ident(ident) if ident.text == "crate" => {
138136
// We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path.
139137
Op::Leaf(tt::Leaf::from(tt::Ident { text: "$crate".into(), id: ident.id }))

crates/mbe/src/syntax_bridge.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn parse_to_token_tree(text: &str) -> Option<(tt::Subtree, TokenMap)> {
8181
}
8282

8383
let mut conv = RawConvertor {
84-
lexed: lexed,
84+
lexed,
8585
pos: 0,
8686
id_alloc: TokenIdAlloc {
8787
map: Default::default(),
@@ -147,8 +147,8 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
147147
let entry = stack.last_mut().unwrap();
148148
let result = &mut entry.subtree.token_trees;
149149
let (token, range) = match conv.bump() {
150-
None => break,
151150
Some(it) => it,
151+
None => break,
152152
};
153153

154154
let k: SyntaxKind = token.kind(&conv);

crates/mbe/src/tt_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! A "Parser" structure for token trees. We use this when parsing a declarative
22
//! macro definition into a list of patterns and templates.
33
4-
use crate::{to_parser_input::to_parser_input, ExpandError, ExpandResult};
5-
64
use syntax::SyntaxKind;
75
use tt::buffer::TokenBuffer;
86

7+
use crate::{to_parser_input::to_parser_input, ExpandError, ExpandResult};
8+
99
macro_rules! err {
1010
() => {
1111
ExpandError::BindingError(format!(""))
@@ -27,7 +27,7 @@ impl<'a> TtIter<'a> {
2727

2828
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ()> {
2929
match self.next() {
30-
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. }))) if *c == char => {
30+
Some(&tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. }))) if c == char => {
3131
Ok(())
3232
}
3333
_ => Err(()),

0 commit comments

Comments
 (0)