parser.yy 17.7 KB
Newer Older
1
/* Copyright (C) 2015-2017 Internet Systems Consortium, Inc. ("ISC")
2

3 4 5
   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0. If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6

7
%skeleton "lalr1.cc" /* -*- C++ -*- */
8
%require "3.0.0"
9
%defines
10
%define parser_class_name {EvalParser}
Francis Dupont's avatar
Francis Dupont committed
11
%define api.prefix {eval}
12 13
%define api.token.constructor
%define api.value.type variant
14
%define api.namespace {isc::eval}
15 16 17
%define parse.assert
%code requires
{
18 19
#include <string>
#include <eval/token.h>
20
#include <eval/eval_context_decl.h>
21
#include <dhcp/option.h>
22
#include <boost/lexical_cast.hpp>
23 24

using namespace isc::dhcp;
25
using namespace isc::eval;
26 27
}
// The parsing context.
28
%param { EvalContext& ctx }
29 30 31 32 33 34 35
%locations
%define parse.trace
%define parse.error verbose
%code
{
# include "eval_context.h"
}
36

37
%define api.token.prefix {TOKEN_}
38
// Tokens in an order which makes sense and related to the intended use.
39 40
%token
  END  0  "end of file"
41 42
  LPAREN  "("
  RPAREN  ")"
43
  NOT "not"
44 45
  AND "and"
  OR "or"
46 47
  EQUAL "=="
  OPTION "option"
48
  RELAY4 "relay4"
49
  RELAY6 "relay6"
Francis Dupont's avatar
Francis Dupont committed
50 51
  PEERADDR "peeraddr"
  LINKADDR "linkaddr"
52 53 54 55
  LBRACKET "["
  RBRACKET "]"
  DOT "."
  TEXT "text"
56
  HEX "hex"
57
  EXISTS "exists"
58 59 60 61 62
  PKT "pkt"
  IFACE "iface"
  SRC "src"
  DST "dst"
  LEN "len"
63 64 65 66 67 68 69 70
  PKT4 "pkt4"
  CHADDR "mac"
  HLEN "hlen"
  HTYPE "htype"
  CIADDR "ciaddr"
  GIADDR "giaddr"
  YIADDR "yiaddr"
  SIADDR "siaddr"
71
  SUBSTRING "substring"
72
  ALL "all"
73
  COMA ","
74
  CONCAT "concat"
75 76 77
  PKT6 "pkt6"
  MSGTYPE "msgtype"
  TRANSID "transid"
78 79 80 81 82
  VENDOR_CLASS "vendor-class"
  VENDOR "vendor"
  ANY "*"
  DATA "data"
  ENTERPRISE "enterprise"
83
;
84

85
%token <std::string> STRING "constant string"
86
%token <std::string> INTEGER "integer"
87
%token <std::string> HEXSTRING "constant hexstring"
88
%token <std::string> OPTION_NAME "option name"
Francis Dupont's avatar
Francis Dupont committed
89
%token <std::string> IP_ADDRESS "ip address"
90

Francis Dupont's avatar
Francis Dupont committed
91
%type <uint16_t> option_code
92
%type <uint32_t> enterprise_id
93
%type <uint32_t> integer_expr
Francis Dupont's avatar
Francis Dupont committed
94
%type <TokenOption::RepresentationType> option_repr_type
95
%type <TokenRelay6Field::FieldType> relay6_field
96
%type <uint8_t> nest_level
97
%type <TokenPkt::MetadataType> pkt_metadata
98
%type <TokenPkt4::FieldType> pkt4_field
99
%type <TokenPkt6::FieldType> pkt6_field
Francis Dupont's avatar
Francis Dupont committed
100

101 102 103 104
%left OR
%left AND
%precedence NOT

105
%printer { yyoutput << $$; } <*>;
106

107 108
%%

109 110
// The whole grammar starts with an expression.
%start expression;
111

112
// Expression can either be a single token or a (something == something) expression
113

114 115
expression : bool_expr
           ;
116

Francis Dupont's avatar
Francis Dupont committed
117
bool_expr : "(" bool_expr ")"
Francis Dupont's avatar
Francis Dupont committed
118 119 120 121 122
          | NOT bool_expr
                {
                    TokenPtr neg(new TokenNot());
                    ctx.expression.push_back(neg);
                }
123 124 125 126 127 128 129 130 131 132
          | bool_expr AND bool_expr
                {
                    TokenPtr neg(new TokenAnd());
                    ctx.expression.push_back(neg);
                }
          | bool_expr OR bool_expr
                {
                    TokenPtr neg(new TokenOr());
                    ctx.expression.push_back(neg);
                }
Francis Dupont's avatar
Francis Dupont committed
133
          | string_expr EQUAL string_expr
134 135 136 137
                {
                    TokenPtr eq(new TokenEqual());
                    ctx.expression.push_back(eq);
                }
138 139 140 141 142
          | OPTION "[" option_code "]" "." EXISTS
                {
                    TokenPtr opt(new TokenOption($3, TokenOption::EXISTS));
                    ctx.expression.push_back(opt);
                }
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
          | RELAY4 "[" option_code "]" "." EXISTS
                {
                   switch (ctx.getUniverse()) {
                   case Option::V4:
                   {
                       TokenPtr opt(new TokenRelay4Option($3, TokenOption::EXISTS));
                       ctx.expression.push_back(opt);
                       break;
                   }
                   case Option::V6:
                       // We will have relay6[123] for the DHCPv6.
                       // In a very distant future we'll possibly be able
                       // to mix both if we have DHCPv4-over-DHCPv6, so it
                       // has some sense to make it explicit whether we
                       // talk about DHCPv4 relay or DHCPv6 relay. However,
                       // for the time being relay4 can be used in DHCPv4
                       // only.
                       error(@1, "relay4 can only be used in DHCPv4.");
                   }
                }
163 164 165 166 167 168 169 170 171 172 173 174 175 176
          | RELAY6 "[" nest_level "]" "." OPTION "[" option_code "]" "." EXISTS
                {
                    switch (ctx.getUniverse()) {
                    case Option::V6:
                    {
                        TokenPtr opt(new TokenRelay6Option($3, $8, TokenOption::EXISTS));
                        ctx.expression.push_back(opt);
                        break;
                    }
                    case Option::V4:
                        // For now we only use relay6 in DHCPv6.
                        error(@1, "relay6 can only be used in DHCPv6.");
                    }
                }
Tomek Mrugalski's avatar
Tomek Mrugalski committed
177
          | VENDOR_CLASS "[" enterprise_id "]" "." EXISTS
Francis Dupont's avatar
Francis Dupont committed
178 179 180 181 182 183 184 185
              {
                  // Expression: vendor-class[1234].exists
                  //
                  // This token will find option 124 (DHCPv4) or 16 (DHCPv6),
                  // and will check if enterprise-id equals specified value.
                  TokenPtr exist(new TokenVendorClass(ctx.getUniverse(), $3, TokenOption::EXISTS));
                  ctx.expression.push_back(exist);
              }
Tomek Mrugalski's avatar
Tomek Mrugalski committed
186
          | VENDOR "[" enterprise_id "]" "." EXISTS
Francis Dupont's avatar
Francis Dupont committed
187 188 189 190 191 192 193 194
              {
                  // Expression: vendor[1234].exists
                  //
                  // This token will find option 125 (DHCPv4) or 17 (DHCPv6),
                  // and will check if enterprise-id equals specified value.
                  TokenPtr exist(new TokenVendor(ctx.getUniverse(), $3, TokenOption::EXISTS));
                  ctx.expression.push_back(exist);
              }
Tomek Mrugalski's avatar
Tomek Mrugalski committed
195
          | VENDOR "[" enterprise_id "]" "." OPTION "[" option_code "]" "." EXISTS
Francis Dupont's avatar
Francis Dupont committed
196 197 198 199 200 201 202 203 204
              {
                  // Expression vendor[1234].option[123].exists
                  //
                  // This token will check if specified vendor option
                  // exists, has specified enterprise-id and if has
                  // specified suboption.
                  TokenPtr exist(new TokenVendor(ctx.getUniverse(), $3, TokenOption::EXISTS, $8));
                  ctx.expression.push_back(exist);
               }
205
          ;
206

207 208 209 210 211 212 213 214 215 216
string_expr : STRING
                  {
                      TokenPtr str(new TokenString($1));
                      ctx.expression.push_back(str);
                  }
            | HEXSTRING
                  {
                      TokenPtr hex(new TokenHexString($1));
                      ctx.expression.push_back(hex);
                  }
Francis Dupont's avatar
Francis Dupont committed
217 218 219 220 221
            | IP_ADDRESS
                  {
                      TokenPtr ip(new TokenIpAddress($1));
                      ctx.expression.push_back(ip);
                  }
Francis Dupont's avatar
Francis Dupont committed
222
            | OPTION "[" option_code "]" "." option_repr_type
223
                  {
Francis Dupont's avatar
Francis Dupont committed
224
                      TokenPtr opt(new TokenOption($3, $6));
225
                      ctx.expression.push_back(opt);
226
                  }
227
            | RELAY4 "[" option_code "]" "." option_repr_type
228 229 230 231 232 233 234 235 236
                  {
                     switch (ctx.getUniverse()) {
                     case Option::V4:
                     {
                         TokenPtr opt(new TokenRelay4Option($3, $6));
                         ctx.expression.push_back(opt);
                         break;
                     }
                     case Option::V6:
237 238 239 240 241 242 243 244
                         // We will have relay6[123] for the DHCPv6.
                         // In a very distant future we'll possibly be able
                         // to mix both if we have DHCPv4-over-DHCPv6, so it
                         // has some sense to make it explicit whether we
                         // talk about DHCPv4 relay or DHCPv6 relay. However,
                         // for the time being relay4 can be used in DHCPv4
                         // only.
                         error(@1, "relay4 can only be used in DHCPv4.");
245 246
                     }
                  }
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

            | RELAY6 "[" nest_level "]" "." OPTION "[" option_code "]" "." option_repr_type
                  {
                     switch (ctx.getUniverse()) {
                     case Option::V6:
                     {
                         TokenPtr opt(new TokenRelay6Option($3, $8, $11));
                         ctx.expression.push_back(opt);
                         break;
                     }
                     case Option::V4:
                         // For now we only use relay6 in DHCPv6.
                         error(@1, "relay6 can only be used in DHCPv6.");
                     }
                  }

263 264 265 266 267
            | PKT "." pkt_metadata
                  {
                      TokenPtr pkt_metadata(new TokenPkt($3));
                      ctx.expression.push_back(pkt_metadata);
                  }
268 269
            | PKT4 "." pkt4_field
                  {
270 271 272 273 274 275 276 277 278 279 280
                     switch (ctx.getUniverse()) {
                     case Option::V4:
                     {
                         TokenPtr pkt4_field(new TokenPkt4($3));
                         ctx.expression.push_back(pkt4_field);
                         break;
                     }
                     case Option::V6:
                         // For now we only use pkt4 in DHCPv4.
                         error(@1, "pkt4 can only be used in DHCPv4.");
                     }
281 282 283
                  }
            | PKT6 "." pkt6_field
                  {
284 285 286 287 288 289 290 291 292 293 294
                     switch (ctx.getUniverse()) {
                     case Option::V6:
                     {
                         TokenPtr pkt6_field(new TokenPkt6($3));
                         ctx.expression.push_back(pkt6_field);
                         break;
                     }
                     case Option::V4:
                         // For now we only use pkt6 in DHCPv6.
                         error(@1, "pkt6 can only be used in DHCPv6.");
                     }
295
                  }
296 297 298 299 300
            | RELAY6 "[" nest_level "]" "." relay6_field
                  {
                     switch (ctx.getUniverse()) {
                     case Option::V6:
                     {
301
                         TokenPtr relay6field(new TokenRelay6Field($3, $6));
302 303 304 305 306 307 308 309 310
                         ctx.expression.push_back(relay6field);
                         break;
                     }
                     case Option::V4:
                         // For now we only use relay6 in DHCPv6.
                         error(@1, "relay6 can only be used in DHCPv6.");
                     }
                  }

311 312 313 314 315
            | SUBSTRING "(" string_expr "," start_expr "," length_expr ")"
                  {
                      TokenPtr sub(new TokenSubstring());
                      ctx.expression.push_back(sub);
                  }
316 317 318 319 320
            | CONCAT "(" string_expr "," string_expr ")"
                  {
                      TokenPtr conc(new TokenConcat());
                      ctx.expression.push_back(conc);
                  }
Francis Dupont's avatar
Francis Dupont committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            | VENDOR "." ENTERPRISE
                {
                    // expression: vendor.enterprise
                    //
                    // This token will return enterprise-id number of
                    // received vendor option.
                    TokenPtr vendor(new TokenVendor(ctx.getUniverse(), 0, TokenVendor::ENTERPRISE_ID));
                    ctx.expression.push_back(vendor);
                }
            | VENDOR_CLASS "." ENTERPRISE
                {
                    // expression: vendor-class.enterprise
                    //
                    // This token will return enterprise-id number of
                    // received vendor class option.
                    TokenPtr vendor(new TokenVendorClass(ctx.getUniverse(), 0,
                                                         TokenVendor::ENTERPRISE_ID));
                    ctx.expression.push_back(vendor);
                }
            | VENDOR "[" enterprise_id "]" "." OPTION "[" option_code "]" "." option_repr_type
                {
                    // This token will search for vendor option with
                    // specified enterprise-id.  If found, will search
                    // for specified suboption and finally will return
                    // its content.
                    TokenPtr opt(new TokenVendor(ctx.getUniverse(), $3, $11, $8));
                    ctx.expression.push_back(opt);
                }
            | VENDOR_CLASS "[" enterprise_id "]" "." DATA
                {
                    // expression: vendor-class[1234].data
                    //
                    // Vendor class option does not have suboptions,
                    // but chunks of data (typically 1, but the option
                    // structure allows multiple of them). If chunk
                    // offset is not specified, we assume the first (0th)
                    // is requested.
                    TokenPtr vendor_class(new TokenVendorClass(ctx.getUniverse(), $3,
                                                               TokenVendor::DATA, 0));
                    ctx.expression.push_back(vendor_class);
                }
            | VENDOR_CLASS "[" enterprise_id "]" "." DATA "[" INTEGER "]"
                {
                    // expression: vendor-class[1234].data[5]
                    //
                    // Vendor class option does not have suboptions,
                    // but chunks of data (typically 1, but the option
                    // structure allows multiple of them). This syntax
                    // specifies which data chunk (tuple) we want.
                    uint8_t index = ctx.convertUint8($8, @8);
                    TokenPtr vendor_class(new TokenVendorClass(ctx.getUniverse(), $3,
                                                               TokenVendor::DATA, index));
                    ctx.expression.push_back(vendor_class);
                }
375
            | integer_expr
376 377 378 379 380 381
                {
                    TokenPtr integer(new TokenInteger($1));
                    ctx.expression.push_back(integer);
                }
            ;

382 383 384 385 386
integer_expr : INTEGER
                 {
                     $$ = ctx.convertUint32($1, @1);
                 }
             ;
387

Francis Dupont's avatar
Francis Dupont committed
388 389
option_code : INTEGER
                 {
390
                     $$ = ctx.convertOptionCode($1, @1);
Francis Dupont's avatar
Francis Dupont committed
391 392 393
                 }
            | OPTION_NAME
                 {
394
                     $$ = ctx.convertOptionName($1, @1);
Francis Dupont's avatar
Francis Dupont committed
395 396 397 398 399 400 401 402 403 404 405 406 407
                 }
            ;

option_repr_type : TEXT
                      {
                          $$ = TokenOption::TEXTUAL;
                      }
                 | HEX
                      {
                          $$ = TokenOption::HEXADECIMAL;
                      }
                 ;

408 409 410 411 412 413 414 415 416
nest_level : INTEGER
                 {
                     $$ = ctx.convertNestLevelNumber($1, @1);
                 }
                 // Eventually we may add strings to handle different
                 // ways of choosing from which relay we want to extract
                 // an option or field.
           ;

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
pkt_metadata : IFACE
                  {
                      $$ = TokenPkt::IFACE;
                  }
             | SRC
                  {
                      $$ = TokenPkt::SRC;
                  }
             | DST
                  {
                      $$ = TokenPkt::DST;
                  }
             | LEN
                  {
                      $$ = TokenPkt::LEN;
                  }
             ;

435
enterprise_id : INTEGER
Francis Dupont's avatar
Francis Dupont committed
436 437 438 439 440 441 442 443
                   {
                       $$ = ctx.convertUint32($1, @1);
                   }
              | "*"
                   {
                       $$ = 0;
                   }
              ;
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
pkt4_field : CHADDR
                {
                    $$ = TokenPkt4::CHADDR;
                }
           | HLEN
                {
                    $$ = TokenPkt4::HLEN;
                }
           | HTYPE
                {
                    $$ = TokenPkt4::HTYPE;
                }
           | CIADDR
                {
                    $$ = TokenPkt4::CIADDR;
                }
           | GIADDR
                {
                    $$ = TokenPkt4::GIADDR;
                }
           | YIADDR
                {
                    $$ = TokenPkt4::YIADDR;
                }
           | SIADDR
                {
                    $$ = TokenPkt4::SIADDR;
                }
473 474 475 476 477 478 479 480
           | MSGTYPE
                 {
                    $$ = TokenPkt4::MSGTYPE;
                 }
           | TRANSID
                 {
                    $$ = TokenPkt4::TRANSID;
                 }
481 482
           ;

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
pkt6_field : MSGTYPE
                 {
                     $$ = TokenPkt6::MSGTYPE;
                 }
           | TRANSID
                 {
                     $$ = TokenPkt6::TRANSID;
                 }
           ;

relay6_field : PEERADDR
                   {
                       $$ = TokenRelay6Field::PEERADDR;
                   }
             | LINKADDR
                   {
                       $$ = TokenRelay6Field::LINKADDR;
                   }
             ;

503
start_expr : INTEGER
Francis Dupont's avatar
Francis Dupont committed
504 505 506 507 508 509 510
                {
                    TokenPtr str(new TokenString($1));
                    ctx.expression.push_back(str);
                }
           ;

length_expr : INTEGER
511 512 513 514 515 516 517 518 519 520
                 {
                     TokenPtr str(new TokenString($1));
                     ctx.expression.push_back(str);
                 }
            | ALL
                 {
                     TokenPtr str(new TokenString("all"));
                     ctx.expression.push_back(str);
                 }
            ;
521

522 523
%%
void
524 525
isc::eval::EvalParser::error(const location_type& loc,
                             const std::string& what)
526
{
527
    ctx.error(loc, what);
528
}