Skip to content

Commit 9ce7f17

Browse files
authored
Add SCSS Grammar (#1)
If an open source Tree-sitter parser is available, we can run `./vendor.sh download` to download the C code thats needed to add a grammar. I'm using [serenadeai/tree-sitter-scss](https://github.com/serenadeai/tree-sitter-scss) to add the SCSS grammar. # One time updates The `vendor.sh` script is using features from the latest version of Bash. MacOS includes an older version of Bash so I updated my system to v5. # Per Grammar ### Update vendor script Add grammar: name; version / tag of repo to download; files to download `["scss"]="v1.0.0;parser.c;scanner.c"` Add repository `["scss"]="serenadeai/tree-sitter-scss"` ### Download C code From root of repo run: `./vendor.sh download` ### Add Go binding In new grammar directory add: - `binding.go` - `binding_test.go` Run your test to verify the new grammar works! # References - [Install Bash 5 on macOS](https://scriptingosx.com/2019/02/install-bash-5-on-macos/) - [Creating parsers | Tree-sitter](https://tree-sitter.github.io/tree-sitter/creating-parsers) - smacker#58
1 parent e218c58 commit 9ce7f17

File tree

6 files changed

+19622
-1
lines changed

6 files changed

+19622
-1
lines changed

scss/binding.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package scss
2+
3+
//#include "parser.h"
4+
//TSLanguage *tree_sitter_scss();
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_scss())
15+
return sitter.NewLanguage(ptr)
16+
}

scss/binding_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package scss_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
sitter "github.com/codepen/go-tree-sitter"
8+
"github.com/codepen/go-tree-sitter/scss"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGrammar(t *testing.T) {
13+
assert := assert.New(t)
14+
15+
n, err := sitter.ParseCtx(context.Background(), []byte(`@import "compass"; $text-color: #555555; body { color: $text-color; }`), scss.GetLanguage())
16+
assert.NoError(err)
17+
assert.Equal(
18+
"(stylesheet (import_statement (string_value)) (declaration (variable_name) (color_value)) (rule_set (selectors (tag_name)) (block (declaration (property_name) (variable_value)))))",
19+
n.String(),
20+
)
21+
}

0 commit comments

Comments
 (0)