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.rs44
1 files changed, 21 insertions, 23 deletions
diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs
index 30c8eda67..3128cb9ae 100644
--- a/crates/ra_mbe/src/tt_cursor.rs
+++ b/crates/ra_mbe/src/tt_cursor.rs
@@ -1,3 +1,5 @@
1use crate::ParseError;
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,
@@ -46,46 +48,42 @@ impl<'a> TtCursor<'a> {
46 } 48 }
47 49
48 pub(crate) fn eat(&mut self) -> Option<&'a tt::TokenTree> { 50 pub(crate) fn eat(&mut self) -> Option<&'a tt::TokenTree> {
49 match self.current() { 51 self.current().map(|it| {
50 Some(it) => { 52 self.bump();
51 self.bump(); 53 it
52 Some(it) 54 })
53 }
54 None => None,
55 }
56 } 55 }
57 56
58 pub(crate) fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> { 57 pub(crate) fn eat_subtree(&mut self) -> Result<&'a tt::Subtree, ParseError> {
59 match self.current()? { 58 match self.current() {
60 tt::TokenTree::Subtree(sub) => { 59 Some(tt::TokenTree::Subtree(sub)) => {
61 self.bump(); 60 self.bump();
62 Some(sub) 61 Ok(sub)
63 } 62 }
64 _ => return None, 63 _ => Err(ParseError::Expected(String::from("subtree"))),
65 } 64 }
66 } 65 }
67 66
68 pub(crate) fn eat_punct(&mut self) -> Option<&'a tt::Punct> { 67 pub(crate) fn eat_punct(&mut self) -> Option<&'a tt::Punct> {
69 if let Some(it) = self.at_punct() { 68 self.at_punct().map(|it| {
70 self.bump(); 69 self.bump();
71 return Some(it); 70 it
72 } 71 })
73 None
74 } 72 }
75 73
76 pub(crate) fn eat_ident(&mut self) -> Option<&'a tt::Ident> { 74 pub(crate) fn eat_ident(&mut self) -> Option<&'a tt::Ident> {
77 if let Some(i) = self.at_ident() { 75 self.at_ident().map(|i| {
78 self.bump(); 76 self.bump();
79 return Some(i); 77 i
80 } 78 })
81 None
82 } 79 }
83 80
84 pub(crate) fn expect_char(&mut self, char: char) -> Option<()> { 81 pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
85 if self.at_char(char) { 82 if self.at_char(char) {
86 self.bump(); 83 self.bump();
87 return Some(()); 84 Ok(())
85 } else {
86 Err(ParseError::Expected(format!("`{}`", char)))
88 } 87 }
89 None
90 } 88 }
91} 89}