[main] fix initializer of MD_Node

This commit is contained in:
Allen Webster
2021-03-30 20:52:22 -07:00
parent c30090c483
commit cc38ed2c96
3 changed files with 58 additions and 38 deletions
+19 -5
View File
@@ -198,9 +198,12 @@
//////////////////////////////// ////////////////////////////////
//~ Code Location Info. //~ Code Location Info.
@doc("Source code location using file, line, column coordinates")
@struct MD_CodeLoc: { @struct MD_CodeLoc: {
filename: MD_String8, filename: MD_String8,
@doc("Line numbers are 1 based, the lowest valid location is on line number 1.")
line: MD_u32, line: MD_u32,
@doc("Column numbers are 1 based, the lowest valid location is on column number 1.")
column: MD_u32, column: MD_u32,
}; };
@@ -815,6 +818,22 @@
return: MD_ParseResult, return: MD_ParseResult,
}; };
////////////////////////////////
//~ Location Conversion
@func MD_CodeLocFromFileOffset: {
filename: MD_String8,
base: *MD_u8,
off: *MD_u8,
return: MD_CodeLoc,
};
@func MD_CodeLocFromNode: {
node: *MD_Node,
return: MD_CodeLoc,
};
//////////////////////////////// ////////////////////////////////
//~ Tree/List Building //~ Tree/List Building
@@ -927,11 +946,6 @@
return: MD_b32, return: MD_b32,
}; };
@func MD_CodeLocFromNode: {
node: *MD_Node,
return: MD_CodeLoc,
};
@func MD_ChildCountFromNode: { @func MD_ChildCountFromNode: {
node: *MD_Node, node: *MD_Node,
return: MD_i64, return: MD_i64,
+4 -1
View File
@@ -723,6 +723,10 @@ MD_FUNCTION MD_ParseResult MD_ParseOneNode (MD_String8 filename, MD_String8
MD_FUNCTION MD_ParseResult MD_ParseWholeString (MD_String8 filename, MD_String8 contents); MD_FUNCTION MD_ParseResult MD_ParseWholeString (MD_String8 filename, MD_String8 contents);
MD_FUNCTION MD_ParseResult MD_ParseWholeFile (MD_String8 filename); 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_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);
@@ -744,7 +748,6 @@ MD_FUNCTION MD_Node * MD_ChildFromIndex(MD_Node *node, int n);
MD_FUNCTION MD_Node * MD_TagFromIndex(MD_Node *node, int n); MD_FUNCTION MD_Node * MD_TagFromIndex(MD_Node *node, int n);
MD_FUNCTION MD_Node * MD_TagArgFromIndex(MD_Node *node, MD_String8 tag_string, int n); MD_FUNCTION MD_Node * MD_TagArgFromIndex(MD_Node *node, MD_String8 tag_string, int n);
MD_FUNCTION MD_b32 MD_NodeHasTag(MD_Node *node, MD_String8 tag_string); MD_FUNCTION MD_b32 MD_NodeHasTag(MD_Node *node, MD_String8 tag_string);
MD_FUNCTION MD_CodeLoc MD_CodeLocFromNode(MD_Node *node);
MD_FUNCTION MD_i64 MD_ChildCountFromNode(MD_Node *node); MD_FUNCTION MD_i64 MD_ChildCountFromNode(MD_Node *node);
MD_FUNCTION MD_i64 MD_TagCountFromNode(MD_Node *node); MD_FUNCTION MD_i64 MD_TagCountFromNode(MD_Node *node);
MD_FUNCTION MD_Node * MD_Deref(MD_Node *node); MD_FUNCTION MD_Node * MD_Deref(MD_Node *node);
+35 -32
View File
@@ -25,10 +25,12 @@ static MD_Node _md_nil_node =
MD_ZERO_STRUCT, // string MD_ZERO_STRUCT, // string
MD_ZERO_STRUCT, // whole_string MD_ZERO_STRUCT, // whole_string
0xdeadffffffffffull, // string_hash 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 &_md_nil_node, // ref_target
{(MD_u8*)"`NIL DD NODE`", 13},
0,
0,
}; };
MD_PRIVATE_FUNCTION_IMPL void MD_PRIVATE_FUNCTION_IMPL void
@@ -1520,28 +1522,6 @@ MD_Parse_RequireKind(MD_ParseCtx *ctx, MD_TokenKind kind, MD_Token *out_token)
return result; return result;
} }
MD_PRIVATE_FUNCTION_IMPL MD_CodeLoc
_MD_CodeLocFromFileOffset(MD_String8 filename, MD_u8 * file_contents, MD_u8 *at)
{
MD_CodeLoc loc;
loc.filename = filename;
loc.line = 1;
loc.column = 1;
for(MD_u64 i = 0; file_contents+i < at && file_contents[i]; i += 1)
{
if(file_contents[i] == '\n')
{
loc.line += 1;
loc.column = 1;
}
else
{
loc.column += 1;
}
}
return loc;
}
MD_PRIVATE_FUNCTION_IMPL void MD_PRIVATE_FUNCTION_IMPL void
_MD_Error(MD_ParseCtx *ctx, MD_Node *node, MD_MessageKind kind, char *fmt, ...) _MD_Error(MD_ParseCtx *ctx, MD_Node *node, MD_MessageKind kind, char *fmt, ...)
{ {
@@ -2173,6 +2153,36 @@ MD_ParseWholeFile(MD_String8 filename)
return parse; return parse;
} }
MD_PRIVATE_FUNCTION_IMPL MD_CodeLoc
MD_CodeLocFromFileOffset(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[i] == '\n')
{
loc.line += 1;
loc.column = 1;
}
else
{
loc.column += 1;
}
}
return loc;
}
MD_FUNCTION_IMPL MD_CodeLoc
MD_CodeLocFromNode(MD_Node *node)
{
MD_CodeLoc loc = MD_CodeLocFromFileOffset(node->filename, node->file_contents, node->at);
return loc;
}
MD_FUNCTION_IMPL MD_b32 MD_FUNCTION_IMPL MD_b32
MD_NodeIsNil(MD_Node *node) MD_NodeIsNil(MD_Node *node)
{ {
@@ -2374,13 +2384,6 @@ MD_NodeHasTag(MD_Node *node, MD_String8 tag_string)
return !MD_NodeIsNil(MD_TagFromString(node, tag_string)); return !MD_NodeIsNil(MD_TagFromString(node, tag_string));
} }
MD_FUNCTION_IMPL MD_CodeLoc
MD_CodeLocFromNode(MD_Node *node)
{
MD_CodeLoc loc = _MD_CodeLocFromFileOffset(node->filename, node->file_contents, node->at);
return loc;
}
MD_FUNCTION_IMPL MD_i64 MD_FUNCTION_IMPL MD_i64
MD_ChildCountFromNode(MD_Node *node) MD_ChildCountFromNode(MD_Node *node)
{ {