apache / apache/datafusion-sqlparser-rs

Question: why is the Visitor trait limited to statements, relations & expressions?

Abierto
#934 25 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Rust
Estrellas
3.5k
Forks
772
Merge medio
4 d 9 h
PR fusionados (30 d)
17

Descripción

What is the reason for that particular design decision versus providing a more general `Visitor` implementation?

Two options for a generalised Visitor trait come to mind:

1. expose pre + post trait method variants for every AST node type, or
2. expose only two trait methods (`pre_visit` + `post_visit`) with signatures like `fn pre_visit(&mut self, node: &AstNode) -> ControlFlow` - where `AstNode` is an enum with a wrapper variant for every AST node type found in `src/ast/mod.rs` and can be `match`ed against.

Would the maintainers be interested in a PR that implements one of the above two approaches?

My preference would be for option 2 because it would not break the trait when node types are added/removed.

Suggested approach:

1. Define a new `RawVisitor` trait (and `RawVisitorMut` trait) like this:

```rust
pub trait RawVisitor {
type Break;
fn pre_visit(&mut self, node: &AstNode) -> ControlFlow;
fn post_visit(&mut self, node: &AstNode) -> ControlFlow;
}
```

2. Define an adapter type (`RawVisitorAdapter` ?) that accepts a `V: Visitor` generic argument and implements `RawVisitor` & `RawVisitorMut`, which calls the appropriate method on `V` (or none at all)

```rust
struct RawVisitorAdapter(v);

impl RawVisitor for RawVisitorAdapter {
type Break = V::Break;

fn pre_visit(&mut self, node: &AstNode) -> ControlFlow {
match node {
AstNode(Statement) => self.0.pre_visit_statement(...),
// etc
}
}

fn post_visit(&mut self, node: &AstNode) -> ControlFlow;
}
```

3. Change the `Visit` derivation macros to generate code in terms of `RawVisitor` & `RawVisitorMut` instead of `Visitor`, like this:

```rust
pub trait Visit {
fn visit_raw(&self, visitor: &mut V) -> ControlFlow;

// This has an identical signature to the existing trait, but has a default implementation
fn visit(&self, visitor: &mut V) -> ControlFlow {
self.visit_raw(RawVisitorAdapter::new(visitor))
}
}
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

Revisa los traits Visitor existentes y las macros de derivación de Visit; después, inspecciona las definiciones de los nodos AST en src/ast/mod.rs. Confirma primero con los maintainers si se quiere el diseño del adaptador RawVisitor o hooks por nodo; la tarea estará terminada cuando se implemente la API de Visitor generalizada acordada, preservando el comportamiento existente de Visitor.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
rust
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.