Update
This commit is contained in:
parent
5bec1546aa
commit
c7fb66c70d
15 changed files with 572 additions and 164 deletions
3
gen.list
3
gen.list
|
|
@ -4,3 +4,6 @@ src/vec/vec_parser_heredoc_functions3.c
|
|||
src/vec/vec_parser_range.c
|
||||
src/vec/vec_parser_range_functions2.c
|
||||
src/vec/vec_parser_range_functions3.c
|
||||
src/vec/vec_reduce_action.c
|
||||
src/vec/vec_reduce_action_functions2.c
|
||||
src/vec/vec_reduce_action_functions3.c
|
||||
|
|
|
|||
|
|
@ -52,3 +52,11 @@ replace.C__TYPENAME__ = "t_heredoc"
|
|||
replace.C__TYPEHEADER__ = '#include "parser/types/types_heredoc.h"'
|
||||
replace.C__PREFIX__ = "parser_heredoc"
|
||||
replace.C__PREFIXUP__ = "PARSER_HEREDOC"
|
||||
|
||||
[[create.vec]]
|
||||
sources_output = "src/vec/"
|
||||
headers_output = "include/me/vec/"
|
||||
replace.C__TYPENAME__ = "t_reduce_action"
|
||||
replace.C__TYPEHEADER__ = '#include "parser/types/types_reduce_action.h"'
|
||||
replace.C__PREFIX__ = "reduce_action"
|
||||
replace.C__PREFIXUP__ = "REDUCE_ACTION"
|
||||
|
|
|
|||
58
output/include/me/vec/vec_reduce_action.h
Normal file
58
output/include/me/vec/vec_reduce_action.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_reduce_action.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef VEC_REDUCE_ACTION_H
|
||||
#define VEC_REDUCE_ACTION_H
|
||||
|
||||
#include "parser/types/types_reduce_action.h"
|
||||
#include "me/types.h"
|
||||
|
||||
typedef bool (*t_vec_reduce_action_sort_fn)(t_reduce_action *, t_reduce_action *);
|
||||
typedef void (*t_free_reduce_action_item)(t_reduce_action);
|
||||
|
||||
typedef struct s_vec_reduce_action
|
||||
{
|
||||
t_free_reduce_action_item free_func;
|
||||
t_usize len;
|
||||
t_usize capacity;
|
||||
t_reduce_action *buffer;
|
||||
} t_vec_reduce_action;
|
||||
|
||||
t_vec_reduce_action vec_reduce_action_new(t_usize capacity,
|
||||
t_free_reduce_action_item free_function);
|
||||
t_error vec_reduce_action_push(t_vec_reduce_action *vec, t_reduce_action element);
|
||||
t_error vec_reduce_action_push_front(t_vec_reduce_action *vec,
|
||||
t_reduce_action element);
|
||||
t_error vec_reduce_action_pop(t_vec_reduce_action *vec, t_reduce_action *value);
|
||||
t_error vec_reduce_action_pop_front(t_vec_reduce_action *vec, t_reduce_action *value);
|
||||
void vec_reduce_action_free(t_vec_reduce_action vec);
|
||||
t_error vec_reduce_action_reserve(t_vec_reduce_action *vec,
|
||||
t_usize wanted_capacity);
|
||||
t_error vec_reduce_action_find(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *), t_usize *index);
|
||||
t_error vec_reduce_action_find_starting(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *),
|
||||
t_usize starting_index, t_usize *index);
|
||||
t_error vec_reduce_action_all(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *), bool *result);
|
||||
t_error vec_reduce_action_any(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *), bool *result);
|
||||
void vec_reduce_action_iter(t_vec_reduce_action *vec,
|
||||
void (*fn)(t_usize index, t_reduce_action *value,
|
||||
void *state),
|
||||
void *state);
|
||||
void vec_reduce_action_reverse(t_vec_reduce_action *vec);
|
||||
void vec_reduce_action_sort(t_vec_reduce_action *vec,
|
||||
t_vec_reduce_action_sort_fn is_sorted);
|
||||
t_error vec_reduce_action_back(t_vec_reduce_action *vec, t_reduce_action **out);
|
||||
|
||||
#endif
|
||||
115
output/src/vec/vec_reduce_action.c
Normal file
115
output/src/vec/vec_reduce_action.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_reduce_action.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_reduce_action.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_vec_reduce_action vec_reduce_action_new(t_usize capacity,
|
||||
t_free_reduce_action_item free_function)
|
||||
{
|
||||
t_vec_reduce_action out;
|
||||
|
||||
out = (t_vec_reduce_action){0};
|
||||
out.free_func = free_function;
|
||||
out.buffer = mem_alloc_array(capacity, sizeof(t_reduce_action));
|
||||
if (out.buffer)
|
||||
out.capacity = capacity;
|
||||
return (out);
|
||||
}
|
||||
|
||||
/// Return true in case of an error
|
||||
t_error vec_reduce_action_push(t_vec_reduce_action *vec, t_reduce_action element)
|
||||
{
|
||||
t_reduce_action *temp_buffer;
|
||||
size_t new_capacity;
|
||||
|
||||
if (vec == NULL)
|
||||
return (ERROR);
|
||||
if (vec->len + 1 > vec->capacity)
|
||||
{
|
||||
new_capacity = (vec->capacity * 3) / 2 + 1;
|
||||
while (vec->len + 1 > new_capacity)
|
||||
new_capacity = (new_capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_reduce_action));
|
||||
if (temp_buffer == NULL)
|
||||
return (ERROR);
|
||||
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_reduce_action));
|
||||
free(vec->buffer);
|
||||
vec->buffer = temp_buffer;
|
||||
vec->capacity = new_capacity;
|
||||
}
|
||||
vec->buffer[vec->len] = element;
|
||||
vec->len += 1;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/// Return true in case of an error
|
||||
t_error vec_reduce_action_reserve(t_vec_reduce_action *vec, t_usize wanted_capacity)
|
||||
{
|
||||
t_reduce_action *temp_buffer;
|
||||
size_t new_capacity;
|
||||
|
||||
if (vec == NULL)
|
||||
return (ERROR);
|
||||
if (wanted_capacity > vec->capacity)
|
||||
{
|
||||
new_capacity = (vec->capacity * 3) / 2 + 1;
|
||||
while (wanted_capacity > new_capacity)
|
||||
new_capacity = (new_capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_reduce_action));
|
||||
if (temp_buffer == NULL)
|
||||
return (ERROR);
|
||||
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_reduce_action));
|
||||
free(vec->buffer);
|
||||
vec->buffer = temp_buffer;
|
||||
vec->capacity = new_capacity;
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/// Return true if the vector is empty
|
||||
/// This function is safe to call with value being NULL
|
||||
t_error vec_reduce_action_pop(t_vec_reduce_action *vec, t_reduce_action *value)
|
||||
{
|
||||
t_reduce_action temp_value;
|
||||
t_reduce_action *ptr;
|
||||
|
||||
if (vec == NULL)
|
||||
return (ERROR);
|
||||
ptr = value;
|
||||
if (vec->len == 0)
|
||||
return (ERROR);
|
||||
if (value == NULL)
|
||||
ptr = &temp_value;
|
||||
vec->len--;
|
||||
*ptr = vec->buffer[vec->len];
|
||||
mem_set_zero(&vec->buffer[vec->len], sizeof(t_reduce_action));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
/// This function is safe to call with `free_elem` being NULL
|
||||
void vec_reduce_action_free(t_vec_reduce_action vec)
|
||||
{
|
||||
if (vec.free_func)
|
||||
{
|
||||
while (vec.len)
|
||||
{
|
||||
vec.free_func(vec.buffer[vec.len - 1]);
|
||||
vec.len--;
|
||||
}
|
||||
}
|
||||
free(vec.buffer);
|
||||
}
|
||||
112
output/src/vec/vec_reduce_action_functions2.c
Normal file
112
output/src/vec/vec_reduce_action_functions2.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_reduce_action.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_reduce_action.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_error vec_reduce_action_find(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *), t_usize *index)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || index == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
while (idx < vec->len)
|
||||
{
|
||||
if (fn(&vec->buffer[idx]))
|
||||
{
|
||||
*index = idx;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error vec_reduce_action_find_starting(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *),
|
||||
t_usize starting_index, t_usize *index)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || index == NULL)
|
||||
return (ERROR);
|
||||
idx = starting_index;
|
||||
while (idx < vec->len)
|
||||
{
|
||||
if (fn(&vec->buffer[idx]))
|
||||
{
|
||||
*index = idx;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error vec_reduce_action_all(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *), bool *result)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || result == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
*result = true;
|
||||
while (*result && idx < vec->len)
|
||||
{
|
||||
if (!fn(&vec->buffer[idx]))
|
||||
*result = false;
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error vec_reduce_action_any(t_vec_reduce_action *vec,
|
||||
bool (*fn)(const t_reduce_action *), bool *result)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL || result == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
*result = false;
|
||||
while (*result && idx < vec->len)
|
||||
{
|
||||
if (fn(&vec->buffer[idx]))
|
||||
*result = true;
|
||||
idx++;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
void vec_reduce_action_iter(t_vec_reduce_action *vec,
|
||||
void (*fn)(t_usize index, t_reduce_action *value,
|
||||
void *state),
|
||||
void *state)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (vec == NULL || fn == NULL)
|
||||
return;
|
||||
idx = 0;
|
||||
while (idx < vec->len)
|
||||
{
|
||||
fn(idx, &vec->buffer[idx], state);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
84
output/src/vec/vec_reduce_action_functions3.c
Normal file
84
output/src/vec/vec_reduce_action_functions3.c
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vec_reduce_action.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_reduce_action.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_error vec_reduce_action_push_front(t_vec_reduce_action *vec,
|
||||
t_reduce_action element)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
if (vec->len == 0)
|
||||
return (vec_reduce_action_push(vec, element));
|
||||
i = vec->len - 1;
|
||||
if (vec->capacity < vec->len + 1 &&
|
||||
vec_reduce_action_reserve(vec, 3 * vec->len / 2 + 1))
|
||||
return (ERROR);
|
||||
while (i > 0)
|
||||
{
|
||||
vec->buffer[i + 1] = vec->buffer[i];
|
||||
i--;
|
||||
}
|
||||
vec->buffer[1] = vec->buffer[0];
|
||||
vec->buffer[0] = element;
|
||||
vec->len++;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error vec_reduce_action_pop_front(t_vec_reduce_action *vec, t_reduce_action *value)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
if (vec->len <= 1)
|
||||
return (vec_reduce_action_pop(vec, value));
|
||||
i = 0;
|
||||
*value = vec->buffer[0];
|
||||
vec->len--;
|
||||
while (i < vec->len)
|
||||
{
|
||||
vec->buffer[i] = vec->buffer[i + 1];
|
||||
i++;
|
||||
}
|
||||
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
void vec_reduce_action_reverse(t_vec_reduce_action *vec)
|
||||
{
|
||||
t_reduce_action temporary;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
while (i < vec->len / 2)
|
||||
{
|
||||
temporary = vec->buffer[vec->len - 1 - i];
|
||||
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
|
||||
vec->buffer[i] = temporary;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
t_error vec_reduce_action_back(t_vec_reduce_action *vec, t_reduce_action **out)
|
||||
{
|
||||
t_reduce_action *temporary;
|
||||
|
||||
if (out == NULL)
|
||||
out = &temporary;
|
||||
if (vec->len != 0)
|
||||
return (*out = &vec->buffer[vec->len - 1], true);
|
||||
return (false);
|
||||
}
|
||||
|
|
@ -33,10 +33,10 @@
|
|||
/*******************/
|
||||
|
||||
typedef struct s_parser t_parser;
|
||||
typedef struct t_parse_tree t_parse_tree;
|
||||
typedef struct t_query t_query;
|
||||
typedef struct t_query_cursor t_query_cursor;
|
||||
typedef struct t_lookahead_iterator t_lookahead_iterator;
|
||||
typedef struct s_parse_tree t_parse_tree;
|
||||
typedef struct s_query t_query;
|
||||
typedef struct s_query_cursor t_query_cursor;
|
||||
typedef struct s_lookahead_iterator t_lookahead_iterator;
|
||||
|
||||
typedef enum t_input_encoding
|
||||
{
|
||||
|
|
|
|||
36
parser/includes/reduce_action.h
Normal file
36
parser/includes/reduce_action.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* reduce_action.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/30 15:25:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/30 15:25:38 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef REDUCE_ACTION_H
|
||||
#define REDUCE_ACTION_H
|
||||
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_reduce_action.h"
|
||||
#include "parser/api.h"
|
||||
#include "parser/types/types_reduce_action.h"
|
||||
|
||||
static inline void ts_reduce_action_set_add(t_vec_reduce_action *self,
|
||||
t_reduce_action new_action)
|
||||
{
|
||||
t_reduce_action action;
|
||||
|
||||
for (t_u32 i = 0; i < self->len; i++)
|
||||
{
|
||||
action = self->buffer[i];
|
||||
if (action.symbol == new_action.symbol &&
|
||||
action.count == new_action.count)
|
||||
return;
|
||||
}
|
||||
vec_reduce_action_push(self, new_action);
|
||||
}
|
||||
|
||||
#endif /* REDUCE_ACTION_H */
|
||||
27
parser/includes/types/types_reduce_action.h
Normal file
27
parser/includes/types/types_reduce_action.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* types_reduce_action.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/30 15:21:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/30 15:22:18 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TYPES_REDUCE_ACTION_H
|
||||
#define TYPES_REDUCE_ACTION_H
|
||||
|
||||
#include "me/types.h"
|
||||
#include "parser/types/types_symbol.h"
|
||||
|
||||
typedef struct s_reduce_action
|
||||
{
|
||||
t_u32 count;
|
||||
t_symbol symbol;
|
||||
t_i32 dynamic_precedence;
|
||||
t_u16 production_id;
|
||||
} t_reduce_action;
|
||||
|
||||
#endif /* TYPES_REDUCE_ACTION_H */
|
||||
|
|
@ -30,7 +30,7 @@ void ts_language_table_entry(
|
|||
const t_language *self,
|
||||
t_state_id state,
|
||||
t_symbol symbol,
|
||||
TableEntry *result
|
||||
t_table_entry *result
|
||||
) {
|
||||
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
|
||||
result->action_count = 0;
|
||||
|
|
@ -171,7 +171,7 @@ t_field_id ts_language_field_id_for_name(
|
|||
|
||||
t_lookahead_iterator *ts_lookahead_iterator_new(const t_language *self, t_state_id state) {
|
||||
if (state >= self->state_count) return NULL;
|
||||
LookaheadIterator *iterator = malloc(sizeof(LookaheadIterator));
|
||||
t_lookahead_iterator *iterator = malloc(sizeof(t_lookahead_iterator));
|
||||
*iterator = ts_language_lookaheads(self, state);
|
||||
return (t_lookahead_iterator *)iterator;
|
||||
}
|
||||
|
|
@ -181,35 +181,35 @@ void ts_lookahead_iterator_delete(t_lookahead_iterator *self) {
|
|||
}
|
||||
|
||||
bool ts_lookahead_iterator_reset_state(t_lookahead_iterator * self, t_state_id state) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
t_lookahead_iterator *iterator = (t_lookahead_iterator *)self;
|
||||
if (state >= iterator->language->state_count) return false;
|
||||
*iterator = ts_language_lookaheads(iterator->language, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
const t_language *ts_lookahead_iterator_language(const t_lookahead_iterator *self) {
|
||||
const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
const t_lookahead_iterator *iterator = (const t_lookahead_iterator *)self;
|
||||
return iterator->language;
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_reset(t_lookahead_iterator *self, const t_language *language, t_state_id state) {
|
||||
if (state >= language->state_count) return false;
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
t_lookahead_iterator *iterator = (t_lookahead_iterator *)self;
|
||||
*iterator = ts_language_lookaheads(language, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_next(t_lookahead_iterator *self) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
t_lookahead_iterator *iterator = (t_lookahead_iterator *)self;
|
||||
return ts_lookahead_iterator__next(iterator);
|
||||
}
|
||||
|
||||
t_symbol ts_lookahead_iterator_current_symbol(const t_lookahead_iterator *self) {
|
||||
const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
const t_lookahead_iterator *iterator = (const t_lookahead_iterator *)self;
|
||||
return iterator->symbol;
|
||||
}
|
||||
|
||||
const char *ts_lookahead_iterator_current_symbol_name(const t_lookahead_iterator *self) {
|
||||
const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
const t_lookahead_iterator *iterator = (const t_lookahead_iterator *)self;
|
||||
return ts_language_symbol_name(iterator->language, iterator->symbol);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#ifndef TREE_SITTER_LANGUAGE_H_
|
||||
#define TREE_SITTER_LANGUAGE_H_
|
||||
|
||||
#include "./parser.h"
|
||||
#include "./subtree.h"
|
||||
#include "parser/types/types_parse_action_type.h"
|
||||
#include "parser/types/types_state_id.h"
|
||||
|
|
@ -12,14 +11,14 @@
|
|||
#define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14
|
||||
#define LANGUAGE_VERSION_USABLE_VIA_WASM 13
|
||||
|
||||
typedef struct
|
||||
typedef struct s_table_entry
|
||||
{
|
||||
const t_parse_actions *actions;
|
||||
t_u32 action_count;
|
||||
bool is_reusable;
|
||||
} TableEntry;
|
||||
} t_table_entry;
|
||||
|
||||
typedef struct
|
||||
typedef struct s_lookahead_iterator
|
||||
{
|
||||
const t_language *language;
|
||||
const t_u16 *data;
|
||||
|
|
@ -34,10 +33,10 @@ typedef struct
|
|||
t_symbol symbol;
|
||||
t_state_id next_state;
|
||||
t_u16 action_count;
|
||||
} LookaheadIterator;
|
||||
} t_lookahead_iterator;
|
||||
|
||||
void ts_language_table_entry(const t_language *, t_state_id, t_symbol,
|
||||
TableEntry *);
|
||||
t_table_entry *);
|
||||
|
||||
t_symbol_metadata ts_language_symbol_metadata(const t_language *, t_symbol);
|
||||
|
||||
|
|
@ -57,7 +56,7 @@ static inline const t_parse_actions *ts_language_actions(const t_language *self,
|
|||
t_symbol symbol,
|
||||
t_u32 *count)
|
||||
{
|
||||
TableEntry entry;
|
||||
t_table_entry entry;
|
||||
ts_language_table_entry(self, state, symbol, &entry);
|
||||
*count = entry.action_count;
|
||||
return entry.actions;
|
||||
|
|
@ -67,7 +66,7 @@ static inline bool ts_language_has_reduce_action(const t_language *self,
|
|||
t_state_id state,
|
||||
t_symbol symbol)
|
||||
{
|
||||
TableEntry entry;
|
||||
t_table_entry entry;
|
||||
ts_language_table_entry(self, state, symbol, &entry);
|
||||
return entry.action_count > 0 && entry.actions[0].type == ActionTypeReduce;
|
||||
}
|
||||
|
|
@ -118,8 +117,8 @@ static inline bool ts_language_has_actions(const t_language *self,
|
|||
// all possible symbols and checking the parse table for each one.
|
||||
// For 'small' parse states, this exploits the structure of the
|
||||
// table to only visit the valid symbols.
|
||||
static inline LookaheadIterator ts_language_lookaheads(const t_language *self,
|
||||
t_state_id state)
|
||||
static inline t_lookahead_iterator ts_language_lookaheads(
|
||||
const t_language *self, t_state_id state)
|
||||
{
|
||||
bool is_small_state = state >= self->large_state_count;
|
||||
const t_u16 *data;
|
||||
|
|
@ -137,7 +136,7 @@ static inline LookaheadIterator ts_language_lookaheads(const t_language *self,
|
|||
{
|
||||
data = &self->parse_table[state * self->symbol_count] - 1;
|
||||
}
|
||||
return (LookaheadIterator){
|
||||
return (t_lookahead_iterator){
|
||||
.language = self,
|
||||
.data = data,
|
||||
.group_end = group_end,
|
||||
|
|
@ -148,7 +147,7 @@ static inline LookaheadIterator ts_language_lookaheads(const t_language *self,
|
|||
};
|
||||
}
|
||||
|
||||
static inline bool ts_lookahead_iterator__next(LookaheadIterator *self)
|
||||
static inline bool ts_lookahead_iterator__next(t_lookahead_iterator *self)
|
||||
{
|
||||
// For small parse states, valid symbols are listed explicitly,
|
||||
// grouped by their value. There's no need to look up the actions
|
||||
|
|
|
|||
|
|
@ -1,15 +1,9 @@
|
|||
#include "./array.h"
|
||||
#include "parser/error_costs.h"
|
||||
#include "./language.h"
|
||||
#include "parser/parser_length.h"
|
||||
#include "parser/lexer.h"
|
||||
#include "./reduce_action.h"
|
||||
#include "./reusable_node.h"
|
||||
#include "./stack.h"
|
||||
#include "./subtree.h"
|
||||
#include "./tree.h"
|
||||
|
||||
#include "parser/api.h"
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
|
|
@ -18,6 +12,12 @@
|
|||
#include <time.h>
|
||||
|
||||
#include "me/vec/vec_parser_range.h"
|
||||
#include "me/vec/vec_reduce_action.h"
|
||||
#include "parser/api.h"
|
||||
#include "parser/error_costs.h"
|
||||
#include "parser/lexer.h"
|
||||
#include "parser/parser_length.h"
|
||||
#include "parser/reduce_action.h"
|
||||
#include "parser/types/types_language.h"
|
||||
|
||||
typedef t_u64 t_duration;
|
||||
|
|
@ -117,7 +117,7 @@ typedef struct s_parser
|
|||
SubtreePool tree_pool;
|
||||
const t_language *language;
|
||||
void *wasm_store;
|
||||
ReduceActionSet reduce_actions;
|
||||
t_vec_reduce_action reduce_actions;
|
||||
Subtree finished_tree;
|
||||
SubtreeArray trailing_extras;
|
||||
SubtreeArray trailing_extras2;
|
||||
|
|
@ -162,8 +162,8 @@ typedef struct s_string_input
|
|||
|
||||
// StringInput
|
||||
|
||||
static const char *ts_string_inpt_read(void *_self, t_u32 byte,
|
||||
t_point point, t_u32 *length)
|
||||
static const char *ts_string_inpt_read(void *_self, t_u32 byte, t_point point,
|
||||
t_u32 *length)
|
||||
{
|
||||
(void)point;
|
||||
t_string_input *self = (t_string_input *)_self;
|
||||
|
|
@ -464,7 +464,7 @@ static bool ts_parser__external_scanner_scan(t_parser *self,
|
|||
|
||||
static bool ts_parser__can_reuse_first_leaf(t_parser *self, t_state_id state,
|
||||
Subtree tree,
|
||||
TableEntry *table_entry)
|
||||
t_table_entry *table_entry)
|
||||
{
|
||||
t_lex_modes current_lex_mode = self->language->lex_modes[state];
|
||||
t_symbol leaf_symbol = ts_subtree_leaf_symbol(tree);
|
||||
|
|
@ -506,7 +506,8 @@ static Subtree ts_parser__lex(t_parser *self, StackVersion version,
|
|||
return NULL_SUBTREE;
|
||||
}
|
||||
|
||||
const t_parse_length start_position = ts_stack_position(self->stack, version);
|
||||
const t_parse_length start_position =
|
||||
ts_stack_position(self->stack, version);
|
||||
const Subtree external_token =
|
||||
ts_stack_last_external_token(self->stack, version);
|
||||
|
||||
|
|
@ -626,10 +627,11 @@ static Subtree ts_parser__lex(t_parser *self, StackVersion version,
|
|||
Subtree result;
|
||||
if (skipped_error)
|
||||
{
|
||||
t_parse_length padding = length_sub(error_start_position, start_position);
|
||||
t_parse_length size = length_sub(error_end_position, error_start_position);
|
||||
t_u32 lookahead_bytes =
|
||||
lookahead_end_byte - error_end_position.bytes;
|
||||
t_parse_length padding =
|
||||
length_sub(error_start_position, start_position);
|
||||
t_parse_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);
|
||||
|
|
@ -690,7 +692,7 @@ static Subtree ts_parser__lex(t_parser *self, StackVersion version,
|
|||
static Subtree ts_parser__get_cached_token(t_parser *self, t_state_id state,
|
||||
size_t position,
|
||||
Subtree last_external_token,
|
||||
TableEntry *table_entry)
|
||||
t_table_entry *table_entry)
|
||||
{
|
||||
t_token_cache *cache = &self->token_cache;
|
||||
if (cache->token.ptr && cache->byte_index == position &&
|
||||
|
|
@ -730,7 +732,7 @@ static void ts_parser__set_cached_token(t_parser *self, t_u32 byte_index,
|
|||
static Subtree ts_parser__reuse_node(t_parser *self, StackVersion version,
|
||||
t_state_id *state, t_u32 position,
|
||||
Subtree last_external_token,
|
||||
TableEntry *table_entry)
|
||||
t_table_entry *table_entry)
|
||||
{
|
||||
Subtree result;
|
||||
while ((result = reusable_node_tree(&self->reusable_node)).ptr)
|
||||
|
|
@ -823,7 +825,8 @@ static Subtree ts_parser__reuse_node(t_parser *self, StackVersion version,
|
|||
// The decision is based on the trees' error costs (if any), their dynamic
|
||||
// precedence, and finally, as a default, by a recursive comparison of the
|
||||
// trees' symbols.
|
||||
static bool ts_parser__select_parse_tree(t_parser *self, Subtree left, Subtree right)
|
||||
static bool ts_parser__select_parse_tree(t_parser *self, Subtree left,
|
||||
Subtree right)
|
||||
{
|
||||
if (!left.ptr)
|
||||
return true;
|
||||
|
|
@ -1141,7 +1144,7 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
|
||||
t_state_id state = ts_stack_state(self->stack, version);
|
||||
bool has_shift_action = false;
|
||||
array_clear(&self->reduce_actions);
|
||||
self->reduce_actions.len = 0;
|
||||
|
||||
t_symbol first_symbol, end_symbol;
|
||||
if (lookahead_symbol != 0)
|
||||
|
|
@ -1157,7 +1160,7 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
|
||||
for (t_symbol symbol = first_symbol; symbol < end_symbol; symbol++)
|
||||
{
|
||||
TableEntry entry;
|
||||
t_table_entry entry;
|
||||
ts_language_table_entry(self->language, state, symbol, &entry);
|
||||
for (t_u32 j = 0; j < entry.action_count; j++)
|
||||
{
|
||||
|
|
@ -1173,7 +1176,7 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
if (action.reduce.child_count > 0)
|
||||
ts_reduce_action_set_add(
|
||||
&self->reduce_actions,
|
||||
(ReduceAction){
|
||||
(t_reduce_action){
|
||||
.symbol = action.reduce.symbol,
|
||||
.count = action.reduce.child_count,
|
||||
.dynamic_precedence =
|
||||
|
|
@ -1188,9 +1191,9 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
}
|
||||
|
||||
StackVersion reduction_version = STACK_VERSION_NONE;
|
||||
for (t_u32 j = 0; j < self->reduce_actions.size; j++)
|
||||
for (t_u32 j = 0; j < self->reduce_actions.len; j++)
|
||||
{
|
||||
ReduceAction action = self->reduce_actions.contents[j];
|
||||
t_reduce_action action = self->reduce_actions.buffer[j];
|
||||
|
||||
reduction_version = ts_parser__reduce(
|
||||
self, version, action.symbol, action.count,
|
||||
|
|
@ -1616,7 +1619,7 @@ static bool ts_parser__advance(t_parser *self, StackVersion version,
|
|||
|
||||
bool did_reuse = true;
|
||||
Subtree lookahead = NULL_SUBTREE;
|
||||
TableEntry table_entry = {.action_count = 0};
|
||||
t_table_entry table_entry = {.action_count = 0};
|
||||
|
||||
// If possible, reuse a node from the previous syntax tree.
|
||||
if (allow_node_reuse)
|
||||
|
|
@ -1980,8 +1983,7 @@ t_parser *ts_parser_new(void)
|
|||
{
|
||||
t_parser *self = calloc(1, sizeof(t_parser));
|
||||
ts_lexer_init(&self->lexer);
|
||||
array_init(&self->reduce_actions);
|
||||
array_reserve(&self->reduce_actions, 4);
|
||||
self->reduce_actions = vec_reduce_action_new(4, NULL);
|
||||
self->tree_pool = ts_subtree_pool_new(32);
|
||||
self->stack = ts_stack_new(&self->tree_pool);
|
||||
self->finished_tree = NULL_SUBTREE;
|
||||
|
|
@ -2008,7 +2010,7 @@ void ts_parser_delete(t_parser *self)
|
|||
|
||||
ts_parser_set_language(self, NULL);
|
||||
ts_stack_delete(self->stack);
|
||||
if (self->reduce_actions.contents)
|
||||
if (self->reduce_actions.buffer)
|
||||
{
|
||||
array_delete(&self->reduce_actions);
|
||||
}
|
||||
|
|
@ -2141,7 +2143,8 @@ void ts_parser_reset(t_parser *self)
|
|||
self->has_scanner_error = false;
|
||||
}
|
||||
|
||||
t_parse_tree *ts_parser_parse(t_parser *self, const t_parse_tree *old_tree, t_parse_input input)
|
||||
t_parse_tree *ts_parser_parse(t_parser *self, const t_parse_tree *old_tree,
|
||||
t_parse_input input)
|
||||
{
|
||||
t_parse_tree *result = NULL;
|
||||
old_tree = NULL;
|
||||
|
|
@ -2254,14 +2257,16 @@ exit:
|
|||
return result;
|
||||
}
|
||||
|
||||
t_parse_tree *ts_parser_parse_string(t_parser *self, const t_parse_tree *old_tree,
|
||||
t_parse_tree *ts_parser_parse_string(t_parser *self,
|
||||
const t_parse_tree *old_tree,
|
||||
const char *string, t_u32 length)
|
||||
{
|
||||
return ts_parser_parse_string_encoding(self, old_tree, string, length,
|
||||
InputEncoding8);
|
||||
}
|
||||
|
||||
t_parse_tree *ts_parser_parse_string_encoding(t_parser *self, const t_parse_tree *old_tree,
|
||||
t_parse_tree *ts_parser_parse_string_encoding(t_parser *self,
|
||||
const t_parse_tree *old_tree,
|
||||
const char *string, t_u32 length,
|
||||
t_input_encoding encoding)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
#ifndef TREE_SITTER_REDUCE_ACTION_H_
|
||||
#define TREE_SITTER_REDUCE_ACTION_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "./array.h"
|
||||
#include "parser/api.h"
|
||||
|
||||
typedef struct {
|
||||
t_u32 count;
|
||||
t_symbol 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 (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);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TREE_SITTER_REDUCE_ACTION_H_
|
||||
|
|
@ -7,7 +7,6 @@
|
|||
#include "./array.h"
|
||||
#include "parser/error_costs.h"
|
||||
#include "parser/parser_length.h"
|
||||
#include "./parser.h"
|
||||
#include "parser/api.h"
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
|
|
|
|||
|
|
@ -3,29 +3,25 @@
|
|||
|
||||
#include "./subtree.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
const Subtree *child;
|
||||
const Subtree *parent;
|
||||
t_parse_length position;
|
||||
t_symbol alias_symbol;
|
||||
} ParentCacheEntry;
|
||||
|
||||
struct t_parse_tree {
|
||||
struct s_parse_tree
|
||||
{
|
||||
Subtree root;
|
||||
const t_language *language;
|
||||
t_parser_range *included_ranges;
|
||||
unsigned included_range_count;
|
||||
t_u32 included_range_count;
|
||||
};
|
||||
|
||||
t_parse_tree *ts_tree_new(Subtree root, const t_language *language, const t_parser_range *, unsigned);
|
||||
t_parse_node ts_node_new(const t_parse_tree *, const Subtree *, t_parse_length, t_symbol);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
t_parse_tree *ts_tree_new(Subtree root, const t_language *language,
|
||||
const t_parser_range *, t_u32);
|
||||
t_parse_node ts_node_new(const t_parse_tree *, const Subtree *, t_parse_length,
|
||||
t_symbol);
|
||||
|
||||
#endif // TREE_SITTER_TREE_H_
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue