Improved error messages on incorrect module name. Issue #209

This commit is contained in:
Christoffer Lerno
2021-07-17 15:20:13 +02:00
parent 429fb647fb
commit 1af27136fa
8 changed files with 47 additions and 2 deletions

View File

@@ -241,6 +241,16 @@ static inline Path *parse_module_path(Context *context)
const char *string = TOKSTR(context->tok);
if (!try_consume(context, TOKEN_IDENT))
{
if (token_is_keyword(context->tok.type))
{
SEMA_TOKEN_ERROR(context->tok, "The module path cannot contain a reserved keyword, try another name.");
return false;
}
if (token_is_some_ident(context->tok.type))
{
SEMA_TOKEN_ERROR(context->tok, "The elements of a module path must consist of only lower case letters, 0-9 and '_'.");
return false;
}
SEMA_TOKEN_ERROR(context->tok, "Each '::' must be followed by a regular lower case sub module name.");
return NULL;
}
@@ -338,7 +348,17 @@ bool parse_module(Context *context)
if (!TOKEN_IS(TOKEN_IDENT))
{
SEMA_TOKEN_ERROR(context->tok, "Module statement should be followed by the name of the module.");
if (token_is_keyword(context->tok.type))
{
SEMA_TOKEN_ERROR(context->tok, "The module name cannot contain a reserved keyword, try another name.");
return false;
}
if (token_is_some_ident(context->tok.type))
{
SEMA_TOKEN_ERROR(context->tok, "The module name must consist of only lower case letters, 0-9 and '_'.");
return false;
}
SEMA_TOKEN_ERROR(context->tok, "'module' should be followed by a module name.");
return false;
}

View File

@@ -19,7 +19,6 @@
#define CHECK_EXPR(_expr) do { if (!expr_ok(_expr)) return _expr; } while(0)
#define COMMA_RPAREN_OR(_res) \
do { if (!try_consume(context, TOKEN_COMMA) && !TOKEN_IS(TOKEN_RPAREN)) { \
SEMA_TOKEN_ERROR(context->tok, "Expected ',' or ')'"); return _res; } } while(0)
@@ -80,6 +79,26 @@ static inline bool expect(Context *context, TokenType token_type)
return false;
}
static inline bool token_is_some_ident(TokenType token_type)
{
switch (token_type)
{
case TOKEN_TYPE_IDENT:
case TOKEN_IDENT:
case TOKEN_CONST_IDENT:
return true;
default:
return false;
}
}
static inline bool token_is_keyword(TokenType token_type)
{
if (token_type >= TOKEN_VOID && token_type <= TOKEN_TYPEID) return true;
if (token_type >= TOKEN_ALIAS && token_type <= TOKEN_WHILE) return true;
return false;
}
static inline bool expect_ident(Context *context, const char* name)
{
switch (context->tok.type)

View File

@@ -0,0 +1 @@
module big::MONEY; // #error: The elements of a module path must consist of only lower

View File

@@ -0,0 +1 @@
module abc::&&; // #error: Each '::' must be followed by a regular lower case sub module name

View File

@@ -0,0 +1 @@
module abc::if::while; // #error: The module path cannot contain a reserved keyword, try another name

View File

@@ -0,0 +1 @@
module BIG::MONEY; // #error: The module name must consist of only lower case letters

View File

@@ -0,0 +1 @@
module &&; // #error: 'module' should be followed by a module name

View File

@@ -0,0 +1 @@
module if::hello; // #error: The module name cannot contain a reserved keyword