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