[expr parser] Treat all sets as leaves (note 1a).

This commit is contained in:
Miguel Lechon
2021-10-03 18:38:13 +02:00
parent 1372db5610
commit d1745050dd
3 changed files with 29 additions and 24 deletions
+8 -11
View File
@@ -3379,6 +3379,9 @@ MD_ExprBakeOperatorTableFromList(MD_Arena *arena, MD_ExprOprList *list)
} }
else else
{ {
// TODO(mal): Allow "[]" and "()" only as unary postfix
// TODO(mal): Disallow all other set types ("[)", "(]", "{}") from becoming operators
for(MD_ExprOpr *op2 = list->first; for(MD_ExprOpr *op2 = list->first;
op2 != op; op2 != op;
op2 = op2->next) op2 = op2->next)
@@ -3520,8 +3523,6 @@ MD_ExprParse_MakeContext(MD_ExprOprTable *op_table)
MD_ExprParseCtx result = MD_ZERO_STRUCT; MD_ExprParseCtx result = MD_ZERO_STRUCT;
result.op_table = op_table; result.op_table = op_table;
result.accel.bracket_set_op = MD_ExprOprFromKindString(op_table, MD_ExprOprKind_Prefix, MD_S8Lit("[]"));
result.accel.brace_set_op = MD_ExprOprFromKindString(op_table, MD_ExprOprKind_Prefix, MD_S8Lit("{}"));
result.accel.call_op = MD_ExprOprFromKindString(op_table, MD_ExprOprKind_Postfix, MD_S8Lit("()")); result.accel.call_op = MD_ExprOprFromKindString(op_table, MD_ExprOprKind_Postfix, MD_S8Lit("()"));
result.accel.subscript_op = MD_ExprOprFromKindString(op_table, MD_ExprOprKind_Binary, MD_S8Lit("[]")); result.accel.subscript_op = MD_ExprOprFromKindString(op_table, MD_ExprOprKind_Binary, MD_S8Lit("[]"));
@@ -3604,16 +3605,12 @@ MD_ExprParse_Atom(MD_Arena *arena, MD_ExprParseCtx *ctx, MD_Node **iter,
*iter = MD_NodeNextWithLimit(*iter, opl); *iter = MD_NodeNextWithLimit(*iter, opl);
result = MD_ExprParse_TopLevel(arena, ctx, node->first_child, MD_NilNode()); result = MD_ExprParse_TopLevel(arena, ctx, node->first_child, MD_NilNode());
} }
// TODO(allen): this part seems a bit odd. I think any (delimited) set else if(((node->flags & MD_NodeFlag_HasBraceLeft) && (node->flags & MD_NodeFlag_HasBraceRight)) ||
// should get this treatment, without making these operators to enable them. ((node->flags & MD_NodeFlag_HasBracketLeft) && (node->flags & MD_NodeFlag_HasBracketRight)) ||
else if(((node->flags & MD_NodeFlag_HasBracketLeft) && (node->flags & MD_NodeFlag_HasBracketRight) && ((node->flags & MD_NodeFlag_HasBracketLeft) && (node->flags & MD_NodeFlag_HasParenRight)) ||
ctx->accel.bracket_set_op) || ((node->flags & MD_NodeFlag_HasParenLeft) && (node->flags & MD_NodeFlag_HasBracketRight)))
((node->flags & MD_NodeFlag_HasBraceLeft) && { // NOTE(mal): Unparsed leaf sets ({...}, [...], [...), (...])
(node->flags & MD_NodeFlag_HasBraceRight) &&
ctx->accel.brace_set_op))
{
*iter = MD_NodeNextWithLimit(*iter, opl); *iter = MD_NodeNextWithLimit(*iter, opl);
// NOTE(mal): Unparsed leaf sets ({ ... }, [ ... ])
result = MD_Expr_NewLeaf(arena, node); result = MD_Expr_NewLeaf(arena, node);
} }
else if(MD_ExprParse_OprConsume(ctx, iter, opl, MD_ExprOprKind_Prefix, 1, &op)) else if(MD_ExprParse_OprConsume(ctx, iter, opl, MD_ExprOprKind_Prefix, 1, &op))
-2
View File
@@ -785,8 +785,6 @@ struct MD_ExprParseCtx
{ {
MD_ExprOpr *call_op; MD_ExprOpr *call_op;
MD_ExprOpr *subscript_op; MD_ExprOpr *subscript_op;
MD_ExprOpr *bracket_set_op;
MD_ExprOpr *brace_set_op;
} accel; } accel;
MD_MessageList errors; MD_MessageList errors;
+21 -11
View File
@@ -75,8 +75,6 @@ X(AssignBitwiseXor, "^=", BinaryRightAssociative, 4) \
X(AssignBitwiseOr, "|=", BinaryRightAssociative, 4) X(AssignBitwiseOr, "|=", BinaryRightAssociative, 4)
// TODO(allen): I don't think we want to do this // TODO(allen): I don't think we want to do this
// X(BracketSet, "[]", Prefix, 3)
// X(BraceSet, "{}", Prefix, 3)
// X(Cast "()", Prefix, 17) // X(Cast "()", Prefix, 17)
// X(Comma, ",", Binary, 3) // X(Comma, ",", Binary, 3)
@@ -193,19 +191,30 @@ static void parenthesize_exclude_outer(MD_Arena *arena, OperatorDescription *des
} }
else else
{ {
if(node->md_node->flags & MD_NodeFlag_HasBracketLeft && if(node->md_node->flags & MD_NodeFlag_HasParenLeft)
node->md_node->flags & MD_NodeFlag_HasBracketRight)
{ {
MD_S8ListPush(arena, l, MD_S8Lit("[...]")); MD_S8ListPush(arena, l, MD_S8Lit("("));
} }
else if(node->md_node->flags & MD_NodeFlag_HasBraceLeft && else if(node->md_node->flags & MD_NodeFlag_HasBraceLeft)
node->md_node->flags & MD_NodeFlag_HasBraceRight)
{ {
MD_S8ListPush(arena, l, MD_S8Lit("{...}")); MD_S8ListPush(arena, l, MD_S8Lit("{"));
} }
else else if(node->md_node->flags & MD_NodeFlag_HasBracketLeft)
{ {
MD_S8ListPush(arena, l, MD_S8Lit("???")); MD_S8ListPush(arena, l, MD_S8Lit("["));
}
MD_S8ListPush(arena, l, MD_S8Lit("..."));
if(node->md_node->flags & MD_NodeFlag_HasParenRight)
{
MD_S8ListPush(arena, l, MD_S8Lit(")"));
}
else if(node->md_node->flags & MD_NodeFlag_HasBraceRight){
MD_S8ListPush(arena, l, MD_S8Lit("}"));
}
else if(node->md_node->flags & MD_NodeFlag_HasBracketRight){
MD_S8ListPush(arena, l, MD_S8Lit("]"));
} }
} }
} }
@@ -316,7 +325,7 @@ operator_array[Op_##name].op = (MD_ExprOpr){ .op_id = Op_##name, .kind = MD_Expr
{ .q = "a(b,c)", .a = "a(...)" }, { .q = "a(b,c)", .a = "a(...)" },
{ .q = "a.b()", .a = "(a . b)(...)" }, { .q = "a.b()", .a = "(a . b)(...)" },
{ .q = "sizeof a + b", .a = "(sizeof a) + b" }, { .q = "sizeof a + b", .a = "(sizeof a) + b" },
{ .q = "[1, 100] * n", .a = "[...] * n" }, { .q = "[1, 100] * [1)",.a = "[...] * [...)" },
{ .q = "a[b+c]", .a = "a[b + c]" }, { .q = "a[b+c]", .a = "a[b + c]" },
{ .q = "a + b[c[d]+e]", .a = "a + (b[(c[d]) + e])" }, { .q = "a + b[c[d]+e]", .a = "a + (b[(c[d]) + e])" },
{ .q = "a++ + b", .a = "(a++) + b" }, { .q = "a++ + b", .a = "(a++) + b" },
@@ -356,6 +365,7 @@ operator_array[Op_##name].op = (MD_ExprOpr){ .op_id = Op_##name, .kind = MD_Expr
{ .q = "a+", .a = "", ExpressionErrorKind_Expr, 2}, { .q = "a+", .a = "", ExpressionErrorKind_Expr, 2},
{ .q = "a 1", .a = "", ExpressionErrorKind_Expr, 2}, { .q = "a 1", .a = "", ExpressionErrorKind_Expr, 2},
{ .q = "a + (a+)", .a = "", ExpressionErrorKind_Expr, 7}, { .q = "a + (a+)", .a = "", ExpressionErrorKind_Expr, 7},
// TODO(mal): This test should not generate an error when [] is postfix and a+ remains unparsed
{ .q = "a[a+]", .a = "", ExpressionErrorKind_Expr, 4}, { .q = "a[a+]", .a = "", ExpressionErrorKind_Expr, 4},
}; };