This commit is contained in:
Maieul BOYER 2024-05-29 16:41:40 +02:00
parent d16b39091a
commit ff1670e264
No known key found for this signature in database
15 changed files with 435 additions and 243 deletions

View file

@ -15,7 +15,6 @@
#include "ast/forward.h" #include "ast/forward.h"
#include "me/types.h" #include "me/types.h"
#include <iso646.h>
/// @brief Node types enumeration /// @brief Node types enumeration
/// @details This enumeration is used to identify the type of a node /// @details This enumeration is used to identify the type of a node
@ -36,6 +35,7 @@ enum e_ast_type
TY_COMMAND_BACKTICKS, TY_COMMAND_BACKTICKS,
TY_COMMAND_SUBSTITUTION, TY_COMMAND_SUBSTITUTION,
TY_COMPOUND_LIST, TY_COMPOUND_LIST,
TY_DOUBLE_QUOTE_STRING,
TY_ELIF_CLAUSE, TY_ELIF_CLAUSE,
TY_ELSE_CLAUSE, TY_ELSE_CLAUSE,
TY_FOR_COMMAND, TY_FOR_COMMAND,
@ -201,7 +201,7 @@ struct s_not
struct s_pipe_list struct s_pipe_list
{ {
t_ast_type type; t_ast_type type;
t_command *cmds; t_ast_node **cmds;
t_usize cmds_len; t_usize cmds_len;
}; };
@ -433,7 +433,7 @@ struct s_assignment
struct s_ast_string struct s_ast_string
{ {
t_ast_type type; t_ast_type type;
t_str *value; t_str value;
}; };
struct s_name struct s_name
@ -490,6 +490,14 @@ union u_expension {
t_arithmetic_expansion *arithmetic_expansion; t_arithmetic_expansion *arithmetic_expansion;
t_command_substitution *command_substitution; t_command_substitution *command_substitution;
t_command_backticks *command_backticks; t_command_backticks *command_backticks;
t_double_quote_string *double_quote_string;
};
struct s_double_quote_string
{
t_ast_type type;
t_expension_or_string *parts;
t_usize parts_len;
}; };
struct s_parameter_expansion struct s_parameter_expansion
@ -498,7 +506,7 @@ struct s_parameter_expansion
t_op_in op_pre; t_op_in op_pre;
t_ast_string *param; t_ast_string *param;
t_op_in op_in; t_op_in op_in;
t_ast_string *_Nullable word; t_word *_Nullable word;
}; };
struct s_arithmetic_expansion struct s_arithmetic_expansion
@ -536,6 +544,7 @@ union u_ast_node {
t_command_backticks command_backticks; t_command_backticks command_backticks;
t_command_substitution command_substitution; t_command_substitution command_substitution;
t_compound_list compound_list; t_compound_list compound_list;
t_double_quote_string double_quote_string;
t_elif_clause elif_clause; t_elif_clause elif_clause;
t_else_clause else_clause; t_else_clause else_clause;
t_for_command for_command; t_for_command for_command;

View file

@ -36,6 +36,7 @@ typedef struct s_command_backticks t_command_backticks;
typedef struct s_command_substitution t_command_substitution; typedef struct s_command_substitution t_command_substitution;
typedef struct s_command t_command; typedef struct s_command t_command;
typedef struct s_compound_list t_compound_list; typedef struct s_compound_list t_compound_list;
typedef struct s_double_quote_string t_double_quote_string;
typedef struct s_elif_clause t_elif_clause; typedef struct s_elif_clause t_elif_clause;
typedef struct s_else_clause t_else_clause; typedef struct s_else_clause t_else_clause;
typedef struct s_for_command t_for_command; typedef struct s_for_command t_for_command;

View file

@ -13,6 +13,7 @@
#ifndef FROM_PROGRAM_C #ifndef FROM_PROGRAM_C
#define FROM_PROGRAM_C #define FROM_PROGRAM_C
#include "forward.h"
#include "me/types.h" #include "me/types.h"
#include "app/node.h" #include "app/node.h"
@ -90,6 +91,11 @@ t_command *build_command(t_node *node, t_usize size);
/// @note can be null in case of error /// @note can be null in case of error
t_compound_list *build_compound_list(t_node *node, t_usize size); t_compound_list *build_compound_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_double_quote_string *build_double_quote_string(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size` /// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes /// @param size the size of the nodes
/// @note can be null in case of error /// @note can be null in case of error
@ -190,7 +196,6 @@ t_while_command *build_while_command(t_node *node, t_usize size);
/// @note can be null in case of error /// @note can be null in case of error
t_word *build_word(t_node *node, t_usize size); t_word *build_word(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size` /// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes /// @param size the size of the nodes
/// @note can be null in case of error /// @note can be null in case of error

View file

@ -1,2 +1 @@
build_ast
from_node from_node

View file

@ -1,13 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* build_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 20:41:33 by maiboyer #+# #+# */
/* Updated: 2024/05/25 20:41:48 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast/ast.h"

View file

@ -6,10 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 13:18:44 by maiboyer #+# #+# */ /* Created: 2024/05/28 13:18:44 by maiboyer #+# #+# */
/* Updated: 2024/05/29 00:51:19 by maiboyer ### ########.fr */ /* Updated: 2024/05/29 13:41:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
#include "app/node.h" #include "app/node.h"
@ -17,6 +18,15 @@
#include "gmr/symbols.h" #include "gmr/symbols.h"
#include <stdio.h> #include <stdio.h>
t_ast_node *alloc_node(t_ast_type ty)
{
t_ast_node *ptr;
ptr = mem_alloc(sizeof(*ptr));
ptr->type = ty;
return (ptr);
}
t_ast_node *build_comment(t_node *node, t_usize size) t_ast_node *build_comment(t_node *node, t_usize size)
{ {
t_ast_node *ptr; t_ast_node *ptr;
@ -24,8 +34,153 @@ t_ast_node *build_comment(t_node *node, t_usize size)
(void)(node); (void)(node);
(void)(size); (void)(size);
ptr = mem_alloc(sizeof(*ptr)); ptr = alloc_node(TY_EMPTY);
ptr->type = TY_EMPTY; return (ptr);
}
t_and_list *build_and_list(t_node *node, t_usize size)
{
t_and_list *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_list || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_AND_LIST);
ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
{
if ((node->childs[i].kind == anon_sym_AMP_AMP ||
node->childs[i].kind == anon_sym_PIPE_PIPE) &&
(i++, true))
continue;
ptr->cmds[j++].cmd = (void *)from_node(&node->childs[i]);
i++;
}
ptr->cmds_len = j;
return (ptr);
}
t_or_list *build_or_list(t_node *node, t_usize size)
{
t_or_list *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_list || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_OR_LIST);
ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
{
if ((node->childs[i].kind == anon_sym_AMP_AMP ||
node->childs[i].kind == anon_sym_PIPE_PIPE) &&
(i++, true))
continue;
ptr->cmds[j++].cmd = (void *)from_node(&node->childs[i]);
i++;
}
ptr->cmds_len = j;
return (ptr);
}
t_ast_string *build_ast_string(t_node *node, t_usize size)
{
t_ast_string *ptr;
(void)(size);
if (node == NULL ||
(node->kind != sym_string_content && node->kind != sym_raw_string &&
node->kind == sym_ansi_c_string) ||
size == 0)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_AST_STRING);
ptr->value = str_clone(node_getstr(node));
return (ptr);
}
t_and_or_list *build_and_or_list(t_node *node, t_usize size)
{
t_and_or_list *ptr;
(void)(node);
(void)(size);
if (node == NULL || node->kind != sym_list || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_AND_OR_LIST);
if (node->childs[1].kind == anon_sym_PIPE_PIPE)
ptr->cmds.or_list = build_or_list(node, 1);
else if (node->childs[1].kind == anon_sym_AMP_AMP)
ptr->cmds.and_list = build_and_list(node, 1);
else
return (mem_free(ptr), NULL);
return (ptr);
}
t_pipe_list *build_pipe_list(t_node *node, t_usize size)
{
t_pipe_list *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_pipeline || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_PIPE_LIST);
ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
{
if (node->childs[i].kind == anon_sym_PIPE)
i++;
ptr->cmds[j++] = from_node(&node->childs[i]);
i++;
}
return (ptr);
}
t_double_quote_string *build_double_qoute_string(t_node *node, t_usize size)
{
t_double_quote_string *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_string || size == 0)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_DOUBLE_QUOTE_STRING);
ptr->parts = mem_alloc_array(sizeof(*ptr->parts), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
ptr->parts[j++].expension->type = (void *)from_node(&node->childs[i++]);
ptr->parts_len = j;
return (ptr);
}
t_not *build_not(t_node *node, t_usize size)
{
t_not *ptr;
(void)(size);
if (node == NULL || node->kind != sym_negated_command || size == 0)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_NOT);
ptr->cmd.type = (void *)from_node(&node->childs[1]);
return (ptr); return (ptr);
} }
@ -36,9 +191,10 @@ t_ast_node *from_node(t_node *node)
if (node->kind == sym_program) if (node->kind == sym_program)
return ((t_ast_node *)build_program(node, 1)); return ((t_ast_node *)build_program(node, 1));
if (node->kind == sym_word)
return ((t_ast_node *)build_word(node, 1));
if (node->kind == sym_string) if (node->kind == sym_string)
return ((t_ast_node *)build_double_quote_string(node, 1));
if (node->kind == sym_word)
return ((t_ast_node *)build_word(node, 1)); return ((t_ast_node *)build_word(node, 1));
if (node->kind == sym_concatenation) if (node->kind == sym_concatenation)
return ((t_ast_node *)build_word(node, 1)); return ((t_ast_node *)build_word(node, 1));
@ -81,6 +237,12 @@ t_ast_node *from_node(t_node *node)
return ((t_ast_node *)build_command(node, 1)); return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_command) if (node->kind == sym_command)
return ((t_ast_node *)build_command(node, 1)); return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_test_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_declaration_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_unset_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_file_redirect) if (node->kind == sym_file_redirect)
return ((t_ast_node *)build_redirect_file(node, 1)); return ((t_ast_node *)build_redirect_file(node, 1));
@ -100,6 +262,35 @@ t_ast_node *from_node(t_node *node)
if (node->kind == sym_brace_expression) if (node->kind == sym_brace_expression)
return ((t_ast_node *)build_brace_command(node, 1)); return ((t_ast_node *)build_brace_command(node, 1));
if (node->kind == sym_compound_statement)
return ((t_ast_node *)build_compound_list(node, 1));
if (node->kind == sym_if_statement)
return ((t_ast_node *)build_if_command(node, 1));
if (node->kind == sym_else_clause)
return ((t_ast_node *)build_else_clause(node, 1));
if (node->kind == sym_elif_clause)
return ((t_ast_node *)build_elif_clause(node, 1));
if (node->kind == sym_case_statement)
return ((t_ast_node *)build_case_command(node, 1));
if (node->kind == sym_case_item)
return ((t_ast_node *)build_case_item(node, 1));
if (node->kind == sym_last_case_item)
return ((t_ast_node *)build_case_item(node, 1));
if (node->kind == sym_while_statement)
return ((t_ast_node *)build_while_command(node, 1));
if (node->kind == sym_for_statement)
return ((t_ast_node *)build_for_command(node, 1));
if (node->kind == sym_function_definition)
return ((t_ast_node *)build_function_definition(node, 1));
printf("unknown node of kind '%s'\n", node->kind_str); printf("unknown node of kind '%s'\n", node->kind_str);
return (NULL); return (NULL);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 18:35:22 by maiboyer #+# #+# */ /* Created: 2024/04/28 18:35:22 by maiboyer #+# #+# */
/* Updated: 2024/05/09 16:23:54 by rparodi ### ########.fr */ /* Updated: 2024/05/29 14:46:20 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,11 @@
typedef struct s_node typedef struct s_node
{ {
t_u64 kind; t_u64 kind;
const char *kind_str; t_const_str kind_str;
const char *input; t_const_str field_str;
char *single_str; t_u64 field;
t_const_str input;
t_str single_str;
t_usize start; t_usize start;
t_usize end; t_usize end;
t_usize childs_count; t_usize childs_count;

View file

@ -3,12 +3,12 @@
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include <assert.h> #include <assert.h>
#include <ctype.h>
#include <limits.h> #include <limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "me/char/char.h"
#define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1) #define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1)
#define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14 #define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14

View file

@ -487,7 +487,8 @@ unsigned ts_subtree_get_changed_ranges(
#endif #endif
// Compare the old and new subtrees. // Compare the old and new subtrees.
t_iterator_comparison comparison = iterator_compare(&old_iter, &new_iter); t_iterator_comparison comparison =
iterator_compare(&old_iter, &new_iter);
// Even if the two subtrees appear to be identical, they could differ // Even if the two subtrees appear to be identical, they could differ
// internally if they contain a range of text that was previously // internally if they contain a range of text that was previously
@ -2422,7 +2423,8 @@ static void ts_parser__breakdown_lookahead(t_first_parser *self,
} }
static t_error_comparaison ts_parser__compare_versions(t_first_parser *self, static t_error_comparaison ts_parser__compare_versions(t_first_parser *self,
t_error_status a, t_error_status b) t_error_status a,
t_error_status b)
{ {
(void)self; (void)self;
if (!a.is_in_error && b.is_in_error) if (!a.is_in_error && b.is_in_error)
@ -2643,7 +2645,8 @@ static bool ts_parser__can_reuse_first_leaf(t_first_parser *self,
return current_lex_mode.external_lex_state == 0 && table_entry->is_reusable; return current_lex_mode.external_lex_state == 0 && table_entry->is_reusable;
} }
const t_external_scanner_state *ts_subtree_external_scanner_state(t_subtree self) const t_external_scanner_state *ts_subtree_external_scanner_state(
t_subtree self)
{ {
static const t_external_scanner_state empty_state = {{.short_data = {0}}, static const t_external_scanner_state empty_state = {{.short_data = {0}},
.length = 0}; .length = 0};
@ -4493,10 +4496,10 @@ typedef struct s_symbol_table
typedef Array(uint8_t) t_capture_quantifiers; typedef Array(uint8_t) t_capture_quantifiers;
/* /*
* t_pattern_entry - Information about the starting point for matching a particular * t_pattern_entry - Information about the starting point for matching a
* pattern. These entries are stored in a 'pattern map' - a sorted array that * particular pattern. These entries are stored in a 'pattern map' - a sorted
* makes it possible to efficiently lookup patterns based on the symbol for * array that makes it possible to efficiently lookup patterns based on the
* their first step. The entry consists of the following fields: * symbol for their first step. The entry consists of the following fields:
* - `pattern_index` - the index of the pattern within the query * - `pattern_index` - the index of the pattern within the query
* - `step_index` - the index of the pattern's first step in the shared `steps` * - `step_index` - the index of the pattern's first step in the shared `steps`
* array * array
@ -4567,10 +4570,10 @@ typedef struct s_query_state
typedef Array(t_query_capture) t_capture_list; typedef Array(t_query_capture) t_capture_list;
/* /*
* t_capture_list_pool - A collection of *lists* of captures. Each query state needs * t_capture_list_pool - A collection of *lists* of captures. Each query state
* to maintain its own list of captures. To avoid repeated allocations, this * needs to maintain its own list of captures. To avoid repeated allocations,
* struct maintains a fixed set of capture lists, and keeps track of which ones * this struct maintains a fixed set of capture lists, and keeps track of which
* are currently in use by a query state. * ones are currently in use by a query state.
*/ */
typedef struct s_capture_list_pool typedef struct s_capture_list_pool
{ {
@ -4588,8 +4591,9 @@ typedef struct s_capture_list_pool
} t_capture_list_pool; } t_capture_list_pool;
/* /*
* t_analysis_state - The state needed for walking the parse table when analyzing * t_analysis_state - The state needed for walking the parse table when
* a query pattern, to determine at which steps the pattern might fail to match. * analyzing a query pattern, to determine at which steps the pattern might fail
* to match.
*/ */
typedef struct s_analysis_state_entry typedef struct s_analysis_state_entry
{ {
@ -4645,9 +4649,9 @@ typedef struct s_analysis_subgraph
typedef Array(t_analysis_subgraph) t_analysis_subgraph_array; typedef Array(t_analysis_subgraph) t_analysis_subgraph_array;
/* /*
* t_state_predecessor_map - A map that stores the predecessors of each parse state. * t_state_predecessor_map - A map that stores the predecessors of each parse
* This is used during query analysis to determine which parse states can lead * state. This is used during query analysis to determine which parse states can
* to which reduce actions. * lead to which reduce actions.
*/ */
typedef struct s_state_predecessor_map typedef struct s_state_predecessor_map
{ {
@ -4756,7 +4760,7 @@ static void stream_skip_whitespace(t_stream *self)
{ {
for (;;) for (;;)
{ {
if (isspace(self->next)) if (me_isspace(self->next))
{ {
stream_advance(self); stream_advance(self);
} }
@ -4779,7 +4783,7 @@ static void stream_skip_whitespace(t_stream *self)
static bool stream_is_ident_start(t_stream *self) static bool stream_is_ident_start(t_stream *self)
{ {
return isalnum(self->next) || self->next == '_' || self->next == '-'; return me_isalnum(self->next) || self->next == '_' || self->next == '-';
} }
static void stream_scan_identifier(t_stream *stream) static void stream_scan_identifier(t_stream *stream)
@ -4787,7 +4791,7 @@ static void stream_scan_identifier(t_stream *stream)
do do
{ {
stream_advance(stream); stream_advance(stream);
} while (isalnum(stream->next) || stream->next == '_' || } while (me_isalnum(stream->next) || stream->next == '_' ||
stream->next == '-' || stream->next == '.' || stream->next == '-' || stream->next == '.' ||
stream->next == '?' || stream->next == '!'); stream->next == '?' || stream->next == '!');
} }
@ -4830,8 +4834,8 @@ static void capture_list_pool_delete(t_capture_list_pool *self)
array_delete(&self->list); array_delete(&self->list);
} }
static const t_capture_list *capture_list_pool_get(const t_capture_list_pool *self, static const t_capture_list *capture_list_pool_get(
uint16_t id) const t_capture_list_pool *self, uint16_t id)
{ {
if (id >= self->list.size) if (id >= self->list.size)
return &self->empty_list; return &self->empty_list;
@ -5178,8 +5182,8 @@ static void symbol_table_delete(t_symbol_table *self)
array_delete(&self->slices); array_delete(&self->slices);
} }
static int symbol_table_id_for_name(const t_symbol_table *self, const char *name, static int symbol_table_id_for_name(const t_symbol_table *self,
uint32_t length) const char *name, uint32_t length)
{ {
for (unsigned i = 0; i < self->slices.size; i++) for (unsigned i = 0; i < self->slices.size; i++)
{ {
@ -5340,8 +5344,8 @@ static unsigned analysis_state__recursion_depth(const t_analysis_state *self)
return result; return result;
} }
static inline int analysis_state__compare_position(t_analysis_state *const *self, static inline int analysis_state__compare_position(
t_analysis_state *const *other) t_analysis_state *const *self, t_analysis_state *const *other)
{ {
for (unsigned i = 0; i < (*self)->depth; i++) for (unsigned i = 0; i < (*self)->depth; i++)
{ {
@ -5385,7 +5389,8 @@ static inline int analysis_state__compare(t_analysis_state *const *self,
return 0; return 0;
} }
static inline t_analysis_state_entry *analysis_state__top(t_analysis_state *self) static inline t_analysis_state_entry *analysis_state__top(
t_analysis_state *self)
{ {
if (self->depth == 0) if (self->depth == 0)
{ {
@ -5409,8 +5414,8 @@ static inline bool analysis_state__has_supertype(t_analysis_state *self,
* t_analysis_state_set * t_analysis_state_set
******************/ ******************/
// Obtains an `t_analysis_state` instance, either by consuming one from this set's // Obtains an `t_analysis_state` instance, either by consuming one from this
// object pool, or by cloning one from scratch. // set's object pool, or by cloning one from scratch.
static inline t_analysis_state *analysis_state_pool__clone_or_reuse( static inline t_analysis_state *analysis_state_pool__clone_or_reuse(
t_analysis_state_set *self, t_analysis_state *borrowed_item) t_analysis_state_set *self, t_analysis_state *borrowed_item)
{ {
@ -5633,8 +5638,8 @@ static inline void ts_query__pattern_map_insert(t_parse_query *self,
// Walk the subgraph for this non-terminal, tracking all of the possible // Walk the subgraph for this non-terminal, tracking all of the possible
// sequences of progress within the pattern. // sequences of progress within the pattern.
static void ts_query__perform_analysis(t_parse_query *self, static void ts_query__perform_analysis(
const t_analysis_subgraph_array *subgraphs, t_parse_query *self, const t_analysis_subgraph_array *subgraphs,
t_query_analysis *analysis) t_query_analysis *analysis)
{ {
unsigned recursion_depth_limit = 0; unsigned recursion_depth_limit = 0;
@ -7873,7 +7878,8 @@ static bool ts_query_cursor__first_in_progress_capture(
(node_start_byte == *byte_offset && (node_start_byte == *byte_offset &&
state->pattern_index < *pattern_index)) state->pattern_index < *pattern_index))
{ {
t_query_step *step = &self->query->steps.contents[state->step_index]; t_query_step *step =
&self->query->steps.contents[state->step_index];
if (root_pattern_guaranteed) if (root_pattern_guaranteed)
{ {
*root_pattern_guaranteed = step->root_pattern_guaranteed; *root_pattern_guaranteed = step->root_pattern_guaranteed;
@ -8047,7 +8053,8 @@ static void ts_query_cursor__add_state(t_query_cursor *self,
// the pool, this will steal the capture list from another existing state, and // the pool, this will steal the capture list from another existing state, and
// mark that other state as 'dead'. // mark that other state as 'dead'.
static t_capture_list *ts_query_cursor__prepare_to_capture( static t_capture_list *ts_query_cursor__prepare_to_capture(
t_query_cursor *self, t_query_state *state, unsigned state_index_to_preserve) t_query_cursor *self, t_query_state *state,
unsigned state_index_to_preserve)
{ {
if (state->capture_list_id == NONE) if (state->capture_list_id == NONE)
{ {
@ -8067,7 +8074,8 @@ static t_capture_list *ts_query_cursor__prepare_to_capture(
{ {
LOG(" abandon state. index:%u, pattern:%u, offset:%u.\n", LOG(" abandon state. index:%u, pattern:%u, offset:%u.\n",
state_index, pattern_index, byte_offset); state_index, pattern_index, byte_offset);
t_query_state *other_state = &self->states.contents[state_index]; t_query_state *other_state =
&self->states.contents[state_index];
state->capture_list_id = other_state->capture_list_id; state->capture_list_id = other_state->capture_list_id;
other_state->capture_list_id = NONE; other_state->capture_list_id = NONE;
other_state->dead = true; other_state->dead = true;
@ -8156,7 +8164,8 @@ static inline bool ts_query_cursor__should_descend(t_query_cursor *self,
{ {
t_query_state *state = &self->states.contents[i]; t_query_state *state = &self->states.contents[i];
; ;
t_query_step *next_step = &self->query->steps.contents[state->step_index]; t_query_step *next_step =
&self->query->steps.contents[state->step_index];
if (next_step->depth != PATTERN_DONE_MARKER && if (next_step->depth != PATTERN_DONE_MARKER &&
state->start_depth + next_step->depth > self->depth) state->start_depth + next_step->depth > self->depth)
{ {
@ -9117,9 +9126,9 @@ static uint32_t stack__subtree_node_count(t_subtree subtree)
return count; return count;
} }
static t_stack_node *stack_node_new(t_stack_node *previous_node, t_subtree subtree, static t_stack_node *stack_node_new(t_stack_node *previous_node,
bool is_pending, t_state_id state, t_subtree subtree, bool is_pending,
t_stack_node_array *pool) t_state_id state, t_stack_node_array *pool)
{ {
t_stack_node *node = t_stack_node *node =
pool->size > 0 ? array_pop(pool) : malloc(sizeof(t_stack_node)); pool->size > 0 ? array_pop(pool) : malloc(sizeof(t_stack_node));
@ -9542,8 +9551,8 @@ void ts_stack_push(t_stack *self, t_stack_version version, t_subtree subtree,
head->node = new_node; head->node = new_node;
} }
static inline t_stack_action pop_count_callback(void *payload, static inline t_stack_action pop_count_callback(
const t_stack_iterator *iterator) void *payload, const t_stack_iterator *iterator)
{ {
unsigned *goal_subtree_count = payload; unsigned *goal_subtree_count = payload;
if (iterator->subtree_count == *goal_subtree_count) if (iterator->subtree_count == *goal_subtree_count)
@ -9562,8 +9571,8 @@ t_stack_slice_array ts_stack_pop_count(t_stack *self, t_stack_version version,
return stack__iter(self, version, pop_count_callback, &count, (int)count); return stack__iter(self, version, pop_count_callback, &count, (int)count);
} }
static inline t_stack_action pop_pending_callback(void *payload, static inline t_stack_action pop_pending_callback(
const t_stack_iterator *iterator) void *payload, const t_stack_iterator *iterator)
{ {
(void)payload; (void)payload;
if (iterator->subtree_count >= 1) if (iterator->subtree_count >= 1)
@ -9595,8 +9604,8 @@ t_stack_slice_array ts_stack_pop_pending(t_stack *self, t_stack_version version)
return pop; return pop;
} }
static inline t_stack_action pop_error_callback(void *payload, static inline t_stack_action pop_error_callback(
const t_stack_iterator *iterator) void *payload, const t_stack_iterator *iterator)
{ {
if (iterator->subtrees.size > 0) if (iterator->subtrees.size > 0)
{ {
@ -9687,8 +9696,8 @@ static inline t_stack_action summarize_stack_callback(
void ts_stack_record_summary(t_stack *self, t_stack_version version, void ts_stack_record_summary(t_stack *self, t_stack_version version,
unsigned max_depth) unsigned max_depth)
{ {
t_summarize_stack_session session = {.summary = malloc(sizeof(t_stack_summary)), t_summarize_stack_session session = {
.max_depth = max_depth}; .summary = malloc(sizeof(t_stack_summary)), .max_depth = max_depth};
array_init(session.summary); array_init(session.summary);
stack__iter(self, version, summarize_stack_callback, &session, -1); stack__iter(self, version, summarize_stack_callback, &session, -1);
t_stack_head *head = &self->heads.contents[version]; t_stack_head *head = &self->heads.contents[version];
@ -11392,7 +11401,8 @@ static inline int64_t ts_tree_cursor_goto_first_child_for_byte_and_point(
bool visible; bool visible;
t_tree_cursor_entry entry; t_tree_cursor_entry entry;
t_cursor_child_iterator iterator = ts_tree_cursor_iterate_children(self); t_cursor_child_iterator iterator =
ts_tree_cursor_iterate_children(self);
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible))
{ {
t_length entry_end = t_length entry_end =
@ -11454,7 +11464,8 @@ t_tree_cursor_step ts_tree_cursor_goto_sibling_internal(
while (self->stack.size > 1) while (self->stack.size > 1)
{ {
t_tree_cursor_entry entry = array_pop(&self->stack); t_tree_cursor_entry entry = array_pop(&self->stack);
t_cursor_child_iterator iterator = ts_tree_cursor_iterate_children(self); t_cursor_child_iterator iterator =
ts_tree_cursor_iterate_children(self);
iterator.child_index = entry.child_index; iterator.child_index = entry.child_index;
iterator.structural_child_index = entry.structural_child_index; iterator.structural_child_index = entry.structural_child_index;
iterator.position = entry.position; iterator.position = entry.position;
@ -11612,7 +11623,8 @@ void ts_tree_cursor_goto_descendant(t_tree_cursor *_self,
did_descend = false; did_descend = false;
bool visible; bool visible;
t_tree_cursor_entry entry; t_tree_cursor_entry entry;
t_cursor_child_iterator iterator = ts_tree_cursor_iterate_children(self); t_cursor_child_iterator iterator =
ts_tree_cursor_iterate_children(self);
if (iterator.descendant_index > goal_descendant_index) if (iterator.descendant_index > goal_descendant_index)
{ {
return; return;

View file

@ -6,13 +6,14 @@
/* 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/05/19 14:51:28 by maiboyer ### ########.fr */ /* Updated: 2024/05/29 16:39:24 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/minishell.h" #include "../includes/minishell.h"
#include "app/env.h" #include "app/env.h"
#include "me/hashmap/hashmap_env.h" #include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
void ts_parser_delete(t_first_parser *self); void ts_parser_delete(t_first_parser *self);
@ -20,7 +21,7 @@ void ts_parser_delete(t_first_parser *self);
void ft_free(void *ptr) void ft_free(void *ptr)
{ {
if (!ptr) if (!ptr)
free(ptr); mem_free(ptr);
} }
void ft_free_strs(t_str *strs) void ft_free_strs(t_str *strs)
@ -36,7 +37,7 @@ void ft_free_strs(t_str *strs)
void ft_free_utils(t_utils *s) void ft_free_utils(t_utils *s)
{ {
if (s->str_input) if (s->str_input)
free(s->str_input); mem_free(s->str_input);
if (s->path) if (s->path)
ft_free_strs(s->path); ft_free_strs(s->path);
if (s->env) if (s->env)

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/05/21 14:53:26 by maiboyer ### ########.fr */ /* Updated: 2024/05/29 16:40:26 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,12 +18,13 @@
#include "gmr/symbols.h" #include "gmr/symbols.h"
#include "me/hashmap/hashmap_env.h" #include "me/hashmap/hashmap_env.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "me/str/str.h"
#include "me/types.h" #include "me/types.h"
#include "minishell.h" #include "minishell.h"
#include "parser/api.h" #include "parser/api.h"
#include <sys/types.h> #include <sys/types.h>
#include "ast/from_node.h"
#undef free #undef free
#undef malloc #undef malloc
#undef realloc #undef realloc
@ -87,7 +88,6 @@ t_error handle_node_getstr(t_node *self, t_utils *shcat, t_str *out)
*out = NULL; *out = NULL;
if (self->kind == sym_word) if (self->kind == sym_word)
{ {
printf("word!!!\n");
*out = node_getstr(self); *out = node_getstr(self);
return (NO_ERROR); return (NO_ERROR);
} }
@ -102,7 +102,11 @@ void print_node_data(t_node *t, t_usize depth)
t_usize idx; t_usize idx;
idx = 0; idx = 0;
while (idx++ < depth) if (t->kind == 7)
return;
printf("\x1b[%im[%s](%lu)\x1b[0m", t->field_str == NULL ? 90 : 32,
t->field_str == NULL ? "nil" : t->field_str, t->field);
while (idx++ < depth + 1)
printf("\t"); printf("\t");
idx = 0; idx = 0;
printf("%s(%lu) = %s\n", t->kind_str, t->kind, node_getstr(t)); printf("%s(%lu) = %s\n", t->kind_str, t->kind, node_getstr(t));
@ -122,31 +126,12 @@ t_node parse_to_nodes(t_first_parser *parser, t_const_str input)
ts_tree_delete(tree); ts_tree_delete(tree);
return (ret); return (ret);
} }
t_node parse_str(t_parser *parser, t_const_str input) t_node parse_str(t_parser *parser, t_const_str input)
{ {
return (parse_to_nodes(parser->parser, input)); return (parse_to_nodes(parser->parser, input));
} }
// void ft_check(t_utils *shcat, char **input)
// {
// t_usize i;
// t_usize prev_i;
//
// i = 0;
// prev_i = 0;
// while (input[i] != NULL)
// {
// if (ft_strcmp(input[i], "exit") == 0)
// ft_exit(shcat, 0);
// else if (ft_strcmp(input[i], "pwd") == 0)
// ft_pwd();
// else
// ft_other_cmd(shcat, i, prev_i);
// prev_i = i;
// i++;
// }
// }
t_error handle_concat(t_node *self, t_utils *shcat, t_str *ret); t_error handle_concat(t_node *self, t_utils *shcat, t_str *ret);
void print_node_concat(t_node *self) void print_node_concat(t_node *self)
@ -171,7 +156,7 @@ void exec_shcat(t_utils *shcat)
{ {
t_i32 ret; t_i32 ret;
// print_node_data(&shcat->current_node, 0); print_node_data(&shcat->current_node, 0);
handle_program(&shcat->current_node, shcat, &ret); handle_program(&shcat->current_node, shcat, &ret);
free_node(shcat->current_node); free_node(shcat->current_node);
(void)ret; (void)ret;
@ -179,16 +164,20 @@ void exec_shcat(t_utils *shcat)
void ft_take_args(t_utils *shcat) void ft_take_args(t_utils *shcat)
{ {
t_str cmd;
while (1) while (1)
{ {
shcat->str_input = readline((t_const_str)shcat->name_shell); shcat->str_input = NULL;
if (!shcat->str_input) cmd = readline((t_const_str)shcat->name_shell);
if (cmd == NULL)
ft_exit(shcat, 0); ft_exit(shcat, 0);
shcat->str_input = str_clone(cmd);
free(cmd);
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);
free(shcat->str_input); mem_free(shcat->str_input);
} }
} }
@ -231,4 +220,5 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
"\001\x1B[0m\002" "\001\x1B[0m\002"
"$ "; "$ ";
ft_take_args(&utils); ft_take_args(&utils);
// (void)from_node(NULL);
} }

View file

@ -1,13 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 18:29:36 by maiboyer #+# #+# */
/* Updated: 2024/04/28 18:31:36 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/api.h"

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */ /* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */
/* Updated: 2024/05/18 16:14:55 by maiboyer ### ########.fr */ /* Updated: 2024/05/29 14:52:46 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "app/node.h" #include "app/node.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/str/str.h" #include "me/str/str.h"
#include "parser/api.h" #include "parser/api.h"
#include <stdio.h> #include <stdio.h>
@ -24,6 +23,11 @@ t_const_str ts_node_type(t_parse_node self);
t_u32 ts_node_start_byte(t_parse_node self); t_u32 ts_node_start_byte(t_parse_node self);
t_u32 ts_node_end_byte(t_parse_node self); t_u32 ts_node_end_byte(t_parse_node self);
t_u32 ts_node_child_count(t_parse_node self); t_u32 ts_node_child_count(t_parse_node self);
t_const_str ts_node_field_name_for_child(t_parse_node self, t_u32 child_index);
t_u64 ts_language_field_id_for_name(t_language *lang, t_const_str name,
t_u64 name_len);
t_language *tree_sitter_bash(void);
t_node *build_childs(t_parse_node parent, t_const_str input, t_usize count) t_node *build_childs(t_parse_node parent, t_const_str input, t_usize count)
{ {
@ -39,6 +43,10 @@ t_node *build_childs(t_parse_node parent, t_const_str input, t_usize count)
{ {
child = ts_node_child(parent, idx); child = ts_node_child(parent, idx);
ret[idx] = build_node(child, input); ret[idx] = build_node(child, input);
ret[idx].field_str = ts_node_field_name_for_child(parent, idx);
ret[idx].field = ts_language_field_id_for_name(
tree_sitter_bash(), ret[idx].field_str,
str_len(ret[idx].field_str));
idx++; idx++;
} }
return (ret); return (ret);
@ -54,6 +62,7 @@ t_node build_node(t_parse_node curr, t_const_str input)
out.end = ts_node_end_byte(curr); out.end = ts_node_end_byte(curr);
out.input = input; out.input = input;
out.single_str = NULL; out.single_str = NULL;
out.field_str = NULL;
out.childs_count = ts_node_child_count(curr); out.childs_count = ts_node_child_count(curr);
if (out.childs_count == 0) if (out.childs_count == 0)
out.childs = NULL; out.childs = NULL;

View file

@ -14,6 +14,5 @@ ft_echo.c
ft_exit.c ft_exit.c
ft_pwd.c ft_pwd.c
main.c main.c
node/create_node.c
node/node.c node/node.c
signal_handler.c signal_handler.c

View file

@ -1,18 +1,28 @@
sym_ansi_c_string sym_ansi_c_string
sym_arithmetic_expansion sym_arithmetic_expansion
sym_brace_expression sym_brace_expression
sym_case_item
sym_case_statement
sym_command sym_command
sym_command_name sym_command_name
sym_command_substitution sym_command_substitution
sym_comment sym_comment
sym_compound_statement
sym_concatenation sym_concatenation
sym_declaration_command
sym_elif_clause
sym_else_clause
sym_expansion sym_expansion
sym_file_redirect sym_file_redirect
sym_for_statement
sym_function_definition
sym_heredoc_body sym_heredoc_body
sym_heredoc_content sym_heredoc_content
sym_heredoc_end sym_heredoc_end
sym_heredoc_redirect sym_heredoc_redirect
sym_heredoc_start sym_heredoc_start
sym_if_statement
sym_last_case_item
sym_list sym_list
sym_negated_command sym_negated_command
sym_pipeline sym_pipeline
@ -23,15 +33,19 @@
sym_string sym_string
sym_string_content sym_string_content
sym_subshell sym_subshell
sym_test_command
sym_translated_string sym_translated_string
sym_unset_command
sym_variable_assignment sym_variable_assignment
sym_variable_assignments sym_variable_assignments
sym_variable_name sym_variable_name
sym_while_statement
sym_word sym_word
#sym_do_group
#sym_extglob_pattern
__sym_array __sym_array
__sym_binary_expression __sym_binary_expression
__sym_c_style_for_statement __sym_c_style_for_statement
__sym_do_group
__sym_file_descriptor __sym_file_descriptor
__sym_herestring_redirect __sym_herestring_redirect
__sym_number __sym_number
@ -42,19 +56,5 @@ __sym_regex
__sym_simple_heredoc_body __sym_simple_heredoc_body
__sym_subscript __sym_subscript
__sym_ternary_expression __sym_ternary_expression
sym_case_item __sym_test_operator
sym_case_statement __sym_unary_expression
sym_compound_statement
sym_declaration_command
sym_elif_clause
sym_else_clause
sym_extglob_pattern
sym_for_statement
sym_function_definition
sym_if_statement
sym_last_case_item
sym_test_command
sym_test_operator
sym_unary_expression
sym_unset_command
sym_while_statement