apache / apache/datafusion-sqlparser-rs
Question: why is the Visitor trait limited to statements, relations & expressions?
- Linguagem predominante
- Rust
- Estrelas
- 3.5k
- Forks
- 772
- Merge médio
- 4d 9h
- PRs com merge (30d)
- 17
Descrição
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))
}
}
```
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Direção de pesquisa
Revise os traits Visitor existentes e as macros de derivação de Visit; em seguida, inspecione as definições dos nós AST em src/ast/mod.rs. Primeiro confirme com os maintainers se o design do adaptador RawVisitor ou hooks por nó é desejado; a tarefa estará concluída quando a API Visitor generalizada acordada for implementada, preservando o comportamento existente de Visitor.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- rust
- Domínio
- compilers
- Tipo de issue
- Funcionalidade
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 25/100