From 9175305b08e6b6987519224b7c859c1537cbde32 Mon Sep 17 00:00:00 2001 From: ryanfleury Date: Thu, 24 Jun 2021 22:12:17 -0600 Subject: [PATCH] rely on tree root for filename --- samples/output_parse/output_parse.c | 2 +- .../static_site_generator.c | 8 ++- source/md.h | 8 +-- source/md_impl.c | 62 ++++++++++--------- tests/grammar.c | 2 +- tests/sanity_tests.c | 2 +- 6 files changed, 45 insertions(+), 39 deletions(-) diff --git a/samples/output_parse/output_parse.c b/samples/output_parse/output_parse.c index 1e294a6..fc70f69 100644 --- a/samples/output_parse/output_parse.c +++ b/samples/output_parse/output_parse.c @@ -71,7 +71,7 @@ int main(int argument_count, char **arguments) for(MD_EachNode(root, first)) { - MD_String8 code_filename = MD_ChopExtension(MD_SkipFolder(root->filename)); + MD_String8 code_filename = MD_ChopExtension(MD_SkipFolder(root->string)); MD_String8 info_filename = MD_PushStringF("parsed_%.*s.txt", MD_StringExpand(code_filename)); printf("Parse Input -> Output: %.*s -> %.*s\n", MD_StringExpand(code_filename), MD_StringExpand(info_filename)); diff --git a/samples/static_site_generator/static_site_generator.c b/samples/static_site_generator/static_site_generator.c index c8de4f8..4689f97 100644 --- a/samples/static_site_generator/static_site_generator.c +++ b/samples/static_site_generator/static_site_generator.c @@ -127,7 +127,7 @@ int main(int argument_count, char **arguments) { PageInfo page_info = ParsePageInfo(root); - MD_String8 name_without_extension = MD_SkipFolder(MD_ChopExtension(root->filename)); + MD_String8 name_without_extension = MD_SkipFolder(MD_ChopExtension(root->string)); FILE *file = fopen(MD_PushStringF("%.*s.html", MD_StringExpand(name_without_extension)).str, "wb"); if(file) { @@ -413,7 +413,8 @@ GeneratePageContent(MD_Map *index_table, SiteInfo *site_info, PageInfo *page_inf { if(strnode->string.str[i] == '@') { - MD_ParseResult parse = MD_ParseOneNode(node->filename, MD_StringSubstring(strnode->string, i, strnode->string.size)); + MD_Node *root = MD_RootFromNode(node); + MD_ParseResult parse = MD_ParseOneNode(root->string, MD_StringSubstring(strnode->string, i, strnode->string.size)); if(!MD_NodeIsNil(parse.node)) { if(MD_NodeHasTag(node, MD_S8Lit("i"))) @@ -521,9 +522,10 @@ GeneratePageContent(MD_Map *index_table, SiteInfo *site_info, PageInfo *page_inf if(slot->val) { MD_Node *node = slot->val; + MD_Node *root = MD_RootFromNode(node); PageInfo info = ParsePageInfo(node); - MD_String8 filename = node->filename; + MD_String8 filename = root->string; MD_String8 filename_no_ext = MD_ChopExtension(MD_SkipFolder(filename)); MD_String8 link = MD_PushStringF("%.*s.html", MD_StringExpand(filename_no_ext)); MD_String8 name = info.title->string; diff --git a/source/md.h b/source/md.h index 9ac6e26..d73c0b2 100644 --- a/source/md.h +++ b/source/md.h @@ -362,8 +362,6 @@ struct MD_Node MD_String8 comment_after; // Source code location information. - MD_String8 filename; - MD_u8 *file_contents; MD_u8 *at; // Reference. @@ -693,15 +691,14 @@ MD_FUNCTION MD_ParseResult MD_ParseWholeString(MD_String8 filename, MD_String8 c MD_FUNCTION MD_ParseResult MD_ParseWholeFile(MD_String8 filename); //~ Location Conversion -MD_FUNCTION MD_CodeLoc MD_CodeLocFromFileOffset(MD_String8 filename, MD_u8 *base, MD_u8 *off); +MD_FUNCTION MD_CodeLoc MD_CodeLocFromFileBaseOff(MD_String8 filename, MD_u8 *base, MD_u8 *off); MD_FUNCTION MD_CodeLoc MD_CodeLocFromNode(MD_Node *node); //~ Tree/List Building MD_FUNCTION MD_b32 MD_NodeIsNil(MD_Node *node); MD_FUNCTION MD_Node *MD_NilNode(void); MD_FUNCTION MD_Node *MD_MakeNode(MD_NodeKind kind, MD_String8 string, - MD_String8 whole_string, MD_String8 filename, - MD_u8 *file_contents, MD_u8 *at); + MD_String8 whole_string, MD_u8 *at); MD_FUNCTION void MD_PushSibling(MD_Node **first, MD_Node **last, MD_Node *new_sibling); MD_FUNCTION void MD_PushChild(MD_Node *parent, MD_Node *new_child); MD_FUNCTION void MD_PushTag(MD_Node *node, MD_Node *tag); @@ -711,6 +708,7 @@ MD_FUNCTION MD_Node *MD_PushReference(MD_Node *list, MD_Node *target); MD_FUNCTION MD_Node * MD_NodeFromString(MD_Node *first, MD_Node *last, MD_String8 string); MD_FUNCTION MD_Node * MD_NodeFromIndex(MD_Node *first, MD_Node *last, int n); MD_FUNCTION int MD_IndexFromNode(MD_Node *node); +MD_FUNCTION MD_Node * MD_RootFromNode(MD_Node *node); MD_FUNCTION MD_Node * MD_NextNodeSibling(MD_Node *last, MD_String8 string); MD_FUNCTION MD_Node * MD_ChildFromString(MD_Node *node, MD_String8 child_string); MD_FUNCTION MD_Node * MD_TagFromString(MD_Node *node, MD_String8 tag_string); diff --git a/source/md_impl.c b/source/md_impl.c index c4eb774..809ca9d 100644 --- a/source/md_impl.c +++ b/source/md_impl.c @@ -27,26 +27,24 @@ static MD_Node _md_nil_node = 0xdeadffffffffffull, // string_hash MD_ZERO_STRUCT, // comment_before MD_ZERO_STRUCT, // comment_after - {(MD_u8*)"`NIL DD NODE`", 13}, // filename - 0, // file_contents 0, // at &_md_nil_node, // ref_target }; //~ Memory Operations -MD_PRIVATE_FUNCTION_IMPL void +MD_FUNCTION_IMPL void MD_MemoryZero(void *memory, MD_u64 size) { memset(memory, 0, size); } -MD_PRIVATE_FUNCTION_IMPL void +MD_FUNCTION_IMPL void MD_MemoryCopy(void *dest, void *src, MD_u64 size) { memcpy(dest, src, size); } -MD_PRIVATE_FUNCTION_IMPL void * +MD_FUNCTION_IMPL void * MD_AllocZero(MD_u64 size) { #if !defined(MD_IMPL_Alloc) @@ -1067,7 +1065,7 @@ MD_PRIVATE_FUNCTION_IMPL MD_Node * _MD_MakeNode_Ctx(MD_ParseCtx *ctx, MD_NodeKind kind, MD_String8 string, MD_String8 outer, MD_u8 *at) { - return MD_MakeNode(kind, string, outer, ctx->filename, ctx->file_contents.str, at); + return MD_MakeNode(kind, string, outer, at); } MD_PRIVATE_FUNCTION_IMPL void _MD_ParseTagList(MD_ParseCtx *ctx, MD_Node **first_out, MD_Node **last_out); @@ -1996,11 +1994,8 @@ MD_FUNCTION_IMPL MD_ParseResult MD_ParseWholeString(MD_String8 filename, MD_String8 contents) { MD_ParseResult result = MD_ZERO_STRUCT; - // TODO(allen): we want to make the string for this actually just - // be the filename in the root/file idea. - MD_String8 root_string = MD_PushStringF("`DD Parsed From \"%.*s\"`", MD_StringExpand(filename)); - MD_Node *root = MD_MakeNode(MD_NodeKind_File, root_string, root_string, - filename, contents.str, contents.str); + MD_String8 root_string = filename; + MD_Node *root = MD_MakeNode(MD_NodeKind_File, root_string, root_string, contents.str); if(contents.size > 0) { // NOTE(allen): setup parse context @@ -2062,23 +2057,26 @@ MD_ParseWholeFile(MD_String8 filename) //~ Location Conversions -MD_PRIVATE_FUNCTION_IMPL MD_CodeLoc -MD_CodeLocFromFileOffset(MD_String8 filename, MD_u8 *base, MD_u8 *at) +MD_FUNCTION_IMPL MD_CodeLoc +MD_CodeLocFromFileBaseOff(MD_String8 filename, MD_u8 *base, MD_u8 *at) { MD_CodeLoc loc; loc.filename = filename; loc.line = 1; loc.column = 1; - for(MD_u64 i = 0; base+i < at && base[i]; i += 1) + if(base != 0) { - if(base[i] == '\n') + for(MD_u64 i = 0; base+i < at && base[i]; i += 1) { - loc.line += 1; - loc.column = 1; - } - else - { - loc.column += 1; + if(base[i] == '\n') + { + loc.line += 1; + loc.column = 1; + } + else + { + loc.column += 1; + } } } return loc; @@ -2087,7 +2085,8 @@ MD_CodeLocFromFileOffset(MD_String8 filename, MD_u8 *base, MD_u8 *at) MD_FUNCTION_IMPL MD_CodeLoc MD_CodeLocFromNode(MD_Node *node) { - MD_CodeLoc loc = MD_CodeLocFromFileOffset(node->filename, node->file_contents, node->at); + MD_Node *root = MD_RootFromNode(node); + MD_CodeLoc loc = MD_CodeLocFromFileBaseOff(root->string, root->at, node->at); return loc; } @@ -2104,8 +2103,7 @@ MD_NilNode(void) { return &_md_nil_node; } MD_FUNCTION_IMPL MD_Node * MD_MakeNode(MD_NodeKind kind, MD_String8 string, - MD_String8 whole_string, MD_String8 filename, - MD_u8 *file_contents, MD_u8 *at) + MD_String8 whole_string, MD_u8 *at) { MD_Node *node = MD_PushArray(MD_Node, 1); node->kind = kind; @@ -2114,8 +2112,6 @@ MD_MakeNode(MD_NodeKind kind, MD_String8 string, node->next = node->prev = node->parent = node->first_child = node->last_child = node->first_tag = node->last_tag = node->ref_target = MD_NilNode(); - node->filename = filename; - node->file_contents = file_contents; node->at = at; return node; } @@ -2161,8 +2157,7 @@ MD_PushTag(MD_Node *node, MD_Node *tag) MD_FUNCTION_IMPL MD_Node* MD_PushReference(MD_Node *list, MD_Node *target) { - MD_Node *n = MD_MakeNode(MD_NodeKind_Reference, target->string, target->whole_string, - target->filename, target->file_contents, target->at); + MD_Node *n = MD_MakeNode(MD_NodeKind_Reference, target->string, target->whole_string, target->at); n->ref_target = target; MD_PushChild(list, n); return(n); @@ -2215,6 +2210,17 @@ MD_IndexFromNode(MD_Node *node) return idx; } +MD_FUNCTION MD_Node * +MD_RootFromNode(MD_Node *node) +{ + MD_Node *parent = node; + for(MD_Node *p = parent; !MD_NodeIsNil(p); p = p->parent) + { + parent = p; + } + return parent; +} + MD_FUNCTION_IMPL MD_Node * MD_NextNodeSibling(MD_Node *last, MD_String8 string) { diff --git a/tests/grammar.c b/tests/grammar.c index de5a5ba..b312d63 100644 --- a/tests/grammar.c +++ b/tests/grammar.c @@ -66,7 +66,7 @@ static MD_Node * NewChildLabel(MD_Node *parent, MD_String8 label) { MD_Node *result = 0; - result = MD_MakeNode(MD_NodeKind_Label, label, label, MD_S8Lit(""), 0, 0); + result = MD_MakeNode(MD_NodeKind_Label, label, label, 0); if(parent) { MD_PushChild(parent, result); diff --git a/tests/sanity_tests.c b/tests/sanity_tests.c index 1a3e0b2..64536a5 100644 --- a/tests/sanity_tests.c +++ b/tests/sanity_tests.c @@ -58,7 +58,7 @@ EndTest(void) static MD_Node * MakeTestNode(MD_NodeKind kind, MD_String8 string) { - return MD_MakeNode(kind, string, string, MD_S8Lit("`TEST_NODE`"), 0, 0); + return MD_MakeNode(kind, string, string, 0); } static MD_C_Expr *