Restored to old state that doesn't leak

This commit is contained in:
Maix0 2024-08-18 21:46:29 +02:00
parent 1a74bb38d3
commit 7329c95ba0
11 changed files with 1028 additions and 328 deletions

View file

@ -1,14 +1,14 @@
#ifndef PARSE_STACK_H
#define PARSE_STACK_H
#include "me/types.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)
#define STACK_VERSION_NONE ((StackVersion)-1)
typedef struct StackSlice
{
@ -29,7 +29,7 @@ typedef Array(StackSummaryEntry) StackSummary;
typedef void (*StackIterateCallback)(void *, TSStateId, t_u32);
// Create a stack.
Stack *ts_stack_new(void);
Stack *ts_stack_new(SubtreePool *);
// Release the memory reserved for a given stack.
void ts_stack_delete(Stack *);

View file

@ -1,11 +1,11 @@
#ifndef SUBTREE_H
#define SUBTREE_H
#include "me/types.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>
@ -36,6 +36,37 @@ struct ExternalScannerState
// 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,
@ -43,14 +74,14 @@ typedef struct ExternalScannerState ExternalScannerState;
// the inline representation.
typedef struct SubtreeHeapData
{
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;
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;
@ -91,83 +122,94 @@ typedef struct 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 {
SubtreeHeapData *ptr;
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(SubtreeArray *);
void ts_subtree_array_delete(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 *);
Subtree ts_subtree_new_leaf(TSSymbol, Length, Length, t_u32, TSStateId, bool, bool, bool, const TSLanguage *);
Subtree ts_subtree_new_error(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(TSSymbol, Length, t_u32, const TSLanguage *);
MutableSubtree ts_subtree_make_mut(Subtree);
void ts_subtree_retain(Subtree);
void ts_subtree_release(Subtree);
int ts_subtree_compare(Subtree, Subtree);
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, const TSLanguage *);
Subtree ts_subtree_edit(Subtree, const TSInputEdit *edit);
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);
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).ptr->symbol);
return ((self).data.is_inline ? (self).data.symbol : (self).ptr->symbol);
}
static inline bool ts_subtree_visible(Subtree self)
{
return ((self).ptr->visible);
return ((self).data.is_inline ? (self).data.visible : (self).ptr->visible);
}
static inline bool ts_subtree_named(Subtree self)
{
return ((self).ptr->named);
return ((self).data.is_inline ? (self).data.named : (self).ptr->named);
}
static inline bool ts_subtree_extra(Subtree self)
{
return ((self).ptr->extra);
return ((self).data.is_inline ? (self).data.extra : (self).ptr->extra);
}
static inline bool ts_subtree_has_changes(Subtree self)
{
return ((self).ptr->has_changes);
return ((self).data.is_inline ? (self).data.has_changes : (self).ptr->has_changes);
}
static inline bool ts_subtree_missing(Subtree self)
{
return ((self).ptr->is_missing);
return ((self).data.is_inline ? (self).data.is_missing : (self).ptr->is_missing);
}
static inline bool ts_subtree_is_keyword(Subtree self)
{
return ((self).ptr->is_keyword);
return ((self).data.is_inline ? (self).data.is_keyword : (self).ptr->is_keyword);
}
static inline TSStateId ts_subtree_parse_state(Subtree self)
{
return ((self).ptr->parse_state);
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).ptr->lookahead_bytes);
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
@ -179,15 +221,20 @@ static inline size_t ts_subtree_alloc_size(t_u32 child_count)
// Get a subtree's children, which are allocated immediately before the
// tree's own heap data.
#define ts_subtree_children(self) ((Subtree *)((self).ptr) - (self).ptr->child_count)
#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)
{
self->ptr->extra = 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;
@ -195,6 +242,8 @@ static inline TSSymbol ts_subtree_leaf_symbol(Subtree self)
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;
@ -202,12 +251,18 @@ static inline TSStateId ts_subtree_leaf_parse_state(Subtree self)
static inline Length ts_subtree_padding(Subtree self)
{
return self.ptr->padding;
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)
{
return self.ptr->size;
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)
@ -222,22 +277,22 @@ static inline t_u32 ts_subtree_total_bytes(Subtree self)
static inline t_u32 ts_subtree_child_count(Subtree self)
{
return (self.ptr->child_count);
return (self.data.is_inline ? 0 : self.ptr->child_count);
}
static inline t_u32 ts_subtree_repeat_depth(Subtree self)
{
return (self.ptr->repeat_depth);
return (self.data.is_inline ? 0 : self.ptr->repeat_depth);
}
static inline t_u32 ts_subtree_is_repetition(Subtree self)
{
return (!self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0);
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.ptr->child_count == 0) ? 0 : self.ptr->visible_descendant_count);
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)
@ -253,12 +308,12 @@ 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.ptr->error_cost);
return (self.data.is_inline ? 0 : self.ptr->error_cost);
}
static inline t_i32 ts_subtree_dynamic_precedence(Subtree self)
{
return ((self.ptr->child_count == 0) ? 0 : self.ptr->dynamic_precedence);
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)
@ -271,32 +326,32 @@ static inline t_u16 ts_subtree_production_id(Subtree self)
static inline bool ts_subtree_fragile_left(Subtree self)
{
return (self.ptr->fragile_left);
return (self.data.is_inline ? false : self.ptr->fragile_left);
}
static inline bool ts_subtree_fragile_right(Subtree self)
{
return (self.ptr->fragile_right);
return (self.data.is_inline ? false : self.ptr->fragile_right);
}
static inline bool ts_subtree_has_external_tokens(Subtree self)
{
return (self.ptr->has_external_tokens);
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.ptr->has_external_scanner_state_change);
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.ptr->depends_on_column);
return (self.data.is_inline ? false : self.ptr->depends_on_column);
}
static inline bool ts_subtree_is_fragile(Subtree self)
{
return ((self.ptr->fragile_left || self.ptr->fragile_right));
return (self.data.is_inline ? false : (self.ptr->fragile_left || self.ptr->fragile_right));
}
static inline bool ts_subtree_is_error(Subtree self)
@ -313,7 +368,7 @@ static inline Subtree ts_subtree_from_mut(MutableSubtree self)
{
Subtree result;
result.ptr = self.ptr;
result.data = self.data;
return (result);
}
@ -321,7 +376,7 @@ static inline MutableSubtree ts_subtree_to_mut_unsafe(Subtree self)
{
MutableSubtree result;
result.ptr = (void *)self.ptr;
result.data = self.data;
return (result);
}