Commit 2c7f1c97 authored by Guillaume Charifi's avatar Guillaume Charifi

compiler: End the lexer output with EOF.

parent 465e29dd
......@@ -136,6 +136,16 @@ int token_deinit(struct bm_token *token)
return 0;
}
int token_replace(struct lexer_output *out, unsigned long id, struct bm_token *token)
{
if (id >= out->nb_tokens)
return -1;
token_deinit(&out->tokens[id]);
out->tokens[id] = *token;
return 0;
}
static int output_append_token(struct lexer_output *out, struct bm_token *token, size_t in_file_size)
{
struct bm_token *tokens;
......@@ -541,6 +551,7 @@ int lexer(const wchar_t *in, struct lexer_output *out)
{
struct buffer buf;
unsigned long count;
struct bm_token token;
out->nb_tokens = 0;
out->nb_tokens_allocated = 0;
......@@ -555,7 +566,6 @@ int lexer(const wchar_t *in, struct lexer_output *out)
for (unsigned long i = 0; i < count; i++)
{
struct bm_token token;
const wchar_t *line = buffer_get_line(&buf, i);
const wchar_t *cursor = line;
......@@ -575,6 +585,10 @@ int lexer(const wchar_t *in, struct lexer_output *out)
wprintf(L"\n");
}
token_init(&token, BM_TOK_EOF);
output_append_token(out, &token, buf.total_size);
wprintf(L"EOF\n");
leave:
buffer_free(&buf);
return 0;
......@@ -592,13 +606,3 @@ int lexer_destroy(struct lexer_output *out)
out->nb_tokens_allocated = 0;
return 0;
}
int token_replace(struct lexer_output *out, unsigned long id, struct bm_token *token)
{
if (id >= out->nb_tokens)
return -1;
token_deinit(&out->tokens[id]);
out->tokens[id] = *token;
return 0;
}
......@@ -36,8 +36,8 @@ extern int token_init_float(struct bm_token *token, enum bm_token_type type, flo
extern int token_init_char(struct bm_token *token, enum bm_token_type type, wchar_t c);
extern int token_init_str(struct bm_token *token, enum bm_token_type type, const wchar_t *str, size_t max_len);
extern int token_deinit(struct bm_token *token);
extern int token_replace(struct lexer_output *out, unsigned long id, struct bm_token *token);
extern int lexer(const wchar_t *in, struct lexer_output *out);
extern int lexer_destroy(struct lexer_output *out);
extern int token_replace(struct lexer_output *out, unsigned long id, struct bm_token *token);
#endif
......@@ -1783,9 +1783,9 @@ int parser(struct lexer_output *in, struct bm_ast *out)
goto failed;
}
if (ctx.in->tokens[ctx.in->nb_tokens - 1].type != BM_TOK_NEWLINE)
if (ctx.in->tokens[ctx.in->nb_tokens - 1].type != BM_TOK_EOF)
{
wprintf(L"Internal error: The lexer output does not end with a newline.\n");
wprintf(L"Internal error: The lexer output does not end with EOF.\n");
goto failed;
}
......@@ -1848,6 +1848,13 @@ int parser(struct lexer_output *in, struct bm_ast *out)
if (result < 0)
result = parse_interface(&ctx, &cur_tok_id);
if (result < 0)
{
if (cur_tok_id == ctx.in->nb_tokens - 1
&& ctx.in->tokens[cur_tok_id].type == BM_TOK_EOF)
break;
}
if (result < 0)
{
wprintf(L"Unexpected token %ls in global namespace.\n", debug_token_type(ctx.in->tokens[cur_tok_id].type));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment