mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
Extract multi-character punct handling into a method
This commit is contained in:
parent
2872e05589
commit
47c6c8e2f3
2 changed files with 53 additions and 43 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue