Rename PreorderVisitor to SourceOrderVisitor (#11798)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Micha Reiser 2024-06-07 19:01:58 +02:00 committed by GitHub
parent 37d8de3316
commit 32ca704956
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 496 additions and 491 deletions

View file

@ -6,8 +6,11 @@ use crate::{
};
use crate::{AnyNodeRef, AstNode};
/// Visitor that traverses all nodes recursively in pre-order.
pub trait PreorderVisitor<'a> {
/// Visitor that traverses all nodes recursively in the order they appear in the source.
///
/// If you need a visitor that visits the nodes in the order they're evaluated at runtime,
/// use [`Visitor`](super::Visitor) instead.
pub trait SourceOrderVisitor<'a> {
#[inline]
fn enter_node(&mut self, _node: AnyNodeRef<'a>) -> TraversalSignal {
TraversalSignal::Traverse
@ -171,13 +174,13 @@ pub trait PreorderVisitor<'a> {
pub fn walk_module<'a, V>(visitor: &mut V, module: &'a Mod)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(module);
if visitor.enter_node(node).is_traverse() {
match module {
Mod::Module(module) => module.visit_preorder(visitor),
Mod::Expression(module) => module.visit_preorder(visitor),
Mod::Module(module) => module.visit_source_order(visitor),
Mod::Expression(module) => module.visit_source_order(visitor),
}
}
@ -186,7 +189,7 @@ where
pub fn walk_body<'a, V>(visitor: &mut V, body: &'a [Stmt])
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
for stmt in body {
visitor.visit_stmt(stmt);
@ -195,37 +198,37 @@ where
pub fn walk_stmt<'a, V>(visitor: &mut V, stmt: &'a Stmt)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(stmt);
if visitor.enter_node(node).is_traverse() {
match stmt {
Stmt::Expr(stmt) => stmt.visit_preorder(visitor),
Stmt::FunctionDef(stmt) => stmt.visit_preorder(visitor),
Stmt::ClassDef(stmt) => stmt.visit_preorder(visitor),
Stmt::Return(stmt) => stmt.visit_preorder(visitor),
Stmt::Delete(stmt) => stmt.visit_preorder(visitor),
Stmt::TypeAlias(stmt) => stmt.visit_preorder(visitor),
Stmt::Assign(stmt) => stmt.visit_preorder(visitor),
Stmt::AugAssign(stmt) => stmt.visit_preorder(visitor),
Stmt::AnnAssign(stmt) => stmt.visit_preorder(visitor),
Stmt::For(stmt) => stmt.visit_preorder(visitor),
Stmt::While(stmt) => stmt.visit_preorder(visitor),
Stmt::If(stmt) => stmt.visit_preorder(visitor),
Stmt::With(stmt) => stmt.visit_preorder(visitor),
Stmt::Match(stmt) => stmt.visit_preorder(visitor),
Stmt::Raise(stmt) => stmt.visit_preorder(visitor),
Stmt::Try(stmt) => stmt.visit_preorder(visitor),
Stmt::Assert(stmt) => stmt.visit_preorder(visitor),
Stmt::Import(stmt) => stmt.visit_preorder(visitor),
Stmt::ImportFrom(stmt) => stmt.visit_preorder(visitor),
Stmt::Pass(stmt) => stmt.visit_preorder(visitor),
Stmt::Break(stmt) => stmt.visit_preorder(visitor),
Stmt::Continue(stmt) => stmt.visit_preorder(visitor),
Stmt::Global(stmt) => stmt.visit_preorder(visitor),
Stmt::Nonlocal(stmt) => stmt.visit_preorder(visitor),
Stmt::IpyEscapeCommand(stmt) => stmt.visit_preorder(visitor),
Stmt::Expr(stmt) => stmt.visit_source_order(visitor),
Stmt::FunctionDef(stmt) => stmt.visit_source_order(visitor),
Stmt::ClassDef(stmt) => stmt.visit_source_order(visitor),
Stmt::Return(stmt) => stmt.visit_source_order(visitor),
Stmt::Delete(stmt) => stmt.visit_source_order(visitor),
Stmt::TypeAlias(stmt) => stmt.visit_source_order(visitor),
Stmt::Assign(stmt) => stmt.visit_source_order(visitor),
Stmt::AugAssign(stmt) => stmt.visit_source_order(visitor),
Stmt::AnnAssign(stmt) => stmt.visit_source_order(visitor),
Stmt::For(stmt) => stmt.visit_source_order(visitor),
Stmt::While(stmt) => stmt.visit_source_order(visitor),
Stmt::If(stmt) => stmt.visit_source_order(visitor),
Stmt::With(stmt) => stmt.visit_source_order(visitor),
Stmt::Match(stmt) => stmt.visit_source_order(visitor),
Stmt::Raise(stmt) => stmt.visit_source_order(visitor),
Stmt::Try(stmt) => stmt.visit_source_order(visitor),
Stmt::Assert(stmt) => stmt.visit_source_order(visitor),
Stmt::Import(stmt) => stmt.visit_source_order(visitor),
Stmt::ImportFrom(stmt) => stmt.visit_source_order(visitor),
Stmt::Pass(stmt) => stmt.visit_source_order(visitor),
Stmt::Break(stmt) => stmt.visit_source_order(visitor),
Stmt::Continue(stmt) => stmt.visit_source_order(visitor),
Stmt::Global(stmt) => stmt.visit_source_order(visitor),
Stmt::Nonlocal(stmt) => stmt.visit_source_order(visitor),
Stmt::IpyEscapeCommand(stmt) => stmt.visit_source_order(visitor),
}
}
@ -244,7 +247,7 @@ impl TraversalSignal {
}
}
pub fn walk_annotation<'a, V: PreorderVisitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
pub fn walk_annotation<'a, V: SourceOrderVisitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
let node = AnyNodeRef::from(expr);
if visitor.enter_node(node).is_traverse() {
visitor.visit_expr(expr);
@ -255,11 +258,11 @@ pub fn walk_annotation<'a, V: PreorderVisitor<'a> + ?Sized>(visitor: &mut V, exp
pub fn walk_decorator<'a, V>(visitor: &mut V, decorator: &'a Decorator)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(decorator);
if visitor.enter_node(node).is_traverse() {
decorator.visit_preorder(visitor);
decorator.visit_source_order(visitor);
}
visitor.leave_node(node);
@ -267,43 +270,43 @@ where
pub fn walk_expr<'a, V>(visitor: &mut V, expr: &'a Expr)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(expr);
if visitor.enter_node(node).is_traverse() {
match expr {
Expr::BoolOp(expr) => expr.visit_preorder(visitor),
Expr::Named(expr) => expr.visit_preorder(visitor),
Expr::BinOp(expr) => expr.visit_preorder(visitor),
Expr::UnaryOp(expr) => expr.visit_preorder(visitor),
Expr::Lambda(expr) => expr.visit_preorder(visitor),
Expr::If(expr) => expr.visit_preorder(visitor),
Expr::Dict(expr) => expr.visit_preorder(visitor),
Expr::Set(expr) => expr.visit_preorder(visitor),
Expr::ListComp(expr) => expr.visit_preorder(visitor),
Expr::SetComp(expr) => expr.visit_preorder(visitor),
Expr::DictComp(expr) => expr.visit_preorder(visitor),
Expr::Generator(expr) => expr.visit_preorder(visitor),
Expr::Await(expr) => expr.visit_preorder(visitor),
Expr::Yield(expr) => expr.visit_preorder(visitor),
Expr::YieldFrom(expr) => expr.visit_preorder(visitor),
Expr::Compare(expr) => expr.visit_preorder(visitor),
Expr::Call(expr) => expr.visit_preorder(visitor),
Expr::FString(expr) => expr.visit_preorder(visitor),
Expr::StringLiteral(expr) => expr.visit_preorder(visitor),
Expr::BytesLiteral(expr) => expr.visit_preorder(visitor),
Expr::NumberLiteral(expr) => expr.visit_preorder(visitor),
Expr::BooleanLiteral(expr) => expr.visit_preorder(visitor),
Expr::NoneLiteral(expr) => expr.visit_preorder(visitor),
Expr::EllipsisLiteral(expr) => expr.visit_preorder(visitor),
Expr::Attribute(expr) => expr.visit_preorder(visitor),
Expr::Subscript(expr) => expr.visit_preorder(visitor),
Expr::Starred(expr) => expr.visit_preorder(visitor),
Expr::Name(expr) => expr.visit_preorder(visitor),
Expr::List(expr) => expr.visit_preorder(visitor),
Expr::Tuple(expr) => expr.visit_preorder(visitor),
Expr::Slice(expr) => expr.visit_preorder(visitor),
Expr::IpyEscapeCommand(expr) => expr.visit_preorder(visitor),
Expr::BoolOp(expr) => expr.visit_source_order(visitor),
Expr::Named(expr) => expr.visit_source_order(visitor),
Expr::BinOp(expr) => expr.visit_source_order(visitor),
Expr::UnaryOp(expr) => expr.visit_source_order(visitor),
Expr::Lambda(expr) => expr.visit_source_order(visitor),
Expr::If(expr) => expr.visit_source_order(visitor),
Expr::Dict(expr) => expr.visit_source_order(visitor),
Expr::Set(expr) => expr.visit_source_order(visitor),
Expr::ListComp(expr) => expr.visit_source_order(visitor),
Expr::SetComp(expr) => expr.visit_source_order(visitor),
Expr::DictComp(expr) => expr.visit_source_order(visitor),
Expr::Generator(expr) => expr.visit_source_order(visitor),
Expr::Await(expr) => expr.visit_source_order(visitor),
Expr::Yield(expr) => expr.visit_source_order(visitor),
Expr::YieldFrom(expr) => expr.visit_source_order(visitor),
Expr::Compare(expr) => expr.visit_source_order(visitor),
Expr::Call(expr) => expr.visit_source_order(visitor),
Expr::FString(expr) => expr.visit_source_order(visitor),
Expr::StringLiteral(expr) => expr.visit_source_order(visitor),
Expr::BytesLiteral(expr) => expr.visit_source_order(visitor),
Expr::NumberLiteral(expr) => expr.visit_source_order(visitor),
Expr::BooleanLiteral(expr) => expr.visit_source_order(visitor),
Expr::NoneLiteral(expr) => expr.visit_source_order(visitor),
Expr::EllipsisLiteral(expr) => expr.visit_source_order(visitor),
Expr::Attribute(expr) => expr.visit_source_order(visitor),
Expr::Subscript(expr) => expr.visit_source_order(visitor),
Expr::Starred(expr) => expr.visit_source_order(visitor),
Expr::Name(expr) => expr.visit_source_order(visitor),
Expr::List(expr) => expr.visit_source_order(visitor),
Expr::Tuple(expr) => expr.visit_source_order(visitor),
Expr::Slice(expr) => expr.visit_source_order(visitor),
Expr::IpyEscapeCommand(expr) => expr.visit_source_order(visitor),
}
}
@ -312,11 +315,11 @@ where
pub fn walk_comprehension<'a, V>(visitor: &mut V, comprehension: &'a Comprehension)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(comprehension);
if visitor.enter_node(node).is_traverse() {
comprehension.visit_preorder(visitor);
comprehension.visit_source_order(visitor);
}
visitor.leave_node(node);
@ -324,11 +327,11 @@ where
pub fn walk_elif_else_clause<'a, V>(visitor: &mut V, elif_else_clause: &'a ElifElseClause)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(elif_else_clause);
if visitor.enter_node(node).is_traverse() {
elif_else_clause.visit_preorder(visitor);
elif_else_clause.visit_source_order(visitor);
}
visitor.leave_node(node);
@ -336,18 +339,20 @@ where
pub fn walk_except_handler<'a, V>(visitor: &mut V, except_handler: &'a ExceptHandler)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(except_handler);
if visitor.enter_node(node).is_traverse() {
match except_handler {
ExceptHandler::ExceptHandler(except_handler) => except_handler.visit_preorder(visitor),
ExceptHandler::ExceptHandler(except_handler) => {
except_handler.visit_source_order(visitor);
}
}
}
visitor.leave_node(node);
}
pub fn walk_format_spec<'a, V: PreorderVisitor<'a> + ?Sized>(
pub fn walk_format_spec<'a, V: SourceOrderVisitor<'a> + ?Sized>(
visitor: &mut V,
format_spec: &'a Expr,
) {
@ -361,11 +366,11 @@ pub fn walk_format_spec<'a, V: PreorderVisitor<'a> + ?Sized>(
pub fn walk_arguments<'a, V>(visitor: &mut V, arguments: &'a Arguments)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(arguments);
if visitor.enter_node(node).is_traverse() {
arguments.visit_preorder(visitor);
arguments.visit_source_order(visitor);
}
visitor.leave_node(node);
@ -373,11 +378,11 @@ where
pub fn walk_parameters<'a, V>(visitor: &mut V, parameters: &'a Parameters)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(parameters);
if visitor.enter_node(node).is_traverse() {
parameters.visit_preorder(visitor);
parameters.visit_source_order(visitor);
}
visitor.leave_node(node);
@ -385,12 +390,12 @@ where
pub fn walk_parameter<'a, V>(visitor: &mut V, parameter: &'a Parameter)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(parameter);
if visitor.enter_node(node).is_traverse() {
parameter.visit_preorder(visitor);
parameter.visit_source_order(visitor);
}
visitor.leave_node(node);
}
@ -399,11 +404,11 @@ pub fn walk_parameter_with_default<'a, V>(
visitor: &mut V,
parameter_with_default: &'a ParameterWithDefault,
) where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(parameter_with_default);
if visitor.enter_node(node).is_traverse() {
parameter_with_default.visit_preorder(visitor);
parameter_with_default.visit_source_order(visitor);
}
visitor.leave_node(node);
@ -412,48 +417,48 @@ pub fn walk_parameter_with_default<'a, V>(
#[inline]
pub fn walk_keyword<'a, V>(visitor: &mut V, keyword: &'a Keyword)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(keyword);
if visitor.enter_node(node).is_traverse() {
keyword.visit_preorder(visitor);
keyword.visit_source_order(visitor);
}
visitor.leave_node(node);
}
pub fn walk_with_item<'a, V>(visitor: &mut V, with_item: &'a WithItem)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(with_item);
if visitor.enter_node(node).is_traverse() {
with_item.visit_preorder(visitor);
with_item.visit_source_order(visitor);
}
visitor.leave_node(node);
}
pub fn walk_type_params<'a, V>(visitor: &mut V, type_params: &'a TypeParams)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(type_params);
if visitor.enter_node(node).is_traverse() {
type_params.visit_preorder(visitor);
type_params.visit_source_order(visitor);
}
visitor.leave_node(node);
}
pub fn walk_type_param<'a, V>(visitor: &mut V, type_param: &'a TypeParam)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(type_param);
if visitor.enter_node(node).is_traverse() {
match type_param {
TypeParam::TypeVar(type_param) => type_param.visit_preorder(visitor),
TypeParam::TypeVarTuple(type_param) => type_param.visit_preorder(visitor),
TypeParam::ParamSpec(type_param) => type_param.visit_preorder(visitor),
TypeParam::TypeVar(type_param) => type_param.visit_source_order(visitor),
TypeParam::TypeVarTuple(type_param) => type_param.visit_source_order(visitor),
TypeParam::ParamSpec(type_param) => type_param.visit_source_order(visitor),
}
}
visitor.leave_node(node);
@ -461,30 +466,30 @@ where
pub fn walk_match_case<'a, V>(visitor: &mut V, match_case: &'a MatchCase)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(match_case);
if visitor.enter_node(node).is_traverse() {
match_case.visit_preorder(visitor);
match_case.visit_source_order(visitor);
}
visitor.leave_node(node);
}
pub fn walk_pattern<'a, V>(visitor: &mut V, pattern: &'a Pattern)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(pattern);
if visitor.enter_node(node).is_traverse() {
match pattern {
Pattern::MatchValue(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchSingleton(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchSequence(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchMapping(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchClass(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchStar(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchAs(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchOr(pattern) => pattern.visit_preorder(visitor),
Pattern::MatchValue(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchSingleton(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchSequence(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchMapping(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchClass(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchStar(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchAs(pattern) => pattern.visit_source_order(visitor),
Pattern::MatchOr(pattern) => pattern.visit_source_order(visitor),
}
}
visitor.leave_node(node);
@ -492,7 +497,7 @@ where
pub fn walk_pattern_arguments<'a, V>(visitor: &mut V, pattern_arguments: &'a PatternArguments)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(pattern_arguments);
if visitor.enter_node(node).is_traverse() {
@ -508,7 +513,7 @@ where
pub fn walk_pattern_keyword<'a, V>(visitor: &mut V, pattern_keyword: &'a PatternKeyword)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(pattern_keyword);
if visitor.enter_node(node).is_traverse() {
@ -517,15 +522,15 @@ where
visitor.leave_node(node);
}
pub fn walk_f_string_element<'a, V: PreorderVisitor<'a> + ?Sized>(
pub fn walk_f_string_element<'a, V: SourceOrderVisitor<'a> + ?Sized>(
visitor: &mut V,
f_string_element: &'a FStringElement,
) {
let node = AnyNodeRef::from(f_string_element);
if visitor.enter_node(node).is_traverse() {
match f_string_element {
FStringElement::Expression(element) => element.visit_preorder(visitor),
FStringElement::Literal(element) => element.visit_preorder(visitor),
FStringElement::Expression(element) => element.visit_source_order(visitor),
FStringElement::Literal(element) => element.visit_source_order(visitor),
}
}
visitor.leave_node(node);
@ -533,39 +538,39 @@ pub fn walk_f_string_element<'a, V: PreorderVisitor<'a> + ?Sized>(
pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
}
#[inline]
pub fn walk_operator<'a, V>(_visitor: &mut V, _operator: &'a Operator)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
}
#[inline]
pub fn walk_unary_op<'a, V>(_visitor: &mut V, _unary_op: &'a UnaryOp)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
}
#[inline]
pub fn walk_cmp_op<'a, V>(_visitor: &mut V, _cmp_op: &'a CmpOp)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
}
#[inline]
pub fn walk_f_string<'a, V>(visitor: &mut V, f_string: &'a FString)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(f_string);
if visitor.enter_node(node).is_traverse() {
f_string.visit_preorder(visitor);
f_string.visit_source_order(visitor);
}
visitor.leave_node(node);
}
@ -573,11 +578,11 @@ where
#[inline]
pub fn walk_string_literal<'a, V>(visitor: &mut V, string_literal: &'a StringLiteral)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(string_literal);
if visitor.enter_node(node).is_traverse() {
string_literal.visit_preorder(visitor);
string_literal.visit_source_order(visitor);
}
visitor.leave_node(node);
}
@ -585,11 +590,11 @@ where
#[inline]
pub fn walk_bytes_literal<'a, V>(visitor: &mut V, bytes_literal: &'a BytesLiteral)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(bytes_literal);
if visitor.enter_node(node).is_traverse() {
bytes_literal.visit_preorder(visitor);
bytes_literal.visit_source_order(visitor);
}
visitor.leave_node(node);
}
@ -597,11 +602,11 @@ where
#[inline]
pub fn walk_alias<'a, V>(visitor: &mut V, alias: &'a Alias)
where
V: PreorderVisitor<'a> + ?Sized,
V: SourceOrderVisitor<'a> + ?Sized,
{
let node = AnyNodeRef::from(alias);
if visitor.enter_node(node).is_traverse() {
alias.visit_preorder(visitor);
alias.visit_source_order(visitor);
}
visitor.leave_node(node);
}