-
Notifications
You must be signed in to change notification settings - Fork 27
/
parser.go
425 lines (374 loc) · 11.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package parser
import (
"fmt"
"github.com/jschaf/pggen/internal/ast"
"github.com/jschaf/pggen/internal/scanner"
"github.com/jschaf/pggen/internal/token"
goscan "go/scanner"
gotok "go/token"
"regexp"
"strconv"
"strings"
)
type parser struct {
file *gotok.File
errors goscan.ErrorList
scanner scanner.Scanner
src []byte // original source
// Tracing and debugging
mode Mode // parsing mode
trace bool // == (mode & Trace != 0)
indent int // indentation used for tracing output
// Comments
comments []*ast.CommentGroup
leadComment *ast.CommentGroup // last lead comment
// Next token
pos gotok.Pos // token position
tok token.Token // one token look-ahead
lit string // token literal
}
func (p *parser) init(fset *gotok.FileSet, filename string, src []byte, mode Mode) {
p.file = fset.AddFile(filename, -1, len(src))
eh := func(pos gotok.Position, msg string) { p.errors.Add(pos, msg) }
p.scanner.Init(p.file, src, eh)
p.src = src
p.mode = mode
p.trace = mode&Trace != 0 // for convenience (p.trace is used frequently)
p.next() // parse overall doc comments
}
// Parsing support
func (p *parser) printTrace(a ...interface{}) {
const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
const n = len(dots)
pos := p.file.Position(p.pos)
fmt.Printf("%5d:%3d: ", pos.Line, pos.Column)
i := 2 * p.indent
for i > n {
fmt.Print(dots)
i -= n
}
// i <= n
fmt.Print(dots[0:i])
fmt.Println(a...)
}
func trace(p *parser, msg string) *parser {
p.printTrace(msg, "(")
p.indent++
return p
}
// Usage pattern: defer un(trace(p, "..."))
func un(p *parser) {
p.indent--
p.printTrace(")")
}
// Advance to the next token.
func (p *parser) next0() {
// Because of one-token look-ahead, print the previous token when tracing as
// it provides a more readable output. The very first token (!p.pos.IsValid())
// is not initialized (it is token.ILLEGAL), so don't print it.
if p.trace && p.pos.IsValid() {
s := p.tok.String()
switch {
case p.tok == token.String || p.tok == token.QueryFragment:
lit := p.lit
// Simplify trace expression.
if lit != "" {
lit = `"` + lit + `"`
}
p.printTrace(s, lit)
default:
p.printTrace(s)
}
}
p.pos, p.tok, p.lit = p.scanner.Scan()
}
// Consume a comment and return it and the line on which it ends.
func (p *parser) consumeComment() (comment *ast.LineComment, endLine int) {
endLine = p.file.Line(p.pos)
comment = &ast.LineComment{Start: p.pos, Text: p.lit}
p.next0()
return
}
// Consume a group of adjacent comments, add it to the parser's comments list,
// and return it together with the line at which the last comment in the group
// ends. A non-comment token or an empty lines terminate a comment group.
func (p *parser) consumeCommentGroup(n int) (comments *ast.CommentGroup, endLine int) {
var list []*ast.LineComment
endLine = p.file.Line(p.pos)
for p.tok == token.LineComment && p.file.Line(p.pos) <= endLine+n {
var comment *ast.LineComment
comment, endLine = p.consumeComment()
list = append(list, comment)
}
// Add comment group to the comments list.
comments = &ast.CommentGroup{List: list}
p.comments = append(p.comments, comments)
return
}
// Advance to the next non-comment token. In the process, collect any comment
// groups encountered, and remember the last lead and line comments.
//
// A lead comment is a comment group that starts and ends in a line without any
// other tokens and that is followed by a non-comment token on the line
// immediately after the comment group.
//
// A line comment is a comment group that follows a non-comment token on the
// same line, and that has no tokens after it on the line where it ends.
//
// Lead comments may be considered documentation that is stored in the AST.
func (p *parser) next() {
p.leadComment = nil
prev := p.pos
p.next0()
if p.tok == token.LineComment {
var comment *ast.CommentGroup
var endLine int
if p.file.Line(p.pos) == p.file.Line(prev) {
// The comment is on same line as the previous token; it/ cannot be a
// lead comment but may be a line comment.
comment, endLine = p.consumeCommentGroup(0)
}
// consume successor comments, if any
for p.tok == token.LineComment {
comment, endLine = p.consumeCommentGroup(1)
}
if endLine+1 == p.file.Line(p.pos) {
// The next token is following on the line immediately after the
// comment group, thus the last comment group is a lead comment.
p.leadComment = comment
}
}
}
// A bailout panic is raised to indicate early termination.
type bailout struct{}
func (p *parser) error(pos gotok.Pos, msg string) {
epos := p.file.Position(pos)
// Discard errors reported on the same line as the last recorded error and
// stop parsing if there are more than 10 errors.
n := len(p.errors)
if n > 0 && p.errors[n-1].Pos.Line == epos.Line {
return // discard - likely a spurious error
}
if n > 10 {
panic(bailout{})
}
p.errors.Add(epos, msg)
}
func (p *parser) expect(tok token.Token) gotok.Pos {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, "'"+tok.String()+"'")
}
p.next() // make progress
return pos
}
func (p *parser) errorExpected(pos gotok.Pos, msg string) {
msg = "expected " + msg
if pos == p.pos {
// The error happened at the current position; make the error message more
// specific.
msg += ", found '" + p.tok.String() + "'"
}
p.error(pos, msg)
}
// Regexp to extract query annotations that control output.
var annotationRegexp = regexp.MustCompile(`name: ([a-zA-Z0-9_$]+)[ \t]+(:many|:one|:exec)[ \t]*(.*)`)
func (p *parser) parseQuery() ast.Query {
if p.trace {
defer un(trace(p, "Query"))
}
doc := p.leadComment
sql := &strings.Builder{}
pos := p.pos
names := make([]argPos, 0, 4) // all pggen.arg names in order, can be duplicated
for p.tok != token.Semicolon {
if p.tok == token.EOF || p.tok == token.Illegal {
p.error(p.pos, "unterminated query (no semicolon): "+string(p.src[pos:p.pos]))
return &ast.BadQuery{From: pos, To: p.pos}
}
hasPggenArg := strings.HasSuffix(p.lit, "pggen.arg(") ||
strings.HasSuffix(p.lit, "pggen.arg (")
if p.tok == token.QueryFragment && hasPggenArg {
arg, ok := p.parsePggenArg()
if !ok {
return &ast.BadQuery{From: pos, To: p.pos}
}
arg.lo -= int(pos) - 1 // adjust lo,hi to be relative to query start
arg.hi -= int(pos) - 1 // subtract 1 because pos is 1-based
names = append(names, arg)
// Don't consume last query fragment that has closing paren ")" because
// the fragment might contain the start of another pggen.arg.
continue
}
p.next()
}
semi := p.pos
p.expect(token.Semicolon)
sql.Write(p.src[pos-1 : semi])
// Extract annotations
if doc == nil || doc.List == nil || len(doc.List) == 0 {
p.error(pos, "no comment preceding query")
return &ast.BadQuery{From: pos, To: p.pos}
}
last := doc.List[len(doc.List)-1]
annotations := annotationRegexp.FindStringSubmatch(last.Text)
if annotations == nil {
p.error(pos, "no 'name: <name> :<type>' token found in comment before query; comment line: \""+last.Text+`"`)
return &ast.BadQuery{From: pos, To: p.pos}
}
args := annotations[3]
pragmas, err := parsePragmas(args)
if err != nil {
p.error(pos, "invalid query pragma: "+err.Error())
return &ast.BadQuery{From: pos, To: p.pos}
}
templateSQL := sql.String()
preparedSQL, params := prepareSQL(templateSQL, names)
return &ast.SourceQuery{
Name: annotations[1],
Doc: doc,
Start: pos,
SourceSQL: templateSQL,
PreparedSQL: preparedSQL,
ParamNames: params,
ResultKind: ast.ResultKind(annotations[2]),
Pragmas: pragmas,
Semi: semi,
}
}
// parsePragmas parses optional pragmas for a query like proto-type=foo.bar.Msg.
func parsePragmas(allPragmas string) (ast.Pragmas, error) {
if allPragmas == "" {
return ast.Pragmas{}, nil
}
ss := strings.Fields(allPragmas)
qp := ast.Pragmas{}
for _, s := range ss {
arg := strings.Split(s, "=")
if len(arg) != 2 {
return ast.Pragmas{}, fmt.Errorf("expected arg format x=y; got %s", s)
}
key, val := arg[0], arg[1]
switch key {
case "proto-type":
p, err := validateProtoMsgType(val)
if err != nil {
return ast.Pragmas{}, err
}
qp.ProtobufType = p
default:
return ast.Pragmas{}, fmt.Errorf("unsupported pramga %q", key)
}
}
return qp, nil
}
// validateProtoMsgType checks that val is a valid message name.
// https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#identifiers
func validateProtoMsgType(val string) (string, error) {
isStart := true
for i, v := range val {
isLast := i == len(val)-1
switch {
case ('a' <= v && v <= 'z') || ('A' <= v && v <= 'Z'):
isStart = false
case ('0' <= v && v <= '9') || v == '_':
if isStart {
return "", fmt.Errorf("invalid proto-type, proto package cannot start with 0-9 or _; got %q", val)
}
case v == '.':
if isStart || isLast {
return "", fmt.Errorf("invalid proto-type, proto package cannot start or end with '.'; got %q", val)
}
isStart = true
default:
return "", fmt.Errorf("invalid proto-type, proto message must only contain [a-zA-Z0-9.-]; got %q", val)
}
}
return val, nil
}
// argPos is the name and position of expression like pggen.arg('foo').
type argPos struct {
lo, hi int
name string
}
// parsePggenArg parses the name from: pggen.arg('foo') and pos for the start
// and end.
func (p *parser) parsePggenArg() (argPos, bool) {
lo := int(p.pos) + strings.LastIndex(p.lit, "pggen") - 1
p.next() // consume query fragment that contains "pggen.arg("
if p.tok != token.String {
p.error(p.pos, `expected string literal after "pggen.arg("`)
return argPos{}, false
}
if len(p.lit) < 3 || p.lit[0] != '\'' || p.lit[len(p.lit)-1] != '\'' {
p.error(p.pos, `expected single-quoted string literal after "pggen.arg("`)
return argPos{}, false
}
name := p.lit[1 : len(p.lit)-1]
p.next() // consume string literal
if p.tok != token.QueryFragment {
p.error(p.pos, `expected query fragment after parsing pggen.arg string`)
return argPos{}, false
}
if !strings.HasPrefix(p.lit, ")") {
p.error(p.pos, `expected closing paren ")" after parsing pggen.arg string`)
return argPos{}, false
}
hi := int(p.pos)
return argPos{lo: lo, hi: hi, name: name}, true
}
// prepareSQL replaces each pggen.arg with the $n, respecting the order that the
// arg first appeared. Args with the same name use the same $n.
func prepareSQL(sql string, args []argPos) (string, []string) {
if len(args) == 0 {
return sql, nil
}
// Figure out order of each params.
paramOrders := make(map[string]int, len(args))
params := make([]string, 0, len(args))
idx := 1
for _, arg := range args {
if _, ok := paramOrders[arg.name]; !ok {
params = append(params, arg.name)
paramOrders[arg.name] = idx
idx++
}
}
// Replace each pggen.arg with the prepare order, like $1. We're not using
// strings.NewReplacer because pggen.arg might appear in a comment.
bs := []byte(sql)
sb := &strings.Builder{}
sb.Grow(len(sql))
prev := 0
for _, arg := range args {
sb.Write(bs[prev:arg.lo])
sb.WriteByte('$')
sb.WriteString(strconv.Itoa(paramOrders[arg.name]))
prev = arg.hi
}
sb.Write(bs[prev:])
return sb.String(), params
}
// ----------------------------------------------------------------------------
// Source files
func (p *parser) parseFile() *ast.File {
if p.trace {
defer un(trace(p, "File"))
}
// Don't bother parsing the rest if we had errors scanning the first token.
// Likely not a query source file at all.
if p.errors.Len() != 0 {
return nil
}
// Opening comment
doc := p.leadComment
var queries []ast.Query
for p.tok != token.EOF && p.tok != token.Illegal {
queries = append(queries, p.parseQuery())
}
return &ast.File{
Doc: doc,
Queries: queries,
Comments: p.comments,
}
}