Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filterx: generator fillable pre-check #237

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/filterx/expr-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/

#include "filterx/expr-generator.h"
#include "filterx/object-dict-interface.h"
#include "filterx/object-list-interface.h"
#include "filterx/filterx-eval.h"

/* Takes reference of fillable */
void
Expand All @@ -41,6 +44,21 @@ _eval(FilterXExpr *s)
if (!fillable)
return NULL;

if (fillable->readonly)
{
filterx_eval_push_error("cannot fill object: object is readonly", self, fillable);
filterx_object_unref(fillable);
return NULL;
}

if (!filterx_object_is_type(fillable, &FILTERX_TYPE_NAME(dict)) &&
!filterx_object_is_type(fillable, &FILTERX_TYPE_NAME(list)))
{
filterx_eval_push_error("cannot fill object: dict or list is expected", self, fillable);
filterx_object_unref(fillable);
return NULL;
}

if (self->generate(self, fillable))
return fillable;

Expand Down
32 changes: 28 additions & 4 deletions lib/filterx/filterx-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ generator_assignment
: expr '.' LL_IDENTIFIER KW_ASSIGN expr_generator
{
filterx_generator_set_fillable($5, filterx_getattr_new(filterx_expr_ref($1), $3));
// filterx_expr_set_location($5, lexer, &@1);

$$ = filterx_compound_expr_new_va(TRUE,
filterx_setattr_new($1, $3, filterx_generator_create_container_new($5, $1)),
Expand All @@ -222,6 +223,7 @@ generator_assignment
| expr '[' expr ']' KW_ASSIGN expr_generator
{
filterx_generator_set_fillable($6, filterx_get_subscript_new(filterx_expr_ref($1), filterx_expr_ref($3)));
// filterx_expr_set_location($6, lexer, &@1);

$$ = filterx_compound_expr_new_va(TRUE,
filterx_set_subscript_new($1, $3, filterx_generator_create_container_new($6, $1)),
Expand All @@ -233,6 +235,7 @@ generator_assignment
{
FilterXExpr *minus_one = filterx_literal_new(filterx_config_freeze_object(configuration, filterx_integer_new(-1)));
filterx_generator_set_fillable($5, filterx_get_subscript_new(filterx_expr_ref($1), minus_one));
// filterx_expr_set_location($5, lexer, &@1);

$$ = filterx_compound_expr_new_va(TRUE,
filterx_set_subscript_new($1, NULL, filterx_generator_create_container_new($5, $1)),
Expand All @@ -245,9 +248,25 @@ generator_assignment
;

generator_plus_assignment
: variable KW_PLUS_ASSIGN expr_generator { $$ = $3; filterx_generator_set_fillable($3, $1); }
| expr '[' expr ']' KW_PLUS_ASSIGN expr_generator { $$ = $6; filterx_generator_set_fillable($6, filterx_get_subscript_new($1, $3)); }
| expr '.' LL_IDENTIFIER KW_PLUS_ASSIGN expr_generator { $$ = $5; filterx_generator_set_fillable($5, filterx_getattr_new($1, $3)); free($3);}
: variable KW_PLUS_ASSIGN expr_generator
{
filterx_generator_set_fillable($3, $1);
// filterx_expr_set_location($3, lexer, &@1);
$$ = $3;
}
| expr '[' expr ']' KW_PLUS_ASSIGN expr_generator
{
filterx_generator_set_fillable($6, filterx_get_subscript_new($1, $3));
// filterx_expr_set_location($6, lexer, &@1);
$$ = $6;
}
| expr '.' LL_IDENTIFIER KW_PLUS_ASSIGN expr_generator
{
filterx_generator_set_fillable($5, filterx_getattr_new($1, $3));
// filterx_expr_set_location($5, lexer, &@1);
free($3);
$$ = $5;
}

generator_casted_assignment
/* TODO extract lvalues */
Expand All @@ -259,8 +278,12 @@ generator_casted_assignment

filterx_generator_set_fillable($5, filterx_expr_ref($1));

FilterXExpr *assign = filterx_assign_new($1, func);
assign->suppress_from_trace = TRUE;
((FilterXExpr *) $5)->suppress_from_trace = TRUE;

$$ = filterx_compound_expr_new_va(TRUE,
filterx_assign_new($1, func),
assign,
$5,
NULL
);
Expand Down Expand Up @@ -358,6 +381,7 @@ expr_value
expr_generator
: expr_generator_unchecked {
$$ = $1;
filterx_expr_set_location($1, lexer, &@1);
CHECK_ERROR($1, @1, "error initializing generator expression");
}
;
Expand Down
Loading