-
Notifications
You must be signed in to change notification settings - Fork 13
/
parser.go
293 lines (251 loc) · 7.8 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package ast
import (
"fmt"
"log/slog"
"strings"
antlr "github.com/antlr4-go/antlr/v4"
"github.com/neilotoole/sq/libsq/ast/internal/slq"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/stringz"
)
// parseSLQ processes SLQ input text according to the rules of the SQL grammar,
// and returns a parse tree. It executes both lexer and parser phases.
func parseSLQ(log *slog.Logger, input string) (*slq.QueryContext, error) {
lex := slq.NewSLQLexer(antlr.NewInputStream(input))
lex.RemoveErrorListeners() // the generated lexer has default listeners we don't want
lexErrs := &antlrErrorListener{name: "lexer", log: log}
lex.AddErrorListener(lexErrs)
p := slq.NewSLQParser(antlr.NewCommonTokenStream(lex, 0))
p.RemoveErrorListeners() // the generated parser has default listeners we don't want
parseErrs := &antlrErrorListener{name: "parser", log: log}
p.AddErrorListener(parseErrs)
qCtx := p.Query()
if err := lexErrs.error(); err != nil {
return nil, errz.Err(err)
}
if err := parseErrs.error(); err != nil {
return nil, errz.Err(err)
}
return qCtx.(*slq.QueryContext), nil
}
var _ antlr.ErrorListener = (*antlrErrorListener)(nil)
type antlrErrorListener struct {
err error
log *slog.Logger
name string
errs []string
warnings []string
}
// SyntaxError implements antlr.ErrorListener.
//
//nolint:revive
func (el *antlrErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{},
line, column int, msg string, e antlr.RecognitionException,
) {
text := fmt.Sprintf("%s: syntax error: [%d:%d] %s", el.name, line, column, msg)
el.errs = append(el.errs, text)
}
// ReportAmbiguity implements antlr.ErrorListener.
//
//nolint:revive
func (el *antlrErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA,
startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs *antlr.ATNConfigSet,
) {
tok := recognizer.GetCurrentToken()
text := fmt.Sprintf("%s: syntax ambiguity: [%d:%d]", el.name, startIndex, stopIndex)
text = text + " >>" + tok.GetText() + "<<"
el.warnings = append(el.warnings, text)
}
// ReportAttemptingFullContext implements antlr.ErrorListener.
//
//nolint:revive
func (el *antlrErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA,
startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs *antlr.ATNConfigSet,
) {
text := fmt.Sprintf("%s: attempting full context: [%d:%d]", el.name, startIndex, stopIndex)
el.warnings = append(el.warnings, text)
}
// ReportContextSensitivity implements antlr.ErrorListener.
//
//nolint:revive
func (el *antlrErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA,
startIndex, stopIndex, prediction int, configs *antlr.ATNConfigSet,
) {
text := fmt.Sprintf("%s: context sensitivity: [%d:%d]", el.name, startIndex, stopIndex)
el.warnings = append(el.warnings, text)
}
func (el *antlrErrorListener) error() error {
if el.err == nil && len(el.errs) > 0 {
msg := strings.Join(el.errs, "\n")
el.err = &parseError{msg: msg}
}
return el.err
}
func (el *antlrErrorListener) String() string {
if len(el.errs)+len(el.warnings) == 0 {
return el.name + ": no issues"
}
strs := make([]string, 0, len(el.errs)+len(el.warnings))
strs = append(strs, el.errs...)
strs = append(strs, el.warnings...)
return strings.Join(strs, "\n")
}
// parseError represents an error in lexing/parsing input.
type parseError struct {
msg string
// TODO: parse error should include more detail, such as
// the offending token, position, etc.
}
// Error implements error.
func (p *parseError) Error() string {
return p.msg
}
var _ slq.SLQVisitor = (*parseTreeVisitor)(nil)
// parseTreeVisitor implements slq.SLQVisitor to
// generate the preliminary AST.
type parseTreeVisitor struct {
log *slog.Logger
// cur is the currently-active node of the AST.
// This value is modified as the tree is descended.
cur Node
ast *AST
}
// using is a convenience function that sets v.cur to cur,
// executes fn, and then restores v.cur to its previous value.
// The type of the returned value is declared as "any" instead of
// error, because that's the generated antlr code returns "any".
func (v *parseTreeVisitor) using(node Node, fn func() any) any {
prev := v.cur
v.cur = node
defer func() { v.cur = prev }()
return fn()
}
// Visit implements antlr.ParseTreeVisitor.
func (v *parseTreeVisitor) Visit(ctx antlr.ParseTree) any {
v.log.Debug("Visit",
lga.Type, stringz.Type(ctx),
lga.Text, ctx.GetText(),
)
switch ctx := ctx.(type) {
case *slq.SegmentContext:
return v.VisitSegment(ctx)
case *slq.ElementContext:
return v.VisitElement(ctx)
case *slq.HandleContext:
return v.VisitHandle(ctx)
case *slq.HandleTableContext:
return v.VisitHandleTable(ctx)
case *slq.SelectorContext:
return v.VisitSelector(ctx)
case *slq.FuncElementContext:
return v.VisitFuncElement(ctx)
case *slq.FuncContext:
return v.VisitFunc(ctx)
case *slq.FuncNameContext:
return v.VisitFuncName(ctx)
case *slq.JoinContext:
return v.VisitJoin(ctx)
case *slq.AliasContext:
return v.VisitAlias(ctx)
case *slq.JoinTableContext:
return v.VisitJoinTable(ctx)
case *slq.RowRangeContext:
return v.VisitRowRange(ctx)
case *slq.ExprElementContext:
return v.VisitExprElement(ctx)
case *slq.ExprContext:
return v.VisitExpr(ctx)
case *slq.GroupByContext:
return v.VisitGroupBy(ctx)
case *slq.GroupByTermContext:
return v.VisitGroupByTerm(ctx)
case *slq.HavingContext:
return v.VisitHaving(ctx)
case *slq.OrderByContext:
return v.VisitOrderBy(ctx)
case *slq.OrderByTermContext:
return v.VisitOrderByTerm(ctx)
case *slq.LiteralContext:
return v.VisitLiteral(ctx)
case *antlr.TerminalNodeImpl:
return v.VisitTerminal(ctx)
case *slq.SelectorElementContext:
return v.VisitSelectorElement(ctx)
case *slq.UniqueFuncContext:
return v.VisitUniqueFunc(ctx)
case *slq.CountFuncContext:
return v.VisitCountFunc(ctx)
case *slq.WhereContext:
return v.VisitWhere(ctx)
case *slq.ArgContext:
return v.VisitArg(ctx)
}
// should never be reached
return errorf("unknown node type: %T", ctx)
}
// VisitChildren implements antlr.ParseTreeVisitor.
func (v *parseTreeVisitor) VisitChildren(ctx antlr.RuleNode) any {
for _, child := range ctx.GetChildren() {
tree, ok := child.(antlr.ParseTree)
if !ok {
return errorf("unknown child node type: %T(%s)", child, child.GetPayload())
}
err := v.Visit(tree)
if err != nil {
return err
}
}
return nil
}
// VisitQuery implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitQuery(ctx *slq.QueryContext) any {
v.ast = &AST{}
v.ast.ctx = ctx
v.ast.text = ctx.GetText()
v.cur = v.ast
for _, seg := range ctx.AllSegment() {
err := v.VisitSegment(seg.(*slq.SegmentContext))
if err != nil {
return err
}
}
return nil
}
// VisitElement implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitElement(ctx *slq.ElementContext) any {
return v.VisitChildren(ctx)
}
// VisitStmtList implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitStmtList(_ *slq.StmtListContext) any {
return nil // not using StmtList just yet
}
// VisitUnaryOperator implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitUnaryOperator(_ *slq.UnaryOperatorContext) any {
return nil
}
// VisitTerminal implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitTerminal(ctx antlr.TerminalNode) any {
val := ctx.GetText()
if isOperator(val) {
op := &OperatorNode{}
op.ctx = ctx
op.text = ctx.GetText()
err := op.SetParent(v.cur)
if err != nil {
return err
}
err = v.cur.AddChild(op)
if err != nil {
return err
}
return nil
}
// Unknown terminal, but that's not a problem.
return nil
}
// VisitErrorNode implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitErrorNode(ctx antlr.ErrorNode) any {
v.log.Debug("Error node", lga.Val, ctx.GetText())
return nil
}