update: parser.c is in the process of being answered

This commit is contained in:
maix0 2024-09-13 13:29:56 +00:00
parent bae0126d8e
commit 7f6146fec7
3 changed files with 47 additions and 32 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/03 14:08:00 by maiboyer #+# #+# */ /* Created: 2024/09/03 14:08:00 by maiboyer #+# #+# */
/* Updated: 2024/09/11 17:32:08 by maiboyer ### ########.fr */ /* Updated: 2024/09/13 13:28:40 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,11 +28,14 @@ bool ts_parser__breakdown_top_of_stack(TSParser *self, t_stack_version version)
t_u32 i; t_u32 i;
t_u32 j; t_u32 j;
t_u32 n; t_u32 n;
bool first;
first = true;
did_break_down = false; did_break_down = false;
pending = false; pending = false;
do while (pending || first)
{ {
first = false;
pop = ts_stack_pop_pending(self->stack, version); pop = ts_stack_pop_pending(self->stack, version);
if (!pop.size) if (!pop.size)
break; break;
@ -69,7 +72,7 @@ bool ts_parser__breakdown_top_of_stack(TSParser *self, t_stack_version version)
array_delete(&slice.subtrees); array_delete(&slice.subtrees);
i++; i++;
} }
} while (pending); };
return (did_break_down); return (did_break_down);
} }
@ -469,8 +472,9 @@ t_stack_version ts_parser__reduce(TSParser *self, t_stack_version version, TSSym
} }
i++; i++;
} }
// Return the first new stack version that was created. if (ts_stack_version_count(self->stack) > initial_version_count)
return ts_stack_version_count(self->stack) > initial_version_count ? initial_version_count : STACK_VERSION_NONE; return (initial_version_count);
return (STACK_VERSION_NONE);
} }
void ts_parser__accept(TSParser *self, t_stack_version version, t_subtree lookahead) void ts_parser__accept(TSParser *self, t_stack_version version, t_subtree lookahead)
@ -556,22 +560,28 @@ bool ts_parser__do_all_potential_reductions(TSParser *self, t_stack_version star
initial_version_count = ts_stack_version_count(self->stack); initial_version_count = ts_stack_version_count(self->stack);
can_shift_lookahead_symbol = false; can_shift_lookahead_symbol = false;
version = starting_version; version = starting_version;
for (i = 0; true; i++) i = 0;
while (true)
{ {
version_count = ts_stack_version_count(self->stack); version_count = ts_stack_version_count(self->stack);
if (version >= version_count) if (version >= version_count)
break; break;
merged = false; merged = false;
for (j = initial_version_count; j < version; j++) j = initial_version_count;
while (j < version)
{ {
if (ts_stack_merge(self->stack, j, version)) if (ts_stack_merge(self->stack, j, version))
{ {
merged = true; merged = true;
break; break;
} }
j++;
} }
if (merged) if (merged)
{
i++;
continue; continue;
}
state = ts_stack_state(self->stack, version); state = ts_stack_state(self->stack, version);
has_shift_action = false; has_shift_action = false;
self->reduce_actions.len = 0; self->reduce_actions.len = 0;
@ -585,45 +595,43 @@ bool ts_parser__do_all_potential_reductions(TSParser *self, t_stack_version star
first_symbol = 1; first_symbol = 1;
end_symbol = self->language->token_count; end_symbol = self->language->token_count;
} }
for (symbol = first_symbol; symbol < end_symbol; symbol++) symbol = first_symbol;
while (symbol < end_symbol)
{ {
ts_language_table_entry(self->language, state, symbol, &entry); ts_language_table_entry(self->language, state, symbol, &entry);
for (k = 0; k < entry.action_count; k++) k = 0;
while (k < entry.action_count)
{ {
action = entry.actions[k]; action = entry.actions[k];
switch (action.type) if ((action.type == TSParseActionTypeShift || action.type == TSParseActionTypeRecover) &&
{ (!action.shift.extra && !action.shift.repetition))
case TSParseActionTypeShift: has_shift_action = true;
case TSParseActionTypeRecover: if ((action.type == TSParseActionTypeReduce) && (action.reduce.child_count > 0))
if (!action.shift.extra && !action.shift.repetition) ts_reduce_action_set_add(&self->reduce_actions, (t_reduce_action){
has_shift_action = true; .symbol = action.reduce.symbol,
break; .count = action.reduce.child_count,
case TSParseActionTypeReduce: .dynamic_precedence = action.reduce.dynamic_precedence,
if (action.reduce.child_count > 0) .production_id = action.reduce.production_id,
ts_reduce_action_set_add(&self->reduce_actions, (t_reduce_action){ });
.symbol = action.reduce.symbol, k++;
.count = action.reduce.child_count,
.dynamic_precedence = action.reduce.dynamic_precedence,
.production_id = action.reduce.production_id,
});
break;
default:
break;
}
} }
symbol++;
} }
reduction_version = STACK_VERSION_NONE; reduction_version = STACK_VERSION_NONE;
for (k = 0; k < self->reduce_actions.len; k++) k = 0;
while (k < self->reduce_actions.len)
{ {
reduce_action = self->reduce_actions.buffer[k]; reduce_action = self->reduce_actions.buffer[k];
reduction_version = ts_parser__reduce(self, version, reduce_action.symbol, reduce_action.count, reduction_version = ts_parser__reduce(self, version, reduce_action.symbol, reduce_action.count,
reduce_action.dynamic_precedence, reduce_action.production_id, true, false); reduce_action.dynamic_precedence, reduce_action.production_id, true, false);
k++;
} }
if (has_shift_action) if (has_shift_action)
can_shift_lookahead_symbol = true; can_shift_lookahead_symbol = true;
else if (reduction_version != STACK_VERSION_NONE && i < MAX_VERSION_COUNT) else if (reduction_version != STACK_VERSION_NONE && i < MAX_VERSION_COUNT)
{ {
ts_stack_renumber_version(self->stack, reduction_version, version); ts_stack_renumber_version(self->stack, reduction_version, version);
i++;
continue; continue;
} }
else if (lookahead_symbol != 0) else if (lookahead_symbol != 0)
@ -632,6 +640,7 @@ bool ts_parser__do_all_potential_reductions(TSParser *self, t_stack_version star
version = version_count; version = version_count;
else else
version++; version++;
i++;
} }
return can_shift_lookahead_symbol; return can_shift_lookahead_symbol;
} }
@ -1136,6 +1145,12 @@ bool ts_parser_has_outstanding_parse(TSParser *self)
// Parser - Public // Parser - Public
bool _parse_condition(TSParser *self, t_u32 *version_count, t_stack_version *version)
{
*version_count = ts_stack_version_count(self->stack);
return (*version < *version_count);
}
TSTree *ts_parser_parse(TSParser *self, TSInput input) TSTree *ts_parser_parse(TSParser *self, TSInput input)
{ {
TSTree *result; TSTree *result;
@ -1166,7 +1181,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input)
{ {
first = false; first = false;
version = 0; version = 0;
for (; version_count = ts_stack_version_count(self->stack), version < version_count;) while (_parse_condition(self, &version_count, &version))
{ {
while (ts_stack_is_active(self->stack, version)) while (ts_stack_is_active(self->stack, version))
{ {

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=yes --trace-children=yes --read-var-info=yes --read-inline-info=yes ./minishell <<<'cat <<EOF | xargs printf "\x1b[32m%s \x1b[0m\n" make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=no --trace-children=yes --read-var-info=yes --read-inline-info=yes ./minishell <<<'cat <<EOF | xargs printf "\x1b[32m%s \x1b[0m\n"
bonjour je suis un heredoc bonjour je suis un heredoc
le saviez tu je dois finir par EOF mais qui est sur la ligne le saviez tu je dois finir par EOF mais qui est sur la ligne
donc par example si j ai cette ligne qui fini avec EOF donc par example si j ai cette ligne qui fini avec EOF

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=yes --trace-children=yes --read-var-info=yes --read-inline-info=yes ./minishell <<<'cat </dev/null | cat | cat "./.envrc"' make && valgrind --leak-check=full --show-leak-kinds=none --track-origins=yes --track-fds=no --trace-children=yes --read-var-info=yes --read-inline-info=yes ./minishell <<<'cat </dev/null | cat | cat "./.envrc"'