Add error logging and corresponding tests for parser syntax errors
This commit is contained in:
+18
-9
@@ -1,25 +1,32 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "log.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
Module* parser_parse(TokenStream* ts) {
|
Module* parser_parse(TokenStream* ts) {
|
||||||
Token t = tokenstream_next(ts);
|
Token t = tokenstream_next(ts);
|
||||||
if (t.token != TOKEN_MODULE) {
|
if (t.token != TOKEN_MODULE) {
|
||||||
|
log_on_line(&t.location, t.location.column_end, "expected 'module' keyword");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = tokenstream_next(ts);
|
t = tokenstream_next(ts);
|
||||||
if (t.token != TOKEN_IDENTIFIER) {
|
if (t.token != TOKEN_IDENTIFIER) {
|
||||||
|
log_on_line(&t.location, t.location.column_end, "expected module name");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Module* module = (Module*)malloc(sizeof(Module));
|
Module* module = (Module*)malloc(sizeof(Module));
|
||||||
if (module == NULL) return NULL;
|
if (module == NULL) {
|
||||||
|
fprintf(stderr, "Out of memory\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
module->name = (char*)malloc(t.text.length + 1);
|
module->name = (char*)malloc(t.text.length + 1);
|
||||||
if (module->name == NULL) {
|
if (module->name == NULL) {
|
||||||
free(module);
|
fprintf(stderr, "Out of memory\n");
|
||||||
return NULL;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(module->name, t.text.data, t.text.length);
|
memcpy(module->name, t.text.data, t.text.length);
|
||||||
@@ -27,8 +34,8 @@ Module* parser_parse(TokenStream* ts) {
|
|||||||
|
|
||||||
t = tokenstream_next(ts);
|
t = tokenstream_next(ts);
|
||||||
if (t.token != TOKEN_SEMICOLON) {
|
if (t.token != TOKEN_SEMICOLON) {
|
||||||
free(module->name);
|
log_on_line(&t.location, t.location.column_end, "expected ';' after module name");
|
||||||
free(module);
|
parser_free(module);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,21 +50,22 @@ Module* parser_parse(TokenStream* ts) {
|
|||||||
|
|
||||||
ImportDeclaration* new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
|
ImportDeclaration* new_imports = realloc(module->imports, (module->import_count + 1) * sizeof(ImportDeclaration));
|
||||||
if (!new_imports) {
|
if (!new_imports) {
|
||||||
parser_free(module);
|
fprintf(stderr, "Out of memory\n");
|
||||||
return NULL;
|
exit(1);
|
||||||
}
|
}
|
||||||
module->imports = new_imports;
|
module->imports = new_imports;
|
||||||
|
|
||||||
t = tokenstream_next(ts);
|
t = tokenstream_next(ts);
|
||||||
if (t.token != TOKEN_IDENTIFIER) {
|
if (t.token != TOKEN_IDENTIFIER) {
|
||||||
|
log_on_line(&t.location, t.location.column_end, "expected module name to import");
|
||||||
parser_free(module);
|
parser_free(module);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
module->imports[module->import_count].module_name = (char*)malloc(t.text.length + 1);
|
module->imports[module->import_count].module_name = (char*)malloc(t.text.length + 1);
|
||||||
if (!module->imports[module->import_count].module_name) {
|
if (!module->imports[module->import_count].module_name) {
|
||||||
parser_free(module);
|
fprintf(stderr, "Out of memory\n");
|
||||||
return NULL;
|
exit(1);
|
||||||
}
|
}
|
||||||
memcpy(module->imports[module->import_count].module_name, t.text.data, t.text.length);
|
memcpy(module->imports[module->import_count].module_name, t.text.data, t.text.length);
|
||||||
module->imports[module->import_count].module_name[t.text.length] = '\0';
|
module->imports[module->import_count].module_name[t.text.length] = '\0';
|
||||||
@@ -65,6 +73,7 @@ Module* parser_parse(TokenStream* ts) {
|
|||||||
|
|
||||||
t = tokenstream_next(ts);
|
t = tokenstream_next(ts);
|
||||||
if (t.token != TOKEN_SEMICOLON) {
|
if (t.token != TOKEN_SEMICOLON) {
|
||||||
|
log_on_line(&t.location, t.location.column_end, "expected ';' after import");
|
||||||
parser_free(module);
|
parser_free(module);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,10 @@ static TestCase s_tests[] = {
|
|||||||
{"tokenstream_unknown_token", test_tokenstream_unknown_token},
|
{"tokenstream_unknown_token", test_tokenstream_unknown_token},
|
||||||
{"tokenstream_info", test_tokenstream_info},
|
{"tokenstream_info", test_tokenstream_info},
|
||||||
{"parser_module_name", test_parser_module_name},
|
{"parser_module_name", test_parser_module_name},
|
||||||
|
{"parser_bad_module_name", test_parser_bad_module_name},
|
||||||
|
{"parser_missing_semicolon_module", test_parser_missing_semicolon_module},
|
||||||
|
{"parser_missing_semicolon_import", test_parser_missing_semicolon_import},
|
||||||
|
{"parser_bad_import_name", test_parser_bad_import_name},
|
||||||
{"parser_imports", test_parser_imports},
|
{"parser_imports", test_parser_imports},
|
||||||
{"log_error", test_log_error},
|
{"log_error", test_log_error},
|
||||||
{"log_on_line", test_log_on_line},
|
{"log_on_line", test_log_on_line},
|
||||||
|
|||||||
+32
-2
@@ -17,7 +17,37 @@ static void test_parser_bad_module_name(void) {
|
|||||||
TokenStream* ts = tokenstream_open("test.c", "import other_module;");
|
TokenStream* ts = tokenstream_open("test.c", "import other_module;");
|
||||||
Module* m = parser_parse(ts);
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
assert_log_file("bad_module_name.log", "expected error to be logged for bad module name");
|
assert_log_file("v0/tests/bad_module_name.txt", "expected error to be logged for bad module name");
|
||||||
|
|
||||||
|
parser_free(m);
|
||||||
|
tokenstream_close(ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_parser_missing_semicolon_module(void) {
|
||||||
|
TokenStream* ts = tokenstream_open("test.c", "module my_module");
|
||||||
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
|
assert_log_file("v0/tests/missing_semicolon_module.txt", "expected error for missing semicolon");
|
||||||
|
|
||||||
|
parser_free(m);
|
||||||
|
tokenstream_close(ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_parser_missing_semicolon_import(void) {
|
||||||
|
TokenStream* ts = tokenstream_open("test.c", "module my_module; import other_module");
|
||||||
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
|
assert_log_file("v0/tests/missing_semicolon_import.txt", "expected error for missing semicolon");
|
||||||
|
|
||||||
|
parser_free(m);
|
||||||
|
tokenstream_close(ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_parser_bad_import_name(void) {
|
||||||
|
TokenStream* ts = tokenstream_open("test.c", "module my_module; import ;");
|
||||||
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
|
assert_log_file("v0/tests/bad_import_name.txt", "expected error for bad import name");
|
||||||
|
|
||||||
parser_free(m);
|
parser_free(m);
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
@@ -36,4 +66,4 @@ static void test_parser_imports(void) {
|
|||||||
|
|
||||||
parser_free(m);
|
parser_free(m);
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
--- test.c ---
|
||||||
|
1| module my_module; import ;
|
||||||
|
^
|
||||||
|
expected module name to import
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| import other_module.c
|
|
||||||
^^^^^^
|
|
||||||
expected `module`
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
--- test.c ---
|
||||||
|
1| import other_module;
|
||||||
|
^^^^^^
|
||||||
|
expected 'module' keyword
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
--- test.c ---
|
||||||
|
1| module my_module; import other_module
|
||||||
|
^
|
||||||
|
expected ';' after import
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
--- test.c ---
|
||||||
|
1| module my_module
|
||||||
|
^
|
||||||
|
expected ';' after module name
|
||||||
Reference in New Issue
Block a user