1. New build system
All checks were successful
build-dsl-smoke / Build judge (push) Successful in 12s
build-dsl-smoke / debug / clang / linux (push) Successful in 6s
build-dsl-smoke / debug / gcc / linux (push) Successful in 8s
build-dsl-smoke / release / clang / linux (push) Successful in 8s
build-dsl-smoke / release / gcc / linux (push) Successful in 6s
build-dsl-smoke / sanitized / clang / linux (push) Successful in 8s
build-dsl-smoke / sanitized / gcc / linux (push) Successful in 7s
build-dsl-smoke / debug / clang / windows (push) Successful in 13s
build-dsl-smoke / debug-valgrind / gcc / linux (push) Successful in 14s
build-dsl-smoke / release / clang / windows (push) Successful in 16s
build-dsl-smoke / debug / msvc / windows (push) Successful in 18s
build-dsl-smoke / release / msvc / windows (push) Successful in 17s
build-dsl-smoke / SUMMARY (push) Successful in 4s
Release / Build & publish (push) Successful in 48s

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-04-12 07:59:38 +00:00
parent 358e3146bc
commit 7ec3a43c7a
47 changed files with 14124 additions and 209 deletions

17
editor/zed/.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
grammars/*.wasm
tree-sitter-jdg/tree-sitter-jdg.wasm
tree-sitter-jdg/node_modules/
tree-sitter-jdg/bindings/
tree-sitter-jdg/binding.gyp
tree-sitter-jdg/Cargo.toml
tree-sitter-jdg/Cargo.lock
tree-sitter-jdg/Makefile
tree-sitter-jdg/Package.swift
tree-sitter-jdg/pyproject.toml
tree-sitter-jdg/setup.py
tree-sitter-jdg/go.mod
tree-sitter-jdg/go.sum
tree-sitter-jdg/bindings.gyp
tree-sitter-jdg/prebuilds/
tree-sitter-jdg/target/

23
editor/zed/extension.toml Normal file
View File

@@ -0,0 +1,23 @@
id = "jdg"
name = "JDG"
version = "0.1.0"
schema_version = 1
description = "Syntax highlighting for the judge DSL (.jdg)"
repository = "https://github.com/Mond1c/judge"
authors = ["judge contributors"]
themes = []
icon_themes = []
languages = ["languages/jdg"]
capabilities = []
[lib]
[grammars.jdg]
repository = "https://github.com/Mond1c/judge"
rev = "main"
[language_servers]
[context_servers]
[slash_commands]

View File

@@ -0,0 +1,2 @@
("{" @open "}" @close)
("(" @open ")" @close)

View File

@@ -0,0 +1,12 @@
name = "JDG"
grammar = "jdg"
path_suffixes = ["jdg"]
line_comments = ["// "]
autoclose_before = ";:.,=}])>"
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = false },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string", "comment"] },
]
word_characters = ["_"]
tab_size = 4

View File

@@ -0,0 +1,100 @@
(comment) @comment
[
"include"
"build"
"build_defaults"
"build_linux"
"build_windows"
"build_darwin"
"toolchains"
"binary"
"sources"
"timeout"
"memory_limit"
"group"
"test"
"pattern"
] @keyword
[
"normalize_crlf"
"trim_trailing_ws"
] @keyword
(build_scalar_field name: _ @property)
(build_list_field name: _ @property)
(bool_decl name: _ @property)
(legacy_build_platform keyword: _ @keyword)
[
"profile"
"warnings"
"define"
"platforms"
"binary"
"class"
"weight"
"scoring"
"wrapper"
"env"
"stdin"
"stdout"
"stderr"
"args"
"exitCode"
"file"
"outFile"
"input"
"output"
"dirs"
] @property
[
"linux"
"windows"
"darwin"
] @type.builtin
(profile_value) @constant.builtin
(warnings_value) @constant.builtin
(scoring_value) @constant.builtin
(class_value) @constant.builtin
(bool) @constant.builtin
"of" @keyword.operator
[
"contains"
"matches"
"anyOrder"
] @function.builtin
[
"="
"~"
] @operator
[
"{"
"}"
"("
")"
] @punctuation.bracket
(simple_string) @string
(heredoc_string) @string
(regex_string) @string.regex
(structured_build name: (string) @string.special)
(legacy_build command: (string) @string.special)
(toolchain_entry name: (string) @type)
(toolchain_entry name: (identifier) @type)
(group name: (string) @string.special)
(test name: (string) @string.special)
(include path: (string) @string.special.path)
(define_field key: (string) @constant.macro)
(env_decl key: (string) @constant.macro)
(file_decl key: (string) @string.special.path)
(out_file_decl key: (string) @string.special.path)

View File

@@ -0,0 +1,11 @@
[
(build_block)
(toolchains)
(toolchain_entry)
(group)
(test)
(pattern)
(any_order_matcher)
] @indent
"}" @end

View File

@@ -0,0 +1,7 @@
((comment) @injection.content
(#set! injection.language "comment"))
((regex_matcher
pattern: (regex_string) @injection.content)
(#set! injection.language "regex")
(#set! injection.include-children))

View File

@@ -0,0 +1,23 @@
(structured_build
"build" @context
name: (string) @name) @item
(build_defaults
"build_defaults" @context) @item
(toolchains
"toolchains" @context) @item
(toolchain_entry
name: (_) @name) @item
(group
"group" @context
name: (string) @name) @item
(test
"test" @context
name: (string) @name) @item
(pattern
"pattern" @context) @item

View File

@@ -0,0 +1,39 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.{json,toml,yml,gyp}]
indent_style = space
indent_size = 2
[*.js]
indent_style = space
indent_size = 2
[*.rs]
indent_style = space
indent_size = 4
[*.{c,cc,h}]
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

View File

@@ -0,0 +1,11 @@
* text eol=lf
src/*.json linguist-generated
src/parser.c linguist-generated
src/tree_sitter/* linguist-generated
bindings/** linguist-generated
binding.gyp linguist-generated
setup.py linguist-generated
Makefile linguist-generated
Package.swift linguist-generated

38
editor/zed/tree-sitter-jdg/.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
# Rust artifacts
Cargo.lock
target/
# Node artifacts
build/
prebuilds/
node_modules/
*.tgz
# Swift artifacts
.build/
# Go artifacts
go.sum
_obj/
# Python artifacts
.venv/
dist/
*.egg-info
*.whl
# C artifacts
*.a
*.so
*.so.*
*.dylib
*.dll
*.pc
# Example dirs
/examples/*/
# Grammar volatiles
*.wasm
*.obj
*.o

View File

@@ -0,0 +1,342 @@
module.exports = grammar({
name: 'jdg',
extras: $ => [/\s+/, $.comment],
word: $ => $.identifier,
conflicts: $ => [],
rules: {
source_file: $ => repeat($._top_statement),
comment: $ => token(seq('//', /[^\n]*/)),
_top_statement: $ => choice(
$.include,
$.toolchains,
$.build_defaults,
$.structured_build,
$.legacy_build_platform,
$.legacy_build,
$.binary_decl,
$.sources_decl,
$.bool_decl,
$.timeout_decl,
$.memory_limit_decl,
$.group,
),
include: $ => seq(
'include',
field('path', $.string)
),
legacy_build: $ => prec(1, seq(
'build',
field('command', $.string)
)),
legacy_build_platform: $ => seq(
field('keyword', choice('build_linux', 'build_windows', 'build_darwin')),
field('command', $.string)
),
structured_build: $ => prec(2, seq(
'build',
field('name', $.string),
field('body', $.build_block)
)),
build_defaults: $ => seq(
'build_defaults',
field('body', $.build_block)
),
build_block: $ => seq(
'{',
repeat($._build_field),
'}'
),
_build_field: $ => choice(
$.build_scalar_field,
$.build_list_field,
$.build_profile_field,
$.build_warnings_field,
$.define_field,
$.os_override,
),
build_scalar_field: $ => seq(
field('name', choice('language', 'standard', 'output', 'wrapper')),
'=',
field('value', $.string)
),
build_list_field: $ => seq(
field('name', choice('sources', 'includes', 'sanitize', 'link', 'extra', 'platforms', 'compilers')),
'=',
field('value', repeat1($.string))
),
build_profile_field: $ => seq(
'profile',
'=',
field('value', $.profile_value)
),
profile_value: $ => choice('release', 'debug', 'sanitized'),
build_warnings_field: $ => seq(
'warnings',
'=',
field('value', $.warnings_value)
),
warnings_value: $ => choice('default', 'strict', 'pedantic'),
define_field: $ => seq(
'define',
'(',
field('key', $.string),
')',
'=',
field('value', $.string)
),
os_override: $ => seq(
field('os', $.os_name),
field('body', $.build_block)
),
os_name: $ => choice('linux', 'windows', 'darwin'),
toolchains: $ => seq(
'toolchains',
'{',
repeat($.toolchain_entry),
'}'
),
toolchain_entry: $ => seq(
field('name', choice($.identifier, $.string)),
'{',
repeat($._toolchain_field),
'}'
),
_toolchain_field: $ => choice(
$.toolchain_platforms_field,
$.toolchain_binary_field,
$.toolchain_class_field,
),
toolchain_platforms_field: $ => seq(
'platforms',
'=',
field('value', repeat1($.string))
),
toolchain_binary_field: $ => seq(
'binary',
'=',
field('value', $.string)
),
toolchain_class_field: $ => seq(
'class',
'=',
field('value', $.class_value)
),
class_value: $ => choice('gnu', 'msvc'),
binary_decl: $ => seq(
'binary',
'=',
field('value', $.string)
),
sources_decl: $ => seq(
'sources',
'=',
field('value', $.string)
),
bool_decl: $ => seq(
field('name', choice('normalize_crlf', 'trim_trailing_ws')),
'=',
field('value', $.bool)
),
timeout_decl: $ => seq(
'timeout',
field('value', $.duration)
),
memory_limit_decl: $ => seq(
'memory_limit',
'=',
field('value', $.size)
),
group: $ => seq(
'group',
'(',
field('name', $.string),
')',
'{',
repeat($._group_field),
'}'
),
_group_field: $ => choice(
$.group_weight_field,
$.group_timeout_field,
$.group_memory_field,
$.group_scoring_field,
$.group_wrapper_field,
$.env_decl,
$.test,
$.pattern,
),
group_weight_field: $ => seq('weight', '=', field('value', $.number)),
group_timeout_field: $ => seq('timeout', '=', field('value', $.duration)),
group_memory_field: $ => seq('memory_limit', '=', field('value', $.size)),
group_scoring_field: $ => seq('scoring', '=', field('value', $.scoring_value)),
scoring_value: $ => choice('partial', 'all_or_none'),
group_wrapper_field: $ => seq('wrapper', '=', field('value', $.string)),
env_decl: $ => seq(
'env',
'(',
field('key', $.string),
')',
'=',
field('value', $.string)
),
test: $ => seq(
'test',
'(',
field('name', $.string),
')',
'{',
repeat($._test_field),
'}'
),
_test_field: $ => choice(
$.test_stdin_field,
$.test_stdout_field,
$.test_stderr_field,
$.test_args_field,
$.test_exit_code_field,
$.test_timeout_field,
$.test_memory_field,
$.test_wrapper_field,
$.env_decl,
$.file_decl,
$.out_file_decl,
),
test_stdin_field: $ => seq('stdin', '=', field('value', $.string)),
test_stdout_field: $ => seq('stdout', $._matcher),
test_stderr_field: $ => seq('stderr', $._matcher),
test_args_field: $ => seq('args', '=', field('value', repeat1($.string))),
test_exit_code_field: $ => seq('exitCode', '=', field('value', $.integer)),
test_timeout_field: $ => seq('timeout', '=', field('value', $.duration)),
test_memory_field: $ => seq('memory_limit', '=', field('value', $.size)),
test_wrapper_field: $ => seq('wrapper', '=', field('value', $.string)),
file_decl: $ => seq(
'file',
'(',
field('key', $.string),
')',
'=',
field('value', $.string)
),
out_file_decl: $ => seq(
'outFile',
'(',
field('key', $.string),
')',
'=',
field('value', $.string)
),
_matcher: $ => choice(
$.exact_matcher,
$.numeric_matcher,
$.contains_matcher,
$.regex_matcher,
$.any_order_matcher,
),
exact_matcher: $ => seq('=', field('value', $.string)),
numeric_matcher: $ => seq(
'~',
field('epsilon', $.number),
'of',
field('value', $.string)
),
contains_matcher: $ => seq('contains', field('value', $.string)),
regex_matcher: $ => seq('matches', field('pattern', $.regex_string)),
any_order_matcher: $ => seq(
'anyOrder',
'{',
repeat1($.string),
'}'
),
pattern: $ => seq(
'pattern',
'{',
repeat($._pattern_field),
'}'
),
_pattern_field: $ => choice(
$.pattern_input_field,
$.pattern_output_field,
$.pattern_dirs_field,
$.pattern_args_field,
),
pattern_input_field: $ => seq('input', '=', field('value', $.string)),
pattern_output_field: $ => seq('output', '=', field('value', $.string)),
pattern_dirs_field: $ => seq('dirs', '=', field('value', $.string)),
pattern_args_field: $ => seq('args', '=', field('value', repeat1($.string))),
bool: $ => choice('true', 'false'),
number: $ => choice($.integer, $.float),
string: $ => choice(
$.heredoc_string,
$.simple_string,
),
simple_string: $ => token(seq(
'"',
repeat(choice(
/[^"\\\n]/,
seq('\\', /./)
)),
'"'
)),
heredoc_string: $ => token(seq(
'"""',
/([^"]|"[^"]|""[^"])*/,
'"""'
)),
regex_string: $ => $.simple_string,
identifier: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
integer: $ => token(/-?\d+/),
float: $ => token(/-?\d+\.\d+/),
duration: $ => token(/\d+(ms|s|m)/),
size: $ => token(/\d+(KiB|MiB|GiB|KB|MB|GB|B|K|M|G)/),
}
});

View File

@@ -0,0 +1,54 @@
{
"name": "tree-sitter-jdg",
"version": "0.1.0",
"description": "Tree-sitter grammar for the judge DSL (.jdg)",
"main": "bindings/node",
"types": "bindings/node",
"keywords": [
"tree-sitter",
"parser",
"jdg",
"judge"
],
"files": [
"grammar.js",
"binding.gyp",
"prebuilds/**",
"bindings/node/*",
"queries/*",
"src/**"
],
"license": "MIT",
"dependencies": {
"node-addon-api": "^7.1.0",
"node-gyp-build": "^4.8.0"
},
"peerDependencies": {
"tree-sitter": "^0.21.0"
},
"peerDependenciesMeta": {
"tree_sitter": {
"optional": true
}
},
"devDependencies": {
"tree-sitter-cli": "^0.22.0",
"prebuildify": "^6.0.0"
},
"scripts": {
"build": "tree-sitter generate",
"test": "tree-sitter test",
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip"
},
"tree-sitter": [
{
"scope": "source.jdg",
"file-types": [
"jdg"
],
"highlights": "queries/highlights.scm",
"injections": "queries/injections.scm"
}
]
}

1732
editor/zed/tree-sitter-jdg/src/grammar.json generated Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

5922
editor/zed/tree-sitter-jdg/src/parser.c generated Normal file

File diff suppressed because it is too large Load Diff

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);
extern void *(*ts_current_calloc)(size_t, size_t);
extern void *(*ts_current_realloc)(void *, size_t);
extern void (*ts_current_free)(void *);
#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_

View File

@@ -0,0 +1,290 @@
#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(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(default : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ARRAY_H_

View File

@@ -0,0 +1,265 @@
#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;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
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 *);
};
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 union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage {
uint32_t 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 TSFieldMapSlice *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 TSLexMode *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;
};
static inline bool set_contains(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;
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;
}
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_