Skip to content

Commit

Permalink
Add SCSS Grammar (#1)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
deequez authored Jun 15, 2022
1 parent e218c58 commit 9ce7f17
Show file tree
Hide file tree
Showing 6 changed files with 19,622 additions and 1 deletion.
16 changes: 16 additions & 0 deletions scss/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scss

//#include "parser.h"
//TSLanguage *tree_sitter_scss();
import "C"

import (
"unsafe"

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

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

import (
"context"
"testing"

sitter "github.com/codepen/go-tree-sitter"
"github.com/codepen/go-tree-sitter/scss"
"github.com/stretchr/testify/assert"
)

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

n, err := sitter.ParseCtx(context.Background(), []byte(`@import "compass"; $text-color: #555555; body { color: $text-color; }`), scss.GetLanguage())
assert.NoError(err)
assert.Equal(
"(stylesheet (import_statement (string_value)) (declaration (variable_name) (color_value)) (rule_set (selectors (tag_name)) (block (declaration (property_name) (variable_value)))))",
n.String(),
)
}
Loading

0 comments on commit 9ce7f17

Please sign in to comment.