mirror of
https://github.com/Ed94/metadesk.git
synced 2026-07-31 20:00:07 +00:00
[examples] custom user errors
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
|
||||
@foo @bar Foo:
|
||||
{
|
||||
x, y, z,
|
||||
a, b, c,
|
||||
1, 2, 3,
|
||||
}
|
||||
|
||||
@baz Blah:
|
||||
{
|
||||
x100 y200 z300
|
||||
}
|
||||
@@ -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,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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user