Commit 04b1ba5b authored by Guillaume Charifi's avatar Guillaume Charifi

compiler/parser: Update parse_use() according to the spec.

parent cede5933
...@@ -1018,14 +1018,14 @@ static int use_dtor(struct bm_ast_node *node) ...@@ -1018,14 +1018,14 @@ static int use_dtor(struct bm_ast_node *node)
static int use_debug_data(const struct bm_ast_node *node, int indent_lvl) static int use_debug_data(const struct bm_ast_node *node, int indent_lvl)
{ {
int result;
const struct bm_ast_node_data_use *data = &node->data.as_use; const struct bm_ast_node_data_use *data = &node->data.as_use;
wprintf(L",\n%*ls\"path\": \"", indent_lvl * 2, L""); wprintf(L",\n%*ls\"path\": \"", indent_lvl * 2, L"");
result = debug_class_path(&data->path); debug_class_path(&data->path);
if (result == 0) wprintf(L"\"");
wprintf(L".");
wprintf(L"%ls\"", data->name); if (data->name)
wprintf(L",\n%*ls\"name\": \"%ls\"", indent_lvl * 2, L"", data->name);
if (data->alias_name) if (data->alias_name)
wprintf(L",\n%*ls\"alias\": \"%ls\"", indent_lvl * 2, L"", data->alias_name); wprintf(L",\n%*ls\"alias\": \"%ls\"", indent_lvl * 2, L"", data->alias_name);
...@@ -1037,68 +1037,90 @@ static int parse_use(struct parser_ctx *ctx, unsigned long *cur_tok_id) ...@@ -1037,68 +1037,90 @@ static int parse_use(struct parser_ctx *ctx, unsigned long *cur_tok_id)
{ {
int result; int result;
unsigned long i = *cur_tok_id; unsigned long i = *cur_tok_id;
struct bm_ast_class_path class_path;
struct bm_ast_node uses;
struct bm_ast_node use; struct bm_ast_node use;
if (ctx->in->tokens[i].type != BM_TOK_USE) if (ctx->in->tokens[i].type != BM_TOK_USE)
return -1; return -1;
i++; i++;
parser_node_init(&use, BM_AST_NODE_USE); parser_node_init(&uses, BM_AST_NODE_UNKNOWN);
use.dtor = use_dtor;
use.debug_data = use_debug_data;
use.data.as_use.name = NULL;
use.data.as_use.alias_name = NULL;
result = parse_class_path(ctx, &i, &use.data.as_use.path); result = parse_class_path(ctx, &i, &class_path);
if (result < 0) if (result < 0)
{ {
wprintf(L"Expected a classpath inside use directive, got %ls.\n", debug_token_type(ctx->in->tokens[i].type)); wprintf(L"Expected a classpath inside use directive, got %ls.\n", debug_token_type(ctx->in->tokens[i].type));
goto failed; goto failed;
} }
if (ctx->in->tokens[i].type == BM_TOK_POINT) if (ctx->in->tokens[i].type == BM_TOK_IMPORT)
{ {
i++; i++;
if (ctx->in->tokens[i].type != BM_TOK_IDENTIFIER) while (i < ctx->in->nb_tokens)
{ {
wprintf(L"Expected a classpath inside use directive, got %ls.\n", debug_token_type(ctx->in->tokens[i].type)); parser_node_init(&use, BM_AST_NODE_USE);
goto failed; use.dtor = use_dtor;
} use.debug_data = use_debug_data;
use.data.as_use.name = str_dup(ctx->in->tokens[i].data.as_wstr); result = clone_class_path(&class_path, &use.data.as_use.path);
if (!use.data.as_use.name) if (result < 0)
goto failed; goto failed;
i++;
}
else
{
unsigned long nb_seqs;
const wchar_t *last_seq;
nb_seqs = buffer_get_line_count(&use.data.as_use.path.seqs); use.data.as_use.name = NULL;
last_seq = buffer_get_line(&use.data.as_use.path.seqs, nb_seqs - 1); use.data.as_use.alias_name = NULL;
use.data.as_use.name = str_dup(last_seq);
buffer_remove_line(&use.data.as_use.path.seqs, nb_seqs - 1);
}
if (ctx->in->tokens[i].type == BM_TOK_AS) parser_ignore_newlines(ctx, &i);
{
i++;
if (ctx->in->tokens[i].type != BM_TOK_IDENTIFIER) if (ctx->in->tokens[i].type != BM_TOK_IDENTIFIER)
{ goto failed;
wprintf(L"Expected an identifier inside use directive, got %ls.\n", debug_token_type(ctx->in->tokens[i].type));
goto failed; use.data.as_use.name = str_dup(ctx->in->tokens[i].data.as_wstr);
if (!use.data.as_use.name)
goto failed;
i++;
if (ctx->in->tokens[i].type == BM_TOK_AS)
{
i++;
if (ctx->in->tokens[i].type != BM_TOK_IDENTIFIER)
goto failed;
use.data.as_use.alias_name = str_dup(ctx->in->tokens[i].data.as_wstr);
if (!use.data.as_use.alias_name)
goto failed;
i++;
}
parser_node_append_child(&uses, &use);
if (ctx->in->tokens[i].type != BM_TOK_COMMA)
break;
i++;
parser_ignore_newlines(ctx, &i);
} }
}
else
{
parser_node_init(&use, BM_AST_NODE_USE);
use.dtor = use_dtor;
use.debug_data = use_debug_data;
use.data.as_use.alias_name = str_dup(ctx->in->tokens[i].data.as_wstr); result = clone_class_path(&class_path, &use.data.as_use.path);
if (!use.data.as_use.alias_name) if (result < 0)
goto failed; goto failed;
i++;
use.data.as_use.name = NULL;
use.data.as_use.alias_name = NULL;
parser_node_append_child(&uses, &use);
} }
destroy_class_path(&class_path);
if (ctx->in->tokens[i].type != BM_TOK_NEWLINE if (ctx->in->tokens[i].type != BM_TOK_NEWLINE
&& ctx->in->tokens[i].type != BM_TOK_SEMICOLON) && ctx->in->tokens[i].type != BM_TOK_SEMICOLON)
{ {
...@@ -1107,13 +1129,22 @@ static int parse_use(struct parser_ctx *ctx, unsigned long *cur_tok_id) ...@@ -1107,13 +1129,22 @@ static int parse_use(struct parser_ctx *ctx, unsigned long *cur_tok_id)
} }
i++; i++;
parser_node_append_child(&ctx->ast.uses, &use); for (unsigned long i = 0; i < parser_node_get_children_count(&uses); i++)
{
struct bm_ast_node *use;
use = parser_node_get_child(&uses, i);
parser_node_append_child(&ctx->ast.uses, use);
}
parser_node_destroy_no_recurse(&uses);
*cur_tok_id = i; *cur_tok_id = i;
return 0; return 0;
failed: failed:
parser_node_destroy(&use); parser_node_destroy(&use);
parser_node_destroy(&uses);
return -1; return -1;
} }
......
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