aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-04 15:22:18 +0000
committerEdwin Cheng <[email protected]>2019-11-04 15:22:18 +0000
commit04af290d4e08e282dda0c0273f20a46f381224f4 (patch)
treea031afe34e6e94fc3bc73394da88192a8564f3c6
parentf9f1effd011b906903891c09f1cb6b2a42f73e95 (diff)
Add TokenId Shif in macro_rules
-rw-r--r--crates/ra_mbe/src/lib.rs49
-rw-r--r--crates/ra_mbe/src/tests.rs27
2 files changed, 74 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
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs
index f34ab52e1..a23e3afe3 100644
--- a/crates/ra_mbe/src/tests.rs
+++ b/crates/ra_mbe/src/tests.rs
@@ -59,6 +59,33 @@ mod rule_parsing {
59// * Make it pass :-) 59// * Make it pass :-)
60 60
61#[test] 61#[test]
62fn test_token_id_shift() {
63 let macro_definition = r#"
64macro_rules! foobar {
65 ($e:ident) => { foo bar $e }
66}
67"#;
68 let rules = create_rules(macro_definition);
69 let expansion = expand(&rules, "foobar!(baz);");
70
71 fn get_id(t: &tt::TokenTree) -> Option<u32> {
72 if let tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) = t {
73 return Some(ident.id.0);
74 }
75 None
76 }
77
78 assert_eq!(expansion.token_trees.len(), 3);
79 // ($e:ident) => { foo bar $e }
80 // 0 1 2 3 4
81 assert_eq!(get_id(&expansion.token_trees[0]), Some(2));
82 assert_eq!(get_id(&expansion.token_trees[1]), Some(3));
83
84 // So baz should be 5
85 assert_eq!(get_id(&expansion.token_trees[2]), Some(5));
86}
87
88#[test]
62fn test_convert_tt() { 89fn test_convert_tt() {
63 let macro_definition = r#" 90 let macro_definition = r#"
64macro_rules! impl_froms { 91macro_rules! impl_froms {