apache / apache/datafusion-sqlparser-rs

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

未关闭
#934 25 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rust
星标
3.5k
派生
772
平均合并
4 天 9 小时
30 天内合并 PR
17

描述

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

贡献指南

这个仓库没有索引到贡献指南

调研方向

查看现有的 Visitor trait 和 Visit 派生宏,然后检查 src/ast/mod.rs 中的 AST 节点定义。首先与维护者确认需要采用 RawVisitor 适配器设计还是按节点提供 hook;完成标准是实现约定的通用 Visitor API,同时保留现有的 Visitor 行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
rust
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。