apache / apache/datafusion-sqlparser-rs

Expose a method for mutating Parser::index

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

描述

We have designed an SQL parser using sqlparser's `Parser`, and we wish to implement two helper methods for consuming tokens—similar to `Parser::consume_token` and `Parser::consume_tokens`—but with the ability to accept string inputs and perform case-insensitive matching.

However, since the Parser does not provide a method to mutate the `Parser::index`, we are unable to correctly implement our own `consume_tokens` because we need to revert the parser state if consumption fails mid-way.

We propose adding and exposing a new method in sqlparser's `Parser`, such as `set_index` or `index_mut`, to allow users to mutate the index.

The expected implementation of our `consume_token` and `consume_tokens`:

``` rust
/// Consumes the next token if it matches the expected token, otherwise return false.
///
/// Note, the matching is not case sensitive.
fn consume_token(&mut self, expected: &str) -> bool {
if self.parser.peek_token().to_string().to_uppercase() == *expected.to_uppercase() {
self.parser.next_token();
true
} else {
false
}
}

/// If the current and subsequent tokens exactly match the `tokens` sequence, consume them and returns true.
/// Otherwise, no tokens are consumed and returns false
///
/// Note, the matching is not case sensitive.
fn consume_tokens(&mut self, tokens: &[&str]) -> bool {
let index = self.parser.index();
for token in tokens {
if !self.consume_token(*token) {
// Or: `*self.parser.index_mut() = index;`
self.parser.set_index(index);
return false;
}
}
true
}

```

贡献指南

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

调研方向

首先定位 Rust Parser 的实现以及现有的 index、consume_token 和 consume_tokens 方法。检查与 token 消费相关的 parser 测试,然后提供一种修改或设置 index 的方式,以便多 token 消费失败时能够恢复到之前的位置。

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

评估

技术栈
rust
领域
databases
Issue 类型
功能
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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