aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/tt_cursor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/tt_cursor.rs')
-rw-r--r--crates/ra_mbe/src/tt_cursor.rs56
1 files changed, 28 insertions, 28 deletions
diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs
index 30c8eda67..117471841 100644
--- a/crates/ra_mbe/src/tt_cursor.rs
+++ b/crates/ra_mbe/src/tt_cursor.rs
@@ -1,3 +1,5 @@
1use crate::{MacroRulesError, Result};
2
1#[derive(Clone)] 3#[derive(Clone)]
2pub(crate) struct TtCursor<'a> { 4pub(crate) struct TtCursor<'a> {
3 subtree: &'a tt::Subtree, 5 subtree: &'a tt::Subtree,
@@ -17,24 +19,24 @@ impl<'a> TtCursor<'a> {
17 self.subtree.token_trees.get(self.pos) 19 self.subtree.token_trees.get(self.pos)
18 } 20 }
19 21
20 pub(crate) fn at_punct(&self) -> Option<&'a tt::Punct> { 22 pub(crate) fn at_punct(&self) -> Result<&'a tt::Punct> {
21 match self.current() { 23 match self.current() {
22 Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Some(it), 24 Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Ok(it),
23 _ => None, 25 _ => Err(MacroRulesError::ParseError),
24 } 26 }
25 } 27 }
26 28
27 pub(crate) fn at_char(&self, char: char) -> bool { 29 pub(crate) fn at_char(&self, char: char) -> bool {
28 match self.at_punct() { 30 match self.at_punct() {
29 Some(tt::Punct { char: c, .. }) if *c == char => true, 31 Ok(tt::Punct { char: c, .. }) if *c == char => true,
30 _ => false, 32 _ => false,
31 } 33 }
32 } 34 }
33 35
34 pub(crate) fn at_ident(&mut self) -> Option<&'a tt::Ident> { 36 pub(crate) fn at_ident(&mut self) -> Result<&'a tt::Ident> {
35 match self.current() { 37 match self.current() {
36 Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Some(i), 38 Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Ok(i),
37 _ => None, 39 _ => Err(MacroRulesError::ParseError),
38 } 40 }
39 } 41 }
40 42
@@ -45,47 +47,45 @@ impl<'a> TtCursor<'a> {
45 self.pos -= 1; 47 self.pos -= 1;
46 } 48 }
47 49
48 pub(crate) fn eat(&mut self) -> Option<&'a tt::TokenTree> { 50 pub(crate) fn eat(&mut self) -> Result<&'a tt::TokenTree> {
49 match self.current() { 51 match self.current() {
50 Some(it) => { 52 Some(it) => {
51 self.bump(); 53 self.bump();
52 Some(it) 54 Ok(it)
53 } 55 }
54 None => None, 56 None => Err(MacroRulesError::ParseError),
55 } 57 }
56 } 58 }
57 59
58 pub(crate) fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> { 60 pub(crate) fn eat_subtree(&mut self) -> Result<&'a tt::Subtree> {
59 match self.current()? { 61 match self.current() {
60 tt::TokenTree::Subtree(sub) => { 62 Some(tt::TokenTree::Subtree(sub)) => {
61 self.bump(); 63 self.bump();
62 Some(sub) 64 Ok(sub)
63 } 65 }
64 _ => return None, 66 _ => Err(MacroRulesError::ParseError),
65 } 67 }
66 } 68 }
67 69
68 pub(crate) fn eat_punct(&mut self) -> Option<&'a tt::Punct> { 70 pub(crate) fn eat_punct(&mut self) -> Result<&'a tt::Punct> {
69 if let Some(it) = self.at_punct() { 71 self.at_punct().map(|it| {
70 self.bump(); 72 self.bump();
71 return Some(it); 73 it
72 } 74 })
73 None
74 } 75 }
75 76
76 pub(crate) fn eat_ident(&mut self) -> Option<&'a tt::Ident> { 77 pub(crate) fn eat_ident(&mut self) -> Result<&'a tt::Ident> {
77 if let Some(i) = self.at_ident() { 78 self.at_ident().map(|i| {
78 self.bump(); 79 self.bump();
79 return Some(i); 80 i
80 } 81 })
81 None
82 } 82 }
83 83
84 pub(crate) fn expect_char(&mut self, char: char) -> Option<()> { 84 pub(crate) fn expect_char(&mut self, char: char) -> Result<()> {
85 if self.at_char(char) { 85 if self.at_char(char) {
86 self.bump(); 86 self.bump();
87 return Some(()); 87 return Ok(());
88 } 88 }
89 None 89 Err(MacroRulesError::ParseError)
90 } 90 }
91} 91}