aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_mbe/src/lib.rs15
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs63
-rw-r--r--crates/ra_mbe/src/mbe_parser.rs33
-rw-r--r--crates/ra_mbe/src/tt_cursor.rs56
4 files changed, 93 insertions, 74 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 907402f5f..8a2d6ff63 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -24,6 +24,15 @@ 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 MacroRulesError {
29 NoMatchingRule,
30 UnexpectedToken,
31 BindingError(String),
32 ParseError,
33}
34
35pub type Result<T> = ::std::result::Result<T, MacroRulesError>;
27pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list}; 36pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list};
28 37
29/// This struct contains AST for a single `macro_rules` definition. What might 38/// This struct contains AST for a single `macro_rules` definition. What might
@@ -36,11 +45,11 @@ pub struct MacroRules {
36} 45}
37 46
38impl MacroRules { 47impl MacroRules {
39 pub fn parse(tt: &tt::Subtree) -> Option<MacroRules> { 48 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules> {
40 mbe_parser::parse(tt) 49 mbe_parser::parse(tt)
41 } 50 }
42 pub fn expand(&self, tt: &tt::Subtree) -> Option<tt::Subtree> { 51 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree> {
43 mbe_expander::exapnd(self, tt) 52 mbe_expander::expand(self, tt)
44 } 53 }
45} 54}
46 55
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}
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs
index 58e2533f1..ee1b11091 100644
--- a/crates/ra_mbe/src/mbe_parser.rs
+++ b/crates/ra_mbe/src/mbe_parser.rs
@@ -1,40 +1,41 @@
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::{MacroRulesError, Result};
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> {
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> {
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> {
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 Ok(tt) = p.eat() {
34 let child: crate::TokenTree = match tt { 35 let child: crate::TokenTree = match tt {
35 tt::TokenTree::Leaf(leaf) => match leaf { 36 tt::TokenTree::Leaf(leaf) => match leaf {
36 tt::Leaf::Punct(tt::Punct { char: '$', .. }) => { 37 tt::Leaf::Punct(tt::Punct { char: '$', .. }) => {
37 if p.at_ident().is_some() { 38 if p.at_ident().is_ok() {
38 crate::Leaf::from(parse_var(&mut p)?).into() 39 crate::Leaf::from(parse_var(&mut p)?).into()
39 } else { 40 } else {
40 parse_repeat(&mut p)?.into() 41 parse_repeat(&mut p)?.into()
@@ -52,15 +53,15 @@ 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> {
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(':') {
62 p.bump(); 63 p.bump();
63 if let Some(ident) = p.eat_ident() { 64 if let Ok(ident) = p.eat_ident() {
64 Some(ident.text.clone()) 65 Some(ident.text.clone())
65 } else { 66 } else {
66 p.rev_bump(); 67 p.rev_bump();
@@ -69,10 +70,10 @@ 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> {
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;
@@ -86,8 +87,8 @@ fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> {
86 '*' => crate::RepeatKind::ZeroOrMore, 87 '*' => crate::RepeatKind::ZeroOrMore,
87 '+' => crate::RepeatKind::OneOrMore, 88 '+' => crate::RepeatKind::OneOrMore,
88 '?' => crate::RepeatKind::ZeroOrOne, 89 '?' => crate::RepeatKind::ZeroOrOne,
89 _ => return None, 90 _ => return Err(MacroRulesError::ParseError),
90 }; 91 };
91 p.bump(); 92 p.bump();
92 Some(crate::Repeat { subtree, kind, separator }) 93 Ok(crate::Repeat { subtree, kind, separator })
93} 94}
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}