Merge into master my changes (#3)

* WIP

* Compiling!

* moved some more headers

* removed src/point.h

* Update

* fixed some stuff
This commit is contained in:
Maix0 2024-04-30 16:23:02 +02:00 committed by GitHub
parent 24d122dc54
commit f51a071d03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 805 additions and 339 deletions

View file

@ -4,3 +4,6 @@ src/vec/vec_parser_heredoc_functions3.c
src/vec/vec_parser_range.c src/vec/vec_parser_range.c
src/vec/vec_parser_range_functions2.c src/vec/vec_parser_range_functions2.c
src/vec/vec_parser_range_functions3.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

View file

@ -52,3 +52,11 @@ replace.C__TYPENAME__ = "t_heredoc"
replace.C__TYPEHEADER__ = '#include "parser/types/types_heredoc.h"' replace.C__TYPEHEADER__ = '#include "parser/types/types_heredoc.h"'
replace.C__PREFIX__ = "parser_heredoc" replace.C__PREFIX__ = "parser_heredoc"
replace.C__PREFIXUP__ = "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"

View 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

View 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);
}

View 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++;
}
}

View 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);
}

View file

@ -33,10 +33,10 @@
/*******************/ /*******************/
typedef struct s_parser t_parser; typedef struct s_parser t_parser;
typedef struct t_parse_tree t_parse_tree; typedef struct s_parse_tree t_parse_tree;
typedef struct t_query t_query; typedef struct s_query t_query;
typedef struct t_query_cursor t_query_cursor; typedef struct s_query_cursor t_query_cursor;
typedef struct t_lookahead_iterator t_lookahead_iterator; typedef struct s_lookahead_iterator t_lookahead_iterator;
typedef enum t_input_encoding typedef enum t_input_encoding
{ {

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_costs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 14:26:02 by maiboyer #+# #+# */
/* Updated: 2024/04/30 14:26:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ERROR_COSTS_H
#define ERROR_COSTS_H
#define ERROR_STATE 0
#define ERROR_COST_PER_RECOVERY 500
#define ERROR_COST_PER_MISSING_TREE 110
#define ERROR_COST_PER_SKIPPED_TREE 100
#define ERROR_COST_PER_SKIPPED_LINE 30
#define ERROR_COST_PER_SKIPPED_CHAR 1
#endif /* ERROR_COSTS_H */

View file

@ -6,21 +6,52 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 19:51:24 by maiboyer #+# #+# */ /* Created: 2024/04/23 19:51:24 by maiboyer #+# #+# */
/* Updated: 2024/04/24 23:03:33 by maiboyer ### ########.fr */ /* Updated: 2024/04/30 14:28:34 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef LEXER_H #ifndef LEXER_H
#define LEXER_H #define LEXER_H
#include <stdbool.h> #include "me/types.h"
#include <stdint.h> #include "parser/api.h"
#include "parser/parser_length.h"
#include "parser/types/types_lexer.h"
#ifndef TREE_SITTER_API_H_ #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t t_state_id;
typedef uint16_t t_symbol; typedef struct s_liblexer
typedef uint16_t t_field_id; {
typedef struct s_language t_language; t_lexer data;
#endif t_parse_length current_position;
t_parse_length token_start_position;
t_parse_length token_end_position;
t_parser_range *included_ranges;
const char *chunk;
t_parse_input input;
t_parse_logger logger;
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];
} t_liblexer;
void ts_lexer_init(t_liblexer *self);
void ts_lexer_delete(t_liblexer *self);
void ts_lexer_set_input(t_liblexer *self, t_parse_input input);
void ts_lexer_reset(t_liblexer *self, t_parse_length range);
void ts_lexer_start(t_liblexer *self);
void ts_lexer_finish(t_liblexer *self, t_i32 *data);
void ts_lexer_advance_to_end(t_liblexer *self);
void ts_lexer_mark_end(t_liblexer *self);
bool ts_lexer_set_included_ranges(t_liblexer *self,
const t_parser_range *ranges, t_u32 count);
t_parser_range *ts_lexer_included_ranges(const t_liblexer *self, t_u32 *count);
#endif /* LEXER_H */ #endif /* LEXER_H */

View file

@ -2,7 +2,7 @@
#define TREE_SITTER_PARSER_H_ #define TREE_SITTER_PARSER_H_
#include "../parse_types.h" #include "../parse_types.h"
#include "./lexer.h" #include "parser/lexer.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>

View file

@ -1,7 +1,7 @@
#ifndef TREE_SITTER_LENGTH_H_ #ifndef TREE_SITTER_LENGTH_H_
#define TREE_SITTER_LENGTH_H_ #define TREE_SITTER_LENGTH_H_
#include "../src/point.h" #include "parser/point.h"
#include "parser/api.h" #include "parser/api.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>

21
parser/includes/point.h Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* point.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 14:35:22 by maiboyer #+# #+# */
/* Updated: 2024/04/30 14:46:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef POINT_H
#define POINT_H
#include "parser/point/inline1.h"
#include "parser/point/inline2.h"
#include "parser/point/inline3.h"
#endif /* POINT_H */

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inline1.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 14:35:50 by maiboyer #+# #+# */
/* Updated: 2024/04/30 14:43:49 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INLINE1_H
#define INLINE1_H
#include "parser/types/types_point.h"
static inline t_point point__new(unsigned row, unsigned column)
{
t_point result = {row, column};
return result;
}
static inline t_point point_add(t_point a, t_point 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 t_point point_sub(t_point a, t_point 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(t_point a, t_point b)
{
return (a.row < b.row) || (a.row == b.row && a.column <= b.column);
}
static inline bool point_lt(t_point a, t_point b)
{
return (a.row < b.row) || (a.row == b.row && a.column < b.column);
}
#endif /* INLINE1_H */

View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inline2.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 14:43:58 by maiboyer #+# #+# */
/* Updated: 2024/04/30 14:44:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INLINE2_H
#define INLINE2_H
#include "parser/types/types_point.h"
static inline bool point_gt(t_point a, t_point b)
{
return (a.row > b.row) || (a.row == b.row && a.column > b.column);
}
static inline bool point_gte(t_point a, t_point b)
{
return (a.row > b.row) || (a.row == b.row && a.column >= b.column);
}
static inline bool point_eq(t_point a, t_point b)
{
return a.row == b.row && a.column == b.column;
}
static inline t_point point_min(t_point a, t_point b)
{
if (a.row < b.row || (a.row == b.row && a.column < b.column))
return a;
else
return b;
}
static inline t_point point_max(t_point a, t_point b)
{
if (a.row > b.row || (a.row == b.row && a.column > b.column))
return a;
else
return b;
}
#endif /* INLINE2_H */

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inline3.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 14:44:49 by maiboyer #+# #+# */
/* Updated: 2024/04/30 15:04:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INLINE3_H
#define INLINE3_H
#include "parser/types/types_point.h"
#include <stdint.h>
static inline t_point point_val_zero(void)
{
return ((t_point){0, 0});
}
static inline t_point point_val_max(void)
{
return ((t_point){UINT32_MAX, UINT32_MAX});
}
#endif /* INLINE3_H */

View 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 */

View 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 */

View file

@ -1,11 +0,0 @@
#ifndef TREE_SITTER_ERROR_COSTS_H_
#define TREE_SITTER_ERROR_COSTS_H_
#define ERROR_STATE 0
#define ERROR_COST_PER_RECOVERY 500
#define ERROR_COST_PER_MISSING_TREE 110
#define ERROR_COST_PER_SKIPPED_TREE 100
#define ERROR_COST_PER_SKIPPED_LINE 30
#define ERROR_COST_PER_SKIPPED_CHAR 1
#endif

View file

@ -30,7 +30,7 @@ void ts_language_table_entry(
const t_language *self, const t_language *self,
t_state_id state, t_state_id state,
t_symbol symbol, t_symbol symbol,
TableEntry *result t_table_entry *result
) { ) {
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
result->action_count = 0; 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) { t_lookahead_iterator *ts_lookahead_iterator_new(const t_language *self, t_state_id state) {
if (state >= self->state_count) return NULL; 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); *iterator = ts_language_lookaheads(self, state);
return (t_lookahead_iterator *)iterator; 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) { 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; if (state >= iterator->language->state_count) return false;
*iterator = ts_language_lookaheads(iterator->language, state); *iterator = ts_language_lookaheads(iterator->language, state);
return true; return true;
} }
const t_language *ts_lookahead_iterator_language(const t_lookahead_iterator *self) { 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; return iterator->language;
} }
bool ts_lookahead_iterator_reset(t_lookahead_iterator *self, const t_language *language, t_state_id state) { bool ts_lookahead_iterator_reset(t_lookahead_iterator *self, const t_language *language, t_state_id state) {
if (state >= language->state_count) return false; 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); *iterator = ts_language_lookaheads(language, state);
return true; return true;
} }
bool ts_lookahead_iterator_next(t_lookahead_iterator *self) { 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); return ts_lookahead_iterator__next(iterator);
} }
t_symbol ts_lookahead_iterator_current_symbol(const t_lookahead_iterator *self) { 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; return iterator->symbol;
} }
const char *ts_lookahead_iterator_current_symbol_name(const t_lookahead_iterator *self) { 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); return ts_language_symbol_name(iterator->language, iterator->symbol);
} }

View file

@ -1,7 +1,6 @@
#ifndef TREE_SITTER_LANGUAGE_H_ #ifndef TREE_SITTER_LANGUAGE_H_
#define TREE_SITTER_LANGUAGE_H_ #define TREE_SITTER_LANGUAGE_H_
#include "./parser.h"
#include "./subtree.h" #include "./subtree.h"
#include "parser/types/types_parse_action_type.h" #include "parser/types/types_parse_action_type.h"
#include "parser/types/types_state_id.h" #include "parser/types/types_state_id.h"
@ -12,14 +11,14 @@
#define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14 #define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14
#define LANGUAGE_VERSION_USABLE_VIA_WASM 13 #define LANGUAGE_VERSION_USABLE_VIA_WASM 13
typedef struct typedef struct s_table_entry
{ {
const t_parse_actions *actions; const t_parse_actions *actions;
t_u32 action_count; t_u32 action_count;
bool is_reusable; bool is_reusable;
} TableEntry; } t_table_entry;
typedef struct typedef struct s_lookahead_iterator
{ {
const t_language *language; const t_language *language;
const t_u16 *data; const t_u16 *data;
@ -34,17 +33,17 @@ typedef struct
t_symbol symbol; t_symbol symbol;
t_state_id next_state; t_state_id next_state;
t_u16 action_count; t_u16 action_count;
} LookaheadIterator; } t_lookahead_iterator;
void ts_language_table_entry(const t_language *, t_state_id, t_symbol, 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); t_symbol_metadata ts_language_symbol_metadata(const t_language *, t_symbol);
t_symbol ts_language_public_symbol(const t_language *, t_symbol); t_symbol ts_language_public_symbol(const t_language *, t_symbol);
t_state_id ts_language_next_state(const t_language *self, t_state_id state, t_state_id ts_language_next_state(const t_language *self, t_state_id state,
t_symbol symbol); t_symbol symbol);
static inline bool ts_language_is_symbol_external(const t_language *self, static inline bool ts_language_is_symbol_external(const t_language *self,
t_symbol symbol) t_symbol symbol)
@ -54,10 +53,10 @@ static inline bool ts_language_is_symbol_external(const t_language *self,
static inline const t_parse_actions *ts_language_actions(const t_language *self, static inline const t_parse_actions *ts_language_actions(const t_language *self,
t_state_id state, t_state_id state,
t_symbol symbol, t_symbol symbol,
t_u32 *count) t_u32 *count)
{ {
TableEntry entry; t_table_entry entry;
ts_language_table_entry(self, state, symbol, &entry); ts_language_table_entry(self, state, symbol, &entry);
*count = entry.action_count; *count = entry.action_count;
return entry.actions; return entry.actions;
@ -67,7 +66,7 @@ static inline bool ts_language_has_reduce_action(const t_language *self,
t_state_id state, t_state_id state,
t_symbol symbol) t_symbol symbol)
{ {
TableEntry entry; t_table_entry entry;
ts_language_table_entry(self, state, symbol, &entry); ts_language_table_entry(self, state, symbol, &entry);
return entry.action_count > 0 && entry.actions[0].type == ActionTypeReduce; 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. // all possible symbols and checking the parse table for each one.
// For 'small' parse states, this exploits the structure of the // For 'small' parse states, this exploits the structure of the
// table to only visit the valid symbols. // table to only visit the valid symbols.
static inline LookaheadIterator ts_language_lookaheads(const t_language *self, static inline t_lookahead_iterator ts_language_lookaheads(
t_state_id state) const t_language *self, t_state_id state)
{ {
bool is_small_state = state >= self->large_state_count; bool is_small_state = state >= self->large_state_count;
const t_u16 *data; 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; data = &self->parse_table[state * self->symbol_count] - 1;
} }
return (LookaheadIterator){ return (t_lookahead_iterator){
.language = self, .language = self,
.data = data, .data = data,
.group_end = group_end, .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, // For small parse states, valid symbols are listed explicitly,
// grouped by their value. There's no need to look up the actions // grouped by their value. There's no need to look up the actions

View file

@ -1,4 +1,4 @@
#include "./lexer.h" #include "parser/lexer.h"
#include "parser/parser_length.h" #include "parser/parser_length.h"
#include "./subtree.h" #include "./subtree.h"
#include <stdint.h> #include <stdint.h>

View file

@ -1,60 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 16:17:29 by maiboyer #+# #+# */
/* Updated: 2024/04/29 16:55:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TREE_SITTER_LEXER_H_
#define TREE_SITTER_LEXER_H_
#include "parser/parser_length.h"
#include "./parser.h"
#include "./subtree.h"
#include "parser/api.h"
#include "me/types.h"
#include "parser/types/types_lexer.h"
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef struct s_liblexer
{
t_lexer data;
t_parse_length current_position;
t_parse_length token_start_position;
t_parse_length token_end_position;
t_parser_range *included_ranges;
const char *chunk;
t_parse_input input;
t_parse_logger logger;
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];
} t_liblexer;
void ts_lexer_init(t_liblexer *);
void ts_lexer_delete(t_liblexer *);
void ts_lexer_set_input(t_liblexer *, t_parse_input);
void ts_lexer_reset(t_liblexer *, t_parse_length);
void ts_lexer_start(t_liblexer *);
void ts_lexer_finish(t_liblexer *, t_i32 *);
void ts_lexer_advance_to_end(t_liblexer *);
void ts_lexer_mark_end(t_liblexer *);
bool ts_lexer_set_included_ranges(t_liblexer *self,
const t_parser_range *ranges, t_u32 count);
t_parser_range *ts_lexer_included_ranges(const t_liblexer *self, t_u32 *count);
#endif // TREE_SITTER_LEXER_H_

View file

@ -1,15 +1,9 @@
#include "./array.h"
#include "./error_costs.h"
#include "./language.h" #include "./language.h"
#include "parser/parser_length.h"
#include "./lexer.h"
#include "./reduce_action.h"
#include "./reusable_node.h" #include "./reusable_node.h"
#include "./stack.h" #include "./stack.h"
#include "./subtree.h" #include "./subtree.h"
#include "./tree.h" #include "./tree.h"
#include "parser/api.h"
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h> #include <limits.h>
@ -18,6 +12,12 @@
#include <time.h> #include <time.h>
#include "me/vec/vec_parser_range.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" #include "parser/types/types_language.h"
typedef t_u64 t_duration; typedef t_u64 t_duration;
@ -117,7 +117,7 @@ typedef struct s_parser
SubtreePool tree_pool; SubtreePool tree_pool;
const t_language *language; const t_language *language;
void *wasm_store; void *wasm_store;
ReduceActionSet reduce_actions; t_vec_reduce_action reduce_actions;
Subtree finished_tree; Subtree finished_tree;
SubtreeArray trailing_extras; SubtreeArray trailing_extras;
SubtreeArray trailing_extras2; SubtreeArray trailing_extras2;
@ -157,13 +157,13 @@ typedef enum e_error_comparison
typedef struct s_string_input typedef struct s_string_input
{ {
const char *string; const char *string;
t_u32 length; t_u32 length;
} t_string_input; } t_string_input;
// StringInput // StringInput
static const char *ts_string_inpt_read(void *_self, t_u32 byte, static const char *ts_string_inpt_read(void *_self, t_u32 byte, t_point point,
t_point point, t_u32 *length) t_u32 *length)
{ {
(void)point; (void)point;
t_string_input *self = (t_string_input *)_self; t_string_input *self = (t_string_input *)_self;
@ -219,7 +219,7 @@ static bool ts_parser__breakdown_top_of_stack(t_parser *self,
for (t_u32 i = 0; i < pop.size; i++) for (t_u32 i = 0; i < pop.size; i++)
{ {
StackSlice slice = pop.contents[i]; StackSlice slice = pop.contents[i];
t_state_id state = ts_stack_state(self->stack, slice.version); t_state_id state = ts_stack_state(self->stack, slice.version);
Subtree parent = *array_front(&slice.subtrees); Subtree parent = *array_front(&slice.subtrees);
for (t_u32 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++)
@ -367,7 +367,7 @@ static bool ts_parser__better_version_exists(t_parser *self,
return true; return true;
} }
t_parse_length position = ts_stack_position(self->stack, version); t_parse_length position = ts_stack_position(self->stack, version);
t_error_status status = { t_error_status status = {
.cost = cost, .cost = cost,
.is_in_error = is_in_error, .is_in_error = is_in_error,
@ -440,7 +440,7 @@ static void ts_parser__external_scanner_deserialize(t_parser *self,
Subtree external_token) Subtree external_token)
{ {
const char *data = NULL; const char *data = NULL;
t_u32 length = 0; t_u32 length = 0;
if (external_token.ptr) if (external_token.ptr)
{ {
data = ts_external_scanner_state_data( data = ts_external_scanner_state_data(
@ -452,7 +452,7 @@ static void ts_parser__external_scanner_deserialize(t_parser *self,
data, length); data, length);
} }
static bool ts_parser__external_scanner_scan(t_parser *self, static bool ts_parser__external_scanner_scan(t_parser *self,
t_state_id external_lex_state) t_state_id external_lex_state)
{ {
const bool *valid_external_tokens = const bool *valid_external_tokens =
@ -463,12 +463,12 @@ 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, static bool ts_parser__can_reuse_first_leaf(t_parser *self, t_state_id state,
Subtree tree, Subtree tree,
TableEntry *table_entry) t_table_entry *table_entry)
{ {
t_lex_modes current_lex_mode = self->language->lex_modes[state]; t_lex_modes current_lex_mode = self->language->lex_modes[state];
t_symbol leaf_symbol = ts_subtree_leaf_symbol(tree); t_symbol leaf_symbol = ts_subtree_leaf_symbol(tree);
t_state_id leaf_state = ts_subtree_leaf_parse_state(tree); t_state_id leaf_state = ts_subtree_leaf_parse_state(tree);
t_lex_modes leaf_lex_mode = self->language->lex_modes[leaf_state]; t_lex_modes leaf_lex_mode = self->language->lex_modes[leaf_state];
// At the end of a non-terminal extra node, the lexer normally returns // At the end of a non-terminal extra node, the lexer normally returns
@ -506,25 +506,26 @@ static Subtree ts_parser__lex(t_parser *self, StackVersion version,
return NULL_SUBTREE; 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 = const Subtree external_token =
ts_stack_last_external_token(self->stack, version); ts_stack_last_external_token(self->stack, version);
bool found_external_token = false; bool found_external_token = false;
bool error_mode = parse_state == ERROR_STATE; bool error_mode = parse_state == ERROR_STATE;
bool skipped_error = false; bool skipped_error = false;
bool called_get_column = false; bool called_get_column = false;
t_i32 first_error_character = 0; t_i32 first_error_character = 0;
t_parse_length error_start_position = length_zero(); t_parse_length error_start_position = length_zero();
t_parse_length error_end_position = length_zero(); t_parse_length error_end_position = length_zero();
t_i32 lookahead_end_byte = 0; t_i32 lookahead_end_byte = 0;
t_i32 external_scanner_state_len = 0; t_i32 external_scanner_state_len = 0;
bool external_scanner_state_changed = false; bool external_scanner_state_changed = false;
bool found_token;
ts_lexer_reset(&self->lexer, start_position); ts_lexer_reset(&self->lexer, start_position);
for (;;) for (;;)
{ {
bool found_token = false;
t_parse_length current_position = self->lexer.current_position; t_parse_length current_position = self->lexer.current_position;
if (lex_mode.external_lex_state != 0) if (lex_mode.external_lex_state != 0)
@ -626,23 +627,24 @@ static Subtree ts_parser__lex(t_parser *self, StackVersion version,
Subtree result; Subtree result;
if (skipped_error) if (skipped_error)
{ {
t_parse_length padding = length_sub(error_start_position, start_position); t_parse_length padding =
t_parse_length size = length_sub(error_end_position, error_start_position); length_sub(error_start_position, start_position);
t_u32 lookahead_bytes = t_parse_length size =
lookahead_end_byte - error_end_position.bytes; 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, result = ts_subtree_new_error(&self->tree_pool, first_error_character,
padding, size, lookahead_bytes, padding, size, lookahead_bytes,
parse_state, self->language); parse_state, self->language);
} }
else else
{ {
bool is_keyword = false; bool is_keyword = false;
t_symbol symbol = self->lexer.data.result_symbol; t_symbol symbol = self->lexer.data.result_symbol;
t_parse_length padding = t_parse_length padding =
length_sub(self->lexer.token_start_position, start_position); length_sub(self->lexer.token_start_position, start_position);
t_parse_length size = length_sub(self->lexer.token_end_position, t_parse_length size = length_sub(self->lexer.token_end_position,
self->lexer.token_start_position); self->lexer.token_start_position);
t_u32 lookahead_bytes = t_u32 lookahead_bytes =
lookahead_end_byte - self->lexer.token_end_position.bytes; lookahead_end_byte - self->lexer.token_end_position.bytes;
if (found_external_token) if (found_external_token)
@ -688,9 +690,9 @@ static Subtree ts_parser__lex(t_parser *self, StackVersion version,
} }
static Subtree ts_parser__get_cached_token(t_parser *self, t_state_id state, static Subtree ts_parser__get_cached_token(t_parser *self, t_state_id state,
size_t position, size_t position,
Subtree last_external_token, Subtree last_external_token,
TableEntry *table_entry) t_table_entry *table_entry)
{ {
t_token_cache *cache = &self->token_cache; t_token_cache *cache = &self->token_cache;
if (cache->token.ptr && cache->byte_index == position && if (cache->token.ptr && cache->byte_index == position &&
@ -729,8 +731,8 @@ 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, static Subtree ts_parser__reuse_node(t_parser *self, StackVersion version,
t_state_id *state, t_u32 position, t_state_id *state, t_u32 position,
Subtree last_external_token, Subtree last_external_token,
TableEntry *table_entry) t_table_entry *table_entry)
{ {
Subtree result; Subtree result;
while ((result = reusable_node_tree(&self->reusable_node)).ptr) 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 // 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 // precedence, and finally, as a default, by a recursive comparison of the
// trees' symbols. // 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) if (!left.ptr)
return true; return true;
@ -901,7 +904,7 @@ static bool ts_parser__select_children(t_parser *self, Subtree left,
ts_subtree_symbol(left), &self->scratch_trees, 0, self->language); ts_subtree_symbol(left), &self->scratch_trees, 0, self->language);
return ts_parser__select_parse_tree(self, left, return ts_parser__select_parse_tree(self, left,
ts_subtree_from_mut(scratch_tree)); ts_subtree_from_mut(scratch_tree));
} }
static void ts_parser__shift(t_parser *self, StackVersion version, static void ts_parser__shift(t_parser *self, StackVersion version,
@ -928,7 +931,7 @@ static void ts_parser__shift(t_parser *self, StackVersion version,
static StackVersion ts_parser__reduce(t_parser *self, StackVersion version, static StackVersion ts_parser__reduce(t_parser *self, StackVersion version,
t_symbol symbol, t_u32 count, t_symbol symbol, t_u32 count,
int dynamic_precedence, int dynamic_precedence,
t_u16 production_id, bool is_fragile, t_u16 production_id, bool is_fragile,
bool end_of_non_terminal_extra) bool end_of_non_terminal_extra)
{ {
@ -940,7 +943,7 @@ static StackVersion ts_parser__reduce(t_parser *self, StackVersion version,
// contain the popped children, and push it onto the stack in place of the // contain the popped children, and push it onto the stack in place of the
// children. // children.
StackSliceArray pop = ts_stack_pop_count(self->stack, version, count); StackSliceArray pop = ts_stack_pop_count(self->stack, version, count);
t_u32 removed_version_count = 0; t_u32 removed_version_count = 0;
for (t_u32 i = 0; i < pop.size; i++) for (t_u32 i = 0; i < pop.size; i++)
{ {
StackSlice slice = pop.contents[i]; StackSlice slice = pop.contents[i];
@ -1074,7 +1077,7 @@ static void ts_parser__accept(t_parser *self, StackVersion version,
if (!ts_subtree_extra(tree)) if (!ts_subtree_extra(tree))
{ {
assert(!tree.data.is_inline); assert(!tree.data.is_inline);
t_u32 child_count = ts_subtree_child_count(tree); t_u32 child_count = ts_subtree_child_count(tree);
const Subtree *children = ts_subtree_children(tree); const Subtree *children = ts_subtree_children(tree);
for (t_u32 k = 0; k < child_count; k++) for (t_u32 k = 0; k < child_count; k++)
{ {
@ -1140,8 +1143,8 @@ static bool ts_parser__do_all_potential_reductions(
continue; continue;
t_state_id state = ts_stack_state(self->stack, version); t_state_id state = ts_stack_state(self->stack, version);
bool has_shift_action = false; bool has_shift_action = false;
array_clear(&self->reduce_actions); self->reduce_actions.len = 0;
t_symbol first_symbol, end_symbol; t_symbol first_symbol, end_symbol;
if (lookahead_symbol != 0) 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++) 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); ts_language_table_entry(self->language, state, symbol, &entry);
for (t_u32 j = 0; j < entry.action_count; j++) 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) if (action.reduce.child_count > 0)
ts_reduce_action_set_add( ts_reduce_action_set_add(
&self->reduce_actions, &self->reduce_actions,
(ReduceAction){ (t_reduce_action){
.symbol = action.reduce.symbol, .symbol = action.reduce.symbol,
.count = action.reduce.child_count, .count = action.reduce.child_count,
.dynamic_precedence = .dynamic_precedence =
@ -1188,9 +1191,9 @@ static bool ts_parser__do_all_potential_reductions(
} }
StackVersion reduction_version = STACK_VERSION_NONE; 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( reduction_version = ts_parser__reduce(
self, version, action.symbol, action.count, self, version, action.symbol, action.count,
@ -1255,8 +1258,8 @@ static bool ts_parser__recover_to_state(t_parser *self, StackVersion version,
if (error_trees.size > 0) if (error_trees.size > 0)
{ {
assert(error_trees.size == 1); assert(error_trees.size == 1);
Subtree error_tree = error_trees.contents[0]; Subtree error_tree = error_trees.contents[0];
t_u32 error_child_count = ts_subtree_child_count(error_tree); t_u32 error_child_count = ts_subtree_child_count(error_tree);
if (error_child_count > 0) if (error_child_count > 0)
{ {
array_splice(&slice.subtrees, 0, 0, error_child_count, array_splice(&slice.subtrees, 0, 0, error_child_count,
@ -1298,11 +1301,11 @@ static bool ts_parser__recover_to_state(t_parser *self, StackVersion version,
static void ts_parser__recover(t_parser *self, StackVersion version, static void ts_parser__recover(t_parser *self, StackVersion version,
Subtree lookahead) Subtree lookahead)
{ {
bool did_recover = false; bool did_recover = false;
unsigned previous_version_count = ts_stack_version_count(self->stack); unsigned previous_version_count = ts_stack_version_count(self->stack);
t_parse_length position = ts_stack_position(self->stack, version); t_parse_length position = ts_stack_position(self->stack, version);
StackSummary *summary = ts_stack_get_summary(self->stack, version); StackSummary *summary = ts_stack_get_summary(self->stack, version);
unsigned node_count_since_error = unsigned node_count_since_error =
ts_stack_node_count_since_error(self->stack, version); ts_stack_node_count_since_error(self->stack, version);
unsigned current_error_cost = ts_stack_error_cost(self->stack, version); unsigned current_error_cost = ts_stack_error_cost(self->stack, version);
@ -1440,7 +1443,7 @@ static void ts_parser__recover(t_parser *self, StackVersion version,
// If the current lookahead token is an extra token, mark it as extra. This // If the current lookahead token is an extra token, mark it as extra. This
// means it won't be counted in error cost calculations. // means it won't be counted in error cost calculations.
unsigned n; unsigned n;
const t_parse_actions *actions = ts_language_actions( const t_parse_actions *actions = ts_language_actions(
self->language, 1, ts_subtree_symbol(lookahead), &n); self->language, 1, ts_subtree_symbol(lookahead), &n);
if (n > 0 && actions[n - 1].type == ActionTypeShift && if (n > 0 && actions[n - 1].type == ActionTypeShift &&
@ -1514,8 +1517,8 @@ static void ts_parser__handle_error(t_parser *self, StackVersion version,
// lookahead. After skipping one or more invalid tokens, the parser might // lookahead. After skipping one or more invalid tokens, the parser might
// find a token that would have allowed a reduction to take place. // find a token that would have allowed a reduction to take place.
ts_parser__do_all_potential_reductions(self, version, 0); ts_parser__do_all_potential_reductions(self, version, 0);
t_u32 version_count = ts_stack_version_count(self->stack); t_u32 version_count = ts_stack_version_count(self->stack);
t_parse_length position = ts_stack_position(self->stack, version); t_parse_length position = ts_stack_position(self->stack, version);
// Push a discontinuity onto the stack. Merge all of the stack versions that // Push a discontinuity onto the stack. Merge all of the stack versions that
// were created in the previous step. // were created in the previous step.
@ -1610,13 +1613,13 @@ static bool ts_parser__advance(t_parser *self, StackVersion version,
bool allow_node_reuse) bool allow_node_reuse)
{ {
t_state_id state = ts_stack_state(self->stack, version); t_state_id state = ts_stack_state(self->stack, version);
t_u32 position = ts_stack_position(self->stack, version).bytes; t_u32 position = ts_stack_position(self->stack, version).bytes;
Subtree last_external_token = Subtree last_external_token =
ts_stack_last_external_token(self->stack, version); ts_stack_last_external_token(self->stack, version);
bool did_reuse = true; bool did_reuse = true;
Subtree lookahead = NULL_SUBTREE; 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 possible, reuse a node from the previous syntax tree.
if (allow_node_reuse) if (allow_node_reuse)
@ -1980,8 +1983,7 @@ t_parser *ts_parser_new(void)
{ {
t_parser *self = calloc(1, sizeof(t_parser)); t_parser *self = calloc(1, sizeof(t_parser));
ts_lexer_init(&self->lexer); ts_lexer_init(&self->lexer);
array_init(&self->reduce_actions); self->reduce_actions = vec_reduce_action_new(4, NULL);
array_reserve(&self->reduce_actions, 4);
self->tree_pool = ts_subtree_pool_new(32); self->tree_pool = ts_subtree_pool_new(32);
self->stack = ts_stack_new(&self->tree_pool); self->stack = ts_stack_new(&self->tree_pool);
self->finished_tree = NULL_SUBTREE; self->finished_tree = NULL_SUBTREE;
@ -2008,14 +2010,10 @@ void ts_parser_delete(t_parser *self)
ts_parser_set_language(self, NULL); ts_parser_set_language(self, NULL);
ts_stack_delete(self->stack); ts_stack_delete(self->stack);
if (self->reduce_actions.contents) if (self->reduce_actions.buffer)
{ vec_reduce_action_free(self->reduce_actions);
array_delete(&self->reduce_actions);
}
if (self->included_range_differences.buffer) if (self->included_range_differences.buffer)
{
array_delete(&self->included_range_differences); array_delete(&self->included_range_differences);
}
if (self->old_tree.ptr) if (self->old_tree.ptr)
{ {
ts_subtree_release(&self->tree_pool, self->old_tree); ts_subtree_release(&self->tree_pool, self->old_tree);
@ -2113,7 +2111,7 @@ bool ts_parser_set_included_ranges(t_parser *self, const t_parser_range *ranges,
} }
const t_parser_range *ts_parser_included_ranges(const t_parser *self, const t_parser_range *ts_parser_included_ranges(const t_parser *self,
t_u32 *count) t_u32 *count)
{ {
return ts_lexer_included_ranges(&self->lexer, count); return ts_lexer_included_ranges(&self->lexer, count);
} }
@ -2141,7 +2139,8 @@ void ts_parser_reset(t_parser *self)
self->has_scanner_error = false; 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; t_parse_tree *result = NULL;
old_tree = NULL; old_tree = NULL;
@ -2254,16 +2253,18 @@ exit:
return result; 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 char *string, t_u32 length) const t_parse_tree *old_tree,
const char *string, t_u32 length)
{ {
return ts_parser_parse_string_encoding(self, old_tree, string, length, return ts_parser_parse_string_encoding(self, old_tree, string, length,
InputEncoding8); 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 char *string, t_u32 length, const t_parse_tree *old_tree,
t_input_encoding encoding) const char *string, t_u32 length,
t_input_encoding encoding)
{ {
t_string_input input = {string, length}; t_string_input input = {string, length};
return ts_parser_parse(self, old_tree, return ts_parser_parse(self, old_tree,

View file

@ -1,62 +0,0 @@
#ifndef TREE_SITTER_POINT_H_
#define TREE_SITTER_POINT_H_
#include "parser/api.h"
#define POINT_ZERO ((t_point) {0, 0})
#define POINT_MAX ((t_point) {UINT32_MAX, UINT32_MAX})
static inline t_point point__new(unsigned row, unsigned column) {
t_point result = {row, column};
return result;
}
static inline t_point point_add(t_point a, t_point 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 t_point point_sub(t_point a, t_point 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(t_point a, t_point b) {
return (a.row < b.row) || (a.row == b.row && a.column <= b.column);
}
static inline bool point_lt(t_point a, t_point b) {
return (a.row < b.row) || (a.row == b.row && a.column < b.column);
}
static inline bool point_gt(t_point a, t_point b) {
return (a.row > b.row) || (a.row == b.row && a.column > b.column);
}
static inline bool point_gte(t_point a, t_point b) {
return (a.row > b.row) || (a.row == b.row && a.column >= b.column);
}
static inline bool point_eq(t_point a, t_point b) {
return a.row == b.row && a.column == b.column;
}
static inline t_point point_min(t_point a, t_point b) {
if (a.row < b.row || (a.row == b.row && a.column < b.column))
return a;
else
return b;
}
static inline t_point point_max(t_point a, t_point b) {
if (a.row > b.row || (a.row == b.row && a.column > b.column))
return a;
else
return b;
}
#endif

View file

@ -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_

View file

@ -7,7 +7,7 @@ extern "C" {
#include "./array.h" #include "./array.h"
#include "./subtree.h" #include "./subtree.h"
#include "./error_costs.h" #include "parser/error_costs.h"
#include <stdio.h> #include <stdio.h>
typedef struct Stack Stack; typedef struct Stack Stack;

View file

@ -7,7 +7,7 @@
#include "./array.h" #include "./array.h"
#include "./error_costs.h" #include "parser/error_costs.h"
#include "./language.h" #include "./language.h"
#include "parser/parser_length.h" #include "parser/parser_length.h"
#include "./subtree.h" #include "./subtree.h"

View file

@ -5,9 +5,8 @@
#include "parser/types/types_symbol.h" #include "parser/types/types_symbol.h"
#include "./array.h" #include "./array.h"
#include "./error_costs.h" #include "parser/error_costs.h"
#include "parser/parser_length.h" #include "parser/parser_length.h"
#include "./parser.h"
#include "parser/api.h" #include "parser/api.h"
#include <limits.h> #include <limits.h>
#include <stdbool.h> #include <stdbool.h>

View file

@ -66,7 +66,7 @@ void ts_tree_edit(t_parse_tree *self, const t_input_edit *edit) {
); );
if (range->end_byte < edit->new_end_byte) { if (range->end_byte < edit->new_end_byte) {
range->end_byte = UINT32_MAX; range->end_byte = UINT32_MAX;
range->end_point = POINT_MAX; range->end_point = point_val_max();
} }
} }
} else if (range->end_byte > edit->start_byte) { } else if (range->end_byte > edit->start_byte) {
@ -81,7 +81,7 @@ void ts_tree_edit(t_parse_tree *self, const t_input_edit *edit) {
); );
if (range->start_byte < edit->new_end_byte) { if (range->start_byte < edit->new_end_byte) {
range->start_byte = UINT32_MAX; range->start_byte = UINT32_MAX;
range->start_point = POINT_MAX; range->start_point = point_val_max();
} }
} else if (range->start_byte > edit->start_byte) { } else if (range->start_byte > edit->start_byte) {
range->start_byte = edit->start_byte; range->start_byte = edit->start_byte;

View file

@ -3,29 +3,25 @@
#include "./subtree.h" #include "./subtree.h"
#ifdef __cplusplus typedef struct
extern "C" { {
#endif const Subtree *child;
const Subtree *parent;
typedef struct { t_parse_length position;
const Subtree *child; t_symbol alias_symbol;
const Subtree *parent;
t_parse_length position;
t_symbol alias_symbol;
} ParentCacheEntry; } ParentCacheEntry;
struct t_parse_tree { struct s_parse_tree
Subtree root; {
const t_language *language; Subtree root;
t_parser_range *included_ranges; const t_language *language;
unsigned included_range_count; t_parser_range *included_ranges;
t_u32 included_range_count;
}; };
t_parse_tree *ts_tree_new(Subtree root, const t_language *language, const t_parser_range *, unsigned); t_parse_tree *ts_tree_new(Subtree root, const t_language *language,
t_parse_node ts_node_new(const t_parse_tree *, const Subtree *, t_parse_length, t_symbol); const t_parser_range *, t_u32);
t_parse_node ts_node_new(const t_parse_tree *, const Subtree *, t_parse_length,
t_symbol);
#ifdef __cplusplus #endif // TREE_SITTER_TREE_H_
}
#endif
#endif // TREE_SITTER_TREE_H_

View file

@ -299,7 +299,7 @@ static inline t_i64 ts_tree_cursor_goto_first_child_for_byte_and_point(
} }
t_i64 ts_tree_cursor_goto_first_child_for_byte(t_parse_tree_cursor *self, t_u32 goal_byte) { t_i64 ts_tree_cursor_goto_first_child_for_byte(t_parse_tree_cursor *self, t_u32 goal_byte) {
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, goal_byte, POINT_ZERO); return ts_tree_cursor_goto_first_child_for_byte_and_point(self, goal_byte, point_val_zero());
} }
t_i64 ts_tree_cursor_goto_first_child_for_point(t_parse_tree_cursor *self, t_point goal_point) { t_i64 ts_tree_cursor_goto_first_child_for_point(t_parse_tree_cursor *self, t_point goal_point) {

View file

@ -6,22 +6,22 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */ /* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */
/* Updated: 2024/04/13 20:15:37 by rparodi ### ########.fr */ /* Updated: 2024/04/30 16:16:55 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/minishell.h" #include "../includes/minishell.h"
void ft_free(void *ptr) void ft_free(void *ptr)
{ {
if (!ptr) if (!ptr)
free(ptr); free(ptr);
ptr = NULL; ptr = NULL;
} }
void ft_free_strs(t_str *strs) void ft_free_strs(t_str *strs)
{ {
t_usize i; t_usize i;
i = 0; i = 0;
while (strs[i]) while (strs[i])
@ -32,20 +32,15 @@ void ft_free_strs(t_str *strs)
ft_free(strs); ft_free(strs);
} }
void ft_free_utils(t_utils *s) void ft_free_utils(t_utils *s)
{ {
if (s->name_shell) (void)(s);
ft_free(s->name_shell);
if (s->str_input) if (s->str_input)
ft_free(s->str_input); free(s->str_input);
if (s->strs_input) ts_parser_delete(s->parser.parser);
ft_free_strs(s->strs_input);
if (s->path)
ft_free_strs(s->path);
free(s);
} }
void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status) void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status)
{ {
if (maiboyerlpb != NULL) if (maiboyerlpb != NULL)
ft_free_utils(maiboyerlpb); ft_free_utils(maiboyerlpb);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/04/30 15:46:55 by rparodi ### ########.fr */ /* Updated: 2024/04/30 16:15:53 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,6 +67,7 @@ void ft_check(t_utils *shcat, char **input) {
void exec_shcat(t_utils *shcat) void exec_shcat(t_utils *shcat)
{ {
print_node_data(&shcat->current_node, 0); print_node_data(&shcat->current_node, 0);
free_node(shcat->current_node);
} }
void ft_take_args(t_utils *shcat) void ft_take_args(t_utils *shcat)
@ -81,7 +82,6 @@ void ft_take_args(t_utils *shcat)
shcat->current_node = parse_str(&shcat->parser, shcat->str_input); shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
exec_shcat(shcat); exec_shcat(shcat);
add_history(shcat->str_input); add_history(shcat->str_input);
ft_free_strs(shcat->strs_input);
free(shcat->str_input); free(shcat->str_input);
i++; i++;
} }
@ -138,7 +138,4 @@ t_i32 main(t_i32 argc, t_str argv[], t_str arge[])
ft_find_path(arge, &utils); ft_find_path(arge, &utils);
utils.name_shell = "42sh > "; utils.name_shell = "42sh > ";
ft_take_args(&utils); ft_take_args(&utils);
// node = parse_string(&parser, "banane \"$VAR\"'truc'");
// print_node_data(&node, 0);
// free_node(node);
} }