Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 47c6c8e

Browse files
committed
Extract multi-character punct handling into a method
1 parent 2872e05 commit 47c6c8e

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

crates/mbe/src/expander/matcher.rs

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ impl<'a> TtIter<'a> {
837837
},
838838
Err(_) => false,
839839
},
840-
Separator::Puncts(lhss) if idx < lhss.len() => match fork.expect_punct() {
840+
Separator::Puncts(lhss) if idx < lhss.len() => match fork.expect_single_punct() {
841841
Ok(rhs) => rhs.char == lhss[idx].char,
842842
Err(_) => false,
843843
},
@@ -850,52 +850,21 @@ impl<'a> TtIter<'a> {
850850
}
851851

852852
fn expect_tt(&mut self) -> Result<tt::TokenTree, ()> {
853-
match self.peek_n(0) {
854-
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '\'' => {
855-
return self.expect_lifetime();
856-
}
857-
_ => (),
858-
}
859-
860-
let tt = self.next().ok_or(())?.clone();
861-
let punct = match tt {
862-
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if punct.spacing == tt::Spacing::Joint => {
863-
punct
864-
}
865-
_ => return Ok(tt),
866-
};
867-
868-
let (second, third) = match (self.peek_n(0), self.peek_n(1)) {
869-
(
870-
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p2))),
871-
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p3))),
872-
) if p2.spacing == tt::Spacing::Joint => (p2.char, Some(p3.char)),
873-
(Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p2))), _) => (p2.char, None),
874-
_ => return Ok(tt),
875-
};
876-
877-
match (punct.char, second, third) {
878-
('.', '.', Some('.' | '=')) | ('<', '<', Some('=')) | ('>', '>', Some('=')) => {
879-
let tt2 = self.next().unwrap().clone();
880-
let tt3 = self.next().unwrap().clone();
881-
Ok(tt::Subtree { delimiter: None, token_trees: vec![tt, tt2, tt3] }.into())
882-
}
883-
('-' | '!' | '*' | '/' | '&' | '%' | '^' | '+' | '<' | '=' | '>' | '|', '=', _)
884-
| ('-' | '=' | '>', '>', _)
885-
| (':', ':', _)
886-
| ('.', '.', _)
887-
| ('&', '&', _)
888-
| ('<', '<', _)
889-
| ('|', '|', _) => {
890-
let tt2 = self.next().unwrap().clone();
891-
Ok(tt::Subtree { delimiter: None, token_trees: vec![tt, tt2] }.into())
853+
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = self.peek_n(0) {
854+
if punct.char == '\'' {
855+
self.expect_lifetime()
856+
} else {
857+
let puncts = self.expect_glued_punct()?;
858+
let token_trees = puncts.into_iter().map(|p| tt::Leaf::Punct(p).into()).collect();
859+
Ok(tt::TokenTree::Subtree(tt::Subtree { delimiter: None, token_trees }))
892860
}
893-
_ => Ok(tt),
861+
} else {
862+
self.next().ok_or(()).cloned()
894863
}
895864
}
896865

897866
fn expect_lifetime(&mut self) -> Result<tt::TokenTree, ()> {
898-
let punct = self.expect_punct()?;
867+
let punct = self.expect_single_punct()?;
899868
if punct.char != '\'' {
900869
return Err(());
901870
}

crates/mbe/src/tt_iter.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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 smallvec::{smallvec, SmallVec};
45
use syntax::SyntaxKind;
56
use tt::buffer::TokenBuffer;
67

@@ -80,13 +81,53 @@ impl<'a> TtIter<'a> {
8081
}
8182
}
8283

83-
pub(crate) fn expect_punct(&mut self) -> Result<&'a tt::Punct, ()> {
84+
pub(crate) fn expect_single_punct(&mut self) -> Result<&'a tt::Punct, ()> {
8485
match self.expect_leaf()? {
8586
tt::Leaf::Punct(it) => Ok(it),
8687
_ => Err(()),
8788
}
8889
}
8990

91+
pub(crate) fn expect_glued_punct(&mut self) -> Result<SmallVec<[tt::Punct; 3]>, ()> {
92+
let tt::TokenTree::Leaf(tt::Leaf::Punct(first)) = self.next().ok_or(())?.clone() else {
93+
return Err(());
94+
};
95+
96+
if first.spacing == tt::Spacing::Alone {
97+
return Ok(smallvec![first]);
98+
}
99+
100+
let (second, third) = match (self.peek_n(0), self.peek_n(1)) {
101+
(
102+
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p2))),
103+
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p3))),
104+
) if p2.spacing == tt::Spacing::Joint => (p2, Some(p3)),
105+
(Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p2))), _) => (p2, None),
106+
_ => return Ok(smallvec![first]),
107+
};
108+
109+
match (first.char, second.char, third.map(|it| it.char)) {
110+
('.', '.', Some('.' | '=')) | ('<', '<', Some('=')) | ('>', '>', Some('=')) => {
111+
let puncts = smallvec![first, second.clone(), third.unwrap().clone()];
112+
let _ = self.next().unwrap();
113+
let _ = self.next().unwrap();
114+
Ok(puncts)
115+
}
116+
('-' | '!' | '*' | '/' | '&' | '%' | '^' | '+' | '<' | '=' | '>' | '|', '=', _)
117+
| ('-' | '=' | '>', '>', _)
118+
| (':', ':', _)
119+
| ('.', '.', _)
120+
| ('&', '&', _)
121+
| ('<', '<', _)
122+
| ('|', '|', _) => {
123+
let puncts = smallvec![first, second.clone()];
124+
let _ = self.next().unwrap();
125+
Ok(puncts)
126+
}
127+
_ => Ok(smallvec![first]),
128+
}
129+
}
130+
90131
pub(crate) fn expect_fragment(
91132
&mut self,
92133
entry_point: parser::PrefixEntryPoint,

0 commit comments

Comments
 (0)