apache / apache/datafusion-sqlparser-rs
Question: why is the Visitor trait limited to statements, relations & expressions?
- 主要語言
- Rust
- 星號
- 3.5k
- 分支
- 774
- 平均合併
- 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 adapter 設計,還是提供每個節點的 hook;完成條件是實作已同意的泛用 Visitor API,同時保留現有的 Visitor 行為。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- rust
- 領域
- compilers
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100