aboutsummaryrefslogtreecommitdiff
path: root/crates/mbe/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/mbe/src')
-rw-r--r--crates/mbe/src/expander/matcher.rs20
-rw-r--r--crates/mbe/src/expander/transcriber.rs8
-rw-r--r--crates/mbe/src/lib.rs6
-rw-r--r--crates/mbe/src/parser.rs14
-rw-r--r--crates/mbe/src/subtree_source.rs6
-rw-r--r--crates/mbe/src/syntax_bridge.rs2
-rw-r--r--crates/mbe/src/tests/expand.rs44
-rw-r--r--crates/mbe/src/tt_iter.rs2
8 files changed, 52 insertions, 50 deletions
diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs
index 961ba3f7b..c2a9a38c9 100644
--- a/crates/mbe/src/expander/matcher.rs
+++ b/crates/mbe/src/expander/matcher.rs
@@ -121,7 +121,7 @@ impl Match {
121 121
122/// Matching errors are added to the `Match`. 122/// Matching errors are added to the `Match`.
123pub(super) fn match_(pattern: &MetaTemplate, input: &tt::Subtree) -> Match { 123pub(super) fn match_(pattern: &MetaTemplate, input: &tt::Subtree) -> Match {
124 let mut res = match_loop(pattern, &input); 124 let mut res = match_loop(pattern, input);
125 res.bound_count = count(res.bindings.bindings()); 125 res.bound_count = count(res.bindings.bindings());
126 return res; 126 return res;
127 127
@@ -202,7 +202,7 @@ impl BindingsBuilder {
202 } 202 }
203 203
204 fn push_nested(&mut self, parent: &mut BindingsIdx, child: &BindingsIdx) { 204 fn push_nested(&mut self, parent: &mut BindingsIdx, child: &BindingsIdx) {
205 let BindingsIdx(idx, nidx) = self.copy(&child); 205 let BindingsIdx(idx, nidx) = self.copy(child);
206 self.nodes[parent.0].push(LinkNode::Node(Rc::new(BindingKind::Nested(idx, nidx)))); 206 self.nodes[parent.0].push(LinkNode::Node(Rc::new(BindingKind::Nested(idx, nidx))));
207 } 207 }
208 208
@@ -221,7 +221,7 @@ impl BindingsBuilder {
221 221
222 fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode<Rc<BindingKind>>]) { 222 fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode<Rc<BindingKind>>]) {
223 let mut nodes = Vec::new(); 223 let mut nodes = Vec::new();
224 self.collect_nodes(&link_nodes, &mut nodes); 224 self.collect_nodes(link_nodes, &mut nodes);
225 225
226 for cmd in nodes { 226 for cmd in nodes {
227 match &**cmd { 227 match &**cmd {
@@ -282,7 +282,7 @@ impl BindingsBuilder {
282 282
283 nested_refs.into_iter().for_each(|iter| { 283 nested_refs.into_iter().for_each(|iter| {
284 let mut child_bindings = Bindings::default(); 284 let mut child_bindings = Bindings::default();
285 self.build_inner(&mut child_bindings, &iter); 285 self.build_inner(&mut child_bindings, iter);
286 nested.push(child_bindings) 286 nested.push(child_bindings)
287 }) 287 })
288 } 288 }
@@ -417,7 +417,7 @@ fn match_loop_inner<'t>(
417 let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count); 417 let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count);
418 if item.sep.is_some() && sep_idx != sep_len { 418 if item.sep.is_some() && sep_idx != sep_len {
419 let sep = item.sep.as_ref().unwrap(); 419 let sep = item.sep.as_ref().unwrap();
420 if src.clone().expect_separator(&sep, sep_idx) { 420 if src.clone().expect_separator(sep, sep_idx) {
421 item.dot.next(); 421 item.dot.next();
422 item.sep_parsed = Some(sep_idx + 1); 422 item.sep_parsed = Some(sep_idx + 1);
423 try_push!(next_items, item); 423 try_push!(next_items, item);
@@ -487,7 +487,7 @@ fn match_loop_inner<'t>(
487 item.meta_result = Some((fork, match_res)); 487 item.meta_result = Some((fork, match_res));
488 try_push!(bb_items, item); 488 try_push!(bb_items, item);
489 } else { 489 } else {
490 bindings_builder.push_optional(&mut item.bindings, &name); 490 bindings_builder.push_optional(&mut item.bindings, name);
491 item.dot.next(); 491 item.dot.next();
492 cur_items.push(item); 492 cur_items.push(item);
493 } 493 }
@@ -495,7 +495,7 @@ fn match_loop_inner<'t>(
495 Some(err) => { 495 Some(err) => {
496 res.add_err(err); 496 res.add_err(err);
497 if let Some(fragment) = match_res.value { 497 if let Some(fragment) = match_res.value {
498 bindings_builder.push_fragment(&mut item.bindings, &name, fragment); 498 bindings_builder.push_fragment(&mut item.bindings, name, fragment);
499 } 499 }
500 item.is_error = true; 500 item.is_error = true;
501 error_items.push(item); 501 error_items.push(item);
@@ -504,7 +504,7 @@ fn match_loop_inner<'t>(
504 } 504 }
505 } 505 }
506 OpDelimited::Op(Op::Leaf(leaf)) => { 506 OpDelimited::Op(Op::Leaf(leaf)) => {
507 if let Err(err) = match_leaf(&leaf, &mut src.clone()) { 507 if let Err(err) = match_leaf(leaf, &mut src.clone()) {
508 res.add_err(err); 508 res.add_err(err);
509 item.is_error = true; 509 item.is_error = true;
510 } else { 510 } else {
@@ -640,10 +640,10 @@ fn match_loop(pattern: &MetaTemplate, src: &tt::Subtree) -> Match {
640 let (iter, match_res) = item.meta_result.take().unwrap(); 640 let (iter, match_res) = item.meta_result.take().unwrap();
641 match match_res.value { 641 match match_res.value {
642 Some(fragment) => { 642 Some(fragment) => {
643 bindings_builder.push_fragment(&mut item.bindings, &name, fragment); 643 bindings_builder.push_fragment(&mut item.bindings, name, fragment);
644 } 644 }
645 None if match_res.err.is_none() => { 645 None if match_res.err.is_none() => {
646 bindings_builder.push_optional(&mut item.bindings, &name); 646 bindings_builder.push_optional(&mut item.bindings, name);
647 } 647 }
648 _ => {} 648 _ => {}
649 } 649 }
diff --git a/crates/mbe/src/expander/transcriber.rs b/crates/mbe/src/expander/transcriber.rs
index dd7fa97d7..49a137577 100644
--- a/crates/mbe/src/expander/transcriber.rs
+++ b/crates/mbe/src/expander/transcriber.rs
@@ -55,7 +55,7 @@ pub(super) fn transcribe(
55 template: &MetaTemplate, 55 template: &MetaTemplate,
56 bindings: &Bindings, 56 bindings: &Bindings,
57) -> ExpandResult<tt::Subtree> { 57) -> ExpandResult<tt::Subtree> {
58 let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() }; 58 let mut ctx = ExpandCtx { bindings, nesting: Vec::new() };
59 let mut arena: Vec<tt::TokenTree> = Vec::new(); 59 let mut arena: Vec<tt::TokenTree> = Vec::new();
60 expand_subtree(&mut ctx, template, None, &mut arena) 60 expand_subtree(&mut ctx, template, None, &mut arena)
61} 61}
@@ -91,12 +91,12 @@ fn expand_subtree(
91 Op::Leaf(tt) => arena.push(tt.clone().into()), 91 Op::Leaf(tt) => arena.push(tt.clone().into()),
92 Op::Subtree { tokens, delimiter } => { 92 Op::Subtree { tokens, delimiter } => {
93 let ExpandResult { value: tt, err: e } = 93 let ExpandResult { value: tt, err: e } =
94 expand_subtree(ctx, &tokens, *delimiter, arena); 94 expand_subtree(ctx, tokens, *delimiter, arena);
95 err = err.or(e); 95 err = err.or(e);
96 arena.push(tt.into()); 96 arena.push(tt.into());
97 } 97 }
98 Op::Var { name, id, .. } => { 98 Op::Var { name, id, .. } => {
99 let ExpandResult { value: fragment, err: e } = expand_var(ctx, &name, *id); 99 let ExpandResult { value: fragment, err: e } = expand_var(ctx, name, *id);
100 err = err.or(e); 100 err = err.or(e);
101 push_fragment(arena, fragment); 101 push_fragment(arena, fragment);
102 } 102 }
@@ -141,7 +141,7 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr, id: tt::TokenId) -> ExpandResult
141 .into(); 141 .into();
142 ExpandResult::ok(Fragment::Tokens(tt)) 142 ExpandResult::ok(Fragment::Tokens(tt))
143 } else { 143 } else {
144 ctx.bindings.get(&v, &mut ctx.nesting).map_or_else( 144 ctx.bindings.get(v, &mut ctx.nesting).map_or_else(
145 |e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) }, 145 |e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) },
146 |b| ExpandResult::ok(b.clone()), 146 |b| ExpandResult::ok(b.clone()),
147 ) 147 )
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs
index 380a50744..8c8528aaf 100644
--- a/crates/mbe/src/lib.rs
+++ b/crates/mbe/src/lib.rs
@@ -280,8 +280,8 @@ impl Rule {
280 .expect_subtree() 280 .expect_subtree()
281 .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; 281 .map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
282 282
283 let lhs = MetaTemplate(parse_pattern(&lhs)?); 283 let lhs = MetaTemplate(parse_pattern(lhs)?);
284 let rhs = MetaTemplate(parse_template(&rhs)?); 284 let rhs = MetaTemplate(parse_template(rhs)?);
285 285
286 Ok(crate::Rule { lhs, rhs }) 286 Ok(crate::Rule { lhs, rhs })
287 } 287 }
@@ -290,7 +290,7 @@ impl Rule {
290fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { 290fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
291 for op in pattern.iter() { 291 for op in pattern.iter() {
292 match op { 292 match op {
293 Op::Subtree { tokens, .. } => validate(&tokens)?, 293 Op::Subtree { tokens, .. } => validate(tokens)?,
294 Op::Repeat { tokens: subtree, separator, .. } => { 294 Op::Repeat { tokens: subtree, separator, .. } => {
295 // Checks that no repetition which could match an empty token 295 // Checks that no repetition which could match an empty token
296 // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558 296 // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558
diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs
index 04c0d3e75..deed884d2 100644
--- a/crates/mbe/src/parser.rs
+++ b/crates/mbe/src/parser.rs
@@ -42,7 +42,7 @@ impl<'a> OpDelimitedIter<'a> {
42 } 42 }
43 43
44 pub(crate) fn reset(&self) -> Self { 44 pub(crate) fn reset(&self) -> Self {
45 Self { inner: &self.inner, idx: 0, delimited: self.delimited } 45 Self { inner: self.inner, idx: 0, delimited: self.delimited }
46 } 46 }
47} 47}
48 48
@@ -126,11 +126,11 @@ impl Separator {
126} 126}
127 127
128pub(crate) fn parse_template(template: &tt::Subtree) -> Result<Vec<Op>, ParseError> { 128pub(crate) fn parse_template(template: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
129 parse_inner(&template, Mode::Template).into_iter().collect() 129 parse_inner(template, Mode::Template).into_iter().collect()
130} 130}
131 131
132pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<Vec<Op>, ParseError> { 132pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
133 parse_inner(&pattern, Mode::Pattern).into_iter().collect() 133 parse_inner(pattern, Mode::Pattern).into_iter().collect()
134} 134}
135 135
136#[derive(Clone, Copy)] 136#[derive(Clone, Copy)]
@@ -140,7 +140,7 @@ enum Mode {
140} 140}
141 141
142fn parse_inner(tt: &tt::Subtree, mode: Mode) -> Vec<Result<Op, ParseError>> { 142fn parse_inner(tt: &tt::Subtree, mode: Mode) -> Vec<Result<Op, ParseError>> {
143 let mut src = TtIter::new(&tt); 143 let mut src = TtIter::new(tt);
144 std::iter::from_fn(move || { 144 std::iter::from_fn(move || {
145 let first = src.next()?; 145 let first = src.next()?;
146 Some(next_op(first, &mut src, mode)) 146 Some(next_op(first, &mut src, mode))
@@ -171,7 +171,7 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul
171 match second { 171 match second {
172 tt::TokenTree::Subtree(subtree) => { 172 tt::TokenTree::Subtree(subtree) => {
173 let (separator, kind) = parse_repeat(src)?; 173 let (separator, kind) = parse_repeat(src)?;
174 let tokens = parse_inner(&subtree, mode) 174 let tokens = parse_inner(subtree, mode)
175 .into_iter() 175 .into_iter()
176 .collect::<Result<Vec<Op>, ParseError>>()?; 176 .collect::<Result<Vec<Op>, ParseError>>()?;
177 Op::Repeat { tokens: MetaTemplate(tokens), separator, kind } 177 Op::Repeat { tokens: MetaTemplate(tokens), separator, kind }
@@ -191,7 +191,7 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul
191 Op::Var { name, kind, id } 191 Op::Var { name, kind, id }
192 } 192 }
193 tt::Leaf::Literal(lit) => { 193 tt::Leaf::Literal(lit) => {
194 if is_boolean_literal(&lit) { 194 if is_boolean_literal(lit) {
195 let name = lit.text.clone(); 195 let name = lit.text.clone();
196 let kind = eat_fragment_kind(src, mode)?; 196 let kind = eat_fragment_kind(src, mode)?;
197 let id = lit.id; 197 let id = lit.id;
@@ -206,7 +206,7 @@ fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Resul
206 tt::TokenTree::Leaf(tt) => Op::Leaf(tt.clone()), 206 tt::TokenTree::Leaf(tt) => Op::Leaf(tt.clone()),
207 tt::TokenTree::Subtree(subtree) => { 207 tt::TokenTree::Subtree(subtree) => {
208 let tokens = 208 let tokens =
209 parse_inner(&subtree, mode).into_iter().collect::<Result<Vec<Op>, ParseError>>()?; 209 parse_inner(subtree, mode).into_iter().collect::<Result<Vec<Op>, ParseError>>()?;
210 Op::Subtree { tokens: MetaTemplate(tokens), delimiter: subtree.delimiter } 210 Op::Subtree { tokens: MetaTemplate(tokens), delimiter: subtree.delimiter }
211 } 211 }
212 }; 212 };
diff --git a/crates/mbe/src/subtree_source.rs b/crates/mbe/src/subtree_source.rs
index bde370fdb..ee80807ad 100644
--- a/crates/mbe/src/subtree_source.rs
+++ b/crates/mbe/src/subtree_source.rs
@@ -22,7 +22,7 @@ impl<'a> SubtreeTokenSource {
22 #[cfg(test)] 22 #[cfg(test)]
23 pub(crate) fn text(&self) -> SmolStr { 23 pub(crate) fn text(&self) -> SmolStr {
24 match self.cached.get(self.curr.1) { 24 match self.cached.get(self.curr.1) {
25 Some(ref tt) => tt.text.clone(), 25 Some(tt) => tt.text.clone(),
26 _ => SmolStr::new(""), 26 _ => SmolStr::new(""),
27 } 27 }
28 } 28 }
@@ -59,7 +59,7 @@ impl<'a> SubtreeTokenSource {
59 59
60 current = match tt { 60 current = match tt {
61 Some(tt::buffer::TokenTreeRef::Leaf(leaf, _)) => { 61 Some(tt::buffer::TokenTreeRef::Leaf(leaf, _)) => {
62 cached.push(convert_leaf(&leaf)); 62 cached.push(convert_leaf(leaf));
63 cursor.bump() 63 cursor.bump()
64 } 64 }
65 Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) => { 65 Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) => {
@@ -114,7 +114,7 @@ impl<'a> TokenSource for SubtreeTokenSource {
114 /// Is the current token a specified keyword? 114 /// Is the current token a specified keyword?
115 fn is_keyword(&self, kw: &str) -> bool { 115 fn is_keyword(&self, kw: &str) -> bool {
116 match self.cached.get(self.curr.1) { 116 match self.cached.get(self.curr.1) {
117 Some(ref t) => t.text == *kw, 117 Some(t) => t.text == *kw,
118 _ => false, 118 _ => false,
119 } 119 }
120 } 120 }
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index 978c75747..cdc22425d 100644
--- a/crates/mbe/src/syntax_bridge.rs
+++ b/crates/mbe/src/syntax_bridge.rs
@@ -633,7 +633,7 @@ impl<'a> TreeSink for TtTreeSink<'a> {
633 } 633 }
634 } 634 }
635 }; 635 };
636 self.buf += &text; 636 self.buf += text;
637 self.text_pos += TextSize::of(text); 637 self.text_pos += TextSize::of(text);
638 } 638 }
639 639
diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs
index 5f173f513..c788e427e 100644
--- a/crates/mbe/src/tests/expand.rs
+++ b/crates/mbe/src/tests/expand.rs
@@ -490,7 +490,7 @@ [email protected]
490 490
491fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree { 491fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree {
492 if let tt::TokenTree::Subtree(subtree) = tt { 492 if let tt::TokenTree::Subtree(subtree) = tt {
493 return &subtree; 493 return subtree;
494 } 494 }
495 unreachable!("It is not a subtree"); 495 unreachable!("It is not a subtree");
496} 496}
@@ -1846,16 +1846,17 @@ fn test_no_space_after_semi_colon() {
1846 [email protected] 1846 [email protected]
1847 [email protected] "#" 1847 [email protected] "#"
1848 [email protected] "[" 1848 [email protected] "["
1849 [email protected] 1849 [email protected]
1850 [email protected] 1850 [email protected]
1851 [email protected] 1851 [email protected]
1852 [email protected] "cfg" 1852 [email protected]
1853 [email protected] 1853 [email protected] "cfg"
1854 [email protected] "(" 1854 [email protected]
1855 [email protected] "feature" 1855 [email protected] "("
1856 [email protected] "=" 1856 [email protected] "feature"
1857 [email protected] "\"std\"" 1857 [email protected] "="
1858 [email protected] ")" 1858 [email protected] "\"std\""
1859 [email protected] ")"
1859 [email protected] "]" 1860 [email protected] "]"
1860 [email protected] "mod" 1861 [email protected] "mod"
1861 [email protected] 1862 [email protected]
@@ -1865,16 +1866,17 @@ fn test_no_space_after_semi_colon() {
1865 [email protected] 1866 [email protected]
1866 [email protected] "#" 1867 [email protected] "#"
1867 [email protected] "[" 1868 [email protected] "["
1868 [email protected] 1869 [email protected]
1869 [email protected] 1870 [email protected]
1870 [email protected] 1871 [email protected]
1871 [email protected] "cfg" 1872 [email protected]
1872 [email protected] 1873 [email protected] "cfg"
1873 [email protected] "(" 1874 [email protected]
1874 [email protected] "feature" 1875 [email protected] "("
1875 [email protected] "=" 1876 [email protected] "feature"
1876 [email protected] "\"std\"" 1877 [email protected] "="
1877 [email protected] ")" 1878 [email protected] "\"std\""
1879 [email protected] ")"
1878 [email protected] "]" 1880 [email protected] "]"
1879 [email protected] "mod" 1881 [email protected] "mod"
1880 [email protected] 1882 [email protected]
diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs
index bd54f2442..5a4eca7bf 100644
--- a/crates/mbe/src/tt_iter.rs
+++ b/crates/mbe/src/tt_iter.rs
@@ -115,7 +115,7 @@ impl<'a> TtIter<'a> {
115 } 115 }
116 } 116 }
117 117
118 let buffer = TokenBuffer::from_tokens(&self.inner.as_slice()); 118 let buffer = TokenBuffer::from_tokens(self.inner.as_slice());
119 let mut src = SubtreeTokenSource::new(&buffer); 119 let mut src = SubtreeTokenSource::new(&buffer);
120 let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false }; 120 let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false };
121 121