aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/tt_iter.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/tt_iter.rs')
-rw-r--r--crates/ra_mbe/src/tt_iter.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/tt_iter.rs b/crates/ra_mbe/src/tt_iter.rs
new file mode 100644
index 000000000..c53f99d1e
--- /dev/null
+++ b/crates/ra_mbe/src/tt_iter.rs
@@ -0,0 +1,67 @@
1#[derive(Debug, Clone)]
2pub(crate) struct TtIter<'a> {
3 pub(crate) inner: std::slice::Iter<'a, tt::TokenTree>,
4}
5
6impl<'a> TtIter<'a> {
7 pub(crate) fn new(subtree: &'a tt::Subtree) -> TtIter<'a> {
8 TtIter { inner: subtree.token_trees.iter() }
9 }
10
11 pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ()> {
12 match self.next() {
13 Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. }))) if *c == char => {
14 Ok(())
15 }
16 _ => Err(()),
17 }
18 }
19
20 pub(crate) fn expect_subtree(&mut self) -> Result<&'a tt::Subtree, ()> {
21 match self.next() {
22 Some(tt::TokenTree::Subtree(it)) => Ok(it),
23 _ => Err(()),
24 }
25 }
26
27 pub(crate) fn expect_leaf(&mut self) -> Result<&'a tt::Leaf, ()> {
28 match self.next() {
29 Some(tt::TokenTree::Leaf(it)) => Ok(it),
30 _ => Err(()),
31 }
32 }
33
34 pub(crate) fn expect_ident(&mut self) -> Result<&'a tt::Ident, ()> {
35 match self.expect_leaf()? {
36 tt::Leaf::Ident(it) => Ok(it),
37 _ => Err(()),
38 }
39 }
40
41 pub(crate) fn expect_literal(&mut self) -> Result<&'a tt::Literal, ()> {
42 match self.expect_leaf()? {
43 tt::Leaf::Literal(it) => Ok(it),
44 _ => Err(()),
45 }
46 }
47
48 pub(crate) fn expect_punct(&mut self) -> Result<&'a tt::Punct, ()> {
49 match self.expect_leaf()? {
50 tt::Leaf::Punct(it) => Ok(it),
51 _ => Err(()),
52 }
53 }
54}
55
56impl<'a> Iterator for TtIter<'a> {
57 type Item = &'a tt::TokenTree;
58 fn next(&mut self) -> Option<Self::Item> {
59 self.inner.next()
60 }
61
62 fn size_hint(&self) -> (usize, Option<usize>) {
63 self.inner.size_hint()
64 }
65}
66
67impl<'a> std::iter::ExactSizeIterator for TtIter<'a> {}