From 9fd9baba43c8ee17f944a1157b77a8e47cb9ba41 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Fri, 10 Mar 2017 18:13:49 +0100 Subject: Introduce boolean datatype and boolean expression Signed-off-by: Phil Sutter Signed-off-by: Pablo Neira Ayuso --- src/datatype.c | 19 +++++++++++++++++++ src/parser_bison.y | 20 ++++++++++++++++++++ src/scanner.l | 3 +++ 3 files changed, 42 insertions(+) (limited to 'src') diff --git a/src/datatype.c b/src/datatype.c index 6b1dd4a0..c61c4245 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -48,6 +48,7 @@ static const struct datatype *datatypes[TYPE_MAX + 1] = { [TYPE_ICMP_CODE] = &icmp_code_type, [TYPE_ICMPV6_CODE] = &icmpv6_code_type, [TYPE_ICMPX_CODE] = &icmpx_code_type, + [TYPE_BOOLEAN] = &boolean_type, }; void datatype_register(const struct datatype *dtype) @@ -1104,3 +1105,21 @@ struct error_record *rate_parse(const struct location *loc, const char *str, return NULL; } + +static const struct symbol_table boolean_tbl = { + .base = BASE_DECIMAL, + .symbols = { + SYMBOL("exists", true), + SYMBOL("missing", false), + SYMBOL_LIST_END + }, +}; + +const struct datatype boolean_type = { + .type = TYPE_BOOLEAN, + .name = "boolean", + .desc = "boolean type", + .size = 1, + .basetype = &integer_type, + .sym_tbl = &boolean_tbl, +}; diff --git a/src/parser_bison.y b/src/parser_bison.y index dff8a5ab..f2ae82f4 100644 --- a/src/parser_bison.y +++ b/src/parser_bison.y @@ -448,6 +448,9 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token NOTRACK "notrack" +%token EXISTS "exists" +%token MISSING "missing" + %type identifier type_identifier string comment_spec %destructor { xfree($$); } identifier type_identifier string comment_spec @@ -651,6 +654,10 @@ static void location_update(struct location *loc, struct location *rhs, int n) %type tcp_hdr_field %type tcp_hdr_option_type tcp_hdr_option_field +%type boolean_expr +%destructor { expr_free($$); } boolean_expr +%type boolean_keys + %% input : /* empty */ @@ -2655,8 +2662,21 @@ concat_rhs_expr : basic_rhs_expr } ; +boolean_keys : EXISTS { $$ = true; } + | MISSING { $$ = false; } + ; + +boolean_expr : boolean_keys + { + $$ = constant_expr_alloc(&@$, &boolean_type, + BYTEORDER_HOST_ENDIAN, + 1, &$1); + } + ; + primary_rhs_expr : symbol_expr { $$ = $1; } | integer_expr { $$ = $1; } + | boolean_expr { $$ = $1; } | ETHER { $$ = symbol_expr_alloc(&@$, SYMBOL_VALUE, diff --git a/src/scanner.l b/src/scanner.l index 003f15eb..b0d57198 100644 --- a/src/scanner.l +++ b/src/scanner.l @@ -504,6 +504,9 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "xml" { return XML; } "json" { return JSON; } +"exists" { return EXISTS; } +"missing" { return MISSING; } + {addrstring} { yylval->string = xstrdup(yytext); return STRING; -- cgit v1.2.3