Improve error message on import "foo", module "foo", and import statements out of order.

This commit is contained in:
Christoffer Lerno
2021-07-12 21:02:51 +02:00
committed by Christoffer Lerno
parent 3173d09e56
commit 9ee24b5846
4 changed files with 22 additions and 0 deletions

View File

@@ -326,12 +326,19 @@ bool parse_module(Context *context)
bool is_private = try_consume(context, TOKEN_PRIVATE);
if (TOKEN_IS(TOKEN_STRING))
{
SEMA_TOKEN_ERROR(context->tok, "'module' should be followed by a plain identifier, not a string. Did you accidentally put the module name between \"\"?");
return false;
}
if (!TOKEN_IS(TOKEN_IDENT))
{
SEMA_TOKEN_ERROR(context->tok, "Module statement should be followed by the name of the module.");
return false;
}
Path *path = parse_module_path(context);
// Expect the module name
@@ -1949,6 +1956,11 @@ static inline bool parse_import(Context *context)
if (!TOKEN_IS(TOKEN_IDENT))
{
if (TOKEN_IS(TOKEN_STRING))
{
SEMA_TOKEN_ERROR(context->tok, "An import should be followed by a plain identifier, not a string. Did you accidentally put the module name between \"\"?");
return false;
}
SEMA_TOKEN_ERROR(context->tok, "Import statement should be followed by the name of the module to import.");
return false;
}
@@ -2238,6 +2250,9 @@ Decl *parse_top_level_statement(Context *context)
SEMA_TOKEN_ERROR(context->tok, "Compile time constant unexpectedly found.");
}
return poisoned_decl;
case TOKEN_IMPORT:
SEMA_TOKEN_ERROR(context->tok, "Imports are only allowed directly after the module declaration.");
return poisoned_decl;
default:
// We could have included all fundamental types above, but do it here instead.
if (!token_is_type(context->tok.type))

View File

@@ -0,0 +1,5 @@
module foo;
func void hello() {}
import bar; // #error: Imports are only allowed directly after the module declaration

View File

@@ -0,0 +1 @@
import "Hello my friend"; // #error: An import should be followed by a plain identifier, not a string. Did you accidentally put the module name between ""?

View File

@@ -0,0 +1 @@
module "Hello my friend"; // #error: 'module' should be followed by a plain identifier, not a string. Did you accidentally put the module name between ""?