From 6478fb24c1dc210577ac57baa902e55f3bc3a169 Mon Sep 17 00:00:00 2001 From: Miguel Lechon Date: Sat, 27 Mar 2021 15:47:54 +0100 Subject: [PATCH] Error for MD_ParseWholeFile of missing file. --- source/md_impl.c | 12 +++++++++++- tests/sanity_tests.c | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/md_impl.c b/source/md_impl.c index 11d3d1d..145d9c5 100644 --- a/source/md_impl.c +++ b/source/md_impl.c @@ -1562,6 +1562,7 @@ _MD_Error(MD_ParseCtx *ctx, MD_Node *node, MD_MessageKind kind, char *fmt, ...) { MD_Error *error = _MD_PushArray(_MD_GetCtx(), MD_Error, 1); error->node = node; + error->kind = kind; va_list args; va_start(args, fmt); error->string = MD_PushStringFV(fmt, args); @@ -2144,7 +2145,16 @@ MD_ParseWholeString(MD_String8 filename, MD_String8 contents) MD_FUNCTION_IMPL MD_ParseResult MD_ParseWholeFile(MD_String8 filename) { - return MD_ParseWholeString(filename, MD_LoadEntireFile(filename)); + MD_String8 file_contents = MD_LoadEntireFile(filename); + MD_ParseResult parse = MD_ParseWholeString(filename, file_contents); + if(file_contents.str == 0) + { + MD_ParseCtx ctx = MD_Parse_InitializeCtx(filename, MD_S8Lit("")); + _MD_Error(&ctx, parse.node, MD_MessageKind_CatastrophicError, "Could not read file \"%.*s\"", + MD_StringExpand(filename)); + parse.first_error = ctx.first_error; + } + return parse; } MD_FUNCTION_IMPL MD_b32 diff --git a/tests/sanity_tests.c b/tests/sanity_tests.c index 42b4906..e3c4382 100644 --- a/tests/sanity_tests.c +++ b/tests/sanity_tests.c @@ -446,7 +446,7 @@ int main(void) } } - Test("Syntax Errors") + Test("Errors") { struct { char *s; int columns[2]; } tests[] = { {"{", {1}}, @@ -486,6 +486,12 @@ int main(void) } TestResult(columns_match); } + + { + MD_ParseResult parse = MD_ParseWholeFile(MD_S8Lit("__does_not_exist.md")); + TestResult(parse.node->kind == MD_NodeKind_File && parse.first_error); + } + } Test("Hash maps")