aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/mbe_expander.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/mbe_expander.rs')
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs63
1 files changed, 36 insertions, 27 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index 1acba86ea..c393d8487 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -5,17 +5,19 @@ use rustc_hash::FxHashMap;
5use ra_syntax::SmolStr; 5use ra_syntax::SmolStr;
6use tt::TokenId; 6use tt::TokenId;
7 7
8use crate::{MacroRulesError, Result};
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(rules: &crate::MacroRules, input: &tt::Subtree) -> Result<tt::Subtree> {
11 rules.rules.iter().find_map(|it| expand_rule(it, input)) 12 rules.rules.iter().find_map(|it| expand_rule(it, input).ok())
13 .ok_or(MacroRulesError::NoMatchingRule)
12} 14}
13 15
14fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> { 16fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree> {
15 let mut input = TtCursor::new(input); 17 let mut input = TtCursor::new(input);
16 let bindings = match_lhs(&rule.lhs, &mut input)?; 18 let bindings = match_lhs(&rule.lhs, &mut input)?;
17 if !input.is_eof() { 19 if !input.is_eof() {
18 return None; 20 return Err(MacroRulesError::UnexpectedToken);
19 } 21 }
20 expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) 22 expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
21} 23}
@@ -77,40 +79,47 @@ enum Binding {
77} 79}
78 80
79impl Bindings { 81impl Bindings {
80 fn get(&self, name: &SmolStr, nesting: &[usize]) -> Option<&tt::TokenTree> { 82 fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> {
81 let mut b = self.inner.get(name)?; 83 let mut b = self.inner.get(name).ok_or(MacroRulesError::BindingError(
84 format!("could not find binding {}", name)
85 ))?;
82 for &idx in nesting.iter() { 86 for &idx in nesting.iter() {
83 b = match b { 87 b = match b {
84 Binding::Simple(_) => break, 88 Binding::Simple(_) => break,
85 Binding::Nested(bs) => bs.get(idx)?, 89 Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError(
90 format!("could not find nested binding {}", name))
91 )?,
86 }; 92 };
87 } 93 }
88 match b { 94 match b {
89 Binding::Simple(it) => Some(it), 95 Binding::Simple(it) => Ok(it),
90 Binding::Nested(_) => None, 96 Binding::Nested(_) => Err(MacroRulesError::BindingError(
97 format!("expected simple binding, found nested binding {}", name))),
91 } 98 }
92 } 99 }
93 fn push_nested(&mut self, nested: Bindings) -> Option<()> { 100
101 fn push_nested(&mut self, nested: Bindings) -> Result<()> {
94 for (key, value) in nested.inner { 102 for (key, value) in nested.inner {
95 if !self.inner.contains_key(&key) { 103 if !self.inner.contains_key(&key) {
96 self.inner.insert(key.clone(), Binding::Nested(Vec::new())); 104 self.inner.insert(key.clone(), Binding::Nested(Vec::new()));
97 } 105 }
98 match self.inner.get_mut(&key) { 106 match self.inner.get_mut(&key) {
99 Some(Binding::Nested(it)) => it.push(value), 107 Some(Binding::Nested(it)) => it.push(value),
100 _ => return None, 108 _ => return Err(MacroRulesError::BindingError(
109 format!("nested binding for {} not found", key))),
101 } 110 }
102 } 111 }
103 Some(()) 112 Ok(())
104 } 113 }
105} 114}
106 115
107fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings> { 116fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings> {
108 let mut res = Bindings::default(); 117 let mut res = Bindings::default();
109 for pat in pattern.token_trees.iter() { 118 for pat in pattern.token_trees.iter() {
110 match pat { 119 match pat {
111 crate::TokenTree::Leaf(leaf) => match leaf { 120 crate::TokenTree::Leaf(leaf) => match leaf {
112 crate::Leaf::Var(crate::Var { text, kind }) => { 121 crate::Leaf::Var(crate::Var { text, kind }) => {
113 let kind = kind.clone()?; 122 let kind = kind.clone().ok_or(MacroRulesError::ParseError)?;
114 match kind.as_str() { 123 match kind.as_str() {
115 "ident" => { 124 "ident" => {
116 let ident = input.eat_ident()?.clone(); 125 let ident = input.eat_ident()?.clone();
@@ -119,28 +128,28 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings>
119 Binding::Simple(tt::Leaf::from(ident).into()), 128 Binding::Simple(tt::Leaf::from(ident).into()),
120 ); 129 );
121 } 130 }
122 _ => return None, 131 _ => return Err(MacroRulesError::UnexpectedToken),
123 } 132 }
124 } 133 }
125 crate::Leaf::Punct(punct) => { 134 crate::Leaf::Punct(punct) => {
126 if input.eat_punct()? != punct { 135 if input.eat_punct()? != punct {
127 return None; 136 return Err(MacroRulesError::UnexpectedToken);
128 } 137 }
129 } 138 }
130 crate::Leaf::Ident(ident) => { 139 crate::Leaf::Ident(ident) => {
131 if input.eat_ident()?.text != ident.text { 140 if input.eat_ident()?.text != ident.text {
132 return None; 141 return Err(MacroRulesError::UnexpectedToken);
133 } 142 }
134 } 143 }
135 _ => return None, 144 _ => return Err(MacroRulesError::UnexpectedToken),
136 }, 145 },
137 crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { 146 crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => {
138 while let Some(nested) = match_lhs(subtree, input) { 147 while let Ok(nested) = match_lhs(subtree, input) {
139 res.push_nested(nested)?; 148 res.push_nested(nested)?;
140 if let Some(separator) = *separator { 149 if let Some(separator) = *separator {
141 if !input.is_eof() { 150 if !input.is_eof() {
142 if input.eat_punct()?.char != separator { 151 if input.eat_punct()?.char != separator {
143 return None; 152 return Err(MacroRulesError::UnexpectedToken);
144 } 153 }
145 } 154 }
146 } 155 }
@@ -149,34 +158,34 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings>
149 _ => {} 158 _ => {}
150 } 159 }
151 } 160 }
152 Some(res) 161 Ok(res)
153} 162}
154 163
155fn expand_subtree( 164fn expand_subtree(
156 template: &crate::Subtree, 165 template: &crate::Subtree,
157 bindings: &Bindings, 166 bindings: &Bindings,
158 nesting: &mut Vec<usize>, 167 nesting: &mut Vec<usize>,
159) -> Option<tt::Subtree> { 168) -> Result<tt::Subtree> {
160 let token_trees = template 169 let token_trees = template
161 .token_trees 170 .token_trees
162 .iter() 171 .iter()
163 .map(|it| expand_tt(it, bindings, nesting)) 172 .map(|it| expand_tt(it, bindings, nesting))
164 .collect::<Option<Vec<_>>>()?; 173 .collect::<Result<Vec<_>>>()?;
165 174
166 Some(tt::Subtree { token_trees, delimiter: template.delimiter }) 175 Ok(tt::Subtree { token_trees, delimiter: template.delimiter })
167} 176}
168 177
169fn expand_tt( 178fn expand_tt(
170 template: &crate::TokenTree, 179 template: &crate::TokenTree,
171 bindings: &Bindings, 180 bindings: &Bindings,
172 nesting: &mut Vec<usize>, 181 nesting: &mut Vec<usize>,
173) -> Option<tt::TokenTree> { 182) -> Result<tt::TokenTree> {
174 let res: tt::TokenTree = match template { 183 let res: tt::TokenTree = match template {
175 crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), 184 crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(),
176 crate::TokenTree::Repeat(repeat) => { 185 crate::TokenTree::Repeat(repeat) => {
177 let mut token_trees = Vec::new(); 186 let mut token_trees = Vec::new();
178 nesting.push(0); 187 nesting.push(0);
179 while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) { 188 while let Ok(t) = expand_subtree(&repeat.subtree, bindings, nesting) {
180 let idx = nesting.pop().unwrap(); 189 let idx = nesting.pop().unwrap();
181 nesting.push(idx + 1); 190 nesting.push(idx + 1);
182 token_trees.push(t.into()) 191 token_trees.push(t.into())
@@ -194,5 +203,5 @@ fn expand_tt(
194 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(), 203 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(),
195 }, 204 },
196 }; 205 };
197 Some(res) 206 Ok(res)
198} 207}