syntax: Use Token
in visitors and fix a mut visitor test
This commit is contained in:
parent
67ce3f4589
commit
738e14565d
4 changed files with 25 additions and 17 deletions
|
@ -6,7 +6,7 @@ use syntax::ast::*;
|
|||
use syntax::ext::hygiene::Mark;
|
||||
use syntax::visit;
|
||||
use syntax::symbol::{kw, sym};
|
||||
use syntax::parse::token::{self, TokenKind};
|
||||
use syntax::parse::token::{self, Token};
|
||||
use syntax_pos::Span;
|
||||
|
||||
/// Creates `DefId`s for nodes in the AST.
|
||||
|
@ -325,8 +325,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn visit_token(&mut self, t: TokenKind) {
|
||||
if let token::Interpolated(nt) = t {
|
||||
fn visit_token(&mut self, t: Token) {
|
||||
if let token::Interpolated(nt) = t.kind {
|
||||
if let token::NtExpr(ref expr) = *nt {
|
||||
if let ExprKind::Mac(..) = expr.node {
|
||||
self.visit_macro_invoc(expr.id);
|
||||
|
|
|
@ -34,7 +34,7 @@ use syntax::ext::base::Determinacy::Undetermined;
|
|||
use syntax::ext::hygiene::Mark;
|
||||
use syntax::ext::tt::macro_rules;
|
||||
use syntax::feature_gate::is_builtin_attr;
|
||||
use syntax::parse::token::{self, TokenKind};
|
||||
use syntax::parse::token::{self, Token};
|
||||
use syntax::span_err;
|
||||
use syntax::std_inject::injected_crate_name;
|
||||
use syntax::symbol::{kw, sym};
|
||||
|
@ -1052,8 +1052,8 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
|
|||
self.resolver.current_module = parent;
|
||||
}
|
||||
|
||||
fn visit_token(&mut self, t: TokenKind) {
|
||||
if let token::Interpolated(nt) = t {
|
||||
fn visit_token(&mut self, t: Token) {
|
||||
if let token::Interpolated(nt) = t.kind {
|
||||
if let token::NtExpr(ref expr) = *nt {
|
||||
if let ast::ExprKind::Mac(..) = expr.node {
|
||||
self.visit_invoc(expr.id);
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
use crate::ast::*;
|
||||
use crate::source_map::{Spanned, respan};
|
||||
use crate::parse::token::{self, Token, TokenKind};
|
||||
use crate::parse::token::{self, Token};
|
||||
use crate::ptr::P;
|
||||
use crate::ThinVec;
|
||||
use crate::tokenstream::*;
|
||||
|
@ -262,7 +262,7 @@ pub trait MutVisitor: Sized {
|
|||
noop_visit_tts(tts, self);
|
||||
}
|
||||
|
||||
fn visit_token(&mut self, t: &mut TokenKind) {
|
||||
fn visit_token(&mut self, t: &mut Token) {
|
||||
noop_visit_token(t, self);
|
||||
}
|
||||
|
||||
|
@ -576,9 +576,8 @@ pub fn noop_visit_arg<T: MutVisitor>(Arg { id, pat, ty }: &mut Arg, vis: &mut T)
|
|||
|
||||
pub fn noop_visit_tt<T: MutVisitor>(tt: &mut TokenTree, vis: &mut T) {
|
||||
match tt {
|
||||
TokenTree::Token(Token { kind, span }) => {
|
||||
vis.visit_token(kind);
|
||||
vis.visit_span(span);
|
||||
TokenTree::Token(token) => {
|
||||
vis.visit_token(token);
|
||||
}
|
||||
TokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
|
||||
vis.visit_span(open);
|
||||
|
@ -595,15 +594,24 @@ pub fn noop_visit_tts<T: MutVisitor>(TokenStream(tts): &mut TokenStream, vis: &m
|
|||
})
|
||||
}
|
||||
|
||||
// apply ident visitor if it's an ident, apply other visits to interpolated nodes
|
||||
pub fn noop_visit_token<T: MutVisitor>(t: &mut TokenKind, vis: &mut T) {
|
||||
match t {
|
||||
// Apply ident visitor if it's an ident, apply other visits to interpolated nodes.
|
||||
// In practice the ident part is not actually used by specific visitors right now,
|
||||
// but there's a test below checking that it works.
|
||||
pub fn noop_visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
|
||||
let Token { kind, span } = t;
|
||||
match kind {
|
||||
token::Ident(name, _) | token::Lifetime(name) => {
|
||||
let mut ident = Ident::new(*name, *span);
|
||||
vis.visit_ident(&mut ident);
|
||||
*name = ident.name;
|
||||
}
|
||||
token::Interpolated(nt) => {
|
||||
let mut nt = Lrc::make_mut(nt);
|
||||
vis.visit_interpolated(&mut nt);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
vis.visit_span(span);
|
||||
}
|
||||
|
||||
/// Apply visitor to elements of interpolated nodes.
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
//! those that are created by the expansion of a macro.
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::parse::token::TokenKind;
|
||||
use crate::parse::token::Token;
|
||||
use crate::tokenstream::{TokenTree, TokenStream};
|
||||
|
||||
use syntax_pos::Span;
|
||||
|
@ -151,7 +151,7 @@ pub trait Visitor<'ast>: Sized {
|
|||
fn visit_tts(&mut self, tts: TokenStream) {
|
||||
walk_tts(self, tts)
|
||||
}
|
||||
fn visit_token(&mut self, _t: TokenKind) {}
|
||||
fn visit_token(&mut self, _t: Token) {}
|
||||
// FIXME: add `visit_interpolated` and `walk_interpolated`
|
||||
fn visit_vis(&mut self, vis: &'ast Visibility) {
|
||||
walk_vis(self, vis)
|
||||
|
@ -855,7 +855,7 @@ pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute)
|
|||
|
||||
pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
|
||||
match tt {
|
||||
TokenTree::Token(token) => visitor.visit_token(token.kind),
|
||||
TokenTree::Token(token) => visitor.visit_token(token),
|
||||
TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue