7
7
// As more changes to this AST are expected, unless explicitly advised, using the structures exposed
8
8
// in this crate directly is not recommended.
9
9
10
- use partiql_source_map:: location:: { ByteOffset , BytePosition , Location } ;
11
10
use rust_decimal:: Decimal as RustDecimal ;
12
11
13
12
use std:: fmt;
14
- use std:: fmt:: Display ;
15
- use std:: ops:: Range ;
16
13
17
14
#[ cfg( feature = "serde" ) ]
18
15
use serde:: { Deserialize , Serialize } ;
19
16
20
- /// Provides the required methods for AstNode conversations.
21
- pub trait ToAstNode : Sized {
22
- /// Wraps the `self` to an [AstNode] and returns an `AstNodeBuilder` for
23
- /// further [AstNode] construction.
24
- /// ## Example:
25
- /// ```
26
- /// use partiql_ast::ast;
27
- /// use partiql_ast::ast::{SymbolPrimitive, ToAstNode};
28
- /// use partiql_ast::ast::CaseSensitivity::CaseInsensitive;
29
- /// use partiql_source_map::location::{ByteOffset, BytePosition, Location, ToLocated};
30
- ///
31
- /// let p = SymbolPrimitive {
32
- /// value: "symbol2".to_string(),
33
- /// case: Some(ast::CaseSensitivity::CaseInsensitive)
34
- /// };
35
- ///
36
- /// let node = p
37
- /// .to_node()
38
- /// .location((BytePosition::from(12)..BytePosition::from(1)).into())
39
- /// .build()
40
- /// .expect("Could not retrieve ast node");
41
- /// ```
42
- fn to_node ( self ) -> AstNodeBuilder < Self , BytePosition >
43
- where
44
- Self : Clone ,
45
- {
46
- AstNodeBuilder :: default ( ) . node ( self ) . clone ( )
47
- }
48
-
49
- fn to_ast < Loc , IntoLoc > ( self , location : IntoLoc ) -> AstNode < Self , Loc >
50
- where
51
- Loc : Display ,
52
- IntoLoc : Into < Location < Loc > > ,
53
- {
54
- AstNode {
55
- node : self ,
56
- location : Some ( location. into ( ) ) ,
57
- }
58
- }
59
-
60
- fn ast ( self , Range { start, end } : Range < ByteOffset > ) -> AstNode < Self , BytePosition > {
61
- self . to_ast ( start. into ( ) ..end. into ( ) )
62
- }
63
- }
64
-
65
- /// Implements [ToAstNode] for all types within this crate, read further [here][1].
66
- ///
67
- /// [1]: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods
68
- impl < T > ToAstNode for T { }
17
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash ) ]
18
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
19
+ pub struct NodeId ( pub u32 ) ;
69
20
70
- /// Represents an AST node. [AstNode] uses [derive_builder][1] to expose a Builder
71
- /// for creating the node. See [ToAstNode] for more details on the usage.
72
- ///
73
- /// [1]: https://crates.io/crates/derive_builder
74
- #[ derive( Builder , Clone , Debug ) ]
21
+ /// Represents an AST node.
22
+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
75
23
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
76
- pub struct AstNode < T , Loc : Display > {
24
+ pub struct AstNode < T > {
25
+ pub id : NodeId ,
77
26
pub node : T ,
78
- #[ builder( setter( strip_option) , default ) ]
79
- pub location : Option < Location < Loc > > ,
80
27
}
81
28
82
- impl < T : PartialEq , Loc : Display > PartialEq for AstNode < T , Loc > {
83
- fn eq ( & self , other : & Self ) -> bool {
84
- self . node == other. node
85
- }
86
- }
87
-
88
- impl < T : Eq , Loc : Display > Eq for AstNode < T , Loc > { }
89
-
90
29
#[ derive( Clone , Debug , PartialEq ) ]
91
30
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
92
31
pub struct Item {
@@ -140,13 +79,13 @@ pub enum DdlOpKind {
140
79
DropIndex ( DropIndex ) ,
141
80
}
142
81
143
- #[ derive( Clone , Debug , PartialEq ) ]
82
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
144
83
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
145
84
pub struct CreateTable {
146
85
pub table_name : SymbolPrimitive ,
147
86
}
148
87
149
- #[ derive( Clone , Debug , PartialEq ) ]
88
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
150
89
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
151
90
pub struct DropTable {
152
91
pub table_name : SymbolPrimitive ,
@@ -159,7 +98,7 @@ pub struct CreateIndex {
159
98
pub fields : Vec < Box < Expr > > ,
160
99
}
161
100
162
- #[ derive( Clone , Debug , PartialEq ) ]
101
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
163
102
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
164
103
pub struct DropIndex {
165
104
pub table : Ident ,
@@ -234,7 +173,7 @@ pub struct OnConflict {
234
173
}
235
174
236
175
/// `CONFLICT_ACTION <action>`
237
- #[ derive( Clone , Debug , PartialEq ) ]
176
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
238
177
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
239
178
pub enum ConflictAction {
240
179
DoNothing ,
@@ -246,43 +185,40 @@ pub struct Expr {
246
185
pub kind : ExprKind ,
247
186
}
248
187
249
- /// Represents an AST Node of type T with BytePosition Location
250
- pub type AstBytePos < T > = AstNode < T , BytePosition > ;
251
-
252
- pub type BagAst = AstBytePos < Bag > ;
253
- pub type BetweenAst = AstBytePos < Between > ;
254
- pub type BinOpAst = AstBytePos < BinOp > ;
255
- pub type CallAggAst = AstBytePos < CallAgg > ;
256
- pub type CallArgAst = AstBytePos < CallArg > ;
257
- pub type CallAst = AstBytePos < Call > ;
258
- pub type CaseAst = AstBytePos < Case > ;
259
- pub type FromClauseAst = AstBytePos < FromClause > ;
260
- pub type FromLetAst = AstBytePos < FromLet > ;
261
- pub type GroupByExprAst = AstBytePos < GroupByExpr > ;
262
- pub type GroupKeyAst = AstBytePos < GroupKey > ;
263
- pub type InAst = AstBytePos < In > ;
264
- pub type JoinAst = AstBytePos < Join > ;
265
- pub type JoinSpecAst = AstBytePos < JoinSpec > ;
266
- pub type LetAst = AstBytePos < Let > ;
267
- pub type LikeAst = AstBytePos < Like > ;
268
- pub type ListAst = AstBytePos < List > ;
269
- pub type LitAst = AstBytePos < Lit > ;
270
- pub type OrderByExprAst = AstBytePos < OrderByExpr > ;
271
- pub type ParamAst = AstBytePos < Param > ;
272
- pub type PathAst = AstBytePos < Path > ;
273
- pub type ProjectItemAst = AstBytePos < ProjectItem > ;
274
- pub type ProjectionAst = AstBytePos < Projection > ;
275
- pub type QueryAst = AstBytePos < Query > ;
276
- pub type QuerySetAst = AstBytePos < QuerySet > ;
277
- pub type SearchedCaseAst = AstBytePos < SearchedCase > ;
278
- pub type SelectAst = AstBytePos < Select > ;
279
- pub type SetExprAst = AstBytePos < SetExpr > ;
280
- pub type SexpAst = AstBytePos < Sexp > ;
281
- pub type SimpleCaseAst = AstBytePos < SimpleCase > ;
282
- pub type SortSpecAst = AstBytePos < SortSpec > ;
283
- pub type StructAst = AstBytePos < Struct > ;
284
- pub type UniOpAst = AstBytePos < UniOp > ;
285
- pub type VarRefAst = AstBytePos < VarRef > ;
188
+ pub type BagAst = AstNode < Bag > ;
189
+ pub type BetweenAst = AstNode < Between > ;
190
+ pub type BinOpAst = AstNode < BinOp > ;
191
+ pub type CallAggAst = AstNode < CallAgg > ;
192
+ pub type CallArgAst = AstNode < CallArg > ;
193
+ pub type CallAst = AstNode < Call > ;
194
+ pub type CaseAst = AstNode < Case > ;
195
+ pub type FromClauseAst = AstNode < FromClause > ;
196
+ pub type FromLetAst = AstNode < FromLet > ;
197
+ pub type GroupByExprAst = AstNode < GroupByExpr > ;
198
+ pub type GroupKeyAst = AstNode < GroupKey > ;
199
+ pub type InAst = AstNode < In > ;
200
+ pub type JoinAst = AstNode < Join > ;
201
+ pub type JoinSpecAst = AstNode < JoinSpec > ;
202
+ pub type LetAst = AstNode < Let > ;
203
+ pub type LikeAst = AstNode < Like > ;
204
+ pub type ListAst = AstNode < List > ;
205
+ pub type LitAst = AstNode < Lit > ;
206
+ pub type OrderByExprAst = AstNode < OrderByExpr > ;
207
+ pub type ParamAst = AstNode < Param > ;
208
+ pub type PathAst = AstNode < Path > ;
209
+ pub type ProjectItemAst = AstNode < ProjectItem > ;
210
+ pub type ProjectionAst = AstNode < Projection > ;
211
+ pub type QueryAst = AstNode < Query > ;
212
+ pub type QuerySetAst = AstNode < QuerySet > ;
213
+ pub type SearchedCaseAst = AstNode < SearchedCase > ;
214
+ pub type SelectAst = AstNode < Select > ;
215
+ pub type SetExprAst = AstNode < SetExpr > ;
216
+ pub type SexpAst = AstNode < Sexp > ;
217
+ pub type SimpleCaseAst = AstNode < SimpleCase > ;
218
+ pub type SortSpecAst = AstNode < SortSpec > ;
219
+ pub type StructAst = AstNode < Struct > ;
220
+ pub type UniOpAst = AstNode < UniOp > ;
221
+ pub type VarRefAst = AstNode < VarRef > ;
286
222
287
223
#[ derive( Clone , Debug , PartialEq ) ]
288
224
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
@@ -311,7 +247,7 @@ pub struct SetExpr {
311
247
pub rhs : Box < QuerySetAst > ,
312
248
}
313
249
314
- #[ derive( Clone , Debug , PartialEq ) ]
250
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
315
251
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
316
252
pub enum SetOperator {
317
253
Union ,
@@ -385,29 +321,29 @@ pub enum Lit {
385
321
TypedLit ( String , Type ) ,
386
322
}
387
323
388
- #[ derive( Clone , Debug , PartialEq ) ]
324
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
389
325
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
390
326
pub enum CollectionLit {
391
327
ArrayLit ( String ) ,
392
328
BagLit ( String ) ,
393
329
}
394
330
395
- #[ derive( Clone , Debug , PartialEq ) ]
331
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
396
332
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
397
333
pub enum DateTimeLit {
398
334
DateLit ( String ) ,
399
335
TimeLit ( String ) ,
400
336
TimestampLit ( String ) ,
401
337
}
402
338
403
- #[ derive( Clone , Debug , PartialEq ) ]
339
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
404
340
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
405
341
pub struct VarRef {
406
342
pub name : SymbolPrimitive ,
407
343
pub qualifier : ScopeQualifier ,
408
344
}
409
345
410
- #[ derive( Clone , Debug , PartialEq ) ]
346
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
411
347
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
412
348
pub struct Param {
413
349
pub index : i32 ,
@@ -421,7 +357,7 @@ pub struct BinOp {
421
357
pub rhs : Box < Expr > ,
422
358
}
423
359
424
- #[ derive( Clone , Debug , PartialEq ) ]
360
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
425
361
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
426
362
pub enum BinOpKind {
427
363
// Arithmetic
@@ -453,7 +389,7 @@ pub struct UniOp {
453
389
pub expr : Box < Expr > ,
454
390
}
455
391
456
- #[ derive( Clone , Debug , PartialEq ) ]
392
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
457
393
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
458
394
pub enum UniOpKind {
459
395
Pos ,
@@ -607,7 +543,7 @@ pub struct PathExpr {
607
543
}
608
544
609
545
/// Is used to determine if variable lookup should be case-sensitive or not.
610
- #[ derive( Clone , Debug , PartialEq ) ]
546
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
611
547
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
612
548
pub enum CaseSensitivity {
613
549
CaseSensitive ,
@@ -706,15 +642,15 @@ pub enum JoinSpec {
706
642
707
643
/// Indicates the type of FromLet, see the following for more details:
708
644
/// https:///github.com/partiql/partiql-lang-kotlin/issues/242
709
- #[ derive( Clone , Debug , PartialEq ) ]
645
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
710
646
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
711
647
pub enum FromLetKind {
712
648
Scan ,
713
649
Unpivot ,
714
650
}
715
651
716
652
/// Indicates the logical type of join.
717
- #[ derive( Clone , Debug , PartialEq ) ]
653
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
718
654
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
719
655
pub enum JoinKind {
720
656
Inner ,
@@ -744,7 +680,7 @@ pub struct GroupByExpr {
744
680
745
681
/// Desired grouping qualifier: ALL or PARTIAL. Note: the `group_` prefix is
746
682
/// needed to avoid naming clashes.
747
- #[ derive( Clone , Debug , PartialEq ) ]
683
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
748
684
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
749
685
pub enum GroupingStrategy {
750
686
GroupFull ,
@@ -782,14 +718,14 @@ pub struct SortSpec {
782
718
pub null_ordering_spec : Option < NullOrderingSpec > ,
783
719
}
784
720
785
- #[ derive( Clone , Debug , PartialEq ) ]
721
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
786
722
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
787
723
pub enum OrderingSpec {
788
724
Asc ,
789
725
Desc ,
790
726
}
791
727
792
- #[ derive( Clone , Debug , PartialEq ) ]
728
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
793
729
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
794
730
pub enum NullOrderingSpec {
795
731
First ,
@@ -798,7 +734,7 @@ pub enum NullOrderingSpec {
798
734
799
735
/// Indicates scope search order when resolving variables.
800
736
/// Has no effect except within `FROM` sources.
801
- #[ derive( Clone , Debug , PartialEq ) ]
737
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
802
738
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
803
739
pub enum ScopeQualifier {
804
740
/// Use the default search order.
@@ -808,7 +744,7 @@ pub enum ScopeQualifier {
808
744
}
809
745
810
746
/// Indicates if a set should be reduced to its distinct elements or not.
811
- #[ derive( Clone , Debug , PartialEq ) ]
747
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
812
748
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
813
749
pub enum SetQuantifier {
814
750
All ,
@@ -844,7 +780,7 @@ pub struct ReturningColumn {
844
780
}
845
781
846
782
/// ( MODIFIED | ALL ) ( NEW | OLD )
847
- #[ derive( Clone , Debug , PartialEq ) ]
783
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
848
784
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
849
785
pub enum ReturningMapping {
850
786
ModifiedNew ,
@@ -863,7 +799,7 @@ pub enum ReturningMapping {
863
799
/// an element of a type. (Even though in the Kotlin code each varaint is its own type.) Hence, we
864
800
/// define an `Ident` type above which can be used without opening up an element's domain to
865
801
/// all of `expr`.
866
- #[ derive( Clone , Debug , PartialEq ) ]
802
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
867
803
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
868
804
pub struct Ident {
869
805
pub name : SymbolPrimitive ,
@@ -937,7 +873,7 @@ pub struct CustomType {
937
873
pub parts : Vec < CustomTypePart > ,
938
874
}
939
875
940
- #[ derive( Clone , Debug , PartialEq ) ]
876
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
941
877
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
942
878
pub struct SymbolPrimitive {
943
879
pub value : String ,
0 commit comments