From 8cbd0cc2dc75d764417f6dcf937253ab3a8792e1 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Fri, 1 Oct 2021 21:03:33 -0700 Subject: [PATCH] [expr parser] fix test fails; refactoring error reporting --- expressions_notes.txt | 9 +++++++-- source/md.c | 33 ++++++++++++++++++++++----------- source/md.h | 7 +++++-- tests/expression_tests.c | 20 ++++++++++++-------- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/expressions_notes.txt b/expressions_notes.txt index 5a0ba1b..6a1d8b9 100644 --- a/expressions_notes.txt +++ b/expressions_notes.txt @@ -3,7 +3,12 @@ I don't get what's going on with error messages: Why is it rebased by "original_first"? Why is it the same for 3 locations but different here? `MD_u64 error_offset = ctx.first->offset - first->offset;` - Why is it named `MD_MakeExprParseError` it doesn't seem to have any\ - details tied expression parsing. + Why is it named `MD_MakeExprParseError`? + +Some changes I made to `MD_MakeExprParseError` (I'm not obsessed I swear): + Attach user pointer to keep the testing features working as originally written + + + diff --git a/source/md.c b/source/md.c index 2b72e14..efec548 100644 --- a/source/md.c +++ b/source/md.c @@ -3387,7 +3387,8 @@ MD_ExprBakeOperatorTableFromList(MD_Arena *arena, MD_ExprOprList *list) { // TODO(allen): review: does it make sense to use these kinds of errors // for *this* which isn't derived from metadesk at all? - MD_Message *error = MD_MakeExprParseError(arena, error_str, 0); + MD_Message *error = MD_MakeExprParseError(arena, MD_MessageKind_Warning, error_str, 0, + op->op_ptr); MD_MessageListPush(&result.errors, error); } @@ -3427,13 +3428,15 @@ _MD_Expr_Make(MD_Arena *arena, MD_ExprOpr *op, MD_Node *op_node, return result; } -MD_FUNCTION void _MD_CtxAdvance(MD_ExprParseCtx *ctx) +MD_FUNCTION void +_MD_CtxAdvance(MD_ExprParseCtx *ctx) { ctx->first = ctx->first->next; } -MD_FUNCTION MD_b32 _MD_ExprOprConsumed(MD_ExprParseCtx *ctx, MD_ExprOprKind kind, MD_u32 min_precedence, - MD_ExprOpr **op_out) +MD_FUNCTION MD_b32 +_MD_ExprOprConsumed(MD_ExprParseCtx *ctx, MD_ExprOprKind kind, MD_u32 min_precedence, + MD_ExprOpr **op_out) { MD_b32 result = 0; if(!MD_NodeIsNil(ctx->first)) @@ -3494,7 +3497,8 @@ _MD_ExprParse_Atom(MD_Arena *arena, MD_ExprParseCtx *ctx) MD_String8 error_str = MD_S8Lit("Unexpected end of expression."); MD_u64 error_offset = last_non_null->offset + last_non_null->raw_string.size - ctx->original_first->offset; - MD_Message *error = MD_MakeExprParseError(arena, error_str, error_offset); + MD_Message *error = MD_MakeExprParseError(arena, MD_MessageKind_FatalError, + error_str, error_offset, last_non_null); MD_MessageListPush(&result.errors, error); } else if(node->flags & MD_NodeFlag_HasParenLeft && node->flags & MD_NodeFlag_HasParenRight) @@ -3529,7 +3533,8 @@ _MD_ExprParse_Atom(MD_Arena *arena, MD_ExprParseCtx *ctx) { MD_String8 error_str = MD_S8Fmt(arena, "Expected leaf. Got operator \"%.*s\".", MD_S8VArg(node->string)); MD_u64 error_offset = node->offset - ctx->original_first->offset; - MD_Message *error = MD_MakeExprParseError(arena, error_str, error_offset); + MD_Message *error = MD_MakeExprParseError(arena, MD_MessageKind_FatalError, + error_str, error_offset, node); MD_MessageListPush(&result.errors, error); } else if(node->flags & (MD_NodeFlag_HasParenLeft | MD_NodeFlag_HasParenRight | MD_NodeFlag_HasBracketLeft | @@ -3537,7 +3542,8 @@ _MD_ExprParse_Atom(MD_Arena *arena, MD_ExprParseCtx *ctx) { MD_String8 error_str = MD_S8Fmt(arena, "Unexpected set.", MD_S8VArg(node->string)); MD_u64 error_offset = node->offset - ctx->original_first->offset; - MD_Message *error = MD_MakeExprParseError(arena, error_str, error_offset); + MD_Message *error = MD_MakeExprParseError(arena, MD_MessageKind_FatalError, + error_str, error_offset, node); MD_MessageListPush(&result.errors, error); } else{ // NOTE(mal): leaf @@ -3629,7 +3635,8 @@ MD_ExprParse(MD_Arena *arena, MD_ExprOprTable *op_table, MD_String8 error_str = MD_S8Lit("Partial parse. Expected binary or unary postfix operator."); MD_u64 error_offset = ctx.first->offset - first->offset; - MD_Message *error = MD_MakeExprParseError(arena, error_str, error_offset); + MD_Message *error = MD_MakeExprParseError(arena, MD_MessageKind_FatalError, + error_str, error_offset, ctx.first); MD_MessageListPush(&result.errors, error); } } @@ -3666,10 +3673,14 @@ MD_ExprOprFromKindString(MD_ExprOprTable *table, MD_ExprOprKind kind, MD_String8 } MD_FUNCTION MD_Message* -MD_MakeExprParseError(MD_Arena *arena, MD_String8 str, MD_u64 offset) +MD_MakeExprParseError(MD_Arena *arena, MD_MessageKind kind, MD_String8 str, MD_u64 offset, + void *user_ptr) { - MD_Node *err_node = MD_MakeNode(arena, MD_NodeKind_ErrorMarker, MD_S8Lit(""), MD_S8Lit(""), offset); - return MD_MakeNodeError(arena, err_node, MD_MessageKind_FatalError, str); + MD_Node *err_node = MD_MakeNode(arena, MD_NodeKind_ErrorMarker, + MD_S8Lit(""), MD_S8Lit(""), offset); + MD_Message *result = MD_MakeNodeError(arena, err_node, kind, str); + result->user_ptr = user_ptr; + return(result); } diff --git a/source/md.h b/source/md.h index 3a5128b..e8a1f96 100644 --- a/source/md.h +++ b/source/md.h @@ -691,6 +691,7 @@ struct MD_Message MD_Node *node; MD_MessageKind kind; MD_String8 string; + void *user_ptr; }; typedef struct MD_MessageList MD_MessageList; @@ -783,7 +784,8 @@ struct MD_ExprParseCtx MD_Node *first; MD_Node *one_past_last; - struct{ + struct + { MD_ExprOpr *call_op; MD_ExprOpr *subscript_op; MD_ExprOpr *bracket_set_op; @@ -1112,7 +1114,8 @@ MD_FUNCTION MD_ExprParseResult MD_ExprParse(MD_Arena *arena, MD_ExprOprTable *op MD_FUNCTION MD_ExprOpr* MD_ExprOprFromKindString(MD_ExprOprTable *table, MD_ExprOprKind kind, MD_String8 s); -MD_FUNCTION MD_Message* MD_MakeExprParseError(MD_Arena *arena, MD_String8 str, MD_u64 offset); +MD_FUNCTION MD_Message* MD_MakeExprParseError(MD_Arena *arena, MD_MessageKind kind, + MD_String8 str, MD_u64 offset, void *ptr); //~ String Generation diff --git a/tests/expression_tests.c b/tests/expression_tests.c index 9bb7eb6..de5bcc3 100644 --- a/tests/expression_tests.c +++ b/tests/expression_tests.c @@ -245,8 +245,9 @@ operator_array[Op_##name].op = (MD_ExprOpr){ .op_id = Op_##name, .kind = MD_Expr MD_ExprOprPush(arena, &operator_list, MD_ExprOprKind_Null, 1, MD_S8Lit("+"), Op_Addition, plus_node); op_table = MD_ExprBakeOperatorTableFromList(arena, &operator_list); - MD_Assert(op_table.errors.max_message_kind = MD_MessageKind_Warning && op_table.errors.node_count == 1 && - op_table.errors.first->node == plus_node); + MD_Assert(op_table.errors.max_message_kind == MD_MessageKind_Warning && + op_table.errors.node_count == 1 && + op_table.errors.first->user_ptr == plus_node); // NOTE: Repeat operator operator_list = (MD_ExprOprList){0}; @@ -255,8 +256,9 @@ operator_array[Op_##name].op = (MD_ExprOpr){ .op_id = Op_##name, .kind = MD_Expr MD_ExprOprPush(arena, &operator_list, MD_ExprOprKind_Binary, 1, MD_S8Lit("+"), Op_Addition, plus_node_bis); op_table = MD_ExprBakeOperatorTableFromList(arena, &operator_list); - MD_Assert(op_table.errors.max_message_kind = MD_MessageKind_Warning && op_table.errors.node_count == 1 && - op_table.errors.first->node == plus_node_bis); + MD_Assert(op_table.errors.max_message_kind == MD_MessageKind_Warning && + op_table.errors.node_count == 1 && + op_table.errors.first->user_ptr == plus_node_bis); operator_list = (MD_ExprOprList){0}; // NOTE: Binary-postfix operator conflict @@ -265,8 +267,9 @@ operator_array[Op_##name].op = (MD_ExprOpr){ .op_id = Op_##name, .kind = MD_Expr MD_ExprOprPush(arena, &operator_list, MD_ExprOprKind_Postfix, 1, MD_S8Lit("+"), Op_Addition, plus_node_bis); op_table = MD_ExprBakeOperatorTableFromList(arena, &operator_list); - MD_Assert(op_table.errors.max_message_kind = MD_MessageKind_Warning && op_table.errors.node_count == 1 && - op_table.errors.first->node == plus_node_bis); + MD_Assert(op_table.errors.max_message_kind == MD_MessageKind_Warning + && op_table.errors.node_count == 1 && + op_table.errors.first->user_ptr == plus_node_bis); operator_list = (MD_ExprOprList){0}; // NOTE: Same precedence difference associativity conflict @@ -275,8 +278,9 @@ operator_array[Op_##name].op = (MD_ExprOpr){ .op_id = Op_##name, .kind = MD_Expr MD_ExprOprPush(arena, &operator_list, MD_ExprOprKind_BinaryRightAssociative, 1, MD_S8Lit("-"), Op_Addition, minus_node); op_table = MD_ExprBakeOperatorTableFromList(arena, &operator_list); - MD_Assert(op_table.errors.max_message_kind = MD_MessageKind_Warning && op_table.errors.node_count == 1 && - op_table.errors.first->node == minus_node); + MD_Assert(op_table.errors.max_message_kind == MD_MessageKind_Warning && + op_table.errors.node_count == 1 && + op_table.errors.first->user_ptr == minus_node); }