-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d59be04
Showing
27 changed files
with
10,069 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Build and Test | ||
|
||
on: | ||
push: | ||
branches: [master] | ||
pull_request: | ||
|
||
jobs: | ||
build-and-test: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
submodules: "recursive" | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23" | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "tree-sitter"] | ||
path = tree-sitter | ||
url = [email protected]:tree-sitter/tree-sitter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <stdlib.h> | ||
|
||
extern void *go_malloc(size_t size); | ||
extern void *go_calloc(size_t num, size_t size); | ||
extern void *go_realloc(void *ptr, size_t size); | ||
extern void go_free(void *ptr); | ||
|
||
void *c_malloc_fn(size_t size) { return go_malloc(size); } | ||
|
||
void *c_calloc_fn(size_t num, size_t size) { return go_calloc(num, size); } | ||
|
||
void *c_realloc_fn(void *ptr, size_t size) { return go_realloc(ptr, size); } | ||
|
||
void c_free_fn(void *ptr) { go_free(ptr); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package tree_sitter | ||
|
||
/* | ||
#cgo CFLAGS: -I${SRCDIR}/tree-sitter/lib/include -I${SRCDIR}/tree-sitter/lib/src -std=c11 | ||
#include <tree_sitter/api.h> | ||
#include "allocator.h" | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
var ( | ||
malloc_fn atomic.Value | ||
calloc_fn atomic.Value | ||
realloc_fn atomic.Value | ||
free_fn atomic.Value | ||
) | ||
|
||
func init() { | ||
malloc_fn.Store(func(size C.size_t) unsafe.Pointer { | ||
return C.malloc(size) | ||
}) | ||
calloc_fn.Store(func(num, size C.size_t) unsafe.Pointer { | ||
return C.calloc(num, size) | ||
}) | ||
realloc_fn.Store(func(ptr unsafe.Pointer, size C.size_t) unsafe.Pointer { | ||
return C.realloc(ptr, size) | ||
}) | ||
free_fn.Store(func(ptr unsafe.Pointer) { | ||
C.free(ptr) | ||
}) | ||
SetAllocator(nil, nil, nil, nil) | ||
} | ||
|
||
//export go_malloc | ||
func go_malloc(size C.size_t) unsafe.Pointer { | ||
return malloc_fn.Load().(func(C.size_t) unsafe.Pointer)(size) | ||
} | ||
|
||
//export go_calloc | ||
func go_calloc(num, size C.size_t) unsafe.Pointer { | ||
return calloc_fn.Load().(func(C.size_t, C.size_t) unsafe.Pointer)(num, size) | ||
} | ||
|
||
//export go_realloc | ||
func go_realloc(ptr unsafe.Pointer, size C.size_t) unsafe.Pointer { | ||
return realloc_fn.Load().(func(unsafe.Pointer, C.size_t) unsafe.Pointer)(ptr, size) | ||
} | ||
|
||
//export go_free | ||
func go_free(ptr unsafe.Pointer) { | ||
free_fn.Load().(func(unsafe.Pointer))(ptr) | ||
} | ||
|
||
// Sets the memory allocation functions that the core library should use. | ||
func SetAllocator( | ||
newMalloc func(size uint) unsafe.Pointer, | ||
newCalloc func(num, size uint) unsafe.Pointer, | ||
newRealloc func(ptr unsafe.Pointer, size uint) unsafe.Pointer, | ||
newFree func(ptr unsafe.Pointer), | ||
) { | ||
if newMalloc != nil { | ||
malloc_fn.Store(func(size C.size_t) unsafe.Pointer { | ||
return newMalloc(uint(size)) | ||
}) | ||
} else { | ||
malloc_fn.Store(func(size C.size_t) unsafe.Pointer { | ||
return C.malloc(size) | ||
}) | ||
} | ||
|
||
if newCalloc != nil { | ||
calloc_fn.Store(func(num, size C.size_t) unsafe.Pointer { | ||
return newCalloc(uint(num), uint(size)) | ||
}) | ||
} else { | ||
calloc_fn.Store(func(num, size C.size_t) unsafe.Pointer { | ||
return C.calloc(num, size) | ||
}) | ||
} | ||
|
||
if newRealloc != nil { | ||
realloc_fn.Store(func(ptr unsafe.Pointer, size C.size_t) unsafe.Pointer { | ||
return newRealloc(ptr, uint(size)) | ||
}) | ||
} else { | ||
realloc_fn.Store(func(ptr unsafe.Pointer, size C.size_t) unsafe.Pointer { | ||
return C.realloc(ptr, size) | ||
}) | ||
} | ||
|
||
if newFree != nil { | ||
free_fn.Store(func(ptr unsafe.Pointer) { | ||
newFree(ptr) | ||
}) | ||
} else { | ||
free_fn.Store(func(ptr unsafe.Pointer) { | ||
C.free(ptr) | ||
}) | ||
} | ||
|
||
C.ts_set_allocator( | ||
(*[0]byte)(C.c_malloc_fn), | ||
(*[0]byte)(C.c_calloc_fn), | ||
(*[0]byte)(C.c_realloc_fn), | ||
(*[0]byte)(C.c_free_fn), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <stdlib.h> | ||
|
||
void *c_malloc_fn(size_t size); | ||
|
||
void *c_calloc_fn(size_t num, size_t size); | ||
|
||
void *c_realloc_fn(void *ptr, size_t size); | ||
|
||
void c_free_fn(void *ptr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build linux || darwin | ||
|
||
package tree_sitter | ||
|
||
/* | ||
#include <unistd.h> | ||
*/ | ||
import "C" | ||
|
||
// Wrapper for Unix systems | ||
func dupeFD(fd uintptr) int { | ||
return int(C.dup(C.int(fd))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build windows | ||
|
||
package tree_sitter | ||
|
||
/* | ||
#include <windows.h> | ||
HANDLE _ts_dup(HANDLE handle); | ||
*/ | ||
import "C" | ||
import "unsafe" | ||
|
||
// Wrapper for Windows systems | ||
func dupeFD(handle uintptr) uintptr { | ||
hHandle := C.HANDLE(unsafe.Pointer(handle)) | ||
return uintptr(unsafe.Pointer(C._ts_dup(hHandle))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tree_sitter | ||
|
||
/* | ||
#cgo CFLAGS: -I${SRCDIR}/tree-sitter/lib/include -I${SRCDIR}/tree-sitter/lib/src -std=c11 | ||
#include <tree_sitter/api.h> | ||
*/ | ||
import "C" | ||
|
||
type InputEdit struct { | ||
StartByte uint | ||
OldEndByte uint | ||
NewEndByte uint | ||
StartPosition Point | ||
OldEndPosition Point | ||
NewEndPosition Point | ||
} | ||
|
||
func (i *InputEdit) ToTSInputEdit() *C.TSInputEdit { | ||
return &C.TSInputEdit{ | ||
start_byte: C.uint(i.StartByte), | ||
old_end_byte: C.uint(i.OldEndByte), | ||
new_end_byte: C.uint(i.NewEndByte), | ||
start_point: i.StartPosition.toTSPoint(), | ||
old_end_point: i.OldEndPosition.toTSPoint(), | ||
new_end_point: i.NewEndPosition.toTSPoint(), | ||
} | ||
} | ||
|
||
func (i *InputEdit) FromTSInputEdit(edit *C.TSInputEdit) { | ||
i.StartByte = uint(edit.start_byte) | ||
i.OldEndByte = uint(edit.old_end_byte) | ||
i.NewEndByte = uint(edit.new_end_byte) | ||
i.StartPosition.fromTSPoint(edit.start_point) | ||
i.OldEndPosition.fromTSPoint(edit.old_end_point) | ||
i.NewEndPosition.fromTSPoint(edit.new_end_point) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package tree_sitter_test | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
. "github.com/tree-sitter/go-tree-sitter" | ||
) | ||
|
||
type testEdit struct { | ||
insertedText []byte | ||
position uint | ||
deletedLength uint | ||
} | ||
|
||
func performEdit(tree *Tree, input *[]byte, edit *testEdit) (InputEdit, error) { | ||
startByte := edit.position | ||
oldEndByte := edit.position + edit.deletedLength | ||
newEndByte := edit.position + uint(len(edit.insertedText)) | ||
|
||
startPosition, err := positionForOffset(*input, startByte) | ||
if err != nil { | ||
return InputEdit{}, err | ||
} | ||
|
||
oldEndPosition, err := positionForOffset(*input, oldEndByte) | ||
if err != nil { | ||
return InputEdit{}, err | ||
} | ||
|
||
newInput := make([]byte, 0, len(*input)-int(edit.deletedLength)+len(edit.insertedText)) | ||
newInput = append(newInput, (*input)[:startByte]...) | ||
newInput = append(newInput, edit.insertedText...) | ||
newInput = append(newInput, (*input)[oldEndByte:]...) | ||
*input = newInput | ||
|
||
newEndPosition, err := positionForOffset(*input, newEndByte) | ||
if err != nil { | ||
return InputEdit{}, err | ||
} | ||
|
||
inputEdit := InputEdit{ | ||
StartByte: startByte, | ||
OldEndByte: oldEndByte, | ||
NewEndByte: newEndByte, | ||
StartPosition: startPosition, | ||
OldEndPosition: oldEndPosition, | ||
NewEndPosition: newEndPosition, | ||
} | ||
tree.Edit(&inputEdit) | ||
return inputEdit, nil | ||
} | ||
|
||
func positionForOffset(input []byte, offset uint) (Point, error) { | ||
if offset > uint(len(input)) { | ||
return Point{}, fmt.Errorf("failed to address an offset: %d", offset) | ||
} | ||
|
||
var result Point | ||
var last uint | ||
|
||
for i := uint(0); i < offset; i++ { | ||
if input[i] == '\n' { | ||
result.Row++ | ||
last = i | ||
} | ||
} | ||
|
||
if result.Row > 0 { | ||
result.Column = uint(offset - last - 1) | ||
} else { | ||
result.Column = uint(offset) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func invertEdit(input []byte, edit *testEdit) *testEdit { | ||
position := edit.position | ||
removedContent := input[position : position+edit.deletedLength] | ||
return &testEdit{ | ||
position: position, | ||
deletedLength: uint(len(edit.insertedText)), | ||
insertedText: removedContent, | ||
} | ||
} | ||
|
||
func getRandomEdit(rand *rand.Rand, input []byte) testEdit { | ||
choice := rand.Intn(10) | ||
if choice < 2 { | ||
// Insert text at end | ||
insertedText := randWords(rand, 3) | ||
return testEdit{ | ||
position: uint(len(input)), | ||
deletedLength: 0, | ||
insertedText: insertedText, | ||
} | ||
} else if choice < 5 { | ||
// Delete text from the end | ||
deletedLength := uint(rand.Intn(30)) | ||
if deletedLength > uint(len(input)) { | ||
deletedLength = uint(len(input)) | ||
} | ||
return testEdit{ | ||
position: uint(len(input)) - deletedLength, | ||
deletedLength: deletedLength, | ||
insertedText: []byte{}, | ||
} | ||
} else if choice < 8 { | ||
// Insert at a random position | ||
position := uint(rand.Intn(len(input))) | ||
wordCount := 1 + rand.Intn(3) | ||
insertedText := randWords(rand, wordCount) | ||
return testEdit{ | ||
position: position, | ||
deletedLength: 0, | ||
insertedText: insertedText, | ||
} | ||
} else { | ||
// Replace at random position | ||
position := uint(rand.Intn(len(input))) | ||
deletedLength := uint(rand.Intn(len(input) - int(position))) | ||
wordCount := 1 + rand.Intn(3) | ||
insertedText := randWords(rand, wordCount) | ||
return testEdit{ | ||
position: position, | ||
deletedLength: deletedLength, | ||
insertedText: insertedText, | ||
} | ||
} | ||
} | ||
|
||
var operators = []byte{'+', '-', '<', '>', '(', ')', '*', '/', '&', '|', '!', ',', '.', '%'} | ||
|
||
func randWords(rand *rand.Rand, maxCount int) []byte { | ||
var result []byte | ||
wordCount := rand.Intn(maxCount) | ||
for i := 0; i < wordCount; i++ { | ||
if i > 0 { | ||
if rand.Intn(5) == 0 { | ||
result = append(result, '\n') | ||
} else { | ||
result = append(result, ' ') | ||
} | ||
} | ||
if rand.Intn(3) == 0 { | ||
index := rand.Intn(len(operators)) | ||
result = append(result, operators[index]) | ||
} else { | ||
for j := 0; j < rand.Intn(8); j++ { | ||
result = append(result, byte(rand.Intn(26)+'a')) | ||
} | ||
} | ||
} | ||
return result | ||
} |
Oops, something went wrong.