apache / apache/datafusion-sqlparser-rs
Question: why is the Visitor trait limited to statements, relations & expressions?
- Lingua principale
- Rust
- Stelle
- 3.5k
- Fork
- 772
- Merge medio
- 4g 9h
- PR unite (30g)
- 17
Descrizione
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))
}
}
```
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Esamina i trait Visitor esistenti e le macro di derivazione di Visit, quindi ispeziona le definizioni dei nodi AST in src/ast/mod.rs. Conferma innanzitutto con i maintainer se è desiderato il design dell’adapter RawVisitor o degli hook per nodo; il lavoro è completo quando viene implementata l’API Visitor generalizzata concordata, preservando il comportamento esistente di Visitor.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rust
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100