Skip to content

Commit

Permalink
Merge pull request #162 from vladlyt/master
Browse files Browse the repository at this point in the history
Add SQL bindings support
  • Loading branch information
smacker authored Jun 25, 2024
2 parents 0ac8d7d + 79334e4 commit a31a98a
Show file tree
Hide file tree
Showing 7 changed files with 548,125 additions and 0 deletions.
12 changes: 12 additions & 0 deletions _automation/grammars.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,17 @@
"reference": "v0.2.3",
"revision": "62516e8c78380e3b51d5b55727995d2c511436d8",
"updateBasedOn": "tag"
},
{
"language": "sql",
"url": "https://github.com/DerekStride/tree-sitter-sql",
"files": [
"parser.c",
"parser.h",
"scanner.c"
],
"reference": "gh-pages",
"revision": "89fd00d0aff3bc9985ac37caf362ec4fd9b2ba1d",
"updateBasedOn": "commit"
}
]
28 changes: 28 additions & 0 deletions _automation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ func (s *UpdateService) downloadGrammar(ctx context.Context, g *Grammar) {
s.downloadPhp(ctx, g)
case "markdown":
s.downloadMarkdown(ctx, g)
case "sql":
s.downloadSql(ctx, g)
default:
s.defaultGrammarDownload(ctx, g)
}
Expand Down Expand Up @@ -488,6 +490,32 @@ func (s *UpdateService) downloadYaml(ctx context.Context, g *Grammar) {
_ = os.WriteFile(fmt.Sprintf("%s/scanner.cc", g.Language), b, 0644)
}

// sql is special since its folder structure is different from the other ones
func (s *UpdateService) downloadSql(ctx context.Context, g *Grammar) {
fileMapping := map[string]string{
"parser.h": "tree_sitter/parser.h",
"scanner.c": "scanner.c",
"parser.c": "parser.c",
}

s.makeDir(ctx, fmt.Sprintf("%s/tree_sitter", g.Language))

url := g.ContentURL()
for _, f := range g.Files {
fp, ok := fileMapping[f]
if !ok {
logAndExit(getLogger(ctx), "mapping for file not found", "file", f)
}

s.downloadFile(
ctx,
fmt.Sprintf("%s/%s/src/%s", url, g.Revision, fp),
fmt.Sprintf("%s/%s", g.Language, fp),
nil,
)
}
}

func logAndExit(logger *Logger, msg string, args ...interface{}) {
logger.Error(msg, args...)
os.Exit(1)
Expand Down
15 changes: 15 additions & 0 deletions sql/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sql

//#include "tree_sitter/parser.h"
//TSLanguage *tree_sitter_sql();
import "C"
import (
"unsafe"

sitter "github.com/smacker/go-tree-sitter"
)

func GetLanguage() *sitter.Language {
ptr := unsafe.Pointer(C.tree_sitter_sql())
return sitter.NewLanguage(ptr)
}
28 changes: 28 additions & 0 deletions sql/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sql_test

import (
"context"
"testing"

sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/sql"
"github.com/stretchr/testify/assert"
)

const code = `SELECT * FROM my_table
WHERE count > 1
ORDER BY time DESC
LIMIT 5;`

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)))))`

func TestGrammar(t *testing.T) {
assert := assert.New(t)

n, err := sitter.ParseCtx(context.Background(), []byte(code), sql.GetLanguage())
assert.NoError(err)
assert.Equal(
expected,
n.String(),
)
}
Loading

0 comments on commit a31a98a

Please sign in to comment.