From 38bdd66f7819f96123d4f8221ec9a0b8c55cdc86 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Wed, 3 Jul 2024 19:03:19 +0200 Subject: [PATCH] Changed (u)int\d+_t to their stdme types --- parser/src/api.h | 111 +++--- parser/src/array.h | 35 +- parser/src/create_language.c | 48 +-- parser/src/error_costs.h | 6 - parser/src/language.c | 297 ++++++++------- parser/src/language.h | 26 +- parser/src/length.h | 66 ++-- parser/src/lexer.c | 675 ++++++++++++++++++----------------- parser/src/lexer.h | 61 ++-- parser/src/node.c | 84 ++--- parser/src/parser.c | 115 +++--- parser/src/parser.h | 90 +++-- parser/src/point.h | 81 +++-- parser/src/reduce_action.h | 35 +- parser/src/scanner.c | 63 ++-- parser/src/stack.c | 34 +- parser/src/stack.h | 9 +- parser/src/subtree.c | 70 ++-- parser/src/subtree.h | 81 ++--- parser/src/tree.c | 6 +- parser/src/tree.h | 2 + parser/src/unicode.h | 20 +- 22 files changed, 1034 insertions(+), 981 deletions(-) delete mode 100644 parser/src/error_costs.h diff --git a/parser/src/api.h b/parser/src/api.h index 9924a695..3dc444a7 100644 --- a/parser/src/api.h +++ b/parser/src/api.h @@ -1,9 +1,7 @@ #ifndef TREE_SITTER_API_H_ #define TREE_SITTER_API_H_ -#include -#include -#include +#include "me/types.h" #define ERROR_STATE 0 #define ERROR_COST_PER_RECOVERY 500 @@ -35,9 +33,9 @@ /* Section - Types */ /*******************/ -typedef uint16_t TSStateId; -typedef uint16_t TSSymbol; -typedef uint16_t TSFieldId; +typedef t_u16 TSStateId; +typedef t_u16 TSSymbol; +typedef t_u16 TSFieldId; typedef struct TSLanguage TSLanguage; typedef struct TSParser TSParser; typedef struct TSTree TSTree; @@ -60,22 +58,22 @@ typedef enum TSSymbolType typedef struct TSPoint { - uint32_t row; - uint32_t column; + t_u32 row; + t_u32 column; } TSPoint; typedef struct TSRange { - TSPoint start_point; - TSPoint end_point; - uint32_t start_byte; - uint32_t end_byte; + TSPoint start_point; + TSPoint end_point; + t_u32 start_byte; + t_u32 end_byte; } TSRange; typedef struct TSInput { void *payload; - const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read); + const char *(*read)(void *payload, t_u32 byte_index, TSPoint position, t_u32 *bytes_read); TSInputEncoding encoding; } TSInput; @@ -93,17 +91,17 @@ typedef struct TSLogger typedef struct TSInputEdit { - uint32_t start_byte; - uint32_t old_end_byte; - uint32_t new_end_byte; - TSPoint start_point; - TSPoint old_end_point; - TSPoint new_end_point; + t_u32 start_byte; + t_u32 old_end_byte; + t_u32 new_end_byte; + TSPoint start_point; + TSPoint old_end_point; + TSPoint new_end_point; } TSInputEdit; typedef struct TSNode { - uint32_t context[4]; + t_u32 context[4]; const void *id; const TSTree *tree; } TSNode; @@ -112,13 +110,13 @@ typedef struct TSTreeCursor { const void *tree; const void *id; - uint32_t context[3]; + t_u32 context[3]; } TSTreeCursor; typedef struct TSQueryCapture { - TSNode node; - uint32_t index; + TSNode node; + t_u32 index; } TSQueryCapture; typedef enum TSQuantifier @@ -132,9 +130,9 @@ typedef enum TSQuantifier typedef struct TSQueryMatch { - uint32_t id; - uint16_t pattern_index; - uint16_t capture_count; + t_u32 id; + t_u16 pattern_index; + t_u16 capture_count; const TSQueryCapture *captures; } TSQueryMatch; @@ -148,7 +146,7 @@ typedef enum TSQueryPredicateStepType typedef struct TSQueryPredicateStep { TSQueryPredicateStepType type; - uint32_t value_id; + t_u32 value_id; } TSQueryPredicateStep; typedef enum TSQueryError @@ -215,7 +213,7 @@ bool ts_parser_set_language(TSParser *self, const TSLanguage *language); * will not be assigned, and this function will return `false`. On success, * this function returns `true` */ -bool ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, uint32_t count); +bool ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, t_u32 count); /** * Get the ranges of text that the parser will include when parsing. @@ -224,7 +222,7 @@ bool ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, uint32 * or write to it. The length of the array will be written to the given * `count` pointer. */ -const TSRange *ts_parser_included_ranges(const TSParser *self, uint32_t *count); +const TSRange *ts_parser_included_ranges(const TSParser *self, t_u32 *count); /** * Use the parser to parse some source code and create a syntax tree. @@ -277,7 +275,7 @@ TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input); * above. The second two parameters indicate the location of the buffer and its * length in bytes. */ -TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, const char *string, uint32_t length); +TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, const char *string, t_u32 length); /** * Use the parser to parse some source code stored in one contiguous buffer with @@ -285,8 +283,7 @@ TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, const cha * [`ts_parser_parse_string`] method above. The final parameter indicates whether * the text is encoded as UTF8 or UTF16. */ -TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, const char *string, uint32_t length, - TSInputEncoding encoding); +TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, const char *string, t_u32 length, TSInputEncoding encoding); /** * Instruct the parser to start the next parse from the beginning. @@ -306,12 +303,12 @@ void ts_parser_reset(TSParser *self); * If parsing takes longer than this, it will halt early, returning NULL. * See [`ts_parser_parse`] for more information. */ -void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout_micros); +void ts_parser_set_timeout_micros(TSParser *self, t_u64 timeout_micros); /** * Get the duration in microseconds that parsing is allowed to take. */ -uint64_t ts_parser_timeout_micros(const TSParser *self); +t_u64 ts_parser_timeout_micros(const TSParser *self); /** * Set the parser's current cancellation flag pointer. @@ -375,7 +372,7 @@ TSNode ts_tree_root_node(const TSTree *self); * Get the root node of the syntax tree, but with its position * shifted forward by the given offset. */ -TSNode ts_tree_root_node_with_offset(const TSTree *self, uint32_t offset_bytes, TSPoint offset_extent); +TSNode ts_tree_root_node_with_offset(const TSTree *self, t_u32 offset_bytes, TSPoint offset_extent); /** * Get the language that was used to parse the syntax tree. @@ -387,7 +384,7 @@ const TSLanguage *ts_tree_language(const TSTree *self); * * The returned pointer must be freed by the caller. */ -TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length); +TSRange *ts_tree_included_ranges(const TSTree *self, t_u32 *length); /** * Edit the syntax tree to keep it in sync with source code that has been @@ -412,7 +409,7 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit); * for freeing it using `free`. The length of the array will be written to the * given `length` pointer. */ -TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, uint32_t *length); +TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, t_u32 *length); /** * Write a DOT graph describing the syntax tree to the given file. @@ -454,7 +451,7 @@ TSSymbol ts_node_grammar_symbol(TSNode self); /** * Get the node's start byte. */ -uint32_t ts_node_start_byte(TSNode self); +t_u32 ts_node_start_byte(TSNode self); /** * Get the node's start position in terms of rows and columns. @@ -464,7 +461,7 @@ TSPoint ts_node_start_point(TSNode self); /** * Get the node's end byte. */ -uint32_t ts_node_end_byte(TSNode self); +t_u32 ts_node_end_byte(TSNode self); /** * Get the node's end position in terms of rows and columns. @@ -546,43 +543,43 @@ TSNode ts_node_child_containing_descendant(TSNode self, TSNode descendant); * Get the node's child at the given index, where zero represents the first * child. */ -TSNode ts_node_child(TSNode self, uint32_t child_index); +TSNode ts_node_child(TSNode self, t_u32 child_index); /** * Get the field name for node's child at the given index, where zero represents * the first child. Returns NULL, if no field is found. */ -const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index); +const char *ts_node_field_name_for_child(TSNode self, t_u32 child_index); /** * Get the field name for node's child at the given index, where zero represents * the first child. Returns NULL, if no field is found. */ -TSFieldId ts_node_field_id_for_child(TSNode self, uint32_t child_index); +TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index); /** * Get the node's number of children. */ -uint32_t ts_node_child_count(TSNode self); +t_u32 ts_node_child_count(TSNode self); /** * Get the node's *named* child at the given index. * * See also [`ts_node_is_named`]. */ -TSNode ts_node_named_child(TSNode self, uint32_t child_index); +TSNode ts_node_named_child(TSNode self, t_u32 child_index); /** * Get the node's number of *named* children. * * See also [`ts_node_is_named`]. */ -uint32_t ts_node_named_child_count(TSNode self); +t_u32 ts_node_named_child_count(TSNode self); /** * Get the node's child with the given field name. */ -TSNode ts_node_child_by_field_name(TSNode self, const char *name, uint32_t name_length); +TSNode ts_node_child_by_field_name(TSNode self, const char *name, t_u32 name_length); /** * Get the node's child with the given numerical field id. @@ -607,30 +604,30 @@ TSNode ts_node_prev_named_sibling(TSNode self); /** * Get the node's first child that extends beyond the given byte offset. */ -TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte); +TSNode ts_node_first_child_for_byte(TSNode self, t_u32 byte); /** * Get the node's first named child that extends beyond the given byte offset. */ -TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte); +TSNode ts_node_first_named_child_for_byte(TSNode self, t_u32 byte); /** * Get the node's number of descendants, including one for the node itself. */ -uint32_t ts_node_descendant_count(TSNode self); +t_u32 ts_node_descendant_count(TSNode self); /** * Get the smallest node within this node that spans the given range of bytes * or (row, column) positions. */ -TSNode ts_node_descendant_for_byte_range(TSNode self, uint32_t start, uint32_t end); +TSNode ts_node_descendant_for_byte_range(TSNode self, t_u32 start, t_u32 end); TSNode ts_node_descendant_for_point_range(TSNode self, TSPoint start, TSPoint end); /** * Get the smallest named node within this node that spans the given range of * bytes or (row, column) positions. */ -TSNode ts_node_named_descendant_for_byte_range(TSNode self, uint32_t start, uint32_t end); +TSNode ts_node_named_descendant_for_byte_range(TSNode self, t_u32 start, t_u32 end); TSNode ts_node_named_descendant_for_point_range(TSNode self, TSPoint start, TSPoint end); /** @@ -667,12 +664,12 @@ void ts_language_delete(const TSLanguage *self); /** * Get the number of distinct node types in the language. */ -uint32_t ts_language_symbol_count(const TSLanguage *self); +t_u32 ts_language_symbol_count(const TSLanguage *self); /** * Get the number of valid states in this language. */ -uint32_t ts_language_state_count(const TSLanguage *self); +t_u32 ts_language_state_count(const TSLanguage *self); /** * Get a node type string for the given numerical id. @@ -682,12 +679,12 @@ const char *ts_language_symbol_name(const TSLanguage *self, TSSymbol symbol); /** * Get the numerical id for the given node type string. */ -TSSymbol ts_language_symbol_for_name(const TSLanguage *self, const char *string, uint32_t length, bool is_named); +TSSymbol ts_language_symbol_for_name(const TSLanguage *self, const char *string, t_u32 length, bool is_named); /** * Get the number of distinct field names in the language. */ -uint32_t ts_language_field_count(const TSLanguage *self); +t_u32 ts_language_field_count(const TSLanguage *self); /** * Get the field name string for the given numerical id. @@ -697,7 +694,7 @@ const char *ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id); /** * Get the numerical id for the given field name string. */ -TSFieldId ts_language_field_id_for_name(const TSLanguage *self, const char *name, uint32_t name_length); +TSFieldId ts_language_field_id_for_name(const TSLanguage *self, const char *name, t_u32 name_length); /** * Check whether the given node type id belongs to named nodes, anonymous nodes, @@ -714,7 +711,7 @@ TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol); * * See also [`ts_parser_set_language`]. */ -uint32_t ts_language_version(const TSLanguage *self); +t_u32 ts_language_version(const TSLanguage *self); /** * Get the next parse state. Combine this with lookahead iterators to generate diff --git a/parser/src/array.h b/parser/src/array.h index ad1dae5e..3682d542 100644 --- a/parser/src/array.h +++ b/parser/src/array.h @@ -1,9 +1,9 @@ #ifndef TREE_SITTER_ARRAY_H_ #define TREE_SITTER_ARRAY_H_ +#include "me/types.h" #include #include -#include #include #include @@ -12,9 +12,9 @@ #define Array(T) \ struct \ { \ - T *contents; \ - uint32_t size; \ - uint32_t capacity; \ + T *contents; \ + t_u32 size; \ + t_u32 capacity; \ } /// Initialize an array. @@ -27,7 +27,7 @@ } /// Get a pointer to the element at a given `index` in the array. -#define array_get(self, _index) (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) +#define array_get(self, _index) (assert((t_u32)(_index) < (self)->size), &(self)->contents[_index]) /// Get a pointer to the first element in the array. #define array_front(self) array_get(self, 0) @@ -151,7 +151,7 @@ static inline void _array__delete(Array *self) } /// This is not what you're looking for, see `array_erase`. -static inline void _array__erase(Array *self, size_t element_size, uint32_t index) +static inline void _array__erase(Array *self, size_t element_size, t_u32 index) { assert(index < self->size); char *contents = (char *)self->contents; @@ -160,7 +160,7 @@ static inline void _array__erase(Array *self, size_t element_size, uint32_t inde } /// This is not what you're looking for, see `array_reserve`. -static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) +static inline void _array__reserve(Array *self, size_t element_size, t_u32 new_capacity) { if (new_capacity > self->capacity) { @@ -193,12 +193,12 @@ static inline void _array__swap(Array *self, Array *other) } /// This is not what you're looking for, see `array_push` or `array_grow_by`. -static inline void _array__grow(Array *self, uint32_t count, size_t element_size) +static inline void _array__grow(Array *self, t_u32 count, size_t element_size) { - uint32_t new_size = self->size + count; + t_u32 new_size = self->size + count; if (new_size > self->capacity) { - uint32_t new_capacity = self->capacity * 2; + t_u32 new_capacity = self->capacity * 2; if (new_capacity < 8) new_capacity = 8; if (new_capacity < new_size) @@ -208,12 +208,11 @@ static inline void _array__grow(Array *self, uint32_t count, size_t element_size } /// This is not what you're looking for, see `array_splice`. -static inline void _array__splice(Array *self, size_t element_size, uint32_t index, uint32_t old_count, uint32_t new_count, - const void *elements) +static inline void _array__splice(Array *self, size_t element_size, t_u32 index, t_u32 old_count, t_u32 new_count, const void *elements) { - uint32_t new_size = self->size + new_count - old_count; - uint32_t old_end = index + old_count; - uint32_t new_end = index + new_count; + t_u32 new_size = self->size + new_count - old_count; + t_u32 old_end = index + old_count; + t_u32 new_end = index + new_count; assert(old_end <= self->size); _array__reserve(self, element_size, new_size); @@ -244,14 +243,14 @@ static inline void _array__splice(Array *self, size_t element_size, uint32_t ind { \ *(_index) = start; \ *(_exists) = false; \ - uint32_t size = (self)->size - *(_index); \ + t_u32 size = (self)->size - *(_index); \ if (size == 0) \ break; \ int comparison; \ while (size > 1) \ { \ - uint32_t half_size = size / 2; \ - uint32_t mid_index = *(_index) + half_size; \ + t_u32 half_size = size / 2; \ + t_u32 mid_index = *(_index) + half_size; \ comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ if (comparison <= 0) \ *(_index) = mid_index; \ diff --git a/parser/src/create_language.c b/parser/src/create_language.c index b83fadbd..e08b4613 100644 --- a/parser/src/create_language.c +++ b/parser/src/create_language.c @@ -13,33 +13,33 @@ #include "../static/headers/constants.h" #include "../static/headers/symbols.h" #include "./parser.h" +#include "me/types.h" // bool lex_keywords_main(TSLexer *lexer, TSStateId state); // bool lex_normal_main(TSLexer *lexer, TSStateId state); -bool tree_sitter_sh_external_scanner_scan(void *ctx, TSLexer *lexer, const bool *ret); -void *create_external_scanner_states(void); -void *create_field_names(void); -void *create_symbols_names(void); -void *create_field_map_entries(void); -void *create_field_map_slices(void); -void *create_lex_modes(void); -void *create_parse_actions_entries(void); -void *create_primary_state_ids(void); -void *create_alias_sequences(void); -void *create_external_scanner_symbol_map(void); -void *create_non_terminal_alias_map(void); -void *create_unique_symbols_map(void); -void *create_symbols_metadata(void); -void *create_parse_table(void); -void *create_small_parse_table(void); -void *create_small_parse_table_map(void); -bool ts_lex_keywords(TSLexer *lexer, TSStateId state); -bool ts_lex_keywords(TSLexer *lexer, TSStateId state); - -uint32_t tree_sitter_sh_external_scanner_serialize(void *ctx, char *s); -void tree_sitter_sh_external_scanner_deserialize(void *ctx, const char *s, uint32_t val); -void tree_sitter_sh_external_scanner_destroy(void *ctx); -void *tree_sitter_sh_external_scanner_create(void); +bool tree_sitter_sh_external_scanner_scan(void *ctx, TSLexer *lexer, const bool *ret); +void *create_external_scanner_states(void); +void *create_field_names(void); +void *create_symbols_names(void); +void *create_field_map_entries(void); +void *create_field_map_slices(void); +void *create_lex_modes(void); +void *create_parse_actions_entries(void); +void *create_primary_state_ids(void); +void *create_alias_sequences(void); +void *create_external_scanner_symbol_map(void); +void *create_non_terminal_alias_map(void); +void *create_unique_symbols_map(void); +void *create_symbols_metadata(void); +void *create_parse_table(void); +void *create_small_parse_table(void); +void *create_small_parse_table_map(void); +bool ts_lex_keywords(TSLexer *lexer, TSStateId state); +bool ts_lex(TSLexer *lexer, TSStateId state); +t_u32 tree_sitter_sh_external_scanner_serialize(void *ctx, char *s); +void tree_sitter_sh_external_scanner_deserialize(void *ctx, const char *s, t_u32 val); +void tree_sitter_sh_external_scanner_destroy(void *ctx); +void *tree_sitter_sh_external_scanner_create(void); static struct ExternalScannerDefinition init_scanner(void) { diff --git a/parser/src/error_costs.h b/parser/src/error_costs.h deleted file mode 100644 index 2f58a206..00000000 --- a/parser/src/error_costs.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef TREE_SITTER_ERROR_COSTS_H_ -#define TREE_SITTER_ERROR_COSTS_H_ - - - -#endif diff --git a/parser/src/language.c b/parser/src/language.c index 5e70f04d..074735ab 100644 --- a/parser/src/language.c +++ b/parser/src/language.c @@ -1,173 +1,194 @@ #include "./language.h" #include "./api.h" -#include +#include "me/types.h" #include +#include -const TSLanguage *ts_language_copy(const TSLanguage *self) { - return self; +const TSLanguage *ts_language_copy(const TSLanguage *self) +{ + return self; } -void ts_language_delete(const TSLanguage *self) { - (void)(self); +void ts_language_delete(const TSLanguage *self) +{ + (void)(self); } -uint32_t ts_language_symbol_count(const TSLanguage *self) { - return self->symbol_count + self->alias_count; +t_u32 ts_language_symbol_count(const TSLanguage *self) +{ + return self->symbol_count + self->alias_count; } -uint32_t ts_language_state_count(const TSLanguage *self) { - return self->state_count; +t_u32 ts_language_state_count(const TSLanguage *self) +{ + return self->state_count; } -uint32_t ts_language_version(const TSLanguage *self) { - return self->version; +t_u32 ts_language_version(const TSLanguage *self) +{ + return self->version; } -uint32_t ts_language_field_count(const TSLanguage *self) { - return self->field_count; +t_u32 ts_language_field_count(const TSLanguage *self) +{ + return self->field_count; } -void ts_language_table_entry( - const TSLanguage *self, - TSStateId state, - TSSymbol symbol, - TableEntry *result -) { - if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { - result->action_count = 0; - result->is_reusable = false; - result->actions = NULL; - } else { - assert(symbol < self->token_count); - uint32_t action_index = ts_language_lookup(self, state, symbol); - const TSParseActionEntry *entry = &self->parse_actions[action_index]; - result->action_count = entry->entry.count; - result->is_reusable = entry->entry.reusable; - result->actions = (const TSParseAction *)(entry + 1); - } +void ts_language_table_entry(const TSLanguage *self, TSStateId state, TSSymbol symbol, TableEntry *result) +{ + if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) + { + result->action_count = 0; + result->is_reusable = false; + result->actions = NULL; + } + else + { + assert(symbol < self->token_count); + t_u32 action_index = ts_language_lookup(self, state, symbol); + const TSParseActionEntry *entry = &self->parse_actions[action_index]; + result->action_count = entry->entry.count; + result->is_reusable = entry->entry.reusable; + result->actions = (const TSParseAction *)(entry + 1); + } } -TSSymbolMetadata ts_language_symbol_metadata( - const TSLanguage *self, - TSSymbol symbol -) { - if (symbol == ts_builtin_sym_error) { - return (TSSymbolMetadata) {.visible = true, .named = true}; - } else if (symbol == ts_builtin_sym_error_repeat) { - return (TSSymbolMetadata) {.visible = false, .named = false}; - } else { - return self->symbol_metadata[symbol]; - } +TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *self, TSSymbol symbol) +{ + if (symbol == ts_builtin_sym_error) + { + return (TSSymbolMetadata){.visible = true, .named = true}; + } + else if (symbol == ts_builtin_sym_error_repeat) + { + return (TSSymbolMetadata){.visible = false, .named = false}; + } + else + { + return self->symbol_metadata[symbol]; + } } -TSSymbol ts_language_public_symbol( - const TSLanguage *self, - TSSymbol symbol -) { - if (symbol == ts_builtin_sym_error) return symbol; - return self->public_symbol_map[symbol]; +TSSymbol ts_language_public_symbol(const TSLanguage *self, TSSymbol symbol) +{ + if (symbol == ts_builtin_sym_error) + return symbol; + return self->public_symbol_map[symbol]; } -TSStateId ts_language_next_state( - const TSLanguage *self, - TSStateId state, - TSSymbol symbol -) { - if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { - return 0; - } else if (symbol < self->token_count) { - uint32_t count; - const TSParseAction *actions = ts_language_actions(self, state, symbol, &count); - if (count > 0) { - TSParseAction action = actions[count - 1]; - if (action.type == TSParseActionTypeShift) { - return action.shift.extra ? state : action.shift.state; - } - } - return 0; - } else { - return ts_language_lookup(self, state, symbol); - } +TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol) +{ + if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) + { + return 0; + } + else if (symbol < self->token_count) + { + t_u32 count; + const TSParseAction *actions = ts_language_actions(self, state, symbol, &count); + if (count > 0) + { + TSParseAction action = actions[count - 1]; + if (action.type == TSParseActionTypeShift) + { + return action.shift.extra ? state : action.shift.state; + } + } + return 0; + } + else + { + return ts_language_lookup(self, state, symbol); + } } -const char *ts_language_symbol_name( - const TSLanguage *self, - TSSymbol symbol -) { - if (symbol == ts_builtin_sym_error) { - return "ERROR"; - } else if (symbol == ts_builtin_sym_error_repeat) { - return "_ERROR"; - } else if (symbol < ts_language_symbol_count(self)) { - return self->symbol_names[symbol]; - } else { - return NULL; - } +const char *ts_language_symbol_name(const TSLanguage *self, TSSymbol symbol) +{ + if (symbol == ts_builtin_sym_error) + { + return "ERROR"; + } + else if (symbol == ts_builtin_sym_error_repeat) + { + return "_ERROR"; + } + else if (symbol < ts_language_symbol_count(self)) + { + return self->symbol_names[symbol]; + } + else + { + return NULL; + } } -TSSymbol ts_language_symbol_for_name( - const TSLanguage *self, - const char *string, - uint32_t length, - bool is_named -) { - if (!strncmp(string, "ERROR", length)) return ts_builtin_sym_error; - uint16_t count = (uint16_t)ts_language_symbol_count(self); - for (TSSymbol i = 0; i < count; i++) { - TSSymbolMetadata metadata = ts_language_symbol_metadata(self, i); - if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) continue; - const char *symbol_name = self->symbol_names[i]; - if (!strncmp(symbol_name, string, length) && !symbol_name[length]) { - return self->public_symbol_map[i]; - } - } - return 0; +TSSymbol ts_language_symbol_for_name(const TSLanguage *self, const char *string, t_u32 length, bool is_named) +{ + if (!strncmp(string, "ERROR", length)) + return ts_builtin_sym_error; + t_u16 count = (t_u16)ts_language_symbol_count(self); + for (TSSymbol i = 0; i < count; i++) + { + TSSymbolMetadata metadata = ts_language_symbol_metadata(self, i); + if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) + continue; + const char *symbol_name = self->symbol_names[i]; + if (!strncmp(symbol_name, string, length) && !symbol_name[length]) + { + return self->public_symbol_map[i]; + } + } + return 0; } -TSSymbolType ts_language_symbol_type( - const TSLanguage *self, - TSSymbol symbol -) { - TSSymbolMetadata metadata = ts_language_symbol_metadata(self, symbol); - if (metadata.named && metadata.visible) { - return TSSymbolTypeRegular; - } else if (metadata.visible) { - return TSSymbolTypeAnonymous; - } else { - return TSSymbolTypeAuxiliary; - } +TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol) +{ + TSSymbolMetadata metadata = ts_language_symbol_metadata(self, symbol); + if (metadata.named && metadata.visible) + { + return TSSymbolTypeRegular; + } + else if (metadata.visible) + { + return TSSymbolTypeAnonymous; + } + else + { + return TSSymbolTypeAuxiliary; + } } -const char *ts_language_field_name_for_id( - const TSLanguage *self, - TSFieldId id -) { - uint32_t count = ts_language_field_count(self); - if (count && id <= count) { - return self->field_names[id]; - } else { - return NULL; - } +const char *ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id) +{ + t_u32 count = ts_language_field_count(self); + if (count && id <= count) + { + return self->field_names[id]; + } + else + { + return NULL; + } } -TSFieldId ts_language_field_id_for_name( - const TSLanguage *self, - const char *name, - uint32_t name_length -) { - uint16_t count = (uint16_t)ts_language_field_count(self); - for (TSSymbol i = 1; i < count + 1; i++) { - switch (strncmp(name, self->field_names[i], name_length)) { - case 0: - if (self->field_names[i][name_length] == 0) return i; - break; - case -1: - return 0; - default: - break; - } - } - return 0; +TSFieldId ts_language_field_id_for_name(const TSLanguage *self, const char *name, t_u32 name_length) +{ + t_u16 count = (t_u16)ts_language_field_count(self); + for (TSSymbol i = 1; i < count + 1; i++) + { + switch (strncmp(name, self->field_names[i], name_length)) + { + case 0: + if (self->field_names[i][name_length] == 0) + return i; + break; + case -1: + return 0; + default: + break; + } + } + return 0; } // TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state) { diff --git a/parser/src/language.h b/parser/src/language.h index 876524ae..00076a4b 100644 --- a/parser/src/language.h +++ b/parser/src/language.h @@ -2,6 +2,7 @@ #define TREE_SITTER_LANGUAGE_H_ #include "./parser.h" +#include "me/types.h" #define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1) @@ -11,7 +12,7 @@ typedef struct TableEntry { const TSParseAction *actions; - uint32_t action_count; + t_u32 action_count; bool is_reusable; } TableEntry; @@ -28,7 +29,7 @@ static inline bool ts_language_is_symbol_external(const TSLanguage *self, TSSymb return 0 < symbol && symbol < self->external_token_count + 1; } -static inline const TSParseAction *ts_language_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol, uint32_t *count) +static inline const TSParseAction *ts_language_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol, t_u32 *count) { TableEntry entry; ts_language_table_entry(self, state, symbol, &entry); @@ -50,17 +51,17 @@ static inline bool ts_language_has_reduce_action(const TSLanguage *self, TSState // For 'large' parse states, this is a direct lookup. For 'small' parse // states, this requires searching through the symbol groups to find // the given symbol. -static inline uint16_t ts_language_lookup(const TSLanguage *self, TSStateId state, TSSymbol symbol) +static inline t_u16 ts_language_lookup(const TSLanguage *self, TSStateId state, TSSymbol symbol) { if (state >= self->large_state_count) { - uint32_t index = self->small_parse_table_map[state - self->large_state_count]; - const uint16_t *data = &self->small_parse_table[index]; - uint16_t group_count = *(data++); + t_u32 index = self->small_parse_table_map[state - self->large_state_count]; + const t_u16 *data = &self->small_parse_table[index]; + t_u16 group_count = *(data++); for (unsigned i = 0; i < group_count; i++) { - uint16_t section_value = *(data++); - uint16_t symbol_count = *(data++); + t_u16 section_value = *(data++); + t_u16 symbol_count = *(data++); for (unsigned j = 0; j < symbol_count; j++) { if (*(data++) == symbol) @@ -92,17 +93,17 @@ static inline const bool *ts_language_enabled_external_tokens(const TSLanguage * } } -static inline const TSSymbol *ts_language_alias_sequence(const TSLanguage *self, uint32_t production_id) +static inline const TSSymbol *ts_language_alias_sequence(const TSLanguage *self, t_u32 production_id) { return production_id ? &self->alias_sequences[production_id * self->max_alias_sequence_length] : NULL; } -static inline TSSymbol ts_language_alias_at(const TSLanguage *self, uint32_t production_id, uint32_t child_index) +static inline TSSymbol ts_language_alias_at(const TSLanguage *self, t_u32 production_id, t_u32 child_index) { return production_id ? self->alias_sequences[production_id * self->max_alias_sequence_length + child_index] : 0; } -static inline void ts_language_field_map(const TSLanguage *self, uint32_t production_id, const TSFieldMapEntry **start, +static inline void ts_language_field_map(const TSLanguage *self, t_u32 production_id, const TSFieldMapEntry **start, const TSFieldMapEntry **end) { if (self->field_count == 0) @@ -129,7 +130,7 @@ static inline void ts_language_aliases_for_symbol(const TSLanguage *self, TSSymb TSSymbol symbol = self->alias_map[idx++]; if (symbol == 0 || symbol > original_symbol) break; - uint16_t count = self->alias_map[idx++]; + t_u16 count = self->alias_map[idx++]; if (symbol == original_symbol) { *start = &self->alias_map[idx]; @@ -140,5 +141,4 @@ static inline void ts_language_aliases_for_symbol(const TSLanguage *self, TSSymb } } - #endif // TREE_SITTER_LANGUAGE_H_ diff --git a/parser/src/length.h b/parser/src/length.h index 27be3a33..da2fb3c4 100644 --- a/parser/src/length.h +++ b/parser/src/length.h @@ -1,51 +1,61 @@ #ifndef TREE_SITTER_LENGTH_H_ #define TREE_SITTER_LENGTH_H_ -#include -#include "./point.h" #include "./api.h" +#include "./point.h" +#include "me/types.h" -typedef struct Length { - uint32_t bytes; - TSPoint extent; +typedef struct Length +{ + t_u32 bytes; + TSPoint extent; } Length; static const Length LENGTH_UNDEFINED = {0, {0, 1}}; static const Length LENGTH_MAX = {UINT32_MAX, {UINT32_MAX, UINT32_MAX}}; -static inline bool length_is_undefined(Length length) { - return length.bytes == 0 && length.extent.column != 0; +static inline bool length_is_undefined(Length length) +{ + return length.bytes == 0 && length.extent.column != 0; } -static inline Length length_min(Length len1, Length len2) { - return (len1.bytes < len2.bytes) ? len1 : len2; +static inline Length length_min(Length len1, Length len2) +{ + return (len1.bytes < len2.bytes) ? len1 : len2; } -static inline Length length_add(Length len1, Length len2) { - Length result; - result.bytes = len1.bytes + len2.bytes; - result.extent = point_add(len1.extent, len2.extent); - return result; +static inline Length length_add(Length len1, Length len2) +{ + Length result; + result.bytes = len1.bytes + len2.bytes; + result.extent = point_add(len1.extent, len2.extent); + return result; } -static inline Length length_sub(Length len1, Length len2) { - Length result; - result.bytes = len1.bytes - len2.bytes; - result.extent = point_sub(len1.extent, len2.extent); - return result; +static inline Length length_sub(Length len1, Length len2) +{ + Length result; + result.bytes = len1.bytes - len2.bytes; + result.extent = point_sub(len1.extent, len2.extent); + return result; } -static inline Length length_zero(void) { - Length result = {0, {0, 0}}; - return result; +static inline Length length_zero(void) +{ + Length result = {0, {0, 0}}; + return result; } -static inline Length length_saturating_sub(Length len1, Length len2) { - if (len1.bytes > len2.bytes) { - return length_sub(len1, len2); - } else { - return length_zero(); - } +static inline Length length_saturating_sub(Length len1, Length len2) +{ + if (len1.bytes > len2.bytes) + { + return length_sub(len1, len2); + } + else + { + return length_zero(); + } } #endif diff --git a/parser/src/lexer.c b/parser/src/lexer.c index 0c54451a..03e5f4d6 100644 --- a/parser/src/lexer.c +++ b/parser/src/lexer.c @@ -1,419 +1,442 @@ -#include #include "./lexer.h" -#include "./subtree.h" #include "./length.h" #include "./unicode.h" +#include "me/mem/mem.h" +#include "me/types.h" +#include -#define LOG(message, character) \ - if (self->logger.log) { \ - snprintf( \ - self->debug_buffer, \ - TREE_SITTER_SERIALIZATION_BUFFER_SIZE, \ - 32 <= character && character < 127 ? \ - message " character:'%c'" : \ - message " character:%d", \ - character \ - ); \ - self->logger.log( \ - self->logger.payload, \ - TSLogTypeLex, \ - self->debug_buffer \ - ); \ - } +#define LOG(...) -static const int32_t BYTE_ORDER_MARK = 0xFEFF; +static const t_i32 BYTE_ORDER_MARK = 0xFEFF; -static const TSRange DEFAULT_RANGE = { - .start_point = { - .row = 0, - .column = 0, - }, - .end_point = { - .row = UINT32_MAX, - .column = UINT32_MAX, - }, - .start_byte = 0, - .end_byte = UINT32_MAX -}; +static const TSRange DEFAULT_RANGE = {.start_point = + { + .row = 0, + .column = 0, + }, + .end_point = + { + .row = UINT32_MAX, + .column = UINT32_MAX, + }, + .start_byte = 0, + .end_byte = UINT32_MAX}; // Check if the lexer has reached EOF. This state is stored // by setting the lexer's `current_included_range_index` such that // it has consumed all of its available ranges. -static bool ts_lexer__eof(const TSLexer *_self) { - Lexer *self = (Lexer *)_self; - return self->current_included_range_index == self->included_range_count; +static bool ts_lexer__eof(const TSLexer *_self) +{ + Lexer *self = (Lexer *)_self; + return self->current_included_range_index == self->included_range_count; } // Clear the currently stored chunk of source code, because the lexer's // position has changed. -static void ts_lexer__clear_chunk(Lexer *self) { - self->chunk = NULL; - self->chunk_size = 0; - self->chunk_start = 0; +static void ts_lexer__clear_chunk(Lexer *self) +{ + self->chunk = NULL; + self->chunk_size = 0; + self->chunk_start = 0; } // Call the lexer's input callback to obtain a new chunk of source code // for the current position. -static void ts_lexer__get_chunk(Lexer *self) { - self->chunk_start = self->current_position.bytes; - self->chunk = self->input.read( - self->input.payload, - self->current_position.bytes, - self->current_position.extent, - &self->chunk_size - ); - if (!self->chunk_size) { - self->current_included_range_index = self->included_range_count; - self->chunk = NULL; - } +static void ts_lexer__get_chunk(Lexer *self) +{ + self->chunk_start = self->current_position.bytes; + self->chunk = self->input.read(self->input.payload, self->current_position.bytes, self->current_position.extent, &self->chunk_size); + if (!self->chunk_size) + { + self->current_included_range_index = self->included_range_count; + self->chunk = NULL; + } } // Decode the next unicode character in the current chunk of source code. // This assumes that the lexer has already retrieved a chunk of source // code that spans the current position. -static void ts_lexer__get_lookahead(Lexer *self) { - uint32_t position_in_chunk = self->current_position.bytes - self->chunk_start; - uint32_t size = self->chunk_size - position_in_chunk; +static void ts_lexer__get_lookahead(Lexer *self) +{ + t_u32 position_in_chunk = self->current_position.bytes - self->chunk_start; + t_u32 size = self->chunk_size - position_in_chunk; - if (size == 0) { - self->lookahead_size = 1; - self->data.lookahead = '\0'; - return; - } + if (size == 0) + { + self->lookahead_size = 1; + self->data.lookahead = '\0'; + return; + } - const uint8_t *chunk = (const uint8_t *)self->chunk + position_in_chunk; - UnicodeDecodeFunction decode = self->input.encoding == TSInputEncodingUTF8 - ? ts_decode_utf8 - : ts_decode_utf16; + const t_u8 *chunk = (const t_u8 *)self->chunk + position_in_chunk; + UnicodeDecodeFunction decode = self->input.encoding == TSInputEncodingUTF8 ? ts_decode_utf8 : ts_decode_utf16; - self->lookahead_size = decode(chunk, size, &self->data.lookahead); + self->lookahead_size = decode(chunk, size, &self->data.lookahead); - // If this chunk ended in the middle of a multi-byte character, - // try again with a fresh chunk. - if (self->data.lookahead == TS_DECODE_ERROR && size < 4) { - ts_lexer__get_chunk(self); - chunk = (const uint8_t *)self->chunk; - size = self->chunk_size; - self->lookahead_size = decode(chunk, size, &self->data.lookahead); - } + // If this chunk ended in the middle of a multi-byte character, + // try again with a fresh chunk. + if (self->data.lookahead == TS_DECODE_ERROR && size < 4) + { + ts_lexer__get_chunk(self); + chunk = (const t_u8 *)self->chunk; + size = self->chunk_size; + self->lookahead_size = decode(chunk, size, &self->data.lookahead); + } - if (self->data.lookahead == TS_DECODE_ERROR) { - self->lookahead_size = 1; - } + if (self->data.lookahead == TS_DECODE_ERROR) + { + self->lookahead_size = 1; + } } -static void ts_lexer_goto(Lexer *self, Length position) { - self->current_position = position; +static void ts_lexer_goto(Lexer *self, Length position) +{ + self->current_position = position; - // Move to the first valid position at or after the given position. - bool found_included_range = false; - for (unsigned i = 0; i < self->included_range_count; i++) { - TSRange *included_range = &self->included_ranges[i]; - if ( - included_range->end_byte > self->current_position.bytes && - included_range->end_byte > included_range->start_byte - ) { - if (included_range->start_byte >= self->current_position.bytes) { - self->current_position = (Length) { - .bytes = included_range->start_byte, - .extent = included_range->start_point, - }; - } + // Move to the first valid position at or after the given position. + bool found_included_range = false; + for (unsigned i = 0; i < self->included_range_count; i++) + { + TSRange *included_range = &self->included_ranges[i]; + if (included_range->end_byte > self->current_position.bytes && included_range->end_byte > included_range->start_byte) + { + if (included_range->start_byte >= self->current_position.bytes) + { + self->current_position = (Length){ + .bytes = included_range->start_byte, + .extent = included_range->start_point, + }; + } - self->current_included_range_index = i; - found_included_range = true; - break; - } - } + self->current_included_range_index = i; + found_included_range = true; + break; + } + } - if (found_included_range) { - // If the current position is outside of the current chunk of text, - // then clear out the current chunk of text. - if (self->chunk && ( - self->current_position.bytes < self->chunk_start || - self->current_position.bytes >= self->chunk_start + self->chunk_size - )) { - ts_lexer__clear_chunk(self); - } + if (found_included_range) + { + // If the current position is outside of the current chunk of text, + // then clear out the current chunk of text. + if (self->chunk && + (self->current_position.bytes < self->chunk_start || self->current_position.bytes >= self->chunk_start + self->chunk_size)) + { + ts_lexer__clear_chunk(self); + } - self->lookahead_size = 0; - self->data.lookahead = '\0'; - } + self->lookahead_size = 0; + self->data.lookahead = '\0'; + } - // If the given position is beyond any of included ranges, move to the EOF - // state - past the end of the included ranges. - else { - self->current_included_range_index = self->included_range_count; - TSRange *last_included_range = &self->included_ranges[self->included_range_count - 1]; - self->current_position = (Length) { - .bytes = last_included_range->end_byte, - .extent = last_included_range->end_point, - }; - ts_lexer__clear_chunk(self); - self->lookahead_size = 1; - self->data.lookahead = '\0'; - } + // If the given position is beyond any of included ranges, move to the EOF + // state - past the end of the included ranges. + else + { + self->current_included_range_index = self->included_range_count; + TSRange *last_included_range = &self->included_ranges[self->included_range_count - 1]; + self->current_position = (Length){ + .bytes = last_included_range->end_byte, + .extent = last_included_range->end_point, + }; + ts_lexer__clear_chunk(self); + self->lookahead_size = 1; + self->data.lookahead = '\0'; + } } // Intended to be called only from functions that control logging. -static void ts_lexer__do_advance(Lexer *self, bool skip) { - if (self->lookahead_size) { - self->current_position.bytes += self->lookahead_size; - if (self->data.lookahead == '\n') { - self->current_position.extent.row++; - self->current_position.extent.column = 0; - } else { - self->current_position.extent.column += self->lookahead_size; - } - } +static void ts_lexer__do_advance(Lexer *self, bool skip) +{ + if (self->lookahead_size) + { + self->current_position.bytes += self->lookahead_size; + if (self->data.lookahead == '\n') + { + self->current_position.extent.row++; + self->current_position.extent.column = 0; + } + else + { + self->current_position.extent.column += self->lookahead_size; + } + } - const TSRange *current_range = &self->included_ranges[self->current_included_range_index]; - while ( - self->current_position.bytes >= current_range->end_byte || - current_range->end_byte == current_range->start_byte - ) { - if (self->current_included_range_index < self->included_range_count) { - self->current_included_range_index++; - } - if (self->current_included_range_index < self->included_range_count) { - current_range++; - self->current_position = (Length) { - current_range->start_byte, - current_range->start_point, - }; - } else { - current_range = NULL; - break; - } - } + const TSRange *current_range = &self->included_ranges[self->current_included_range_index]; + while (self->current_position.bytes >= current_range->end_byte || current_range->end_byte == current_range->start_byte) + { + if (self->current_included_range_index < self->included_range_count) + { + self->current_included_range_index++; + } + if (self->current_included_range_index < self->included_range_count) + { + current_range++; + self->current_position = (Length){ + current_range->start_byte, + current_range->start_point, + }; + } + else + { + current_range = NULL; + break; + } + } - if (skip) self->token_start_position = self->current_position; + if (skip) + self->token_start_position = self->current_position; - if (current_range) { - if ( - self->current_position.bytes < self->chunk_start || - self->current_position.bytes >= self->chunk_start + self->chunk_size - ) { - ts_lexer__get_chunk(self); - } - ts_lexer__get_lookahead(self); - } else { - ts_lexer__clear_chunk(self); - self->data.lookahead = '\0'; - self->lookahead_size = 1; - } + if (current_range) + { + if (self->current_position.bytes < self->chunk_start || self->current_position.bytes >= self->chunk_start + self->chunk_size) + { + ts_lexer__get_chunk(self); + } + ts_lexer__get_lookahead(self); + } + else + { + ts_lexer__clear_chunk(self); + self->data.lookahead = '\0'; + self->lookahead_size = 1; + } } // Advance to the next character in the source code, retrieving a new // chunk of source code if needed. -static void ts_lexer__advance(TSLexer *_self, bool skip) { - Lexer *self = (Lexer *)_self; - if (!self->chunk) return; +static void ts_lexer__advance(TSLexer *_self, bool skip) +{ + Lexer *self = (Lexer *)_self; + if (!self->chunk) + return; - if (skip) { - LOG("skip", self->data.lookahead) - } else { - LOG("consume", self->data.lookahead) - } + if (skip) + { + LOG("skip", self->data.lookahead) + } + else + { + LOG("consume", self->data.lookahead) + } - ts_lexer__do_advance(self, skip); + ts_lexer__do_advance(self, skip); } // Mark that a token match has completed. This can be called multiple // times if a longer match is found later. -static void ts_lexer__mark_end(TSLexer *_self) { - Lexer *self = (Lexer *)_self; - if (!ts_lexer__eof(&self->data)) { - // If the lexer is right at the beginning of included range, - // then the token should be considered to end at the *end* of the - // previous included range, rather than here. - TSRange *current_included_range = &self->included_ranges[ - self->current_included_range_index - ]; - if ( - self->current_included_range_index > 0 && - self->current_position.bytes == current_included_range->start_byte - ) { - TSRange *previous_included_range = current_included_range - 1; - self->token_end_position = (Length) { - previous_included_range->end_byte, - previous_included_range->end_point, - }; - return; - } - } - self->token_end_position = self->current_position; +static void ts_lexer__mark_end(TSLexer *_self) +{ + Lexer *self = (Lexer *)_self; + if (!ts_lexer__eof(&self->data)) + { + // If the lexer is right at the beginning of included range, + // then the token should be considered to end at the *end* of the + // previous included range, rather than here. + TSRange *current_included_range = &self->included_ranges[self->current_included_range_index]; + if (self->current_included_range_index > 0 && self->current_position.bytes == current_included_range->start_byte) + { + TSRange *previous_included_range = current_included_range - 1; + self->token_end_position = (Length){ + previous_included_range->end_byte, + previous_included_range->end_point, + }; + return; + } + } + self->token_end_position = self->current_position; } -static uint32_t ts_lexer__get_column(TSLexer *_self) { - Lexer *self = (Lexer *)_self; +static t_u32 ts_lexer__get_column(TSLexer *_self) +{ + Lexer *self = (Lexer *)_self; - uint32_t goal_byte = self->current_position.bytes; + t_u32 goal_byte = self->current_position.bytes; - self->did_get_column = true; - self->current_position.bytes -= self->current_position.extent.column; - self->current_position.extent.column = 0; + self->did_get_column = true; + self->current_position.bytes -= self->current_position.extent.column; + self->current_position.extent.column = 0; - if (self->current_position.bytes < self->chunk_start) { - ts_lexer__get_chunk(self); - } + if (self->current_position.bytes < self->chunk_start) + { + ts_lexer__get_chunk(self); + } - uint32_t result = 0; - if (!ts_lexer__eof(_self)) { - ts_lexer__get_lookahead(self); - while (self->current_position.bytes < goal_byte && self->chunk) { - result++; - ts_lexer__do_advance(self, false); - if (ts_lexer__eof(_self)) break; - } - } + t_u32 result = 0; + if (!ts_lexer__eof(_self)) + { + ts_lexer__get_lookahead(self); + while (self->current_position.bytes < goal_byte && self->chunk) + { + result++; + ts_lexer__do_advance(self, false); + if (ts_lexer__eof(_self)) + break; + } + } - return result; + return result; } // Is the lexer at a boundary between two disjoint included ranges of // source code? This is exposed as an API because some languages' external // scanners need to perform custom actions at these boundaries. -static bool ts_lexer__is_at_included_range_start(const TSLexer *_self) { - const Lexer *self = (const Lexer *)_self; - if (self->current_included_range_index < self->included_range_count) { - TSRange *current_range = &self->included_ranges[self->current_included_range_index]; - return self->current_position.bytes == current_range->start_byte; - } else { - return false; - } +static bool ts_lexer__is_at_included_range_start(const TSLexer *_self) +{ + const Lexer *self = (const Lexer *)_self; + if (self->current_included_range_index < self->included_range_count) + { + TSRange *current_range = &self->included_ranges[self->current_included_range_index]; + return self->current_position.bytes == current_range->start_byte; + } + else + { + return false; + } } -void ts_lexer_init(Lexer *self) { - *self = (Lexer) { - .data = { - // The lexer's methods are stored as struct fields so that generated - // parsers can call them without needing to be linked against this - // library. - .advance = ts_lexer__advance, - .mark_end = ts_lexer__mark_end, - .get_column = ts_lexer__get_column, - .is_at_included_range_start = ts_lexer__is_at_included_range_start, - .eof = ts_lexer__eof, - .lookahead = 0, - .result_symbol = 0, - }, - .chunk = NULL, - .chunk_size = 0, - .chunk_start = 0, - .current_position = {0, {0, 0}}, - .logger = { - .payload = NULL, - .log = NULL - }, - .included_ranges = NULL, - .included_range_count = 0, - .current_included_range_index = 0, - }; - ts_lexer_set_included_ranges(self, NULL, 0); +void ts_lexer_init(Lexer *self) +{ + *self = (Lexer){ + .data = + { + // The lexer's methods are stored as struct fields so that generated + // parsers can call them without needing to be linked against this + // library. + .advance = ts_lexer__advance, + .mark_end = ts_lexer__mark_end, + .get_column = ts_lexer__get_column, + .is_at_included_range_start = ts_lexer__is_at_included_range_start, + .eof = ts_lexer__eof, + .lookahead = 0, + .result_symbol = 0, + }, + .chunk = NULL, + .chunk_size = 0, + .chunk_start = 0, + .current_position = {0, {0, 0}}, + .logger = {.payload = NULL, .log = NULL}, + .included_ranges = NULL, + .included_range_count = 0, + .current_included_range_index = 0, + }; + ts_lexer_set_included_ranges(self, NULL, 0); } -void ts_lexer_delete(Lexer *self) { - mem_free(self->included_ranges); +void ts_lexer_delete(Lexer *self) +{ + mem_free(self->included_ranges); } -void ts_lexer_set_input(Lexer *self, TSInput input) { - self->input = input; - ts_lexer__clear_chunk(self); - ts_lexer_goto(self, self->current_position); +void ts_lexer_set_input(Lexer *self, TSInput input) +{ + self->input = input; + ts_lexer__clear_chunk(self); + ts_lexer_goto(self, self->current_position); } // Move the lexer to the given position. This doesn't do any work // if the parser is already at the given position. -void ts_lexer_reset(Lexer *self, Length position) { - if (position.bytes != self->current_position.bytes) { - ts_lexer_goto(self, position); - } +void ts_lexer_reset(Lexer *self, Length position) +{ + if (position.bytes != self->current_position.bytes) + { + ts_lexer_goto(self, position); + } } -void ts_lexer_start(Lexer *self) { - self->token_start_position = self->current_position; - self->token_end_position = LENGTH_UNDEFINED; - self->data.result_symbol = 0; - self->did_get_column = false; - if (!ts_lexer__eof(&self->data)) { - if (!self->chunk_size) ts_lexer__get_chunk(self); - if (!self->lookahead_size) ts_lexer__get_lookahead(self); - if ( - self->current_position.bytes == 0 && - self->data.lookahead == BYTE_ORDER_MARK - ) ts_lexer__advance(&self->data, true); - } +void ts_lexer_start(Lexer *self) +{ + self->token_start_position = self->current_position; + self->token_end_position = LENGTH_UNDEFINED; + self->data.result_symbol = 0; + self->did_get_column = false; + if (!ts_lexer__eof(&self->data)) + { + if (!self->chunk_size) + ts_lexer__get_chunk(self); + if (!self->lookahead_size) + ts_lexer__get_lookahead(self); + if (self->current_position.bytes == 0 && self->data.lookahead == BYTE_ORDER_MARK) + ts_lexer__advance(&self->data, true); + } } -void ts_lexer_finish(Lexer *self, uint32_t *lookahead_end_byte) { - if (length_is_undefined(self->token_end_position)) { - ts_lexer__mark_end(&self->data); - } +void ts_lexer_finish(Lexer *self, t_u32 *lookahead_end_byte) +{ + if (length_is_undefined(self->token_end_position)) + { + ts_lexer__mark_end(&self->data); + } - // If the token ended at an included range boundary, then its end position - // will have been reset to the end of the preceding range. Reset the start - // position to match. - if (self->token_end_position.bytes < self->token_start_position.bytes) { - self->token_start_position = self->token_end_position; - } + // If the token ended at an included range boundary, then its end position + // will have been reset to the end of the preceding range. Reset the start + // position to match. + if (self->token_end_position.bytes < self->token_start_position.bytes) + { + self->token_start_position = self->token_end_position; + } - uint32_t current_lookahead_end_byte = self->current_position.bytes + 1; + t_u32 current_lookahead_end_byte = self->current_position.bytes + 1; - // In order to determine that a byte sequence is invalid UTF8 or UTF16, - // the character decoding algorithm may have looked at the following byte. - // Therefore, the next byte *after* the current (invalid) character - // affects the interpretation of the current character. - if (self->data.lookahead == TS_DECODE_ERROR) { - current_lookahead_end_byte += 4; // the maximum number of bytes read to identify an invalid code point - } + // In order to determine that a byte sequence is invalid UTF8 or UTF16, + // the character decoding algorithm may have looked at the following byte. + // Therefore, the next byte *after* the current (invalid) character + // affects the interpretation of the current character. + if (self->data.lookahead == TS_DECODE_ERROR) + { + current_lookahead_end_byte += 4; // the maximum number of bytes read to identify an invalid code point + } - if (current_lookahead_end_byte > *lookahead_end_byte) { - *lookahead_end_byte = current_lookahead_end_byte; - } + if (current_lookahead_end_byte > *lookahead_end_byte) + { + *lookahead_end_byte = current_lookahead_end_byte; + } } -void ts_lexer_advance_to_end(Lexer *self) { - while (self->chunk) { - ts_lexer__advance(&self->data, false); - } +void ts_lexer_advance_to_end(Lexer *self) +{ + while (self->chunk) + { + ts_lexer__advance(&self->data, false); + } } -void ts_lexer_mark_end(Lexer *self) { - ts_lexer__mark_end(&self->data); +void ts_lexer_mark_end(Lexer *self) +{ + ts_lexer__mark_end(&self->data); } -bool ts_lexer_set_included_ranges( - Lexer *self, - const TSRange *ranges, - uint32_t count -) { - if (count == 0 || !ranges) { - ranges = &DEFAULT_RANGE; - count = 1; - } else { - uint32_t previous_byte = 0; - for (unsigned i = 0; i < count; i++) { - const TSRange *range = &ranges[i]; - if ( - range->start_byte < previous_byte || - range->end_byte < range->start_byte - ) return false; - previous_byte = range->end_byte; - } - } +bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, t_u32 count) +{ + if (count == 0 || !ranges) + { + ranges = &DEFAULT_RANGE; + count = 1; + } + else + { + t_u32 previous_byte = 0; + for (unsigned i = 0; i < count; i++) + { + const TSRange *range = &ranges[i]; + if (range->start_byte < previous_byte || range->end_byte < range->start_byte) + return false; + previous_byte = range->end_byte; + } + } - size_t size = count * sizeof(TSRange); - self->included_ranges = mem_realloc(self->included_ranges, size); - memcpy(self->included_ranges, ranges, size); - self->included_range_count = count; - ts_lexer_goto(self, self->current_position); - return true; + size_t size = count * sizeof(TSRange); + self->included_ranges = mem_realloc(self->included_ranges, size); + memcpy(self->included_ranges, ranges, size); + self->included_range_count = count; + ts_lexer_goto(self, self->current_position); + return true; } -TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count) { - *count = self->included_range_count; - return self->included_ranges; +TSRange *ts_lexer_included_ranges(const Lexer *self, t_u32 *count) +{ + *count = self->included_range_count; + return self->included_ranges; } #undef LOG diff --git a/parser/src/lexer.h b/parser/src/lexer.h index 4c5fcc9a..04acf8a8 100644 --- a/parser/src/lexer.h +++ b/parser/src/lexer.h @@ -1,43 +1,42 @@ #ifndef TREE_SITTER_LEXER_H_ #define TREE_SITTER_LEXER_H_ - +#include "./api.h" #include "./length.h" -#include "./subtree.h" -#include "api.h" #include "./parser.h" +#include "me/types.h" -typedef struct Lexer{ - TSLexer data; - Length current_position; - Length token_start_position; - Length token_end_position; +typedef struct Lexer +{ + TSLexer data; + Length current_position; + Length token_start_position; + Length token_end_position; - TSRange *included_ranges; - const char *chunk; - TSInput input; - TSLogger logger; + TSRange *included_ranges; + const char *chunk; + TSInput input; + TSLogger logger; - uint32_t included_range_count; - uint32_t current_included_range_index; - uint32_t chunk_start; - uint32_t chunk_size; - uint32_t lookahead_size; - bool did_get_column; + t_u32 included_range_count; + t_u32 current_included_range_index; + t_u32 chunk_start; + t_u32 chunk_size; + t_u32 lookahead_size; + bool did_get_column; - char debug_buffer[TREE_SITTER_SERIALIZATION_BUFFER_SIZE]; + char debug_buffer[TREE_SITTER_SERIALIZATION_BUFFER_SIZE]; } Lexer; -void ts_lexer_init(Lexer *); -void ts_lexer_delete(Lexer *); -void ts_lexer_set_input(Lexer *, TSInput); -void ts_lexer_reset(Lexer *, Length); -void ts_lexer_start(Lexer *); -void ts_lexer_finish(Lexer *, uint32_t *); -void ts_lexer_advance_to_end(Lexer *); -void ts_lexer_mark_end(Lexer *); -bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, uint32_t count); -TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count); +void ts_lexer_init(Lexer *); +void ts_lexer_delete(Lexer *); +void ts_lexer_set_input(Lexer *, TSInput); +void ts_lexer_reset(Lexer *, Length); +void ts_lexer_start(Lexer *); +void ts_lexer_finish(Lexer *, t_u32 *); +void ts_lexer_advance_to_end(Lexer *); +void ts_lexer_mark_end(Lexer *); +bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, t_u32 count); +TSRange *ts_lexer_included_ranges(const Lexer *self, t_u32 *count); - -#endif // TREE_SITTER_LEXER_H_ +#endif // TREE_SITTER_LEXER_H_ diff --git a/parser/src/node.c b/parser/src/node.c index 94e92c97..4af56084 100644 --- a/parser/src/node.c +++ b/parser/src/node.c @@ -1,16 +1,16 @@ +#include "./api.h" #include "./language.h" #include "./subtree.h" #include "./tree.h" -#include "api.h" -#include +#include "me/types.h" typedef struct NodeChildIterator { Subtree parent; const TSTree *tree; Length position; - uint32_t child_index; - uint32_t structural_child_index; + t_u32 child_index; + t_u32 structural_child_index; const TSSymbol *alias_sequence; } NodeChildIterator; @@ -32,7 +32,7 @@ static inline TSNode ts_node__null(void) // TSNode - accessors -uint32_t ts_node_start_byte(TSNode self) +t_u32 ts_node_start_byte(TSNode self) { return self.context[0]; } @@ -42,7 +42,7 @@ TSPoint ts_node_start_point(TSNode self) return (TSPoint){self.context[1], self.context[2]}; } -static inline uint32_t ts_node__alias(const TSNode *self) +static inline t_u32 ts_node__alias(const TSNode *self) { return self->context[3]; } @@ -124,7 +124,7 @@ static inline bool ts_node__is_relevant(TSNode self, bool include_anonymous) } } -static inline uint32_t ts_node__relevant_child_count(TSNode self, bool include_anonymous) +static inline t_u32 ts_node__relevant_child_count(TSNode self, bool include_anonymous) { Subtree tree = ts_node__subtree(self); if (ts_subtree_child_count(tree) > 0) @@ -144,7 +144,7 @@ static inline uint32_t ts_node__relevant_child_count(TSNode self, bool include_a } } -static inline TSNode ts_node__child(TSNode self, uint32_t child_index, bool include_anonymous) +static inline TSNode ts_node__child(TSNode self, t_u32 child_index, bool include_anonymous) { TSNode result = self; bool did_descend = true; @@ -154,7 +154,7 @@ static inline TSNode ts_node__child(TSNode self, uint32_t child_index, bool incl did_descend = false; TSNode child; - uint32_t index = 0; + t_u32 index = 0; NodeChildIterator iterator = ts_node_iterate_children(&result); while (ts_node_child_iterator_next(&iterator, &child)) { @@ -168,8 +168,8 @@ static inline TSNode ts_node__child(TSNode self, uint32_t child_index, bool incl } else { - uint32_t grandchild_index = child_index - index; - uint32_t grandchild_count = ts_node__relevant_child_count(child, include_anonymous); + t_u32 grandchild_index = child_index - index; + t_u32 grandchild_count = ts_node__relevant_child_count(child, include_anonymous); if (grandchild_index < grandchild_count) { did_descend = true; @@ -202,9 +202,9 @@ static bool ts_subtree_has_trailing_empty_descendant(Subtree self, Subtree other static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) { - Subtree self_subtree = ts_node__subtree(self); - bool self_is_empty = ts_subtree_total_bytes(self_subtree) == 0; - uint32_t target_end_byte = ts_node_end_byte(self); + Subtree self_subtree = ts_node__subtree(self); + bool self_is_empty = ts_subtree_total_bytes(self_subtree) == 0; + t_u32 target_end_byte = ts_node_end_byte(self); TSNode node = ts_node_parent(self); TSNode earlier_node = ts_node__null(); @@ -281,7 +281,7 @@ static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) { - uint32_t target_end_byte = ts_node_end_byte(self); + t_u32 target_end_byte = ts_node_end_byte(self); TSNode node = ts_node_parent(self); TSNode later_node = ts_node__null(); @@ -350,7 +350,7 @@ static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) return ts_node__null(); } -static inline TSNode ts_node__first_child_for_byte(TSNode self, uint32_t goal, bool include_anonymous) +static inline TSNode ts_node__first_child_for_byte(TSNode self, t_u32 goal, bool include_anonymous) { TSNode node = self; bool did_descend = true; @@ -382,7 +382,7 @@ static inline TSNode ts_node__first_child_for_byte(TSNode self, uint32_t goal, b return ts_node__null(); } -static inline TSNode ts_node__descendant_for_byte_range(TSNode self, uint32_t range_start, uint32_t range_end, bool include_anonymous) +static inline TSNode ts_node__descendant_for_byte_range(TSNode self, t_u32 range_start, t_u32 range_end, bool include_anonymous) { TSNode node = self; TSNode last_visible_node = self; @@ -396,7 +396,7 @@ static inline TSNode ts_node__descendant_for_byte_range(TSNode self, uint32_t ra NodeChildIterator iterator = ts_node_iterate_children(&node); while (ts_node_child_iterator_next(&iterator, &child)) { - uint32_t node_end = iterator.position.bytes; + t_u32 node_end = iterator.position.bytes; // The end of this node must extend far enough forward to touch // the end of the range and exceed the start of the range. @@ -466,7 +466,7 @@ static inline TSNode ts_node__descendant_for_point_range(TSNode self, TSPoint ra // TSNode - public -uint32_t ts_node_end_byte(TSNode self) +t_u32 ts_node_end_byte(TSNode self) { return ts_node_start_byte(self) + ts_subtree_size(ts_node__subtree(self)).bytes; } @@ -557,7 +557,7 @@ bool ts_node_is_error(TSNode self) return symbol == ts_builtin_sym_error; } -uint32_t ts_node_descendant_count(TSNode self) +t_u32 ts_node_descendant_count(TSNode self) { return ts_subtree_visible_descendant_count(ts_node__subtree(self)) + 1; } @@ -570,12 +570,12 @@ TSStateId ts_node_parse_state(TSNode self) TSStateId ts_node_next_parse_state(TSNode self) { const TSLanguage *language = self.tree->language; - uint16_t state = ts_node_parse_state(self); + t_u16 state = ts_node_parse_state(self); if (state == TS_TREE_STATE_NONE) { return TS_TREE_STATE_NONE; } - uint16_t symbol = ts_node_grammar_symbol(self); + t_u16 symbol = ts_node_grammar_symbol(self); return ts_language_next_state(language, state, symbol); } @@ -598,8 +598,8 @@ TSNode ts_node_parent(TSNode self) TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode) { - uint32_t start_byte = ts_node_start_byte(subnode); - uint32_t end_byte = ts_node_end_byte(subnode); + t_u32 start_byte = ts_node_start_byte(subnode); + t_u32 end_byte = ts_node_end_byte(subnode); do { @@ -616,12 +616,12 @@ TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode) return self; } -TSNode ts_node_child(TSNode self, uint32_t child_index) +TSNode ts_node_child(TSNode self, t_u32 child_index) { return ts_node__child(self, child_index, true); } -TSNode ts_node_named_child(TSNode self, uint32_t child_index) +TSNode ts_node_named_child(TSNode self, t_u32 child_index) { return ts_node__child(self, child_index, false); } @@ -658,7 +658,7 @@ recur: { if (!ts_subtree_extra(ts_node__subtree(child))) { - uint32_t index = iterator.structural_child_index - 1; + t_u32 index = iterator.structural_child_index - 1; if (index < field_map->child_index) continue; @@ -712,7 +712,7 @@ recur: return ts_node__null(); } -static inline const char *ts_node__field_name_from_language(TSNode self, uint32_t structural_child_index) +static inline const char *ts_node__field_name_from_language(TSNode self, t_u32 structural_child_index) { const TSFieldMapEntry *field_map, *field_map_end; ts_language_field_map(self.tree->language, ts_node__subtree(self).ptr->production_id, &field_map, &field_map_end); @@ -726,7 +726,7 @@ static inline const char *ts_node__field_name_from_language(TSNode self, uint32_ return NULL; } -const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) +const char *ts_node_field_name_for_child(TSNode self, t_u32 child_index) { TSNode result = self; bool did_descend = true; @@ -737,7 +737,7 @@ const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) did_descend = false; TSNode child; - uint32_t index = 0; + t_u32 index = 0; NodeChildIterator iterator = ts_node_iterate_children(&result); while (ts_node_child_iterator_next(&iterator, &child)) { @@ -758,8 +758,8 @@ const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) } else { - uint32_t grandchild_index = child_index - index; - uint32_t grandchild_count = ts_node__relevant_child_count(child, true); + t_u32 grandchild_index = child_index - index; + t_u32 grandchild_count = ts_node__relevant_child_count(child, true); if (grandchild_index < grandchild_count) { const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1); @@ -779,13 +779,13 @@ const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) return NULL; } -TSNode ts_node_child_by_field_name(TSNode self, const char *name, uint32_t name_length) +TSNode ts_node_child_by_field_name(TSNode self, const char *name, t_u32 name_length) { TSFieldId field_id = ts_language_field_id_for_name(self.tree->language, name, name_length); return ts_node_child_by_field_id(self, field_id); } -uint32_t ts_node_child_count(TSNode self) +t_u32 ts_node_child_count(TSNode self) { Subtree tree = ts_node__subtree(self); if (ts_subtree_child_count(tree) > 0) @@ -798,7 +798,7 @@ uint32_t ts_node_child_count(TSNode self) } } -uint32_t ts_node_named_child_count(TSNode self) +t_u32 ts_node_named_child_count(TSNode self) { Subtree tree = ts_node__subtree(self); if (ts_subtree_child_count(tree) > 0) @@ -831,22 +831,22 @@ TSNode ts_node_prev_named_sibling(TSNode self) return ts_node__prev_sibling(self, false); } -TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte) +TSNode ts_node_first_child_for_byte(TSNode self, t_u32 byte) { return ts_node__first_child_for_byte(self, byte, true); } -TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte) +TSNode ts_node_first_named_child_for_byte(TSNode self, t_u32 byte) { return ts_node__first_child_for_byte(self, byte, false); } -TSNode ts_node_descendant_for_byte_range(TSNode self, uint32_t start, uint32_t end) +TSNode ts_node_descendant_for_byte_range(TSNode self, t_u32 start, t_u32 end) { return ts_node__descendant_for_byte_range(self, start, end, true); } -TSNode ts_node_named_descendant_for_byte_range(TSNode self, uint32_t start, uint32_t end) +TSNode ts_node_named_descendant_for_byte_range(TSNode self, t_u32 start, t_u32 end) { return ts_node__descendant_for_byte_range(self, start, end, false); } @@ -863,8 +863,8 @@ TSNode ts_node_named_descendant_for_point_range(TSNode self, TSPoint start, TSPo void ts_node_edit(TSNode *self, const TSInputEdit *edit) { - uint32_t start_byte = ts_node_start_byte(*self); - TSPoint start_point = ts_node_start_point(*self); + t_u32 start_byte = ts_node_start_byte(*self); + TSPoint start_point = ts_node_start_point(*self); if (start_byte >= edit->old_end_byte) { @@ -882,7 +882,7 @@ void ts_node_edit(TSNode *self, const TSInputEdit *edit) self->context[2] = start_point.column; } -TSSymbol ts_node_field_id_for_child(TSNode self, uint32_t child_index) +TSSymbol ts_node_field_id_for_child(TSNode self, t_u32 child_index) { const char *name = ts_node_field_name_for_child(self, child_index); if (name == NULL) diff --git a/parser/src/parser.c b/parser/src/parser.c index fed71f6a..debb8bac 100644 --- a/parser/src/parser.c +++ b/parser/src/parser.c @@ -10,11 +10,9 @@ #include "./subtree.h" #include "./tree.h" #include "me/mem/mem.h" +#include "me/types.h" #include -#include -#include #include -#include #define LOG(...) #define LOG_LOOKAHEAD(...) @@ -33,9 +31,9 @@ static const unsigned OP_COUNT_PER_TIMEOUT_CHECK = 100; typedef struct TokenCache { - Subtree token; - Subtree last_external_token; - uint32_t byte_index; + Subtree token; + Subtree last_external_token; + t_u32 byte_index; } TokenCache; struct TSParser @@ -79,12 +77,12 @@ typedef enum ErrorComparison typedef struct TSStringInput { const char *string; - uint32_t length; + t_u32 length; } TSStringInput; // StringInput -static const char *ts_string_input_read(void *_self, uint32_t byte, TSPoint point, uint32_t *length) +static const char *ts_string_input_read(void *_self, t_u32 byte, TSPoint point, t_u32 *length) { (void)point; TSStringInput *self = (TSStringInput *)_self; @@ -135,13 +133,13 @@ static bool ts_parser__breakdown_top_of_stack(TSParser *self, StackVersion versi did_break_down = true; pending = false; - for (uint32_t i = 0; i < pop.size; i++) + for (t_u32 i = 0; i < pop.size; i++) { StackSlice slice = pop.contents[i]; TSStateId state = ts_stack_state(self->stack, slice.version); Subtree parent = *array_front(&slice.subtrees); - for (uint32_t j = 0, n = ts_subtree_child_count(parent); j < n; j++) + for (t_u32 j = 0, n = ts_subtree_child_count(parent); j < n; j++) { Subtree child = ts_subtree_children(parent)[j]; pending = ts_subtree_child_count(child) > 0; @@ -159,7 +157,7 @@ static bool ts_parser__breakdown_top_of_stack(TSParser *self, StackVersion versi ts_stack_push(self->stack, slice.version, child, pending, state); } - for (uint32_t j = 1; j < slice.subtrees.size; j++) + for (t_u32 j = 1; j < slice.subtrees.size; j++) { Subtree tree = slice.subtrees.contents[j]; ts_stack_push(self->stack, slice.version, tree, false, state); @@ -318,7 +316,7 @@ static void ts_parser__external_scanner_destroy(TSParser *self) static unsigned ts_parser__external_scanner_serialize(TSParser *self) { - uint32_t length = self->language->external_scanner.serialize(self->external_scanner_payload, self->lexer.debug_buffer); + t_u32 length = self->language->external_scanner.serialize(self->external_scanner_payload, self->lexer.debug_buffer); assert(length <= TREE_SITTER_SERIALIZATION_BUFFER_SIZE); return length; } @@ -326,7 +324,7 @@ static unsigned ts_parser__external_scanner_serialize(TSParser *self) static void ts_parser__external_scanner_deserialize(TSParser *self, Subtree external_token) { const char *data = NULL; - uint32_t length = 0; + t_u32 length = 0; if (external_token.ptr) { data = ts_external_scanner_state_data(&external_token.ptr->external_scanner_state); @@ -353,7 +351,7 @@ static bool ts_parser__can_reuse_first_leaf(TSParser *self, TSStateId state, Sub // NULL, which indicates that the parser should look for a reduce action // at symbol `0`. Avoid reusing tokens in this situation to ensure that // the same thing happens when incrementally reparsing. - if (current_lex_mode.lex_state == (uint16_t)(-1)) + if (current_lex_mode.lex_state == (t_u16)(-1)) return false; // If the token was created in a state with the same set of lookaheads, it is reusable. @@ -373,7 +371,7 @@ static bool ts_parser__can_reuse_first_leaf(TSParser *self, TSStateId state, Sub static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId parse_state) { TSLexMode lex_mode = self->language->lex_modes[parse_state]; - if (lex_mode.lex_state == (uint16_t)-1) + if (lex_mode.lex_state == (t_u16)-1) { LOG("no_lookahead_after_non_terminal_extra"); return NULL_SUBTREE; @@ -382,16 +380,16 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa const Length start_position = ts_stack_position(self->stack, version); const Subtree external_token = ts_stack_last_external_token(self->stack, version); - bool found_external_token = false; - bool error_mode = parse_state == ERROR_STATE; - bool skipped_error = false; - bool called_get_column = false; - int32_t first_error_character = 0; - Length error_start_position = length_zero(); - Length error_end_position = length_zero(); - uint32_t lookahead_end_byte = 0; - uint32_t external_scanner_state_len = 0; - bool external_scanner_state_changed = false; + bool found_external_token = false; + bool error_mode = parse_state == ERROR_STATE; + bool skipped_error = false; + bool called_get_column = false; + t_i32 first_error_character = 0; + Length error_start_position = length_zero(); + Length error_end_position = length_zero(); + t_u32 lookahead_end_byte = 0; + t_u32 external_scanner_state_len = 0; + bool external_scanner_state_changed = false; ts_lexer_reset(&self->lexer, start_position); for (;;) @@ -486,9 +484,9 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa Subtree result; if (skipped_error) { - Length padding = length_sub(error_start_position, start_position); - Length size = length_sub(error_end_position, error_start_position); - uint32_t lookahead_bytes = lookahead_end_byte - error_end_position.bytes; + Length padding = length_sub(error_start_position, start_position); + Length size = length_sub(error_end_position, error_start_position); + 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); } else @@ -497,7 +495,7 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa TSSymbol symbol = self->lexer.data.result_symbol; Length padding = length_sub(self->lexer.token_start_position, start_position); Length size = length_sub(self->lexer.token_end_position, self->lexer.token_start_position); - uint32_t lookahead_bytes = lookahead_end_byte - self->lexer.token_end_position.bytes; + t_u32 lookahead_bytes = lookahead_end_byte - self->lexer.token_end_position.bytes; if (found_external_token) { @@ -505,7 +503,7 @@ static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId pa } else if (symbol == self->language->keyword_capture_token && symbol != 0) { - uint32_t end_byte = self->lexer.token_end_position.bytes; + t_u32 end_byte = self->lexer.token_end_position.bytes; ts_lexer_reset(&self->lexer, self->lexer.token_start_position); ts_lexer_start(&self->lexer); @@ -550,7 +548,7 @@ static Subtree ts_parser__get_cached_token(TSParser *self, TSStateId state, size return NULL_SUBTREE; } -static void ts_parser__set_cached_token(TSParser *self, uint32_t byte_index, Subtree last_external_token, Subtree token) +static void ts_parser__set_cached_token(TSParser *self, t_u32 byte_index, Subtree last_external_token, Subtree token) { TokenCache *cache = &self->token_cache; if (token.ptr) @@ -655,10 +653,10 @@ static void ts_parser__shift(TSParser *self, StackVersion version, TSStateId sta } } -static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSymbol symbol, uint32_t count, int dynamic_precedence, - uint16_t production_id, bool is_fragile, bool end_of_non_terminal_extra) +static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSymbol symbol, t_u32 count, int dynamic_precedence, + t_u16 production_id, bool is_fragile, bool end_of_non_terminal_extra) { - uint32_t initial_version_count = ts_stack_version_count(self->stack); + t_u32 initial_version_count = ts_stack_version_count(self->stack); // Pop the given number of nodes from the given version of the parse stack. // If stack versions have previously merged, then there may be more than one @@ -666,8 +664,8 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy // contain the popped children, and push it onto the stack in place of the // children. StackSliceArray pop = ts_stack_pop_count(self->stack, version, count); - uint32_t removed_version_count = 0; - for (uint32_t i = 0; i < pop.size; i++) + t_u32 removed_version_count = 0; + for (t_u32 i = 0; i < pop.size; i++) { StackSlice slice = pop.contents[i]; StackVersion slice_version = slice.version - removed_version_count; @@ -749,7 +747,7 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy // Push the parent node onto the stack, along with any extra tokens that // were previously on top of the stack. ts_stack_push(self->stack, slice_version, ts_subtree_from_mut(parent), false, next_state); - for (uint32_t j = 0; j < self->trailing_extras.size; j++) + for (t_u32 j = 0; j < self->trailing_extras.size; j++) { ts_stack_push(self->stack, slice_version, self->trailing_extras.contents[j], false, next_state); } @@ -776,20 +774,20 @@ static void ts_parser__accept(TSParser *self, StackVersion version, Subtree look ts_stack_push(self->stack, version, lookahead, false, 1); StackSliceArray pop = ts_stack_pop_all(self->stack, version); - for (uint32_t i = 0; i < pop.size; i++) + for (t_u32 i = 0; i < pop.size; i++) { SubtreeArray trees = pop.contents[i].subtrees; Subtree root = NULL_SUBTREE; - for (uint32_t j = trees.size - 1; j + 1 > 0; j--) + for (t_u32 j = trees.size - 1; j + 1 > 0; j--) { Subtree tree = trees.contents[j]; if (!ts_subtree_extra(tree)) { assert(!tree.data.is_inline); - uint32_t child_count = ts_subtree_child_count(tree); + t_u32 child_count = ts_subtree_child_count(tree); const Subtree *children = ts_subtree_children(tree); - for (uint32_t k = 0; k < child_count; k++) + for (t_u32 k = 0; k < child_count; k++) { ts_subtree_retain(children[k]); } @@ -827,13 +825,13 @@ static void ts_parser__accept(TSParser *self, StackVersion version, Subtree look static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion starting_version, TSSymbol lookahead_symbol) { - uint32_t initial_version_count = ts_stack_version_count(self->stack); + t_u32 initial_version_count = ts_stack_version_count(self->stack); bool can_shift_lookahead_symbol = false; StackVersion version = starting_version; for (unsigned i = 0; true; i++) { - uint32_t version_count = ts_stack_version_count(self->stack); + t_u32 version_count = ts_stack_version_count(self->stack); if (version >= version_count) break; @@ -869,7 +867,7 @@ static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion { TableEntry entry; ts_language_table_entry(self->language, state, symbol, &entry); - for (uint32_t j = 0; j < entry.action_count; j++) + for (t_u32 j = 0; j < entry.action_count; j++) { TSParseAction action = entry.actions[j]; switch (action.type) @@ -895,7 +893,7 @@ static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion } StackVersion reduction_version = STACK_VERSION_NONE; - for (uint32_t j = 0; j < self->reduce_actions.size; j++) + for (t_u32 j = 0; j < self->reduce_actions.size; j++) { ReduceAction action = self->reduce_actions.contents[j]; @@ -958,8 +956,8 @@ static bool ts_parser__recover_to_state(TSParser *self, StackVersion version, un if (error_trees.size > 0) { assert(error_trees.size == 1); - Subtree error_tree = error_trees.contents[0]; - uint32_t error_child_count = ts_subtree_child_count(error_tree); + Subtree error_tree = error_trees.contents[0]; + t_u32 error_child_count = ts_subtree_child_count(error_tree); if (error_child_count > 0) { array_splice(&slice.subtrees, 0, 0, error_child_count, ts_subtree_children(error_tree)); @@ -1172,14 +1170,14 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtree lookahead) { - uint32_t previous_version_count = ts_stack_version_count(self->stack); + t_u32 previous_version_count = ts_stack_version_count(self->stack); // Perform any reductions that can happen in this state, regardless of the lookahead. After // skipping one or more invalid tokens, the parser might find a token that would have allowed // a reduction to take place. ts_parser__do_all_potential_reductions(self, version, 0); - uint32_t version_count = ts_stack_version_count(self->stack); - Length position = ts_stack_position(self->stack, version); + t_u32 version_count = ts_stack_version_count(self->stack); + Length position = ts_stack_position(self->stack, version); // Push a discontinuity onto the stack. Merge all of the stack versions that // were created in the previous step. @@ -1189,7 +1187,7 @@ static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtre if (!did_insert_missing_token) { TSStateId state = ts_stack_state(self->stack, v); - for (TSSymbol missing_symbol = 1; missing_symbol < (uint16_t)self->language->token_count; missing_symbol++) + for (TSSymbol missing_symbol = 1; missing_symbol < (t_u16)self->language->token_count; missing_symbol++) { TSStateId state_after_missing_symbol = ts_language_next_state(self->language, state, missing_symbol); if (state_after_missing_symbol == 0 || state_after_missing_symbol == state) @@ -1204,8 +1202,8 @@ static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtre // must be assigned to position it within the next included range. ts_lexer_reset(&self->lexer, position); ts_lexer_mark_end(&self->lexer); - Length padding = length_sub(self->lexer.token_end_position, position); - uint32_t lookahead_bytes = ts_subtree_total_bytes(lookahead) + ts_subtree_lookahead_bytes(lookahead); + Length padding = length_sub(self->lexer.token_end_position, position); + 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); Subtree missing_tree = @@ -1250,7 +1248,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_ { (void)(allow_node_reuse); TSStateId state = ts_stack_state(self->stack, version); - uint32_t position = ts_stack_position(self->stack, version).bytes; + t_u32 position = ts_stack_position(self->stack, version).bytes; Subtree last_external_token = ts_stack_last_external_token(self->stack, version); Subtree lookahead = NULL_SUBTREE; @@ -1310,7 +1308,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_ // version, whereas SHIFT actions update the existing stack version // and terminate this loop. StackVersion last_reduction_version = STACK_VERSION_NONE; - for (uint32_t i = 0; i < table_entry.action_count; i++) + for (t_u32 i = 0; i < table_entry.action_count; i++) { TSParseAction action = table_entry.actions[i]; @@ -1694,7 +1692,7 @@ TSTree *ts_parser_parse(TSParser *self, const TSTree *old_tree, TSInput input) self->operation_count = 0; - uint32_t position = 0, last_position = 0, version_count = 0; + t_u32 position = 0, last_position = 0, version_count = 0; do { for (StackVersion version = 0; version_count = ts_stack_version_count(self->stack), version < version_count; version++) @@ -1753,13 +1751,12 @@ exit: return result; } -TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, const char *string, uint32_t length) +TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree, const char *string, t_u32 length) { return ts_parser_parse_string_encoding(self, old_tree, string, length, TSInputEncodingUTF8); } -TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, const char *string, uint32_t length, - TSInputEncoding encoding) +TSTree *ts_parser_parse_string_encoding(TSParser *self, const TSTree *old_tree, const char *string, t_u32 length, TSInputEncoding encoding) { TSStringInput input = {string, length}; return ts_parser_parse(self, old_tree, diff --git a/parser/src/parser.h b/parser/src/parser.h index 258ab707..3c9d60af 100644 --- a/parser/src/parser.h +++ b/parser/src/parser.h @@ -1,32 +1,30 @@ #ifndef TREE_SITTER_PARSER_H_ #define TREE_SITTER_PARSER_H_ -#include -#include -#include +#include "me/types.h" #define ts_builtin_sym_error ((TSSymbol)-1) #define ts_builtin_sym_end 0 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 #ifndef TREE_SITTER_API_H_ -typedef uint16_t TSStateId; -typedef uint16_t TSSymbol; -typedef uint16_t TSFieldId; +typedef t_u16 TSStateId; +typedef t_u16 TSSymbol; +typedef t_u16 TSFieldId; typedef struct TSLanguage TSLanguage; #endif typedef struct TSFieldMapEntry { TSFieldId field_id; - uint8_t child_index; + t_u8 child_index; bool inherited; } TSFieldMapEntry; typedef struct TSFieldMapSlice { - uint16_t index; - uint16_t length; + t_u16 index; + t_u16 length; } TSFieldMapSlice; typedef struct TSSymbolMetadata @@ -40,11 +38,11 @@ typedef struct TSLexer TSLexer; struct TSLexer { - int32_t lookahead; + t_i32 lookahead; TSSymbol result_symbol; void (*advance)(TSLexer *, bool); void (*mark_end)(TSLexer *); - uint32_t (*get_column)(TSLexer *); + t_u32 (*get_column)(TSLexer *); bool (*is_at_included_range_start)(const TSLexer *); bool (*eof)(const TSLexer *); }; @@ -60,58 +58,58 @@ typedef enum TSParseActionType typedef union TSParseAction { struct TSParseActionShift { - uint8_t type; + t_u8 type; TSStateId state; bool extra; bool repetition; } shift; struct TSParseActionReduce { - uint8_t type; - uint8_t child_count; + t_u8 type; + t_u8 child_count; TSSymbol symbol; - int16_t dynamic_precedence; - uint16_t production_id; + t_i16 dynamic_precedence; + t_u16 production_id; } reduce; - uint8_t type; + t_u8 type; } TSParseAction; typedef struct TSLexMode { - uint16_t lex_state; - uint16_t external_lex_state; + t_u16 lex_state; + t_u16 external_lex_state; } TSLexMode; typedef union TSParseActionEntry { TSParseAction action; struct TSParseActionEntryInner { - uint8_t count; - bool reusable; + t_u8 count; + bool reusable; } entry; } TSParseActionEntry; typedef struct TSCharacterRange { - int32_t start; - int32_t end; + t_i32 start; + t_i32 end; } TSCharacterRange; struct TSLanguage { - uint32_t version; - uint32_t symbol_count; - uint32_t alias_count; - uint32_t token_count; - uint32_t external_token_count; - uint32_t state_count; - uint32_t large_state_count; - uint32_t production_id_count; - uint32_t field_count; - uint16_t max_alias_sequence_length; - const uint16_t *parse_table; - const uint16_t *small_parse_table; - const uint32_t *small_parse_table_map; + t_u32 version; + t_u32 symbol_count; + t_u32 alias_count; + t_u32 token_count; + t_u32 external_token_count; + t_u32 state_count; + t_u32 large_state_count; + t_u32 production_id_count; + t_u32 field_count; + t_u16 max_alias_sequence_length; + const t_u16 *parse_table; + const t_u16 *small_parse_table; + const t_u32 *small_parse_table_map; const TSParseActionEntry *parse_actions; const char *const *symbol_names; const char *const *field_names; @@ -119,7 +117,7 @@ struct TSLanguage const TSFieldMapEntry *field_map_entries; const TSSymbolMetadata *symbol_metadata; const TSSymbol *public_symbol_map; - const uint16_t *alias_map; + const t_u16 *alias_map; const TSSymbol *alias_sequences; const TSLexMode *lex_modes; bool (*lex_fn)(TSLexer *, TSStateId); @@ -138,14 +136,14 @@ struct TSLanguage const TSStateId *primary_state_ids; }; -static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) +static inline bool set_contains(TSCharacterRange *ranges, t_u32 len, t_i32 lookahead) { - uint32_t index = 0; - uint32_t size = len - index; + t_u32 index = 0; + t_u32 size = len - index; while (size > 1) { - uint32_t half_size = size / 2; - uint32_t mid_index = index + half_size; + t_u32 half_size = size / 2; + t_u32 mid_index = index + half_size; TSCharacterRange *range = &ranges[mid_index]; if (lookahead >= range->start && lookahead <= range->end) { @@ -171,8 +169,8 @@ static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t bool result = false; \ bool skip = false; \ UNUSED \ - bool eof = false; \ - int32_t lookahead; \ + bool eof = false; \ + t_i32 lookahead; \ goto start; \ next_state: \ lexer->advance(lexer, skip); \ @@ -188,8 +186,8 @@ start: #define ADVANCE_MAP(...) \ { \ - static const uint16_t map[] = {__VA_ARGS__}; \ - for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) \ + static const t_u16 map[] = {__VA_ARGS__}; \ + for (t_u32 i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) \ { \ if (map[i] == lookahead) \ { \ diff --git a/parser/src/point.h b/parser/src/point.h index 1c8b7133..ef73804c 100644 --- a/parser/src/point.h +++ b/parser/src/point.h @@ -2,61 +2,72 @@ #define TREE_SITTER_POINT_H_ #include "./api.h" +#include "me/types.h" -#define POINT_ZERO ((TSPoint) {0, 0}) -#define POINT_MAX ((TSPoint) {UINT32_MAX, UINT32_MAX}) +#define POINT_ZERO ((TSPoint){0, 0}) +#define POINT_MAX ((TSPoint){UINT32_MAX, UINT32_MAX}) -static inline TSPoint point__new(unsigned row, unsigned column) { - TSPoint result = {row, column}; - return result; +static inline TSPoint point__new(unsigned row, unsigned column) +{ + TSPoint result = {row, column}; + return result; } -static inline TSPoint point_add(TSPoint a, TSPoint b) { - if (b.row > 0) - return point__new(a.row + b.row, b.column); - else - return point__new(a.row, a.column + b.column); +static inline TSPoint point_add(TSPoint a, TSPoint b) +{ + if (b.row > 0) + return point__new(a.row + b.row, b.column); + else + return point__new(a.row, a.column + b.column); } -static inline TSPoint point_sub(TSPoint a, TSPoint b) { - if (a.row > b.row) - return point__new(a.row - b.row, a.column); - else - return point__new(0, a.column - b.column); +static inline TSPoint point_sub(TSPoint a, TSPoint b) +{ + if (a.row > b.row) + return point__new(a.row - b.row, a.column); + else + return point__new(0, a.column - b.column); } -static inline bool point_lte(TSPoint a, TSPoint b) { - return (a.row < b.row) || (a.row == b.row && a.column <= b.column); +static inline bool point_lte(TSPoint a, TSPoint b) +{ + return (a.row < b.row) || (a.row == b.row && a.column <= b.column); } -static inline bool point_lt(TSPoint a, TSPoint b) { - return (a.row < b.row) || (a.row == b.row && a.column < b.column); +static inline bool point_lt(TSPoint a, TSPoint b) +{ + return (a.row < b.row) || (a.row == b.row && a.column < b.column); } -static inline bool point_gt(TSPoint a, TSPoint b) { - return (a.row > b.row) || (a.row == b.row && a.column > b.column); +static inline bool point_gt(TSPoint a, TSPoint b) +{ + return (a.row > b.row) || (a.row == b.row && a.column > b.column); } -static inline bool point_gte(TSPoint a, TSPoint b) { - return (a.row > b.row) || (a.row == b.row && a.column >= b.column); +static inline bool point_gte(TSPoint a, TSPoint b) +{ + return (a.row > b.row) || (a.row == b.row && a.column >= b.column); } -static inline bool point_eq(TSPoint a, TSPoint b) { - return a.row == b.row && a.column == b.column; +static inline bool point_eq(TSPoint a, TSPoint b) +{ + return a.row == b.row && a.column == b.column; } -static inline TSPoint point_min(TSPoint a, TSPoint b) { - if (a.row < b.row || (a.row == b.row && a.column < b.column)) - return a; - else - return b; +static inline TSPoint point_min(TSPoint a, TSPoint b) +{ + if (a.row < b.row || (a.row == b.row && a.column < b.column)) + return a; + else + return b; } -static inline TSPoint point_max(TSPoint a, TSPoint b) { - if (a.row > b.row || (a.row == b.row && a.column > b.column)) - return a; - else - return b; +static inline TSPoint point_max(TSPoint a, TSPoint b) +{ + if (a.row > b.row || (a.row == b.row && a.column > b.column)) + return a; + else + return b; } #endif diff --git a/parser/src/reduce_action.h b/parser/src/reduce_action.h index e8dfda18..eecd7b79 100644 --- a/parser/src/reduce_action.h +++ b/parser/src/reduce_action.h @@ -1,28 +1,29 @@ #ifndef TREE_SITTER_REDUCE_ACTION_H_ #define TREE_SITTER_REDUCE_ACTION_H_ - -#include "./array.h" #include "./api.h" +#include "./array.h" +#include "me/types.h" -typedef struct ReduceAction { - uint32_t count; - TSSymbol symbol; - int dynamic_precedence; - unsigned short production_id; +typedef struct ReduceAction +{ + t_u32 count; + TSSymbol symbol; + int dynamic_precedence; + unsigned short production_id; } ReduceAction; typedef Array(ReduceAction) ReduceActionSet; -static inline void ts_reduce_action_set_add(ReduceActionSet *self, - ReduceAction new_action) { - for (uint32_t i = 0; i < self->size; i++) { - ReduceAction action = self->contents[i]; - if (action.symbol == new_action.symbol && action.count == new_action.count) - return; - } - array_push(self, new_action); +static inline void ts_reduce_action_set_add(ReduceActionSet *self, ReduceAction new_action) +{ + for (t_u32 i = 0; i < self->size; i++) + { + ReduceAction action = self->contents[i]; + if (action.symbol == new_action.symbol && action.count == new_action.count) + return; + } + array_push(self, new_action); } - -#endif // TREE_SITTER_REDUCE_ACTION_H_ +#endif // TREE_SITTER_REDUCE_ACTION_H_ diff --git a/parser/src/scanner.c b/parser/src/scanner.c index 4928879c..9e5a1112 100644 --- a/parser/src/scanner.c +++ b/parser/src/scanner.c @@ -1,8 +1,7 @@ #include "array.h" +#include "me/types.h" #include "parser.h" - #include -#include #include #include @@ -52,9 +51,9 @@ typedef struct Heredoc typedef struct Scanner { - uint8_t last_glob_paren_depth; - bool ext_was_in_double_quote; - bool ext_saw_outside_quote; + t_u8 last_glob_paren_depth; + bool ext_was_in_double_quote; + bool ext_saw_outside_quote; Array(Heredoc) heredocs; } Scanner; @@ -92,7 +91,7 @@ static inline void reset_heredoc(Heredoc *heredoc) static inline void reset(Scanner *scanner) { - for (uint32_t i = 0; i < scanner->heredocs.size; i++) + for (t_u32 i = 0; i < scanner->heredocs.size; i++) { reset_heredoc(array_get(&scanner->heredocs, i)); } @@ -100,14 +99,14 @@ static inline void reset(Scanner *scanner) static unsigned serialize(Scanner *scanner, char *buffer) { - uint32_t size = 0; + t_u32 size = 0; buffer[size++] = (char)scanner->last_glob_paren_depth; buffer[size++] = (char)scanner->ext_was_in_double_quote; buffer[size++] = (char)scanner->ext_saw_outside_quote; buffer[size++] = (char)scanner->heredocs.size; - for (uint32_t i = 0; i < scanner->heredocs.size; i++) + for (t_u32 i = 0; i < scanner->heredocs.size; i++) { Heredoc *heredoc = array_get(&scanner->heredocs, i); if (heredoc->delimiter.size + 3 + size >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) @@ -119,8 +118,8 @@ static unsigned serialize(Scanner *scanner, char *buffer) buffer[size++] = (char)heredoc->started; buffer[size++] = (char)heredoc->allows_indent; - memcpy(&buffer[size], &heredoc->delimiter.size, sizeof(uint32_t)); - size += sizeof(uint32_t); + memcpy(&buffer[size], &heredoc->delimiter.size, sizeof(t_u32)); + size += sizeof(t_u32); if (heredoc->delimiter.size > 0) { memcpy(&buffer[size], heredoc->delimiter.contents, heredoc->delimiter.size); @@ -138,12 +137,12 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length) } else { - uint32_t size = 0; + t_u32 size = 0; scanner->last_glob_paren_depth = buffer[size++]; scanner->ext_was_in_double_quote = buffer[size++]; scanner->ext_saw_outside_quote = buffer[size++]; - uint32_t heredoc_count = (unsigned char)buffer[size++]; - for (uint32_t i = 0; i < heredoc_count; i++) + t_u32 heredoc_count = (unsigned char)buffer[size++]; + for (t_u32 i = 0; i < heredoc_count; i++) { Heredoc *heredoc = NULL; if (i < scanner->heredocs.size) @@ -161,8 +160,8 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length) heredoc->started = buffer[size++]; heredoc->allows_indent = buffer[size++]; - memcpy(&heredoc->delimiter.size, &buffer[size], sizeof(uint32_t)); - size += sizeof(uint32_t); + memcpy(&heredoc->delimiter.size, &buffer[size], sizeof(t_u32)); + size += sizeof(t_u32); array_reserve(&heredoc->delimiter, heredoc->delimiter.size); if (heredoc->delimiter.size > 0) @@ -184,8 +183,8 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length) */ static bool advance_word(TSLexer *lexer, String *unquoted_word) { - bool empty = true; - int32_t quote = 0; + bool empty = true; + t_i32 quote = 0; if (lexer->lookahead == '\'' || lexer->lookahead == '"') { @@ -254,10 +253,10 @@ static bool scan_heredoc_end_identifier(Heredoc *heredoc, TSLexer *lexer) reset_string(&heredoc->current_leading_word); // Scan the first 'n' characters on this line, to see if they match the // heredoc delimiter - int32_t size = 0; + t_i32 size = 0; if (heredoc->delimiter.size > 0) { - while (lexer->lookahead != '\0' && lexer->lookahead != '\n' && (int32_t)*array_get(&heredoc->delimiter, size) == lexer->lookahead && + while (lexer->lookahead != '\0' && lexer->lookahead != '\n' && (t_i32)*array_get(&heredoc->delimiter, size) == lexer->lookahead && heredoc->current_leading_word.size < heredoc->delimiter.size) { array_push(&heredoc->current_leading_word, lexer->lookahead); @@ -710,14 +709,14 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { typedef struct { - bool done; - bool advanced_once; - bool found_non_alnumdollarunderdash; - bool last_was_escape; - bool in_single_quote; - uint32_t paren_depth; - uint32_t bracket_depth; - uint32_t brace_depth; + bool done; + bool advanced_once; + bool found_non_alnumdollarunderdash; + bool last_was_escape; + bool in_single_quote; + t_u32 paren_depth; + t_u32 bracket_depth; + t_u32 brace_depth; } State; if (lexer->lookahead == '$') @@ -952,11 +951,11 @@ extglob_pattern: typedef struct { - bool done; - bool saw_non_alphadot; - uint32_t paren_depth; - uint32_t bracket_depth; - uint32_t brace_depth; + bool done; + bool saw_non_alphadot; + t_u32 paren_depth; + t_u32 bracket_depth; + t_u32 brace_depth; } State; State state = {false, was_non_alpha, scanner->last_glob_paren_depth, 0, 0}; diff --git a/parser/src/stack.c b/parser/src/stack.c index b94a65bd..719e816f 100644 --- a/parser/src/stack.c +++ b/parser/src/stack.c @@ -5,8 +5,8 @@ #include "./length.h" #include "./subtree.h" #include "me/mem/mem.h" +#include "me/types.h" #include -#include #include #define MAX_LINK_COUNT 8 @@ -28,7 +28,7 @@ struct StackNode Length position; StackLink links[MAX_LINK_COUNT]; short unsigned int link_count; - uint32_t ref_count; + t_u32 ref_count; unsigned error_cost; unsigned node_count; int dynamic_precedence; @@ -38,7 +38,7 @@ typedef struct StackIterator { StackNode *node; SubtreeArray subtrees; - uint32_t subtree_count; + t_u32 subtree_count; bool is_pending; } StackIterator; @@ -132,9 +132,9 @@ recur: /// Get the number of nodes in the subtree, for the purpose of measuring /// how much progress has been made by a given version of the stack. -static uint32_t stack__subtree_node_count(Subtree subtree) +static t_u32 stack__subtree_node_count(Subtree subtree) { - uint32_t count = ts_subtree_visible_descendant_count(subtree); + t_u32 count = ts_subtree_visible_descendant_count(subtree); if (ts_subtree_visible(subtree)) count++; @@ -237,7 +237,7 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su { stack_node_add_link(existing_link->node, link.node->links[j], subtree_pool); } - int32_t dynamic_precedence = link.node->dynamic_precedence; + t_i32 dynamic_precedence = link.node->dynamic_precedence; if (link.subtree.ptr) { dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree); @@ -311,7 +311,7 @@ static StackVersion ts_stack__add_version(Stack *self, StackVersion original_ver static void ts_stack__add_slice(Stack *self, StackVersion original_version, StackNode *node, SubtreeArray *subtrees) { - for (uint32_t i = self->slices.size - 1; i + 1 > 0; i--) + for (t_u32 i = self->slices.size - 1; i + 1 > 0; i--) { StackVersion version = self->slices.contents[i].version; if (self->heads.contents[version].node == node) @@ -344,14 +344,14 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb if (goal_subtree_count >= 0) { include_subtrees = true; - array_reserve(&new_iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree)); + array_reserve(&new_iterator.subtrees, (t_u32)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree)); } array_push(&self->iterators, new_iterator); while (self->iterators.size > 0) { - for (uint32_t i = 0, size = self->iterators.size; i < size; i++) + for (t_u32 i = 0, size = self->iterators.size; i < size; i++) { StackIterator *iterator = &self->iterators.contents[i]; StackNode *node = iterator->node; @@ -382,7 +382,7 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb continue; } - for (uint32_t j = 1; j <= node->link_count; j++) + for (t_u32 j = 1; j <= node->link_count; j++) { StackIterator *next_iterator; StackLink link; @@ -459,14 +459,14 @@ void ts_stack_delete(Stack *self) if (self->iterators.contents) array_delete(&self->iterators); stack_node_release(self->base_node, &self->node_pool, self->subtree_pool); - for (uint32_t 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); } array_clear(&self->heads); if (self->node_pool.contents) { - for (uint32_t i = 0; i < self->node_pool.size; i++) + for (t_u32 i = 0; i < self->node_pool.size; i++) mem_free(self->node_pool.contents[i]); array_delete(&self->node_pool); } @@ -474,7 +474,7 @@ void ts_stack_delete(Stack *self) mem_free(self); } -uint32_t ts_stack_version_count(const Stack *self) +t_u32 ts_stack_version_count(const Stack *self) { return self->heads.size; } @@ -547,7 +547,7 @@ StackAction pop_count_callback(void *payload, const StackIterator *iterator) } } -StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) +StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, t_u32 count) { return stack__iter(self, version, pop_count_callback, &count, (int)count); } @@ -729,7 +729,7 @@ void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) if (v1 == v2) return; assert(v2 < v1); - assert((uint32_t)v1 < self->heads.size); + assert((t_u32)v1 < self->heads.size); StackHead *source_head = &self->heads.contents[v1]; StackHead *target_head = &self->heads.contents[v2]; if (target_head->summary && !source_head->summary) @@ -767,7 +767,7 @@ bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2) return false; StackHead *head1 = &self->heads.contents[version1]; StackHead *head2 = &self->heads.contents[version2]; - for (uint32_t 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); } @@ -829,7 +829,7 @@ Subtree ts_stack_resume(Stack *self, StackVersion version) void ts_stack_clear(Stack *self) { stack_node_retain(self->base_node); - for (uint32_t 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); } diff --git a/parser/src/stack.h b/parser/src/stack.h index 269a5faa..257e8c1b 100644 --- a/parser/src/stack.h +++ b/parser/src/stack.h @@ -1,8 +1,9 @@ #ifndef TREE_SITTER_PARSE_STACK_H_ #define TREE_SITTER_PARSE_STACK_H_ + +#include "me/types.h" #include "./array.h" -// #include "./error_costs.h" #include "./subtree.h" typedef struct Stack Stack; @@ -26,7 +27,7 @@ typedef struct StackSummaryEntry typedef Array(StackSummaryEntry) StackSummary; -typedef void (*StackIterateCallback)(void *, TSStateId, uint32_t); +typedef void (*StackIterateCallback)(void *, TSStateId, t_u32); // Create a stack. Stack *ts_stack_new(SubtreePool *); @@ -35,7 +36,7 @@ Stack *ts_stack_new(SubtreePool *); void ts_stack_delete(Stack *); // Get the stack's current number of versions. -uint32_t ts_stack_version_count(const Stack *); +t_u32 ts_stack_version_count(const Stack *); // Get the state at the top of the given version of the stack. If the stack is // empty, this returns the initial state, 0. @@ -62,7 +63,7 @@ void ts_stack_push(Stack *, StackVersion, Subtree, bool, TSStateId); // versions which had previously been merged. It returns an array that // specifies the index of each revealed version and the trees that were // removed from that version. -StackSliceArray ts_stack_pop_count(Stack *, StackVersion, uint32_t count); +StackSliceArray ts_stack_pop_count(Stack *, StackVersion, t_u32 count); // Remove an error at the top of the given version of the stack. SubtreeArray ts_stack_pop_error(Stack *, StackVersion); diff --git a/parser/src/subtree.c b/parser/src/subtree.c index f1088564..75c7a8b0 100644 --- a/parser/src/subtree.c +++ b/parser/src/subtree.c @@ -1,13 +1,11 @@ #include #include -#include -#include #include #include #include +#include "me/types.h" #include "./array.h" -#include "./error_costs.h" #include "./language.h" #include "./length.h" #include "./subtree.h" @@ -86,7 +84,7 @@ void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest) { dest->contents = mem_alloc_array(self.capacity, sizeof(Subtree)); memcpy(dest->contents, self.contents, self.size * sizeof(Subtree)); - for (uint32_t i = 0; i < self.size; i++) + for (t_u32 i = 0; i < self.size; i++) { ts_subtree_retain(dest->contents[i]); } @@ -95,7 +93,7 @@ void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest) void ts_subtree_array_clear(SubtreePool *pool, SubtreeArray *self) { - for (uint32_t i = 0; i < self->size; i++) + for (t_u32 i = 0; i < self->size; i++) { ts_subtree_release(pool, self->contents[i]); } @@ -129,7 +127,7 @@ void ts_subtree_array_remove_trailing_extras(SubtreeArray *self, SubtreeArray *d void ts_subtree_array_reverse(SubtreeArray *self) { - for (uint32_t i = 0, limit = self->size / 2; i < limit; i++) + for (t_u32 i = 0, limit = self->size / 2; i < limit; i++) { size_t reverse_index = self->size - 1 - i; Subtree swap = self->contents[i]; @@ -140,7 +138,7 @@ void ts_subtree_array_reverse(SubtreeArray *self) // SubtreePool -SubtreePool ts_subtree_pool_new(uint32_t capacity) +SubtreePool ts_subtree_pool_new(t_u32 capacity) { SubtreePool self = {array_new(), array_new()}; array_reserve(&self.free_trees, capacity); @@ -187,13 +185,13 @@ static void ts_subtree_pool_free(SubtreePool *self, SubtreeHeapData *tree) // Subtree -static inline bool ts_subtree_can_inline(Length padding, Length size, uint32_t lookahead_bytes) +static inline bool ts_subtree_can_inline(Length padding, Length size, t_u32 lookahead_bytes) { 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, uint32_t lookahead_bytes, +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) { @@ -266,7 +264,7 @@ void ts_subtree_set_symbol(MutableSubtree *self, TSSymbol symbol, const TSLangua } } -Subtree ts_subtree_new_error(SubtreePool *pool, int32_t lookahead_char, Length padding, Length size, uint32_t bytes_scanned, +Subtree ts_subtree_new_error(SubtreePool *pool, t_i32 lookahead_char, Length padding, Length size, t_u32 bytes_scanned, TSStateId parse_state, const TSLanguage *language) { Subtree result = @@ -288,7 +286,7 @@ MutableSubtree ts_subtree_clone(Subtree self) SubtreeHeapData *result = (SubtreeHeapData *)&new_children[self.ptr->child_count]; if (self.ptr->child_count > 0) { - for (uint32_t i = 0; i < self.ptr->child_count; i++) + for (t_u32 i = 0; i < self.ptr->child_count; i++) { ts_subtree_retain(new_children[i]); } @@ -384,7 +382,7 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu } } - for (uint32_t i = 0; i < tree.ptr->child_count; i++) + for (t_u32 i = 0; i < tree.ptr->child_count; i++) { Subtree child = ts_subtree_children(tree)[i]; if (ts_subtree_child_count(child) > 0 && child.ptr->ref_count == 1) @@ -410,12 +408,12 @@ void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *langua self.ptr->has_external_scanner_state_change = false; self.ptr->dynamic_precedence = 0; - uint32_t structural_index = 0; + t_u32 structural_index = 0; const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id); - uint32_t lookahead_end_byte = 0; + t_u32 lookahead_end_byte = 0; const Subtree *children = ts_subtree_children(self); - for (uint32_t i = 0; i < self.ptr->child_count; i++) + for (t_u32 i = 0; i < self.ptr->child_count; i++) { Subtree child = children[i]; @@ -439,7 +437,7 @@ void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *langua self.ptr->size = length_add(self.ptr->size, ts_subtree_total_size(child)); } - uint32_t child_lookahead_end_byte = self.ptr->padding.bytes + self.ptr->size.bytes + ts_subtree_lookahead_bytes(child); + t_u32 child_lookahead_end_byte = self.ptr->padding.bytes + self.ptr->size.bytes + ts_subtree_lookahead_bytes(child); if (child_lookahead_end_byte > lookahead_end_byte) { lookahead_end_byte = child_lookahead_end_byte; @@ -450,7 +448,7 @@ void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *langua self.ptr->error_cost += ts_subtree_error_cost(child); } - uint32_t grandchild_count = ts_subtree_child_count(child); + t_u32 grandchild_count = ts_subtree_child_count(child); if (self.ptr->symbol == ts_builtin_sym_error || self.ptr->symbol == ts_builtin_sym_error_repeat) { if (!ts_subtree_extra(child) && !(ts_subtree_is_error(child) && grandchild_count == 0)) @@ -552,7 +550,7 @@ MutableSubtree ts_subtree_new_node(TSSymbol symbol, SubtreeArray *children, unsi if (children->capacity * sizeof(Subtree) < new_byte_size) { children->contents = mem_realloc(children->contents, new_byte_size); - children->capacity = (uint32_t)(new_byte_size / sizeof(Subtree)); + children->capacity = (t_u32)(new_byte_size / sizeof(Subtree)); } SubtreeHeapData *data = (SubtreeHeapData *)&children->contents[children->size]; @@ -591,7 +589,7 @@ Subtree ts_subtree_new_error_node(SubtreeArray *children, bool extra, const TSLa // // This node is treated as 'extra'. Its children are prevented from having // having any effect on the parse state. -Subtree ts_subtree_new_missing_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, uint32_t lookahead_bytes, +Subtree ts_subtree_new_missing_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, t_u32 lookahead_bytes, const TSLanguage *language) { Subtree result = ts_subtree_new_leaf(pool, symbol, padding, length_zero(), lookahead_bytes, 0, false, false, false, language); @@ -611,7 +609,7 @@ void ts_subtree_retain(Subtree self) if (self.data.is_inline) return; assert(self.ptr->ref_count > 0); - (*(uint32_t *)(&self.ptr->ref_count))++; + (*(t_u32 *)(&self.ptr->ref_count))++; assert(self.ptr->ref_count != 0); } @@ -622,7 +620,7 @@ void ts_subtree_release(SubtreePool *pool, Subtree self) array_clear(&pool->tree_stack); assert(self.ptr->ref_count > 0); - if (--(*(uint32_t *)(&self.ptr->ref_count)) == 0) + if (--(*(t_u32 *)(&self.ptr->ref_count)) == 0) { array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self)); } @@ -633,13 +631,13 @@ void ts_subtree_release(SubtreePool *pool, Subtree self) if (tree.ptr->child_count > 0) { Subtree *children = ts_subtree_children(tree); - for (uint32_t i = 0; i < tree.ptr->child_count; i++) + for (t_u32 i = 0; i < tree.ptr->child_count; i++) { Subtree child = children[i]; if (child.data.is_inline) continue; assert(child.ptr->ref_count > 0); - if (--(*(uint32_t *)(&child.ptr->ref_count)) == 0) + if (--(*(t_u32 *)(&child.ptr->ref_count)) == 0) { array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(child)); } @@ -682,7 +680,7 @@ int ts_subtree_compare(Subtree left, Subtree right, SubtreePool *pool) return result; } - for (uint32_t i = ts_subtree_child_count(left); i > 0; i--) + for (t_u32 i = ts_subtree_child_count(left); i > 0; i--) { Subtree left_child = ts_subtree_children(left)[i - 1]; Subtree right_child = ts_subtree_children(right)[i - 1]; @@ -736,8 +734,8 @@ Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool Length size = ts_subtree_size(*entry.tree); Length padding = ts_subtree_padding(*entry.tree); Length total_size = length_add(padding, size); - uint32_t lookahead_bytes = ts_subtree_lookahead_bytes(*entry.tree); - uint32_t end_byte = total_size.bytes + lookahead_bytes; + t_u32 lookahead_bytes = ts_subtree_lookahead_bytes(*entry.tree); + t_u32 end_byte = total_size.bytes + lookahead_bytes; if (edit.start.bytes > end_byte || (is_noop && edit.start.bytes == end_byte)) continue; @@ -814,7 +812,7 @@ Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool *entry.tree = ts_subtree_from_mut(result); Length child_left, child_right = length_zero(); - for (uint32_t i = 0, n = ts_subtree_child_count(*entry.tree); i < n; i++) + for (t_u32 i = 0, n = ts_subtree_child_count(*entry.tree); i < n; i++) { Subtree *child = &ts_subtree_children(*entry.tree)[i]; Length child_size = ts_subtree_total_size(*child); @@ -874,7 +872,7 @@ Subtree ts_subtree_last_external_token(Subtree tree) return NULL_SUBTREE; while (tree.ptr->child_count > 0) { - for (uint32_t i = tree.ptr->child_count - 1; i + 1 > 0; i--) + for (t_u32 i = tree.ptr->child_count - 1; i + 1 > 0; i--) { Subtree child = ts_subtree_children(tree)[i]; if (ts_subtree_has_external_tokens(child)) @@ -887,7 +885,7 @@ Subtree ts_subtree_last_external_token(Subtree tree) return tree; } -static size_t ts_subtree__write_char_to_string(char *str, size_t n, int32_t chr) +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"); @@ -981,8 +979,8 @@ static size_t ts_subtree__write_to_string(Subtree self, char *string, size_t lim const TSFieldMapEntry *field_map, *field_map_end; ts_language_field_map(language, self.ptr->production_id, &field_map, &field_map_end); - uint32_t structural_child_index = 0; - for (uint32_t i = 0; i < self.ptr->child_count; i++) + 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)) @@ -1029,11 +1027,11 @@ char *ts_subtree_string(Subtree self, TSSymbol alias_symbol, bool alias_is_named /* -void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset, const TSLanguage *language, TSSymbol alias_symbol, FILE *f) +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; - uint32_t end_offset = start_offset + ts_subtree_total_bytes(*self); + 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, "\""); @@ -1064,9 +1062,9 @@ void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset, con fprintf(f, "\"]\n"); - uint32_t child_start_offset = start_offset; - uint32_t child_info_offset = language->max_alias_sequence_length * ts_subtree_production_id(*self); - for (uint32_t i = 0, n = ts_subtree_child_count(*self); i < n; i++) + 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; diff --git a/parser/src/subtree.h b/parser/src/subtree.h index a7469fe2..c74c86de 100644 --- a/parser/src/subtree.h +++ b/parser/src/subtree.h @@ -1,8 +1,9 @@ #ifndef TREE_SITTER_SUBTREE_H_ #define TREE_SITTER_SUBTREE_H_ + +#include "me/types.h" #include "./array.h" -#include "./error_costs.h" #include "./length.h" #include "./parser.h" #include "api.h" @@ -31,7 +32,7 @@ typedef struct ExternalScannerState char *long_data; char short_data[24]; }; - uint32_t length; + t_u32 length; } ExternalScannerState; // A compact representation of a subtree. @@ -56,19 +57,19 @@ typedef struct SubtreeInlineData SubtreeInlineData; bool is_keyword : 1; #define SUBTREE_SIZE \ - uint8_t padding_columns; \ - uint8_t padding_rows : 4; \ - uint8_t lookahead_bytes : 4; \ - uint8_t padding_bytes; \ - uint8_t size_bytes; + 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 { - uint16_t parse_state; - uint8_t symbol; + t_u16 parse_state; + t_u8 symbol; SUBTREE_BITS bool unused : 1; bool is_inline : 1; @@ -80,8 +81,8 @@ struct SubtreeInlineData struct SubtreeInlineData { SUBTREE_SIZE - uint16_t parse_state; - uint8_t symbol; + t_u16 parse_state; + t_u8 symbol; SUBTREE_BITS bool unused : 1; bool is_inline : 1; @@ -94,8 +95,8 @@ struct SubtreeInlineData { bool is_inline : 1; SUBTREE_BITS - uint8_t symbol; - uint16_t parse_state; + t_u8 symbol; + t_u16 parse_state; SUBTREE_SIZE }; @@ -111,12 +112,12 @@ struct SubtreeInlineData // the inline representation. typedef struct SubtreeHeapData { - volatile uint32_t ref_count; + volatile t_u32 ref_count; Length padding; Length size; - uint32_t lookahead_bytes; - uint32_t error_cost; - uint32_t child_count; + t_u32 lookahead_bytes; + t_u32 error_cost; + t_u32 child_count; TSSymbol symbol; TSStateId parse_state; @@ -136,12 +137,12 @@ typedef struct SubtreeHeapData // Non-terminal subtrees (`child_count > 0`) struct { - uint32_t visible_child_count; - uint32_t named_child_count; - uint32_t visible_descendant_count; - int32_t dynamic_precedence; - uint16_t repeat_depth; - uint16_t production_id; + t_u32 visible_child_count; + t_u32 named_child_count; + t_u32 visible_descendant_count; + t_i32 dynamic_precedence; + t_u16 repeat_depth; + t_u16 production_id; struct { TSSymbol symbol; @@ -153,7 +154,7 @@ typedef struct SubtreeHeapData ExternalScannerState external_scanner_state; // Error terminal subtrees (`child_count == 0 && symbol == ts_builtin_sym_error`) - int32_t lookahead_char; + t_i32 lookahead_char; }; } SubtreeHeapData; @@ -189,20 +190,20 @@ void ts_subtree_array_delete(SubtreePool *, SubtreeArray *); void ts_subtree_array_remove_trailing_extras(SubtreeArray *, SubtreeArray *); void ts_subtree_array_reverse(SubtreeArray *); -SubtreePool ts_subtree_pool_new(uint32_t capacity); +SubtreePool ts_subtree_pool_new(t_u32 capacity); void ts_subtree_pool_delete(SubtreePool *); -Subtree ts_subtree_new_leaf(SubtreePool *, TSSymbol, Length, Length, uint32_t, TSStateId, bool, bool, bool, const TSLanguage *); -Subtree ts_subtree_new_error(SubtreePool *, int32_t, Length, Length, uint32_t, TSStateId, const TSLanguage *); +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 *, unsigned, const TSLanguage *); Subtree ts_subtree_new_error_node(SubtreeArray *, bool, const TSLanguage *); -Subtree ts_subtree_new_missing_leaf(SubtreePool *, TSSymbol, Length, uint32_t, 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 *, uint32_t, 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 *); @@ -246,7 +247,7 @@ static inline TSStateId ts_subtree_parse_state(Subtree self) { return SUBTREE_GET(self, parse_state); } -static inline uint32_t ts_subtree_lookahead_bytes(Subtree self) +static inline t_u32 ts_subtree_lookahead_bytes(Subtree self) { return SUBTREE_GET(self, lookahead_bytes); } @@ -255,7 +256,7 @@ static inline uint32_t ts_subtree_lookahead_bytes(Subtree self) // Get the size needed to store a heap-allocated subtree with the given // number of children. -static inline size_t ts_subtree_alloc_size(uint32_t child_count) +static inline size_t ts_subtree_alloc_size(t_u32 child_count) { return child_count * sizeof(Subtree) + sizeof(SubtreeHeapData); } @@ -325,32 +326,32 @@ static inline Length ts_subtree_total_size(Subtree self) return length_add(ts_subtree_padding(self), ts_subtree_size(self)); } -static inline uint32_t ts_subtree_total_bytes(Subtree self) +static inline t_u32 ts_subtree_total_bytes(Subtree self) { return ts_subtree_total_size(self).bytes; } -static inline uint32_t 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; } -static inline uint32_t 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; } -static inline uint32_t 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; } -static inline uint32_t 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; } -static inline uint32_t ts_subtree_visible_child_count(Subtree self) +static inline t_u32 ts_subtree_visible_child_count(Subtree self) { if (ts_subtree_child_count(self) > 0) { @@ -362,7 +363,7 @@ static inline uint32_t ts_subtree_visible_child_count(Subtree self) } } -static inline uint32_t ts_subtree_error_cost(Subtree self) +static inline t_u32 ts_subtree_error_cost(Subtree self) { if (ts_subtree_missing(self)) { @@ -374,12 +375,12 @@ static inline uint32_t ts_subtree_error_cost(Subtree self) } } -static inline int32_t 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; } -static inline uint16_t ts_subtree_production_id(Subtree self) +static inline t_u16 ts_subtree_production_id(Subtree self) { if (ts_subtree_child_count(self) > 0) { diff --git a/parser/src/tree.c b/parser/src/tree.c index ac2d4c9f..c1f48e15 100644 --- a/parser/src/tree.c +++ b/parser/src/tree.c @@ -1,5 +1,7 @@ #define _POSIX_C_SOURCE 200112L + +#include "me/types.h" #include "./tree.h" #include "./array.h" #include "./length.h" @@ -41,7 +43,7 @@ TSNode ts_tree_root_node(const TSTree *self) return ts_node_new(self, &self->root, ts_subtree_padding(self->root), 0); } -TSNode ts_tree_root_node_with_offset(const TSTree *self, uint32_t offset_bytes, TSPoint offset_extent) +TSNode ts_tree_root_node_with_offset(const TSTree *self, t_u32 offset_bytes, TSPoint offset_extent) { Length offset = {offset_bytes, offset_extent}; return ts_node_new(self, &self->root, length_add(offset, ts_subtree_padding(self->root)), 0); @@ -97,7 +99,7 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit) ts_subtree_pool_delete(&pool); } -TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length) +TSRange *ts_tree_included_ranges(const TSTree *self, t_u32 *length) { *length = self->included_range_count; TSRange *ranges = mem_alloc_array(self->included_range_count, sizeof(TSRange)); diff --git a/parser/src/tree.h b/parser/src/tree.h index 3692adc9..d4b8487d 100644 --- a/parser/src/tree.h +++ b/parser/src/tree.h @@ -1,6 +1,8 @@ #ifndef TREE_SITTER_TREE_H_ #define TREE_SITTER_TREE_H_ + +#include "me/types.h" #include "./subtree.h" typedef struct ParentCacheEntry diff --git a/parser/src/unicode.h b/parser/src/unicode.h index f2d623ad..09778477 100644 --- a/parser/src/unicode.h +++ b/parser/src/unicode.h @@ -1,35 +1,35 @@ #ifndef TREE_SITTER_UNICODE_H_ #define TREE_SITTER_UNICODE_H_ -#include +#include "me/types.h" -static const int32_t TS_DECODE_ERROR = -1; +static const t_i32 TS_DECODE_ERROR = -1; // These functions read one unicode code point from the given string, // returning the number of bytes consumed. -typedef uint32_t (*UnicodeDecodeFunction)(const uint8_t *string, uint32_t length, int32_t *code_point); +typedef t_u32 (*UnicodeDecodeFunction)(const t_u8 *string, t_u32 length, t_i32 *code_point); -static inline uint32_t ts_decode_ascii(const uint8_t *string, uint32_t length, int32_t *code_point) +static inline t_u32 ts_decode_ascii(const t_u8 *string, t_u32 length, t_i32 *code_point) { (void)(length); *code_point = 0; - *(uint8_t *)code_point = *string; + *(t_u8 *)code_point = *string; return (1); } -static inline uint32_t ts_decode_utf8(const uint8_t *string, uint32_t length, int32_t *code_point) +static inline t_u32 ts_decode_utf8(const t_u8 *string, t_u32 length, t_i32 *code_point) { return (ts_decode_ascii(string, length, code_point)); - // uint32_t i = 0; + // t_u32 i = 0; // U8_NEXT(string, i, length, *code_point); // return i; } -static inline uint32_t ts_decode_utf16(const uint8_t *string, uint32_t length, int32_t *code_point) +static inline t_u32 ts_decode_utf16(const t_u8 *string, t_u32 length, t_i32 *code_point) { return (ts_decode_ascii(string, length, code_point)); - // uint32_t i = 0; - // U16_NEXT(((uint16_t *)string), i, length, *code_point); + // t_u32 i = 0; + // U16_NEXT(((t_u16 *)string), i, length, *code_point); // return i * 2; }