Skip to content

Commit

Permalink
Reset parser more completely after errors
Browse files Browse the repository at this point in the history
Clear up lexer and code gen state too.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed May 30, 2019
1 parent e7ef9e4 commit 56be547
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
12 changes: 9 additions & 3 deletions snek-code.c
Expand Up @@ -401,6 +401,14 @@ snek_code_patch_forward(snek_offset_t start, snek_offset_t stop, snek_forward_t
}
}

void
snek_code_reset(void)
{
snek_compile_size = 0;
compile_alloc = 0;
snek_compile = NULL;
}

snek_code_t *
snek_code_finish(void)
{
Expand All @@ -416,9 +424,7 @@ snek_code_finish(void)
snek_code_dump(code);
#endif
}
snek_compile_size = 0;
compile_alloc = 0;
snek_compile = NULL;
snek_code_reset();
return code;
}

Expand Down
15 changes: 14 additions & 1 deletion snek-parse.c
Expand Up @@ -161,12 +161,22 @@ snek_parse(void)
for (;;) {
/* Reset lex state */
snek_current_indent = 0;
snek_lex_indent = 0;
snek_ignore_nl = 0;
snek_abort = false;
snek_lex_midline = false;
snek_lex_exdent = false;

/* Reset parse state */
snek_parse_middle = false;
value_stack_p = 0;
for_depth = 0;

/* Reset codegen state */
snek_code_reset();

/* Reset error state */
snek_abort = false;

parse_return_t ret = parse(NULL);

switch (ret) {
Expand All @@ -185,6 +195,9 @@ snek_parse(void)
default:
snek_error_syntax(snek_lex_text);
{
/* Skip input until we get back to
* zero indent
*/
token_t token;
for (;;) {
snek_ignore_nl = 0;
Expand Down
3 changes: 3 additions & 0 deletions snek.h
Expand Up @@ -432,6 +432,9 @@ snek_code_add_in_enum(snek_id_t id, uint8_t for_depth);
void
snek_code_patch_branch(snek_offset_t branch, snek_offset_t target);

void
snek_code_reset(void);

snek_code_t *
snek_code_finish(void);

Expand Down

0 comments on commit 56be547

Please sign in to comment.