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)
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;
wprintf(L",\n%*ls\"path\": \"", indent_lvl * 2, L"");
result = debug_class_path(&data->path);
if (result == 0)
wprintf(L".");
wprintf(L"%ls\"", data->name);
debug_class_path(&data->path);
wprintf(L"\"");
if (data->name)
wprintf(L",\n%*ls\"name\": \"%ls\"", indent_lvl * 2, L"", data->name);
if (data->alias_name)
wprintf(L",\n%*ls\"alias\": \"%ls\"", indent_lvl * 2, L"", data->alias_name);
......@@ -1037,61 +1037,56 @@ static int parse_use(struct parser_ctx *ctx, unsigned long *cur_tok_id)
{
int result;
unsigned long i = *cur_tok_id;
struct bm_ast_class_path class_path;
struct bm_ast_node uses;
struct bm_ast_node use;
if (ctx->in->tokens[i].type != BM_TOK_USE)
return -1;
i++;
parser_node_init(&use, BM_AST_NODE_USE);
use.dtor = use_dtor;
use.debug_data = use_debug_data;
use.data.as_use.name = NULL;
use.data.as_use.alias_name = NULL;
parser_node_init(&uses, BM_AST_NODE_UNKNOWN);
result = parse_class_path(ctx, &i, &use.data.as_use.path);
result = parse_class_path(ctx, &i, &class_path);
if (result < 0)
{
wprintf(L"Expected a classpath inside use directive, got %ls.\n", debug_token_type(ctx->in->tokens[i].type));
goto failed;
}
if (ctx->in->tokens[i].type == BM_TOK_POINT)
if (ctx->in->tokens[i].type == BM_TOK_IMPORT)
{
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);
use.dtor = use_dtor;
use.debug_data = use_debug_data;
result = clone_class_path(&class_path, &use.data.as_use.path);
if (result < 0)
goto failed;
use.data.as_use.name = NULL;
use.data.as_use.alias_name = NULL;
parser_ignore_newlines(ctx, &i);
if (ctx->in->tokens[i].type != BM_TOK_IDENTIFIER)
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++;
}
else
{
unsigned long nb_seqs;
const wchar_t *last_seq;
nb_seqs = buffer_get_line_count(&use.data.as_use.path.seqs);
last_seq = buffer_get_line(&use.data.as_use.path.seqs, nb_seqs - 1);
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)
{
i++;
if (ctx->in->tokens[i].type != BM_TOK_IDENTIFIER)
{
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.alias_name = str_dup(ctx->in->tokens[i].data.as_wstr);
if (!use.data.as_use.alias_name)
......@@ -1099,6 +1094,33 @@ static int parse_use(struct parser_ctx *ctx, unsigned long *cur_tok_id)
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;
result = clone_class_path(&class_path, &use.data.as_use.path);
if (result < 0)
goto failed;
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
&& 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)
}
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;
return 0;
failed:
parser_node_destroy(&use);
parser_node_destroy(&uses);
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