You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+17
Original file line number
Diff line number
Diff line change
@@ -100,6 +100,23 @@ similar semantics are represented with the same AST. We welcome PRs to fix such
100
100
issues and distinguish different syntaxes in the AST.
101
101
102
102
103
+
## WIP: Extracting source locations from AST nodes
104
+
105
+
This crate allows recovering source locations from AST nodes via the [Spanned](https://docs.rs/sqlparser/latest/sqlparser/ast/trait.Spanned.html) trait, which can be used for advanced diagnostics tooling. Note that this feature is a work in progress and many nodes report missing or inaccurate spans. Please see [this document](./docs/source_spans.md#source-span-contributing-guidelines) for information on how to contribute missing improvements.
106
+
107
+
```rust
108
+
usesqlparser::ast::Spanned;
109
+
110
+
// Parse SQL
111
+
letast=Parser::parse_sql(&GenericDialect, "SELECT A FROM B").unwrap();
112
+
113
+
// The source span can be retrieved with start and end locations
114
+
assert_eq!(ast[0].span(), Span {
115
+
start:Location::of(1, 1),
116
+
end:Location::of(1, 16),
117
+
});
118
+
```
119
+
103
120
## SQL compliance
104
121
105
122
SQL was first standardized in 1987, and revisions of the standard have been
These are the current breaking changes introduced by the source spans feature:
5
+
6
+
#### Added fields for spans (must be added to any existing pattern matches)
7
+
-`Ident` now stores a `Span`
8
+
-`Select`, `With`, `Cte`, `WildcardAdditionalOptions` now store a `TokenWithLocation`
9
+
10
+
#### Misc.
11
+
-`TokenWithLocation` stores a full `Span`, rather than just a source location. Users relying on `token.location` should use `token.location.start` instead.
12
+
## Source Span Contributing Guidelines
13
+
14
+
For contributing source spans improvement in addition to the general [contribution guidelines](../README.md#contributing), please make sure to pay attention to the following:
15
+
16
+
17
+
### Source Span Design Considerations
18
+
19
+
-`Ident` always have correct source spans
20
+
- Downstream breaking change impact is to be as minimal as possible
21
+
- To this end, use recursive merging of spans in favor of storing spans on all nodes
22
+
- Any metadata added to compute spans must not change semantics (Eq, Ord, Hash, etc.)
23
+
24
+
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.
25
+
26
+
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.
27
+
28
+
Example of a trivial change
29
+
```rust
30
+
matchnode {
31
+
ast::Query {
32
+
field1,
33
+
field2,
34
+
location:_, // add a new line to ignored location
35
+
}
36
+
```
37
+
38
+
If adding source spans to a type would require a significant change like wrapping that type or similar, please open an issue to discuss.
39
+
40
+
### AST Node Equality and Hashes
41
+
42
+
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.
43
+
44
+
```rust
45
+
structSelect {
46
+
select_token:AttachedToken, // only used for spans
0 commit comments