Skip to content

Commit

Permalink
Merge pull request #225 from bshifter/filterx-expr-plus2
Browse files Browse the repository at this point in the history
Filterx expr plus operator for generators and += operator (part 2)
  • Loading branch information
alltilla authored Aug 5, 2024
2 parents 9b00d52 + a9c69e1 commit c5a0330
Show file tree
Hide file tree
Showing 11 changed files with 610 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(FILTERX_HEADERS
filterx/func-flatten.h
filterx/expr-plus.h
filterx/expr-null-coalesce.h
filterx/expr-plus-generator.h
PARENT_SCOPE
)

Expand Down Expand Up @@ -94,6 +95,7 @@ set(FILTERX_SOURCES
filterx/expr-plus.c
filterx/filterx-private.c
filterx/expr-null-coalesce.c
filterx/expr-plus-generator.c
PARENT_SCOPE
)

Expand Down
4 changes: 3 additions & 1 deletion lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ filterxinclude_HEADERS = \
lib/filterx/func-str-transform.h \
lib/filterx/func-flatten.h \
lib/filterx/filterx-private.h \
lib/filterx/expr-null-coalesce.h
lib/filterx/expr-null-coalesce.h \
lib/filterx/expr-plus-generator.h


filterx_sources = \
Expand Down Expand Up @@ -95,6 +96,7 @@ filterx_sources = \
lib/filterx/func-flatten.c \
lib/filterx/filterx-private.c \
lib/filterx/expr-null-coalesce.c \
lib/filterx/expr-plus-generator.c \
lib/filterx/filterx-grammar.y

BUILT_SOURCES += \
Expand Down
6 changes: 6 additions & 0 deletions lib/filterx/expr-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ _eval(FilterXExpr *s)
return NULL;
}

gboolean
filterx_expr_is_generator(FilterXExpr *s)
{
return s->eval == _eval;
}

void
filterx_generator_init_instance(FilterXExpr *s)
{
Expand Down
1 change: 1 addition & 0 deletions lib/filterx/expr-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct FilterXExprGenerator_
void filterx_generator_set_fillable(FilterXExpr *s, FilterXExpr *fillable);
void filterx_generator_init_instance(FilterXExpr *s);
void filterx_generator_free_method(FilterXExpr *s);
gboolean filterx_expr_is_generator(FilterXExpr *s);

FilterXExpr *filterx_generator_create_container_new(FilterXExpr *g, FilterXExpr *fillable_parent);

Expand Down
120 changes: 120 additions & 0 deletions lib/filterx/expr-plus-generator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 shifter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "expr-plus.h"
#include "object-string.h"
#include "filterx-eval.h"
#include "scratch-buffers.h"
#include "expr-generator.h"
#include "object-list-interface.h"
#include "object-dict-interface.h"

typedef struct FilterXOperatorPlusGenerator
{
FilterXExprGenerator super;
FilterXExpr *lhs;
FilterXExpr *rhs;
} FilterXOperatorPlusGenerator;

static gboolean
_generate_obj(FilterXOperatorPlusGenerator *self, FilterXObject *obj, FilterXObject *fillable)
{
if (filterx_object_is_type(fillable, &FILTERX_TYPE_NAME(list)))
return filterx_list_merge(fillable, obj);

if (filterx_object_is_type(fillable, &FILTERX_TYPE_NAME(dict)))
return filterx_dict_merge(fillable, obj);

filterx_eval_push_error("Failed to merge objects, invalid fillable type", &self->super.super, fillable);

return FALSE;
}

static gboolean
_handle_object_or_generator(FilterXOperatorPlusGenerator *self, FilterXExpr *expr, FilterXObject *fillable)
{
FilterXObject *obj = NULL;
if (filterx_expr_is_generator(expr))
{
filterx_generator_set_fillable(expr, filterx_expr_ref(self->super.fillable));
obj = filterx_expr_eval(expr);
filterx_object_unref(obj);
return !!obj;
}

obj = filterx_expr_eval(expr);
if (!obj)
return FALSE;
if (!_generate_obj(self, obj, fillable))
{
filterx_object_unref(obj);
return FALSE;
}

filterx_object_unref(obj);
return TRUE;
}

static gboolean
_expr_plus_generator_generate(FilterXExprGenerator *s, FilterXObject *fillable)
{
FilterXOperatorPlusGenerator *self = (FilterXOperatorPlusGenerator *) s;

if (!_handle_object_or_generator(self, self->lhs, fillable))
return FALSE;
if (!_handle_object_or_generator(self, self->rhs, fillable))
return FALSE;
return TRUE;
}

static FilterXObject *
_expr_plus_generator_create_container(FilterXExprGenerator *s, FilterXExpr *fillable_parent)
{
FilterXOperatorPlusGenerator *self = (FilterXOperatorPlusGenerator *) s;
FilterXExprGenerator *generator = (FilterXExprGenerator *)(filterx_expr_is_generator(
self->rhs) ? self->rhs : self->lhs);
return generator->create_container(generator, fillable_parent);
}

static void
_expr_plus_generator_free(FilterXExpr *s)
{
FilterXOperatorPlusGenerator *self = (FilterXOperatorPlusGenerator *) s;
filterx_expr_unref(self->lhs);
filterx_expr_unref(self->rhs);
filterx_generator_free_method(s);
}

FilterXExpr *
filterx_operator_plus_generator_new(FilterXExpr *lhs, FilterXExpr *rhs)
{
FilterXOperatorPlusGenerator *self = g_new0(FilterXOperatorPlusGenerator, 1);
filterx_generator_init_instance(&self->super.super);
self->lhs = lhs;
self->rhs = rhs;
self->super.generate = _expr_plus_generator_generate;
self->super.super.free_fn = _expr_plus_generator_free;
self->super.create_container = _expr_plus_generator_create_container;

return &self->super.super;
}
31 changes: 31 additions & 0 deletions lib/filterx/expr-plus-generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 shifter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#ifndef FILTERX_EXPR_PLUS_GENERATOR_H_INCLUDED
#define FILTERX_EXPR_PLUS_GENERATOR_H_INCLUDED

#include "filterx-expr.h"

FilterXExpr *filterx_operator_plus_generator_new(FilterXExpr *lhs, FilterXExpr *rhs);

#endif
28 changes: 26 additions & 2 deletions lib/filterx/filterx-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "filterx/expr-regexp.h"
#include "filterx/expr-plus.h"
#include "filterx/expr-null-coalesce.h"
#include "filterx/expr-plus-generator.h"

#include "template/templates.h"

Expand Down Expand Up @@ -111,6 +112,8 @@ construct_template_expr(LogTemplate *template)
%type <node> stmt
%type <node> stmt_expr
%type <node> assignment
%type <node> plus_assignment
%type <node> generator_plus_assignment
%type <node> generator_assignment
%type <node> generator_casted_assignment
%type <node> declaration
Expand Down Expand Up @@ -180,15 +183,29 @@ stmt_expr
| declaration { $$ = $1; }
;

plus_assignment
: variable KW_PLUS_ASSIGN expr { $$ = filterx_assign_new(filterx_expr_ref($1), filterx_operator_plus_new($1, $3)); }
| expr '[' expr ']' KW_PLUS_ASSIGN expr { $$ = filterx_set_subscript_new(filterx_expr_ref($1), filterx_expr_ref($3), filterx_operator_plus_new(filterx_get_subscript_new($1, $3), $6)); }
| expr '.' LL_IDENTIFIER KW_PLUS_ASSIGN expr
{
$$ = filterx_setattr_new(filterx_expr_ref($1), $3, filterx_operator_plus_new(filterx_getattr_new($1, $3), $5));
free($3);
}
;


assignment
/* TODO extract lvalues */
: variable KW_ASSIGN expr { $$ = filterx_assign_new($1, $3); }
| expr '.' LL_IDENTIFIER KW_ASSIGN expr { $$ = filterx_setattr_new($1, $3, $5); free($3); }
| expr '[' expr ']' KW_ASSIGN expr { $$ = filterx_set_subscript_new($1, $3, $6); }
| expr '[' ']' KW_ASSIGN expr { $$ = filterx_set_subscript_new($1, NULL, $5); }
| generator_assignment
| plus_assignment { $$ = $1; }
;



generator_assignment
/* TODO extract lvalues */
: expr '.' LL_IDENTIFIER KW_ASSIGN expr_generator
Expand Down Expand Up @@ -223,10 +240,15 @@ generator_assignment
NULL
);
}
| expr KW_PLUS_ASSIGN expr_generator { $$ = $3; filterx_generator_set_fillable($3, $1); }
| generator_plus_assignment
| generator_casted_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);}

generator_casted_assignment
/* TODO extract lvalues */
: variable KW_ASSIGN LL_IDENTIFIER '(' expr_generator ')'
Expand Down Expand Up @@ -296,7 +318,6 @@ declaration
: KW_DECLARE filterx_variable KW_ASSIGN expr { filterx_variable_expr_declare($2); $$ = filterx_assign_new($2, $4); }
;


expr
: expr_value { $$ = $1; }
| function_call { $$ = $1; }
Expand Down Expand Up @@ -346,6 +367,9 @@ expr_generator_unchecked
| list_generator
| regexp_search
| '(' expr_generator ')' { $$ = $2; }
| expr '+' expr_generator { $$ = filterx_operator_plus_generator_new($1, $3); }
| expr_generator '+' expr { $$ = filterx_operator_plus_generator_new($1, $3); }
| expr_generator '+' expr_generator { $$ = filterx_operator_plus_generator_new($1, $3); }
;

function_call
Expand Down
1 change: 1 addition & 0 deletions lib/filterx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ add_unit_test(LIBTEST CRITERION TARGET test_expr_function DEPENDS json-plugin ${
add_unit_test(LIBTEST CRITERION TARGET test_expr_regexp DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_null_coalesce DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_plus DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_plus_generator DEPENDS json-plugin ${JSONC_LIBRARY})
6 changes: 5 additions & 1 deletion lib/filterx/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ lib_filterx_tests_TESTS = \
lib/filterx/tests/test_func_flatten \
lib/filterx/tests/test_expr_regexp \
lib/filterx/tests/test_expr_null_coalesce \
lib/filterx/tests/test_expr_plus
lib/filterx/tests/test_expr_plus \
lib/filterx/tests/test_expr_plus_generator

EXTRA_DIST += lib/filterx/tests/CMakeLists.txt

Expand Down Expand Up @@ -100,3 +101,6 @@ lib_filterx_tests_test_expr_null_coalesce_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_expr_plus_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_expr_plus_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_expr_plus_generator_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_expr_plus_generator_LDADD = $(TEST_LDADD) $(JSON_LIBS)
Loading

0 comments on commit c5a0330

Please sign in to comment.