From 36862a18d676418f7b883f50bb00099b4fabef3c Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sun, 31 Aug 2025 15:14:26 -0700 Subject: [PATCH] tightened up parsing of $field=X record constructor expressions --- src/parse.y | 63 ++++++++++++------- .../Baseline/language.bogus-field-assign/out | 2 + .../Baseline/language.record-bad-ctor3/out | 2 +- .../btest/language/bogus-field-assign.zeek | 6 ++ 4 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 testing/btest/Baseline/language.bogus-field-assign/out create mode 100644 testing/btest/language/bogus-field-assign.zeek diff --git a/src/parse.y b/src/parse.y index ea4fc7ae26..03b77683af 100644 --- a/src/parse.y +++ b/src/parse.y @@ -61,7 +61,7 @@ %type local_id_list case_type_list %type init_class %type TOK_CONSTANT -%type expr opt_expr rhs opt_init anonymous_function lambda_body index_slice opt_deprecated when_condition +%type expr opt_expr rhs opt_init anonymous_function lambda_body index_slice opt_deprecated when_condition field_assign %type event %type stmt stmt_list func_body for_head %type simple_type type opt_type enum_body @@ -70,7 +70,7 @@ %type type_decl formal_args_decl %type type_decl_list formal_args_decl_list %type formal_args -%type expr_list opt_expr_list rhs_expr_list +%type expr_list opt_expr_list rhs_expr_list opt_exprs_or_field_assigns field_assigns %type case %type case_list %type attr @@ -756,22 +756,6 @@ expr: $$ = new FieldExpr({AdoptRef{}, $1}, $3); } - | '$' TOK_ID '=' expr - { - set_location(@1, @4); - $$ = new FieldAssignExpr($2, {AdoptRef{}, $4}); - } - - | '$' TOK_ID begin_lambda '=' - { - func_hdr_location = @1; - $3->SetInferReturnType(true); - } - lambda_body - { - $$ = new FieldAssignExpr($2, IntrusivePtr{AdoptRef{}, $6}); - } - | expr TOK_IN expr { set_location(@1, @3); @@ -786,7 +770,7 @@ expr: ExprPtr{AdoptRef{}, $3})); } - | '[' opt_expr_list ']' + | '[' opt_exprs_or_field_assigns ']' { set_location(@1, @3); @@ -810,7 +794,7 @@ expr: $$ = $2; } - | TOK_RECORD '(' expr_list ')' + | TOK_RECORD '(' field_assigns expr_list_opt_comma ')' { set_location(@1, @4); $$ = new RecordConstructorExpr({AdoptRef{}, $3}); @@ -843,7 +827,7 @@ expr: ++in_init; } - opt_expr_list + opt_exprs_or_field_assigns { if ( expr_is_table_type_name($1) ) --in_init; @@ -1018,6 +1002,43 @@ expr: } ; +opt_exprs_or_field_assigns: + { $$ = new ListExpr(); } + | expr_list expr_list_opt_comma + | field_assigns expr_list_opt_comma + ; + +field_assigns: + field_assigns ',' field_assign + { + set_location(@1, @3); + $1->Append({AdoptRef{}, $3}); + } + | field_assign + { + set_location(@1); + expr_list_has_opt_comma = 0; + $$ = new ListExpr({AdoptRef{}, $1}); + } + ; + +field_assign: '$' TOK_ID '=' expr + { + set_location(@1, @4); + $$ = new FieldAssignExpr($2, {AdoptRef{}, $4}); + } + + | '$' TOK_ID begin_lambda '=' + { + func_hdr_location = @1; + $3->SetInferReturnType(true); + } + lambda_body + { + $$ = new FieldAssignExpr($2, IntrusivePtr{AdoptRef{}, $6}); + } + ; + rhs: '{' { ++in_init; } rhs_expr_list '}' { --in_init; diff --git a/testing/btest/Baseline/language.bogus-field-assign/out b/testing/btest/Baseline/language.bogus-field-assign/out new file mode 100644 index 0000000000..fc5f26152f --- /dev/null +++ b/testing/btest/Baseline/language.bogus-field-assign/out @@ -0,0 +1,2 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +error in <...>/bogus-field-assign.zeek, line 6: syntax error, at or near "$" diff --git a/testing/btest/Baseline/language.record-bad-ctor3/out b/testing/btest/Baseline/language.record-bad-ctor3/out index 4903963619..a8c5997f8f 100644 --- a/testing/btest/Baseline/language.record-bad-ctor3/out +++ b/testing/btest/Baseline/language.record-bad-ctor3/out @@ -1,2 +1,2 @@ ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. -error in <...>/record-bad-ctor3.zeek, line 13: bad type in record constructor ([$x=a + 5, a + 9] and a + 9) +error in <...>/record-bad-ctor3.zeek, line 13: syntax error, at or near "a" diff --git a/testing/btest/language/bogus-field-assign.zeek b/testing/btest/language/bogus-field-assign.zeek new file mode 100644 index 0000000000..c29e6e6dde --- /dev/null +++ b/testing/btest/language/bogus-field-assign.zeek @@ -0,0 +1,6 @@ +# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +# Zeek used to happily print the value of "5" here. + +print $foo=5;