aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_expand/src/db.rs13
-rw-r--r--crates/ra_hir_expand/src/lib.rs12
-rw-r--r--crates/ra_mbe/src/lib.rs106
3 files changed, 84 insertions, 47 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index 5eadee9c2..9de7c1ea8 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -32,10 +32,17 @@ impl TokenExpander {
32 } 32 }
33 } 33 }
34 34
35 pub fn shift(&self) -> u32 { 35 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
36 match self { 36 match self {
37 TokenExpander::MacroRules(it) => it.shift(), 37 TokenExpander::MacroRules(it) => it.map_id_down(id),
38 TokenExpander::Builtin(_) => 0, 38 TokenExpander::Builtin(..) => id,
39 }
40 }
41
42 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
43 match self {
44 TokenExpander::MacroRules(it) => it.map_id_up(id),
45 TokenExpander::Builtin(..) => (id, mbe::Origin::Def),
39 } 46 }
40 } 47 }
41} 48}
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 1774cb58b..5927a03ba 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -161,7 +161,7 @@ impl ExpansionInfo {
161 pub fn translate_offset(&self, offset: TextUnit) -> Option<TextUnit> { 161 pub fn translate_offset(&self, offset: TextUnit) -> Option<TextUnit> {
162 let offset = offset.checked_sub(self.arg_start.1)?; 162 let offset = offset.checked_sub(self.arg_start.1)?;
163 let token_id = self.macro_arg.1.token_by_offset(offset)?; 163 let token_id = self.macro_arg.1.token_by_offset(offset)?;
164 let token_id = tt::TokenId(token_id.0 + self.macro_def.0.shift()); 164 let token_id = self.macro_def.0.map_id_down(token_id);
165 165
166 let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?; 166 let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?;
167 Some(r.start()) 167 Some(r.start())
@@ -170,11 +170,11 @@ impl ExpansionInfo {
170 pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { 170 pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> {
171 let token_id = look_in_rev_map(&self.exp_map, from)?; 171 let token_id = look_in_rev_map(&self.exp_map, from)?;
172 172
173 let shift = self.macro_def.0.shift(); 173 let (token_id, origin) = self.macro_def.0.map_id_up(token_id);
174 let (token_map, (file_id, start_offset), token_id) = if token_id.0 >= shift { 174
175 (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - shift).into()) 175 let (token_map, (file_id, start_offset)) = match origin {
176 } else { 176 mbe::Origin::Call => (&self.macro_arg.1, self.arg_start),
177 (&self.macro_def.1, self.def_start, token_id) 177 mbe::Origin::Def => (&self.macro_def.1, self.def_start),
178 }; 178 };
179 179
180 let range = token_map.relative_range_of(token_id)?; 180 let range = token_map.relative_range_of(token_id)?;
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 8a31d1c36..58ca95368 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -40,49 +40,75 @@ pub use crate::syntax_bridge::{
40/// and `$()*` have special meaning (see `Var` and `Repeat` data structures) 40/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
41#[derive(Clone, Debug, PartialEq, Eq)] 41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct MacroRules { 42pub struct MacroRules {
43 pub(crate) rules: Vec<Rule>, 43 rules: Vec<Rule>,
44 /// Highest id of the token we have in TokenMap 44 /// Highest id of the token we have in TokenMap
45 pub(crate) shift: u32, 45 shift: Shift,
46} 46}
47 47
48#[derive(Clone, Debug, PartialEq, Eq)] 48#[derive(Clone, Debug, PartialEq, Eq)]
49pub(crate) struct Rule { 49struct Rule {
50 pub(crate) lhs: tt::Subtree, 50 lhs: tt::Subtree,
51 pub(crate) rhs: tt::Subtree, 51 rhs: tt::Subtree,
52} 52}
53 53
54// Find the max token id inside a subtree 54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
55fn max_id(subtree: &tt::Subtree) -> Option<u32> { 55struct Shift(u32);
56 subtree 56
57 .token_trees 57impl Shift {
58 .iter() 58 fn new(tt: &tt::Subtree) -> Shift {
59 .filter_map(|tt| match tt { 59 // Note that TokenId is started from zero,
60 tt::TokenTree::Subtree(subtree) => max_id(subtree), 60 // We have to add 1 to prevent duplication.
61 tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) 61 let value = max_id(tt).map_or(0, |it| it + 1);
62 if ident.id != tt::TokenId::unspecified() => 62 return Shift(value);
63 { 63
64 Some(ident.id.0) 64 // Find the max token id inside a subtree
65 fn max_id(subtree: &tt::Subtree) -> Option<u32> {
66 subtree
67 .token_trees
68 .iter()
69 .filter_map(|tt| match tt {
70 tt::TokenTree::Subtree(subtree) => max_id(subtree),
71 tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
72 if ident.id != tt::TokenId::unspecified() =>
73 {
74 Some(ident.id.0)
75 }
76 _ => None,
77 })
78 .max()
79 }
80 }
81
82 /// Shift given TokenTree token id
83 fn shift_all(self, tt: &mut tt::Subtree) {
84 for t in tt.token_trees.iter_mut() {
85 match t {
86 tt::TokenTree::Leaf(leaf) => match leaf {
87 tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id),
88 _ => (),
89 },
90 tt::TokenTree::Subtree(tt) => self.shift_all(tt),
65 } 91 }
66 _ => None, 92 }
67 }) 93 }
68 .max()
69}
70 94
71/// Shift given TokenTree token id 95 fn shift(self, id: tt::TokenId) -> tt::TokenId {
72fn shift_subtree(tt: &mut tt::Subtree, shift: u32) { 96 if id == tt::TokenId::unspecified() {
73 for t in tt.token_trees.iter_mut() { 97 return id;
74 match t {
75 tt::TokenTree::Leaf(leaf) => match leaf {
76 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
77 ident.id.0 += shift;
78 }
79 _ => (),
80 },
81 tt::TokenTree::Subtree(tt) => shift_subtree(tt, shift),
82 } 98 }
99 tt::TokenId(id.0 + self.0)
100 }
101
102 fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
103 id.0.checked_sub(self.0).map(tt::TokenId)
83 } 104 }
84} 105}
85 106
107pub enum Origin {
108 Def,
109 Call,
110}
111
86impl MacroRules { 112impl MacroRules {
87 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> { 113 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
88 // Note: this parsing can be implemented using mbe machinery itself, by 114 // Note: this parsing can be implemented using mbe machinery itself, by
@@ -105,21 +131,25 @@ impl MacroRules {
105 validate(&rule.lhs)?; 131 validate(&rule.lhs)?;
106 } 132 }
107 133
108 // Note that TokenId is started from zero, 134 Ok(MacroRules { rules, shift: Shift::new(tt) })
109 // We have to add 1 to prevent duplication.
110 let shift = max_id(tt).map_or(0, |it| it + 1);
111 Ok(MacroRules { rules, shift })
112 } 135 }
113 136
114 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> { 137 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
115 // apply shift 138 // apply shift
116 let mut tt = tt.clone(); 139 let mut tt = tt.clone();
117 shift_subtree(&mut tt, self.shift); 140 self.shift.shift_all(&mut tt);
118 mbe_expander::expand(self, &tt) 141 mbe_expander::expand(self, &tt)
119 } 142 }
120 143
121 pub fn shift(&self) -> u32 { 144 pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
122 self.shift 145 self.shift.shift(id)
146 }
147
148 pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
149 match self.shift.unshift(id) {
150 Some(id) => (id, Origin::Call),
151 None => (id, Origin::Def),
152 }
123 } 153 }
124} 154}
125 155