mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 19:58:32 +00:00
Make shift an implementation detail of mbe
This commit is contained in:
parent
64dac40a86
commit
c8f858d043
@ -32,10 +32,17 @@ impl TokenExpander {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shift(&self) -> u32 {
|
pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
|
||||||
match self {
|
match self {
|
||||||
TokenExpander::MacroRules(it) => it.shift(),
|
TokenExpander::MacroRules(it) => it.map_id_down(id),
|
||||||
TokenExpander::Builtin(_) => 0,
|
TokenExpander::Builtin(..) => id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
|
||||||
|
match self {
|
||||||
|
TokenExpander::MacroRules(it) => it.map_id_up(id),
|
||||||
|
TokenExpander::Builtin(..) => (id, mbe::Origin::Def),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ impl ExpansionInfo {
|
|||||||
pub fn translate_offset(&self, offset: TextUnit) -> Option<TextUnit> {
|
pub fn translate_offset(&self, offset: TextUnit) -> Option<TextUnit> {
|
||||||
let offset = offset.checked_sub(self.arg_start.1)?;
|
let offset = offset.checked_sub(self.arg_start.1)?;
|
||||||
let token_id = self.macro_arg.1.token_by_offset(offset)?;
|
let token_id = self.macro_arg.1.token_by_offset(offset)?;
|
||||||
let token_id = tt::TokenId(token_id.0 + self.macro_def.0.shift());
|
let token_id = self.macro_def.0.map_id_down(token_id);
|
||||||
|
|
||||||
let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?;
|
let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?;
|
||||||
Some(r.start())
|
Some(r.start())
|
||||||
@ -170,11 +170,11 @@ impl ExpansionInfo {
|
|||||||
pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> {
|
pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> {
|
||||||
let token_id = look_in_rev_map(&self.exp_map, from)?;
|
let token_id = look_in_rev_map(&self.exp_map, from)?;
|
||||||
|
|
||||||
let shift = self.macro_def.0.shift();
|
let (token_id, origin) = self.macro_def.0.map_id_up(token_id);
|
||||||
let (token_map, (file_id, start_offset), token_id) = if token_id.0 >= shift {
|
|
||||||
(&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - shift).into())
|
let (token_map, (file_id, start_offset)) = match origin {
|
||||||
} else {
|
mbe::Origin::Call => (&self.macro_arg.1, self.arg_start),
|
||||||
(&self.macro_def.1, self.def_start, token_id)
|
mbe::Origin::Def => (&self.macro_def.1, self.def_start),
|
||||||
};
|
};
|
||||||
|
|
||||||
let range = token_map.relative_range_of(token_id)?;
|
let range = token_map.relative_range_of(token_id)?;
|
||||||
|
@ -40,47 +40,73 @@ pub use crate::syntax_bridge::{
|
|||||||
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
|
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct MacroRules {
|
pub struct MacroRules {
|
||||||
pub(crate) rules: Vec<Rule>,
|
rules: Vec<Rule>,
|
||||||
/// Highest id of the token we have in TokenMap
|
/// Highest id of the token we have in TokenMap
|
||||||
pub(crate) shift: u32,
|
shift: Shift,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub(crate) struct Rule {
|
struct Rule {
|
||||||
pub(crate) lhs: tt::Subtree,
|
lhs: tt::Subtree,
|
||||||
pub(crate) rhs: tt::Subtree,
|
rhs: tt::Subtree,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the max token id inside a subtree
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
fn max_id(subtree: &tt::Subtree) -> Option<u32> {
|
struct Shift(u32);
|
||||||
subtree
|
|
||||||
.token_trees
|
|
||||||
.iter()
|
|
||||||
.filter_map(|tt| match tt {
|
|
||||||
tt::TokenTree::Subtree(subtree) => max_id(subtree),
|
|
||||||
tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
|
|
||||||
if ident.id != tt::TokenId::unspecified() =>
|
|
||||||
{
|
|
||||||
Some(ident.id.0)
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.max()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shift given TokenTree token id
|
impl Shift {
|
||||||
fn shift_subtree(tt: &mut tt::Subtree, shift: u32) {
|
fn new(tt: &tt::Subtree) -> Shift {
|
||||||
for t in tt.token_trees.iter_mut() {
|
// Note that TokenId is started from zero,
|
||||||
match t {
|
// We have to add 1 to prevent duplication.
|
||||||
tt::TokenTree::Leaf(leaf) => match leaf {
|
let value = max_id(tt).map_or(0, |it| it + 1);
|
||||||
tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
|
return Shift(value);
|
||||||
ident.id.0 += shift;
|
|
||||||
}
|
// Find the max token id inside a subtree
|
||||||
_ => (),
|
fn max_id(subtree: &tt::Subtree) -> Option<u32> {
|
||||||
},
|
subtree
|
||||||
tt::TokenTree::Subtree(tt) => shift_subtree(tt, shift),
|
.token_trees
|
||||||
|
.iter()
|
||||||
|
.filter_map(|tt| match tt {
|
||||||
|
tt::TokenTree::Subtree(subtree) => max_id(subtree),
|
||||||
|
tt::TokenTree::Leaf(tt::Leaf::Ident(ident))
|
||||||
|
if ident.id != tt::TokenId::unspecified() =>
|
||||||
|
{
|
||||||
|
Some(ident.id.0)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.max()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shift given TokenTree token id
|
||||||
|
fn shift_all(self, tt: &mut tt::Subtree) {
|
||||||
|
for t in tt.token_trees.iter_mut() {
|
||||||
|
match t {
|
||||||
|
tt::TokenTree::Leaf(leaf) => match leaf {
|
||||||
|
tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id),
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
tt::TokenTree::Subtree(tt) => self.shift_all(tt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shift(self, id: tt::TokenId) -> tt::TokenId {
|
||||||
|
if id == tt::TokenId::unspecified() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
tt::TokenId(id.0 + self.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
|
||||||
|
id.0.checked_sub(self.0).map(tt::TokenId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Origin {
|
||||||
|
Def,
|
||||||
|
Call,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacroRules {
|
impl MacroRules {
|
||||||
@ -105,21 +131,25 @@ impl MacroRules {
|
|||||||
validate(&rule.lhs)?;
|
validate(&rule.lhs)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note that TokenId is started from zero,
|
Ok(MacroRules { rules, shift: Shift::new(tt) })
|
||||||
// We have to add 1 to prevent duplication.
|
|
||||||
let shift = max_id(tt).map_or(0, |it| it + 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> {
|
||||||
// apply shift
|
// apply shift
|
||||||
let mut tt = tt.clone();
|
let mut tt = tt.clone();
|
||||||
shift_subtree(&mut tt, self.shift);
|
self.shift.shift_all(&mut tt);
|
||||||
mbe_expander::expand(self, &tt)
|
mbe_expander::expand(self, &tt)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shift(&self) -> u32 {
|
pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
|
||||||
self.shift
|
self.shift.shift(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) {
|
||||||
|
match self.shift.unshift(id) {
|
||||||
|
Some(id) => (id, Origin::Call),
|
||||||
|
None => (id, Origin::Def),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user