aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs49
1 files changed, 47 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index afdbee84e..7d4a5f307 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -42,6 +42,8 @@ pub use crate::syntax_bridge::{
42#[derive(Clone, Debug, PartialEq, Eq)] 42#[derive(Clone, Debug, PartialEq, Eq)]
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
46 pub(crate) shift: Option<u32>,
45} 47}
46 48
47#[derive(Clone, Debug, PartialEq, Eq)] 49#[derive(Clone, Debug, PartialEq, Eq)]
@@ -50,6 +52,42 @@ pub(crate) struct Rule {
50 pub(crate) rhs: tt::Subtree, 52 pub(crate) rhs: tt::Subtree,
51} 53}
52 54
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> {
57 use std::cmp::max;
58
59 for t in &tt.token_trees {
60 cur = match t {
61 tt::TokenTree::Leaf(leaf) => match leaf {
62 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
63 Some(max(cur.unwrap_or(0), ident.id.0))
64 }
65 _ => cur,
66 },
67 tt::TokenTree::Subtree(tt) => find_subtree_shift(tt, cur),
68 }
69 }
70
71 cur
72}
73
74/// Shift given TokenTree token id
75fn shift_token_tree(tt: &mut tt::Subtree, shift: u32) {
76 for t in tt.token_trees.iter_mut() {
77 match t {
78 tt::TokenTree::Leaf(leaf) => match leaf {
79 tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
80 // Note that TokenId is started from zero,
81 // We have to add 1 to prevent duplication.
82 ident.id.0 += shift + 1;
83 }
84 _ => (),
85 },
86 tt::TokenTree::Subtree(tt) => shift_token_tree(tt, shift),
87 }
88 }
89}
90
53impl MacroRules { 91impl MacroRules {
54 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> { 92 pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
55 // Note: this parsing can be implemented using mbe machinery itself, by 93 // Note: this parsing can be implemented using mbe machinery itself, by
@@ -72,10 +110,17 @@ impl MacroRules {
72 validate(&rule.lhs)?; 110 validate(&rule.lhs)?;
73 } 111 }
74 112
75 Ok(MacroRules { rules }) 113 Ok(MacroRules { rules, shift: find_subtree_shift(tt, None) })
76 } 114 }
115
77 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> { 116 pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
78 mbe_expander::expand(self, tt) 117 // apply shift
118 let mut tt = tt.clone();
119 if let Some(shift) = self.shift {
120 shift_token_tree(&mut tt, shift)
121 }
122
123 mbe_expander::expand(self, &tt)
79 } 124 }
80} 125}
81 126