parser_context.h 4.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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/.

#ifndef PARSER_CONTEXT_H
#define PARSER_CONTEXT_H
#include <string>
#include <map>
#include <vector>
#include <dhcp6/dhcp6_parser.h>
#include <dhcp6/parser_context_decl.h>
#include <exceptions/exceptions.h>

// Tell Flex the lexer's prototype ...
17
#define YY_DECL isc::dhcp::Dhcp6Parser::symbol_type parser6_lex (Parser6Context& driver)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

// ... and declare it for the parser's sake.
YY_DECL;

namespace isc {
namespace dhcp {

/// @brief Evaluation error exception raised when trying to parse an axceptions.
class EvalParseError : public isc::Exception {
public:
    EvalParseError(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) { };
};


/// @brief Evaluation context, an interface to the expression evaluation.
class Parser6Context
{
public:
37

38 39
    /// @brief Defines currently support the content supported
    typedef enum {
40 41
        PARSER_GENERIC_JSON, // This will parse the content as generic JSON
        PARSER_DHCP6 // This will parse the content as DHCP6 config
42
    } ParserType;
43

44 45 46 47 48 49 50
    /// @brief Default constructor.
    Parser6Context();

    /// @brief destructor
    virtual ~Parser6Context();

    /// @brief JSON elements being parsed.
51
    std::vector<isc::data::ElementPtr> stack_;
52 53

    /// @brief Method called before scanning starts on a string.
54
    void scanStringBegin(const std::string& str, ParserType type);
55 56 57 58

    /// @brief Method called after the last tokens are scanned from a string.
    void scanStringEnd();

59
    /// @brief Method called before scanning starts on a file.
Francis Dupont's avatar
Francis Dupont committed
60
    void scanFileBegin(FILE * f, const std::string& filename, ParserType type);
61 62

    /// @brief Method called after the last tokens are scanned from a file.
63 64
    void scanFileEnd(FILE * f);

Francis Dupont's avatar
Francis Dupont committed
65
    /// @brief Divert input to an include file.
66
    void includeFile(const std::string& filename);
Francis Dupont's avatar
Francis Dupont committed
67

68 69
    /// @brief Run the parser on the string specified.
    ///
70 71
    /// @param str string to be parsed
    /// @param parser_type specifies expected content (either DHCP6 or generic JSON)
72
    /// @return true on success.
73 74
    isc::data::ConstElementPtr parseString(const std::string& str,
                                           ParserType parser_type);
75

76
    /// @brief Run the parser on the file specified.
77 78
    isc::data::ConstElementPtr parseFile(const std::string& filename,
                                         ParserType parser_type);
79

80 81 82 83
    /// @brief Error handler
    ///
    /// @param loc location within the parsed file when experienced a problem.
    /// @param what string explaining the nature of the error.
84
    void error(const isc::dhcp::location& loc, const std::string& what);
85 86 87 88 89

    /// @brief Error handler
    ///
    /// This is a simplified error reporting tool for possible future
    /// cases when the EvalParser is not able to handle the packet.
90
    void error(const std::string& what);
91 92 93

    /// @brief Fatal error handler
    ///
94 95
    /// This is for should not happen but fatal errors.
    /// Used by YY_FATAL_ERROR macro so required to be static.
96 97
    static void fatal(const std::string& what);

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /// @brief Defines syntactic contexts for lexical tie-ins
    typedef enum {
        /// at toplevel
        NO_KEYWORD,
        CONFIG,
        /// in config
        DHCP6,
        // not yet DHCP4,
        // not yet DHCP_DDNS,
        LOGGING,
        /// Dhcp6
        INTERFACES_CONFIG,
        DATABASE,
        MAC_SOURCES,
        HOST_RESERVATION_IDENTIFIERS,
        HOOKS_LIBRARIES,
        SUBNET6,
        OPTION_DATA,
        CLIENT_CLASSES,
        SERVER_ID,
        DHCP_DDNS,
        /// subnet6
        POOLS,
        PD_POOLS,
        RESERVATIONS,
	/// client-classes
	CLIENT_CLASS,
        /// Logging
        LOGGERS,
        /// loggers
        OUTPUT_OPTIONS
    } ParserContext;    

    /// @brief Current syntactic context
    ParserContext ctx_;

    /// @brief Enter a new syntactic context
    void enter(const ParserContext& ctx);

    /// @brief Leave a syntactic context
    /// @throw isc::Unexpected if unbalanced
    void leave();

141 142 143 144 145 146
 private:
    /// @brief Flag determining scanner debugging.
    bool trace_scanning_;

    /// @brief Flag determing parser debugging.
    bool trace_parsing_;
147 148 149

    /// @brief Syntactic context stack
    std::vector<ParserContext> cstack_;
150 151 152 153 154 155
};

}; // end of isc::eval namespace
}; // end of isc namespace

#endif