♻️ 更名tree-sitter-tsf文件夹

This commit is contained in:
csh
2025-11-02 16:29:05 +08:00
parent d8027f2f1a
commit bcb83f7cad
17 changed files with 0 additions and 0 deletions
@@ -0,0 +1,46 @@
root = true
[*]
charset = utf-8
[*.{json,toml,yml,gyp}]
indent_style = space
indent_size = 2
[*.js]
indent_style = space
indent_size = 2
[*.scm]
indent_style = space
indent_size = 2
[*.{c,cc,h}]
indent_style = space
indent_size = 4
[*.rs]
indent_style = space
indent_size = 4
[*.{py,pyi}]
indent_style = space
indent_size = 4
[*.swift]
indent_style = space
indent_size = 4
[*.go]
indent_style = tab
indent_size = 8
[Makefile]
indent_style = tab
indent_size = 8
[parser.c]
indent_size = 2
[{alloc,array,parser}.h]
indent_size = 2
@@ -0,0 +1,41 @@
* text=auto eol=lf
# Generated source files
src/*.json linguist-generated
src/parser.c linguist-generated
src/tree_sitter/* linguist-generated
# C bindings
bindings/c/** linguist-generated
CMakeLists.txt linguist-generated
Makefile linguist-generated
# Rust bindings
bindings/rust/* linguist-generated
Cargo.toml linguist-generated
Cargo.lock linguist-generated
# Node.js bindings
bindings/node/* linguist-generated
binding.gyp linguist-generated
package.json linguist-generated
package-lock.json linguist-generated
# Python bindings
bindings/python/** linguist-generated
setup.py linguist-generated
pyproject.toml linguist-generated
# Go bindings
bindings/go/* linguist-generated
go.mod linguist-generated
go.sum linguist-generated
# Swift bindings
bindings/swift/** linguist-generated
Package.swift linguist-generated
Package.resolved linguist-generated
# Zig bindings
build.zig linguist-generated
build.zig.zon linguist-generated
@@ -0,0 +1,50 @@
# Rust artifacts
target/
Cargo.lock
# Node artifacts
build/
prebuilds/
node_modules/
package-lock.json
# Swift artifacts
.build/
Package.resolved
# Go artifacts
_obj/
# Python artifacts
.venv/
dist/
*.egg-info
*.whl
# C artifacts
*.a
*.so
*.so.*
*.dylib
*.dll
*.pc
*.exp
*.lib
# Zig artifacts
.zig-cache/
zig-cache/
zig-out/
# Example dirs
/examples/*/
# Grammar volatiles
*.wasm
*.obj
*.o
# Archives
*.tar.gz
*.tgz
*.zip
@@ -0,0 +1 @@
ret := insert into sqltable tableName of FDBAlias data[i:i+999-1];
@@ -0,0 +1,66 @@
#!/bin/bash
# 生成 Tree-sitter 解析器
echo "=== 生成 Tree-sitter 解析器 ==="
# tree-sitter generate || { echo "❌ tree-sitter generate 失败"; exit 1; }
# 要解析的根目录数组
ROOT_DIRS=(
# "/mnt/d/code/tinysoft/OfficeXml-dev/funcext/OfficeXml/autounit"
# "/mnt/d/code/tinysoft/OfficeXml-dev/funcext/OfficeXml/openxml"
# "/mnt/d/code/tinysoft/OfficeXml-dev/funcext/OfficeXml/utils"
# "/mnt/d/code/tinysoft/OfficeXml-dev/funcext/OfficeXml/docx"
# "/mnt/d/code/tinysoft/OfficeXml-dev/generator"
# "/mnt/d/code/tinysoft/tsoffice/"
# "/mnt/d/code/tinysoft/pdfconverter"
# "/mnt/c/Programs/Tinysoft/TSLGen2/funcext/other"
# "/mnt/c/Programs/Tinysoft/TSLGen2/funcext/tsword"
# "/mnt/c/Programs/Tinysoft/TSLGen2/funcext/word2arr"
# "/mnt/c/Programs/Tinysoft/TSLGen2/funcext/tsoffice"
# "/mnt/c/Programs/Tinysoft/TSLGen2/funcext/OfficeXml-dev"
# "/mnt/d/code/tinysoft/PdfConverter"
"/mnt/c/Programs/Tinysoft/TSLGen2/funcext"
# 可以添加更多目录
# "/path/to/third/directory"
)
echo "=== 开始递归解析所有 .tsf 文件 ==="
# 错误标志
has_error=false
# 遍历所有根目录
for root_dir in "${ROOT_DIRS[@]}"; do
echo "--- 处理目录: $root_dir"
# 检查目录是否存在
if [ ! -d "$root_dir" ]; then
echo "❌ 目录不存在: $root_dir"
has_error=true
continue
fi
# 遍历当前目录下的所有 .tsf 文件
while IFS= read -r -d '' file; do
echo "--- 正在解析: $file"
output=$(tree-sitter parse "$file" 2>&1)
if echo "$output" | grep -q "ERROR\|MISSING"; then
echo "❌ 语法错误在文件: $file"
echo "$output"
echo "--- 错误详情结束 ---"
has_error=true
# 如果希望遇到错误立即停止,取消注释下面这行
break
else
echo "✓ 解析成功: $file"
fi
done < <(find "$root_dir" -type f -name "*.tsf" -print0)
done
# 检查是否有错误
if [ "$has_error" = true ]; then
echo "❌ 解析过程中发现错误"
exit 1
fi
echo "✅ 所有文件解析成功"
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,364 @@
// Example TSF program demonstrating various language features
// Variable declarations
var x, y, z: int;
var name: string;
var matrix: array of real;
// Constants
const PI = 3.14159;
const MAX_SIZE: int = 100;
const GREETING = "Hello, World!";
// Global and static variables
global config: object;
static counter := 0;
// Basic arithmetic
x := 10;
y := 20;
z := x + y * 2; // z = 50
// String operations
name := "John" $ " " $ "Doe";
// Boolean operations
var isValid := x > 0 and y < 100;
var isReady := not (z = 0) or name <> "";
// Bitwise operations
var flags := 0b1010;
flags := flags .| 0b0101; // flags = 0b1111
flags := flags shl 2; // flags = 0b111100
// Matrix operations
var A, B, C: matrix;
C := A :* B; // Matrix multiplication
C := A :^ 2; // Matrix power
// Set operations
var set1, set2, result: set;
result := set1 union set2;
result := set1 intersect set2;
// Ternary operator
var max := x > y ? x : y;
var sign := x > 0 ? 1 : x < 0 ? -1 : 0;
// Prefix and postfix operators
++counter;
var oldValue := counter++;
// Augmented assignments
x += 10;
y *= 2;
flags .&= 0xFF;
set1 union= set2;
obj.a := 123;
obj["a"] := 456;
// Special values
var nothing := nil;
var infinity := +inf;
var negInfinity := -inf;
// array
arr := array();
arr := array(1, 2, 3);
arr := array(1, "abc", true, nil);
arr := array("key1": "value1", "key2": "value2");
arr := array((1, "a"), array(2, "b"));
arr := array(array(1, 2), array(3, 4));
arr := array(array(array(1, 2), array(3, 4)), array(array(5, 6), array(7, 8)));
arr := array(("key1": array(1, 2, 3)), ("key2": (4, 5, 6)));
arr := array("outer": array("inner1": 1, "inner2": 2), "data": array(1, 2, 3));
arr := array((array(1, 2), array("a", "b")), (array(3, 4), array("c", "d")));
arr := array("data": array((1, "first"), (2, "second")), "meta": array("count": 2, "type": "test"));
arr := array(var1, var2, array(var3, var4));
arr := array(func1(), func2(arg), array(func3(), func4(arg1, arg2)));
arr := array(func(), (a + b));
arr := array((x + y), z, (a * b));
arr := array(func(), (a + b), var1, (c * d));
arr := array((a, b, c), (x + y), ("key": "value"));
arr := array(1, func(), (expr), (a, b), "key": (value));
data := array(
"users": array(
("id": 1, "name": "Alice", "scores": array(85, 92, 78)),
("id": 2, "name": "Bob", "scores": array(91, 87, 95))
),
"metadata": array(
"total": 2,
"average": array(88.0, 89.5, 86.5),
"nested": array(
"level1": array(
"level2": array(1, 2, 3),
"level2b": (true, false, nil)
)
)
)
);
// 访问嵌套数据
first_user := data["users"][0];
first_score := first_user["scores"][0];
nested_value := data["metadata"]["nested"]["level1"]["level2"][1];
echo "First user score:", first_score;
echo "Nested value:", nested_value;
// Function calls (hypothetical functions)
process(data);
calculate(x: 10, y: 20, mode: "fast");
##process(data);
// Attribute access (hypothetical objects)
// var length := myString.length;
// config.settings.timeout := 3000;
// Array subscripting and slicing
// var first := array[0];
// var subArray := array[1:10];
// var tail := array[5:];
// Complex expressions with precedence
var result1 := 2 + 3 * 4 ^ 2; // 2 + 3 * 16 = 50
var result2 := (2 + 3) * 4 ^ 2; // 5 * 16 = 80
var result3 := not a > b and c <= d or e = f;
// Derivative operator (mathematical)
// var derivative := !f;
// Expression operators
// var reference := @value;
// var address := &variable;
{ This is a block comment
It can span multiple lines
and contain any text }
(* This is a nested comment
It can also span multiple lines
and is Pascal-style *)
// Chained comparisons
var inRange := 0 <= x <= 100;
var ordered := a < b < c;
// Nested assignments
var a := var b := var c := 0;
// Complex type specifications
var complexType: array of array of real;
var functionType: procedure of integer;
// const
const a: real = 10;
const b = "123";
// augmented_assignment
a += 10;
b += func();
c /= ma[1];
// return
return;
return 10;
return f(10);
// break
break;
// continue
continue;
// echo
echo abc $ def $ "123" $ 456, funcstr(), "\n";
// raise
raise abc() $ "123";
// inherited
inherited abc();
// new
obj := new abc();
// if
if condition then
f1();
else
raise "abc";
if condition1 then
begin
if f1() then echo 1;
else echo 2;
end
else if condition2 then
begin
echo 3;
end
else begin
echo 4;
end
// for
for i:=0 to 10 do
echo i, "\n";
for k,v in arr do
begin
echo "k = 0", "\n";
echo "v = 1", "\n";
end
// while
while true do
echo 123;
// repeat
repeat
echo 1;
a++;
until a > 10;
// case
case x of
1,2: return abc;
"acde": begin
return def;
end
else begin
a := 1;
return a;
end
end;
// try
try
a := 1;
except
a := 2;
end
// function
function foo(a, b, c): real
begin
end
function foo(a, b: integer; c: string);
begin
end;
function foo(a: boolean; b: integer; c: string): real;
begin
end
function foo(a: boolean = true; b: integer = 123);
begin
end;
// function pointer
pf := function(a, b)
begin
echo a;
end;
// type
type A = class
public
function create()
begin
end
function foo1(a: real; b: real);virtual;
function foo2(a: real; b: real);virtual;
begin
end
function operator[](index);
property row read readrow;
property col read readcol write wirtecol;
private
[weakref]a1: real;
static a2: real;
a3: tslobj;
end;
function A.create();
begin
pf := aa;
end;
function operator A.[](index);
begin
a := 1;
end;
function A.foo1(a: real; b: real);virtual;
begin
pf := function();
begin
end
end
// select
select * from abc end;
select *, ["abc"] from abc end;
select *, ["abc"] as "def" from abc end;
select * from abc where func(["abc"]) end;
select * from abc where ["abc"] > 1 end;
R1 := select *,RoundTo((["英语成绩"]+["语文成绩"]+["历史成绩"]+["地理成绩"])/4,2) as "文科成绩" from ScoreList end;
return select ['stockid'],['date'],['close']*['vol'] as nil from markettable end;
A := select *,ThisOrder as "Order" from A order by ["AAA"] end;
B := select * from EnglishScore where ["英语成绩"] > 85 order by ["英语成绩"] end;
B := select drange(0 to 9) * from EnglishScore order by ["英语成绩"] desc end;
B := select drange(1 of 10) from EnglishScore order by ["英语成绩"] desc end;
a := select * from abc where ["abc"] > 1 end;
b := select * from abc group by ["abc"] end;
c := select * from abc group by func(["acbb"]) end;
b := select * from abc group by ["abc"], ["def"] end;
e := select * from abc where func(["abc"]) order by ["A"] desc end;
select AvgOf(["英语成绩"]) from EnglishScore where ["英语成绩"]>85 end;
return select ["性别"],AvgOf(["英语成绩"]),CountOf( * ),groupfunc(["英语成绩"]) from EnglishScore group by ["性别"],groupfunc(["英语成绩"]) end;
return select AvgOf(["英语成绩"]), CountOf( * ), groupfunc(["英语成绩"]) from EnglishScore group by groupfunc(["英语成绩"]) having CountOf( * ) >1 end;
R := select [1].*,[2].["英语成绩"] from A join B on [1].["学号"]=[2].["学号"] end;
R := select [1].*,[2].["英语成绩"] from A cross join B where [1].["学号"]=[2].["学号"] end;
R := select [1].*,[2].["英语成绩"] from A, B where [1].["学号"]=[2].["学号"] end;
R := select [1].*,[2].["英语成绩"] from A join B with ([1].["学号"] on [2].["学号"]) end;
R := select [1].*,[2].["英语成绩"] from A join B with ([1].["学号"],[1].["班级"] on [2].["学号"],[2].["班级"]) end;
R := select [1].*,[2].["英语成绩"],[3].["语文成绩"] from A join B on [1].["学号"]=[2].["学号"] join C on [1].["学号"]=[3].["学号"] end;
R := select [1].*,[2].["英语成绩"],[3].["俄语成绩"] from A left join B on [1].["学号"]=[2].["学号"] left join C on [1].["学号"]=[3].["学号"] end;
R := select [1].["学号"]?:[2].["学号"] as "学号",[1].["英语成绩"],[2].["俄语成绩"] from B full join C on [1].["学号"]=[2].["学号"] end;
R1 := select ThisRow from R where ThisRow>5 end;
R2 := sselect ThisRow from R where ThisRow>5 end;
R2 := vselect SumOf( ["英语成绩"] ) from B end;
R1 := select ["性别"],["年龄"], AvgOf(["身高"]), select * from ThisGroup end as "详细信息" from R group by ["性别"],["年龄"] end;
// update
update B set ["英语成绩"] = 79 where ["学号"] = "03" end;
update B set ["英语成绩"]=79,["语文成绩"]=80 where ["学号"]="03" end;
update children set ['origin_field'] = ['field'] end;
update children set ['field'] = uppercase(['prefix']) + ['field'] where not ifnil(['ml']) and ['field'] in fields end;
update children set ['new_field'] = format('XmlChild%s', ['field']) end;
// delete
delete from A where ["学号"] = "01";
delete from A;
// insert
insert into a values("06","路人甲");
insert into a insertfields(["学号"],["姓名"],["英语成绩"]) values("06","路人甲",80);
// []
A[2,3];
A[2:5,3:6];
A[:,3:6];
A[3,:];
A[:,"columnName"];
A[array(2,4,6)];
A[array(2,3),array(1,2)];
[a,b,c] := arr;
// End of example
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+22
View File
@@ -0,0 +1,22 @@
{
"name": "tree-sitter-tsf",
"version": "1.0.0",
"description": "",
"main": "bingings/node",
"scripts": {
"test": "tree-sitter test"
},
"tree-sitter":
[
{
"scope": "source.tsf",
"file-types": ["tsf"]
}
],
"dependencies": {
"nan": "^2.23.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.25.6"
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+54
View File
@@ -0,0 +1,54 @@
#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void *(*ts_current_malloc)(size_t size);
extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void *ptr);
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif
#else
#ifndef ts_malloc
#define ts_malloc malloc
#endif
#ifndef ts_calloc
#define ts_calloc calloc
#endif
#ifndef ts_realloc
#define ts_realloc realloc
#endif
#ifndef ts_free
#define ts_free free
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ALLOC_H_
+291
View File
@@ -0,0 +1,291 @@
#ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "./alloc.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#define Array(T) \
struct { \
T *contents; \
uint32_t size; \
uint32_t capacity; \
}
/// Initialize an array.
#define array_init(self) \
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
/// Create an empty array.
#define array_new() \
{ NULL, 0, 0 }
/// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0)
/// Get a pointer to the last element in the array.
#define array_back(self) array_get(self, (self)->size - 1)
/// Clear the array, setting its size to zero. Note that this does not free any
/// memory allocated for the array's contents.
#define array_clear(self) ((self)->size = 0)
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
/// less than the array's current capacity, this function has no effect.
#define array_reserve(self, new_capacity) \
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
/// Free any memory allocated for this array. Note that this does not free any
/// memory allocated for the array's contents.
#define array_delete(self) _array__delete((Array *)(self))
/// Push a new `element` onto the end of the array.
#define array_push(self, element) \
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
(self)->contents[(self)->size++] = (element))
/// Increase the array's size by `count` elements.
/// New elements are zero-initialized.
#define array_grow_by(self, count) \
do { \
if ((count) == 0) break; \
_array__grow((Array *)(self), count, array_elem_size(self)); \
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
(self)->size += (count); \
} while (0)
/// Append all elements from one array to the end of another.
#define array_push_all(self, other) \
array_extend((self), (other)->size, (other)->contents)
/// Append `count` elements to the end of the array, reading their values from the
/// `contents` pointer.
#define array_extend(self, count, contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), (self)->size, \
0, count, contents \
)
/// Remove `old_count` elements from the array starting at the given `index`. At
/// the same index, insert `new_count` new elements, reading their values from the
/// `new_contents` pointer.
#define array_splice(self, _index, old_count, new_count, new_contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), _index, \
old_count, new_count, new_contents \
)
/// Insert one `element` into the array at the given `index`.
#define array_insert(self, _index, element) \
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
/// Remove one element from the array at the given `index`.
#define array_erase(self, _index) \
_array__erase((Array *)(self), array_elem_size(self), _index)
/// Pop the last element off the array, returning the element by value.
#define array_pop(self) ((self)->contents[--(self)->size])
/// Assign the contents of one array to another, reallocating if necessary.
#define array_assign(self, other) \
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
/// Swap one array with another
#define array_swap(self, other) \
_array__swap((Array *)(self), (Array *)(other))
/// Get the size of the array contents
#define array_elem_size(self) (sizeof *(self)->contents)
/// Search a sorted array for a given `needle` value, using the given `compare`
/// callback to determine the order.
///
/// If an existing element is found to be equal to `needle`, then the `index`
/// out-parameter is set to the existing value's index, and the `exists`
/// out-parameter is set to true. Otherwise, `index` is set to an index where
/// `needle` should be inserted in order to preserve the sorting, and `exists`
/// is set to false.
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
/// Search a sorted array for a given `needle` value, using integer comparisons
/// of a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, _index, _exists) \
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
/// Insert a given `value` into a sorted array, using the given `compare`
/// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
/// Insert a given `value` into a sorted array, using integer comparisons of
/// a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
// Private
typedef Array(void) Array;
/// This is not what you're looking for, see `array_delete`.
static inline void _array__delete(Array *self) {
if (self->contents) {
ts_free(self->contents);
self->contents = NULL;
self->size = 0;
self->capacity = 0;
}
}
/// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array *self, size_t element_size,
uint32_t index) {
assert(index < self->size);
char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size,
(self->size - index - 1) * element_size);
self->size--;
}
/// This is not what you're looking for, see `array_reserve`.
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
if (new_capacity > self->capacity) {
if (self->contents) {
self->contents = ts_realloc(self->contents, new_capacity * element_size);
} else {
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
}
}
/// This is not what you're looking for, see `array_assign`.
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
_array__reserve(self, element_size, other->size);
self->size = other->size;
memcpy(self->contents, other->contents, self->size * element_size);
}
/// This is not what you're looking for, see `array_swap`.
static inline void _array__swap(Array *self, Array *other) {
Array swap = *other;
*other = *self;
*self = swap;
}
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
uint32_t new_size = self->size + count;
if (new_size > self->capacity) {
uint32_t new_capacity = self->capacity * 2;
if (new_capacity < 8) new_capacity = 8;
if (new_capacity < new_size) new_capacity = new_size;
_array__reserve(self, element_size, new_capacity);
}
}
/// This is not what you're looking for, see `array_splice`.
static inline void _array__splice(Array *self, size_t element_size,
uint32_t index, uint32_t old_count,
uint32_t new_count, const void *elements) {
uint32_t new_size = self->size + new_count - old_count;
uint32_t old_end = index + old_count;
uint32_t new_end = index + new_count;
assert(old_end <= self->size);
_array__reserve(self, element_size, new_size);
char *contents = (char *)self->contents;
if (self->size > old_end) {
memmove(
contents + new_end * element_size,
contents + old_end * element_size,
(self->size - old_end) * element_size
);
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
}
}
self->size += new_count - old_count;
}
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
do { \
*(_index) = start; \
*(_exists) = false; \
uint32_t size = (self)->size - *(_index); \
if (size == 0) break; \
int comparison; \
while (size > 1) { \
uint32_t half_size = size / 2; \
uint32_t mid_index = *(_index) + half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
if (comparison <= 0) *(_index) = mid_index; \
size -= half_size; \
} \
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
if (comparison == 0) *(_exists) = true; \
else if (comparison < 0) *(_index) += 1; \
} while (0)
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
/// parameter by reference in order to work with the generic sorting function above.
#define _compare_int(a, b) ((int)*(a) - (int)(b))
#ifdef _MSC_VER
#pragma warning(pop)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ARRAY_H_
+286
View File
@@ -0,0 +1,286 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata {
uint8_t major_version;
uint8_t minor_version;
uint8_t patch_version;
} TSLanguageMetadata;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
// Used to index the field and supertype maps.
typedef struct {
uint16_t index;
uint16_t length;
} TSMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
void (*log)(const TSLexer *, const char *, ...);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
uint16_t reserved_word_set_id;
} TSLexerMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage {
uint32_t abi_version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexerMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
const char *name;
const TSSymbol *reserved_words;
uint16_t max_reserved_word_set_size;
uint32_t supertype_count;
const TSSymbol *supertype_symbols;
const TSMapSlice *supertype_map_slices;
const TSSymbol *supertype_map_entries;
TSLanguageMetadata metadata;
};
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
uint32_t index = 0;
uint32_t size = len - index;
while (size > 1) {
uint32_t half_size = size / 2;
uint32_t mid_index = index + half_size;
const TSCharacterRange *range = &ranges[mid_index];
if (lookahead >= range->start && lookahead <= range->end) {
return true;
} else if (lookahead > range->end) {
index = mid_index;
}
size -= half_size;
}
const TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
}
/*
* Lexer Macros
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define ADVANCE_MAP(...) \
{ \
static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
if (map[i] == lookahead) { \
state = map[i + 1]; \
goto next_state; \
} \
} \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value) \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value), \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_name, children, precedence, prod_id) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_name, \
.child_count = children, \
.dynamic_precedence = precedence, \
.production_id = prod_id \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_
@@ -0,0 +1,33 @@
{
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json",
"grammars": [
{
"name": "tsf",
"camelcase": "Tsf",
"title": "TSF",
"scope": "source.tsf",
"file-types": [
"tsf",
"tsl"
],
"injection-regex": "^tsf$",
"class-name": "TreeSitterTsf"
}
],
"metadata": {
"version": "0.1.0",
"license": "MIT",
"description": "tsf",
"authors": [
{
"name": "csh"
}
],
"links": {
"repository": "https://github.com/tree-sitter/tree-sitter-tsf"
}
},
"bindings": {
"c": true
}
}