aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_mbe/src/lib.rs41
1 files changed, 20 insertions, 21 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 1c076b36b..b92312d52 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -52,23 +52,21 @@ pub(crate) struct Rule {
52 pub(crate) rhs: tt::Subtree, 52 pub(crate) rhs: tt::Subtree,
53} 53}
54 54
55/// Find the "shift" (the highest id of the TokenId) inside a subtree 55// Find the max token id inside a subtree
56fn find_subtree_shift(tt: &tt::Subtree, mut cur: u32) -> u32 { 56fn max_id(subtree: &tt::Subtree) -> Option<u32> {
57 use std::cmp::max; 57 subtree
58 58 .token_trees
59 for t in &tt.token_trees { 59 .iter()
60 cur = match t { 60 .filter_map(|tt| match tt {
61 tt::TokenTree::Leaf(leaf) => match leaf { 61 tt::TokenTree::Subtree(subtree) => max_id(subtree),
62 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { 62 tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
63 max(cur, ident.id.0) 63 if ident.id != tt::TokenId::unspecified() =>
64 } 64 {
65 _ => cur, 65 Some(ident.id.0)
66 }, 66 }
67 tt::TokenTree::Subtree(tt) => find_subtree_shift(tt, cur), 67 _ => None,
68 } 68 })
69 } 69 .max()
70
71 cur
72} 70}
73 71
74/// Shift given TokenTree token id 72/// Shift given TokenTree token id
@@ -77,9 +75,7 @@ fn shift_subtree(tt: &mut tt::Subtree, shift: u32) {
77 match t { 75 match t {
78 tt::TokenTree::Leaf(leaf) => match leaf { 76 tt::TokenTree::Leaf(leaf) => match leaf {
79 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { 77 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
80 // Note that TokenId is started from zero, 78 ident.id.0 += shift;
81 // We have to add 1 to prevent duplication.
82 ident.id.0 += shift + 1;
83 } 79 }
84 _ => (), 80 _ => (),
85 }, 81 },
@@ -110,7 +106,10 @@ impl MacroRules {
110 validate(&rule.lhs)?; 106 validate(&rule.lhs)?;
111 } 107 }
112 108
113 Ok(MacroRules { rules, shift: find_subtree_shift(tt, 0) }) 109 // Note that TokenId is started from zero,
110 // We have to add 1 to prevent duplication.
111 let shift = max_id(tt).unwrap_or(0) + 1;
112 Ok(MacroRules { rules, shift })
114 } 113 }
115 114
116 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> { 115 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {