aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-04 10:58:08 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-04 10:58:08 +0000
commit5197e1664856fa4fef5a4c4dd43b6915e9fa847d (patch)
tree9ae47c46374251c049531aa0b25859cfe98a78a6
parent698aa9b3f6420351a41a3fb4819b871fec3c891c (diff)
parentc9d6efc468b2e845aba3237331ea2e02af1b8cc2 (diff)
Merge #916
916: Error handling for macros r=matklad a=detrumi Part of #720 Co-authored-by: Wilco Kusee <[email protected]>
-rw-r--r--crates/ra_mbe/src/lib.rs18
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs147
-rw-r--r--crates/ra_mbe/src/mbe_parser.rs83
-rw-r--r--crates/ra_mbe/src/tt_cursor.rs44
4 files changed, 220 insertions, 72 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 907402f5f..2c75b7b4f 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -24,6 +24,18 @@ use ra_syntax::SmolStr;
24 24
25pub use tt::{Delimiter, Punct}; 25pub use tt::{Delimiter, Punct};
26 26
27#[derive(Debug, PartialEq, Eq)]
28pub enum ParseError {
29 Expected(String),
30}
31
32#[derive(Debug, PartialEq, Eq)]
33pub enum ExpandError {
34 NoMatchingRule,
35 UnexpectedToken,
36 BindingError(String),
37}
38
27pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list}; 39pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list};
28 40
29/// This struct contains AST for a single `macro_rules` definition. What might 41/// This struct contains AST for a single `macro_rules` definition. What might
@@ -36,11 +48,11 @@ pub struct MacroRules {
36} 48}
37 49
38impl MacroRules { 50impl MacroRules {
39 pub fn parse(tt: &tt::Subtree) -> Option<MacroRules> { 51 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
40 mbe_parser::parse(tt) 52 mbe_parser::parse(tt)
41 } 53 }
42 pub fn expand(&self, tt: &tt::Subtree) -> Option<tt::Subtree> { 54 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
43 mbe_expander::exapnd(self, tt) 55 mbe_expander::expand(self, tt)
44 } 56 }
45} 57}
46 58
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index 1acba86ea..2dd97b665 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -5,17 +5,21 @@ use rustc_hash::FxHashMap;
5use ra_syntax::SmolStr; 5use ra_syntax::SmolStr;
6use tt::TokenId; 6use tt::TokenId;
7 7
8use crate::ExpandError;
8use crate::tt_cursor::TtCursor; 9use crate::tt_cursor::TtCursor;
9 10
10pub(crate) fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option<tt::Subtree> { 11pub(crate) fn expand(
11 rules.rules.iter().find_map(|it| expand_rule(it, input)) 12 rules: &crate::MacroRules,
13 input: &tt::Subtree,
14) -> Result<tt::Subtree, ExpandError> {
15 rules.rules.iter().find_map(|it| expand_rule(it, input).ok()).ok_or(ExpandError::NoMatchingRule)
12} 16}
13 17
14fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> { 18fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
15 let mut input = TtCursor::new(input); 19 let mut input = TtCursor::new(input);
16 let bindings = match_lhs(&rule.lhs, &mut input)?; 20 let bindings = match_lhs(&rule.lhs, &mut input)?;
17 if !input.is_eof() { 21 if !input.is_eof() {
18 return None; 22 return Err(ExpandError::UnexpectedToken);
19 } 23 }
20 expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) 24 expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
21} 25}
@@ -77,70 +81,86 @@ enum Binding {
77} 81}
78 82
79impl Bindings { 83impl Bindings {
80 fn get(&self, name: &SmolStr, nesting: &[usize]) -> Option<&tt::TokenTree> { 84 fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> {
81 let mut b = self.inner.get(name)?; 85 let mut b = self
86 .inner
87 .get(name)
88 .ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?;
82 for &idx in nesting.iter() { 89 for &idx in nesting.iter() {
83 b = match b { 90 b = match b {
84 Binding::Simple(_) => break, 91 Binding::Simple(_) => break,
85 Binding::Nested(bs) => bs.get(idx)?, 92 Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!(
93 "could not find nested binding `{}`",
94 name
95 )))?,
86 }; 96 };
87 } 97 }
88 match b { 98 match b {
89 Binding::Simple(it) => Some(it), 99 Binding::Simple(it) => Ok(it),
90 Binding::Nested(_) => None, 100 Binding::Nested(_) => Err(ExpandError::BindingError(format!(
101 "expected simple binding, found nested binding `{}`",
102 name
103 ))),
91 } 104 }
92 } 105 }
93 fn push_nested(&mut self, nested: Bindings) -> Option<()> { 106
107 fn push_nested(&mut self, nested: Bindings) -> Result<(), ExpandError> {
94 for (key, value) in nested.inner { 108 for (key, value) in nested.inner {
95 if !self.inner.contains_key(&key) { 109 if !self.inner.contains_key(&key) {
96 self.inner.insert(key.clone(), Binding::Nested(Vec::new())); 110 self.inner.insert(key.clone(), Binding::Nested(Vec::new()));
97 } 111 }
98 match self.inner.get_mut(&key) { 112 match self.inner.get_mut(&key) {
99 Some(Binding::Nested(it)) => it.push(value), 113 Some(Binding::Nested(it)) => it.push(value),
100 _ => return None, 114 _ => {
115 return Err(ExpandError::BindingError(format!(
116 "could not find binding `{}`",
117 key
118 )));
119 }
101 } 120 }
102 } 121 }
103 Some(()) 122 Ok(())
104 } 123 }
105} 124}
106 125
107fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings> { 126fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, ExpandError> {
108 let mut res = Bindings::default(); 127 let mut res = Bindings::default();
109 for pat in pattern.token_trees.iter() { 128 for pat in pattern.token_trees.iter() {
110 match pat { 129 match pat {
111 crate::TokenTree::Leaf(leaf) => match leaf { 130 crate::TokenTree::Leaf(leaf) => match leaf {
112 crate::Leaf::Var(crate::Var { text, kind }) => { 131 crate::Leaf::Var(crate::Var { text, kind }) => {
113 let kind = kind.clone()?; 132 let kind = kind.clone().ok_or(ExpandError::UnexpectedToken)?;
114 match kind.as_str() { 133 match kind.as_str() {
115 "ident" => { 134 "ident" => {
116 let ident = input.eat_ident()?.clone(); 135 let ident =
136 input.eat_ident().ok_or(ExpandError::UnexpectedToken)?.clone();
117 res.inner.insert( 137 res.inner.insert(
118 text.clone(), 138 text.clone(),
119 Binding::Simple(tt::Leaf::from(ident).into()), 139 Binding::Simple(tt::Leaf::from(ident).into()),
120 ); 140 );
121 } 141 }
122 _ => return None, 142 _ => return Err(ExpandError::UnexpectedToken),
123 } 143 }
124 } 144 }
125 crate::Leaf::Punct(punct) => { 145 crate::Leaf::Punct(punct) => {
126 if input.eat_punct()? != punct { 146 if input.eat_punct() != Some(punct) {
127 return None; 147 return Err(ExpandError::UnexpectedToken);
128 } 148 }
129 } 149 }
130 crate::Leaf::Ident(ident) => { 150 crate::Leaf::Ident(ident) => {
131 if input.eat_ident()?.text != ident.text { 151 if input.eat_ident().map(|i| &i.text) != Some(&ident.text) {
132 return None; 152 return Err(ExpandError::UnexpectedToken);
133 } 153 }
134 } 154 }
135 _ => return None, 155 _ => return Err(ExpandError::UnexpectedToken),
136 }, 156 },
137 crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { 157 crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => {
138 while let Some(nested) = match_lhs(subtree, input) { 158 while let Ok(nested) = match_lhs(subtree, input) {
139 res.push_nested(nested)?; 159 res.push_nested(nested)?;
140 if let Some(separator) = *separator { 160 if let Some(separator) = *separator {
141 if !input.is_eof() { 161 if !input.is_eof() {
142 if input.eat_punct()?.char != separator { 162 if input.eat_punct().map(|p| p.char) != Some(separator) {
143 return None; 163 return Err(ExpandError::UnexpectedToken);
144 } 164 }
145 } 165 }
146 } 166 }
@@ -149,34 +169,34 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings>
149 _ => {} 169 _ => {}
150 } 170 }
151 } 171 }
152 Some(res) 172 Ok(res)
153} 173}
154 174
155fn expand_subtree( 175fn expand_subtree(
156 template: &crate::Subtree, 176 template: &crate::Subtree,
157 bindings: &Bindings, 177 bindings: &Bindings,
158 nesting: &mut Vec<usize>, 178 nesting: &mut Vec<usize>,
159) -> Option<tt::Subtree> { 179) -> Result<tt::Subtree, ExpandError> {
160 let token_trees = template 180 let token_trees = template
161 .token_trees 181 .token_trees
162 .iter() 182 .iter()
163 .map(|it| expand_tt(it, bindings, nesting)) 183 .map(|it| expand_tt(it, bindings, nesting))
164 .collect::<Option<Vec<_>>>()?; 184 .collect::<Result<Vec<_>, ExpandError>>()?;
165 185
166 Some(tt::Subtree { token_trees, delimiter: template.delimiter }) 186 Ok(tt::Subtree { token_trees, delimiter: template.delimiter })
167} 187}
168 188
169fn expand_tt( 189fn expand_tt(
170 template: &crate::TokenTree, 190 template: &crate::TokenTree,
171 bindings: &Bindings, 191 bindings: &Bindings,
172 nesting: &mut Vec<usize>, 192 nesting: &mut Vec<usize>,
173) -> Option<tt::TokenTree> { 193) -> Result<tt::TokenTree, ExpandError> {
174 let res: tt::TokenTree = match template { 194 let res: tt::TokenTree = match template {
175 crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), 195 crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
176 crate::TokenTree::Repeat(repeat) => { 196 crate::TokenTree::Repeat(repeat) => {
177 let mut token_trees = Vec::new(); 197 let mut token_trees = Vec::new();
178 nesting.push(0); 198 nesting.push(0);
179 while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) { 199 while let Ok(t) = expand_subtree(&repeat.subtree, bindings, nesting) {
180 let idx = nesting.pop().unwrap(); 200 let idx = nesting.pop().unwrap();
181 nesting.push(idx + 1); 201 nesting.push(idx + 1);
182 token_trees.push(t.into()) 202 token_trees.push(t.into())
@@ -194,5 +214,70 @@ fn expand_tt(
194 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(), 214 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(),
195 }, 215 },
196 }; 216 };
197 Some(res) 217 Ok(res)
218}
219
220#[cfg(test)]
221mod tests {
222 use ra_syntax::{ast, AstNode};
223
224 use super::*;
225 use crate::ast_to_token_tree;
226
227 #[test]
228 fn test_expand_rule() {
229 assert_err(
230 "($i:ident) => ($j)",
231 "foo!{a}",
232 ExpandError::BindingError(String::from("could not find binding `j`")),
233 );
234
235 assert_err(
236 "($($i:ident);*) => ($i)",
237 "foo!{a}",
238 ExpandError::BindingError(String::from(
239 "expected simple binding, found nested binding `i`",
240 )),
241 );
242
243 assert_err("($i) => ($i)", "foo!{a}", ExpandError::UnexpectedToken);
244 assert_err("($i:) => ($i)", "foo!{a}", ExpandError::UnexpectedToken);
245 }
246
247 fn assert_err(macro_body: &str, invocation: &str, err: ExpandError) {
248 assert_eq!(expand_first(&create_rules(&format_macro(macro_body)), invocation), Err(err));
249 }
250
251 fn format_macro(macro_body: &str) -> String {
252 format!(
253 "
254 macro_rules! foo {{
255 {}
256 }}
257",
258 macro_body
259 )
260 }
261
262 fn create_rules(macro_definition: &str) -> crate::MacroRules {
263 let source_file = ast::SourceFile::parse(macro_definition);
264 let macro_definition =
265 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
266
267 let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
268 crate::MacroRules::parse(&definition_tt).unwrap()
269 }
270
271 fn expand_first(
272 rules: &crate::MacroRules,
273 invocation: &str,
274 ) -> Result<tt::Subtree, ExpandError> {
275 let source_file = ast::SourceFile::parse(invocation);
276 let macro_invocation =
277 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
278
279 let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
280
281 expand_rule(&rules.rules[0], &invocation_tt)
282 }
198} 283}
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs
index 58e2533f1..e3669f66c 100644
--- a/crates/ra_mbe/src/mbe_parser.rs
+++ b/crates/ra_mbe/src/mbe_parser.rs
@@ -1,33 +1,34 @@
1/// This module parses a raw `tt::TokenStream` into macro-by-example token 1/// This module parses a raw `tt::TokenStream` into macro-by-example token
2/// stream. This is a *mostly* identify function, expect for handling of 2/// stream. This is a *mostly* identify function, expect for handling of
3/// `$var:tt_kind` and `$(repeat),*` constructs. 3/// `$var:tt_kind` and `$(repeat),*` constructs.
4use crate::ParseError;
4use crate::tt_cursor::TtCursor; 5use crate::tt_cursor::TtCursor;
5 6
6pub(crate) fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> { 7pub(crate) fn parse(tt: &tt::Subtree) -> Result<crate::MacroRules, ParseError> {
7 let mut parser = TtCursor::new(tt); 8 let mut parser = TtCursor::new(tt);
8 let mut rules = Vec::new(); 9 let mut rules = Vec::new();
9 while !parser.is_eof() { 10 while !parser.is_eof() {
10 rules.push(parse_rule(&mut parser)?); 11 rules.push(parse_rule(&mut parser)?);
11 if parser.expect_char(';') == None { 12 if let Err(e) = parser.expect_char(';') {
12 if !parser.is_eof() { 13 if !parser.is_eof() {
13 return None; 14 return Err(e);
14 } 15 }
15 break; 16 break;
16 } 17 }
17 } 18 }
18 Some(crate::MacroRules { rules }) 19 Ok(crate::MacroRules { rules })
19} 20}
20 21
21fn parse_rule(p: &mut TtCursor) -> Option<crate::Rule> { 22fn parse_rule(p: &mut TtCursor) -> Result<crate::Rule, ParseError> {
22 let lhs = parse_subtree(p.eat_subtree()?)?; 23 let lhs = parse_subtree(p.eat_subtree()?)?;
23 p.expect_char('=')?; 24 p.expect_char('=')?;
24 p.expect_char('>')?; 25 p.expect_char('>')?;
25 let mut rhs = parse_subtree(p.eat_subtree()?)?; 26 let mut rhs = parse_subtree(p.eat_subtree()?)?;
26 rhs.delimiter = crate::Delimiter::None; 27 rhs.delimiter = crate::Delimiter::None;
27 Some(crate::Rule { lhs, rhs }) 28 Ok(crate::Rule { lhs, rhs })
28} 29}
29 30
30fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> { 31fn parse_subtree(tt: &tt::Subtree) -> Result<crate::Subtree, ParseError> {
31 let mut token_trees = Vec::new(); 32 let mut token_trees = Vec::new();
32 let mut p = TtCursor::new(tt); 33 let mut p = TtCursor::new(tt);
33 while let Some(tt) = p.eat() { 34 while let Some(tt) = p.eat() {
@@ -52,10 +53,10 @@ fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> {
52 }; 53 };
53 token_trees.push(child); 54 token_trees.push(child);
54 } 55 }
55 Some(crate::Subtree { token_trees, delimiter: tt.delimiter }) 56 Ok(crate::Subtree { token_trees, delimiter: tt.delimiter })
56} 57}
57 58
58fn parse_var(p: &mut TtCursor) -> Option<crate::Var> { 59fn parse_var(p: &mut TtCursor) -> Result<crate::Var, ParseError> {
59 let ident = p.eat_ident().unwrap(); 60 let ident = p.eat_ident().unwrap();
60 let text = ident.text.clone(); 61 let text = ident.text.clone();
61 let kind = if p.at_char(':') { 62 let kind = if p.at_char(':') {
@@ -69,25 +70,77 @@ fn parse_var(p: &mut TtCursor) -> Option<crate::Var> {
69 } else { 70 } else {
70 None 71 None
71 }; 72 };
72 Some(crate::Var { text, kind }) 73 Ok(crate::Var { text, kind })
73} 74}
74 75
75fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> { 76fn parse_repeat(p: &mut TtCursor) -> Result<crate::Repeat, ParseError> {
76 let subtree = p.eat_subtree().unwrap(); 77 let subtree = p.eat_subtree().unwrap();
77 let mut subtree = parse_subtree(subtree)?; 78 let mut subtree = parse_subtree(subtree)?;
78 subtree.delimiter = crate::Delimiter::None; 79 subtree.delimiter = crate::Delimiter::None;
79 let sep = p.eat_punct()?; 80 let sep = p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?;
80 let (separator, rep) = match sep.char { 81 let (separator, rep) = match sep.char {
81 '*' | '+' | '?' => (None, sep.char), 82 '*' | '+' | '?' => (None, sep.char),
82 char => (Some(char), p.eat_punct()?.char), 83 char => {
84 (Some(char), p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?.char)
85 }
83 }; 86 };
84 87
85 let kind = match rep { 88 let kind = match rep {
86 '*' => crate::RepeatKind::ZeroOrMore, 89 '*' => crate::RepeatKind::ZeroOrMore,
87 '+' => crate::RepeatKind::OneOrMore, 90 '+' => crate::RepeatKind::OneOrMore,
88 '?' => crate::RepeatKind::ZeroOrOne, 91 '?' => crate::RepeatKind::ZeroOrOne,
89 _ => return None, 92 _ => return Err(ParseError::Expected(String::from("repeat"))),
90 }; 93 };
91 p.bump(); 94 p.bump();
92 Some(crate::Repeat { subtree, kind, separator }) 95 Ok(crate::Repeat { subtree, kind, separator })
96}
97
98#[cfg(test)]
99mod tests {
100 use ra_syntax::{ast, AstNode};
101
102 use super::*;
103 use crate::ast_to_token_tree;
104
105 #[test]
106 fn test_invalid_parse() {
107 expect_err("invalid", "subtree");
108
109 is_valid("($i:ident) => ()");
110 expect_err("$i:ident => ()", "subtree");
111 expect_err("($i:ident) ()", "`=`");
112 expect_err("($($i:ident)_) => ()", "separator");
113 }
114
115 fn expect_err(macro_body: &str, expected: &str) {
116 assert_eq!(
117 create_rules(&format_macro(macro_body)),
118 Err(ParseError::Expected(String::from(expected)))
119 );
120 }
121
122 fn is_valid(macro_body: &str) {
123 assert!(create_rules(&format_macro(macro_body)).is_ok());
124 }
125
126 fn format_macro(macro_body: &str) -> String {
127 format!(
128 "
129 macro_rules! foo {{
130 {}
131 }}
132",
133 macro_body
134 )
135 }
136
137 fn create_rules(macro_definition: &str) -> Result<crate::MacroRules, ParseError> {
138 let source_file = ast::SourceFile::parse(macro_definition);
139 let macro_definition =
140 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
141
142 let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
143 parse(&definition_tt)
144 }
145
93} 146}
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}