-
Notifications
You must be signed in to change notification settings - Fork 612
Implement Spanned
to retrieve source locations on AST nodes
#1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
1a77bac
feat(tokenizer): add source location spans to tokens
Nyrox d818012
feat: begin work on trait Spanned
Nyrox 079a4e2
implement a bunch more stuff
Nyrox b97a781
Merge branch 'feat/ast-source-locations'
Nyrox df9ab1e
fix: restore old behaviour of location display
Nyrox b718c76
implement spans for eveeeeen more ast nodes
Nyrox 8986a1e
feat: more ast nodes
Nyrox aeb4f3a
start working on better tests
Nyrox 4de3209
feat: implement spans for Wildcard projections
Nyrox a04888a
make union_spans public
Nyrox 1b2b03d
enable serde feat for spans and locations
Nyrox 5f60bdc
feat: implement remaining ast nodes
Nyrox ea8a6b1
fix unused variable warnings
Nyrox 6a9250a
undo parse_keyword signature change
Nyrox 0804e99
fix: diverging hash and partialeq implementations
Nyrox eb9ff9a
Update src/ast/spans.rs
Nyrox a93cebc
improve docs & un-pub union_spans
Nyrox 734264a
move union_spans to top of file
Nyrox 441ceb1
replace old tests
Nyrox e6a4340
pr feedback
Nyrox 16a3f2a
add small comment
Nyrox 98b051d
refactor: rewrite all span implementations to pattern match exhaustiv…
Nyrox d76d1e0
for_clause is mssql, not mysql
Nyrox bf75fe4
Merge branch 'main' into main
Nyrox 71c27ea
cargo fmt
Nyrox 1353bf2
Merge remote-tracking branch 'apache/main'
Nyrox a31c6a6
lint & no-std
Nyrox ce8b35c
add IgnoreField helper
Nyrox 2bb72a4
docs and fixes
Nyrox 4dda1fe
fix: test failing
Nyrox 3fa3766
pr feedback
Nyrox 6bfe13f
rename ignore_field.rs -> attached_token.rs
Nyrox 903f24a
pr feedback
Nyrox 3989efe
add AttachedToken::empty
Nyrox b2b8795
Merge branch 'main' of https://github.com/apache/datafusion-sqlparser-rs
Nyrox 1f3d514
Merge remote-tracking branch 'apache/main' into Nyrox/main
alamb 23c4922
update test to avoid overflow
alamb b24c9fe
Merge branch 'main' of https://github.com/apache/datafusion-sqlparser-rs
Nyrox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
## Breaking Changes | ||
|
||
These are the current breaking changes introduced by the source spans feature: | ||
|
||
#### Added fields for spans (must be added to any existing pattern matches) | ||
- `Ident` now stores a `Span` | ||
- `Select`, `With`, `Cte`, `WildcardAdditionalOptions` now store a `TokenWithLocation` | ||
|
||
#### Misc. | ||
- `TokenWithLocation` stores a full `Span`, rather than just a source location. Users relying on `token.location` should use `token.location.start` instead. | ||
## Source Span Contributing Guidelines | ||
|
||
For contributing source spans improvement in addition to the general [contribution guidelines](../README.md#contributing), please make sure to pay attention to the following: | ||
|
||
|
||
### Source Span Design Considerations | ||
|
||
- `Ident` always have correct source spans | ||
- Downstream breaking change impact is to be as minimal as possible | ||
- To this end, use recursive merging of spans in favor of storing spans on all nodes | ||
- Any metadata added to compute spans must not change semantics (Eq, Ord, Hash, etc.) | ||
|
||
The primary reason for missing and inaccurate source spans at this time is missing spans of keyword tokens and values in many structures, either due to lack of time or because adding them would break downstream significantly. | ||
|
||
When considering adding support for source spans on a type, consider the impact to consumers of that type and whether your change would require a consumer to do non-trivial changes to their code. | ||
|
||
Example of a trivial change | ||
```rust | ||
match node { | ||
ast::Query { | ||
field1, | ||
field2, | ||
location: _, // add a new line to ignored location | ||
} | ||
``` | ||
|
||
If adding source spans to a type would require a significant change like wrapping that type or similar, please open an issue to discuss. | ||
|
||
### AST Node Equality and Hashes | ||
|
||
When adding tokens to AST nodes, make sure to store them using the [AttachedToken](https://docs.rs/sqlparser/latest/sqlparser/ast/helpers/struct.AttachedToken.html) helper to ensure that semantically equivalent AST nodes always compare as equal and hash to the same value. F.e. `select 5` and `SELECT 5` would compare as different `Select` nodes, if the select token was stored directly. f.e. | ||
|
||
```rust | ||
struct Select { | ||
select_token: AttachedToken, // only used for spans | ||
/// remaining fields | ||
field1, | ||
field2, | ||
... | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}; | ||
use core::fmt::{self, Debug, Formatter}; | ||
use core::hash::{Hash, Hasher}; | ||
|
||
use crate::tokenizer::{Token, TokenWithLocation}; | ||
|
||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg(feature = "visitor")] | ||
use sqlparser_derive::{Visit, VisitMut}; | ||
|
||
/// A wrapper type for attaching tokens to AST nodes that should be ignored in comparisons and hashing. | ||
/// This should be used when a token is not relevant for semantics, but is still needed for | ||
/// accurate source location tracking. | ||
#[derive(Clone)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||
pub struct AttachedToken(pub TokenWithLocation); | ||
|
||
impl AttachedToken { | ||
pub fn empty() -> Self { | ||
AttachedToken(TokenWithLocation::wrap(Token::EOF)) | ||
} | ||
} | ||
|
||
// Conditional Implementations | ||
impl Debug for AttachedToken { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
// Blanket Implementations | ||
impl PartialEq for AttachedToken { | ||
fn eq(&self, _: &Self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
impl Eq for AttachedToken {} | ||
|
||
impl PartialOrd for AttachedToken { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for AttachedToken { | ||
fn cmp(&self, _: &Self) -> Ordering { | ||
Ordering::Equal | ||
} | ||
} | ||
|
||
impl Hash for AttachedToken { | ||
fn hash<H: Hasher>(&self, _state: &mut H) { | ||
// Do nothing | ||
} | ||
} | ||
|
||
impl From<TokenWithLocation> for AttachedToken { | ||
fn from(value: TokenWithLocation) -> Self { | ||
AttachedToken(value) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.