diff options
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 106 |
1 files changed, 68 insertions, 38 deletions
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)] |
42 | pub struct MacroRules { | 42 | pub 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)] |
49 | pub(crate) struct Rule { | 49 | struct 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)] |
55 | fn max_id(subtree: &tt::Subtree) -> Option<u32> { | 55 | struct Shift(u32); |
56 | subtree | 56 | |
57 | .token_trees | 57 | impl 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 { |
72 | fn 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 | ||
107 | pub enum Origin { | ||
108 | Def, | ||
109 | Call, | ||
110 | } | ||
111 | |||
86 | impl MacroRules { | 112 | impl 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 | ||