WIP: doing stuff in the parser lib

This commit is contained in:
Maieul BOYER 2024-08-14 19:20:19 +02:00
parent 13bc603a59
commit dc6a4b1c98
No known key found for this signature in database
10 changed files with 246 additions and 621 deletions

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/08/14 17:36:13 by maiboyer ### ########.fr # # Updated: 2024/08/14 19:09:04 by maiboyer ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ast_utils.c :+: :+: :+: */ /* node_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/04 10:55:45 by rparodi #+# #+# */ /* Created: 2024/08/04 10:55:45 by rparodi #+# #+# */
/* Updated: 2024/08/04 11:06:14 by rparodi ### ########.fr */ /* Updated: 2024/08/14 18:29:10 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,7 +45,6 @@ t_ast_expansion_operator _extract_exp_op(t_parse_node self)
t_ast_expansion_operator kind; t_ast_expansion_operator kind;
t_symbol symbol; t_symbol symbol;
kind = E_OP_NONE;
symbol = ts_node_grammar_symbol(self); symbol = ts_node_grammar_symbol(self);
if (symbol == anon_sym_DASH) if (symbol == anon_sym_DASH)
kind = E_OP_DEFAULT; kind = E_OP_DEFAULT;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
/* Updated: 2024/08/14 18:13:54 by maiboyer ### ########.fr */ /* Updated: 2024/08/14 18:30:23 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -523,7 +523,6 @@ t_error _ast_into_str(t_ast_node self, t_state *state, t_vec_str *append)
// t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out) NOT_DONE; // t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out) NOT_DONE;
// t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out) NOT_DONE; // t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out) NOT_DONE;
t_error run_case_(t_ast_case *case_, t_state *state, void *out) NOT_DONE; t_error run_case_(t_ast_case *case_, t_state *state, void *out) NOT_DONE;
t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out) NOT_DONE; t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out) NOT_DONE;
t_error run_command_substitution(t_ast_command_substitution *command_substitution, t_state *state, void *out) NOT_DONE; t_error run_command_substitution(t_ast_command_substitution *command_substitution, t_state *state, void *out) NOT_DONE;
@ -709,6 +708,7 @@ t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_cmd_pipe cmd_pi
if (info.stderr != NULL) if (info.stderr != NULL)
(dup2(info.stderr->fd, 2), close_fd(info.stderr)); (dup2(info.stderr->fd, 2), close_fd(info.stderr));
i = 0; i = 0;
code = 0;
while (i < subshell->body.len) while (i < subshell->body.len)
{ {
if (_run_get_exit_code(subshell->body.buffer[i], state, &code)) if (_run_get_exit_code(subshell->body.buffer[i], state, &code))
@ -719,6 +719,8 @@ t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_cmd_pipe cmd_pi
} }
if (forked == -1) if (forked == -1)
return (ERROR); return (ERROR);
out->exit = -1;
out->pid = forked;
if (info.stdin != NULL) if (info.stdin != NULL)
(dup2(info.stdin->fd, 0), close_fd(info.stdin)); (dup2(info.stdin->fd, 0), close_fd(info.stdin));
if (info.stdout != NULL) if (info.stdout != NULL)
@ -726,7 +728,7 @@ t_error run_subshell(t_ast_subshell *subshell, t_state *state, t_cmd_pipe cmd_pi
if (info.stderr != NULL) if (info.stderr != NULL)
(dup2(info.stderr->fd, 2), close_fd(info.stderr)); (dup2(info.stderr->fd, 2), close_fd(info.stderr));
if (cmd_pipe.create_output || cmd_pipe.input != NULL) if (cmd_pipe.create_output || cmd_pipe.input != NULL)
return (out->pid = forked, out->stdout = info.ret_stdout, NO_ERROR); return (out->stdout = info.ret_stdout, NO_ERROR);
int status; int status;
if (waitpid(forked, &status, 0) == -1) if (waitpid(forked, &status, 0) == -1)
return (ERROR); return (ERROR);

View file

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

View file

@ -1,11 +1,11 @@
#ifndef SUBTREE_H #ifndef SUBTREE_H
#define SUBTREE_H #define SUBTREE_H
#include "me/types.h"
#include "parser/api.h" #include "parser/api.h"
#include "parser/array.h" #include "parser/array.h"
#include "parser/length.h" #include "parser/length.h"
#include "parser/parser.h" #include "parser/parser.h"
#include "me/types.h"
#include <limits.h> #include <limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@ -36,37 +36,6 @@ struct ExternalScannerState
// separately on the heap. // separately on the heap.
typedef struct ExternalScannerState ExternalScannerState; 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. // A heap-allocated representation of a subtree.
// //
// This representation is used for parent nodes, external tokens, // This representation is used for parent nodes, external tokens,
@ -74,14 +43,14 @@ struct SubtreeInlineData
// the inline representation. // the inline representation.
typedef struct SubtreeHeapData typedef struct SubtreeHeapData
{ {
volatile t_u32 ref_count; t_u32 ref_count;
Length padding; Length padding;
Length size; Length size;
t_u32 lookahead_bytes; t_u32 lookahead_bytes;
t_u32 error_cost; t_u32 error_cost;
t_u32 child_count; t_u32 child_count;
TSSymbol symbol; TSSymbol symbol;
TSStateId parse_state; TSStateId parse_state;
bool visible : 1; bool visible : 1;
bool named : 1; bool named : 1;
@ -122,94 +91,83 @@ typedef struct SubtreeHeapData
// The fundamental building block of a syntax tree. // The fundamental building block of a syntax tree.
typedef union Subtree { typedef union Subtree {
SubtreeInlineData data;
const SubtreeHeapData *ptr; const SubtreeHeapData *ptr;
} Subtree; } Subtree;
// Like Subtree, but mutable. // Like Subtree, but mutable.
typedef union MutableSubtree { typedef union MutableSubtree {
SubtreeInlineData data; SubtreeHeapData *ptr;
SubtreeHeapData *ptr;
} MutableSubtree; } MutableSubtree;
typedef Array(Subtree) SubtreeArray; typedef Array(Subtree) SubtreeArray;
typedef Array(MutableSubtree) MutableSubtreeArray; 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); void ts_external_scanner_state_init(ExternalScannerState *, const t_u8 *, t_u32);
const t_u8 *ts_external_scanner_state_data(const ExternalScannerState *); const t_u8 *ts_external_scanner_state_data(const ExternalScannerState *);
bool ts_external_scanner_state_eq(const ExternalScannerState *self, const t_u8 *, t_u32); bool ts_external_scanner_state_eq(const ExternalScannerState *self, const t_u8 *, t_u32);
void ts_external_scanner_state_delete(ExternalScannerState *self); void ts_external_scanner_state_delete(ExternalScannerState *self);
void ts_subtree_array_copy(SubtreeArray, SubtreeArray *); void ts_subtree_array_copy(SubtreeArray, SubtreeArray *);
void ts_subtree_array_clear(SubtreePool *, SubtreeArray *); void ts_subtree_array_clear(SubtreeArray *);
void ts_subtree_array_delete(SubtreePool *, SubtreeArray *); void ts_subtree_array_delete(SubtreeArray *);
void ts_subtree_array_remove_trailing_extras(SubtreeArray *, SubtreeArray *); void ts_subtree_array_remove_trailing_extras(SubtreeArray *, SubtreeArray *);
void ts_subtree_array_reverse(SubtreeArray *); void ts_subtree_array_reverse(SubtreeArray *);
SubtreePool ts_subtree_pool_new(t_u32 capacity); Subtree ts_subtree_new_leaf(TSSymbol, Length, Length, t_u32, TSStateId, bool, bool, bool, const TSLanguage *);
void ts_subtree_pool_delete(SubtreePool *); 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_leaf(SubtreePool *, TSSymbol, Length, Length, t_u32, TSStateId, bool, bool, bool, const TSLanguage *); Subtree ts_subtree_new_error_node(SubtreeArray *, bool, const TSLanguage *);
Subtree ts_subtree_new_error(SubtreePool *, t_i32, Length, Length, t_u32, TSStateId, const TSLanguage *); Subtree ts_subtree_new_missing_leaf(TSSymbol, Length, t_u32, const TSLanguage *);
MutableSubtree ts_subtree_new_node(TSSymbol, SubtreeArray *, t_u32, const TSLanguage *); MutableSubtree ts_subtree_make_mut(Subtree);
Subtree ts_subtree_new_error_node(SubtreeArray *, bool, const TSLanguage *); void ts_subtree_retain(Subtree);
Subtree ts_subtree_new_missing_leaf(SubtreePool *, TSSymbol, Length, t_u32, const TSLanguage *); void ts_subtree_release(Subtree);
MutableSubtree ts_subtree_make_mut(SubtreePool *, Subtree); int ts_subtree_compare(Subtree, Subtree);
void ts_subtree_retain(Subtree); void ts_subtree_set_symbol(MutableSubtree *, TSSymbol, const TSLanguage *);
void ts_subtree_release(SubtreePool *, Subtree); void ts_subtree_summarize(MutableSubtree, const Subtree *, t_u32, const TSLanguage *);
int ts_subtree_compare(Subtree, Subtree, SubtreePool *); void ts_subtree_summarize_children(MutableSubtree, const TSLanguage *);
void ts_subtree_set_symbol(MutableSubtree *, TSSymbol, const TSLanguage *); void ts_subtree_balance(Subtree, const TSLanguage *);
void ts_subtree_summarize(MutableSubtree, const Subtree *, t_u32, const TSLanguage *); Subtree ts_subtree_edit(Subtree, const TSInputEdit *edit);
void ts_subtree_summarize_children(MutableSubtree, const TSLanguage *); char *ts_subtree_string(Subtree, TSSymbol, bool, const TSLanguage *, bool include_all);
void ts_subtree_balance(Subtree, SubtreePool *, const TSLanguage *); void ts_subtree_print_dot_graph(Subtree, const TSLanguage *, FILE *);
Subtree ts_subtree_edit(Subtree, const TSInputEdit *edit, SubtreePool *); Subtree ts_subtree_last_external_token(Subtree);
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); const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self);
bool ts_subtree_external_scanner_state_eq(Subtree, Subtree); bool ts_subtree_external_scanner_state_eq(Subtree, Subtree);
static inline TSSymbol ts_subtree_symbol(Subtree self) static inline TSSymbol ts_subtree_symbol(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.symbol : (self).ptr->symbol); return ((self).ptr->symbol);
} }
static inline bool ts_subtree_visible(Subtree self) static inline bool ts_subtree_visible(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.visible : (self).ptr->visible); return ((self).ptr->visible);
} }
static inline bool ts_subtree_named(Subtree self) static inline bool ts_subtree_named(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.named : (self).ptr->named); return ((self).ptr->named);
} }
static inline bool ts_subtree_extra(Subtree self) static inline bool ts_subtree_extra(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.extra : (self).ptr->extra); return ((self).ptr->extra);
} }
static inline bool ts_subtree_has_changes(Subtree self) static inline bool ts_subtree_has_changes(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.has_changes : (self).ptr->has_changes); return ((self).ptr->has_changes);
} }
static inline bool ts_subtree_missing(Subtree self) static inline bool ts_subtree_missing(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.is_missing : (self).ptr->is_missing); return ((self).ptr->is_missing);
} }
static inline bool ts_subtree_is_keyword(Subtree self) static inline bool ts_subtree_is_keyword(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.is_keyword : (self).ptr->is_keyword); return ((self).ptr->is_keyword);
} }
static inline TSStateId ts_subtree_parse_state(Subtree self) static inline TSStateId ts_subtree_parse_state(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.parse_state : (self).ptr->parse_state); return ((self).ptr->parse_state);
} }
static inline t_u32 ts_subtree_lookahead_bytes(Subtree self) static inline t_u32 ts_subtree_lookahead_bytes(Subtree self)
{ {
return ((self).data.is_inline ? (self).data.lookahead_bytes : (self).ptr->lookahead_bytes); return ((self).ptr->lookahead_bytes);
} }
// Get the size needed to store a heap-allocated subtree with the given // Get the size needed to store a heap-allocated subtree with the given
@ -221,20 +179,15 @@ static inline size_t ts_subtree_alloc_size(t_u32 child_count)
// Get a subtree's children, which are allocated immediately before the // Get a subtree's children, which are allocated immediately before the
// tree's own heap data. // tree's own heap data.
#define ts_subtree_children(self) ((self).data.is_inline ? NULL : (Subtree *)((self).ptr) - (self).ptr->child_count) #define ts_subtree_children(self) ((Subtree *)((self).ptr) - (self).ptr->child_count)
static inline void ts_subtree_set_extra(MutableSubtree *self, bool is_extra) static inline void ts_subtree_set_extra(MutableSubtree *self, bool is_extra)
{ {
if (self->data.is_inline) self->ptr->extra = is_extra;
self->data.extra = is_extra;
else
self->ptr->extra = is_extra;
} }
static inline TSSymbol ts_subtree_leaf_symbol(Subtree self) static inline TSSymbol ts_subtree_leaf_symbol(Subtree self)
{ {
if (self.data.is_inline)
return self.data.symbol;
if (self.ptr->child_count == 0) if (self.ptr->child_count == 0)
return self.ptr->symbol; return self.ptr->symbol;
return self.ptr->first_leaf.symbol; return self.ptr->first_leaf.symbol;
@ -242,8 +195,6 @@ static inline TSSymbol ts_subtree_leaf_symbol(Subtree self)
static inline TSStateId ts_subtree_leaf_parse_state(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) if (self.ptr->child_count == 0)
return self.ptr->parse_state; return self.ptr->parse_state;
return self.ptr->first_leaf.parse_state; return self.ptr->first_leaf.parse_state;
@ -251,18 +202,12 @@ static inline TSStateId ts_subtree_leaf_parse_state(Subtree self)
static inline Length ts_subtree_padding(Subtree self) static inline Length ts_subtree_padding(Subtree self)
{ {
if (self.data.is_inline) return self.ptr->padding;
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) static inline Length ts_subtree_size(Subtree self)
{ {
if (self.data.is_inline) return self.ptr->size;
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) static inline Length ts_subtree_total_size(Subtree self)
@ -277,22 +222,22 @@ static inline t_u32 ts_subtree_total_bytes(Subtree self)
static inline t_u32 ts_subtree_child_count(Subtree self) static inline t_u32 ts_subtree_child_count(Subtree self)
{ {
return (self.data.is_inline ? 0 : self.ptr->child_count); return (self.ptr->child_count);
} }
static inline t_u32 ts_subtree_repeat_depth(Subtree self) static inline t_u32 ts_subtree_repeat_depth(Subtree self)
{ {
return (self.data.is_inline ? 0 : self.ptr->repeat_depth); return (self.ptr->repeat_depth);
} }
static inline t_u32 ts_subtree_is_repetition(Subtree self) 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); return (!self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0);
} }
static inline t_u32 ts_subtree_visible_descendant_count(Subtree self) 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); return ((self.ptr->child_count == 0) ? 0 : self.ptr->visible_descendant_count);
} }
static inline t_u32 ts_subtree_visible_child_count(Subtree self) static inline t_u32 ts_subtree_visible_child_count(Subtree self)
@ -308,12 +253,12 @@ static inline t_u32 ts_subtree_error_cost(Subtree self)
if (ts_subtree_missing(self)) if (ts_subtree_missing(self))
return (ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY); return (ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY);
else else
return (self.data.is_inline ? 0 : self.ptr->error_cost); return (self.ptr->error_cost);
} }
static inline t_i32 ts_subtree_dynamic_precedence(Subtree self) 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); return ((self.ptr->child_count == 0) ? 0 : self.ptr->dynamic_precedence);
} }
static inline t_u16 ts_subtree_production_id(Subtree self) static inline t_u16 ts_subtree_production_id(Subtree self)
@ -326,32 +271,32 @@ static inline t_u16 ts_subtree_production_id(Subtree self)
static inline bool ts_subtree_fragile_left(Subtree self) static inline bool ts_subtree_fragile_left(Subtree self)
{ {
return (self.data.is_inline ? false : self.ptr->fragile_left); return (self.ptr->fragile_left);
} }
static inline bool ts_subtree_fragile_right(Subtree self) static inline bool ts_subtree_fragile_right(Subtree self)
{ {
return (self.data.is_inline ? false : self.ptr->fragile_right); return (self.ptr->fragile_right);
} }
static inline bool ts_subtree_has_external_tokens(Subtree self) static inline bool ts_subtree_has_external_tokens(Subtree self)
{ {
return (self.data.is_inline ? false : self.ptr->has_external_tokens); return (self.ptr->has_external_tokens);
} }
static inline bool ts_subtree_has_external_scanner_state_change(Subtree self) 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); return (self.ptr->has_external_scanner_state_change);
} }
static inline bool ts_subtree_depends_on_column(Subtree self) static inline bool ts_subtree_depends_on_column(Subtree self)
{ {
return (self.data.is_inline ? false : self.ptr->depends_on_column); return (self.ptr->depends_on_column);
} }
static inline bool ts_subtree_is_fragile(Subtree self) static inline bool ts_subtree_is_fragile(Subtree self)
{ {
return (self.data.is_inline ? false : (self.ptr->fragile_left || self.ptr->fragile_right)); return ((self.ptr->fragile_left || self.ptr->fragile_right));
} }
static inline bool ts_subtree_is_error(Subtree self) static inline bool ts_subtree_is_error(Subtree self)
@ -368,7 +313,7 @@ static inline Subtree ts_subtree_from_mut(MutableSubtree self)
{ {
Subtree result; Subtree result;
result.data = self.data; result.ptr = self.ptr;
return (result); return (result);
} }
@ -376,7 +321,7 @@ static inline MutableSubtree ts_subtree_to_mut_unsafe(Subtree self)
{ {
MutableSubtree result; MutableSubtree result;
result.data = self.data; result.ptr = (void *)self.ptr;
return (result); return (result);
} }

View file

@ -84,6 +84,8 @@ static inline bool ts_node_child_iterator_next(NodeChildIterator *self, TSNode *
return false; return false;
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index]; const Subtree *child = &ts_subtree_children(self->parent)[self->child_index];
TSSymbol alias_symbol = 0; TSSymbol alias_symbol = 0;
if (child == NULL)
return (false);
if (!ts_subtree_extra(*child)) if (!ts_subtree_extra(*child))
{ {
if (self->alias_sequence) if (self->alias_sequence)
@ -509,12 +511,14 @@ t_const_str ts_node_grammar_type(TSNode self)
return ts_language_symbol_name(self.tree->language, symbol); return ts_language_symbol_name(self.tree->language, symbol);
} }
/*
char *ts_node_string(TSNode self) char *ts_node_string(TSNode self)
{ {
TSSymbol alias_symbol = ts_node__alias(&self); TSSymbol alias_symbol = ts_node__alias(&self);
return ts_subtree_string(ts_node__subtree(self), alias_symbol, ts_language_symbol_metadata(self.tree->language, alias_symbol).visible, return ts_subtree_string(ts_node__subtree(self), alias_symbol, ts_language_symbol_metadata(self.tree->language, alias_symbol).visible,
self.tree->language, false); self.tree->language, false);
} }
*/
bool ts_node_eq(TSNode self, TSNode other) bool ts_node_eq(TSNode self, TSNode other)
{ {

View file

@ -1,5 +1,7 @@
#define _POSIX_C_SOURCE 200112L #define _POSIX_C_SOURCE 200112L
#include "me/mem/mem.h"
#include "me/types.h"
#include "parser/api.h" #include "parser/api.h"
#include "parser/array.h" #include "parser/array.h"
#include "parser/language.h" #include "parser/language.h"
@ -9,8 +11,6 @@
#include "parser/stack.h" #include "parser/stack.h"
#include "parser/subtree.h" #include "parser/subtree.h"
#include "parser/tree.h" #include "parser/tree.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
@ -40,7 +40,6 @@ struct TSParser
{ {
Lexer lexer; Lexer lexer;
Stack *stack; Stack *stack;
SubtreePool tree_pool;
const TSLanguage *language; const TSLanguage *language;
ReduceActionSet reduce_actions; ReduceActionSet reduce_actions;
Subtree finished_tree; Subtree finished_tree;
@ -163,7 +162,7 @@ static bool ts_parser__breakdown_top_of_stack(TSParser *self, StackVersion versi
ts_stack_push(self->stack, slice.version, tree, false, state); ts_stack_push(self->stack, slice.version, tree, false, state);
} }
ts_subtree_release(&self->tree_pool, parent); ts_subtree_release(parent);
array_delete(&slice.subtrees); array_delete(&slice.subtrees);
LOG("breakdown_top_of_stack tree:%s", TREE_NAME(parent)); LOG("breakdown_top_of_stack tree:%s", TREE_NAME(parent));
@ -487,7 +486,7 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa
Length padding = length_sub(error_start_position, start_position); Length padding = length_sub(error_start_position, start_position);
Length size = length_sub(error_end_position, error_start_position); Length size = length_sub(error_end_position, error_start_position);
t_u32 lookahead_bytes = lookahead_end_byte - error_end_position.bytes; t_u32 lookahead_bytes = lookahead_end_byte - error_end_position.bytes;
result = ts_subtree_new_error(&self->tree_pool, first_error_character, padding, size, lookahead_bytes, parse_state, self->language); result = ts_subtree_new_error(first_error_character, padding, size, lookahead_bytes, parse_state, self->language);
} }
else else
{ {
@ -516,8 +515,8 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa
} }
} }
result = ts_subtree_new_leaf(&self->tree_pool, symbol, padding, size, lookahead_bytes, parse_state, found_external_token, result = ts_subtree_new_leaf(symbol, padding, size, lookahead_bytes, parse_state, found_external_token, called_get_column,
called_get_column, is_keyword, self->language); is_keyword, self->language);
if (found_external_token) if (found_external_token)
{ {
@ -556,9 +555,9 @@ static void ts_parser__set_cached_token(TSParser *self, t_u32 byte_index, Subtre
if (last_external_token.ptr) if (last_external_token.ptr)
ts_subtree_retain(last_external_token); ts_subtree_retain(last_external_token);
if (cache->token.ptr) if (cache->token.ptr)
ts_subtree_release(&self->tree_pool, cache->token); ts_subtree_release(cache->token);
if (cache->last_external_token.ptr) if (cache->last_external_token.ptr)
ts_subtree_release(&self->tree_pool, cache->last_external_token); ts_subtree_release(cache->last_external_token);
cache->token = token; cache->token = token;
cache->byte_index = byte_index; cache->byte_index = byte_index;
cache->last_external_token = last_external_token; cache->last_external_token = last_external_token;
@ -570,6 +569,7 @@ static void ts_parser__set_cached_token(TSParser *self, t_u32 byte_index, Subtre
// and finally, as a default, by a recursive comparison of the trees' symbols. // and finally, as a default, by a recursive comparison of the trees' symbols.
static bool ts_parser__select_tree(TSParser *self, Subtree left, Subtree right) static bool ts_parser__select_tree(TSParser *self, Subtree left, Subtree right)
{ {
(void)(self);
if (!left.ptr) if (!left.ptr)
return true; return true;
if (!right.ptr) if (!right.ptr)
@ -604,7 +604,7 @@ static bool ts_parser__select_tree(TSParser *self, Subtree left, Subtree right)
if (ts_subtree_error_cost(left) > 0) if (ts_subtree_error_cost(left) > 0)
return true; return true;
int comparison = ts_subtree_compare(left, right, &self->tree_pool); int comparison = ts_subtree_compare(left, right);
switch (comparison) switch (comparison)
{ {
case -1: case -1:
@ -641,7 +641,7 @@ static void ts_parser__shift(TSParser *self, StackVersion version, TSStateId sta
Subtree subtree_to_push = lookahead; Subtree subtree_to_push = lookahead;
if (extra != ts_subtree_extra(lookahead) && is_leaf) if (extra != ts_subtree_extra(lookahead) && is_leaf)
{ {
MutableSubtree result = ts_subtree_make_mut(&self->tree_pool, lookahead); MutableSubtree result = ts_subtree_make_mut(lookahead);
ts_subtree_set_extra(&result, extra); ts_subtree_set_extra(&result, extra);
subtree_to_push = ts_subtree_from_mut(result); subtree_to_push = ts_subtree_from_mut(result);
} }
@ -677,14 +677,14 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
if (slice_version > MAX_VERSION_COUNT + MAX_VERSION_COUNT_OVERFLOW) if (slice_version > MAX_VERSION_COUNT + MAX_VERSION_COUNT_OVERFLOW)
{ {
ts_stack_remove_version(self->stack, slice_version); ts_stack_remove_version(self->stack, slice_version);
ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); ts_subtree_array_delete(&slice.subtrees);
removed_version_count++; removed_version_count++;
while (i + 1 < pop.size) while (i + 1 < pop.size)
{ {
StackSlice next_slice = pop.contents[i + 1]; StackSlice next_slice = pop.contents[i + 1];
if (next_slice.version != slice.version) if (next_slice.version != slice.version)
break; break;
ts_subtree_array_delete(&self->tree_pool, &next_slice.subtrees); ts_subtree_array_delete(&next_slice.subtrees);
i++; i++;
} }
continue; continue;
@ -714,15 +714,15 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
if (ts_parser__select_children(self, ts_subtree_from_mut(parent), &next_slice_children)) if (ts_parser__select_children(self, ts_subtree_from_mut(parent), &next_slice_children))
{ {
ts_subtree_array_clear(&self->tree_pool, &self->trailing_extras); ts_subtree_array_clear(&self->trailing_extras);
ts_subtree_release(&self->tree_pool, ts_subtree_from_mut(parent)); ts_subtree_release(ts_subtree_from_mut(parent));
array_swap(&self->trailing_extras, &self->trailing_extras2); array_swap(&self->trailing_extras, &self->trailing_extras2);
parent = ts_subtree_new_node(symbol, &next_slice_children, production_id, self->language); parent = ts_subtree_new_node(symbol, &next_slice_children, production_id, self->language);
} }
else else
{ {
array_clear(&self->trailing_extras2); array_clear(&self->trailing_extras2);
ts_subtree_array_delete(&self->tree_pool, &next_slice.subtrees); ts_subtree_array_delete(&next_slice.subtrees);
} }
} }
@ -784,7 +784,6 @@ static void ts_parser__accept(TSParser *self, StackVersion version, Subtree look
Subtree tree = trees.contents[j]; Subtree tree = trees.contents[j];
if (!ts_subtree_extra(tree)) if (!ts_subtree_extra(tree))
{ {
assert(!tree.data.is_inline);
t_u32 child_count = ts_subtree_child_count(tree); t_u32 child_count = ts_subtree_child_count(tree);
const Subtree *children = ts_subtree_children(tree); const Subtree *children = ts_subtree_children(tree);
for (t_u32 k = 0; k < child_count; k++) for (t_u32 k = 0; k < child_count; k++)
@ -793,7 +792,7 @@ static void ts_parser__accept(TSParser *self, StackVersion version, Subtree look
} }
array_splice(&trees, j, 1, child_count, children); array_splice(&trees, j, 1, child_count, children);
root = ts_subtree_from_mut(ts_subtree_new_node(ts_subtree_symbol(tree), &trees, tree.ptr->production_id, self->language)); root = ts_subtree_from_mut(ts_subtree_new_node(ts_subtree_symbol(tree), &trees, tree.ptr->production_id, self->language));
ts_subtree_release(&self->tree_pool, tree); ts_subtree_release(tree);
break; break;
} }
} }
@ -805,12 +804,12 @@ static void ts_parser__accept(TSParser *self, StackVersion version, Subtree look
{ {
if (ts_parser__select_tree(self, self->finished_tree, root)) if (ts_parser__select_tree(self, self->finished_tree, root))
{ {
ts_subtree_release(&self->tree_pool, self->finished_tree); ts_subtree_release(self->finished_tree);
self->finished_tree = root; self->finished_tree = root;
} }
else else
{ {
ts_subtree_release(&self->tree_pool, root); ts_subtree_release(root);
} }
} }
else else
@ -939,7 +938,7 @@ static bool ts_parser__recover_to_state(TSParser *self, StackVersion version, t_
if (slice.version == previous_version) if (slice.version == previous_version)
{ {
ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); ts_subtree_array_delete(&slice.subtrees);
array_erase(&pop, i--); array_erase(&pop, i--);
continue; continue;
} }
@ -947,7 +946,7 @@ static bool ts_parser__recover_to_state(TSParser *self, StackVersion version, t_
if (ts_stack_state(self->stack, slice.version) != goal_state) if (ts_stack_state(self->stack, slice.version) != goal_state)
{ {
ts_stack_halt(self->stack, slice.version); ts_stack_halt(self->stack, slice.version);
ts_subtree_array_delete(&self->tree_pool, &slice.subtrees); ts_subtree_array_delete(&slice.subtrees);
array_erase(&pop, i--); array_erase(&pop, i--);
continue; continue;
} }
@ -966,7 +965,7 @@ static bool ts_parser__recover_to_state(TSParser *self, StackVersion version, t_
ts_subtree_retain(slice.subtrees.contents[j]); ts_subtree_retain(slice.subtrees.contents[j]);
} }
} }
ts_subtree_array_delete(&self->tree_pool, &error_trees); ts_subtree_array_delete(&error_trees);
} }
ts_subtree_array_remove_trailing_extras(&slice.subtrees, &self->trailing_extras); ts_subtree_array_remove_trailing_extras(&slice.subtrees, &self->trailing_extras);
@ -1081,14 +1080,14 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
if (did_recover && ts_stack_version_count(self->stack) > MAX_VERSION_COUNT) if (did_recover && ts_stack_version_count(self->stack) > MAX_VERSION_COUNT)
{ {
ts_stack_halt(self->stack, version); ts_stack_halt(self->stack, version);
ts_subtree_release(&self->tree_pool, lookahead); ts_subtree_release(lookahead);
return; return;
} }
if (did_recover && ts_subtree_has_external_scanner_state_change(lookahead)) if (did_recover && ts_subtree_has_external_scanner_state_change(lookahead))
{ {
ts_stack_halt(self->stack, version); ts_stack_halt(self->stack, version);
ts_subtree_release(&self->tree_pool, lookahead); ts_subtree_release(lookahead);
return; return;
} }
@ -1110,7 +1109,7 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
if (ts_parser__better_version_exists(self, version, false, new_cost)) if (ts_parser__better_version_exists(self, version, false, new_cost))
{ {
ts_stack_halt(self->stack, version); ts_stack_halt(self->stack, version);
ts_subtree_release(&self->tree_pool, lookahead); ts_subtree_release(lookahead);
return; return;
} }
@ -1120,7 +1119,7 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
const TSParseAction *actions = ts_language_actions(self->language, 1, ts_subtree_symbol(lookahead), &n); const TSParseAction *actions = ts_language_actions(self->language, 1, ts_subtree_symbol(lookahead), &n);
if (n > 0 && actions[n - 1].type == TSParseActionTypeShift && actions[n - 1].shift.extra) if (n > 0 && actions[n - 1].type == TSParseActionTypeShift && actions[n - 1].shift.extra)
{ {
MutableSubtree mutable_lookahead = ts_subtree_make_mut(&self->tree_pool, lookahead); MutableSubtree mutable_lookahead = ts_subtree_make_mut(lookahead);
ts_subtree_set_extra(&mutable_lookahead, true); ts_subtree_set_extra(&mutable_lookahead, true);
lookahead = ts_subtree_from_mut(mutable_lookahead); lookahead = ts_subtree_from_mut(mutable_lookahead);
} }
@ -1147,7 +1146,7 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
{ {
for (t_u32 i = 1; i < pop.size; i++) for (t_u32 i = 1; i < pop.size; i++)
{ {
ts_subtree_array_delete(&self->tree_pool, &pop.contents[i].subtrees); ts_subtree_array_delete(&pop.contents[i].subtrees);
} }
while (ts_stack_version_count(self->stack) > pop.contents[0].version + 1) while (ts_stack_version_count(self->stack) > pop.contents[0].version + 1)
{ {
@ -1206,8 +1205,7 @@ static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtre
t_u32 lookahead_bytes = ts_subtree_total_bytes(lookahead) + ts_subtree_lookahead_bytes(lookahead); t_u32 lookahead_bytes = ts_subtree_total_bytes(lookahead) + ts_subtree_lookahead_bytes(lookahead);
StackVersion version_with_missing_tree = ts_stack_copy_version(self->stack, v); StackVersion version_with_missing_tree = ts_stack_copy_version(self->stack, v);
Subtree missing_tree = Subtree missing_tree = ts_subtree_new_missing_leaf(missing_symbol, padding, lookahead_bytes, self->language);
ts_subtree_new_missing_leaf(&self->tree_pool, missing_symbol, padding, lookahead_bytes, self->language);
ts_stack_push(self->stack, version_with_missing_tree, missing_tree, false, state_after_missing_symbol); ts_stack_push(self->stack, version_with_missing_tree, missing_tree, false, state_after_missing_symbol);
if (ts_parser__do_all_potential_reductions(self, version_with_missing_tree, ts_subtree_leaf_symbol(lookahead))) if (ts_parser__do_all_potential_reductions(self, version_with_missing_tree, ts_subtree_leaf_symbol(lookahead)))
@ -1297,7 +1295,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_
{ {
if (lookahead.ptr) if (lookahead.ptr)
{ {
ts_subtree_release(&self->tree_pool, lookahead); ts_subtree_release(lookahead);
} }
return false; return false;
} }
@ -1413,7 +1411,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_
{ {
LOG("switch from_keyword:%s, to_word_token:%s", TREE_NAME(lookahead), SYM_NAME(self->language->keyword_capture_token)); LOG("switch from_keyword:%s, to_word_token:%s", TREE_NAME(lookahead), SYM_NAME(self->language->keyword_capture_token));
MutableSubtree mutable_lookahead = ts_subtree_make_mut(&self->tree_pool, lookahead); MutableSubtree mutable_lookahead = ts_subtree_make_mut(lookahead);
ts_subtree_set_symbol(&mutable_lookahead, self->language->keyword_capture_token, self->language); ts_subtree_set_symbol(&mutable_lookahead, self->language->keyword_capture_token, self->language);
lookahead = ts_subtree_from_mut(mutable_lookahead); lookahead = ts_subtree_from_mut(mutable_lookahead);
continue; continue;
@ -1437,7 +1435,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_
if (ts_parser__breakdown_top_of_stack(self, version)) if (ts_parser__breakdown_top_of_stack(self, version))
{ {
state = ts_stack_state(self->stack, version); state = ts_stack_state(self->stack, version);
ts_subtree_release(&self->tree_pool, lookahead); ts_subtree_release(lookahead);
needs_lex = true; needs_lex = true;
continue; continue;
} }
@ -1586,8 +1584,7 @@ TSParser *ts_parser_new(void)
ts_lexer_init(&self->lexer); ts_lexer_init(&self->lexer);
array_init(&self->reduce_actions); array_init(&self->reduce_actions);
array_reserve(&self->reduce_actions, 4); array_reserve(&self->reduce_actions, 4);
self->tree_pool = ts_subtree_pool_new(32); self->stack = ts_stack_new();
self->stack = ts_stack_new(&self->tree_pool);
self->finished_tree = NULL_SUBTREE; self->finished_tree = NULL_SUBTREE;
self->cancellation_flag = NULL; self->cancellation_flag = NULL;
self->language = NULL; self->language = NULL;
@ -1613,12 +1610,11 @@ void ts_parser_delete(TSParser *self)
} }
if (self->old_tree.ptr) if (self->old_tree.ptr)
{ {
ts_subtree_release(&self->tree_pool, self->old_tree); ts_subtree_release(self->old_tree);
self->old_tree = NULL_SUBTREE; self->old_tree = NULL_SUBTREE;
} }
ts_lexer_delete(&self->lexer); ts_lexer_delete(&self->lexer);
ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE); ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE);
ts_subtree_pool_delete(&self->tree_pool);
array_delete(&self->trailing_extras); array_delete(&self->trailing_extras);
array_delete(&self->trailing_extras2); array_delete(&self->trailing_extras2);
array_delete(&self->scratch_trees); array_delete(&self->scratch_trees);
@ -1651,7 +1647,7 @@ void ts_parser_reset(TSParser *self)
ts_parser__external_scanner_destroy(self); ts_parser__external_scanner_destroy(self);
if (self->old_tree.ptr) if (self->old_tree.ptr)
{ {
ts_subtree_release(&self->tree_pool, self->old_tree); ts_subtree_release(self->old_tree);
self->old_tree = NULL_SUBTREE; self->old_tree = NULL_SUBTREE;
} }
@ -1660,7 +1656,7 @@ void ts_parser_reset(TSParser *self)
ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE); ts_parser__set_cached_token(self, 0, NULL_SUBTREE, NULL_SUBTREE);
if (self->finished_tree.ptr) if (self->finished_tree.ptr)
{ {
ts_subtree_release(&self->tree_pool, self->finished_tree); ts_subtree_release(self->finished_tree);
self->finished_tree = NULL_SUBTREE; self->finished_tree = NULL_SUBTREE;
} }
self->accept_count = 0; self->accept_count = 0;
@ -1739,7 +1735,7 @@ TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input)
} while (version_count != 0); } while (version_count != 0);
assert(self->finished_tree.ptr); assert(self->finished_tree.ptr);
ts_subtree_balance(self->finished_tree, &self->tree_pool, self->language); ts_subtree_balance(self->finished_tree, self->language);
LOG("done"); LOG("done");
LOG_TREE(self->finished_tree); LOG_TREE(self->finished_tree);

View file

@ -1,11 +1,11 @@
#include "parser/stack.h" #include "parser/stack.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "parser/array.h" #include "parser/array.h"
#include "parser/language.h" #include "parser/language.h"
#include "parser/length.h" #include "parser/length.h"
#include "parser/subtree.h" #include "parser/subtree.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
@ -24,14 +24,14 @@ typedef struct StackLink
struct StackNode struct StackNode
{ {
TSStateId state; TSStateId state;
Length position; Length position;
StackLink links[MAX_LINK_COUNT]; StackLink links[MAX_LINK_COUNT];
t_u16 link_count; t_u16 link_count;
t_u32 ref_count; t_u32 ref_count;
t_u32 error_cost; t_u32 error_cost;
t_u32 node_count; t_u32 node_count;
int dynamic_precedence; int dynamic_precedence;
}; };
typedef struct StackIterator typedef struct StackIterator
@ -55,7 +55,7 @@ typedef struct StackHead
{ {
StackNode *node; StackNode *node;
StackSummary *summary; StackSummary *summary;
t_u32 node_count_at_last_error; t_u32 node_count_at_last_error;
Subtree last_external_token; Subtree last_external_token;
Subtree lookahead_when_paused; Subtree lookahead_when_paused;
StackStatus status; StackStatus status;
@ -68,7 +68,6 @@ struct Stack
Array(StackIterator) iterators; Array(StackIterator) iterators;
StackNodeArray node_pool; StackNodeArray node_pool;
StackNode *base_node; StackNode *base_node;
SubtreePool *subtree_pool;
}; };
typedef t_u32 StackAction; typedef t_u32 StackAction;
@ -90,7 +89,7 @@ static void stack_node_retain(StackNode *self)
assert(self->ref_count != 0); assert(self->ref_count != 0);
} }
static void stack_node_release(StackNode *self, StackNodeArray *pool, SubtreePool *subtree_pool) static void stack_node_release(StackNode *self, StackNodeArray *pool)
{ {
recur: recur:
assert(self->ref_count != 0); assert(self->ref_count != 0);
@ -105,12 +104,12 @@ recur:
{ {
StackLink link = self->links[i]; StackLink link = self->links[i];
if (link.subtree.ptr) if (link.subtree.ptr)
ts_subtree_release(subtree_pool, link.subtree); ts_subtree_release(link.subtree);
stack_node_release(link.node, pool, subtree_pool); stack_node_release(link.node, pool);
} }
StackLink link = self->links[0]; StackLink link = self->links[0];
if (link.subtree.ptr) if (link.subtree.ptr)
ts_subtree_release(subtree_pool, link.subtree); ts_subtree_release(link.subtree);
first_predecessor = self->links[0].node; first_predecessor = self->links[0].node;
} }
@ -203,7 +202,7 @@ static bool stack__subtree_is_equivalent(Subtree left, Subtree right)
ts_subtree_extra(left) == ts_subtree_extra(right) && ts_subtree_external_scanner_state_eq(left, right)); ts_subtree_extra(left) == ts_subtree_extra(right) && ts_subtree_external_scanner_state_eq(left, right));
} }
static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *subtree_pool) static void stack_node_add_link(StackNode *self, StackLink link)
{ {
if (link.node == self) if (link.node == self)
return; return;
@ -222,7 +221,7 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su
if (ts_subtree_dynamic_precedence(link.subtree) > ts_subtree_dynamic_precedence(existing_link->subtree)) if (ts_subtree_dynamic_precedence(link.subtree) > ts_subtree_dynamic_precedence(existing_link->subtree))
{ {
ts_subtree_retain(link.subtree); ts_subtree_retain(link.subtree);
ts_subtree_release(subtree_pool, existing_link->subtree); ts_subtree_release(existing_link->subtree);
existing_link->subtree = link.subtree; existing_link->subtree = link.subtree;
self->dynamic_precedence = link.node->dynamic_precedence + ts_subtree_dynamic_precedence(link.subtree); self->dynamic_precedence = link.node->dynamic_precedence + ts_subtree_dynamic_precedence(link.subtree);
} }
@ -235,7 +234,7 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su
{ {
for (int j = 0; j < link.node->link_count; j++) for (int j = 0; j < link.node->link_count; j++)
{ {
stack_node_add_link(existing_link->node, link.node->links[j], subtree_pool); stack_node_add_link(existing_link->node, link.node->links[j]);
} }
t_i32 dynamic_precedence = link.node->dynamic_precedence; t_i32 dynamic_precedence = link.node->dynamic_precedence;
if (link.subtree.ptr) if (link.subtree.ptr)
@ -256,7 +255,7 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su
stack_node_retain(link.node); stack_node_retain(link.node);
t_u32 node_count = link.node->node_count; t_u32 node_count = link.node->node_count;
int dynamic_precedence = link.node->dynamic_precedence; int dynamic_precedence = link.node->dynamic_precedence;
self->links[self->link_count++] = link; self->links[self->link_count++] = link;
if (link.subtree.ptr) if (link.subtree.ptr)
@ -272,24 +271,24 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su
self->dynamic_precedence = dynamic_precedence; self->dynamic_precedence = dynamic_precedence;
} }
static void stack_head_delete(StackHead *self, StackNodeArray *pool, SubtreePool *subtree_pool) static void stack_head_delete(StackHead *self, StackNodeArray *pool)
{ {
if (self->node) if (self->node)
{ {
if (self->last_external_token.ptr) if (self->last_external_token.ptr)
{ {
ts_subtree_release(subtree_pool, self->last_external_token); ts_subtree_release(self->last_external_token);
} }
if (self->lookahead_when_paused.ptr) if (self->lookahead_when_paused.ptr)
{ {
ts_subtree_release(subtree_pool, self->lookahead_when_paused); ts_subtree_release(self->lookahead_when_paused);
} }
if (self->summary) if (self->summary)
{ {
array_delete(self->summary); array_delete(self->summary);
mem_free(self->summary); mem_free(self->summary);
} }
stack_node_release(self->node, pool, subtree_pool); stack_node_release(self->node, pool);
} }
} }
@ -375,7 +374,7 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb
{ {
if (!should_pop) if (!should_pop)
{ {
ts_subtree_array_delete(self->subtree_pool, &iterator->subtrees); ts_subtree_array_delete(&iterator->subtrees);
} }
array_erase(&self->iterators, i); array_erase(&self->iterators, i);
i--, size--; i--, size--;
@ -432,7 +431,7 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb
return self->slices; return self->slices;
} }
Stack *ts_stack_new(SubtreePool *subtree_pool) Stack *ts_stack_new(void)
{ {
Stack *self = mem_alloc_array(1, sizeof(Stack)); Stack *self = mem_alloc_array(1, sizeof(Stack));
@ -445,7 +444,6 @@ Stack *ts_stack_new(SubtreePool *subtree_pool)
array_reserve(&self->iterators, 4); array_reserve(&self->iterators, 4);
array_reserve(&self->node_pool, MAX_NODE_POOL_SIZE); array_reserve(&self->node_pool, MAX_NODE_POOL_SIZE);
self->subtree_pool = subtree_pool;
self->base_node = stack_node_new(NULL, NULL_SUBTREE, false, 1, &self->node_pool); self->base_node = stack_node_new(NULL, NULL_SUBTREE, false, 1, &self->node_pool);
ts_stack_clear(self); ts_stack_clear(self);
@ -458,10 +456,10 @@ void ts_stack_delete(Stack *self)
array_delete(&self->slices); array_delete(&self->slices);
if (self->iterators.contents) if (self->iterators.contents)
array_delete(&self->iterators); array_delete(&self->iterators);
stack_node_release(self->base_node, &self->node_pool, self->subtree_pool); stack_node_release(self->base_node, &self->node_pool);
for (t_u32 i = 0; i < self->heads.size; i++) for (t_u32 i = 0; i < self->heads.size; i++)
{ {
stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); stack_head_delete(&self->heads.contents[i], &self->node_pool);
} }
array_clear(&self->heads); array_clear(&self->heads);
if (self->node_pool.contents) if (self->node_pool.contents)
@ -500,14 +498,14 @@ void ts_stack_set_last_external_token(Stack *self, StackVersion version, Subtree
if (token.ptr) if (token.ptr)
ts_subtree_retain(token); ts_subtree_retain(token);
if (head->last_external_token.ptr) if (head->last_external_token.ptr)
ts_subtree_release(self->subtree_pool, head->last_external_token); ts_subtree_release(head->last_external_token);
head->last_external_token = token; head->last_external_token = token;
} }
t_u32 ts_stack_error_cost(const Stack *self, StackVersion version) t_u32 ts_stack_error_cost(const Stack *self, StackVersion version)
{ {
StackHead *head = array_get(&self->heads, version); StackHead *head = array_get(&self->heads, version);
t_u32 result = head->node->error_cost; t_u32 result = head->node->error_cost;
if (head->status == StackStatusPaused || (head->node->state == ERROR_STATE && !head->node->links[0].subtree.ptr)) if (head->status == StackStatusPaused || (head->node->state == ERROR_STATE && !head->node->links[0].subtree.ptr))
{ {
result += ERROR_COST_PER_RECOVERY; result += ERROR_COST_PER_RECOVERY;
@ -639,14 +637,14 @@ StackSliceArray ts_stack_pop_all(Stack *self, StackVersion version)
typedef struct SummarizeStackSession typedef struct SummarizeStackSession
{ {
StackSummary *summary; StackSummary *summary;
t_u32 max_depth; t_u32 max_depth;
} SummarizeStackSession; } SummarizeStackSession;
StackAction summarize_stack_callback(void *payload, const StackIterator *iterator) StackAction summarize_stack_callback(void *payload, const StackIterator *iterator)
{ {
SummarizeStackSession *session = payload; SummarizeStackSession *session = payload;
TSStateId state = iterator->node->state; TSStateId state = iterator->node->state;
t_u32 depth = iterator->subtree_count; t_u32 depth = iterator->subtree_count;
if (depth > session->max_depth) if (depth > session->max_depth)
return StackActionStop; return StackActionStop;
for (t_u32 i = session->summary->size - 1; i + 1 > 0; i--) for (t_u32 i = session->summary->size - 1; i + 1 > 0; i--)
@ -720,7 +718,7 @@ bool ts_stack_has_advanced_since_error(const Stack *self, StackVersion version)
void ts_stack_remove_version(Stack *self, StackVersion version) void ts_stack_remove_version(Stack *self, StackVersion version)
{ {
stack_head_delete(array_get(&self->heads, version), &self->node_pool, self->subtree_pool); stack_head_delete(array_get(&self->heads, version), &self->node_pool);
array_erase(&self->heads, version); array_erase(&self->heads, version);
} }
@ -737,7 +735,7 @@ void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2)
source_head->summary = target_head->summary; source_head->summary = target_head->summary;
target_head->summary = NULL; target_head->summary = NULL;
} }
stack_head_delete(target_head, &self->node_pool, self->subtree_pool); stack_head_delete(target_head, &self->node_pool);
*target_head = *source_head; *target_head = *source_head;
array_erase(&self->heads, v1); array_erase(&self->heads, v1);
} }
@ -769,7 +767,7 @@ bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2)
StackHead *head2 = &self->heads.contents[version2]; StackHead *head2 = &self->heads.contents[version2];
for (t_u32 i = 0; i < head2->node->link_count; i++) for (t_u32 i = 0; i < head2->node->link_count; i++)
{ {
stack_node_add_link(head1->node, head2->node->links[i], self->subtree_pool); stack_node_add_link(head1->node, head2->node->links[i]);
} }
if (head1->node->state == ERROR_STATE) if (head1->node->state == ERROR_STATE)
{ {
@ -831,7 +829,7 @@ void ts_stack_clear(Stack *self)
stack_node_retain(self->base_node); stack_node_retain(self->base_node);
for (t_u32 i = 0; i < self->heads.size; i++) for (t_u32 i = 0; i < self->heads.size; i++)
{ {
stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); stack_head_delete(&self->heads.contents[i], &self->node_pool);
} }
array_clear(&self->heads); array_clear(&self->heads);
array_push(&self->heads, ((StackHead){ array_push(&self->heads, ((StackHead){

View file

@ -1,15 +1,19 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stddef.h>
#include "me/mem/mem.h"
#include "me/types.h" #include "me/types.h"
#include "parser/array.h" #include "parser/array.h"
#include "parser/language.h" #include "parser/language.h"
#include "parser/length.h" #include "parser/length.h"
#include "parser/subtree.h" #include "parser/subtree.h"
#include "me/mem/mem.h"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wunused-parameter"
typedef struct typedef struct
{ {
@ -91,18 +95,18 @@ void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest)
} }
} }
void ts_subtree_array_clear(SubtreePool *pool, SubtreeArray *self) void ts_subtree_array_clear(SubtreeArray *self)
{ {
for (t_u32 i = 0; i < self->size; i++) for (t_u32 i = 0; i < self->size; i++)
{ {
ts_subtree_release(pool, self->contents[i]); ts_subtree_release(self->contents[i]);
} }
array_clear(self); array_clear(self);
} }
void ts_subtree_array_delete(SubtreePool *pool, SubtreeArray *self) void ts_subtree_array_delete(SubtreeArray *self)
{ {
ts_subtree_array_clear(pool, self); ts_subtree_array_clear(self);
array_delete(self); array_delete(self);
} }
@ -136,139 +140,60 @@ void ts_subtree_array_reverse(SubtreeArray *self)
} }
} }
// SubtreePool static SubtreeHeapData *ts_subtree_pool_allocate()
SubtreePool ts_subtree_pool_new(t_u32 capacity)
{ {
SubtreePool self = {array_new(), array_new()}; return mem_alloc(sizeof(SubtreeHeapData));
array_reserve(&self.free_trees, capacity);
return self;
} }
void ts_subtree_pool_delete(SubtreePool *self) // static void ts_subtree_pool_free(SubtreeHeapData *tree)
{ //{
if (self->free_trees.contents) // mem_free(tree);
{ // }
for (t_u32 i = 0; i < self->free_trees.size; i++)
{
mem_free(self->free_trees.contents[i].ptr);
}
array_delete(&self->free_trees);
}
if (self->tree_stack.contents)
array_delete(&self->tree_stack);
}
static SubtreeHeapData *ts_subtree_pool_allocate(SubtreePool *self)
{
if (self->free_trees.size > 0)
{
return array_pop(&self->free_trees).ptr;
}
else
{
return mem_alloc(sizeof(SubtreeHeapData));
}
}
static void ts_subtree_pool_free(SubtreePool *self, SubtreeHeapData *tree)
{
if (self->free_trees.capacity > 0 && self->free_trees.size + 1 <= TS_MAX_TREE_POOL_SIZE)
{
array_push(&self->free_trees, (MutableSubtree){.ptr = tree});
}
else
{
mem_free(tree);
}
}
// Subtree // Subtree
static inline bool ts_subtree_can_inline(Length padding, Length size, t_u32 lookahead_bytes) Subtree ts_subtree_new_leaf(TSSymbol symbol, Length padding, Length size, t_u32 lookahead_bytes, TSStateId parse_state,
{ bool has_external_tokens, bool depends_on_column, bool is_keyword, const TSLanguage *language)
return padding.bytes < TS_MAX_INLINE_TREE_LENGTH && padding.extent.row < 16 && padding.extent.column < TS_MAX_INLINE_TREE_LENGTH &&
size.extent.row == 0 && size.extent.column < TS_MAX_INLINE_TREE_LENGTH && lookahead_bytes < 16;
}
Subtree ts_subtree_new_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, Length size, t_u32 lookahead_bytes,
TSStateId parse_state, bool has_external_tokens, bool depends_on_column, bool is_keyword,
const TSLanguage *language)
{ {
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
bool extra = symbol == ts_builtin_sym_end; bool extra = symbol == ts_builtin_sym_end;
bool is_inline = (symbol <= UINT8_MAX && !has_external_tokens && ts_subtree_can_inline(padding, size, lookahead_bytes)); SubtreeHeapData *data = ts_subtree_pool_allocate();
*data = (SubtreeHeapData){.ref_count = 1,
if (is_inline) .padding = padding,
{ .size = size,
return (Subtree){{ .lookahead_bytes = lookahead_bytes,
.parse_state = parse_state, .error_cost = 0,
.symbol = symbol, .child_count = 0,
.padding_bytes = padding.bytes, .symbol = symbol,
.padding_rows = padding.extent.row, .parse_state = parse_state,
.padding_columns = padding.extent.column, .visible = metadata.visible,
.size_bytes = size.bytes, .named = metadata.named,
.lookahead_bytes = lookahead_bytes, .extra = extra,
.visible = metadata.visible, .fragile_left = false,
.named = metadata.named, .fragile_right = false,
.extra = extra, .has_changes = false,
.has_changes = false, .has_external_tokens = has_external_tokens,
.is_missing = false, .has_external_scanner_state_change = false,
.is_keyword = is_keyword, .depends_on_column = depends_on_column,
.is_inline = true, .is_missing = false,
}}; .is_keyword = is_keyword,
} {{.first_leaf = {.symbol = 0, .parse_state = 0}}}};
else return (Subtree){.ptr = data};
{
SubtreeHeapData *data = ts_subtree_pool_allocate(pool);
*data = (SubtreeHeapData){.ref_count = 1,
.padding = padding,
.size = size,
.lookahead_bytes = lookahead_bytes,
.error_cost = 0,
.child_count = 0,
.symbol = symbol,
.parse_state = parse_state,
.visible = metadata.visible,
.named = metadata.named,
.extra = extra,
.fragile_left = false,
.fragile_right = false,
.has_changes = false,
.has_external_tokens = has_external_tokens,
.has_external_scanner_state_change = false,
.depends_on_column = depends_on_column,
.is_missing = false,
.is_keyword = is_keyword,
{{.first_leaf = {.symbol = 0, .parse_state = 0}}}};
return (Subtree){.ptr = data};
}
} }
void ts_subtree_set_symbol(MutableSubtree *self, TSSymbol symbol, const TSLanguage *language) void ts_subtree_set_symbol(MutableSubtree *self, TSSymbol symbol, const TSLanguage *language)
{ {
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
if (self->data.is_inline) self->ptr->symbol = symbol;
{ self->ptr->named = metadata.named;
assert(symbol < UINT8_MAX); self->ptr->visible = metadata.visible;
self->data.symbol = symbol;
self->data.named = metadata.named;
self->data.visible = metadata.visible;
}
else
{
self->ptr->symbol = symbol;
self->ptr->named = metadata.named;
self->ptr->visible = metadata.visible;
}
} }
Subtree ts_subtree_new_error(SubtreePool *pool, t_i32 lookahead_char, Length padding, Length size, t_u32 bytes_scanned, Subtree ts_subtree_new_error(t_i32 lookahead_char, Length padding, Length size, t_u32 bytes_scanned, TSStateId parse_state,
TSStateId parse_state, const TSLanguage *language) const TSLanguage *language)
{ {
Subtree result = Subtree result = ts_subtree_new_leaf(ts_builtin_sym_error, padding, size, bytes_scanned, parse_state, false, false, false, language);
ts_subtree_new_leaf(pool, ts_builtin_sym_error, padding, size, bytes_scanned, parse_state, false, false, false, language);
SubtreeHeapData *data = (SubtreeHeapData *)result.ptr; SubtreeHeapData *data = (SubtreeHeapData *)result.ptr;
data->fragile_left = true; data->fragile_left = true;
data->fragile_right = true; data->fragile_right = true;
@ -304,17 +229,16 @@ MutableSubtree ts_subtree_clone(Subtree self)
// This takes ownership of the subtree. If the subtree has only one owner, // This takes ownership of the subtree. If the subtree has only one owner,
// this will directly convert it into a mutable version. Otherwise, it will // this will directly convert it into a mutable version. Otherwise, it will
// perform a copy. // perform a copy.
MutableSubtree ts_subtree_make_mut(SubtreePool *pool, Subtree self) MutableSubtree ts_subtree_make_mut(Subtree self)
{ {
if (self.data.is_inline)
return (MutableSubtree){self.data};
if (self.ptr->ref_count == 1) if (self.ptr->ref_count == 1)
return ts_subtree_to_mut_unsafe(self); return ts_subtree_to_mut_unsafe(self);
MutableSubtree result = ts_subtree_clone(self); MutableSubtree result = ts_subtree_clone(self);
ts_subtree_release(pool, self); ts_subtree_release(self);
return result; return result;
} }
/*
static void ts_subtree__compress(MutableSubtree self, t_u32 count, const TSLanguage *language, MutableSubtreeArray *stack) static void ts_subtree__compress(MutableSubtree self, t_u32 count, const TSLanguage *language, MutableSubtreeArray *stack)
{ {
t_u32 initial_stack_size = stack->size; t_u32 initial_stack_size = stack->size;
@ -327,12 +251,11 @@ static void ts_subtree__compress(MutableSubtree self, t_u32 count, const TSLangu
break; break;
MutableSubtree child = ts_subtree_to_mut_unsafe(ts_subtree_children(tree)[0]); MutableSubtree child = ts_subtree_to_mut_unsafe(ts_subtree_children(tree)[0]);
if (child.data.is_inline || child.ptr->child_count < 2 || child.ptr->ref_count > 1 || child.ptr->symbol != symbol) if (child.ptr->child_count < 2 || child.ptr->ref_count > 1 || child.ptr->symbol != symbol)
break; break;
MutableSubtree grandchild = ts_subtree_to_mut_unsafe(ts_subtree_children(child)[0]); MutableSubtree grandchild = ts_subtree_to_mut_unsafe(ts_subtree_children(child)[0]);
if (grandchild.data.is_inline || grandchild.ptr->child_count < 2 || grandchild.ptr->ref_count > 1 || if (grandchild.ptr->child_count < 2 || grandchild.ptr->ref_count > 1 || grandchild.ptr->symbol != symbol)
grandchild.ptr->symbol != symbol)
break; break;
ts_subtree_children(tree)[0] = ts_subtree_from_mut(grandchild); ts_subtree_children(tree)[0] = ts_subtree_from_mut(grandchild);
@ -352,11 +275,12 @@ static void ts_subtree__compress(MutableSubtree self, t_u32 count, const TSLangu
ts_subtree_summarize_children(tree, language); ts_subtree_summarize_children(tree, language);
} }
} }
*/
void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *language) void ts_subtree_balance(Subtree self, const TSLanguage *language)
{ {
array_clear(&pool->tree_stack); printf("BALANCING TREES\n");
/*
if (ts_subtree_child_count(self) > 0 && self.ptr->ref_count == 1) if (ts_subtree_child_count(self) > 0 && self.ptr->ref_count == 1)
{ {
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self)); array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self));
@ -391,13 +315,12 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu
} }
} }
} }
*/
} }
// Assign all of the node's properties that depend on its children. // Assign all of the node's properties that depend on its children.
void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *language) void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *language)
{ {
assert(!self.data.is_inline);
self.ptr->named_child_count = 0; self.ptr->named_child_count = 0;
self.ptr->visible_child_count = 0; self.ptr->visible_child_count = 0;
self.ptr->error_cost = 0; self.ptr->error_cost = 0;
@ -408,9 +331,9 @@ void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *langua
self.ptr->has_external_scanner_state_change = false; self.ptr->has_external_scanner_state_change = false;
self.ptr->dynamic_precedence = 0; self.ptr->dynamic_precedence = 0;
t_u32 structural_index = 0; t_u32 structural_index = 0;
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id); const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id);
t_u32 lookahead_end_byte = 0; t_u32 lookahead_end_byte = 0;
const Subtree *children = ts_subtree_children(self); const Subtree *children = ts_subtree_children(self);
for (t_u32 i = 0; i < self.ptr->child_count; i++) for (t_u32 i = 0; i < self.ptr->child_count; i++)
@ -589,38 +512,51 @@ Subtree ts_subtree_new_error_node(SubtreeArray *children, bool extra, const TSLa
// //
// This node is treated as 'extra'. Its children are prevented from having // This node is treated as 'extra'. Its children are prevented from having
// having any effect on the parse state. // having any effect on the parse state.
Subtree ts_subtree_new_missing_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, t_u32 lookahead_bytes, Subtree ts_subtree_new_missing_leaf(TSSymbol symbol, Length padding, t_u32 lookahead_bytes, const TSLanguage *language)
const TSLanguage *language)
{ {
Subtree result = ts_subtree_new_leaf(pool, symbol, padding, length_zero(), lookahead_bytes, 0, false, false, false, language); Subtree result = ts_subtree_new_leaf(symbol, padding, length_zero(), lookahead_bytes, 0, false, false, false, language);
if (result.data.is_inline) ((SubtreeHeapData *)result.ptr)->is_missing = true;
{
result.data.is_missing = true;
}
else
{
((SubtreeHeapData *)result.ptr)->is_missing = true;
}
return result; return result;
} }
void ts_subtree_retain(Subtree self) void ts_subtree_retain(Subtree self)
{ {
if (self.data.is_inline)
return;
assert(self.ptr->ref_count > 0); assert(self.ptr->ref_count > 0);
(*(t_u32 *)(&self.ptr->ref_count))++; (*(t_u32 *)(&self.ptr->ref_count))++;
assert(self.ptr->ref_count != 0); assert(self.ptr->ref_count != 0);
} }
void ts_subtree_release(SubtreePool *pool, Subtree self) void ts_subtree_release(Subtree self)
{ {
if (self.data.is_inline) t_usize i;
return; Subtree *children;
if (self.ptr->ref_count != 0 && --(*(t_u32 *)(&self.ptr->ref_count)) == 0)
{
if (self.ptr->child_count > 0)
{
children = ts_subtree_children(self);
i = 0;
while (i < self.ptr->child_count)
ts_subtree_release(children[i++]);
mem_free(children);
}
else
{
if (self.ptr->has_external_tokens)
{
ts_external_scanner_state_delete((void *)&self.ptr->external_scanner_state);
}
mem_free((void *)self.ptr);
}
}
/*
array_clear(&pool->tree_stack); array_clear(&pool->tree_stack);
assert(self.ptr->ref_count > 0); assert(self.ptr->ref_count > 0);
if (--(*(t_u32 *)(&self.ptr->ref_count)) == 0) if ()
{ {
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self)); array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self));
} }
@ -634,8 +570,6 @@ void ts_subtree_release(SubtreePool *pool, Subtree self)
for (t_u32 i = 0; i < tree.ptr->child_count; i++) for (t_u32 i = 0; i < tree.ptr->child_count; i++)
{ {
Subtree child = children[i]; Subtree child = children[i];
if (child.data.is_inline)
continue;
assert(child.ptr->ref_count > 0); assert(child.ptr->ref_count > 0);
if (--(*(t_u32 *)(&child.ptr->ref_count)) == 0) if (--(*(t_u32 *)(&child.ptr->ref_count)) == 0)
{ {
@ -653,10 +587,13 @@ void ts_subtree_release(SubtreePool *pool, Subtree self)
ts_subtree_pool_free(pool, tree.ptr); ts_subtree_pool_free(pool, tree.ptr);
} }
} }
*/
} }
int ts_subtree_compare(Subtree left, Subtree right, SubtreePool *pool) int ts_subtree_compare(Subtree left, Subtree right)
{ {
printf("SUBTREE COMPARE\n");
/*
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left)); array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left));
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right)); array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right));
@ -687,24 +624,17 @@ int ts_subtree_compare(Subtree left, Subtree right, SubtreePool *pool)
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left_child)); array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left_child));
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right_child)); array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right_child));
} }
} }*/
return 0; return 0;
} }
static inline void ts_subtree_set_has_changes(MutableSubtree *self) static inline void ts_subtree_set_has_changes(MutableSubtree *self)
{ {
if (self->data.is_inline) self->ptr->has_changes = true;
{
self->data.has_changes = true;
}
else
{
self->ptr->has_changes = true;
}
} }
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool *pool) Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit)
{ {
typedef struct typedef struct
{ {
@ -731,11 +661,11 @@ Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool
bool is_pure_insertion = edit.old_end.bytes == edit.start.bytes; bool is_pure_insertion = edit.old_end.bytes == edit.start.bytes;
bool invalidate_first_row = ts_subtree_depends_on_column(*entry.tree); bool invalidate_first_row = ts_subtree_depends_on_column(*entry.tree);
Length size = ts_subtree_size(*entry.tree); Length size = ts_subtree_size(*entry.tree);
Length padding = ts_subtree_padding(*entry.tree); Length padding = ts_subtree_padding(*entry.tree);
Length total_size = length_add(padding, size); Length total_size = length_add(padding, size);
t_u32 lookahead_bytes = ts_subtree_lookahead_bytes(*entry.tree); t_u32 lookahead_bytes = ts_subtree_lookahead_bytes(*entry.tree);
t_u32 end_byte = total_size.bytes + lookahead_bytes; t_u32 end_byte = total_size.bytes + lookahead_bytes;
if (edit.start.bytes > end_byte || (is_noop && edit.start.bytes == end_byte)) if (edit.start.bytes > end_byte || (is_noop && edit.start.bytes == end_byte))
continue; continue;
@ -767,46 +697,10 @@ Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool
size = length_add(length_sub(edit.new_end, padding), length_saturating_sub(total_size, edit.old_end)); size = length_add(length_sub(edit.new_end, padding), length_saturating_sub(total_size, edit.old_end));
} }
MutableSubtree result = ts_subtree_make_mut(pool, *entry.tree); MutableSubtree result = ts_subtree_make_mut(*entry.tree);
if (result.data.is_inline) result.ptr->padding = padding;
{ result.ptr->size = size;
if (ts_subtree_can_inline(padding, size, lookahead_bytes))
{
result.data.padding_bytes = padding.bytes;
result.data.padding_rows = padding.extent.row;
result.data.padding_columns = padding.extent.column;
result.data.size_bytes = size.bytes;
}
else
{
SubtreeHeapData *data = ts_subtree_pool_allocate(pool);
data->ref_count = 1;
data->padding = padding;
data->size = size;
data->lookahead_bytes = lookahead_bytes;
data->error_cost = 0;
data->child_count = 0;
data->symbol = result.data.symbol;
data->parse_state = result.data.parse_state;
data->visible = result.data.visible;
data->named = result.data.named;
data->extra = result.data.extra;
data->fragile_left = false;
data->fragile_right = false;
data->has_changes = false;
data->has_external_tokens = false;
data->depends_on_column = false;
data->is_missing = result.data.is_missing;
data->is_keyword = result.data.is_keyword;
result.ptr = data;
}
}
else
{
result.ptr->padding = padding;
result.ptr->size = size;
}
ts_subtree_set_has_changes(&result); ts_subtree_set_has_changes(&result);
*entry.tree = ts_subtree_from_mut(result); *entry.tree = ts_subtree_from_mut(result);
@ -885,215 +779,10 @@ Subtree ts_subtree_last_external_token(Subtree tree)
return tree; return tree;
} }
static size_t ts_subtree__write_char_to_string(char *str, size_t n, t_i32 chr)
{
if (chr == -1)
return snprintf(str, n, "INVALID");
else if (chr == '\0')
return snprintf(str, n, "'\\0'");
else if (chr == '\n')
return snprintf(str, n, "'\\n'");
else if (chr == '\t')
return snprintf(str, n, "'\\t'");
else if (chr == '\r')
return snprintf(str, n, "'\\r'");
else if (0 < chr && chr < 128 && isprint(chr))
return snprintf(str, n, "'%c'", chr);
else
return snprintf(str, n, "%d", chr);
}
static t_const_str const ROOT_FIELD = "__ROOT__";
static size_t ts_subtree__write_to_string(Subtree self, char *string, size_t limit, const TSLanguage *language, bool include_all,
TSSymbol alias_symbol, bool alias_is_named, t_const_str field_name)
{
if (!self.ptr)
return snprintf(string, limit, "(NULL)");
char *cursor = string;
char **writer = (limit > 1) ? &cursor : &string;
bool is_root = field_name == ROOT_FIELD;
bool is_visible =
include_all || ts_subtree_missing(self) || (alias_symbol ? alias_is_named : ts_subtree_visible(self) && ts_subtree_named(self));
if (is_visible)
{
if (!is_root)
{
cursor += snprintf(*writer, limit, " ");
if (field_name)
{
cursor += snprintf(*writer, limit, "%s: ", field_name);
}
}
if (ts_subtree_is_error(self) && ts_subtree_child_count(self) == 0 && self.ptr->size.bytes > 0)
{
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
cursor += ts_subtree__write_char_to_string(*writer, limit, self.ptr->lookahead_char);
}
else
{
TSSymbol symbol = alias_symbol ? alias_symbol : ts_subtree_symbol(self);
t_const_str symbol_name = ts_language_symbol_name(language, symbol);
if (ts_subtree_missing(self))
{
cursor += snprintf(*writer, limit, "(MISSING ");
if (alias_is_named || ts_subtree_named(self))
{
cursor += snprintf(*writer, limit, "%s", symbol_name);
}
else
{
cursor += snprintf(*writer, limit, "\"%s\"", symbol_name);
}
}
else
{
cursor += snprintf(*writer, limit, "(%s", symbol_name);
}
}
}
else if (is_root)
{
TSSymbol symbol = alias_symbol ? alias_symbol : ts_subtree_symbol(self);
t_const_str symbol_name = ts_language_symbol_name(language, symbol);
if (ts_subtree_child_count(self) > 0)
{
cursor += snprintf(*writer, limit, "(%s", symbol_name);
}
else if (ts_subtree_named(self))
{
cursor += snprintf(*writer, limit, "(%s)", symbol_name);
}
else
{
cursor += snprintf(*writer, limit, "(\"%s\")", symbol_name);
}
}
if (ts_subtree_child_count(self))
{
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id);
const TSFieldMapEntry *field_map, *field_map_end;
ts_language_field_map(language, self.ptr->production_id, &field_map, &field_map_end);
t_u32 structural_child_index = 0;
for (t_u32 i = 0; i < self.ptr->child_count; i++)
{
Subtree child = ts_subtree_children(self)[i];
if (ts_subtree_extra(child))
{
cursor += ts_subtree__write_to_string(child, *writer, limit, language, include_all, 0, false, NULL);
}
else
{
TSSymbol subtree_alias_symbol = alias_sequence ? alias_sequence[structural_child_index] : 0;
bool subtree_alias_is_named =
subtree_alias_symbol ? ts_language_symbol_metadata(language, subtree_alias_symbol).named : false;
t_const_str child_field_name = is_visible ? NULL : field_name;
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++)
{
if (!map->inherited && map->child_index == structural_child_index)
{
child_field_name = language->field_names[map->field_id];
break;
}
}
cursor += ts_subtree__write_to_string(child, *writer, limit, language, include_all, subtree_alias_symbol,
subtree_alias_is_named, child_field_name);
structural_child_index++;
}
}
}
if (is_visible)
cursor += snprintf(*writer, limit, ")");
return cursor - string;
}
char *ts_subtree_string(Subtree self, TSSymbol alias_symbol, bool alias_is_named, const TSLanguage *language, bool include_all)
{
char scratch_string[1];
size_t size = ts_subtree__write_to_string(self, scratch_string, 1, language, include_all, alias_symbol, alias_is_named, ROOT_FIELD) + 1;
char *result = mem_alloc(size * sizeof(char));
ts_subtree__write_to_string(self, result, size, language, include_all, alias_symbol, alias_is_named, ROOT_FIELD);
return result;
}
/*
void ts_subtree__print_dot_graph(const Subtree *self, t_u32 start_offset, const TSLanguage *language, TSSymbol alias_symbol, FILE *f)
{
TSSymbol subtree_symbol = ts_subtree_symbol(*self);
TSSymbol symbol = alias_symbol ? alias_symbol : subtree_symbol;
t_u32 end_offset = start_offset + ts_subtree_total_bytes(*self);
fprintf(f, "tree_%p [label=\"", (void *)self);
ts_language_write_symbol_as_dot_string(language, f, symbol);
fprintf(f, "\"");
if (ts_subtree_child_count(*self) == 0)
fprintf(f, ", shape=plaintext");
if (ts_subtree_extra(*self))
fprintf(f, ", fontcolor=gray");
fprintf(f,
", tooltip=\""
"range: %u - %u\n"
"state: %d\n"
"error-cost: %u\n"
"has-changes: %u\n"
"depends-on-column: %u\n"
"descendant-count: %u\n"
"repeat-depth: %u\n"
"lookahead-bytes: %u",
start_offset, end_offset, ts_subtree_parse_state(*self), ts_subtree_error_cost(*self), ts_subtree_has_changes(*self),
ts_subtree_depends_on_column(*self), ts_subtree_visible_descendant_count(*self), ts_subtree_repeat_depth(*self),
ts_subtree_lookahead_bytes(*self));
if (ts_subtree_is_error(*self) && ts_subtree_child_count(*self) == 0 && self->ptr->lookahead_char != 0)
{
fprintf(f, "\ncharacter: '%c'", self->ptr->lookahead_char);
}
fprintf(f, "\"]\n");
t_u32 child_start_offset = start_offset;
t_u32 child_info_offset = language->max_alias_sequence_length * ts_subtree_production_id(*self);
for (t_u32 i = 0, n = ts_subtree_child_count(*self); i < n; i++)
{
const Subtree *child = &ts_subtree_children(*self)[i];
TSSymbol subtree_alias_symbol = 0;
if (!ts_subtree_extra(*child) && child_info_offset)
{
subtree_alias_symbol = language->alias_sequences[child_info_offset];
child_info_offset++;
}
ts_subtree__print_dot_graph(child, child_start_offset, language, subtree_alias_symbol, f);
fprintf(f, "tree_%p -> tree_%p [tooltip=%u]\n", (void *)self, (void *)child, i);
child_start_offset += ts_subtree_total_bytes(*child);
}
}
*/
/*
void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f)
{
fprintf(f, "digraph tree {\n");
fprintf(f, "edge [arrowhead=none]\n");
ts_subtree__print_dot_graph(&self, 0, language, 0, f);
fprintf(f, "}\n");
}
*/
const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self) const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self)
{ {
static const ExternalScannerState empty_state = {{.short_data = {0}}, .length = 0}; static const ExternalScannerState empty_state = {{.short_data = {0}}, .length = 0};
if (self.ptr && !self.data.is_inline && self.ptr->has_external_tokens && self.ptr->child_count == 0) if (self.ptr && self.ptr->has_external_tokens && self.ptr->child_count == 0)
{ {
return &self.ptr->external_scanner_state; return &self.ptr->external_scanner_state;
} }

View file

@ -30,10 +30,6 @@ void ts_tree_delete(TSTree *self)
{ {
if (!self) if (!self)
return; return;
SubtreePool pool = ts_subtree_pool_new(0);
ts_subtree_release(&pool, self->root);
ts_subtree_pool_delete(&pool);
ts_language_delete(self->language); ts_language_delete(self->language);
mem_free(self->included_ranges); mem_free(self->included_ranges);
mem_free(self); mem_free(self);
@ -94,10 +90,6 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit)
range->start_point = edit->start_point; range->start_point = edit->start_point;
} }
} }
SubtreePool pool = ts_subtree_pool_new(0);
self->root = ts_subtree_edit(self->root, edit, &pool);
ts_subtree_pool_delete(&pool);
} }
TSRange *ts_tree_included_ranges(const TSTree *self, t_u32 *length) TSRange *ts_tree_included_ranges(const TSTree *self, t_u32 *length)