WIP parser norminettation

This commit is contained in:
Maieul BOYER 2024-04-29 15:38:44 +02:00
parent 16f49181b5
commit ff4b0c471f
No known key found for this signature in database
44 changed files with 1130 additions and 265 deletions

View file

@ -1,5 +1,5 @@
#include "./language.h"
#include "api.h"
#include "parser/api.h"
#include <string.h>
const TSLanguage *ts_language_copy(const TSLanguage *self) {

View file

@ -4,11 +4,11 @@
#include <stdlib.h>
#include <stdbool.h>
#include "./point.h"
#include "api.h"
#include "parser/api.h"
typedef struct {
uint32_t bytes;
TSPoint extent;
t_point extent;
} Length;
static const Length LENGTH_UNDEFINED = {0, {0, 1}};

View file

@ -18,7 +18,7 @@
static const int32_t BYTE_ORDER_MARK = 0xFEFF;
static const TSRange DEFAULT_RANGE = {.start_point =
static const t_parser_range DEFAULT_RANGE = {.start_point =
{
.row = 0,
.column = 0,
@ -121,7 +121,7 @@ static void ts_lexer_goto(Lexer *self, Length position)
bool found_included_range = false;
for (unsigned i = 0; i < self->included_range_count; i++)
{
TSRange *included_range = &self->included_ranges[i];
t_parser_range *included_range = &self->included_ranges[i];
if (included_range->end_byte > self->current_position.bytes &&
included_range->end_byte > included_range->start_byte)
{
@ -159,7 +159,7 @@ static void ts_lexer_goto(Lexer *self, Length position)
else
{
self->current_included_range_index = self->included_range_count;
TSRange *last_included_range =
t_parser_range *last_included_range =
&self->included_ranges[self->included_range_count - 1];
self->current_position = (Length){
.bytes = last_included_range->end_byte,
@ -188,7 +188,7 @@ static void ts_lexer__do_advance(Lexer *self, bool skip)
}
}
const TSRange *current_range =
const t_parser_range *current_range =
&self->included_ranges[self->current_included_range_index];
while (self->current_position.bytes >= current_range->end_byte ||
current_range->end_byte == current_range->start_byte)
@ -253,12 +253,12 @@ static void ts_lexer__mark_end(TSLexer *_self)
// If the lexer is right at the beginning of included range,
// then the token should be considered to end at the *end* of the
// previous included range, rather than here.
TSRange *current_included_range =
t_parser_range *current_included_range =
&self->included_ranges[self->current_included_range_index];
if (self->current_included_range_index > 0 &&
self->current_position.bytes == current_included_range->start_byte)
{
TSRange *previous_included_range = current_included_range - 1;
t_parser_range *previous_included_range = current_included_range - 1;
self->token_end_position = (Length){
previous_included_range->end_byte,
previous_included_range->end_point,
@ -308,7 +308,7 @@ static bool ts_lexer__is_at_included_range_start(const TSLexer *_self)
const Lexer *self = (const Lexer *)_self;
if (self->current_included_range_index < self->included_range_count)
{
TSRange *current_range =
t_parser_range *current_range =
&self->included_ranges[self->current_included_range_index];
return self->current_position.bytes == current_range->start_byte;
}
@ -434,12 +434,12 @@ void ts_lexer_mark_end(Lexer *self)
ts_lexer__mark_end(&self->data);
}
bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges,
bool ts_lexer_set_included_ranges(Lexer *self, const t_parser_range *ranges,
uint32_t count)
{
ranges = &DEFAULT_RANGE;
count = 1;
size_t size = count * sizeof(TSRange);
size_t size = count * sizeof(t_parser_range);
self->included_ranges = realloc(self->included_ranges, size);
memcpy(self->included_ranges, ranges, size);
self->included_range_count = count;
@ -447,7 +447,7 @@ bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges,
return true;
}
TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count)
t_parser_range *ts_lexer_included_ranges(const Lexer *self, uint32_t *count)
{
*count = self->included_range_count;
return self->included_ranges;

View file

@ -7,7 +7,7 @@ extern "C" {
#include "./length.h"
#include "./subtree.h"
#include "api.h"
#include "parser/api.h"
#include "./parser.h"
typedef struct {
@ -16,7 +16,7 @@ typedef struct {
Length token_start_position;
Length token_end_position;
TSRange *included_ranges;
t_parser_range *included_ranges;
const char *chunk;
TSInput input;
TSLogger logger;
@ -39,8 +39,8 @@ void ts_lexer_start(Lexer *);
void ts_lexer_finish(Lexer *, uint32_t *);
void ts_lexer_advance_to_end(Lexer *);
void ts_lexer_mark_end(Lexer *);
bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, uint32_t count);
TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count);
bool ts_lexer_set_included_ranges(Lexer *self, const t_parser_range *ranges, uint32_t count);
t_parser_range *ts_lexer_included_ranges(const Lexer *self, uint32_t *count);
#ifdef __cplusplus
}

View file

@ -37,8 +37,8 @@ uint32_t ts_node_start_byte(TSNode self) {
return self.context[0];
}
TSPoint ts_node_start_point(TSNode self) {
return (TSPoint) {self.context[1], self.context[2]};
t_point ts_node_start_point(TSNode self) {
return (t_point) {self.context[1], self.context[2]};
}
static inline uint32_t ts_node__alias(const TSNode *self) {
@ -366,8 +366,8 @@ static inline TSNode ts_node__descendant_for_byte_range(
static inline TSNode ts_node__descendant_for_point_range(
TSNode self,
TSPoint range_start,
TSPoint range_end,
t_point range_start,
t_point range_end,
bool include_anonymous
) {
TSNode node = self;
@ -380,7 +380,7 @@ static inline TSNode ts_node__descendant_for_point_range(
TSNode child;
NodeChildIterator iterator = ts_node_iterate_children(&node);
while (ts_node_child_iterator_next(&iterator, &child)) {
TSPoint node_end = iterator.position.extent;
t_point node_end = iterator.position.extent;
// The end of this node must extend far enough forward to touch
// the end of the range and exceed the start of the range.
@ -409,7 +409,7 @@ uint32_t ts_node_end_byte(TSNode self) {
return ts_node_start_byte(self) + ts_subtree_size(ts_node__subtree(self)).bytes;
}
TSPoint ts_node_end_point(TSNode self) {
t_point ts_node_end_point(TSNode self) {
return point_add(ts_node_start_point(self), ts_subtree_size(ts_node__subtree(self)).extent);
}
@ -742,23 +742,23 @@ TSNode ts_node_named_descendant_for_byte_range(
TSNode ts_node_descendant_for_point_range(
TSNode self,
TSPoint start,
TSPoint end
t_point start,
t_point end
) {
return ts_node__descendant_for_point_range(self, start, end, true);
}
TSNode ts_node_named_descendant_for_point_range(
TSNode self,
TSPoint start,
TSPoint end
t_point start,
t_point end
) {
return ts_node__descendant_for_point_range(self, start, end, false);
}
void ts_node_edit(TSNode *self, const TSInputEdit *edit) {
uint32_t start_byte = ts_node_start_byte(*self);
TSPoint start_point = ts_node_start_point(*self);
t_point start_point = ts_node_start_point(*self);
if (start_byte >= edit->old_end_byte) {
start_byte = edit->new_end_byte + (start_byte - edit->old_end_byte);

View file

@ -1,5 +1,3 @@
#define _POSIX_C_SOURCE 200112L
#include "./array.h"
#include "./error_costs.h"
#include "./language.h"
@ -11,7 +9,7 @@
#include "./subtree.h"
#include "./tree.h"
#include "api.h"
#include "parser/api.h"
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
@ -19,7 +17,8 @@
#include <stdio.h>
#include <time.h>
typedef Array(TSRange) ArrayRange;
#include "me/vec/vec_parser_range.h"
typedef uint64_t TSDuration;
typedef uint64_t TSClock;
@ -101,7 +100,7 @@ static const unsigned MAX_VERSION_COUNT = 6;
static const unsigned MAX_VERSION_COUNT_OVERFLOW = 4;
static const unsigned MAX_SUMMARY_DEPTH = 16;
static const unsigned MAX_COST_DIFFERENCE = 16 * ERROR_COST_PER_SKIPPED_TREE;
//static const unsigned OP_COUNT_PER_TIMEOUT_CHECK = 100;
// static const unsigned OP_COUNT_PER_TIMEOUT_CHECK = 100;
typedef struct
{
@ -132,7 +131,7 @@ struct TSParser
unsigned operation_count;
const volatile size_t *cancellation_flag;
Subtree old_tree;
ArrayRange included_range_differences;
t_vec_parser_range included_range_differences;
unsigned included_range_difference_index;
bool has_scanner_error;
};
@ -163,7 +162,7 @@ typedef struct
// StringInput
static const char *ts_string_input_read(void *_self, uint32_t byte,
TSPoint point, uint32_t *length)
t_point point, uint32_t *length)
{
(void)point;
TSStringInput *self = (TSStringInput *)_self;
@ -1994,7 +1993,7 @@ TSParser *ts_parser_new(void)
self->end_clock = 0;
self->operation_count = 0;
self->old_tree = NULL_SUBTREE;
self->included_range_differences = (ArrayRange)array_new();
self->included_range_differences = vec_parser_range_new(0, NULL);
self->included_range_difference_index = 0;
ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE);
return self;
@ -2011,7 +2010,7 @@ void ts_parser_delete(TSParser *self)
{
array_delete(&self->reduce_actions);
}
if (self->included_range_differences.contents)
if (self->included_range_differences.buffer)
{
array_delete(&self->included_range_differences);
}
@ -2105,13 +2104,14 @@ void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout_micros)
self->timeout_duration = 0;
}
bool ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges,
bool ts_parser_set_included_ranges(TSParser *self, const t_parser_range *ranges,
uint32_t count)
{
return ts_lexer_set_included_ranges(&self->lexer, ranges, count);
}
const TSRange *ts_parser_included_ranges(const TSParser *self, uint32_t *count)
const t_parser_range *ts_parser_included_ranges(const TSParser *self,
uint32_t *count)
{
return ts_lexer_included_ranges(&self->lexer, count);
}
@ -2148,7 +2148,7 @@ TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input)
return NULL;
ts_lexer_set_input(&self->lexer, input);
array_clear(&self->included_range_differences);
self->included_range_differences.len = 0;
self->included_range_difference_index = 0;
if (ts_parser_has_outstanding_parse(self))
@ -2221,11 +2221,11 @@ TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input)
}
while (self->included_range_difference_index <
self->included_range_differences.size)
self->included_range_differences.len)
{
TSRange *range =
t_parser_range *range =
&self->included_range_differences
.contents[self->included_range_difference_index];
.buffer[self->included_range_difference_index];
if (range->end_byte <= position)
{
self->included_range_difference_index++;

View file

@ -1,58 +1,58 @@
#ifndef TREE_SITTER_POINT_H_
#define TREE_SITTER_POINT_H_
#include "api.h"
#include "parser/api.h"
#define POINT_ZERO ((TSPoint) {0, 0})
#define POINT_MAX ((TSPoint) {UINT32_MAX, UINT32_MAX})
#define POINT_ZERO ((t_point) {0, 0})
#define POINT_MAX ((t_point) {UINT32_MAX, UINT32_MAX})
static inline TSPoint point__new(unsigned row, unsigned column) {
TSPoint result = {row, column};
static inline t_point point__new(unsigned row, unsigned column) {
t_point result = {row, column};
return result;
}
static inline TSPoint point_add(TSPoint a, TSPoint b) {
static inline t_point point_add(t_point a, t_point b) {
if (b.row > 0)
return point__new(a.row + b.row, b.column);
else
return point__new(a.row, a.column + b.column);
}
static inline TSPoint point_sub(TSPoint a, TSPoint b) {
static inline t_point point_sub(t_point a, t_point b) {
if (a.row > b.row)
return point__new(a.row - b.row, a.column);
else
return point__new(0, a.column - b.column);
}
static inline bool point_lte(TSPoint a, TSPoint b) {
static inline bool point_lte(t_point a, t_point b) {
return (a.row < b.row) || (a.row == b.row && a.column <= b.column);
}
static inline bool point_lt(TSPoint a, TSPoint b) {
static inline bool point_lt(t_point a, t_point b) {
return (a.row < b.row) || (a.row == b.row && a.column < b.column);
}
static inline bool point_gt(TSPoint a, TSPoint b) {
static inline bool point_gt(t_point a, t_point b) {
return (a.row > b.row) || (a.row == b.row && a.column > b.column);
}
static inline bool point_gte(TSPoint a, TSPoint b) {
static inline bool point_gte(t_point a, t_point b) {
return (a.row > b.row) || (a.row == b.row && a.column >= b.column);
}
static inline bool point_eq(TSPoint a, TSPoint b) {
static inline bool point_eq(t_point a, t_point b) {
return a.row == b.row && a.column == b.column;
}
static inline TSPoint point_min(TSPoint a, TSPoint b) {
static inline t_point point_min(t_point a, t_point b) {
if (a.row < b.row || (a.row == b.row && a.column < b.column))
return a;
else
return b;
}
static inline TSPoint point_max(TSPoint a, TSPoint b) {
static inline t_point point_max(t_point a, t_point b) {
if (a.row > b.row || (a.row == b.row && a.column > b.column))
return a;
else

View file

@ -6,7 +6,7 @@ extern "C" {
#endif
#include "./array.h"
#include "api.h"
#include "parser/api.h"
typedef struct {
uint32_t count;

View file

@ -12,7 +12,7 @@ extern "C" {
#include "./array.h"
#include "./error_costs.h"
#include "./host.h"
#include "api.h"
#include "parser/api.h"
#include "./parser.h"
#define TS_TREE_STATE_NONE USHRT_MAX

View file

@ -1,6 +1,6 @@
#define _POSIX_C_SOURCE 200112L
#include "api.h"
#include "parser/api.h"
#include "./array.h"
#include "./length.h"
@ -10,13 +10,13 @@
TSTree *ts_tree_new(
Subtree root, const TSLanguage *language,
const TSRange *included_ranges, unsigned included_range_count
const t_parser_range *included_ranges, unsigned included_range_count
) {
TSTree *result = malloc(sizeof(TSTree));
result->root = root;
result->language = ts_language_copy(language);
result->included_ranges = calloc(included_range_count, sizeof(TSRange));
memcpy(result->included_ranges, included_ranges, included_range_count * sizeof(TSRange));
result->included_ranges = calloc(included_range_count, sizeof(t_parser_range));
memcpy(result->included_ranges, included_ranges, included_range_count * sizeof(t_parser_range));
result->included_range_count = included_range_count;
return result;
}
@ -44,7 +44,7 @@ TSNode ts_tree_root_node(const TSTree *self) {
TSNode ts_tree_root_node_with_offset(
const TSTree *self,
uint32_t offset_bytes,
TSPoint offset_extent
t_point offset_extent
) {
Length offset = {offset_bytes, offset_extent};
return ts_node_new(self, &self->root, length_add(offset, ts_subtree_padding(self->root)), 0);
@ -56,7 +56,7 @@ const TSLanguage *ts_tree_language(const TSTree *self) {
void ts_tree_edit(TSTree *self, const TSInputEdit *edit) {
for (unsigned i = 0; i < self->included_range_count; i++) {
TSRange *range = &self->included_ranges[i];
t_parser_range *range = &self->included_ranges[i];
if (range->end_byte >= edit->old_end_byte) {
if (range->end_byte != UINT32_MAX) {
range->end_byte = edit->new_end_byte + (range->end_byte - edit->old_end_byte);
@ -94,10 +94,10 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit) {
ts_subtree_pool_delete(&pool);
}
TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length) {
t_parser_range *ts_tree_included_ranges(const TSTree *self, uint32_t *length) {
*length = self->included_range_count;
TSRange *ranges = calloc(self->included_range_count, sizeof(TSRange));
memcpy(ranges, self->included_ranges, self->included_range_count * sizeof(TSRange));
t_parser_range *ranges = calloc(self->included_range_count, sizeof(t_parser_range));
memcpy(ranges, self->included_ranges, self->included_range_count * sizeof(t_parser_range));
return ranges;
}

View file

@ -17,11 +17,11 @@ typedef struct {
struct TSTree {
Subtree root;
const TSLanguage *language;
TSRange *included_ranges;
t_parser_range *included_ranges;
unsigned included_range_count;
};
TSTree *ts_tree_new(Subtree root, const TSLanguage *language, const TSRange *, unsigned);
TSTree *ts_tree_new(Subtree root, const TSLanguage *language, const t_parser_range *, unsigned);
TSNode ts_node_new(const TSTree *, const Subtree *, Length, TSSymbol);
#ifdef __cplusplus

View file

@ -1,4 +1,4 @@
#include "api.h"
#include "parser/api.h"
#include "./tree_cursor.h"
#include "./language.h"
@ -259,7 +259,7 @@ bool ts_tree_cursor_goto_last_child(TSTreeCursor *self) {
static inline int64_t ts_tree_cursor_goto_first_child_for_byte_and_point(
TSTreeCursor *_self,
uint32_t goal_byte,
TSPoint goal_point
t_point goal_point
) {
TreeCursor *self = (TreeCursor *)_self;
uint32_t initial_size = self->stack.size;
@ -302,7 +302,7 @@ int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t go
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, goal_byte, POINT_ZERO);
}
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, TSPoint goal_point) {
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, t_point goal_point) {
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, 0, goal_point);
}