[examples] custom user errors

This commit is contained in:
Allen Webster
2021-09-16 22:52:29 -07:00
parent 39f3d50419
commit 7d82d4f0ef
11 changed files with 123 additions and 83 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ bin/bld_core.sh show_ctx
metasrc="examples/metaprograms"
bin/bld_core.sh unit datadesk_like $metasrc/datadesk_like_template.c
bin/bld_core.sh unit node_errors $metasrc/node_errors.c
bin/bld_core.sh unit user_errors $metasrc/user_errors.c
bin/bld_core.sh unit parse_check $metasrc/parse_check.c
echo
+1 -16
View File
@@ -8,24 +8,9 @@ cd ..
build_path=$root_path/build
examples_path=$root_path/examples
echo ~~~ Running Output Parse Example ~~~
cd $examples_path
if [ -d "output_parse/examples" ]; then
cd output_parse/examples
mkdir -p output
cd output
$build_path/output_parse.exe ../example.mdesk ../example2.mdesk
fi
echo
echo ~~~ Running Error Generation Example ~~~
cd $build_path
./node_errors.exe $examples_path/node_errors/node_errors.mdesk
echo
echo ~~~ Running C++ Example ~~~
cd $build_path
./cpp_build_test.exe
./user_errors.exe $examples_path/mdesk_files/user_errors.mdesk
echo
###### Restore Path ###########################################################
-12
View File
@@ -1,12 +0,0 @@
@foo @bar Foo:
{
x, y, z,
a, b, c,
1, 2, 3,
}
@baz Blah:
{
x100 y200 z300
}
+14
View File
@@ -0,0 +1,14 @@
@foo @bar Foo:
{
x, y, z,
a, b, c,
1, 2, 3,
}
@baz Blah:
{
x100 y200 z300
}
Example: [100 + 200]
+1 -12
View File
@@ -1,20 +1,12 @@
/*
** Example: hello world
**
**
**
*/
//~ Includes and globals //////////////////////////////////////////////////////
// @notes Metadesk is a source-include library. So we include the
// file "md.c" into the usage code directly. The library
//~ includes and globals //////////////////////////////////////////////////////
#include "md.h"
#include "md.c"
// @notes For simple single-threaded memory management in a run-once-and-exit
// utility, a single global arena is our prefered setup.
static MD_Arena *arena = 0;
//~ main //////////////////////////////////////////////////////////////////////
@@ -22,9 +14,6 @@ static MD_Arena *arena = 0;
int
main(int argc, char **argv){
// setup the global arena
// @notes Metadesk arenas do linear reserve-and-commit allocation. This
// code makes an arena with a 1 terabyte reserve which works so long as
// we're only doing one or a few arenas.
arena = MD_ArenaAlloc(1ull << 40);
// parse a string
-33
View File
@@ -1,33 +0,0 @@
// Sample code to demonstrate errors being reported for certain nodes.
#define MD_ENABLE_PRINT_HELPERS 1
#include "md.h"
#include "md.c"
static MD_Arena *arena = 0;
int main(int argument_count, char **arguments)
{
arena = MD_ArenaAlloc(1ull << 40);
// NOTE(rjf): Parse all the files passed in via command line.
MD_Node *list = MD_MakeList(arena);
for(int i = 1; i < argument_count; i += 1)
{
MD_Node *root = MD_ParseWholeFile(arena, MD_S8CString(arguments[i])).node;
MD_PushNewReference(arena, list, root);
}
// NOTE(rjf): Print errors on every single node.
for(MD_EachNode(ref, list->first_child))
{
MD_Node *root = MD_ResolveNodeFromReference(ref);
for(MD_EachNode(node, root->first_child))
{
MD_PrintNodeMessageFmt(stderr, node, MD_MessageKind_Error, "This node has an error!");
}
}
return 0;
}
+4 -6
View File
@@ -7,7 +7,7 @@
**
*/
//~ Includes and globals //////////////////////////////////////////////////////
//~ includes and globals //////////////////////////////////////////////////////
#include "md.h"
#include "md.c"
@@ -22,12 +22,10 @@ static MD_Arena *arena = 0;
int main(int argc, char **argv)
{
// setup the global arena
// @notes Metadesk arenas do linear reserve-and-commit allocation. This
// code makes an arena with a 1 terabyte reserve which works so long as
// we're only doing one or a few arenas.
// @notes This code makes an arena with a 1 terabyte reserve which works as
// long as we only have one or a few arenas.
arena = MD_ArenaAlloc(1ull << 40);
// parse all files passed to the command line
MD_Node *list = MD_MakeList(arena);
for (int i = 1; i < argc; i += 1)
@@ -94,7 +92,7 @@ int main(int argc, char **argv)
// When the string needs to be finalized into a single contiguous
// block a user can just call `MD_S8ListJoin` as shown here.
MD_String8List stream = {0};
MD_DebugStringListFromNode(arena, &stream, node, 0, MD_S8Lit(" "), MD_GenerateFlags_Tree);
MD_DebugDumpFromNode(arena, &stream, node, 0, MD_S8Lit(" "), MD_GenerateFlags_Tree);
MD_String8 str = MD_S8ListJoin(arena, stream, 0);
fwrite(str.str, str.size, 1, stdout);
fwrite("\n", 1, 1, stdout);
+84
View File
@@ -0,0 +1,84 @@
/*
** Example: user-errors
**
** This example shows how to print custom error messages.
**
*/
//~ includes and globals //////////////////////////////////////////////////////
#include "md.h"
#include "md.c"
// @notes For simple single-threaded memory management in a run-once-and-exit
// utility, a single global arena is our recommended approach.
static MD_Arena *arena = 0;
//~ main //////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
char *argv_dummy[] = {
0,
"W:/metadesk/examples/mdesk_files/user_errors.mdesk"
};
argc = 2;
argv = argv_dummy;
// setup the global arena
arena = MD_ArenaAlloc(1ull << 40);
// parse all files passed to the command line
MD_Node *list = MD_MakeList(arena);
for(int i = 1; i < argc; i += 1)
{
// parse the file
MD_String8 file_name = MD_S8CString(argv[i]);
MD_ParseResult parse_result = MD_ParseWholeFile(arena, file_name);
// print metadesk errors
for (MD_Message *message = parse_result.errors.first;
message != 0;
message = message->next)
{
MD_CodeLoc code_loc = MD_CodeLocFromNode(message->node);
MD_PrintMessage(stderr, code_loc, message->kind, message->string);
}
// save to parse results list
MD_PushNewReference(arena, list, parse_result.node);
}
// check for custom errors
for(MD_EachNode(ref, list->first_child))
{
MD_Node *root = MD_ResolveNodeFromReference(ref);
for(MD_EachNode(node, root->first_child))
{
// top level node should have one or zero tags.
MD_Node *tag_2 = MD_TagFromIndex(node, 1);
if (!MD_NodeIsNil(tag_2))
{
MD_CodeLoc loc = MD_CodeLocFromNode(tag_2);
MD_PrintMessage(stderr, loc, MD_MessageKind_Error,
MD_S8Lit("Not supposed to have multiple tags."));
}
// top level sets with brackets should not have names
if ((node->flags & MD_NodeFlag_HasBracketLeft) ||
(node->flags & MD_NodeFlag_HasBracketRight))
{
if (node->string.size > 0)
{
MD_CodeLoc loc = MD_CodeLocFromNode(node);
MD_PrintMessage(stderr, loc, MD_MessageKind_Error,
MD_S8Lit("Nodes with brackets should not have names."));
}
}
}
}
return 0;
}
+2 -1
View File
@@ -5,8 +5,9 @@ immutable tree model
nil
Example Programs:
[ ] Metadesk hello world
[x] Metadesk hello world
[x] Metadesk parse checker
[x] User error checking
[ ] Example of helpers: string helpers, linked lists, map type
printing errors, cmd line, file iter
[x] Datadesk-like setup
+15 -1
View File
@@ -80,8 +80,22 @@ command_list =
{ "bin/build_tests.sh", .os = "mac" },
},
},
{
.name = "run_examples",
.out = "*compilation*",
.footer_panel = true,
.save_dirty_files = true,
.cursor_at_end = false,
.cmd =
{
{ "git_bash bin\\run_examples.sh", .os = "win" },
{ "bin/build_tests.sh", .os = "linux" },
{ "bin/build_tests.sh", .os = "mac" },
},
},
};
fkey_command[1] = "build_tests";
fkey_command[2] = "run_tests";
fkey_command[3] = "build_examples";
fkey_command[3] = "build_examples";
fkey_command[4] = "run_examples";
+1 -1
View File
@@ -2784,7 +2784,7 @@ MD_ChildFromIndex(MD_Node *node, int n)
MD_FUNCTION MD_Node *
MD_TagFromIndex(MD_Node *node, int n)
{
return MD_NodeFromIndex(node->first_child, MD_NilNode(), n);
return MD_NodeFromIndex(node->first_tag, MD_NilNode(), n);
}
MD_FUNCTION MD_Node *