Works as intended except for SEGFAULT in free, need to check vec impl
This commit is contained in:
parent
9f6b90d596
commit
efec224b6a
7 changed files with 1427 additions and 10 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/17 12:41:56 by maiboyer #+# #+# */
|
/* Created: 2024/06/17 12:41:56 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/06/23 18:45:46 by maiboyer ### ########.fr */
|
/* Updated: 2024/06/24 00:51:59 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -72,11 +72,14 @@ sym_while_statement
|
||||||
sym_word
|
sym_word
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#undef ERROR
|
#undef ERROR
|
||||||
#define ERROR ((void)printf("ERROR HERE: " __FILE__ ":%d in %s\n", __LINE__, __func__), 1)
|
#define ERROR ((void)printf("ERROR HERE: " __FILE__ ":%d in %s\n", __LINE__, __func__), 1)
|
||||||
|
|
||||||
void ast_free(t_ast_node elem)
|
void ast_free(t_ast_node elem)
|
||||||
{
|
{
|
||||||
|
printf("elem = %p\n", elem);
|
||||||
if (elem == NULL)
|
if (elem == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
@ -190,6 +193,7 @@ void ast_free(t_ast_node elem)
|
||||||
}
|
}
|
||||||
if (elem->kind == AST_VARIABLE_ASSIGNMENT)
|
if (elem->kind == AST_VARIABLE_ASSIGNMENT)
|
||||||
{
|
{
|
||||||
|
printf("value = %p\n", elem->data.variable_assignment.value);
|
||||||
ast_free(elem->data.variable_assignment.value);
|
ast_free(elem->data.variable_assignment.value);
|
||||||
mem_free(elem->data.variable_assignment.name);
|
mem_free(elem->data.variable_assignment.name);
|
||||||
}
|
}
|
||||||
|
|
@ -822,6 +826,7 @@ sym_word
|
||||||
|
|
||||||
t_error ast_from_node(t_parse_node node, t_const_str input, t_ast_node *out)
|
t_error ast_from_node(t_parse_node node, t_const_str input, t_ast_node *out)
|
||||||
{
|
{
|
||||||
|
return (ERROR);
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (ts_node_grammar_symbol(node) == sym_arithmetic_binary_expression)
|
if (ts_node_grammar_symbol(node) == sym_arithmetic_binary_expression)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
|
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# #
|
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# #
|
||||||
# Updated: 2024/06/23 22:03:59 by maiboyer ### ########.fr #
|
# Updated: 2024/06/24 00:37:07 by maiboyer ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
@ -24,7 +24,7 @@ CC ?= cc
|
||||||
CFLAGS = -Wall -Wextra -Werror -MMD -I./includes -I../includes -I../output/include -g3
|
CFLAGS = -Wall -Wextra -Werror -MMD -I./includes -I../includes -I../output/include -g3
|
||||||
#CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fsanitize-address-use-after-return=runtime -fno-common -fsanitize-address-use-after-scope
|
#CFLAGS += -fsanitize=address -fno-omit-frame-pointer -fsanitize-address-use-after-return=runtime -fno-common -fsanitize-address-use-after-scope
|
||||||
|
|
||||||
SRC_FILES = combined scanner funcs create_language
|
SRC_FILES = lib
|
||||||
GEN_FILES =
|
GEN_FILES =
|
||||||
|
|
||||||
SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES))) $(addsuffix .c,$(addprefix $(GEN_DIR)/,$(GEN_FILES)))
|
SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES))) $(addsuffix .c,$(addprefix $(GEN_DIR)/,$(GEN_FILES)))
|
||||||
|
|
|
||||||
99
parser/nsrc/create_language.c
Normal file
99
parser/nsrc/create_language.c
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* create_language.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/25 16:13:52 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/06/24 00:35:41 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../static/headers/constants.h"
|
||||||
|
#include "../static/headers/symbols.h"
|
||||||
|
#include "./parser.h"
|
||||||
|
|
||||||
|
bool lex_keywords_main(TSLexer *lexer, TSStateId state);
|
||||||
|
bool lex_normal_main(TSLexer *lexer, TSStateId state);
|
||||||
|
bool tree_sitter_sh_external_scanner_scan(void *ctx, TSLexer *lexer, const bool *ret);
|
||||||
|
void *create_external_scanner_states(void);
|
||||||
|
void *create_field_names(void);
|
||||||
|
void *create_symbols_names(void);
|
||||||
|
void *create_field_map_entries(void);
|
||||||
|
void *create_field_map_slices(void);
|
||||||
|
void *create_lex_modes(void);
|
||||||
|
void *create_parse_actions_entries(void);
|
||||||
|
void *create_primary_state_ids(void);
|
||||||
|
void *create_alias_sequences(void);
|
||||||
|
void *create_external_scanner_symbol_map(void);
|
||||||
|
void *create_non_terminal_alias_map(void);
|
||||||
|
void *create_unique_symbols_map(void);
|
||||||
|
void *create_symbols_metadata(void);
|
||||||
|
void *create_parse_table(void);
|
||||||
|
void *create_small_parse_table(void);
|
||||||
|
void *create_small_parse_table_map(void);
|
||||||
|
|
||||||
|
uint32_t tree_sitter_sh_external_scanner_serialize(void *ctx, char *s);
|
||||||
|
void tree_sitter_sh_external_scanner_deserialize(void *ctx, const char *s, uint32_t val);
|
||||||
|
void tree_sitter_sh_external_scanner_destroy(void *ctx);
|
||||||
|
void *tree_sitter_sh_external_scanner_create(void);
|
||||||
|
|
||||||
|
static struct ExternalScannerDefinition init_scanner(void)
|
||||||
|
{
|
||||||
|
return ((struct ExternalScannerDefinition){
|
||||||
|
create_external_scanner_states(),
|
||||||
|
create_external_scanner_symbol_map(),
|
||||||
|
tree_sitter_sh_external_scanner_create,
|
||||||
|
tree_sitter_sh_external_scanner_destroy,
|
||||||
|
tree_sitter_sh_external_scanner_scan,
|
||||||
|
tree_sitter_sh_external_scanner_serialize,
|
||||||
|
tree_sitter_sh_external_scanner_deserialize,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_language(TSLanguage *language)
|
||||||
|
{
|
||||||
|
language->parse_table = create_parse_table();
|
||||||
|
language->small_parse_table = create_small_parse_table();
|
||||||
|
language->small_parse_table_map = create_small_parse_table_map();
|
||||||
|
language->parse_actions = create_parse_actions_entries();
|
||||||
|
language->symbol_names = create_symbols_names();
|
||||||
|
language->field_names = create_field_names();
|
||||||
|
language->field_map_slices = create_field_map_slices();
|
||||||
|
language->field_map_entries = create_field_map_entries();
|
||||||
|
language->symbol_metadata = create_symbols_metadata();
|
||||||
|
language->public_symbol_map = create_unique_symbols_map();
|
||||||
|
language->alias_map = create_non_terminal_alias_map();
|
||||||
|
language->alias_sequences = create_alias_sequences();
|
||||||
|
language->lex_modes = create_lex_modes();
|
||||||
|
language->primary_state_ids = create_primary_state_ids();
|
||||||
|
language->lex_fn = lex_normal_main;
|
||||||
|
language->keyword_lex_fn = lex_keywords_main;
|
||||||
|
language->keyword_capture_token = sym_word;
|
||||||
|
language->external_scanner = init_scanner();
|
||||||
|
}
|
||||||
|
|
||||||
|
const TSLanguage *tree_sitter_bash(void)
|
||||||
|
{
|
||||||
|
static bool init = false;
|
||||||
|
static TSLanguage language = {
|
||||||
|
.version = LANGUAGE_VERSION,
|
||||||
|
.symbol_count = SYMBOL_COUNT,
|
||||||
|
.alias_count = ALIAS_COUNT,
|
||||||
|
.token_count = TOKEN_COUNT,
|
||||||
|
.external_token_count = EXTERNAL_TOKEN_COUNT,
|
||||||
|
.state_count = STATE_COUNT,
|
||||||
|
.large_state_count = LARGE_STATE_COUNT,
|
||||||
|
.production_id_count = PRODUCTION_ID_COUNT,
|
||||||
|
.field_count = FIELD_COUNT,
|
||||||
|
.max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!init)
|
||||||
|
{
|
||||||
|
init_language(&language);
|
||||||
|
init = true;
|
||||||
|
}
|
||||||
|
return ((TSLanguage *)&language);
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
#define _POSIX_C_SOURCE 200112L
|
#define _POSIX_C_SOURCE 200112L
|
||||||
|
|
||||||
#include "./alloc.c"
|
#include "./alloc.c"
|
||||||
|
#include "./create_language.c"
|
||||||
#include "./get_changed_ranges.c"
|
#include "./get_changed_ranges.c"
|
||||||
#include "./language.c"
|
#include "./language.c"
|
||||||
#include "./lexer.c"
|
#include "./lexer.c"
|
||||||
#include "./node.c"
|
#include "./node.c"
|
||||||
#include "./parser.c"
|
#include "./parser.c"
|
||||||
#include "./query.c"
|
#include "./query.c"
|
||||||
|
#include "./scanner.c"
|
||||||
#include "./stack.c"
|
#include "./stack.c"
|
||||||
#include "./subtree.c"
|
#include "./subtree.c"
|
||||||
#include "./tree_cursor.c"
|
|
||||||
#include "./tree.c"
|
#include "./tree.c"
|
||||||
|
#include "./tree_cursor.c"
|
||||||
|
|
|
||||||
|
|
@ -711,6 +711,20 @@ recur:
|
||||||
return ts_node__null();
|
return ts_node__null();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline TSFieldId ts_node__field_id_from_language(TSNode self, uint32_t structural_child_index)
|
||||||
|
{
|
||||||
|
const TSFieldMapEntry *field_map, *field_map_end;
|
||||||
|
ts_language_field_map(self.tree->language, ts_node__subtree(self).ptr->inner.non_terminal.production_id, &field_map, &field_map_end);
|
||||||
|
for (; field_map != field_map_end; field_map++)
|
||||||
|
{
|
||||||
|
if (!field_map->inherited && field_map->child_index == structural_child_index)
|
||||||
|
{
|
||||||
|
return field_map->field_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static inline const char *ts_node__field_name_from_language(TSNode self, uint32_t structural_child_index)
|
static inline const char *ts_node__field_name_from_language(TSNode self, uint32_t structural_child_index)
|
||||||
{
|
{
|
||||||
const TSFieldMapEntry *field_map, *field_map_end;
|
const TSFieldMapEntry *field_map, *field_map_end;
|
||||||
|
|
@ -725,6 +739,59 @@ static inline const char *ts_node__field_name_from_language(TSNode self, uint32_
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TSFieldId ts_node_field_id_for_child(TSNode self, uint32_t child_index)
|
||||||
|
{
|
||||||
|
TSNode result = self;
|
||||||
|
bool did_descend = true;
|
||||||
|
TSFieldId inherited_field_name = 0;
|
||||||
|
|
||||||
|
while (did_descend)
|
||||||
|
{
|
||||||
|
did_descend = false;
|
||||||
|
|
||||||
|
TSNode child;
|
||||||
|
uint32_t index = 0;
|
||||||
|
NodeChildIterator iterator = ts_node_iterate_children(&result);
|
||||||
|
while (ts_node_child_iterator_next(&iterator, &child))
|
||||||
|
{
|
||||||
|
if (ts_node__is_relevant(child, true))
|
||||||
|
{
|
||||||
|
if (index == child_index)
|
||||||
|
{
|
||||||
|
if (ts_node_is_extra(child))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
TSFieldId field_name = ts_node__field_id_from_language(result, iterator.structural_child_index - 1);
|
||||||
|
if (field_name)
|
||||||
|
return field_name;
|
||||||
|
return inherited_field_name;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uint32_t grandchild_index = child_index - index;
|
||||||
|
uint32_t grandchild_count = ts_node__relevant_child_count(child, true);
|
||||||
|
if (grandchild_index < grandchild_count)
|
||||||
|
{
|
||||||
|
TSFieldId field_name = ts_node__field_id_from_language(result, iterator.structural_child_index - 1);
|
||||||
|
if (field_name)
|
||||||
|
inherited_field_name = field_name;
|
||||||
|
|
||||||
|
did_descend = true;
|
||||||
|
result = child;
|
||||||
|
child_index = grandchild_index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index += grandchild_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index)
|
const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index)
|
||||||
{
|
{
|
||||||
TSNode result = self;
|
TSNode result = self;
|
||||||
|
|
|
||||||
1242
parser/nsrc/scanner.c
Normal file
1242
parser/nsrc/scanner.c
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,9 @@
|
||||||
#include "tree_sitter/array.h"
|
#include "tree_sitter/array.h"
|
||||||
#include "tree_sitter/parser.h"
|
#include "tree_sitter/parser.h"
|
||||||
|
/*
|
||||||
|
#include "array.h"
|
||||||
|
#include "parser.h"
|
||||||
|
*/
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
@ -249,7 +252,7 @@ static inline bool scan_bare_dollar(TSLexer *lexer)
|
||||||
{
|
{
|
||||||
while (iswspace(lexer->lookahead) && lexer->lookahead != '\n' && !lexer->eof(lexer))
|
while (iswspace(lexer->lookahead) && lexer->lookahead != '\n' && !lexer->eof(lexer))
|
||||||
skip(lexer);
|
skip(lexer);
|
||||||
|
|
||||||
|
|
||||||
if (lexer->lookahead == '$')
|
if (lexer->lookahead == '$')
|
||||||
{
|
{
|
||||||
|
|
@ -378,7 +381,7 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, enum TokenTyp
|
||||||
{
|
{
|
||||||
if (lexer->result_symbol == HEREDOC_END)
|
if (lexer->result_symbol == HEREDOC_END)
|
||||||
{
|
{
|
||||||
array_pop(&scanner->heredocs);
|
(void)array_pop(&scanner->heredocs);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -521,7 +524,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols)
|
||||||
{
|
{
|
||||||
array_delete(&heredoc->current_leading_word);
|
array_delete(&heredoc->current_leading_word);
|
||||||
array_delete(&heredoc->delimiter);
|
array_delete(&heredoc->delimiter);
|
||||||
array_pop(&scanner->heredocs);
|
(void)array_pop(&scanner->heredocs);
|
||||||
lexer->result_symbol = HEREDOC_END;
|
lexer->result_symbol = HEREDOC_END;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -727,7 +730,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
regex:
|
|
||||||
if ((valid_symbols[REGEX]) && !in_error_recovery(valid_symbols))
|
if ((valid_symbols[REGEX]) && !in_error_recovery(valid_symbols))
|
||||||
{
|
{
|
||||||
if (valid_symbols[REGEX])
|
if (valid_symbols[REGEX])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue