dhcp6_lexer.ll 13.6 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 45 46
/// @brief State stack.
std::vector<struct yy_buffer_state*> states;

bool start_token_flag = false;

isc::dhcp::Parser6Context::ParserType start_token_value;

};
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
// 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

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

/* This line tells flex to track the line numbers. It's not really that
   useful for client classes, which typically are one-liners, but it may be
   useful in more complex cases. */
%option yylineno

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

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* 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]

UnicodeEscapeSequence                   u[0-9A-Fa-f]{4}
JSONEscapeCharacter                     ["\\/bfnrt]
JSONEscapeSequence                      {JSONEscapeCharacter}|{UnicodeEscapeSequence}
JSONStringCharacter                     [^"\\]|\\{JSONEscapeSequence}
JSONString                              \"{JSONStringCharacter}*\"


%{
// 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);
%}

%%

%{
105 106 107 108
    // 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

109 110
    // Code run each time yylex is called.
    loc.step();
Tomek Mrugalski's avatar
Tomek Mrugalski committed
111 112

    int comment_start_line = 0;
113 114 115 116 117 118 119

    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:
120
        default:
121 122 123
            return isc::dhcp::Dhcp6Parser::make_TOPLEVEL_GENERIC_JSON(loc);
        }
    }
124 125
%}

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

"//"(.*) ;

"/*" {
  BEGIN(COMMENT);
  comment_start_line = yylineno;
}

<COMMENT>"*/" BEGIN(INITIAL);
136
<COMMENT>. ;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
137 138 139 140
<COMMENT><<EOF>> {
    isc_throw(isc::BadValue, "Comment not closed. (/* in line " << comment_start_line);
}

Francis Dupont's avatar
Francis Dupont committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154
"<?" 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);

    Parser6Context::includeFile(tmp);
}
<DIR_EXIT>"?>" BEGIN(INITIAL);
    

155 156 157 158
{blank}+   {
    // Ok, we found a with space. Let's ignore it and update loc variable.
    loc.step();
}
159

160 161 162 163 164 165
[\n]+      {
    // Newline found. Let's update the location and continue.
    loc.lines(yyleng);
    loc.step();
}

166 167 168
\"Dhcp6\"  { return isc::dhcp::Dhcp6Parser::make_DHCP6(loc); }
\"interfaces-config\" { return  isc::dhcp::Dhcp6Parser::make_INTERFACES_CONFIG(loc); }
\"interfaces\" { return  isc::dhcp::Dhcp6Parser::make_INTERFACES(loc); }
169

170
\"lease-database\" { return  isc::dhcp::Dhcp6Parser::make_LEASE_DATABASE(loc); }
171 172 173 174 175
\"hosts-database\" { return  isc::dhcp::Dhcp6Parser::make_HOSTS_DATABASE(loc); }
\"type\" { return isc::dhcp::Dhcp6Parser::make_TYPE(loc); }
\"user\" { return isc::dhcp::Dhcp6Parser::make_USER(loc); }
\"password\" { return isc::dhcp::Dhcp6Parser::make_PASSWORD(loc); }
\"host\" { return isc::dhcp::Dhcp6Parser::make_HOST(loc); }
176
\"persist\" { return isc::dhcp::Dhcp6Parser::make_PERSIST(loc); }
177
\"lfc-interval\" { return isc::dhcp::Dhcp6Parser::make_LFC_INTERVAL(loc); }
178

179 180 181 182 183 184 185 186 187
\"preferred-lifetime\" { return  isc::dhcp::Dhcp6Parser::make_PREFERRED_LIFETIME(loc); }
\"valid-lifetime\" { return  isc::dhcp::Dhcp6Parser::make_VALID_LIFETIME(loc); }
\"renew-timer\" { return  isc::dhcp::Dhcp6Parser::make_RENEW_TIMER(loc); }
\"rebind-timer\" { return  isc::dhcp::Dhcp6Parser::make_REBIND_TIMER(loc); }
\"subnet6\" { return  isc::dhcp::Dhcp6Parser::make_SUBNET6(loc); }
\"option-data\" { return  isc::dhcp::Dhcp6Parser::make_OPTION_DATA(loc); }
\"name\" { return  isc::dhcp::Dhcp6Parser::make_NAME(loc); }
\"data\" { return  isc::dhcp::Dhcp6Parser::make_DATA(loc); }
\"pools\" { return  isc::dhcp::Dhcp6Parser::make_POOLS(loc); }
188 189 190 191 192 193

\"pd-pools\" { return  isc::dhcp::Dhcp6Parser::make_PD_POOLS(loc); }
\"prefix\" { return  isc::dhcp::Dhcp6Parser::make_PREFIX(loc); }
\"prefix-len\" { return  isc::dhcp::Dhcp6Parser::make_PREFIX_LEN(loc); }
\"delegated-len\" { return  isc::dhcp::Dhcp6Parser::make_DELEGATED_LEN(loc); }

194 195 196
\"pool\" { return  isc::dhcp::Dhcp6Parser::make_POOL(loc); }
\"subnet\" { return  isc::dhcp::Dhcp6Parser::make_SUBNET(loc); }
\"interface\" { return  isc::dhcp::Dhcp6Parser::make_INTERFACE(loc); }
197 198
\"id\" { return  isc::dhcp::Dhcp6Parser::make_ID(loc); }

199
\"code\" { return isc::dhcp::Dhcp6Parser::make_CODE(loc); }
200 201
\"mac-sources\" { return isc::dhcp::Dhcp6Parser::make_MAC_SOURCES(loc); }
\"relay-supplied-options\" { return isc::dhcp::Dhcp6Parser::make_RELAY_SUPPLIED_OPTIONS(loc); }
202
\"host-reservation-identifiers\" { return isc::dhcp::Dhcp6Parser::make_HOST_RESERVATION_IDENTIFIERS(loc); }
203 204 205 206

\"Logging\" { return isc::dhcp::Dhcp6Parser::make_LOGGING(loc); }
\"loggers\" { return isc::dhcp::Dhcp6Parser::make_LOGGERS(loc); }
\"output_options\" { return isc::dhcp::Dhcp6Parser::make_OUTPUT_OPTIONS(loc); }
207
\"output\" { return isc::dhcp::Dhcp6Parser::make_OUTPUT(loc); }
208 209
\"debuglevel\" { return isc::dhcp::Dhcp6Parser::make_DEBUGLEVEL(loc); }
\"severity\" { return isc::dhcp::Dhcp6Parser::make_SEVERITY(loc); }
210

211 212 213 214 215
\"client-classes\" { return isc::dhcp::Dhcp6Parser::make_CLIENT_CLASSES(loc); }
\"client-class\" { return isc::dhcp::Dhcp6Parser::make_CLIENT_CLASS(loc); }
\"test\" { return isc::dhcp::Dhcp6Parser::make_TEST(loc); }

\"reservations\" { return isc::dhcp::Dhcp6Parser::make_RESERVATIONS(loc); }
216 217
\"ip-addresses\" { return isc::dhcp::Dhcp6Parser::make_IP_ADDRESSES(loc); }
\"prefixes\" { return isc::dhcp::Dhcp6Parser::make_PREFIXES(loc); }
218
\"duid\" { return isc::dhcp::Dhcp6Parser::make_DUID(loc); }
219 220 221
\"hw-address\" { return isc::dhcp::Dhcp6Parser::make_HW_ADDRESS(loc); }
\"hostname\" { return isc::dhcp::Dhcp6Parser::make_HOSTNAME(loc); }
\"space\" { return isc::dhcp::Dhcp6Parser::make_SPACE(loc); }
222
\"csv-format\" { return isc::dhcp::Dhcp6Parser::make_CSV_FORMAT(loc); }
223

224 225 226 227 228 229 230
\"hooks-libraries\" { return isc::dhcp::Dhcp6Parser::make_HOOKS_LIBRARIES(loc); }
\"library\" { return isc::dhcp::Dhcp6Parser::make_LIBRARY(loc); }

\"server-id\" { return isc::dhcp::Dhcp6Parser::make_SERVER_ID(loc); }
\"identifier\" { return isc::dhcp::Dhcp6Parser::make_IDENTIFIER(loc); }
\"htype\" { return isc::dhcp::Dhcp6Parser::make_HTYPE(loc); }
\"time\" { return isc::dhcp::Dhcp6Parser::make_TIME(loc); }
231
\"enterprise-id\" { return isc::dhcp::Dhcp6Parser::make_ENTERPRISE_ID(loc); }
232 233 234

\"expired-leases-processing\" { return isc::dhcp::Dhcp6Parser::make_EXPIRED_LEASES_PROCESSING(loc); }

235
\"dhcp4o6-port\" { return isc::dhcp::Dhcp6Parser::make_DHCP4O6_PORT(loc); }
236

237 238 239 240 241
\"dhcp-ddns\" { return isc::dhcp::Dhcp6Parser::make_DHCP_DDNS(loc); }
\"enable-updates\" { return isc::dhcp::Dhcp6Parser::make_ENABLE_UPDATES(loc); }
\"qualifying-suffix\" { return isc::dhcp::Dhcp6Parser::make_QUALIFYING_SUFFIX(loc); }


242 243 244 245 246 247 248 249 250 251
{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
    std::string tmp(yytext+1);
    tmp.resize(tmp.size() - 1);

    return isc::dhcp::Dhcp6Parser::make_STRING(tmp, loc);
}

252 253 254 255 256 257
"["                  { 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); }
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 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

{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);
}
[-+]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)? {
    // A floating point was found.
    std::string tmp(yytext);
    double fp = 0.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.
        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);
}

.          driver.error (loc, "Invalid character: " + std::string(yytext));
Francis Dupont's avatar
Francis Dupont committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
<<EOF>>    {
    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);
}

318 319 320 321 322
%%

using namespace isc::dhcp;

void
323
Parser6Context::scanStringBegin(const std::string& str, ParserType parser_type)
324
{
325 326 327
    start_token_flag = true;
    start_token_value = parser_type;

Francis Dupont's avatar
Francis Dupont committed
328 329
    file = "<string>";
    loc.initialize(&file);
330 331
    yy_flex_debug = trace_scanning_;
    YY_BUFFER_STATE buffer;
332
    buffer = yy_scan_bytes(str.c_str(), str.size());
333 334 335 336 337 338 339 340 341 342 343 344
    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);
}

345
void
Francis Dupont's avatar
Francis Dupont committed
346 347 348
Parser6Context::scanFileBegin(FILE * f,
                              const std::string& filename,
                              ParserType parser_type) {
349 350 351 352

    start_token_flag = true;
    start_token_value = parser_type;

Francis Dupont's avatar
Francis Dupont committed
353 354
    file = filename;
    loc.initialize(&file);
355 356 357 358 359 360
    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
361
        fatal("cannot scan file " + filename);
362
    }
363
    parser6__switch_to_buffer(buffer);
364 365 366 367 368 369 370 371
}

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

372 373
void
Parser6Context::includeFile(const std::string& filename) {
Francis Dupont's avatar
Francis Dupont committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    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);
395 396
}

397 398 399 400 401 402 403
namespace {
/// To avoid unused function error
class Dummy {
    // cppcheck-suppress unusedPrivateFunction
    void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
};
}