Token-based reverse-mapping

This commit is contained in:
Aleksey Kladov 2019-11-18 15:08:39 +03:00
parent 5e8c22dbf3
commit b79d678923
3 changed files with 52 additions and 38 deletions

View file

@ -20,7 +20,7 @@ pub struct TokenMap {
/// Maps relative range of the expanded syntax node to `tt::TokenId`
#[derive(Debug, PartialEq, Eq, Default)]
pub struct RevTokenMap {
pub ranges: Vec<(TextRange, tt::TokenId)>,
ranges: Vec<(TextRange, tt::TokenId)>,
}
/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
@ -96,6 +96,10 @@ impl TokenMap {
}
impl RevTokenMap {
pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> {
self.ranges.iter().find(|&it| it.0 == relative_range).map(|it| it.1)
}
pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TextRange> {
let &(r, _) = self.ranges.iter().find(|(_, tid)| *tid == token_id)?;
Some(r)