Refactoring some stuff in the parser lib, moving functions out of headers
This commit is contained in:
parent
4580d68951
commit
fb3a2d94a0
19 changed files with 522 additions and 608 deletions
|
|
@ -1,12 +1,13 @@
|
|||
#include "./language.h"
|
||||
#include "./api.h"
|
||||
#include "me/types.h"
|
||||
#include "parser.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
const TSLanguage *ts_language_copy(const TSLanguage *self)
|
||||
{
|
||||
return self;
|
||||
return (self);
|
||||
}
|
||||
|
||||
void ts_language_delete(const TSLanguage *self)
|
||||
|
|
@ -16,26 +17,29 @@ void ts_language_delete(const TSLanguage *self)
|
|||
|
||||
t_u32 ts_language_symbol_count(const TSLanguage *self)
|
||||
{
|
||||
return self->symbol_count + self->alias_count;
|
||||
return (self->symbol_count + self->alias_count);
|
||||
}
|
||||
|
||||
t_u32 ts_language_state_count(const TSLanguage *self)
|
||||
{
|
||||
return self->state_count;
|
||||
return (self->state_count);
|
||||
}
|
||||
|
||||
t_u32 ts_language_version(const TSLanguage *self)
|
||||
{
|
||||
return self->version;
|
||||
return (self->version);
|
||||
}
|
||||
|
||||
t_u32 ts_language_field_count(const TSLanguage *self)
|
||||
{
|
||||
return self->field_count;
|
||||
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;
|
||||
|
|
@ -45,8 +49,8 @@ void ts_language_table_entry(const TSLanguage *self, TSStateId state, TSSymbol s
|
|||
else
|
||||
{
|
||||
assert(symbol < self->token_count);
|
||||
t_u32 action_index = ts_language_lookup(self, state, symbol);
|
||||
const TSParseActionEntry *entry = &self->parse_actions[action_index];
|
||||
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);
|
||||
|
|
@ -56,182 +60,252 @@ void ts_language_table_entry(const TSLanguage *self, TSStateId state, TSSymbol s
|
|||
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *self, TSSymbol symbol)
|
||||
{
|
||||
if (symbol == ts_builtin_sym_error)
|
||||
{
|
||||
return (TSSymbolMetadata){.visible = true, .named = true};
|
||||
}
|
||||
return ((TSSymbolMetadata){.visible = true, .named = true});
|
||||
else if (symbol == ts_builtin_sym_error_repeat)
|
||||
{
|
||||
return (TSSymbolMetadata){.visible = false, .named = false};
|
||||
}
|
||||
return ((TSSymbolMetadata){.visible = false, .named = false});
|
||||
else
|
||||
{
|
||||
return self->symbol_metadata[symbol];
|
||||
}
|
||||
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];
|
||||
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;
|
||||
return (0);
|
||||
}
|
||||
else if (symbol < self->token_count)
|
||||
{
|
||||
t_u32 count;
|
||||
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
|
||||
|
||||
actions = ts_language_actions(self, state, symbol, &count);
|
||||
if (count > 0)
|
||||
{
|
||||
TSParseAction action = actions[count - 1];
|
||||
action = actions[count - 1];
|
||||
if (action.type == TSParseActionTypeShift)
|
||||
{
|
||||
return action.shift.extra ? state : action.shift.state;
|
||||
}
|
||||
return (action.shift.extra ? state : action.shift.state);
|
||||
}
|
||||
return 0;
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ts_language_lookup(self, state, symbol);
|
||||
}
|
||||
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";
|
||||
}
|
||||
return ("ERROR");
|
||||
else if (symbol == ts_builtin_sym_error_repeat)
|
||||
{
|
||||
return "_ERROR";
|
||||
}
|
||||
return ("_ERROR");
|
||||
else if (symbol < ts_language_symbol_count(self))
|
||||
{
|
||||
return self->symbol_names[symbol];
|
||||
}
|
||||
return (self->symbol_names[symbol]);
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
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;
|
||||
t_u16 count = (t_u16)ts_language_symbol_count(self);
|
||||
for (TSSymbol i = 0; i < count; i++)
|
||||
return (ts_builtin_sym_error);
|
||||
count = (t_u16)ts_language_symbol_count(self);
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
TSSymbolMetadata metadata = ts_language_symbol_metadata(self, i);
|
||||
metadata = ts_language_symbol_metadata(self, i);
|
||||
if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named)
|
||||
continue;
|
||||
t_const_str symbol_name = self->symbol_names[i];
|
||||
if (!strncmp(symbol_name, string, length) && !symbol_name[length])
|
||||
{
|
||||
return self->public_symbol_map[i];
|
||||
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;
|
||||
return (0);
|
||||
}
|
||||
|
||||
TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol)
|
||||
{
|
||||
TSSymbolMetadata metadata = ts_language_symbol_metadata(self, symbol);
|
||||
TSSymbolMetadata metadata;
|
||||
|
||||
metadata = ts_language_symbol_metadata(self, symbol);
|
||||
if (metadata.named && metadata.visible)
|
||||
{
|
||||
return TSSymbolTypeRegular;
|
||||
}
|
||||
return (TSSymbolTypeRegular);
|
||||
else if (metadata.visible)
|
||||
{
|
||||
return TSSymbolTypeAnonymous;
|
||||
}
|
||||
return (TSSymbolTypeAnonymous);
|
||||
else
|
||||
{
|
||||
return TSSymbolTypeAuxiliary;
|
||||
}
|
||||
return (TSSymbolTypeAuxiliary);
|
||||
}
|
||||
|
||||
t_const_str ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id)
|
||||
{
|
||||
t_u32 count = ts_language_field_count(self);
|
||||
t_u32 count;
|
||||
|
||||
count = ts_language_field_count(self);
|
||||
if (count && id <= count)
|
||||
{
|
||||
return self->field_names[id];
|
||||
}
|
||||
return (self->field_names[id]);
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
TSFieldId ts_language_field_id_for_name(const TSLanguage *self, t_const_str name, t_u32 name_length)
|
||||
{
|
||||
t_u16 count = (t_u16)ts_language_field_count(self);
|
||||
for (TSSymbol i = 1; i < count + 1; i++)
|
||||
t_u16 count;
|
||||
TSSymbol i;
|
||||
t_i32 res;
|
||||
|
||||
count = (t_u16)ts_language_field_count(self);
|
||||
i = 1;
|
||||
while (i < count + 1)
|
||||
{
|
||||
switch (strncmp(name, self->field_names[i], name_length))
|
||||
res = strncmp(name, self->field_names[i], name_length);
|
||||
if (res == 0)
|
||||
{
|
||||
case 0:
|
||||
if (self->field_names[i][name_length] == 0)
|
||||
return i;
|
||||
break;
|
||||
case -1:
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
return (i);
|
||||
}
|
||||
else if (res == -1)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
// TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state) {
|
||||
// if (state >= self->state_count) return NULL;
|
||||
// LookaheadIterator *iterator = mem_alloc(sizeof(LookaheadIterator));
|
||||
// *iterator = ts_language_lookaheads(self, state);
|
||||
// return (TSLookaheadIterator *)iterator;
|
||||
// }
|
||||
bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol)
|
||||
{
|
||||
return (0 < symbol && symbol < self->external_token_count + 1);
|
||||
}
|
||||
|
||||
// void ts_lookahead_iterator_delete(TSLookaheadIterator *self) {
|
||||
// mem_free(self);
|
||||
// }
|
||||
const TSParseAction *ts_language_actions(const TSLanguage *self, TSStateId state, TSSymbol symbol, t_u32 *count)
|
||||
{
|
||||
TableEntry entry;
|
||||
|
||||
// bool ts_lookahead_iterator_reset_state(TSLookaheadIterator * self, TSStateId state) {
|
||||
// LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
// if (state >= iterator->language->state_count) return false;
|
||||
// *iterator = ts_language_lookaheads(iterator->language, state);
|
||||
// return true;
|
||||
// }
|
||||
ts_language_table_entry(self, state, symbol, &entry);
|
||||
*count = entry.action_count;
|
||||
return (entry.actions);
|
||||
}
|
||||
|
||||
// const TSLanguage *ts_lookahead_iterator_language(const TSLookaheadIterator *self) {
|
||||
// const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
// return iterator->language;
|
||||
// }
|
||||
bool ts_language_has_reduce_action(const TSLanguage *self, TSStateId state, TSSymbol symbol)
|
||||
{
|
||||
TableEntry entry;
|
||||
|
||||
// bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state) {
|
||||
// if (state >= language->state_count) return false;
|
||||
// LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
// *iterator = ts_language_lookaheads(language, state);
|
||||
// return true;
|
||||
// }
|
||||
ts_language_table_entry(self, state, symbol, &entry);
|
||||
return (entry.action_count > 0 && entry.actions[0].type == TSParseActionTypeReduce);
|
||||
}
|
||||
|
||||
// bool ts_lookahead_iterator_next(TSLookaheadIterator *self) {
|
||||
// LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
// return ts_lookahead_iterator__next(iterator);
|
||||
// }
|
||||
// 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)
|
||||
{
|
||||
t_u32 index;
|
||||
const t_u16 *data;
|
||||
t_u16 group_count;
|
||||
t_u16 section_value;
|
||||
t_u16 symbol_count;
|
||||
t_u32 i;
|
||||
t_u32 j;
|
||||
|
||||
// TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self) {
|
||||
// const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
// return iterator->symbol;
|
||||
// }
|
||||
if (state >= self->large_state_count)
|
||||
{
|
||||
index = self->small_parse_table_map[state - self->large_state_count];
|
||||
data = &self->small_parse_table[index];
|
||||
group_count = *(data++);
|
||||
i = 0;
|
||||
while (i++ < group_count)
|
||||
{
|
||||
section_value = *(data++);
|
||||
symbol_count = *(data++);
|
||||
j = 0;
|
||||
while (j++ < symbol_count)
|
||||
if (*(data++) == symbol)
|
||||
return (section_value);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (self->parse_table[state * self->symbol_count + symbol]);
|
||||
}
|
||||
|
||||
// t_const_str ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self) {
|
||||
// const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
// return ts_language_symbol_name(iterator->language, iterator->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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue