Refactoring some stuff in the parser lib, moving functions out of headers
This commit is contained in:
parent
4580d68951
commit
fb3a2d94a0
19 changed files with 522 additions and 608 deletions
|
|
@ -1,12 +1,11 @@
|
|||
#ifndef TREE_SITTER_SUBTREE_H_
|
||||
#define TREE_SITTER_SUBTREE_H_
|
||||
#ifndef SUBTREE_H
|
||||
#define SUBTREE_H
|
||||
|
||||
|
||||
#include "me/types.h"
|
||||
#include "./api.h"
|
||||
#include "./array.h"
|
||||
#include "./length.h"
|
||||
#include "./parser.h"
|
||||
#include "api.h"
|
||||
#include "me/types.h"
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
|
@ -16,6 +15,15 @@
|
|||
#define TS_TREE_STATE_NONE USHRT_MAX
|
||||
#define NULL_SUBTREE ((Subtree){.ptr = NULL})
|
||||
|
||||
struct ExternalScannerState
|
||||
{
|
||||
union {
|
||||
char *long_data;
|
||||
char short_data[24];
|
||||
};
|
||||
t_u32 length;
|
||||
};
|
||||
|
||||
// The serialized state of an external scanner.
|
||||
//
|
||||
// Every time an external token subtree is created after a call to an
|
||||
|
|
@ -26,14 +34,7 @@
|
|||
//
|
||||
// Small byte arrays are stored inline, and long ones are allocated
|
||||
// separately on the heap.
|
||||
typedef struct ExternalScannerState
|
||||
{
|
||||
union {
|
||||
char *long_data;
|
||||
char short_data[24];
|
||||
};
|
||||
t_u32 length;
|
||||
} ExternalScannerState;
|
||||
typedef struct ExternalScannerState ExternalScannerState;
|
||||
|
||||
// A compact representation of a subtree.
|
||||
//
|
||||
|
|
@ -48,63 +49,24 @@ typedef struct ExternalScannerState
|
|||
// the pointer or the inline struct.
|
||||
typedef struct SubtreeInlineData SubtreeInlineData;
|
||||
|
||||
#define SUBTREE_BITS \
|
||||
bool visible : 1; \
|
||||
bool named : 1; \
|
||||
bool extra : 1; \
|
||||
bool has_changes : 1; \
|
||||
bool is_missing : 1; \
|
||||
bool is_keyword : 1;
|
||||
|
||||
#define SUBTREE_SIZE \
|
||||
t_u8 padding_columns; \
|
||||
t_u8 padding_rows : 4; \
|
||||
t_u8 lookahead_bytes : 4; \
|
||||
t_u8 padding_bytes; \
|
||||
t_u8 size_bytes;
|
||||
|
||||
#if TS_BIG_ENDIAN
|
||||
# if TS_PTR_SIZE == 32
|
||||
|
||||
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 symbol;
|
||||
SUBTREE_BITS
|
||||
bool unused : 1;
|
||||
bool is_inline : 1;
|
||||
SUBTREE_SIZE
|
||||
t_u8 padding_columns;
|
||||
t_u8 padding_rows : 4;
|
||||
t_u8 lookahead_bytes : 4;
|
||||
t_u8 padding_bytes;
|
||||
t_u8 size_bytes;
|
||||
};
|
||||
|
||||
# else
|
||||
|
||||
struct SubtreeInlineData
|
||||
{
|
||||
SUBTREE_SIZE
|
||||
t_u16 parse_state;
|
||||
t_u8 symbol;
|
||||
SUBTREE_BITS
|
||||
bool unused : 1;
|
||||
bool is_inline : 1;
|
||||
};
|
||||
|
||||
# endif
|
||||
#else
|
||||
|
||||
struct SubtreeInlineData
|
||||
{
|
||||
bool is_inline : 1;
|
||||
SUBTREE_BITS
|
||||
t_u8 symbol;
|
||||
t_u16 parse_state;
|
||||
SUBTREE_SIZE
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#undef SUBTREE_BITS
|
||||
#undef SUBTREE_SIZE
|
||||
|
||||
// A heap-allocated representation of a subtree.
|
||||
//
|
||||
// This representation is used for parent nodes, external tokens,
|
||||
|
|
@ -113,13 +75,13 @@ struct SubtreeInlineData
|
|||
typedef struct SubtreeHeapData
|
||||
{
|
||||
volatile t_u32 ref_count;
|
||||
Length padding;
|
||||
Length size;
|
||||
t_u32 lookahead_bytes;
|
||||
t_u32 error_cost;
|
||||
t_u32 child_count;
|
||||
TSSymbol symbol;
|
||||
TSStateId parse_state;
|
||||
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;
|
||||
|
|
@ -140,7 +102,7 @@ typedef struct SubtreeHeapData
|
|||
t_u32 visible_child_count;
|
||||
t_u32 named_child_count;
|
||||
t_u32 visible_descendant_count;
|
||||
t_i32 dynamic_precedence;
|
||||
t_i32 dynamic_precedence;
|
||||
t_u16 repeat_depth;
|
||||
t_u16 production_id;
|
||||
struct
|
||||
|
|
@ -213,47 +175,43 @@ 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);
|
||||
|
||||
#define SUBTREE_GET(self, name) ((self).data.is_inline ? (self).data.name : (self).ptr->name)
|
||||
|
||||
static inline TSSymbol ts_subtree_symbol(Subtree self)
|
||||
{
|
||||
return SUBTREE_GET(self, symbol);
|
||||
return ((self).data.is_inline ? (self).data.symbol : (self).ptr->symbol);
|
||||
}
|
||||
static inline bool ts_subtree_visible(Subtree self)
|
||||
{
|
||||
return SUBTREE_GET(self, visible);
|
||||
return ((self).data.is_inline ? (self).data.visible : (self).ptr->visible);
|
||||
}
|
||||
static inline bool ts_subtree_named(Subtree self)
|
||||
{
|
||||
return SUBTREE_GET(self, named);
|
||||
return ((self).data.is_inline ? (self).data.named : (self).ptr->named);
|
||||
}
|
||||
static inline bool ts_subtree_extra(Subtree self)
|
||||
{
|
||||
return SUBTREE_GET(self, extra);
|
||||
return ((self).data.is_inline ? (self).data.extra : (self).ptr->extra);
|
||||
}
|
||||
static inline bool ts_subtree_has_changes(Subtree self)
|
||||
{
|
||||
return SUBTREE_GET(self, has_changes);
|
||||
return ((self).data.is_inline ? (self).data.has_changes : (self).ptr->has_changes);
|
||||
}
|
||||
static inline bool ts_subtree_missing(Subtree self)
|
||||
{
|
||||
return SUBTREE_GET(self, 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 SUBTREE_GET(self, 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 SUBTREE_GET(self, 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 SUBTREE_GET(self, lookahead_bytes);
|
||||
return ((self).data.is_inline ? (self).data.lookahead_bytes : (self).ptr->lookahead_bytes);
|
||||
}
|
||||
|
||||
#undef SUBTREE_GET
|
||||
|
||||
// Get the size needed to store a heap-allocated subtree with the given
|
||||
// number of children.
|
||||
static inline size_t ts_subtree_alloc_size(t_u32 child_count)
|
||||
|
|
@ -268,13 +226,9 @@ static inline size_t ts_subtree_alloc_size(t_u32 child_count)
|
|||
static inline void ts_subtree_set_extra(MutableSubtree *self, bool is_extra)
|
||||
{
|
||||
if (self->data.is_inline)
|
||||
{
|
||||
self->data.extra = is_extra;
|
||||
}
|
||||
else
|
||||
{
|
||||
self->ptr->extra = is_extra;
|
||||
}
|
||||
}
|
||||
|
||||
static inline TSSymbol ts_subtree_leaf_symbol(Subtree self)
|
||||
|
|
@ -298,156 +252,132 @@ static inline TSStateId ts_subtree_leaf_parse_state(Subtree self)
|
|||
static inline Length ts_subtree_padding(Subtree self)
|
||||
{
|
||||
if (self.data.is_inline)
|
||||
{
|
||||
Length result = {self.data.padding_bytes, {self.data.padding_rows, self.data.padding_columns}};
|
||||
return result;
|
||||
}
|
||||
return ((Length){self.data.padding_bytes, {self.data.padding_rows, self.data.padding_columns}});
|
||||
else
|
||||
{
|
||||
return self.ptr->padding;
|
||||
}
|
||||
}
|
||||
|
||||
static inline Length ts_subtree_size(Subtree self)
|
||||
{
|
||||
if (self.data.is_inline)
|
||||
{
|
||||
Length result = {self.data.size_bytes, {0, self.data.size_bytes}};
|
||||
return result;
|
||||
}
|
||||
return ((Length){self.data.size_bytes, {0, self.data.size_bytes}});
|
||||
else
|
||||
{
|
||||
return self.ptr->size;
|
||||
}
|
||||
}
|
||||
|
||||
static inline Length ts_subtree_total_size(Subtree self)
|
||||
{
|
||||
return length_add(ts_subtree_padding(self), ts_subtree_size(self));
|
||||
return (length_add(ts_subtree_padding(self), ts_subtree_size(self)));
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_total_bytes(Subtree self)
|
||||
{
|
||||
return ts_subtree_total_size(self).bytes;
|
||||
return (ts_subtree_total_size(self).bytes);
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_child_count(Subtree self)
|
||||
{
|
||||
return self.data.is_inline ? 0 : self.ptr->child_count;
|
||||
return (self.data.is_inline ? 0 : self.ptr->child_count);
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_repeat_depth(Subtree self)
|
||||
{
|
||||
return self.data.is_inline ? 0 : self.ptr->repeat_depth;
|
||||
return (self.data.is_inline ? 0 : self.ptr->repeat_depth);
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_is_repetition(Subtree self)
|
||||
{
|
||||
return self.data.is_inline ? 0 : !self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0;
|
||||
return (self.data.is_inline ? 0 : !self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0);
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_visible_descendant_count(Subtree self)
|
||||
{
|
||||
return (self.data.is_inline || self.ptr->child_count == 0) ? 0 : self.ptr->visible_descendant_count;
|
||||
return ((self.data.is_inline || self.ptr->child_count == 0) ? 0 : self.ptr->visible_descendant_count);
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_visible_child_count(Subtree self)
|
||||
{
|
||||
if (ts_subtree_child_count(self) > 0)
|
||||
{
|
||||
return self.ptr->visible_child_count;
|
||||
}
|
||||
return (self.ptr->visible_child_count);
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static inline t_u32 ts_subtree_error_cost(Subtree self)
|
||||
{
|
||||
if (ts_subtree_missing(self))
|
||||
{
|
||||
return ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY;
|
||||
}
|
||||
return (ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY);
|
||||
else
|
||||
{
|
||||
return self.data.is_inline ? 0 : 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.data.is_inline || 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)
|
||||
{
|
||||
if (ts_subtree_child_count(self) > 0)
|
||||
{
|
||||
return self.ptr->production_id;
|
||||
}
|
||||
return (self.ptr->production_id);
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static inline bool ts_subtree_fragile_left(Subtree self)
|
||||
{
|
||||
return self.data.is_inline ? false : 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.data.is_inline ? false : 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.data.is_inline ? false : 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.data.is_inline ? false : 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.data.is_inline ? false : 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.data.is_inline ? false : (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)
|
||||
{
|
||||
return ts_subtree_symbol(self) == ts_builtin_sym_error;
|
||||
return (ts_subtree_symbol(self) == ts_builtin_sym_error);
|
||||
}
|
||||
|
||||
static inline bool ts_subtree_is_eof(Subtree self)
|
||||
{
|
||||
return ts_subtree_symbol(self) == ts_builtin_sym_end;
|
||||
return (ts_subtree_symbol(self) == ts_builtin_sym_end);
|
||||
}
|
||||
|
||||
static inline Subtree ts_subtree_from_mut(MutableSubtree self)
|
||||
{
|
||||
Subtree result;
|
||||
|
||||
result.data = self.data;
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
static inline MutableSubtree ts_subtree_to_mut_unsafe(Subtree self)
|
||||
{
|
||||
MutableSubtree result;
|
||||
|
||||
result.data = self.data;
|
||||
return result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_SUBTREE_H_
|
||||
#endif // SUBTREE_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue