Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Aug 24, 2024
0 parents commit 7c613cc
Show file tree
Hide file tree
Showing 28 changed files with 10,090 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
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 ./...
3 changes: 3 additions & 0 deletions .gitmodules
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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Amaan Qureshi <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions allocator.c
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); }
111 changes: 111 additions & 0 deletions allocator.go
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),
)
}
9 changes: 9 additions & 0 deletions allocator.h
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);
13 changes: 13 additions & 0 deletions dup_unix.go
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)))
}
16 changes: 16 additions & 0 deletions dup_windows.go
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)))
}
36 changes: 36 additions & 0 deletions edit.go
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)
}
Loading

0 comments on commit 7c613cc

Please sign in to comment.