diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-17 13:53:01 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-17 13:53:01 +0100 |
commit | 9421d2a953516b392ae35446bc4f2206dd993c84 (patch) | |
tree | e6c7b46cabe1f10f7da28f3db209df2260045fa8 /crates/ra_mbe/src/tt_iter.rs | |
parent | 8eb2697b7d2a98c952b3acd1711829a13e13cab1 (diff) | |
parent | 4551182f94fe81c314f79ddf8916a5520cfd03b0 (diff) |
Merge #1858
1858: use usual token tree for macro expansions r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_mbe/src/tt_iter.rs')
-rw-r--r-- | crates/ra_mbe/src/tt_iter.rs | 67 |
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)] | ||
2 | pub(crate) struct TtIter<'a> { | ||
3 | pub(crate) inner: std::slice::Iter<'a, tt::TokenTree>, | ||
4 | } | ||
5 | |||
6 | impl<'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 | |||
56 | impl<'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 | |||
67 | impl<'a> std::iter::ExactSizeIterator for TtIter<'a> {} | ||