Skip to content

Commit

Permalink
appmodel: add @line for generated content
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed May 24, 2024
1 parent e9f8b43 commit d912abb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
67 changes: 46 additions & 21 deletions modules/appmodel/app-parser-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,58 @@ app_parser_generator_parse_arguments(AppObjectGenerator *s, CfgArgs *args, const
}

static void
_generate_filter(AppParserGenerator *self, const gchar *filter_expr)
_generate_at_line(GString *block, const CFG_LTYPE *lloc)
{
if (filter_expr)
g_string_append_printf(self->block,
" filter {\n"
" %s\n"
" };\n", filter_expr);
if (lloc->first_column && lloc->first_line)
g_string_append_printf(block,
"@line \"%s\" %d %d\n",
lloc->name, lloc->first_line, lloc->first_column);

}

static void
_generate_parser(AppParserGenerator *self, const gchar *parser_expr)
_generate_filter(AppParserGenerator *self, const Application *app)
{
if (parser_expr)
g_string_append_printf(self->block,
" parser {\n"
" %s\n"
" };\n", parser_expr);
if (app->filter_expr)
{
g_string_append(self->block,
" filter {\n");
_generate_at_line(self->block, &app->filter_lloc);
g_string_append_printf(self->block,
"%s\n"
"@line\n"
" };\n", app->filter_expr);
}
}

static void
_generate_filterx(AppParserGenerator *self, const gchar *filterx_expr)
_generate_parser(AppParserGenerator *self, const Application *app)
{
if (filterx_expr)
g_string_append_printf(self->block,
" filterx {\n"
" %s\n"
" };\n", filterx_expr);
if (app->parser_expr)
{
g_string_append(self->block,
" parser {\n");
_generate_at_line(self->block, &app->parser_lloc);
g_string_append_printf(self->block,
"%s\n"
"@line\n"
" };\n", app->parser_expr);
}
}

static void
_generate_filterx(AppParserGenerator *self, const Application *app)
{
if (app->filterx_expr)
{
g_string_append(self->block,
" filterx {\n");
_generate_at_line(self->block, &app->filterx_lloc);
g_string_append_printf(self->block,
"%s\n"
"@line\n"
" };\n", app->filterx_expr);
}
}

static void
Expand Down Expand Up @@ -163,9 +188,9 @@ _generate_application(Application *app, gpointer user_data)
g_string_append_printf(self->block,
" #Start Application %s\n", app->super.name);

_generate_filter(self, app->filter_expr);
_generate_parser(self, app->parser_expr);
_generate_filterx(self, app->filterx_expr);
_generate_filter(self, app);
_generate_parser(self, app);
_generate_filterx(self, app);
_generate_action(self, app);
g_string_append_printf(self->block,
" #End Application %s\n", app->super.name);
Expand Down
12 changes: 9 additions & 3 deletions modules/appmodel/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,30 @@
#include "application.h"

void
application_set_filter(Application *self, const gchar *filter_expr)
application_set_filter(Application *self, const gchar *filter_expr, CFG_LTYPE *lloc)
{
g_free(self->filter_expr);
self->filter_expr = g_strdup(filter_expr);
if (lloc)
self->filter_lloc = *lloc;
}

void
application_set_parser(Application *self, const gchar *parser_expr)
application_set_parser(Application *self, const gchar *parser_expr, CFG_LTYPE *lloc)
{
g_free(self->parser_expr);
self->parser_expr = g_strdup(parser_expr);
if (lloc)
self->parser_lloc = *lloc;
}

void
application_set_filterx(Application *self, const gchar *filterx_expr)
application_set_filterx(Application *self, const gchar *filterx_expr, CFG_LTYPE *lloc)
{
g_free(self->filterx_expr);
self->filterx_expr = g_strdup(filterx_expr);
if (lloc)
self->filterx_lloc = *lloc;
}

static void
Expand Down
10 changes: 7 additions & 3 deletions modules/appmodel/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define APPMODEL_APPLICATION_H_INCLUDED

#include "appmodel-context.h"
#include "cfg-lexer.h"

#define APPLICATION_TYPE_NAME "application"

Expand All @@ -34,11 +35,14 @@ typedef struct _Application
gchar *filter_expr;
gchar *parser_expr;
gchar *filterx_expr;
CFG_LTYPE filter_lloc;
CFG_LTYPE parser_lloc;
CFG_LTYPE filterx_lloc;
} Application;

void application_set_filter(Application *self, const gchar *filter_expr);
void application_set_parser(Application *self, const gchar *parser_expr);
void application_set_filterx(Application *self, const gchar *parser_expr);
void application_set_filter(Application *self, const gchar *filter_expr, CFG_LTYPE *lloc);
void application_set_parser(Application *self, const gchar *parser_expr, CFG_LTYPE *lloc);
void application_set_filterx(Application *self, const gchar *parser_expr, CFG_LTYPE *lloc);

Application *application_new(const gchar *name, const gchar *topic);

Expand Down
6 changes: 3 additions & 3 deletions modules/appmodel/appmodel-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ application_options
;

application_option
: KW_FILTER _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_filter($<ptr>0, $3); free($3); }
| KW_PARSER _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_parser($<ptr>0, $3); free($3); }
| KW_FILTERX _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_filterx($<ptr>0, $3); free($3); }
: KW_FILTER _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_filter($<ptr>0, $3, &@3); free($3); }
| KW_PARSER _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_parser($<ptr>0, $3, &@3); free($3); }
| KW_FILTERX _block_content_context_push LL_BLOCK _block_content_context_pop { application_set_filterx($<ptr>0, $3, &@3); free($3); }
;


Expand Down
8 changes: 4 additions & 4 deletions modules/appmodel/tests/test_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Test(application, filter_can_be_set_and_queried)
const gchar *filter_expr2 = "'2' eq '2'";

app = application_new("foobar", "*");
application_set_filter(app, filter_expr);
application_set_filter(app, filter_expr, NULL);
cr_assert_str_eq(app->filter_expr, filter_expr);

application_set_filter(app, filter_expr2);
application_set_filter(app, filter_expr2, NULL);
cr_assert_str_eq(app->filter_expr, filter_expr2);
appmodel_object_free(&app->super);
}
Expand All @@ -55,10 +55,10 @@ Test(application, parser_can_be_set_and_queried)
const gchar *parser_expr2 = "csv-parser();";

app = application_new("foobar", "*");
application_set_parser(app, parser_expr);
application_set_parser(app, parser_expr, NULL);
cr_assert_str_eq(app->parser_expr, parser_expr);

application_set_parser(app, parser_expr2);
application_set_parser(app, parser_expr2, NULL);
cr_assert_str_eq(app->parser_expr, parser_expr2);
appmodel_object_free(&app->super);
}

0 comments on commit d912abb

Please sign in to comment.