Skip to content

Commit 1f4d759

Browse files
committed
unify AST naming
This commit unifies the naming patterns of the AST to be consistent with the DAG and the semantic tree. Ops have suffixes "Op" and ast.Exprs have suffix "Expr". We removed the Kind field from structs that aren't part of sum types. We also separate ast.ID into ID and IDExpr to differentiate the use of ID in an expression versus identifiers in other non-expression entities. There are further improvements that can be made but this is a big step in the right direction.
1 parent e957e3f commit 1f4d759

File tree

16 files changed

+5305
-5332
lines changed

16 files changed

+5305
-5332
lines changed

compiler/ast/ast.go

Lines changed: 0 additions & 772 deletions
This file was deleted.

compiler/ast/decl.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ast
2+
3+
type Decl interface {
4+
Node
5+
declNode()
6+
}
7+
8+
type ConstDecl struct {
9+
Kind string `json:"kind" unpack:""`
10+
Name *ID `json:"name"`
11+
Expr Expr `json:"expr"`
12+
Loc `json:"loc"`
13+
}
14+
15+
type FuncDecl struct {
16+
Kind string `json:"kind" unpack:""`
17+
Name *ID `json:"name"`
18+
Lambda *LambdaExpr `json:"lambda"`
19+
Loc `json:"loc"`
20+
}
21+
22+
type OpDecl struct {
23+
Kind string `json:"kind" unpack:""`
24+
Name *ID `json:"name"`
25+
Params []*ID `json:"params"`
26+
Body Seq `json:"body"`
27+
Loc `json:"loc"`
28+
}
29+
30+
type QueryDecl struct {
31+
Kind string `json:"kind" unpack:""`
32+
Name *ID `json:"name"`
33+
Body Seq `json:"body"`
34+
Loc `json:"loc"`
35+
}
36+
37+
type TypeDecl struct {
38+
Kind string `json:"kind" unpack:""`
39+
Name *ID `json:"name"`
40+
Type Type `json:"type"`
41+
Loc `json:"loc"`
42+
}
43+
44+
func (*ConstDecl) declNode() {}
45+
func (*FuncDecl) declNode() {}
46+
func (*OpDecl) declNode() {}
47+
func (*QueryDecl) declNode() {}
48+
func (*TypeDecl) declNode() {}

compiler/ast/expr.go

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package ast
2+
3+
type Expr interface {
4+
Node
5+
exprNode()
6+
}
7+
8+
type (
9+
ArrayExpr struct {
10+
Kind string `json:"kind" unpack:""`
11+
Elems []ArrayElem `json:"elems"`
12+
Loc `json:"loc"`
13+
}
14+
BetweenExpr struct {
15+
Kind string `json:"kind" unpack:""`
16+
Not bool `json:"not"`
17+
Expr Expr `json:"expr"`
18+
Lower Expr `json:"lower"`
19+
Upper Expr `json:"upper"`
20+
Loc `json:"loc"`
21+
}
22+
// A BinaryExpr is any expression of the form "lhs kind rhs"
23+
// including arithmetic (+, -, *, /), logical operators (and, or),
24+
// comparisons (=, !=, <, <=, >, >=), and a dot expression (".") (on records).
25+
BinaryExpr struct {
26+
Kind string `json:"kind" unpack:""`
27+
Op string `json:"op"`
28+
LHS Expr `json:"lhs"`
29+
RHS Expr `json:"rhs"`
30+
Loc `json:"loc"`
31+
}
32+
// A CallExpr represents different things dependending on its context.
33+
// As an operator (when wrapped in an OpExpr), it is either an aggregate
34+
// with no grouping keys and no duration, or a filter with a function
35+
// that is boolean valued. This is determined by the compiler rather than
36+
// the syntax tree based on the specific functions and aggregators that
37+
// are defined at compile time. In expression context, a function call has
38+
// the standard semantics where it takes one or more arguments and returns a result.
39+
CallExpr struct {
40+
Kind string `json:"kind" unpack:""`
41+
Func Expr `json:"func"`
42+
Args []Expr `json:"args"`
43+
Where Expr `json:"where"`
44+
Loc `json:"loc"`
45+
}
46+
CaseExpr struct {
47+
Kind string `json:"kind" unpack:""`
48+
Expr Expr `json:"expr"`
49+
Whens []When `json:"whens"`
50+
Else Expr `json:"else"`
51+
Loc `json:"loc"`
52+
}
53+
CondExpr struct {
54+
Kind string `json:"kind" unpack:""`
55+
Cond Expr `json:"cond"`
56+
Then Expr `json:"then"`
57+
Else Expr `json:"else"`
58+
Loc `json:"loc"`
59+
}
60+
// DoubleQuoteExpr is specialized from the other primitive types because
61+
// these values can be interpreted either as a string value or an identifier based
62+
// on SQL vs pipe context. The semantic pass needs to know the string was
63+
// originally double quoted to perform this analysis.
64+
DoubleQuoteExpr struct {
65+
Kind string `json:"kind" unpack:""`
66+
Text string `json:"text"`
67+
Loc `json:"loc"`
68+
}
69+
ExistsExpr struct {
70+
Kind string `json:"kind" unpack:""`
71+
Body Seq `json:"body"`
72+
Loc `json:"loc"`
73+
}
74+
ExtractExpr struct {
75+
Kind string `json:"kind" unpack:""`
76+
Part Expr `json:"part"`
77+
Expr Expr `json:"expr"`
78+
Loc `json:"loc"`
79+
}
80+
FuncNameExpr struct {
81+
Kind string `json:"kind" unpack:""`
82+
Name string `json:"name"`
83+
Loc `json:"loc"`
84+
}
85+
FStringExpr struct {
86+
Kind string `json:"kind" unpack:""`
87+
Elems []FStringElem `json:"elems"`
88+
Loc `json:"loc"`
89+
}
90+
GlobExpr struct {
91+
Kind string `json:"kind" unpack:""`
92+
Pattern string `json:"pattern"`
93+
Loc `json:"loc"`
94+
}
95+
IDExpr struct {
96+
Kind string `json:"kind" unpack:""`
97+
ID `json:"id"`
98+
}
99+
IndexExpr struct {
100+
Kind string `json:"kind" unpack:""`
101+
Expr Expr `json:"expr"`
102+
Index Expr `json:"index"`
103+
Loc `json:"loc"`
104+
}
105+
IsNullExpr struct {
106+
Kind string `json:"kind" unpack:""`
107+
Expr Expr `json:"expr"`
108+
Not bool `json:"not"`
109+
Loc `json:"loc"`
110+
}
111+
LambdaExpr struct {
112+
Kind string `json:"kind" unpack:""`
113+
Params []*ID `json:"params"`
114+
Expr Expr `json:"expr"`
115+
Loc `json:"loc"`
116+
}
117+
MapExpr struct {
118+
Kind string `json:"kind" unpack:""`
119+
Entries []MapEntry `json:"entries"`
120+
Loc `json:"loc"`
121+
}
122+
RecordExpr struct {
123+
Kind string `json:"kind" unpack:""`
124+
Elems []RecordElem `json:"elems"`
125+
Loc `json:"loc"`
126+
}
127+
RegexpExpr struct {
128+
Kind string `json:"kind" unpack:""`
129+
Pattern string `json:"pattern"`
130+
Loc `json:"loc"`
131+
}
132+
SearchTermExpr struct {
133+
Kind string `json:"kind" unpack:""`
134+
Text string `json:"text"`
135+
Value Any `json:"value"`
136+
Loc `json:"loc"`
137+
}
138+
SetExpr struct {
139+
Kind string `json:"kind" unpack:""`
140+
Elems []ArrayElem `json:"elems"`
141+
Loc `json:"loc"`
142+
}
143+
SliceExpr struct {
144+
Kind string `json:"kind" unpack:""`
145+
Expr Expr `json:"expr"`
146+
From Expr `json:"from"`
147+
To Expr `json:"to"`
148+
Loc `json:"loc"`
149+
}
150+
SQLTimeExpr struct {
151+
Kind string `json:"kind" unpack:""`
152+
Type string `json:"type"`
153+
Value *Primitive `json:"value"`
154+
Loc `json:"loc"`
155+
}
156+
SubqueryExpr struct {
157+
Kind string `json:"kind" unpack:""`
158+
Body Seq `json:"body"`
159+
Array bool `json:"array"`
160+
Loc `json:"loc"`
161+
}
162+
TupleExpr struct {
163+
Kind string `json:"kind" unpack:""`
164+
Elems []Expr `json:"elems"`
165+
Loc `json:"loc"`
166+
}
167+
UnaryExpr struct {
168+
Kind string `json:"kind" unpack:""`
169+
Op string `json:"op"`
170+
Operand Expr `json:"operand"`
171+
Loc `json:"loc"`
172+
}
173+
)
174+
175+
// Support structures embedded in Expr nodes
176+
177+
type ID struct {
178+
Name string `json:"name"`
179+
Loc `json:"loc"`
180+
}
181+
182+
type MapEntry struct {
183+
Key Expr `json:"key"`
184+
Value Expr `json:"value"`
185+
Loc `json:"loc"`
186+
}
187+
188+
type When struct {
189+
Cond Expr `json:"expr"`
190+
Then Expr `json:"else"`
191+
Loc `json:"loc"`
192+
}
193+
194+
type ArrayElem interface {
195+
arrayElemNode()
196+
}
197+
198+
type RecordElem interface {
199+
recordElemNode()
200+
}
201+
202+
type (
203+
FieldElem struct {
204+
Kind string `json:"kind" unpack:""`
205+
Name *Text `json:"name"`
206+
Value Expr `json:"value"`
207+
Loc `json:"loc"`
208+
}
209+
SpreadElem struct {
210+
Kind string `json:"kind" unpack:""`
211+
Expr Expr `json:"expr"`
212+
Loc `json:"loc"`
213+
}
214+
ExprElem struct {
215+
Kind string `json:"kind" unpack:""`
216+
Expr Expr `json:"expr"`
217+
Loc `json:"loc"`
218+
}
219+
)
220+
221+
func (*ExprElem) arrayElemNode() {}
222+
func (*ExprElem) recordElemNode() {}
223+
func (*FieldElem) recordElemNode() {}
224+
func (*SpreadElem) arrayElemNode() {}
225+
func (*SpreadElem) recordElemNode() {}
226+
227+
type FStringElem interface {
228+
Node
229+
fStringElemNode()
230+
}
231+
232+
type (
233+
FStringTextElem struct {
234+
Kind string `json:"kind" unpack:""`
235+
Text string `json:"text"`
236+
Loc `json:"loc"`
237+
}
238+
FStringExprElem struct {
239+
Kind string `json:"kind" unpack:""`
240+
Expr Expr `json:"expr"`
241+
Loc `json:"loc"`
242+
}
243+
)
244+
245+
func (*FStringTextElem) fStringElemNode() {}
246+
func (*FStringExprElem) fStringElemNode() {}
247+
248+
func (*Agg) exprNode() {}
249+
func (*ArrayExpr) exprNode() {}
250+
func (*BetweenExpr) exprNode() {}
251+
func (*BinaryExpr) exprNode() {}
252+
func (*CallExpr) exprNode() {}
253+
func (*CaseExpr) exprNode() {}
254+
func (*CondExpr) exprNode() {}
255+
func (*DoubleQuoteExpr) exprNode() {}
256+
func (*ExistsExpr) exprNode() {}
257+
func (*ExtractExpr) exprNode() {}
258+
func (*FStringExpr) exprNode() {}
259+
func (*FuncNameExpr) exprNode() {}
260+
func (*GlobExpr) exprNode() {}
261+
func (*IDExpr) exprNode() {}
262+
func (*IndexExpr) exprNode() {}
263+
func (*IsNullExpr) exprNode() {}
264+
func (*LambdaExpr) exprNode() {}
265+
func (*MapExpr) exprNode() {}
266+
func (*Primitive) exprNode() {}
267+
func (*RecordExpr) exprNode() {}
268+
func (*RegexpExpr) exprNode() {}
269+
func (*SearchTermExpr) exprNode() {}
270+
func (*SetExpr) exprNode() {}
271+
func (*SliceExpr) exprNode() {}
272+
func (*SQLTimeExpr) exprNode() {}
273+
func (*TupleExpr) exprNode() {}
274+
func (*TypeValue) exprNode() {}
275+
func (*UnaryExpr) exprNode() {}
276+
func (*SubqueryExpr) exprNode() {}

compiler/ast/loc.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Package ast declares the types used to represent syntax trees for SuperSQL
2+
// queries.
3+
package ast
4+
5+
// This module is derived from the GO AST design pattern in
6+
// https://golang.org/pkg/go/ast/
7+
//
8+
// Copyright 2009 The Go Authors. All rights reserved.
9+
// Use of this source code is governed by a BSD-style
10+
// license that can be found in the LICENSE file.
11+
12+
type Node interface {
13+
Pos() int // Position of first character belonging to the node.
14+
End() int // Position of first character immediately after the node.
15+
}
16+
17+
type Loc struct {
18+
First int `json:"first"`
19+
Last int `json:"last"`
20+
}
21+
22+
func NewLoc(pos, end int) Loc {
23+
return Loc{pos, end}
24+
}
25+
26+
func (l Loc) Pos() int { return l.First }
27+
func (l Loc) End() int { return l.Last }

0 commit comments

Comments
 (0)