dhcp6_lexer.ll 27.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")

   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/. */

%{ /* -*- C++ -*- */
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <string>
#include <dhcp6/parser_context.h>
#include <asiolink/io_address.h>
#include <boost/lexical_cast.hpp>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
15
#include <exceptions/exceptions.h>
16 17 18 19 20 21 22 23

// Work around an incompatibility in flex (at least versions
// 2.5.31 through 2.5.33): it generates code that does
// not conform to C89.  See Debian bug 333231
// <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
# undef yywrap
# define yywrap() 1

Francis Dupont's avatar
Francis Dupont committed
24 25
namespace {

26 27
// The location of the current token. The lexer will keep updating it. This
// variable will be useful for logging errors.
Francis Dupont's avatar
Francis Dupont committed
28 29 30 31 32 33 34
isc::dhcp::location loc;

/// @brief Location stack.
std::vector<isc::dhcp::location> locs;

/// @brief File name.
std::string file;
35

Francis Dupont's avatar
Francis Dupont committed
36 37
/// @brief File name stack.
std::vector<std::string> files;
38

Francis Dupont's avatar
Francis Dupont committed
39 40 41 42 43 44
/// @brief State stack.
std::vector<struct yy_buffer_state*> states;

bool start_token_flag = false;

isc::dhcp::Parser6Context::ParserType start_token_value;
45
unsigned int comment_start_line = 0;
Francis Dupont's avatar
Francis Dupont committed
46 47

};
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
// To avoid the call to exit... oops!
#define YY_FATAL_ERROR(msg) isc::dhcp::Parser6Context::fatal(msg)
%}

/* noyywrap disables automatic rewinding for the next file to parse. Since we
   always parse only a single string, there's no need to do any wraps. And
   using yywrap requires linking with -lfl, which provides the default yywrap
   implementation that always returns 1 anyway. */
%option noyywrap

/* nounput simplifies the lexer, by removing support for putting a character
   back into the input stream. We never use such capability anyway. */
%option nounput

/* batch means that we'll never use the generated lexer interactively. */
%option batch

66 67 68
/* avoid to get static global variables to remain with C++. */
/* in last resort %option reentrant */

69 70 71 72 73 74 75 76 77
/* Enables debug mode. To see the debug messages, one needs to also set
   yy_flex_debug to 1, then the debug messages will be printed on stderr. */
%option debug

/* I have no idea what this option does, except it was specified in the bison
   examples and Postgres folks added it to remove gcc 4.3 warnings. Let's
   be on the safe side and keep it. */
%option noinput

Tomek Mrugalski's avatar
Tomek Mrugalski committed
78
%x COMMENT
Francis Dupont's avatar
Francis Dupont committed
79
%x DIR_ENTER DIR_INCLUDE DIR_EXIT
Tomek Mrugalski's avatar
Tomek Mrugalski committed
80

81 82 83 84 85 86
/* These are not token expressions yet, just convenience expressions that
   can be used during actual token definitions. Note some can match
   incorrect inputs (e.g., IP addresses) which must be checked. */
int   \-?[0-9]+
blank [ \t]

87 88 89 90 91 92
UnicodeEscapeSequence           u[0-9A-Fa-f]{4}
JSONEscapeCharacter             ["\\/bfnrt]
JSONEscapeSequence              {JSONEscapeCharacter}|{UnicodeEscapeSequence}
JSONStandardCharacter           [^\x00-\x1f"\\]
JSONStringCharacter             {JSONStandardCharacter}|\\{JSONEscapeSequence}
JSONString                      \"{JSONStringCharacter}*\"
93

94 95 96 97 98 99
/* for errors */

BadUnicodeEscapeSequence        u[0-9A-Fa-f]{0,3}[^0-9A-Fa-f]
BadJSONEscapeSequence           [^"\\/bfnrtu]|{BadUnicodeEscapeSequence}
ControlCharacter                [\x00-\x1f]
ControlCharacterFill            [^"\\]|\\{JSONEscapeSequence}
100 101 102 103 104 105 106 107 108 109 110

%{
// This code run each time a pattern is matched. It updates the location
// by moving it ahead by yyleng bytes. yyleng specifies the length of the
// currently matched token.
#define YY_USER_ACTION  loc.columns(yyleng);
%}

%%

%{
111 112 113 114
    // This part of the code is copied over to the verbatim to the top
    // of the generated yylex function. Explanation:
    // http://www.gnu.org/software/bison/manual/html_node/Multiple-start_002dsymbols.html

115 116
    // Code run each time yylex is called.
    loc.step();
Tomek Mrugalski's avatar
Tomek Mrugalski committed
117

118 119 120 121 122 123
    if (start_token_flag) {
        start_token_flag = false;
        switch (start_token_value) {
        case Parser6Context::PARSER_DHCP6:
            return isc::dhcp::Dhcp6Parser::make_TOPLEVEL_DHCP6(loc);
        case Parser6Context::PARSER_GENERIC_JSON:
124
        default:
125 126 127
            return isc::dhcp::Dhcp6Parser::make_TOPLEVEL_GENERIC_JSON(loc);
        }
    }
128 129
%}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
130 131 132 133 134 135
#.* ;

"//"(.*) ;

"/*" {
  BEGIN(COMMENT);
136
  comment_start_line = loc.end.line;;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
137 138 139
}

<COMMENT>"*/" BEGIN(INITIAL);
140
<COMMENT>. ;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
141
<COMMENT><<EOF>> {
142
    isc_throw(Dhcp6ParseError, "Comment not closed. (/* in line " << comment_start_line);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
143 144
}

Francis Dupont's avatar
Francis Dupont committed
145 146 147 148 149 150 151 152 153
"<?" BEGIN(DIR_ENTER);
<DIR_ENTER>"include" BEGIN(DIR_INCLUDE);
<DIR_INCLUDE>\"([^\"\n])+\" {
    // Include directive.

    // Extract the filename.
    std::string tmp(yytext+1);
    tmp.resize(tmp.size() - 1);

154
    driver.includeFile(tmp);
Francis Dupont's avatar
Francis Dupont committed
155
}
156
<DIR_ENTER,DIR_INCLUDE,DIR_EXIT><<EOF>> {
157
    isc_throw(Dhcp6ParseError, "Directive not closed.");
158
}
Francis Dupont's avatar
Francis Dupont committed
159 160 161
<DIR_EXIT>"?>" BEGIN(INITIAL);
    

162
<*>{blank}+   {
163 164 165
    // Ok, we found a with space. Let's ignore it and update loc variable.
    loc.step();
}
166

167
<*>[\n]+      {
168 169 170 171 172
    // Newline found. Let's update the location and continue.
    loc.lines(yyleng);
    loc.step();
}

173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
\"Dhcp6\"  {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::CONFIG:
        return isc::dhcp::Dhcp6Parser::make_DHCP6(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("Dhcp6", loc);
    }
}

\"interfaces-config\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return  isc::dhcp::Dhcp6Parser::make_INTERFACES_CONFIG(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("interfaces-config", loc);
    }
}

\"interfaces\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::INTERFACES_CONFIG:
        return  isc::dhcp::Dhcp6Parser::make_INTERFACES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("interfaces", loc);
    }
}

\"lease-database\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_LEASE_DATABASE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("lease-database", loc);
    }
}

\"hosts-database\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_HOSTS_DATABASE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("hosts-database", loc);
    }
}

\"type\" {
    switch(driver.ctx_) {
221 222
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
223 224 225 226 227 228 229 230 231
    case isc::dhcp::Parser6Context::SERVER_ID:
        return isc::dhcp::Dhcp6Parser::make_TYPE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("type", loc);
    }
}

\"user\" {
    switch(driver.ctx_) {
232 233
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
234 235 236 237 238 239 240 241
        return isc::dhcp::Dhcp6Parser::make_USER(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("user", loc);
    }
}

\"password\" {
    switch(driver.ctx_) {
242 243
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
244 245 246 247 248 249 250 251
        return isc::dhcp::Dhcp6Parser::make_PASSWORD(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("password", loc);
    }
}

\"host\" {
    switch(driver.ctx_) {
252 253
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
254 255 256 257 258 259 260 261
        return isc::dhcp::Dhcp6Parser::make_HOST(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("host", loc);
    }
}

\"persist\" {
    switch(driver.ctx_) {
262 263
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
264 265 266 267 268 269 270 271 272
    case isc::dhcp::Parser6Context::SERVER_ID:
        return isc::dhcp::Dhcp6Parser::make_PERSIST(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("persist", loc);
    }
}

\"lfc-interval\" {
    switch(driver.ctx_) {
273 274
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        return isc::dhcp::Dhcp6Parser::make_LFC_INTERVAL(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("lfc-interval", loc);
    }
}

\"preferred-lifetime\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_PREFERRED_LIFETIME(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("preferred-lifetime", loc);
    }
}

\"valid-lifetime\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_VALID_LIFETIME(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("valid-lifetime", loc);
    }
}

\"renew-timer\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_RENEW_TIMER(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("renew-timer", loc);
    }
}

\"rebind-timer\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_REBIND_TIMER(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("rebind-timer", loc);
    }
}

\"subnet6\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_SUBNET6(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("subnet6", loc);
    }
}

\"option-data\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
    case isc::dhcp::Parser6Context::SUBNET6:
    case isc::dhcp::Parser6Context::POOLS:
    case isc::dhcp::Parser6Context::PD_POOLS:
    case isc::dhcp::Parser6Context::RESERVATIONS:
    case isc::dhcp::Parser6Context::CLIENT_CLASSES:
    case isc::dhcp::Parser6Context::CLIENT_CLASS:
        return isc::dhcp::Dhcp6Parser::make_OPTION_DATA(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("option-data", loc);
    }
}

\"name\" {
    switch(driver.ctx_) {
343 344
    case isc::dhcp::Parser6Context::LEASE_DATABASE:
    case isc::dhcp::Parser6Context::HOSTS_DATABASE:
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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    case isc::dhcp::Parser6Context::OPTION_DATA:
    case isc::dhcp::Parser6Context::CLIENT_CLASSES:
    case isc::dhcp::Parser6Context::CLIENT_CLASS:
    case isc::dhcp::Parser6Context::LOGGERS:
        return isc::dhcp::Dhcp6Parser::make_NAME(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("name", loc);
    }
}

\"data\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::OPTION_DATA:
        return isc::dhcp::Dhcp6Parser::make_DATA(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("data", loc);
    }
}

\"pools\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
        return isc::dhcp::Dhcp6Parser::make_POOLS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("pools", loc);
    }
}

\"pd-pools\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
        return isc::dhcp::Dhcp6Parser::make_PD_POOLS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("pd-pools", loc);
    }
}

\"prefix\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::PD_POOLS:
        return isc::dhcp::Dhcp6Parser::make_PREFIX(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("prefix", loc);
    }
}

\"prefix-len\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::PD_POOLS:
        return isc::dhcp::Dhcp6Parser::make_PREFIX_LEN(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("prefix-len", loc);
    }
}

\"delegated-len\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::PD_POOLS:
        return isc::dhcp::Dhcp6Parser::make_DELEGATED_LEN(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("delegated-len", loc);
    }
}

\"pool\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::POOLS:
        return isc::dhcp::Dhcp6Parser::make_POOL(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("pool", loc);
    }
}

\"subnet\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
        return isc::dhcp::Dhcp6Parser::make_SUBNET(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("subnet", loc);
    }
}

\"interface\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
        return isc::dhcp::Dhcp6Parser::make_INTERFACE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("interface", loc);
    }
}

\"id\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
        return isc::dhcp::Dhcp6Parser::make_ID(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("id", loc);
    }
}

\"code\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::OPTION_DATA:
        return isc::dhcp::Dhcp6Parser::make_CODE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("code", loc);
    }
}

\"mac-sources\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_MAC_SOURCES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("mac-sources", loc);
    }
}

\"relay-supplied-options\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_RELAY_SUPPLIED_OPTIONS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("relay-supplied-options", loc);
    }
}

\"host-reservation-identifiers\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_HOST_RESERVATION_IDENTIFIERS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("host-reservation-identifiers", loc);
    }
}

\"Logging\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::CONFIG:
        return isc::dhcp::Dhcp6Parser::make_LOGGING(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("Logging", loc);
    }
}

\"loggers\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::LOGGING:
        return isc::dhcp::Dhcp6Parser::make_LOGGERS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("loggers", loc);
    }
}

\"output_options\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::LOGGERS:
        return isc::dhcp::Dhcp6Parser::make_OUTPUT_OPTIONS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("output_options", loc);
    }
}

\"output\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::OUTPUT_OPTIONS:
        return isc::dhcp::Dhcp6Parser::make_OUTPUT(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("output", loc);
    }
}

\"debuglevel\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::LOGGERS:
        return isc::dhcp::Dhcp6Parser::make_DEBUGLEVEL(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("debuglevel", loc);
    }
}

\"severity\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::LOGGERS:
        return isc::dhcp::Dhcp6Parser::make_SEVERITY(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("severity", loc);
    }
}

\"client-classes\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
    case isc::dhcp::Parser6Context::RESERVATIONS:
        return isc::dhcp::Dhcp6Parser::make_CLIENT_CLASSES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("client-classes", loc);
    }
}

\"client-class\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
    case isc::dhcp::Parser6Context::CLIENT_CLASSES:
        return isc::dhcp::Dhcp6Parser::make_CLIENT_CLASS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("client-class", loc);
    }
}

\"test\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::CLIENT_CLASSES:
    case isc::dhcp::Parser6Context::CLIENT_CLASS:
        return isc::dhcp::Dhcp6Parser::make_TEST(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("test", loc);
    }
}

\"reservations\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SUBNET6:
        return isc::dhcp::Dhcp6Parser::make_RESERVATIONS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("reservations", loc);
    }
}

\"ip-addresses\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::RESERVATIONS:
        return isc::dhcp::Dhcp6Parser::make_IP_ADDRESSES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("ip-addresses", loc);
    }
}

\"prefixes\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::RESERVATIONS:
        return isc::dhcp::Dhcp6Parser::make_PREFIXES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("prefixes", loc);
    }
}

\"duid\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::MAC_SOURCES:
    case isc::dhcp::Parser6Context::HOST_RESERVATION_IDENTIFIERS:
    case isc::dhcp::Parser6Context::RESERVATIONS:
        return isc::dhcp::Dhcp6Parser::make_DUID(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("duid", loc);
    }
}

\"hw-address\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::HOST_RESERVATION_IDENTIFIERS:
    case isc::dhcp::Parser6Context::RESERVATIONS:
        return isc::dhcp::Dhcp6Parser::make_HW_ADDRESS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("hw-address", loc);
    }
}

\"hostname\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::RESERVATIONS:
        return isc::dhcp::Dhcp6Parser::make_HOSTNAME(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("hostname", loc);
    }
}

\"space\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::OPTION_DATA:
        return isc::dhcp::Dhcp6Parser::make_SPACE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("space", loc);
    }
}

\"csv-format\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::OPTION_DATA:
        return isc::dhcp::Dhcp6Parser::make_CSV_FORMAT(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("csv-format", loc);
    }
}

\"hooks-libraries\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_HOOKS_LIBRARIES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("hooks-libraries", loc);
    }
}

\"library\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::HOOKS_LIBRARIES:
        return isc::dhcp::Dhcp6Parser::make_LIBRARY(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("library", loc);
    }
}

\"server-id\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_SERVER_ID(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("server-id", loc);
    }
}

\"identifier\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SERVER_ID:
        return isc::dhcp::Dhcp6Parser::make_IDENTIFIER(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("identifier", loc);
    }
}

\"htype\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SERVER_ID:
        return isc::dhcp::Dhcp6Parser::make_HTYPE(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("htype", loc);
    }
}

\"time\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SERVER_ID:
        return isc::dhcp::Dhcp6Parser::make_TIME(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("time", loc);
    }
}

\"enterprise-id\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::SERVER_ID:
        return isc::dhcp::Dhcp6Parser::make_ENTERPRISE_ID(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("enterprise-id", loc);
    }
}

\"expired-leases-processing\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_EXPIRED_LEASES_PROCESSING(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("expired-leases-processing", loc);
    }
}

\"dhcp4o6-port\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_DHCP4O6_PORT(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("dhcp4o6-port", loc);
    }
}

\"dhcp-ddns\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP6:
        return isc::dhcp::Dhcp6Parser::make_DHCP_DDNS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("dhcp-ddns", loc);
    }
}

\"enable-updates\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP_DDNS:
        return isc::dhcp::Dhcp6Parser::make_ENABLE_UPDATES(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("enable-updates", loc);
    }
}

\"qualifying-suffix\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::DHCP_DDNS:
        return isc::dhcp::Dhcp6Parser::make_QUALIFYING_SUFFIX(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("qualifying-suffix", loc);
    }
}
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
\"Dhcp4\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::CONFIG:
        return isc::dhcp::Dhcp6Parser::make_DHCP4(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("Dhcp4", loc);
    }
}

\"DhcpDdns\" {
    switch(driver.ctx_) {
    case isc::dhcp::Parser6Context::CONFIG:
        return isc::dhcp::Dhcp6Parser::make_DHCPDDNS(loc);
    default:
        return isc::dhcp::Dhcp6Parser::make_STRING("DhcpDdns", loc);
    }
}
765

766 767 768 769
{JSONString} {
    // A string has been matched. It contains the actual string and single quotes.
    // We need to get those quotes out of the way and just use its content, e.g.
    // for 'foo' we should get foo
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    std::string raw(yytext+1);
    size_t len = raw.size() - 1;
    raw.resize(len);
    std::string decoded;
    decoded.reserve(len);
    for (size_t pos = 0; pos < len; ++pos) {
        char c = raw[pos];
        switch (c) {
        case '"':
            // impossible condition
            driver.error(loc, "Bad quote in \"" + raw + "\"");
        case '\\':
            ++pos;
            if (pos >= len) {
                // impossible condition
                driver.error(loc, "Overflow escape in \"" + raw + "\"");
            }
            c = raw[pos];
            switch (c) {
            case '"':
            case '\\':
            case '/':
                decoded.push_back(c);
                break;
            case 'b':
                decoded.push_back('\b');
                break;
            case 'f':
                decoded.push_back('\f');
                break;
            case 'n':
                decoded.push_back('\n');
                break;
            case 'r':
                decoded.push_back('\r');
                break;
            case 't':
                decoded.push_back('\t');
                break;
            case 'u':
                // not yet implemented
                driver.error(loc, "Unsupported unicode escape in \"" + raw + "\"");
            default:
                // impossible condition
                driver.error(loc, "Bad escape in \"" + raw + "\"");
            }
            break;
        default:
            if (c < 0x20) {
                // impossible condition
                driver.error(loc, "Invalid control in \"" + raw + "\"");
            }
            decoded.push_back(c);
        }
    }
825

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    return isc::dhcp::Dhcp6Parser::make_STRING(decoded, loc);
}

\"{JSONStringCharacter}*{ControlCharacter}{ControlCharacterFill}*\" {
    // Bad string with a forbidden control character inside
    driver.error(loc, "Invalid control in " + std::string(yytext));
}

\"{JSONStringCharacter}*\\{BadJSONEscapeSequence}[^\x00-\x1f"]*\" {
    // Bad string with a bad escape inside
    driver.error(loc, "Bad escape in " + std::string(yytext));
}
    
\"{JSONStringCharacter}*\\\" {
    // Bad string with an open escape at the end
    driver.error(loc, "Overflow escape in " + std::string(yytext));
842 843
}

844 845 846 847 848 849
"["    { return isc::dhcp::Dhcp6Parser::make_LSQUARE_BRACKET(loc); }
"]"    { return isc::dhcp::Dhcp6Parser::make_RSQUARE_BRACKET(loc); }
"{"    { return isc::dhcp::Dhcp6Parser::make_LCURLY_BRACKET(loc); }
"}"    { return isc::dhcp::Dhcp6Parser::make_RCURLY_BRACKET(loc); }
","    { return isc::dhcp::Dhcp6Parser::make_COMMA(loc); }
":"    { return isc::dhcp::Dhcp6Parser::make_COLON(loc); }
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

{int} {
    // An integer was found.
    std::string tmp(yytext);
    int64_t integer = 0;
    try {
        // In substring we want to use negative values (e.g. -1).
        // In enterprise-id we need to use values up to 0xffffffff.
        // To cover both of those use cases, we need at least
        // int64_t.
        integer = boost::lexical_cast<int64_t>(tmp);
    } catch (const boost::bad_lexical_cast &) {
        driver.error(loc, "Failed to convert " + tmp + " to an integer.");
    }

    // The parser needs the string form as double conversion is no lossless
    return isc::dhcp::Dhcp6Parser::make_INTEGER(integer, loc);
}
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
[-+]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)? {
    // A floating point was found.
    std::string tmp(yytext);
    double fp = 0.0;
    try {
        fp = boost::lexical_cast<double>(tmp);
    } catch (const boost::bad_lexical_cast &) {
        driver.error(loc, "Failed to convert " + tmp + " to a floating point.");
    }

    return isc::dhcp::Dhcp6Parser::make_FLOAT(fp, loc);
}

true|false {
    string tmp(yytext);
    return isc::dhcp::Dhcp6Parser::make_BOOLEAN(tmp == "true", loc);
}

null {
   return isc::dhcp::Dhcp6Parser::make_NULL_TYPE(loc);
}

891
<*>.   driver.error (loc, "Invalid character: " + std::string(yytext));
892

893
<<EOF>> {
Francis Dupont's avatar
Francis Dupont committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907
    if (states.empty()) {
        return isc::dhcp::Dhcp6Parser::make_END(loc);
    }
    loc = locs.back();
    locs.pop_back();
    file = files.back();
    files.pop_back();
    parser6__delete_buffer(YY_CURRENT_BUFFER);
    parser6__switch_to_buffer(states.back());
    states.pop_back();

    BEGIN(DIR_EXIT);
}

908 909 910 911 912
%%

using namespace isc::dhcp;

void
913
Parser6Context::scanStringBegin(const std::string& str, ParserType parser_type)
914
{
915 916 917 918
    locs.clear();
    files.clear();
    states.clear();
    static_cast<void>(parser6_lex_destroy());
919 920 921
    start_token_flag = true;
    start_token_value = parser_type;

Francis Dupont's avatar
Francis Dupont committed
922 923
    file = "<string>";
    loc.initialize(&file);
924 925
    yy_flex_debug = trace_scanning_;
    YY_BUFFER_STATE buffer;
926
    buffer = yy_scan_bytes(str.c_str(), str.size());
927 928 929 930 931 932 933 934 935 936 937 938
    if (!buffer) {
        fatal("cannot scan string");
        // fatal() throws an exception so this can't be reached
    }
}

void
Parser6Context::scanStringEnd()
{
    yy_delete_buffer(YY_CURRENT_BUFFER);
}

939
void
Francis Dupont's avatar
Francis Dupont committed
940 941
Parser6Context::scanFileBegin(FILE * f,
                              const std::string& filename,
942 943 944 945 946 947
                              ParserType parser_type)
{
    locs.clear();
    files.clear();
    states.clear();
    static_cast<void>(parser6_lex_destroy());
948 949 950
    start_token_flag = true;
    start_token_value = parser_type;

Francis Dupont's avatar
Francis Dupont committed
951 952
    file = filename;
    loc.initialize(&file);
953 954 955 956 957 958
    yy_flex_debug = trace_scanning_;
    YY_BUFFER_STATE buffer;

    // See dhcp6_lexer.cc header for available definitions
    buffer = parser6__create_buffer(f, 65536 /*buffer size*/);
    if (!buffer) {
Francis Dupont's avatar
Francis Dupont committed
959
        fatal("cannot scan file " + filename);
960
    }
961
    parser6__switch_to_buffer(buffer);
962 963 964 965 966 967 968 969
}

void
Parser6Context::scanFileEnd(FILE * f) {
    fclose(f);
    yy_delete_buffer(YY_CURRENT_BUFFER);
}

970 971
void
Parser6Context::includeFile(const std::string& filename) {
Francis Dupont's avatar
Francis Dupont committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    if (states.size() > 10) {
        fatal("Too many nested include.");
    }

    FILE* f = fopen(filename.c_str(), "r");
    if (!f) {
        fatal("Can't open include file " + filename);
    }
    states.push_back(YY_CURRENT_BUFFER);
    YY_BUFFER_STATE buffer;
    buffer = parser6__create_buffer(f, 65536 /*buffer size*/);
    if (!buffer) {
        fatal( "Can't scan include file " + filename);
    }
    parser6__switch_to_buffer(buffer);
    files.push_back(file);
    file = filename;
    locs.push_back(loc);
    loc.initialize(&file);

    BEGIN(INITIAL);
993 994
}

995 996 997 998 999 1000 1001
namespace {
/// To avoid unused function error
class Dummy {
    // cppcheck-suppress unusedPrivateFunction
    void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
};
}