Skip to contentSkip to conversation

parser: add typeof keyword for declarations

RFC PATCH New Patrick McHardy 1 participant 1 message Nov 27, 2015 netfilter

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 parser: add typeof keyword for declarations

Add a typeof keyword to automatically use the correct type in set and map declarations.

table filter {
	set blacklist {
		typeof ip saddr
	}

	chain input {
		ip saddr @blacklist counter drop
	}
}
---
 src/parser_bison.y | 17 +++++++++++++++++
 src/scanner.l      |  1 +
 2 files changed, 18 insertions(+)

Just a small hack since as a distraction from the tracing stuff. So far it only supports primary types, but its still easier than remembering or looking up the exact type names.

Please not that it is just a parser feature. The output will still use the type keyword and the actual data type:

table ip filter {
	set blacklist { 
		type ipv4_addr
	}
	...

Any comments?

src/parser_bison.y +17 -0
index fbfe7ea..cd97424 100644
@@ -163,6 +163,7 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 
 %token INCLUDE			"include"
 %token DEFINE			"define"
+%token TYPEOF			"typeof"
 
 %token HOOK			"hook"
 %token DEVICE			"device"
@@ -968,6 +969,12 @@ set_block		:	/* empty */	{ $$ = $<set>-1; }
 				$1->keytype = $3;
 				$$ = $1;
 			}
+			|	set_block	TYPEOF		primary_expr	stmt_seperator
+			{
+				$1->keytype = $3->dtype;
+				expr_free($3);
+				$$ = $1;
+			}
 			|	set_block	FLAGS		set_flag_list	stmt_seperator
 			{
 				$1->flags = $3;
@@ -1021,6 +1028,16 @@ map_block		:	/* empty */	{ $$ = $<set>-1; }
 				$1->datatype = $5;
 				$$ = $1;
 			}
+			|	map_block	TYPEOF
+						primary_expr	COLON	primary_expr
+						stmt_seperator
+			{
+				$1->keytype  = $3->dtype;
+				$1->datatype = $5->dtype;
+				expr_free($3);
+				expr_free($5);
+				$$ = $1;
+			}
 			|	map_block	FLAGS		set_flag_list	stmt_seperator
 			{
 				$1->flags = $3;
src/scanner.l +1 -0
index a98e7b6..e19b13b 100644
@@ -230,6 +230,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "include"		{ return INCLUDE; }
 "define"		{ return DEFINE; }
+"typeof"		{ return TYPEOF; }
 
 "describe"		{ return DESCRIBE; }
 
Signature · 1 line
2.5.0