mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Refactor and rename
This commit is contained in:
parent
42661a3b27
commit
188a1412b9
1 changed files with 20 additions and 21 deletions
|
@ -52,23 +52,21 @@ pub(crate) struct Rule {
|
||||||
pub(crate) rhs: tt::Subtree,
|
pub(crate) rhs: tt::Subtree,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find the "shift" (the highest id of the TokenId) inside a subtree
|
// Find the max token id inside a subtree
|
||||||
fn find_subtree_shift(tt: &tt::Subtree, mut cur: u32) -> u32 {
|
fn max_id(subtree: &tt::Subtree) -> Option<u32> {
|
||||||
use std::cmp::max;
|
subtree
|
||||||
|
.token_trees
|
||||||
for t in &tt.token_trees {
|
.iter()
|
||||||
cur = match t {
|
.filter_map(|tt| match tt {
|
||||||
tt::TokenTree::Leaf(leaf) => match leaf {
|
tt::TokenTree::Subtree(subtree) => max_id(subtree),
|
||||||
tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
|
tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
|
||||||
max(cur, ident.id.0)
|
if ident.id != tt::TokenId::unspecified() =>
|
||||||
}
|
{
|
||||||
_ => cur,
|
Some(ident.id.0)
|
||||||
},
|
}
|
||||||
tt::TokenTree::Subtree(tt) => find_subtree_shift(tt, cur),
|
_ => None,
|
||||||
}
|
})
|
||||||
}
|
.max()
|
||||||
|
|
||||||
cur
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shift given TokenTree token id
|
/// Shift given TokenTree token id
|
||||||
|
@ -77,9 +75,7 @@ fn shift_subtree(tt: &mut tt::Subtree, shift: u32) {
|
||||||
match t {
|
match t {
|
||||||
tt::TokenTree::Leaf(leaf) => match leaf {
|
tt::TokenTree::Leaf(leaf) => match leaf {
|
||||||
tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
|
tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
|
||||||
// Note that TokenId is started from zero,
|
ident.id.0 += shift;
|
||||||
// We have to add 1 to prevent duplication.
|
|
||||||
ident.id.0 += shift + 1;
|
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
},
|
||||||
|
@ -110,7 +106,10 @@ impl MacroRules {
|
||||||
validate(&rule.lhs)?;
|
validate(&rule.lhs)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MacroRules { rules, shift: find_subtree_shift(tt, 0) })
|
// Note that TokenId is started from zero,
|
||||||
|
// We have to add 1 to prevent duplication.
|
||||||
|
let shift = max_id(tt).unwrap_or(0) + 1;
|
||||||
|
Ok(MacroRules { rules, shift })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
|
pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
|
||||||
|
|
Loading…
Reference in a new issue