Extract multi-character punct handling into a method

This commit is contained in:
Ryo Yoshida 2022-12-24 17:57:50 +09:00
parent 2872e05589
commit 47c6c8e2f3
No known key found for this signature in database
GPG key ID: E25698A930586171
2 changed files with 53 additions and 43 deletions

View file

@ -1,6 +1,7 @@
//! A "Parser" structure for token trees. We use this when parsing a declarative
//! macro definition into a list of patterns and templates.
use smallvec::{smallvec, SmallVec};
use syntax::SyntaxKind;
use tt::buffer::TokenBuffer;
@ -80,13 +81,53 @@ impl<'a> TtIter<'a> {
}
}
pub(crate) fn expect_punct(&mut self) -> Result<&'a tt::Punct, ()> {
pub(crate) fn expect_single_punct(&mut self) -> Result<&'a tt::Punct, ()> {
match self.expect_leaf()? {
tt::Leaf::Punct(it) => Ok(it),
_ => Err(()),
}
}
pub(crate) fn expect_glued_punct(&mut self) -> Result<SmallVec<[tt::Punct; 3]>, ()> {
let tt::TokenTree::Leaf(tt::Leaf::Punct(first)) = self.next().ok_or(())?.clone() else {
return Err(());
};
if first.spacing == tt::Spacing::Alone {
return Ok(smallvec![first]);
}
let (second, third) = match (self.peek_n(0), self.peek_n(1)) {
(
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p2))),
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p3))),
) if p2.spacing == tt::Spacing::Joint => (p2, Some(p3)),
(Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p2))), _) => (p2, None),
_ => return Ok(smallvec![first]),
};
match (first.char, second.char, third.map(|it| it.char)) {
('.', '.', Some('.' | '=')) | ('<', '<', Some('=')) | ('>', '>', Some('=')) => {
let puncts = smallvec![first, second.clone(), third.unwrap().clone()];
let _ = self.next().unwrap();
let _ = self.next().unwrap();
Ok(puncts)
}
('-' | '!' | '*' | '/' | '&' | '%' | '^' | '+' | '<' | '=' | '>' | '|', '=', _)
| ('-' | '=' | '>', '>', _)
| (':', ':', _)
| ('.', '.', _)
| ('&', '&', _)
| ('<', '<', _)
| ('|', '|', _) => {
let puncts = smallvec![first, second.clone()];
let _ = self.next().unwrap();
Ok(puncts)
}
_ => Ok(smallvec![first]),
}
}
pub(crate) fn expect_fragment(
&mut self,
entry_point: parser::PrefixEntryPoint,