rely on tree root for filename

This commit is contained in:
ryanfleury
2021-06-24 22:12:17 -06:00
parent 8f8c5cb9d7
commit 9175305b08
6 changed files with 45 additions and 39 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ int main(int argument_count, char **arguments)
for(MD_EachNode(root, first)) 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)); 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)); printf("Parse Input -> Output: %.*s -> %.*s\n", MD_StringExpand(code_filename), MD_StringExpand(info_filename));
@@ -127,7 +127,7 @@ int main(int argument_count, char **arguments)
{ {
PageInfo page_info = ParsePageInfo(root); 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"); FILE *file = fopen(MD_PushStringF("%.*s.html", MD_StringExpand(name_without_extension)).str, "wb");
if(file) if(file)
{ {
@@ -413,7 +413,8 @@ GeneratePageContent(MD_Map *index_table, SiteInfo *site_info, PageInfo *page_inf
{ {
if(strnode->string.str[i] == '@') 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_NodeIsNil(parse.node))
{ {
if(MD_NodeHasTag(node, MD_S8Lit("i"))) 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) if(slot->val)
{ {
MD_Node *node = slot->val; MD_Node *node = slot->val;
MD_Node *root = MD_RootFromNode(node);
PageInfo info = ParsePageInfo(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 filename_no_ext = MD_ChopExtension(MD_SkipFolder(filename));
MD_String8 link = MD_PushStringF("%.*s.html", MD_StringExpand(filename_no_ext)); MD_String8 link = MD_PushStringF("%.*s.html", MD_StringExpand(filename_no_ext));
MD_String8 name = info.title->string; MD_String8 name = info.title->string;
+3 -5
View File
@@ -362,8 +362,6 @@ struct MD_Node
MD_String8 comment_after; MD_String8 comment_after;
// Source code location information. // Source code location information.
MD_String8 filename;
MD_u8 *file_contents;
MD_u8 *at; MD_u8 *at;
// Reference. // 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); MD_FUNCTION MD_ParseResult MD_ParseWholeFile(MD_String8 filename);
//~ Location Conversion //~ 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); MD_FUNCTION MD_CodeLoc MD_CodeLocFromNode(MD_Node *node);
//~ Tree/List Building //~ Tree/List Building
MD_FUNCTION MD_b32 MD_NodeIsNil(MD_Node *node); MD_FUNCTION MD_b32 MD_NodeIsNil(MD_Node *node);
MD_FUNCTION MD_Node *MD_NilNode(void); MD_FUNCTION MD_Node *MD_NilNode(void);
MD_FUNCTION MD_Node *MD_MakeNode(MD_NodeKind kind, MD_String8 string, MD_FUNCTION MD_Node *MD_MakeNode(MD_NodeKind kind, MD_String8 string,
MD_String8 whole_string, MD_String8 filename, MD_String8 whole_string, MD_u8 *at);
MD_u8 *file_contents, MD_u8 *at);
MD_FUNCTION void MD_PushSibling(MD_Node **first, MD_Node **last, MD_Node *new_sibling); 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_PushChild(MD_Node *parent, MD_Node *new_child);
MD_FUNCTION void MD_PushTag(MD_Node *node, MD_Node *tag); 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_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 MD_Node * MD_NodeFromIndex(MD_Node *first, MD_Node *last, int n);
MD_FUNCTION int MD_IndexFromNode(MD_Node *node); 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_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_ChildFromString(MD_Node *node, MD_String8 child_string);
MD_FUNCTION MD_Node * MD_TagFromString(MD_Node *node, MD_String8 tag_string); MD_FUNCTION MD_Node * MD_TagFromString(MD_Node *node, MD_String8 tag_string);
+34 -28
View File
@@ -27,26 +27,24 @@ static MD_Node _md_nil_node =
0xdeadffffffffffull, // string_hash 0xdeadffffffffffull, // string_hash
MD_ZERO_STRUCT, // comment_before MD_ZERO_STRUCT, // comment_before
MD_ZERO_STRUCT, // comment_after MD_ZERO_STRUCT, // comment_after
{(MD_u8*)"`NIL DD NODE`", 13}, // filename
0, // file_contents
0, // at 0, // at
&_md_nil_node, // ref_target &_md_nil_node, // ref_target
}; };
//~ Memory Operations //~ Memory Operations
MD_PRIVATE_FUNCTION_IMPL void MD_FUNCTION_IMPL void
MD_MemoryZero(void *memory, MD_u64 size) MD_MemoryZero(void *memory, MD_u64 size)
{ {
memset(memory, 0, size); memset(memory, 0, size);
} }
MD_PRIVATE_FUNCTION_IMPL void MD_FUNCTION_IMPL void
MD_MemoryCopy(void *dest, void *src, MD_u64 size) MD_MemoryCopy(void *dest, void *src, MD_u64 size)
{ {
memcpy(dest, src, size); memcpy(dest, src, size);
} }
MD_PRIVATE_FUNCTION_IMPL void * MD_FUNCTION_IMPL void *
MD_AllocZero(MD_u64 size) MD_AllocZero(MD_u64 size)
{ {
#if !defined(MD_IMPL_Alloc) #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_MakeNode_Ctx(MD_ParseCtx *ctx, MD_NodeKind kind,
MD_String8 string, MD_String8 outer, MD_u8 *at) 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); 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_ParseWholeString(MD_String8 filename, MD_String8 contents)
{ {
MD_ParseResult result = MD_ZERO_STRUCT; MD_ParseResult result = MD_ZERO_STRUCT;
// TODO(allen): we want to make the string for this actually just MD_String8 root_string = filename;
// be the filename in the root/file idea. MD_Node *root = MD_MakeNode(MD_NodeKind_File, root_string, root_string, contents.str);
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);
if(contents.size > 0) if(contents.size > 0)
{ {
// NOTE(allen): setup parse context // NOTE(allen): setup parse context
@@ -2062,23 +2057,26 @@ MD_ParseWholeFile(MD_String8 filename)
//~ Location Conversions //~ Location Conversions
MD_PRIVATE_FUNCTION_IMPL MD_CodeLoc MD_FUNCTION_IMPL MD_CodeLoc
MD_CodeLocFromFileOffset(MD_String8 filename, MD_u8 *base, MD_u8 *at) MD_CodeLocFromFileBaseOff(MD_String8 filename, MD_u8 *base, MD_u8 *at)
{ {
MD_CodeLoc loc; MD_CodeLoc loc;
loc.filename = filename; loc.filename = filename;
loc.line = 1; loc.line = 1;
loc.column = 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; if(base[i] == '\n')
loc.column = 1; {
} loc.line += 1;
else loc.column = 1;
{ }
loc.column += 1; else
{
loc.column += 1;
}
} }
} }
return loc; return loc;
@@ -2087,7 +2085,8 @@ MD_CodeLocFromFileOffset(MD_String8 filename, MD_u8 *base, MD_u8 *at)
MD_FUNCTION_IMPL MD_CodeLoc MD_FUNCTION_IMPL MD_CodeLoc
MD_CodeLocFromNode(MD_Node *node) 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; return loc;
} }
@@ -2104,8 +2103,7 @@ MD_NilNode(void) { return &_md_nil_node; }
MD_FUNCTION_IMPL MD_Node * MD_FUNCTION_IMPL MD_Node *
MD_MakeNode(MD_NodeKind kind, MD_String8 string, MD_MakeNode(MD_NodeKind kind, MD_String8 string,
MD_String8 whole_string, MD_String8 filename, MD_String8 whole_string, MD_u8 *at)
MD_u8 *file_contents, MD_u8 *at)
{ {
MD_Node *node = MD_PushArray(MD_Node, 1); MD_Node *node = MD_PushArray(MD_Node, 1);
node->kind = kind; node->kind = kind;
@@ -2114,8 +2112,6 @@ MD_MakeNode(MD_NodeKind kind, MD_String8 string,
node->next = node->prev = node->parent = node->next = node->prev = node->parent =
node->first_child = node->last_child = node->first_child = node->last_child =
node->first_tag = node->last_tag = node->ref_target = MD_NilNode(); node->first_tag = node->last_tag = node->ref_target = MD_NilNode();
node->filename = filename;
node->file_contents = file_contents;
node->at = at; node->at = at;
return node; return node;
} }
@@ -2161,8 +2157,7 @@ MD_PushTag(MD_Node *node, MD_Node *tag)
MD_FUNCTION_IMPL MD_Node* MD_FUNCTION_IMPL MD_Node*
MD_PushReference(MD_Node *list, MD_Node *target) MD_PushReference(MD_Node *list, MD_Node *target)
{ {
MD_Node *n = MD_MakeNode(MD_NodeKind_Reference, target->string, target->whole_string, MD_Node *n = MD_MakeNode(MD_NodeKind_Reference, target->string, target->whole_string, target->at);
target->filename, target->file_contents, target->at);
n->ref_target = target; n->ref_target = target;
MD_PushChild(list, n); MD_PushChild(list, n);
return(n); return(n);
@@ -2215,6 +2210,17 @@ MD_IndexFromNode(MD_Node *node)
return idx; 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_FUNCTION_IMPL MD_Node *
MD_NextNodeSibling(MD_Node *last, MD_String8 string) MD_NextNodeSibling(MD_Node *last, MD_String8 string)
{ {
+1 -1
View File
@@ -66,7 +66,7 @@ static MD_Node * NewChildLabel(MD_Node *parent, MD_String8 label)
{ {
MD_Node *result = 0; 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) if(parent)
{ {
MD_PushChild(parent, result); MD_PushChild(parent, result);
+1 -1
View File
@@ -58,7 +58,7 @@ EndTest(void)
static MD_Node * static MD_Node *
MakeTestNode(MD_NodeKind kind, MD_String8 string) 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 * static MD_C_Expr *