apache / apache/datafusion-sqlparser-rs

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

Offen
#934 25 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
3.5k
Forks
772
Ø Merge
4 T. 9 Std.
Gemergte PRs (30 T.)
17

Beschreibung

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))
}
}
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Überprüfe die vorhandenen Visitor-Traits und Visit-Ableitungsmakros und untersuche anschließend die Definitionen der AST-Knoten in src/ast/mod.rs. Kläre zunächst mit den Maintainer:innen, ob das RawVisitor-Adapter-Design oder Hooks pro Knoten gewünscht ist; abgeschlossen ist die Aufgabe, wenn die vereinbarte generalisierte Visitor-API implementiert wurde und das bestehende Visitor-Verhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rust
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.