Skip to content

Commit a31a98a

Browse files
authored
Merge pull request #162 from vladlyt/master
Add SQL bindings support
2 parents 0ac8d7d + 79334e4 commit a31a98a

File tree

7 files changed

+548125
-0
lines changed

7 files changed

+548125
-0
lines changed

_automation/grammars.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,17 @@
333333
"reference": "v0.2.3",
334334
"revision": "62516e8c78380e3b51d5b55727995d2c511436d8",
335335
"updateBasedOn": "tag"
336+
},
337+
{
338+
"language": "sql",
339+
"url": "https://github.com/DerekStride/tree-sitter-sql",
340+
"files": [
341+
"parser.c",
342+
"parser.h",
343+
"scanner.c"
344+
],
345+
"reference": "gh-pages",
346+
"revision": "89fd00d0aff3bc9985ac37caf362ec4fd9b2ba1d",
347+
"updateBasedOn": "commit"
336348
}
337349
]

_automation/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ func (s *UpdateService) downloadGrammar(ctx context.Context, g *Grammar) {
241241
s.downloadPhp(ctx, g)
242242
case "markdown":
243243
s.downloadMarkdown(ctx, g)
244+
case "sql":
245+
s.downloadSql(ctx, g)
244246
default:
245247
s.defaultGrammarDownload(ctx, g)
246248
}
@@ -488,6 +490,32 @@ func (s *UpdateService) downloadYaml(ctx context.Context, g *Grammar) {
488490
_ = os.WriteFile(fmt.Sprintf("%s/scanner.cc", g.Language), b, 0644)
489491
}
490492

493+
// sql is special since its folder structure is different from the other ones
494+
func (s *UpdateService) downloadSql(ctx context.Context, g *Grammar) {
495+
fileMapping := map[string]string{
496+
"parser.h": "tree_sitter/parser.h",
497+
"scanner.c": "scanner.c",
498+
"parser.c": "parser.c",
499+
}
500+
501+
s.makeDir(ctx, fmt.Sprintf("%s/tree_sitter", g.Language))
502+
503+
url := g.ContentURL()
504+
for _, f := range g.Files {
505+
fp, ok := fileMapping[f]
506+
if !ok {
507+
logAndExit(getLogger(ctx), "mapping for file not found", "file", f)
508+
}
509+
510+
s.downloadFile(
511+
ctx,
512+
fmt.Sprintf("%s/%s/src/%s", url, g.Revision, fp),
513+
fmt.Sprintf("%s/%s", g.Language, fp),
514+
nil,
515+
)
516+
}
517+
}
518+
491519
func logAndExit(logger *Logger, msg string, args ...interface{}) {
492520
logger.Error(msg, args...)
493521
os.Exit(1)

sql/binding.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sql
2+
3+
//#include "tree_sitter/parser.h"
4+
//TSLanguage *tree_sitter_sql();
5+
import "C"
6+
import (
7+
"unsafe"
8+
9+
sitter "github.com/smacker/go-tree-sitter"
10+
)
11+
12+
func GetLanguage() *sitter.Language {
13+
ptr := unsafe.Pointer(C.tree_sitter_sql())
14+
return sitter.NewLanguage(ptr)
15+
}

sql/binding_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sql_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
sitter "github.com/smacker/go-tree-sitter"
8+
"github.com/smacker/go-tree-sitter/sql"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
const code = `SELECT * FROM my_table
13+
WHERE count > 1
14+
ORDER BY time DESC
15+
LIMIT 5;`
16+
17+
const expected = `(program (statement (select (keyword_select) (select_expression (term value: (all_fields)))) (from (keyword_from) (relation (object_reference name: (identifier))) (where (keyword_where) predicate: (binary_expression left: (field name: (identifier)) right: (literal))) (order_by (keyword_order) (keyword_by) (order_target (field name: (identifier)) (direction (keyword_desc)))) (limit (keyword_limit) (literal)))))`
18+
19+
func TestGrammar(t *testing.T) {
20+
assert := assert.New(t)
21+
22+
n, err := sitter.ParseCtx(context.Background(), []byte(code), sql.GetLanguage())
23+
assert.NoError(err)
24+
assert.Equal(
25+
expected,
26+
n.String(),
27+
)
28+
}

0 commit comments

Comments
 (0)