Skip to content

Commit d0ab41c

Browse files
committed
Added nunjucks grammar
1 parent 712019e commit d0ab41c

File tree

9 files changed

+17722
-0
lines changed

9 files changed

+17722
-0
lines changed

_automation/grammars.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@
117117
"revision": "a1bbe92a6370bb4c15386735fbda12f2b812a923",
118118
"updateBasedOn": "tag"
119119
},
120+
{
121+
"language": "nunjucks",
122+
"url": "https://github.com/mickeynp/tree-sitter-jinja2",
123+
"files": [
124+
"alloc.h",
125+
"parser.c",
126+
"parser.h",
127+
"scanner.c"
128+
],
129+
"reference": "main",
130+
"revision": "d9a9bf0d9cca79c6798308648d4f7040b4ca95c6",
131+
"updateBasedOn": "commit"
132+
},
120133
{
121134
"language": "javascript",
122135
"url": "https://github.com/tree-sitter/tree-sitter-javascript",

_automation/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ func (s *UpdateService) downloadGrammar(ctx context.Context, g *Grammar) {
269269
s.downloadPhp(ctx, g)
270270
case "markdown":
271271
s.downloadMarkdown(ctx, g)
272+
case "nunjucks":
273+
s.downloadNunjucks(ctx, g)
272274
case "sql":
273275
s.downloadSql(ctx, g)
274276
default:
@@ -579,6 +581,33 @@ func (s *UpdateService) downloadYaml(ctx context.Context, g *Grammar) {
579581
_ = os.WriteFile(fmt.Sprintf("%s/scanner.cc", g.Language), b, 0644)
580582
}
581583

584+
// jinja2 is special since its folder structure is different from the other ones
585+
func (s *UpdateService) downloadNunjucks(ctx context.Context, g *Grammar) {
586+
fileMapping := map[string]string{
587+
"alloc.h": "tree_sitter/alloc.h",
588+
"parser.h": "tree_sitter/parser.h",
589+
"scanner.c": "scanner.c",
590+
"parser.c": "parser.c",
591+
}
592+
593+
s.makeDir(ctx, fmt.Sprintf("%s/tree_sitter", g.Language))
594+
595+
url := g.ContentURL()
596+
for _, f := range g.Files {
597+
fp, ok := fileMapping[f]
598+
if !ok {
599+
logAndExit(getLogger(ctx), "mapping for file not found", "file", f)
600+
}
601+
602+
s.downloadFile(
603+
ctx,
604+
fmt.Sprintf("%s/%s/src/%s", url, g.Revision, fp),
605+
fmt.Sprintf("%s/%s", g.Language, fp),
606+
nil,
607+
)
608+
}
609+
}
610+
582611
// sql is special since its folder structure is different from the other ones
583612
func (s *UpdateService) downloadSql(ctx context.Context, g *Grammar) {
584613
fileMapping := map[string]string{

nunjucks/binding.go

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

nunjucks/binding_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package nunjucks_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
sitter "github.com/codepen/go-tree-sitter"
8+
"github.com/codepen/go-tree-sitter/nunjucks"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestGrammar(t *testing.T) {
14+
assert := assert.New(t)
15+
16+
content := `{% include "folder-2/header.njk" %}`
17+
rootNode, err := sitter.ParseCtx(context.Background(), []byte(content), nunjucks.GetLanguage())
18+
19+
assert.NoError(err)
20+
expected := `(fragment (fragment_statement (statement (include_statement template: (expression (string))))))`
21+
assert.Equal(expected, rootNode.String())
22+
}

0 commit comments

Comments
 (0)