Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : %{ 6 : 7 : #define __STDC_LIMIT_MACROS 8 : #define __STDC_FORMAT_MACROS 9 : #include <stdio.h> 10 : #include <inttypes.h> 11 : #include <limits.h> 12 : #include "ruleparserl.h" 13 : #include "ruleutil.h" 14 : #include "t_ruleparser.h" 15 : #include "ruleglob.h" 16 : 17 : /** 18 : * This global variable is used for automatic numbering of field indices etc. 19 : * when parsing the members of a struct. Field values are automatically 20 : * assigned starting from -1 and working their way down. 21 : */ 22 : int y_field_val = -1; 23 : int g_arglist = 0; 24 : const int struct_is_struct = 0; 25 : const int struct_is_union = 1; 26 : 27 : %} 28 : 29 : /** 30 : * This structure is used by the parser to hold the data types associated with 31 : * various parse nodes. 32 : */ 33 : %union { 34 : char symbol; 35 : char* id; 36 : char* dtext; 37 : t_rulelist* trulelist; 38 : t_rule* trule; 39 : t_rulemsgtype* tmsgtype; 40 : char* tmsgtypebase;; 41 : char* tcontext; 42 : t_rulecondlist* tcondlist; 43 : t_cond_base* tcondbase; 44 : t_rangevalue* trangevalue_v; 45 : t_rangevalue_base* trangevalue; 46 : t_ruleactionlist* tactionlist; 47 : t_ruleaction* taction; 48 : t_ruleaction* tparamlist; 49 : } 50 : 51 : /** 52 : * Strings identifier 53 : */ 54 : %token<id> tok_st_identifier 55 : %token<symbol> tok_symbolcmp 56 : 57 : /** 58 : * Constant values 59 : */ 60 : %token<iconst> tok_int_constant 61 : %token<dconst> tok_dub_constant 62 : 63 : /** 64 : * Header keywords 65 : */ 66 : %token tok_rule 67 : %token tok_include 68 : %token tok_for 69 : %token tok_eq 70 : %token tok_and 71 : %token tok_match 72 : %token tok_in 73 : %token tok_action 74 : %token tok_msgtype 75 : %token tok_context 76 : 77 : /** 78 : * Grammar nodes 79 : */ 80 : %type<dtext> CaptureDocText 81 : %type<trulelist> Rulelist 82 : %type<trule> Ruleitembase 83 : %type<trule> Ruleitem 84 : %type<trule> Rule 85 : %type<tmsgtypebase> Rulemsgtypebase 86 : %type<tmsgtype> Rulemsgtype 87 : %type<tcontext> Rulecontextbase 88 : %type<tcontext> Rulecontext 89 : %type<tcondlist> Rulecondlistbase 90 : %type<tcondlist> Rulecondlist 91 : %type<tcondbase> Ruleconditem 92 : %type<trangevalue_v> Rulefieldrange 93 : %type<trangevalue> Rulerangeitem 94 : %type<tactionlist> Ruleactionlist 95 : %type<taction> Ruleaction 96 : %type<taction> Actionparamlist 97 : 98 : %% 99 : 100 : /** 101 : * Rule Grammar Implementation. 102 : * 103 : */ 104 : Rulefile: 105 : Rulelist 106 : { 107 0 : pdebug("Rulefile -> Rulelist"); 108 : } 109 : 110 : Rulelist: 111 : Ruleitembase 112 : { 113 0 : pdebug("Rulelist -> Ruleitembase"); 114 0 : g_rulelist->add_rule($1); 115 0 : $$ = g_rulelist; 116 : } 117 : | Rulelist Ruleitembase 118 : { 119 0 : pdebug("Rulelist -> Rulelist Ruleitembase"); 120 0 : g_rulelist->add_rule($2); 121 0 : $$ = g_rulelist; 122 : } 123 : 124 : CaptureDocText: 125 : { 126 0 : pdebug("CaptureDocText ->"); 127 0 : if (g_parse_mode == PROGRAM) { 128 0 : $$ = g_doctext; 129 0 : g_doctext = NULL; 130 : } else { 131 0 : $$ = NULL; 132 : } 133 : } 134 : 135 : Ruleitembase: 136 : CaptureDocText Ruleitem 137 : { 138 0 : pdebug("Ruleitembase -> CaptureDocText Ruleitem"); 139 0 : pdebug("CaptureDocText is %p, Ruleitem is %p", $1, $2); 140 0 : if ($1 != NULL) { 141 0 : $2->set_doc($1); 142 : } 143 0 : $$ = $2; 144 : } 145 : 146 : Ruleitem: 147 : tok_rule tok_st_identifier ':' Rule 148 : { 149 0 : pdebug("Ruleitem -> tok_rule tok_st_identifier..."); 150 0 : t_rule* ruleptr = $4; 151 : 152 0 : ruleptr->set_name($2); 153 0 : free($2); 154 0 : $$ = $4; 155 : } 156 : 157 : Rule: 158 : tok_for Rulemsgtype Rulecondlistbase Ruleactionlist 159 : { 160 0 : pdebug("Rule -> tok_for Rulemsgtype Rulecondlistbase Ruleactionlist"); 161 0 : t_rule *rule = new t_rule($2, $3, $4); 162 0 : $$ = rule; 163 : } 164 : 165 : Rulemsgtype: 166 : Rulemsgtypebase Rulecontext 167 : { 168 0 : pdebug("Rulemsgtype -> Rulemsgtypebase Rulecontext"); 169 : t_rulemsgtype *msgtype; 170 0 : if ($2 == NULL) { 171 0 : msgtype = new t_rulemsgtype($1); 172 0 : free($1); 173 : } else { 174 0 : msgtype = new t_rulemsgtype($1, $2); 175 0 : free($1); 176 0 : free($2); 177 : } 178 0 : $$ = msgtype; 179 : } 180 : | '(' Rulemsgtype ')' 181 : { 182 0 : pdebug("Rulemsgtype -> ( Rulemsgtype )"); 183 0 : $$ = $2; 184 : } 185 : 186 : Rulemsgtypebase: 187 : tok_msgtype tok_eq tok_st_identifier 188 : { 189 0 : pdebug("Rulemsgtypebase -> tok_msgtype tok_eq tok_st_identifier"); 190 0 : $$ = $3; 191 : } 192 : | '(' Rulemsgtypebase ')' 193 : { 194 0 : pdebug("Rulemsgtypebase -> ( Rulemsgtypebase )"); 195 0 : $$ = $2; 196 : } 197 : 198 : Rulecontext: 199 : { 200 0 : pdebug("Rulecontext -> "); 201 0 : $$ = NULL; 202 : } 203 : | tok_and Rulecontextbase 204 : { 205 0 : pdebug("Rulecontext -> tok_and Rulecontextbase"); 206 0 : $$ = $2; 207 : } 208 : 209 : Rulecontextbase: 210 : tok_context tok_eq tok_st_identifier 211 : { 212 0 : pdebug("Rulecontextbase -> tok_context tok_eq tok_st_identifier"); 213 0 : $$ = $3; 214 : } 215 : | '(' Rulecontextbase ')' 216 : { 217 0 : pdebug("Rulecontextbase -> ( Rulecontextbase )"); 218 0 : $$ = $2; 219 : } 220 : 221 : Rulecondlistbase: 222 : { 223 0 : pdebug("Rulecondlistbase -> "); 224 0 : $$ = NULL; 225 : } 226 : | tok_match Rulecondlist 227 : { 228 0 : pdebug("Rulecondlistbase -> tok_match Rulecondlist"); 229 0 : $$ = $2; 230 : } 231 : 232 : Rulecondlist: 233 : Ruleconditem 234 : { 235 0 : pdebug("Rulecondlist -> Ruleconditem"); 236 0 : t_rulecondlist *condlist = new t_rulecondlist(); 237 : 238 0 : condlist->add_field($1); 239 0 : $$ = condlist; 240 : } 241 : | Ruleconditem tok_and Rulecondlist 242 : { 243 0 : pdebug("Rulecondlist -> Ruleconditem tok_and Rulecondlist"); 244 0 : t_rulecondlist *condlist = $3; 245 0 : condlist->add_field($1); 246 0 : $$ = condlist; 247 : } 248 : 249 : Ruleconditem: 250 : tok_st_identifier tok_symbolcmp tok_st_identifier 251 : { 252 0 : pdebug("Ruleconditem -> tok_st_identifier tok_symbolcmp tok_st_identifier"); 253 : t_cond_simple *cond = 254 0 : new t_cond_simple($1, $2, $3); 255 0 : free($1); 256 0 : free($3); 257 0 : $$ = cond; 258 : } 259 : | tok_st_identifier tok_in '[' Rulefieldrange ']' 260 : { 261 0 : pdebug("Ruleconditem -> tok_st_identifier tok_in [ Rulefieldrange ]"); 262 0 : t_cond_range *cond = new t_cond_range($1, $4); 263 0 : free($1); 264 0 : $$ = cond; 265 : } 266 : | '(' Ruleconditem ')' 267 : { 268 0 : pdebug("Ruleconditem -> ( Ruleconditem ) "); 269 0 : $$ = $2; 270 : } 271 : 272 : Rulefieldrange: 273 : Rulerangeitem 274 : { 275 0 : pdebug("Rulefieldrange -> Rulerangeitem"); 276 0 : t_rangevalue* rangevalue = new t_rangevalue(); 277 : 278 0 : rangevalue->add_rangevalue($1); 279 0 : $$ = rangevalue; 280 : } 281 : | Rulefieldrange ',' Rulerangeitem 282 : { 283 0 : pdebug("Rulefieldrange -> Rulefieldrange , Rulerangeitem"); 284 0 : t_rangevalue* rangevalue = $1; 285 0 : rangevalue->add_rangevalue($3); 286 0 : $$ = rangevalue; 287 : } 288 : 289 : Rulerangeitem: 290 : tok_st_identifier 291 : { 292 0 : pdebug("Rulerangeitem -> tok_st_identifier"); 293 0 : t_rangevalue_s* rangevalue_s = new t_rangevalue_s($1); 294 0 : free($1); 295 0 : $$ = rangevalue_s; 296 : } 297 : | tok_st_identifier '-' tok_st_identifier 298 : { 299 0 : pdebug("Rulerangeitem -> tok_st_identifier - tok_st_identifier"); 300 0 : t_rangevalue_d *rangevalue_d = new t_rangevalue_d($1, $3); 301 0 : free($1); 302 0 : free($3); 303 0 : $$ = rangevalue_d; 304 : } 305 : 306 : Ruleactionlist: 307 : Ruleaction 308 : { 309 0 : pdebug("Ruleactionlist -> Ruleaction"); 310 0 : t_ruleactionlist *actionlist = new t_ruleactionlist(); 311 : 312 0 : actionlist->add_action($1); 313 0 : $$ = actionlist; 314 : } 315 : | Ruleaction Ruleactionlist 316 : { 317 0 : pdebug("Ruleactionlist -> Ruleaction Ruleactionlist"); 318 0 : t_ruleactionlist *actionlist = $2; 319 0 : actionlist->add_action($1); 320 : 321 0 : $$ = actionlist; 322 : } 323 : 324 : Ruleaction: tok_action tok_st_identifier Actionparamlist 325 : { 326 0 : pdebug("Ruleaction -> tok_action tok_st_identifier Actionparamlist"); 327 0 : $3->set_actionid($2); 328 0 : free($2); 329 0 : $$ = $3; 330 : } 331 : 332 : Actionparamlist: 333 : tok_st_identifier 334 : { 335 0 : pdebug("Actionparamlist -> tok_st_identifier"); 336 0 : t_ruleaction *action = new t_ruleaction(); 337 : 338 0 : action->add_actionparam($1); 339 0 : free($1); 340 0 : $$ = action; 341 : } 342 : | Actionparamlist tok_st_identifier 343 : { 344 0 : pdebug("Actionparamlist -> Actionparamlist tok_st_identifier"); 345 0 : $1->add_actionparam($2); 346 0 : free($2); 347 0 : $$ = $1; 348 : } 349 : 350 : %%