Skip to content

Commit 8f800de

Browse files
committed
Give the README a runnable example and links to the reference
The usage section was a single call with no imports and no output, which shows the entry point but not what you get back. The example is now a whole program: parse, walk the tree for tables and bind parameters, and handle a rejection. Its output is what it actually prints. Also a pkg.go.dev badge and links to the four packages, and the corpus count in Status, which still said 20,971 from before extraction widened to every script in the pinned tree. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JzBeCg7rjweVW3uGPg5G7T
1 parent 7f940f2 commit 8f800de

1 file changed

Lines changed: 69 additions & 6 deletions

File tree

README.md

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,83 @@ meyer is being built to replace the ANTLR-generated SQLite parser in
1010
[sqlc](https://github.com/sqlc-dev/sqlc). See [PLAN.md](PLAN.md) for the
1111
architecture and [CLAUDE.md](CLAUDE.md) for the development workflow.
1212

13+
[![Go Reference](https://pkg.go.dev/badge/github.com/sqlc-dev/meyer.svg)](https://pkg.go.dev/github.com/sqlc-dev/meyer)
14+
1315
## Usage
1416

17+
```sh
18+
go get github.com/sqlc-dev/meyer
19+
```
20+
1521
```go
16-
stmts, err := parser.Parse(ctx, strings.NewReader("SELECT id FROM users WHERE id = ?"))
22+
package main
23+
24+
import (
25+
"errors"
26+
"fmt"
27+
28+
"github.com/sqlc-dev/meyer/ast"
29+
"github.com/sqlc-dev/meyer/parser"
30+
)
31+
32+
func main() {
33+
const src = `SELECT u.name, count(o.id) FROM users u
34+
JOIN orders o ON o.user_id = u.id
35+
WHERE u.created_at > :since`
36+
37+
stmts, err := parser.ParseString(src)
38+
if err != nil {
39+
var perr *parser.Error
40+
if errors.As(err, &perr) {
41+
fmt.Printf("%s at byte %d\n", perr.Message, perr.Offset)
42+
}
43+
return
44+
}
45+
46+
for _, stmt := range stmts {
47+
ast.Walk(stmt, func(n ast.Node) bool {
48+
switch n := n.(type) {
49+
case *ast.TableRef:
50+
if n.Name != nil {
51+
fmt.Println("table:", n.Name.Name.Name)
52+
}
53+
case *ast.BindParam:
54+
fmt.Printf("param: %s at %d:%d\n", n.Raw, n.Pos(), n.End())
55+
}
56+
return true
57+
})
58+
}
59+
}
1760
```
1861

19-
`ParseString` and `ParseStatement` take a string; `ParseExpr` parses a single
20-
expression. A rejected input returns a `*parser.Error` carrying SQLite's
21-
exact message and the byte offset of the fault.
62+
```
63+
table: users
64+
table: orders
65+
param: :since at 100:106
66+
```
67+
68+
`ParseString` and `ParseStatement` take a string, `Parse` takes an
69+
`io.Reader`, and `ParseExpr` parses a single expression. A rejected input
70+
returns a `*parser.Error` carrying SQLite's exact message and the byte offset
71+
of the fault; its `Error()` renders as `line:column: message`.
72+
73+
```go
74+
_, err := parser.ParseString("SELECT FROM t")
75+
fmt.Println(err) // 1:8: near "FROM": syntax error
76+
```
2277

2378
Every node embeds `ast.Span`, so `Pos()` and `End()` give byte offsets into
2479
the original input — sqlc slices the source with them to find `-- name:`
2580
comments and to report errors, so they are load-bearing rather than
26-
diagnostic.
81+
diagnostic. `ast.String` and `ast.Statements` render a tree back to SQL,
82+
which is a re-parseable rendering rather than a formatter.
83+
84+
The full API is on
85+
[pkg.go.dev](https://pkg.go.dev/github.com/sqlc-dev/meyer): `parser` for the
86+
entry points, [`ast`](https://pkg.go.dev/github.com/sqlc-dev/meyer/ast) for
87+
the node set, and [`lexer`](https://pkg.go.dev/github.com/sqlc-dev/meyer/lexer)
88+
and [`token`](https://pkg.go.dev/github.com/sqlc-dev/meyer/token) if you want
89+
the token stream on its own.
2790

2891
To see what the parser did with something:
2992

@@ -36,7 +99,7 @@ go run ./cmd/debug-parse -render -f query.sql # re-rendered SQL
3699
## Status
37100

38101
The parser covers the whole grammar of the pinned SQLite release and passes
39-
the full corpus: **20,971 of 20,971 cases**, extracted from every test script
102+
the full corpus: **21,326 of 21,326 cases**, extracted from every test script
40103
in SQLite's own test suite.
41104

42105
Still to come, from [PLAN.md](PLAN.md): the sqlc integration itself, and the

0 commit comments

Comments
 (0)