Updated to not have headers in source directories

This commit is contained in:
Maieul BOYER 2024-08-04 15:48:08 +00:00
parent 0c435ed040
commit 91bb0a7533
33 changed files with 3668 additions and 4101 deletions

741
parser/include/parser/api.h Normal file
View file

@ -0,0 +1,741 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* api.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 13:54:54 by maiboyer #+# #+# */
/* Updated: 2024/07/22 13:55:02 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef API_H
#define API_H
#include "me/types.h"
#define ERROR_STATE 0
#define ERROR_COST_PER_RECOVERY 500
#define ERROR_COST_PER_MISSING_TREE 110
#define ERROR_COST_PER_SKIPPED_TREE 100
#define ERROR_COST_PER_SKIPPED_LINE 30
#define ERROR_COST_PER_SKIPPED_CHAR 1
/****************************/
/* Section - ABI Versioning */
/****************************/
/**
* The latest ABI version that is supported by the current version of the
* library. When Languages are generated by the Tree-sitter CLI, they are
* assigned an ABI version number that corresponds to the current CLI version.
* The Tree-sitter library is generally backwards-compatible with languages
* generated using older CLI versions, but is not forwards-compatible.
*/
#define TREE_SITTER_LANGUAGE_VERSION 14
/**
* The earliest ABI version that is supported by the current version of the
* library.
*/
#define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 13
/*******************/
/* Section - Types */
/*******************/
typedef t_u16 TSStateId;
typedef t_u16 TSSymbol;
typedef t_u16 TSFieldId;
typedef struct TSLanguage TSLanguage;
typedef struct TSParser TSParser;
typedef struct TSTree TSTree;
typedef struct TSQuery TSQuery;
typedef struct TSQueryCursor TSQueryCursor;
typedef struct TSLookaheadIterator TSLookaheadIterator;
typedef enum TSInputEncoding
{
TSInputEncodingUTF8,
TSInputEncodingUTF16,
} TSInputEncoding;
typedef enum TSSymbolType
{
TSSymbolTypeRegular,
TSSymbolTypeAnonymous,
TSSymbolTypeAuxiliary,
} TSSymbolType;
typedef struct TSPoint
{
t_u32 row;
t_u32 column;
} TSPoint;
typedef struct TSRange
{
TSPoint start_point;
TSPoint end_point;
t_u32 start_byte;
t_u32 end_byte;
} TSRange;
typedef struct TSInput
{
void *payload;
const t_u8 *(*read)(void *payload, t_u32 byte_index, TSPoint position, t_u32 *bytes_read);
TSInputEncoding encoding;
} TSInput;
typedef enum TSLogType
{
TSLogTypeParse,
TSLogTypeLex,
} TSLogType;
typedef struct TSLogger
{
void *payload;
void (*log)(void *payload, TSLogType log_type, const t_u8 *buffer);
} TSLogger;
typedef struct TSInputEdit
{
t_u32 start_byte;
t_u32 old_end_byte;
t_u32 new_end_byte;
TSPoint start_point;
TSPoint old_end_point;
TSPoint new_end_point;
} TSInputEdit;
typedef struct TSNode
{
t_u32 context[4];
const void *id;
const TSTree *tree;
} TSNode;
typedef TSNode t_parse_node;
typedef TSSymbol t_symbol;
typedef TSParser t_first_parser;
typedef TSLanguage t_language;
typedef TSTree t_first_tree;
typedef struct TSTreeCursor
{
const void *tree;
const void *id;
t_u32 context[3];
} TSTreeCursor;
typedef struct TSQueryCapture
{
TSNode node;
t_u32 index;
} TSQueryCapture;
typedef enum TSQuantifier
{
TSQuantifierZero = 0, // must match the array initialization value
TSQuantifierZeroOrOne,
TSQuantifierZeroOrMore,
TSQuantifierOne,
TSQuantifierOneOrMore,
} TSQuantifier;
typedef struct TSQueryMatch
{
t_u32 id;
t_u16 pattern_index;
t_u16 capture_count;
const TSQueryCapture *captures;
} TSQueryMatch;
typedef enum TSQueryPredicateStepType
{
TSQueryPredicateStepTypeDone,
TSQueryPredicateStepTypeCapture,
TSQueryPredicateStepTypeString,
} TSQueryPredicateStepType;
typedef struct TSQueryPredicateStep
{
TSQueryPredicateStepType type;
t_u32 value_id;
} TSQueryPredicateStep;
typedef enum TSQueryError
{
TSQueryErrorNone = 0,
TSQueryErrorSyntax,
TSQueryErrorNodeType,
TSQueryErrorField,
TSQueryErrorCapture,
TSQueryErrorStructure,
TSQueryErrorLanguage,
} TSQueryError;
/********************/
/* Section - Parser */
/********************/
/**
* Create a new parser.
*/
TSParser *ts_parser_new(void);
/**
* Delete the parser, freeing all of the memory that it used.
*/
void ts_parser_delete(TSParser *self);
/**
* Get the parser's current language.
*/
const TSLanguage *ts_parser_language(const TSParser *self);
/**
* Set the language that the parser should use for parsing.
*
* Returns a boolean indicating whether or not the language was successfully
* assigned. True means assignment succeeded. False means there was a version
* mismatch: the language was generated with an incompatible version of the
* Tree-sitter CLI. Check the language's version using [`ts_language_version`]
* and compare it to this library's [`TREE_SITTER_LANGUAGE_VERSION`] and
* [`TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION`] constants.
*/
bool ts_parser_set_language(TSParser *self, const TSLanguage *language);
/**
* Set the ranges of text that the parser should include when parsing.
*
* By default, the parser will always include entire documents. This function
* allows you to parse only a *portion* of a document but still return a syntax
* tree whose ranges match up with the document as a whole. You can also pass
* multiple disjoint ranges.
*
* The second and third parameters specify the location and length of an array
* of ranges. The parser does *not* take ownership of these ranges; it copies
* the data, so it doesn't matter how these ranges are allocated.
*
* If `count` is zero, then the entire document will be parsed. Otherwise,
* the given ranges must be ordered from earliest to latest in the document,
* and they must not overlap. That is, the following must hold for all:
*
* `i < count - 1`: `ranges[i].end_byte <= ranges[i + 1].start_byte`
*
* If this requirement is not satisfied, the operation will fail, the ranges
* will not be assigned, and this function will return `false`. On success,
* this function returns `true`
*/
bool ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, t_u32 count);
/**
* Get the ranges of text that the parser will include when parsing.
*
* The returned pointer is owned by the parser. The caller should not free it
* or write to it. The length of the array will be written to the given
* `count` pointer.
*/
const TSRange *ts_parser_included_ranges(const TSParser *self, t_u32 *count);
/**
* Use the parser to parse some source code and create a syntax tree.
*
* If you are parsing this document for the first time, pass `NULL` for the
* `old_tree` parameter. Otherwise, if you have already parsed an earlier
* version of this document and the document has since been edited, pass the
* previous syntax tree so that the unchanged parts of it can be reused.
* This will save time and memory. For this to work correctly, you must have
* already edited the old syntax tree using the [`ts_tree_edit`] function in a
* way that exactly matches the source code changes.
*
* The [`TSInput`] parameter lets you specify how to read the text. It has the
* following three fields:
* 1. [`read`]: A function to retrieve a chunk of text at a given byte offset
* and (row, column) position. The function should return a pointer to the
* text and write its length to the [`bytes_read`] pointer. The parser does
* not take ownership of this buffer; it just borrows it until it has
* finished reading it. The function should write a zero value to the
* [`bytes_read`] pointer to indicate the end of the document.
* 2. [`payload`]: An arbitrary pointer that will be passed to each invocation
* of the [`read`] function.
* 3. [`encoding`]: An indication of how the text is encoded. Either
* `TSInputEncodingUTF8` or `TSInputEncodingUTF16`.
*
* This function returns a syntax tree on success, and `NULL` on failure. There
* are three possible reasons for failure:
* 1. The parser does not have a language assigned. Check for this using the
[`ts_parser_language`] function.
* 2. Parsing was cancelled due to a timeout that was set by an earlier call to
* the [`ts_parser_set_timeout_micros`] function. You can resume parsing from
* where the parser left out by calling [`ts_parser_parse`] again with the
* same arguments. Or you can start parsing from scratch by first calling
* [`ts_parser_reset`].
* 3. Parsing was cancelled using a cancellation flag that was set by an
* earlier call to [`ts_parser_set_cancellation_flag`]. You can resume parsing
* from where the parser left out by calling [`ts_parser_parse`] again with
* the same arguments.
*
* [`read`]: TSInput::read
* [`payload`]: TSInput::payload
* [`encoding`]: TSInput::encoding
* [`bytes_read`]: TSInput::read
*/
TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input);
/**
* Use the parser to parse some source code stored in one contiguous buffer.
* The first two parameters are the same as in the [`ts_parser_parse`] function
* above. The second two parameters indicate the location of the buffer and its
* length in bytes.
*/
TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, t_const_str string, t_u32 length);
/**
* Use the parser to parse some source code stored in one contiguous buffer with
* a given encoding. The first four parameters work the same as in the
* [`ts_parser_parse_string`] method above. The final parameter indicates whether
* the text is encoded as UTF8 or UTF16.
*/
TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, t_const_str string, t_u32 length, TSInputEncoding encoding);
/**
* Instruct the parser to start the next parse from the beginning.
*
* If the parser previously failed because of a timeout or a cancellation, then
* by default, it will resume where it left off on the next call to
* [`ts_parser_parse`] or other parsing functions. If you don't want to resume,
* and instead intend to use this parser to parse some other document, you must
* call [`ts_parser_reset`] first.
*/
void ts_parser_reset(TSParser *self);
/**
* Set the maximum duration in microseconds that parsing should be allowed to
* take before halting.
*
* If parsing takes longer than this, it will halt early, returning NULL.
* See [`ts_parser_parse`] for more information.
*/
void ts_parser_set_timeout_micros(TSParser *self, t_u64 timeout_micros);
/**
* Get the duration in microseconds that parsing is allowed to take.
*/
t_u64 ts_parser_timeout_micros(const TSParser *self);
/**
* Set the parser's current cancellation flag pointer.
*
* If a non-null pointer is assigned, then the parser will periodically read
* from this pointer during parsing. If it reads a non-zero value, it will
* halt early, returning NULL. See [`ts_parser_parse`] for more information.
*/
void ts_parser_set_cancellation_flag(TSParser *self, const size_t *flag);
/**
* Get the parser's current cancellation flag pointer.
*/
const size_t *ts_parser_cancellation_flag(const TSParser *self);
/**
* Set the logger that a parser should use during parsing.
*
* The parser does not take ownership over the logger payload. If a logger was
* previously assigned, the caller is responsible for releasing any memory
* owned by the previous logger.
*/
void ts_parser_set_logger(TSParser *self, TSLogger logger);
/**
* Get the parser's current logger.
*/
TSLogger ts_parser_logger(const TSParser *self);
/**
* Set the file descriptor to which the parser should write debugging graphs
* during parsing. The graphs are formatted in the DOT language. You may want
* to pipe these graphs directly to a `dot(1)` process in order to generate
* SVG output. You can turn off this logging by passing a negative number.
*/
void ts_parser_print_dot_graphs(TSParser *self, int fd);
/******************/
/* Section - Tree */
/******************/
/**
* Create a shallow copy of the syntax tree. This is very fast.
*
* You need to copy a syntax tree in order to use it on more than one thread at
* a time, as syntax trees are not thread safe.
*/
TSTree *ts_tree_copy(const TSTree *self);
/**
* Delete the syntax tree, freeing all of the memory that it used.
*/
void ts_tree_delete(TSTree *self);
/**
* Get the root node of the syntax tree.
*/
TSNode ts_tree_root_node(const TSTree *self);
/**
* Get the root node of the syntax tree, but with its position
* shifted forward by the given offset.
*/
TSNode ts_tree_root_node_with_offset(const TSTree *self, t_u32 offset_bytes, TSPoint offset_extent);
/**
* Get the language that was used to parse the syntax tree.
*/
const TSLanguage *ts_tree_language(const TSTree *self);
/**
* Get the array of included ranges that was used to parse the syntax tree.
*
* The returned pointer must be freed by the caller.
*/
TSRange *ts_tree_included_ranges(const TSTree *self, t_u32 *length);
/**
* Edit the syntax tree to keep it in sync with source code that has been
* edited.
*
* You must describe the edit both in terms of byte offsets and in terms of
* (row, column) coordinates.
*/
void ts_tree_edit(TSTree *self, const TSInputEdit *edit);
/**
* Compare an old edited syntax tree to a new syntax tree representing the same
* document, returning an array of ranges whose syntactic structure has changed.
*
* For this to work correctly, the old syntax tree must have been edited such
* that its ranges match up to the new tree. Generally, you'll want to call
* this function right after calling one of the [`ts_parser_parse`] functions.
* You need to pass the old tree that was passed to parse, as well as the new
* tree that was returned from that function.
*
* The returned array is allocated using `malloc` and the caller is responsible
* for freeing it using `free`. The length of the array will be written to the
* given `length` pointer.
*/
TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, t_u32 *length);
/**
* Write a DOT graph describing the syntax tree to the given file.
*/
void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor);
/******************/
/* Section - Node */
/******************/
/**
* Get the node's type as a null-terminated string.
*/
t_const_str ts_node_type(TSNode self);
/**
* Get the node's type as a numerical id.
*/
TSSymbol ts_node_symbol(TSNode self);
/**
* Get the node's language.
*/
const TSLanguage *ts_node_language(TSNode self);
/**
* Get the node's type as it appears in the grammar ignoring aliases as a
* null-terminated string.
*/
t_const_str ts_node_grammar_type(TSNode self);
/**
* Get the node's type as a numerical id as it appears in the grammar ignoring
* aliases. This should be used in [`ts_language_next_state`] instead of
* [`ts_node_symbol`].
*/
TSSymbol ts_node_grammar_symbol(TSNode self);
/**
* Get the node's start byte.
*/
t_u32 ts_node_start_byte(TSNode self);
/**
* Get the node's start position in terms of rows and columns.
*/
TSPoint ts_node_start_point(TSNode self);
/**
* Get the node's end byte.
*/
t_u32 ts_node_end_byte(TSNode self);
/**
* Get the node's end position in terms of rows and columns.
*/
TSPoint ts_node_end_point(TSNode self);
/**
* Get an S-expression representing the node as a string.
*
* This string is allocated with `malloc` and the caller is responsible for
* freeing it using `free`.
*/
char *ts_node_string(TSNode self);
/**
* Check if the node is null. Functions like [`ts_node_child`] and
* [`ts_node_next_sibling`] will return a null node to indicate that no such node
* was found.
*/
bool ts_node_is_null(TSNode self);
/**
* Check if the node is *named*. Named nodes correspond to named rules in the
* grammar, whereas *anonymous* nodes correspond to string literals in the
* grammar.
*/
bool ts_node_is_named(TSNode self);
/**
* Check if the node is *missing*. Missing nodes are inserted by the parser in
* order to recover from certain kinds of syntax errors.
*/
bool ts_node_is_missing(TSNode self);
/**
* Check if the node is *extra*. Extra nodes represent things like comments,
* which are not required the grammar, but can appear anywhere.
*/
bool ts_node_is_extra(TSNode self);
/**
* Check if a syntax node has been edited.
*/
bool ts_node_has_changes(TSNode self);
/**
* Check if the node is a syntax error or contains any syntax errors.
*/
bool ts_node_has_error(TSNode self);
/**
* Check if the node is a syntax error.
*/
bool ts_node_is_error(TSNode self);
/**
* Get this node's parse state.
*/
TSStateId ts_node_parse_state(TSNode self);
/**
* Get the parse state after this node.
*/
TSStateId ts_node_next_parse_state(TSNode self);
/**
* Get the node's immediate parent.
* Prefer [`ts_node_child_containing_descendant`] for
* iterating over the node's ancestors.
*/
TSNode ts_node_parent(TSNode self);
/**
* Get the node's child that contains `descendant`.
*/
TSNode ts_node_child_containing_descendant(TSNode self, TSNode descendant);
/**
* Get the node's child at the given index, where zero represents the first
* child.
*/
TSNode ts_node_child(TSNode self, t_u32 child_index);
/**
* Get the field name for node's child at the given index, where zero represents
* the first child. Returns NULL, if no field is found.
*/
t_const_str ts_node_field_name_for_child(TSNode self, t_u32 child_index);
/**
* Get the field name for node's child at the given index, where zero represents
* the first child. Returns NULL, if no field is found.
*/
TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index);
/**
* Get the node's number of children.
*/
t_u32 ts_node_child_count(TSNode self);
/**
* Get the node's *named* child at the given index.
*
* See also [`ts_node_is_named`].
*/
TSNode ts_node_named_child(TSNode self, t_u32 child_index);
/**
* Get the node's number of *named* children.
*
* See also [`ts_node_is_named`].
*/
t_u32 ts_node_named_child_count(TSNode self);
/**
* Get the node's child with the given field name.
*/
TSNode ts_node_child_by_field_name(TSNode self, t_const_str name, t_u32 name_length);
/**
* Get the node's child with the given numerical field id.
*
* You can convert a field name to an id using the
* [`ts_language_field_id_for_name`] function.
*/
TSNode ts_node_child_by_field_id(TSNode self, TSFieldId field_id);
/**
* Get the node's next / previous sibling.
*/
TSNode ts_node_next_sibling(TSNode self);
TSNode ts_node_prev_sibling(TSNode self);
/**
* Get the node's next / previous *named* sibling.
*/
TSNode ts_node_next_named_sibling(TSNode self);
TSNode ts_node_prev_named_sibling(TSNode self);
/**
* Get the node's first child that extends beyond the given byte offset.
*/
TSNode ts_node_first_child_for_byte(TSNode self, t_u32 byte);
/**
* Get the node's first named child that extends beyond the given byte offset.
*/
TSNode ts_node_first_named_child_for_byte(TSNode self, t_u32 byte);
/**
* Get the node's number of descendants, including one for the node itself.
*/
t_u32 ts_node_descendant_count(TSNode self);
/**
* Get the smallest node within this node that spans the given range of bytes
* or (row, column) positions.
*/
TSNode ts_node_descendant_for_byte_range(TSNode self, t_u32 start, t_u32 end);
TSNode ts_node_descendant_for_point_range(TSNode self, TSPoint start, TSPoint end);
/**
* Get the smallest named node within this node that spans the given range of
* bytes or (row, column) positions.
*/
TSNode ts_node_named_descendant_for_byte_range(TSNode self, t_u32 start, t_u32 end);
TSNode ts_node_named_descendant_for_point_range(TSNode self, TSPoint start, TSPoint end);
/**
* Edit the node to keep it in-sync with source code that has been edited.
*
* This function is only rarely needed. When you edit a syntax tree with the
* [`ts_tree_edit`] function, all of the nodes that you retrieve from the tree
* afterward will already reflect the edit. You only need to use [`ts_node_edit`]
* when you have a [`TSNode`] instance that you want to keep and continue to use
* after an edit.
*/
void ts_node_edit(TSNode *self, const TSInputEdit *edit);
/**
* Check if two nodes are identical.
*/
bool ts_node_eq(TSNode self, TSNode other);
/**********************/
/* Section - Language */
/**********************/
/**
* Get another reference to the given language.
*/
const TSLanguage *ts_language_copy(const TSLanguage *self);
/**
* Free any dynamically-allocated resources for this language, if
* this is the last reference.
*/
void ts_language_delete(const TSLanguage *self);
/**
* Get the number of distinct node types in the language.
*/
t_u32 ts_language_symbol_count(const TSLanguage *self);
/**
* Get the number of valid states in this language.
*/
t_u32 ts_language_state_count(const TSLanguage *self);
/**
* Get a node type string for the given numerical id.
*/
t_const_str ts_language_symbol_name(const TSLanguage *self, TSSymbol symbol);
/**
* Get the numerical id for the given node type string.
*/
TSSymbol ts_language_symbol_for_name(const TSLanguage *self, t_const_str string, t_u32 length, bool is_named);
/**
* Get the number of distinct field names in the language.
*/
t_u32 ts_language_field_count(const TSLanguage *self);
/**
* Get the field name string for the given numerical id.
*/
t_const_str ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id);
/**
* Get the numerical id for the given field name string.
*/
TSFieldId ts_language_field_id_for_name(const TSLanguage *self, t_const_str name, t_u32 name_length);
/**
* Check whether the given node type id belongs to named nodes, anonymous nodes,
* or a hidden nodes.
*
* See also [`ts_node_is_named`]. Hidden nodes are never returned from the API.
*/
TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol);
/**
* Get the ABI version number for this language. This version number is used
* to ensure that languages were generated by a compatible version of
* Tree-sitter.
*
* See also [`ts_parser_set_language`].
*/
t_u32 ts_language_version(const TSLanguage *self);
/**
* Get the next parse state. Combine this with lookahead iterators to generate
* completion suggestions or valid symbols in error nodes. Use
* [`ts_node_grammar_symbol`] for valid symbols.
*/
TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);
#endif // TREE_SITTER_API_H_

View file

@ -0,0 +1,270 @@
#ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_
#include "me/types.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "me/mem/mem.h"
#define Array(T) \
struct \
{ \
T *contents; \
t_u32 size; \
t_u32 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((t_u32)(_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 \
{ \
t_u32 _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 \
{ \
t_u32 _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)
{
mem_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, t_u32 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, t_u32 new_capacity)
{
if (new_capacity > self->capacity)
{
if (self->contents)
{
self->contents = mem_realloc(self->contents, new_capacity * element_size);
}
else
{
self->contents = mem_alloc(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, t_u32 count, size_t element_size)
{
t_u32 new_size = self->size + count;
if (new_size > self->capacity)
{
t_u32 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, t_u32 index, t_u32 old_count, t_u32 new_count, const void *elements)
{
t_u32 new_size = self->size + new_count - old_count;
t_u32 old_end = index + old_count;
t_u32 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; \
t_u32 size = (self)->size - *(_index); \
if (size == 0) \
break; \
int comparison; \
while (size > 1) \
{ \
t_u32 half_size = size / 2; \
t_u32 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))
#endif // TREE_SITTER_ARRAY_H_

View file

@ -0,0 +1,12 @@
#ifndef INPUT_H
#define INPUT_H
#include "me/types.h"
#define TS_DECODE_ERROR -1i
typedef t_u32 (*UnicodeDecodeFunction)(const t_u8 *string, t_u32 length, t_i32 *code_point);
t_u32 ts_decode_ascii(const t_u8 *string, t_u32 length, t_i32 *code_point);
#endif // INPUT_H

View file

@ -0,0 +1,36 @@
#ifndef LANGUAGE_H
#define LANGUAGE_H
#include "parser/parser.h"
#include "me/types.h"
#define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1)
#define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14
#define LANGUAGE_VERSION_USABLE_VIA_WASM 13
struct TableEntry
{
const TSParseAction *actions;
t_u32 action_count;
bool is_reusable;
};
typedef struct TableEntry TableEntry;
void ts_language_table_entry(const TSLanguage *, TSStateId, TSSymbol, TableEntry *);
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *, TSSymbol);
TSSymbol ts_language_public_symbol(const TSLanguage *, TSSymbol);
TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);
bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol);
const TSParseAction *ts_language_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol, t_u32 *count);
bool ts_language_has_reduce_action(const TSLanguage *self, TSStateId state, TSSymbol symbol);
t_u16 ts_language_lookup(const TSLanguage *self, TSStateId state, TSSymbol symbol);
bool ts_language_has_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol);
const bool *ts_language_enabled_external_tokens(const TSLanguage *self, t_u32 external_scanner_state);
const TSSymbol *ts_language_alias_sequence(const TSLanguage *self, t_u32 production_id);
TSSymbol ts_language_alias_at(const TSLanguage *self, t_u32 production_id, t_u32 child_index);
void ts_language_field_map(const TSLanguage *self, t_u32 production_id, const TSFieldMapEntry **start, const TSFieldMapEntry **end);
void ts_language_aliases_for_symbol(const TSLanguage *self, TSSymbol original_symbol, const TSSymbol **start, const TSSymbol **end);
#endif // LANGUAGE_H

View file

@ -0,0 +1,26 @@
#ifndef LENGTH_H
#define LENGTH_H
#include "parser/api.h"
#include "me/types.h"
struct Length
{
t_u32 bytes;
TSPoint extent;
};
typedef struct Length Length;
static const Length LENGTH_UNDEFINED = {0, {0, 1}};
static const Length LENGTH_MAX = {UINT32_MAX, {UINT32_MAX, UINT32_MAX}};
Length length_saturating_sub(Length len1, Length len2);
Length length_zero(void);
Length length_sub(Length len1, Length len2);
Length length_add(Length len1, Length len2);
Length length_min(Length len1, Length len2);
Length length_max(Length len1, Length len2);
bool length_is_undefined(Length length);
#endif

View file

@ -0,0 +1,41 @@
#ifndef LEXER_H
#define LEXER_H
#include "parser/api.h"
#include "parser/length.h"
#include "parser/parser.h"
#include "me/types.h"
struct Lexer
{
TSLexer data;
Length current_position;
Length token_start_position;
Length token_end_position;
TSRange *included_ranges;
const t_u8 *chunk;
TSInput input;
TSLogger logger;
t_u32 included_range_count;
t_u32 current_included_range_index;
t_u32 chunk_start;
t_u32 chunk_size;
t_u32 lookahead_size;
bool did_get_column;
t_u8 debug_buffer[TREE_SITTER_SERIALIZATION_BUFFER_SIZE];
};
typedef struct Lexer Lexer;
void ts_lexer_init(Lexer *self);
void ts_lexer_delete(Lexer *self);
void ts_lexer_set_input(Lexer *self, TSInput input);
void ts_lexer_reset(Lexer *self, Length length);
void ts_lexer_start(Lexer *self);
void ts_lexer_finish(Lexer *self, t_u32 *lookahead);
void ts_lexer_advance_to_end(Lexer *self);
void ts_lexer_mark_end(Lexer *self);
bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, t_u32 count);
TSRange *ts_lexer_included_ranges(const Lexer *self, t_u32 *count);
#endif // LEXER_H

View file

@ -0,0 +1,270 @@
#ifndef PARSER_H
#define PARSER_H
#include "me/types.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 t_u16 TSStateId;
typedef t_u16 TSSymbol;
typedef t_u16 TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct TSFieldMapEntry
{
TSFieldId field_id;
t_u8 child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct TSFieldMapSlice
{
t_u16 index;
t_u16 length;
} TSFieldMapSlice;
typedef struct TSSymbolMetadata
{
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer
{
t_i32 lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
t_u32 (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum TSParseActionType
{
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union TSParseAction {
struct TSParseActionShift
{
t_u8 type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct TSParseActionReduce
{
t_u8 type;
t_u8 child_count;
TSSymbol symbol;
t_i16 dynamic_precedence;
t_u16 production_id;
} reduce;
t_u8 type;
} TSParseAction;
typedef struct TSLexMode
{
t_u16 lex_state;
t_u16 external_lex_state;
} TSLexMode;
typedef union TSParseActionEntry {
TSParseAction action;
struct TSParseActionEntryInner
{
t_u8 count;
bool reusable;
} entry;
} TSParseActionEntry;
typedef struct TSCharacterRange
{
t_i32 start;
t_i32 end;
} TSCharacterRange;
struct TSLanguage
{
t_u32 version;
t_u32 symbol_count;
t_u32 alias_count;
t_u32 token_count;
t_u32 external_token_count;
t_u32 state_count;
t_u32 large_state_count;
t_u32 production_id_count;
t_u32 field_count;
t_u16 max_alias_sequence_length;
const t_u16 *parse_table;
const t_u16 *small_parse_table;
const t_u32 *small_parse_table_map;
const TSParseActionEntry *parse_actions;
t_const_str const *symbol_names;
t_const_str const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const t_u16 *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 ExternalScannerDefinition
{
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
t_u32 (*serialize)(void *, t_u8 *);
void (*deserialize)(void *, const t_u8 *, t_u32);
} external_scanner;
const TSStateId *primary_state_ids;
};
static inline bool set_contains(TSCharacterRange *ranges, t_u32 len, t_i32 lookahead)
{
t_u32 index = 0;
t_u32 size = len - index;
while (size > 1)
{
t_u32 half_size = size / 2;
t_u32 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
*/
#define UNUSED __attribute__((unused))
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
t_i32 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 t_u16 map[] = {__VA_ARGS__}; \
for (t_u32 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 \
} \
}
#endif // PARSER_H

View file

@ -0,0 +1,21 @@
#ifndef POINT_H
#define POINT_H
#include "parser/api.h"
#include "me/types.h"
#define POINT_ZERO ((TSPoint){0, 0})
#define POINT_MAX ((TSPoint){UINT32_MAX, UINT32_MAX})
TSPoint point_max(TSPoint a, TSPoint b);
TSPoint point_min(TSPoint a, TSPoint b);
TSPoint point__new(t_u32 row, t_u32 column);
TSPoint point_add(TSPoint a, TSPoint b);
TSPoint point_sub(TSPoint a, TSPoint b);
bool point_lte(TSPoint a, TSPoint b);
bool point_lt(TSPoint a, TSPoint b);
bool point_gte(TSPoint a, TSPoint b);
bool point_gt(TSPoint a, TSPoint b);
bool point_eq(TSPoint a, TSPoint b);
#endif

View file

@ -0,0 +1,21 @@
#ifndef REDUCE_ACTION_H
#define REDUCE_ACTION_H
#include "parser/api.h"
#include "parser/array.h"
#include "me/types.h"
struct ReduceAction
{
t_u32 count;
TSSymbol symbol;
int dynamic_precedence;
t_u16 production_id;
};
typedef struct ReduceAction ReduceAction;
typedef Array(ReduceAction) ReduceActionSet;
void ts_reduce_action_set_add(ReduceActionSet *self, ReduceAction new_action);
#endif // REDUCE_ACTION_H

View file

@ -0,0 +1,125 @@
#ifndef PARSE_STACK_H
#define PARSE_STACK_H
#include "parser/array.h"
#include "parser/subtree.h"
#include "me/types.h"
typedef struct Stack Stack;
typedef t_u32 StackVersion;
#define STACK_VERSION_NONE ((StackVersion)-1)
typedef struct StackSlice
{
SubtreeArray subtrees;
StackVersion version;
} StackSlice;
typedef Array(StackSlice) StackSliceArray;
typedef struct StackSummaryEntry
{
Length position;
t_u32 depth;
TSStateId state;
} StackSummaryEntry;
typedef Array(StackSummaryEntry) StackSummary;
typedef void (*StackIterateCallback)(void *, TSStateId, t_u32);
// Create a stack.
Stack *ts_stack_new(SubtreePool *);
// Release the memory reserved for a given stack.
void ts_stack_delete(Stack *);
// Get the stack's current number of versions.
t_u32 ts_stack_version_count(const Stack *);
// Get the state at the top of the given version of the stack. If the stack is
// empty, this returns the initial state, 0.
TSStateId ts_stack_state(const Stack *, StackVersion);
// Get the last external token associated with a given version of the stack.
Subtree ts_stack_last_external_token(const Stack *, StackVersion);
// Set the last external token associated with a given version of the stack.
void ts_stack_set_last_external_token(Stack *, StackVersion, Subtree);
// Get the position of the given version of the stack within the document.
Length ts_stack_position(const Stack *, StackVersion);
// Push a tree and state onto the given version of the stack.
//
// This transfers ownership of the tree to the Stack. Callers that
// need to retain ownership of the tree for their own purposes should
// first retain the tree.
void ts_stack_push(Stack *, StackVersion, Subtree, bool, TSStateId);
// Pop the given number of entries from the given version of the stack. This
// operation can increase the number of stack versions by revealing multiple
// versions which had previously been merged. It returns an array that
// specifies the index of each revealed version and the trees that were
// removed from that version.
StackSliceArray ts_stack_pop_count(Stack *, StackVersion, t_u32 count);
// Remove an error at the top of the given version of the stack.
SubtreeArray ts_stack_pop_error(Stack *, StackVersion);
// Remove any pending trees from the top of the given version of the stack.
StackSliceArray ts_stack_pop_pending(Stack *, StackVersion);
// Remove any all trees from the given version of the stack.
StackSliceArray ts_stack_pop_all(Stack *, StackVersion);
// Get the maximum number of tree nodes reachable from this version of the stack
// since the last error was detected.
t_u32 ts_stack_node_count_since_error(const Stack *, StackVersion);
int ts_stack_dynamic_precedence(Stack *, StackVersion);
bool ts_stack_has_advanced_since_error(const Stack *, StackVersion);
// Compute a summary of all the parse states near the top of the given
// version of the stack and store the summary for later retrieval.
void ts_stack_record_summary(Stack *, StackVersion, t_u32 max_depth);
// Retrieve a summary of all the parse states near the top of the
// given version of the stack.
StackSummary *ts_stack_get_summary(Stack *, StackVersion);
// Get the total cost of all errors on the given version of the stack.
t_u32 ts_stack_error_cost(const Stack *, StackVersion version);
// Merge the given two stack versions if possible, returning true
// if they were successfully merged and false otherwise.
bool ts_stack_merge(Stack *, StackVersion, StackVersion);
// Determine whether the given two stack versions can be merged.
bool ts_stack_can_merge(Stack *, StackVersion, StackVersion);
Subtree ts_stack_resume(Stack *, StackVersion);
void ts_stack_pause(Stack *, StackVersion, Subtree);
void ts_stack_halt(Stack *, StackVersion);
bool ts_stack_is_active(const Stack *, StackVersion);
bool ts_stack_is_paused(const Stack *, StackVersion);
bool ts_stack_is_halted(const Stack *, StackVersion);
void ts_stack_renumber_version(Stack *, StackVersion, StackVersion);
void ts_stack_swap_versions(Stack *, StackVersion, StackVersion);
StackVersion ts_stack_copy_version(Stack *, StackVersion);
// Remove the given version from the stack.
void ts_stack_remove_version(Stack *, StackVersion);
void ts_stack_clear(Stack *);
#endif // PARSE_STACK_H

View file

@ -0,0 +1,383 @@
#ifndef SUBTREE_H
#define SUBTREE_H
#include "parser/api.h"
#include "parser/array.h"
#include "parser/length.h"
#include "parser/parser.h"
#include "me/types.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define TS_BIG_ENDIAN 0
#define TS_PTR_SIZE 64
#define TS_TREE_STATE_NONE USHRT_MAX
#define NULL_SUBTREE ((Subtree){.ptr = NULL})
struct ExternalScannerState
{
union {
char *long_data;
char short_data[24];
};
t_u32 length;
};
// The serialized state of an external scanner.
//
// Every time an external token subtree is created after a call to an
// external scanner, the scanner's `serialize` function is called to
// retrieve a serialized copy of its state. The bytes are then copied
// onto the subtree itself so that the scanner's state can later be
// restored using its `deserialize` function.
//
// Small byte arrays are stored inline, and long ones are allocated
// separately on the heap.
typedef struct ExternalScannerState ExternalScannerState;
// A compact representation of a subtree.
//
// This representation is used for small leaf nodes that are not
// errors, and were not created by an external scanner.
//
// The idea behind the layout of this struct is that the `is_inline`
// bit will fall exactly into the same location as the least significant
// bit of the pointer in `Subtree` or `MutableSubtree`, respectively.
// Because of alignment, for any valid pointer this will be 0, giving
// us the opportunity to make use of this bit to signify whether to use
// the pointer or the inline struct.
typedef struct SubtreeInlineData SubtreeInlineData;
struct SubtreeInlineData
{
bool is_inline : 1;
bool visible : 1;
bool named : 1;
bool extra : 1;
bool has_changes : 1;
bool is_missing : 1;
bool is_keyword : 1;
t_u8 symbol;
t_u16 parse_state;
t_u8 padding_columns;
t_u8 padding_rows : 4;
t_u8 lookahead_bytes : 4;
t_u8 padding_bytes;
t_u8 size_bytes;
};
// A heap-allocated representation of a subtree.
//
// This representation is used for parent nodes, external tokens,
// errors, and other leaf nodes whose data is too large to fit into
// the inline representation.
typedef struct SubtreeHeapData
{
volatile t_u32 ref_count;
Length padding;
Length size;
t_u32 lookahead_bytes;
t_u32 error_cost;
t_u32 child_count;
TSSymbol symbol;
TSStateId parse_state;
bool visible : 1;
bool named : 1;
bool extra : 1;
bool fragile_left : 1;
bool fragile_right : 1;
bool has_changes : 1;
bool has_external_tokens : 1;
bool has_external_scanner_state_change : 1;
bool depends_on_column : 1;
bool is_missing : 1;
bool is_keyword : 1;
union {
// Non-terminal subtrees (`child_count > 0`)
struct
{
t_u32 visible_child_count;
t_u32 named_child_count;
t_u32 visible_descendant_count;
t_i32 dynamic_precedence;
t_u16 repeat_depth;
t_u16 production_id;
struct
{
TSSymbol symbol;
TSStateId parse_state;
} first_leaf;
};
// External terminal subtrees (`child_count == 0 && has_external_tokens`)
ExternalScannerState external_scanner_state;
// Error terminal subtrees (`child_count == 0 && symbol == ts_builtin_sym_error`)
t_i32 lookahead_char;
};
} SubtreeHeapData;
// The fundamental building block of a syntax tree.
typedef union Subtree {
SubtreeInlineData data;
const SubtreeHeapData *ptr;
} Subtree;
// Like Subtree, but mutable.
typedef union MutableSubtree {
SubtreeInlineData data;
SubtreeHeapData *ptr;
} MutableSubtree;
typedef Array(Subtree) SubtreeArray;
typedef Array(MutableSubtree) MutableSubtreeArray;
typedef struct SubtreePool
{
MutableSubtreeArray free_trees;
MutableSubtreeArray tree_stack;
} SubtreePool;
void ts_external_scanner_state_init(ExternalScannerState *, const t_u8 *, t_u32);
const t_u8 *ts_external_scanner_state_data(const ExternalScannerState *);
bool ts_external_scanner_state_eq(const ExternalScannerState *self, const t_u8 *, t_u32);
void ts_external_scanner_state_delete(ExternalScannerState *self);
void ts_subtree_array_copy(SubtreeArray, SubtreeArray *);
void ts_subtree_array_clear(SubtreePool *, SubtreeArray *);
void ts_subtree_array_delete(SubtreePool *, SubtreeArray *);
void ts_subtree_array_remove_trailing_extras(SubtreeArray *, SubtreeArray *);
void ts_subtree_array_reverse(SubtreeArray *);
SubtreePool ts_subtree_pool_new(t_u32 capacity);
void ts_subtree_pool_delete(SubtreePool *);
Subtree ts_subtree_new_leaf(SubtreePool *, TSSymbol, Length, Length, t_u32, TSStateId, bool, bool, bool, const TSLanguage *);
Subtree ts_subtree_new_error(SubtreePool *, t_i32, Length, Length, t_u32, TSStateId, const TSLanguage *);
MutableSubtree ts_subtree_new_node(TSSymbol, SubtreeArray *, t_u32, const TSLanguage *);
Subtree ts_subtree_new_error_node(SubtreeArray *, bool, const TSLanguage *);
Subtree ts_subtree_new_missing_leaf(SubtreePool *, TSSymbol, Length, t_u32, const TSLanguage *);
MutableSubtree ts_subtree_make_mut(SubtreePool *, Subtree);
void ts_subtree_retain(Subtree);
void ts_subtree_release(SubtreePool *, Subtree);
int ts_subtree_compare(Subtree, Subtree, SubtreePool *);
void ts_subtree_set_symbol(MutableSubtree *, TSSymbol, const TSLanguage *);
void ts_subtree_summarize(MutableSubtree, const Subtree *, t_u32, const TSLanguage *);
void ts_subtree_summarize_children(MutableSubtree, const TSLanguage *);
void ts_subtree_balance(Subtree, SubtreePool *, const TSLanguage *);
Subtree ts_subtree_edit(Subtree, const TSInputEdit *edit, SubtreePool *);
char *ts_subtree_string(Subtree, TSSymbol, bool, const TSLanguage *, bool include_all);
void ts_subtree_print_dot_graph(Subtree, const TSLanguage *, FILE *);
Subtree ts_subtree_last_external_token(Subtree);
const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self);
bool ts_subtree_external_scanner_state_eq(Subtree, Subtree);
static inline TSSymbol ts_subtree_symbol(Subtree self)
{
return ((self).data.is_inline ? (self).data.symbol : (self).ptr->symbol);
}
static inline bool ts_subtree_visible(Subtree self)
{
return ((self).data.is_inline ? (self).data.visible : (self).ptr->visible);
}
static inline bool ts_subtree_named(Subtree self)
{
return ((self).data.is_inline ? (self).data.named : (self).ptr->named);
}
static inline bool ts_subtree_extra(Subtree self)
{
return ((self).data.is_inline ? (self).data.extra : (self).ptr->extra);
}
static inline bool ts_subtree_has_changes(Subtree self)
{
return ((self).data.is_inline ? (self).data.has_changes : (self).ptr->has_changes);
}
static inline bool ts_subtree_missing(Subtree self)
{
return ((self).data.is_inline ? (self).data.is_missing : (self).ptr->is_missing);
}
static inline bool ts_subtree_is_keyword(Subtree self)
{
return ((self).data.is_inline ? (self).data.is_keyword : (self).ptr->is_keyword);
}
static inline TSStateId ts_subtree_parse_state(Subtree self)
{
return ((self).data.is_inline ? (self).data.parse_state : (self).ptr->parse_state);
}
static inline t_u32 ts_subtree_lookahead_bytes(Subtree self)
{
return ((self).data.is_inline ? (self).data.lookahead_bytes : (self).ptr->lookahead_bytes);
}
// Get the size needed to store a heap-allocated subtree with the given
// number of children.
static inline size_t ts_subtree_alloc_size(t_u32 child_count)
{
return child_count * sizeof(Subtree) + sizeof(SubtreeHeapData);
}
// Get a subtree's children, which are allocated immediately before the
// tree's own heap data.
#define ts_subtree_children(self) ((self).data.is_inline ? NULL : (Subtree *)((self).ptr) - (self).ptr->child_count)
static inline void ts_subtree_set_extra(MutableSubtree *self, bool is_extra)
{
if (self->data.is_inline)
self->data.extra = is_extra;
else
self->ptr->extra = is_extra;
}
static inline TSSymbol ts_subtree_leaf_symbol(Subtree self)
{
if (self.data.is_inline)
return self.data.symbol;
if (self.ptr->child_count == 0)
return self.ptr->symbol;
return self.ptr->first_leaf.symbol;
}
static inline TSStateId ts_subtree_leaf_parse_state(Subtree self)
{
if (self.data.is_inline)
return self.data.parse_state;
if (self.ptr->child_count == 0)
return self.ptr->parse_state;
return self.ptr->first_leaf.parse_state;
}
static inline Length ts_subtree_padding(Subtree self)
{
if (self.data.is_inline)
return ((Length){self.data.padding_bytes, {self.data.padding_rows, self.data.padding_columns}});
else
return self.ptr->padding;
}
static inline Length ts_subtree_size(Subtree self)
{
if (self.data.is_inline)
return ((Length){self.data.size_bytes, {0, self.data.size_bytes}});
else
return self.ptr->size;
}
static inline Length ts_subtree_total_size(Subtree self)
{
return (length_add(ts_subtree_padding(self), ts_subtree_size(self)));
}
static inline t_u32 ts_subtree_total_bytes(Subtree self)
{
return (ts_subtree_total_size(self).bytes);
}
static inline t_u32 ts_subtree_child_count(Subtree self)
{
return (self.data.is_inline ? 0 : self.ptr->child_count);
}
static inline t_u32 ts_subtree_repeat_depth(Subtree self)
{
return (self.data.is_inline ? 0 : self.ptr->repeat_depth);
}
static inline t_u32 ts_subtree_is_repetition(Subtree self)
{
return (self.data.is_inline ? 0 : !self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0);
}
static inline t_u32 ts_subtree_visible_descendant_count(Subtree self)
{
return ((self.data.is_inline || self.ptr->child_count == 0) ? 0 : self.ptr->visible_descendant_count);
}
static inline t_u32 ts_subtree_visible_child_count(Subtree self)
{
if (ts_subtree_child_count(self) > 0)
return (self.ptr->visible_child_count);
else
return 0;
}
static inline t_u32 ts_subtree_error_cost(Subtree self)
{
if (ts_subtree_missing(self))
return (ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY);
else
return (self.data.is_inline ? 0 : self.ptr->error_cost);
}
static inline t_i32 ts_subtree_dynamic_precedence(Subtree self)
{
return ((self.data.is_inline || self.ptr->child_count == 0) ? 0 : self.ptr->dynamic_precedence);
}
static inline t_u16 ts_subtree_production_id(Subtree self)
{
if (ts_subtree_child_count(self) > 0)
return (self.ptr->production_id);
else
return (0);
}
static inline bool ts_subtree_fragile_left(Subtree self)
{
return (self.data.is_inline ? false : self.ptr->fragile_left);
}
static inline bool ts_subtree_fragile_right(Subtree self)
{
return (self.data.is_inline ? false : self.ptr->fragile_right);
}
static inline bool ts_subtree_has_external_tokens(Subtree self)
{
return (self.data.is_inline ? false : self.ptr->has_external_tokens);
}
static inline bool ts_subtree_has_external_scanner_state_change(Subtree self)
{
return (self.data.is_inline ? false : self.ptr->has_external_scanner_state_change);
}
static inline bool ts_subtree_depends_on_column(Subtree self)
{
return (self.data.is_inline ? false : self.ptr->depends_on_column);
}
static inline bool ts_subtree_is_fragile(Subtree self)
{
return (self.data.is_inline ? false : (self.ptr->fragile_left || self.ptr->fragile_right));
}
static inline bool ts_subtree_is_error(Subtree self)
{
return (ts_subtree_symbol(self) == ts_builtin_sym_error);
}
static inline bool ts_subtree_is_eof(Subtree self)
{
return (ts_subtree_symbol(self) == ts_builtin_sym_end);
}
static inline Subtree ts_subtree_from_mut(MutableSubtree self)
{
Subtree result;
result.data = self.data;
return (result);
}
static inline MutableSubtree ts_subtree_to_mut_unsafe(Subtree self)
{
MutableSubtree result;
result.data = self.data;
return (result);
}
#endif // SUBTREE_H

View file

@ -0,0 +1,30 @@
#ifndef TREE_H
#define TREE_H
#include "parser/subtree.h"
#include "me/types.h"
typedef struct ParentCacheEntry ParentCacheEntry;
struct TSTree
{
Subtree root;
const TSLanguage *language;
TSRange *included_ranges;
t_u32 included_range_count;
};
struct ParentCacheEntry
{
const Subtree *child;
const Subtree *parent;
Length position;
TSSymbol alias_symbol;
};
TSTree *ts_tree_new(Subtree root, const TSLanguage *language, const TSRange *, t_u32);
TSNode ts_node_new(const TSTree *, const Subtree *, Length, TSSymbol);
#endif // TREE_H