Skip to content

Commit

Permalink
add sql support
Browse files Browse the repository at this point in the history
  • Loading branch information
vladlyt committed May 17, 2024
1 parent c5d1f3f commit 4b10910
Show file tree
Hide file tree
Showing 7 changed files with 544,596 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 @@ -322,5 +322,17 @@
"reference": "v0.5.0",
"revision": "6129a83eeec7d6070b1c0567ec7ce3509ead607c",
"updateBasedOn": "tag"
},
{
"language": "sql",
"url": "https://github.com/DerekStride/tree-sitter-sql",
"files": [
"parser.c",
"parser.h",
"scanner.c"
],
"reference": "gh-pages",
"revision": "25f94f998de79bae9df28add9782f9ea6ea0e2b8",
"updateBasedOn": "commit"
}
]
28 changes: 28 additions & 0 deletions _automation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ func (s *UpdateService) downloadGrammar(ctx context.Context, g *Grammar) {
s.downloadYaml(ctx, g)
case "php":
s.downloadPhp(ctx, g)
case "sql":
s.downloadSql(ctx, g)
default:
s.defaultGrammarDownload(ctx, g)
}
Expand Down Expand Up @@ -458,6 +460,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 4b10910

Please sign in to comment.