diff --git a/parser/Filelist.parser.mk b/parser/Filelist.parser.mk index d05611c2..0c6b559a 100644 --- a/parser/Filelist.parser.mk +++ b/parser/Filelist.parser.mk @@ -1,9 +1,11 @@ SRC_FILES = \ create_language \ external_scanner_state \ +external_scanner_state2 \ input \ language \ -length \ +length/length_funcs1 \ +length/length_funcs2 \ lex \ lexer \ node/node_child \ @@ -34,7 +36,8 @@ stack/stack_node \ stack/stack_summary \ stack/stack_version \ subtree \ -tree \ +tree/tree_funcs1 \ +tree/tree_funcs2 \ GEN_FILES = \ \ diff --git a/parser/src/external_scanner_state.c b/parser/src/external_scanner_state.c deleted file mode 100644 index deb7a629..00000000 --- a/parser/src/external_scanner_state.c +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* external_scanner_state.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/08/24 13:55:33 by maiboyer #+# #+# */ -/* Updated: 2024/08/24 14:01:34 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "parser/external_scanner_state.h" -#include "parser/subtree.h" - -void ts_external_scanner_state_init(ExternalScannerState *self, const t_u8 *data, t_u32 length) -{ - self->length = length; - self->long_data = mem_alloc(length); - mem_copy(self->long_data, data, length); -} - -ExternalScannerState ts_external_scanner_state_copy(const ExternalScannerState *self) -{ - ExternalScannerState result = *self; - result.long_data = mem_alloc(self->length); - mem_copy(result.long_data, self->long_data, self->length); - return result; -} - -void ts_external_scanner_state_delete(ExternalScannerState *self) -{ - mem_free(self->long_data); -} - -const t_u8 *ts_external_scanner_state_data(const ExternalScannerState *self) -{ - return (const t_u8 *)self->long_data; -} - -bool ts_external_scanner_state_eq(const ExternalScannerState *self, const t_u8 *buffer, t_u32 length) -{ - return self->length == length && mem_compare(ts_external_scanner_state_data(self), buffer, length); -} - -const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self) -{ -#ifdef static -# undef static -# define __REAPPLY_STATIC -#endif - static const ExternalScannerState empty_state = {NULL, .length = 0}; -#ifdef __REAPPLY_STATIC -# define static -#endif - if (self && self->has_external_tokens && self->child_count == 0) - { - return &self->external_scanner_state; - } - else - { - return &empty_state; - } -} - -bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other) -{ - const ExternalScannerState *state_self = ts_subtree_external_scanner_state(self); - const ExternalScannerState *state_other = ts_subtree_external_scanner_state(other); - return ts_external_scanner_state_eq(state_self, ts_external_scanner_state_data(state_other), state_other->length); -} diff --git a/parser/src/input.c b/parser/src/input.c deleted file mode 100644 index d9c55c35..00000000 --- a/parser/src/input.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "parser/input.h" -#include "me/types.h" - -t_u32 ts_decode_ascii(const t_u8 *string, t_u32 length, t_i32 *code_point) -{ - if (string == NULL || length == 0 || code_point == 0) - return (0); - *code_point = 0; - *(t_u8 *)code_point = *string; - return (1); -} diff --git a/parser/src/language.c b/parser/src/language.c deleted file mode 100644 index a68239e5..00000000 --- a/parser/src/language.c +++ /dev/null @@ -1,277 +0,0 @@ -#include "parser/language.h" -#include "me/types.h" -#include "parser/api.h" -#include "parser/parser.h" -#include -#include - -t_u32 ts_language_symbol_count(const TSLanguage *self) -{ - return (self->symbol_count + self->alias_count); -} - -t_u32 ts_language_state_count(const TSLanguage *self) -{ - return (self->state_count); -} - -t_u32 ts_language_version(const TSLanguage *self) -{ - return (self->version); -} - -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) -{ - t_u32 action_index; - const TSParseActionEntry *entry; - - 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); - action_index = ts_language_lookup(self, state, symbol); - 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]); -} - -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) -{ - t_u32 count; - const TSParseAction *actions; - TSParseAction action; - - if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) - { - return (0); - } - else if (symbol < self->token_count) - { - - actions = ts_language_actions(self, state, symbol, &count); - if (count > 0) - { - 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)); -} - -t_const_str 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, t_const_str string, t_u32 length, bool is_named) -{ - TSSymbolMetadata metadata; - t_u16 count; - TSSymbol i; - t_const_str symbol_name; - if (!strncmp(string, "ERROR", length)) - return (ts_builtin_sym_error); - count = (t_u16)ts_language_symbol_count(self); - i = 0; - while (i < count) - { - metadata = ts_language_symbol_metadata(self, i); - if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) - { - i++; - continue; - } - symbol_name = self->symbol_names[i]; - if (!strncmp(symbol_name, string, length) && !symbol_name[length]) - return (self->public_symbol_map[i]); - i++; - } - return (0); -} - -TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol) -{ - TSSymbolMetadata metadata; - - metadata = ts_language_symbol_metadata(self, symbol); - if (metadata.named && metadata.visible) - return (TSSymbolTypeRegular); - else if (metadata.visible) - return (TSSymbolTypeAnonymous); - else - return (TSSymbolTypeAuxiliary); -} - -t_const_str ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id) -{ - t_u32 count; - - 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, t_const_str name, t_u32 name_length) -{ - t_u16 count; - TSSymbol i; - t_i32 res; - - count = (t_u16)ts_language_field_count(self); - i = 1; - while (i < count + 1) - { - res = strncmp(name, self->field_names[i], name_length); - if (res == 0) - { - if (self->field_names[i][name_length] == 0) - return (i); - } - else if (res == -1) - return (0); - i++; - } - return (0); -} - -bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol) -{ - return (0 < symbol && symbol < self->external_token_count + 1); -} - -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); - *count = entry.action_count; - return (entry.actions); -} - -bool ts_language_has_reduce_action(const TSLanguage *self, TSStateId state, TSSymbol symbol) -{ - TableEntry entry; - - ts_language_table_entry(self, state, symbol, &entry); - return (entry.action_count > 0 && entry.actions[0].type == TSParseActionTypeReduce); -} - -// Lookup the table value for a given symbol and state. -// -// For non-terminal symbols, the table value represents a successor state. -// For terminal symbols, it represents an index in the actions table. -// 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. -t_u16 ts_language_lookup(const TSLanguage *self, TSStateId state, TSSymbol symbol) -{ - if (state >= self->large_state_count) - return (me_abort("we got a small parse table, which isn't supported"), -1); - return (self->parse_table[state * self->symbol_count + symbol]); -} - -bool ts_language_has_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol) -{ - return (ts_language_lookup(self, state, symbol) != 0); -} - -const bool *ts_language_enabled_external_tokens(const TSLanguage *self, t_u32 external_scanner_state) -{ - if (external_scanner_state == 0) - return (NULL); - else - return (self->external_scanner.states + self->external_token_count * external_scanner_state); -} - -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); -} - -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); -} - -void ts_language_field_map(const TSLanguage *self, t_u32 production_id, const TSFieldMapEntry **start, const TSFieldMapEntry **end) -{ - TSFieldMapSlice slice; - - if (self->field_count == 0) - { - *start = NULL; - *end = NULL; - return; - } - - slice = self->field_map_slices[production_id]; - *start = &self->field_map_entries[slice.index]; - *end = &self->field_map_entries[slice.index] + slice.length; -} - -void ts_language_aliases_for_symbol(const TSLanguage *self, TSSymbol original_symbol, const TSSymbol **start, const TSSymbol **end) -{ - - t_u32 idx; - TSSymbol symbol; - t_u16 count; - - *start = &self->public_symbol_map[original_symbol]; - *end = *start + 1; - idx = 0; - while (true) - { - symbol = self->alias_map[idx++]; - if (symbol == 0 || symbol > original_symbol) - break; - count = self->alias_map[idx++]; - if (symbol == original_symbol) - { - *start = &self->alias_map[idx]; - *end = &self->alias_map[idx + count]; - break; - } - idx += count; - } -} diff --git a/parser/src/language/language_field.c b/parser/src/language/language_field.c new file mode 100644 index 00000000..ef8724b6 --- /dev/null +++ b/parser/src/language/language_field.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* language_field.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:47:24 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:48:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/str/str.h" +#include "me/types.h" +#include "parser/api.h" +#include "parser/language.h" +#include "parser/parser.h" + +t_const_str ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id) +{ + t_u32 count; + + 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, + t_const_str name, t_u32 name_length) +{ + t_u16 count; + TSSymbol i; + bool res; + + count = (t_u16)ts_language_field_count(self); + i = 1; + while (i < count + 1) + { + if (str_n_compare(name, self->field_names[i], name_length) + && self->field_names[i][name_length] == 0) + return (i); + i++; + } + return (0); +} + +void ts_language_field_map(const TSLanguage *self, t_u32 production_id, + const TSFieldMapEntry **start, const TSFieldMapEntry **end) +{ + TSFieldMapSlice slice; + + if (self->field_count == 0) + { + *start = NULL; + *end = NULL; + return ; + } + slice = self->field_map_slices[production_id]; + *start = &self->field_map_entries[slice.index]; + *end = &self->field_map_entries[slice.index] + slice.length; +} diff --git a/parser/src/language/language_getters.c b/parser/src/language/language_getters.c new file mode 100644 index 00000000..8bedd206 --- /dev/null +++ b/parser/src/language/language_getters.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* language_getters.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:42:37 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:43:04 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "parser/language.h" + +t_u32 ts_language_symbol_count(const TSLanguage *self) +{ + return (self->symbol_count + self->alias_count); +} + +t_u32 ts_language_state_count(const TSLanguage *self) +{ + return (self->state_count); +} + +t_u32 ts_language_version(const TSLanguage *self) +{ + return (self->version); +} + +t_u32 ts_language_field_count(const TSLanguage *self) +{ + return (self->field_count); +} diff --git a/parser/src/language/language_getters2.c b/parser/src/language/language_getters2.c new file mode 100644 index 00000000..86d01e79 --- /dev/null +++ b/parser/src/language/language_getters2.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* language_getters2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:50:29 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:54:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "parser/api.h" +#include "parser/language.h" +#include "parser/parser.h" + +bool ts_language_has_actions(const TSLanguage *self, TSStateId state, + TSSymbol symbol) +{ + return (ts_language_lookup(self, state, symbol) != 0); +} + +const bool *ts_language_enabled_external_tokens(const TSLanguage *self, + t_u32 external_scanner_state) +{ + if (external_scanner_state == 0) + return (NULL); + else + return (self->external_scanner.states \ + + self->external_token_count * external_scanner_state); +} + +const TSSymbol *ts_language_alias_sequence(const TSLanguage *self, + t_u32 production_id) +{ + if (production_id) + return (&self->alias_sequences[production_id \ + * self->max_alias_sequence_length]); + else + return (NULL); +} + +TSSymbol ts_language_alias_at(const TSLanguage *self, t_u32 production_id, + t_u32 child_index) +{ + if (production_id) + return (self->alias_sequences[production_id \ + * self->max_alias_sequence_length + child_index]); + else + return (0); +} diff --git a/parser/src/language/language_misc.c b/parser/src/language/language_misc.c new file mode 100644 index 00000000..a849b8a9 --- /dev/null +++ b/parser/src/language/language_misc.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* language_misc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:51:00 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:55:29 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "parser/api.h" +#include "parser/language.h" +#include "parser/parser.h" + +void ts_language_table_entry(const TSLanguage *self, TSStateId state, + TSSymbol symbol, TableEntry *result) +{ + t_u32 action_index; + const TSParseActionEntry *entry; + + if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) + { + result->action_count = 0; + result->is_reusable = false; + result->actions = NULL; + } + else + { + action_index = ts_language_lookup(self, state, symbol); + entry = &self->parse_actions[action_index]; + result->action_count = entry->entry.count; + result->is_reusable = entry->entry.reusable; + result->actions = (const TSParseAction *)(entry + 1); + } +} + +TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, + TSSymbol symbol) +{ + t_u32 count; + const TSParseAction *actions; + TSParseAction action; + + if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) + { + return (0); + } + else if (symbol < self->token_count) + { + actions = ts_language_actions(self, state, symbol, &count); + if (count > 0) + { + action = actions[count - 1]; + if (action.type == TSParseActionTypeShift) + { + if (action.shift.extra) + return (state); + return (action.shift.state); + } + } + return (0); + } + else + return (ts_language_lookup(self, state, symbol)); +} + +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); + *count = entry.action_count; + return (entry.actions); +} + +bool ts_language_has_reduce_action(const TSLanguage *self, TSStateId state, + TSSymbol symbol) +{ + TableEntry entry; + + ts_language_table_entry(self, state, symbol, &entry); + return (entry.action_count > 0 + && entry.actions[0].type == TSParseActionTypeReduce); +} + +// Lookup the table value for a given symbol and state. +// +// For non-terminal symbols, the table value represents a successor state. +// For terminal symbols, it represents an index in the actions table. +// 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. +t_u16 ts_language_lookup(const TSLanguage *self, TSStateId state, + TSSymbol symbol) +{ + if (state >= self->large_state_count) + return (me_abort("we got a small parse table, which isn't supported"), + -1); + return (self->parse_table[state * self->symbol_count + symbol]); +} diff --git a/parser/src/language/language_symbol.c b/parser/src/language/language_symbol.c new file mode 100644 index 00000000..ee49e718 --- /dev/null +++ b/parser/src/language/language_symbol.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* language_symbol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:43:20 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:51:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/str/str.h" +#include "me/types.h" +#include "parser/api.h" +#include "parser/language.h" +#include "parser/parser.h" + +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]); +} + +t_const_str 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, + t_const_str string, t_u32 length, bool is_named) +{ + TSSymbolMetadata metadata; + t_u16 count; + TSSymbol i; + t_const_str symbol_name; + + if (str_n_compare(string, "ERROR", length)) + return (ts_builtin_sym_error); + count = (t_u16)ts_language_symbol_count(self); + i = 0; + while (i < count) + { + metadata = ts_language_symbol_metadata(self, i); + if ((!metadata.visible && !metadata.supertype) + || metadata.named != is_named) + { + i++; + continue ; + } + symbol_name = self->symbol_names[i]; + if (str_n_compare(symbol_name, string, length) && !symbol_name[length]) + return (self->public_symbol_map[i]); + i++; + } + return (0); +} + +TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol) +{ + TSSymbolMetadata metadata; + + metadata = ts_language_symbol_metadata(self, symbol); + if (metadata.named && metadata.visible) + return (TSSymbolTypeRegular); + else if (metadata.visible) + return (TSSymbolTypeAnonymous); + else + return (TSSymbolTypeAuxiliary); +} diff --git a/parser/src/language/language_symbol2.c b/parser/src/language/language_symbol2.c new file mode 100644 index 00000000..a20cb884 --- /dev/null +++ b/parser/src/language/language_symbol2.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* language_symbol2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:46:08 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:46:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "parser/api.h" +#include "parser/language.h" +#include "parser/parser.h" + +void ts_language_aliases_for_symbol(const TSLanguage *self, + TSSymbol original_symbol, const TSSymbol **start, const TSSymbol **end) +{ + t_u32 idx; + TSSymbol symbol; + t_u16 count; + + *start = &self->public_symbol_map[original_symbol]; + *end = *start + 1; + idx = 0; + while (true) + { + symbol = self->alias_map[idx++]; + if (symbol == 0 || symbol > original_symbol) + break ; + count = self->alias_map[idx++]; + if (symbol == original_symbol) + { + *start = &self->alias_map[idx]; + *end = &self->alias_map[idx + count]; + break ; + } + idx += count; + } +} + +bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol) +{ + return (0 < symbol && symbol < self->external_token_count + 1); +} diff --git a/parser/src/length.c b/parser/src/length.c deleted file mode 100644 index 9fe87bb4..00000000 --- a/parser/src/length.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "parser/length.h" -#include "parser/point.h" - -bool length_is_undefined(Length length) -{ - return (length.bytes == 0 && length.extent.column != 0); -} - -Length length_min(Length len1, Length len2) -{ - return ((len1.bytes < len2.bytes) ? len1 : len2); -} - -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); -} - -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); -} - -Length length_zero(void) -{ - return ((Length){0, {0, 0}}); -} - -Length length_saturating_sub(Length len1, Length len2) -{ - if (len1.bytes > len2.bytes) - return (length_sub(len1, len2)); - else - return (length_zero()); -} \ No newline at end of file diff --git a/parser/src/length/length_funcs1.c b/parser/src/length/length_funcs1.c new file mode 100644 index 00000000..84458579 --- /dev/null +++ b/parser/src/length/length_funcs1.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* length_funcs1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:38:43 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:53:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/length.h" +#include "parser/point.h" + +bool length_is_undefined(Length length) +{ + return (length.bytes == 0 && length.extent.column != 0); +} + +Length length_min(Length len1, Length len2) +{ + if (len1.bytes < len2.bytes) + return (len1); + return (len2); +} + +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); +} + +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); +} diff --git a/parser/src/length/length_funcs2.c b/parser/src/length/length_funcs2.c new file mode 100644 index 00000000..7455b4b6 --- /dev/null +++ b/parser/src/length/length_funcs2.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* length_funcs2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:38:43 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:39:17 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/length.h" +#include "parser/point.h" + +Length length_zero(void) +{ + return ((Length){0, {0, 0}}); +} + +Length length_saturating_sub(Length len1, Length len2) +{ + if (len1.bytes > len2.bytes) + return (length_sub(len1, len2)); + else + return (length_zero()); +} diff --git a/parser/src/create_language.c b/parser/src/misc/create_language.c similarity index 63% rename from parser/src/create_language.c rename to parser/src/misc/create_language.c index 6c3330de..a16fa405 100644 --- a/parser/src/create_language.c +++ b/parser/src/misc/create_language.c @@ -6,47 +6,42 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/25 16:13:52 by maiboyer #+# #+# */ -/* Updated: 2024/08/22 16:25:57 by maiboyer ### ########.fr */ +/* Updated: 2024/08/31 17:36:29 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "../static/headers/constants.h" #include "../static/headers/symbols.h" -#include "parser/parser.h" #include "me/types.h" +#include "parser/parser.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); +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, t_u8 *state); +void tree_sitter_sh_external_scanner_deserialize(void *ctx, \ + const t_u8 *state, t_u32 val); +void tree_sitter_sh_external_scanner_destroy(void *ctx); +void *tree_sitter_sh_external_scanner_create(void); -#ifdef static -# undef static -#endif - -// 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(TSLexer *lexer, TSStateId state); -t_u32 tree_sitter_sh_external_scanner_serialize(void *ctx, t_u8 *state); -void tree_sitter_sh_external_scanner_deserialize(void *ctx, const t_u8 *state, 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) +static struct ExternalScannerDefinition init_scanner(void) { return ((struct ExternalScannerDefinition){ create_external_scanner_states(), @@ -59,9 +54,9 @@ static struct ExternalScannerDefinition init_scanner(void) }); } -static void init_language(TSLanguage *language) +static void init_language(TSLanguage *language) { - static uint32_t empty_map[] = {0, 0 ,0}; + static uint32_t empty_map[] = {0, 0, 0}; language->parse_table = create_parse_table(); language->small_parse_table = (void *)empty_map; @@ -83,10 +78,10 @@ static void init_language(TSLanguage *language) language->external_scanner = init_scanner(); } -const TSLanguage *tree_sitter_sh(void) +const TSLanguage *tree_sitter_sh(void) { - static bool init = false; - static TSLanguage language = { + static bool init = false; + static TSLanguage language = { .version = LANGUAGE_VERSION, .symbol_count = SYMBOL_COUNT, .alias_count = ALIAS_COUNT, diff --git a/parser/src/misc/external_scanner_state.c b/parser/src/misc/external_scanner_state.c new file mode 100644 index 00000000..ed80b192 --- /dev/null +++ b/parser/src/misc/external_scanner_state.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* external_scanner_state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/24 13:55:33 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:38:04 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/external_scanner_state.h" +#include "parser/subtree.h" + +void ts_external_scanner_state_init(ExternalScannerState *self, + const t_u8 *data, t_u32 length) +{ + self->length = length; + self->long_data = mem_alloc(length); + mem_copy(self->long_data, data, length); +} + +ExternalScannerState ts_external_scanner_state_copy(\ + const ExternalScannerState *self) +{ + ExternalScannerState result; + + result = *self; + result.long_data = mem_alloc(self->length); + mem_copy(result.long_data, self->long_data, self->length); + return (result); +} + +void ts_external_scanner_state_delete(ExternalScannerState *self) +{ + mem_free(self->long_data); +} + +const t_u8 *ts_external_scanner_state_data(const ExternalScannerState *self) +{ + return ((const t_u8 *)self->long_data); +} + +bool ts_external_scanner_state_eq(const ExternalScannerState *self, + const t_u8 *buffer, t_u32 length) +{ + return (self->length == length + && mem_compare(ts_external_scanner_state_data(self), buffer, length)); +} diff --git a/parser/src/misc/external_scanner_state2.c b/parser/src/misc/external_scanner_state2.c new file mode 100644 index 00000000..49971181 --- /dev/null +++ b/parser/src/misc/external_scanner_state2.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* external_scanner_state2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:37:45 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:38:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/external_scanner_state.h" +#include "parser/subtree.h" + +const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self) +{ + static const ExternalScannerState empty_state = {NULL, .length = 0}; + + if (self && self->has_external_tokens && self->child_count == 0) + return (&self->external_scanner_state); + else + return (&empty_state); +} + +bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other) +{ + const ExternalScannerState *state_self = \ + ts_subtree_external_scanner_state(self); + const ExternalScannerState *state_other = \ + ts_subtree_external_scanner_state(other); + + return (ts_external_scanner_state_eq(state_self, + ts_external_scanner_state_data(state_other), state_other->length)); +} diff --git a/parser/src/misc/input.c b/parser/src/misc/input.c new file mode 100644 index 00000000..bd0dfa92 --- /dev/null +++ b/parser/src/misc/input.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:33:08 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:33:45 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/input.h" +#include "me/types.h" + +t_u32 ts_decode_ascii(const t_u8 *string, t_u32 length, t_i32 *code_point) +{ + if (string == NULL || length == 0 || code_point == 0) + return (0); + *code_point = 0; + *(t_u8 *)code_point = *string; + return (1); +} diff --git a/parser/src/misc/reduce_action.c b/parser/src/misc/reduce_action.c new file mode 100644 index 00000000..99641d93 --- /dev/null +++ b/parser/src/misc/reduce_action.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* reduce_action.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:33:11 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:33:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/reduce_action.h" + +void ts_reduce_action_set_add(ReduceActionSet *self, ReduceAction new_action) +{ + ReduceAction action; + t_u32 i; + + i = 0; + while (i < self->size) + { + action = self->contents[i]; + if (action.symbol == new_action.symbol \ + && action.count == new_action.count) + return ; + i++; + } + array_push(self, new_action); +} diff --git a/parser/src/reduce_action.c b/parser/src/reduce_action.c deleted file mode 100644 index ba607c40..00000000 --- a/parser/src/reduce_action.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "parser/reduce_action.h" - -void ts_reduce_action_set_add(ReduceActionSet *self, ReduceAction new_action) -{ - ReduceAction action; - t_u32 i; - - i = 0; - while (i < self->size) - { - action = self->contents[i]; - if (action.symbol == new_action.symbol && action.count == new_action.count) - return; - i++; - } - array_push(self, new_action); -} diff --git a/parser/src/tree.c b/parser/src/tree.c deleted file mode 100644 index c1571bd0..00000000 --- a/parser/src/tree.c +++ /dev/null @@ -1,46 +0,0 @@ -#define _POSIX_C_SOURCE 200112L - -#include "parser/tree.h" -#include "me/types.h" -#include "parser/api.h" -#include "parser/length.h" -#include "parser/subtree.h" - -TSTree *ts_tree_new(Subtree root, const TSLanguage *language) -{ - TSTree *result; - result = mem_alloc(sizeof(*result)); - result->root = root; - result->language = language; - return result; -} - -TSTree *ts_tree_copy(const TSTree *self) -{ - ts_subtree_retain(self->root); - return ts_tree_new(self->root, self->language); -} - -void ts_tree_delete(TSTree *self) -{ - if (self == NULL) - return; - ts_subtree_release(self->root); - mem_free(self); -} - -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, 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); -} - -const TSLanguage *ts_tree_language(const TSTree *self) -{ - return self->language; -} diff --git a/parser/src/tree/tree_funcs1.c b/parser/src/tree/tree_funcs1.c new file mode 100644 index 00000000..a972fc18 --- /dev/null +++ b/parser/src/tree/tree_funcs1.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tree_funcs1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:40:05 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:40:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/api.h" +#include "parser/subtree.h" +#include "parser/tree.h" + +TSTree *ts_tree_new(Subtree root, const TSLanguage *language) +{ + TSTree *result; + + result = mem_alloc(sizeof(*result)); + result->root = root; + result->language = language; + return (result); +} + +TSTree *ts_tree_copy(const TSTree *self) +{ + ts_subtree_retain(self->root); + return (ts_tree_new(self->root, self->language)); +} + +void ts_tree_delete(TSTree *self) +{ + if (self == NULL) + return ; + ts_subtree_release(self->root); + mem_free(self); +} diff --git a/parser/src/tree/tree_funcs2.c b/parser/src/tree/tree_funcs2.c new file mode 100644 index 00000000..6050905f --- /dev/null +++ b/parser/src/tree/tree_funcs2.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tree_funcs2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/31 17:40:17 by maiboyer #+# #+# */ +/* Updated: 2024/08/31 17:41:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/api.h" +#include "parser/subtree.h" +#include "parser/tree.h" + +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, t_u32 offset_bytes, + TSPoint offset_extent) +{ + Length offset; + + offset = (Length){offset_bytes, offset_extent}; + return (ts_node_new(self, &self->root, length_add(offset, + ts_subtree_padding(self->root)), 0)); +} + +const TSLanguage *ts_tree_language(const TSTree *self) +{ + return (self->language); +}