aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/lib.rs13
1 files changed, 5 insertions, 8 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index f51149258..1c076b36b 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -43,7 +43,7 @@ pub use crate::syntax_bridge::{
43pub struct MacroRules { 43pub struct MacroRules {
44 pub(crate) rules: Vec<Rule>, 44 pub(crate) rules: Vec<Rule>,
45 /// Highest id of the token we have in TokenMap 45 /// Highest id of the token we have in TokenMap
46 pub(crate) shift: Option<u32>, 46 pub(crate) shift: u32,
47} 47}
48 48
49#[derive(Clone, Debug, PartialEq, Eq)] 49#[derive(Clone, Debug, PartialEq, Eq)]
@@ -53,14 +53,14 @@ pub(crate) struct Rule {
53} 53}
54 54
55/// Find the "shift" (the highest id of the TokenId) inside a subtree 55/// Find the "shift" (the highest id of the TokenId) inside a subtree
56fn find_subtree_shift(tt: &tt::Subtree, mut cur: Option<u32>) -> Option<u32> { 56fn find_subtree_shift(tt: &tt::Subtree, mut cur: u32) -> u32 {
57 use std::cmp::max; 57 use std::cmp::max;
58 58
59 for t in &tt.token_trees { 59 for t in &tt.token_trees {
60 cur = match t { 60 cur = match t {
61 tt::TokenTree::Leaf(leaf) => match leaf { 61 tt::TokenTree::Leaf(leaf) => match leaf {
62 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => { 62 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
63 Some(max(cur.unwrap_or(0), ident.id.0)) 63 max(cur, ident.id.0)
64 } 64 }
65 _ => cur, 65 _ => cur,
66 }, 66 },
@@ -110,16 +110,13 @@ impl MacroRules {
110 validate(&rule.lhs)?; 110 validate(&rule.lhs)?;
111 } 111 }
112 112
113 Ok(MacroRules { rules, shift: find_subtree_shift(tt, None) }) 113 Ok(MacroRules { rules, shift: find_subtree_shift(tt, 0) })
114 } 114 }
115 115
116 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> { 116 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
117 // apply shift 117 // apply shift
118 let mut tt = tt.clone(); 118 let mut tt = tt.clone();
119 if let Some(shift) = self.shift { 119 shift_subtree(&mut tt, self.shift);
120 shift_subtree(&mut tt, shift)
121 }
122
123 mbe_expander::expand(self, &tt) 120 mbe_expander::expand(self, &tt)
124 } 121 }
125} 122}