parser_context.cc 3.86 KB
Newer Older
1
// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// 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/.

#include <dhcp4/parser_context.h>
#include <dhcp4/dhcp4_parser.h>
#include <exceptions/exceptions.h>
#include <cc/data.h>
#include <boost/lexical_cast.hpp>
#include <fstream>
#include <limits>

namespace isc {
namespace dhcp {

Parser4Context::Parser4Context()
19
  : trace_scanning_(false), trace_parsing_(false), ctx_(NO_KEYWORD)
20
21
22
23
24
25
26
{
}

Parser4Context::~Parser4Context()
{
}

27
isc::data::ElementPtr
28
29
30
31
32
33
Parser4Context::parseString(const std::string& str, ParserType parser_type)
{
    scanStringBegin(str, parser_type);
    return (parseCommon());
}

34
isc::data::ElementPtr
35
36
37
38
39
40
41
42
43
Parser4Context::parseFile(const std::string& filename, ParserType parser_type) {
    FILE* f = fopen(filename.c_str(), "r");
    if (!f) {
        isc_throw(Dhcp4ParseError, "Unable to open file " << filename);
    }
    scanFileBegin(f, filename, parser_type);
    return (parseCommon());
}

44
isc::data::ElementPtr
45
46
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Parser4Context::parseCommon() {
    isc::dhcp::Dhcp4Parser parser(*this);
    // Uncomment this to get detailed parser logs.
    // trace_parsing_ = true;
    parser.set_debug_level(trace_parsing_);
    try {
        int res = parser.parse();
        if (res != 0) {
            isc_throw(Dhcp4ParseError, "Parser abort");
        }
        scanEnd();
    }
    catch (...) {
        scanEnd();
        throw;
    }
    if (stack_.size() == 1) {
        return (stack_[0]);
    } else {
        isc_throw(Dhcp4ParseError, "Expected exactly one terminal Element expected, found "
                  << stack_.size());
    }
}


void
Parser4Context::error(const isc::dhcp::location& loc, const std::string& what)
{
    isc_throw(Dhcp4ParseError, loc << ": " << what);
}

void
Parser4Context::error (const std::string& what)
{
    isc_throw(Dhcp4ParseError, what);
}

void
Parser4Context::fatal (const std::string& what)
{
    isc_throw(Dhcp4ParseError, what);
}

isc::data::Element::Position
Parser4Context::loc2pos(isc::dhcp::location& loc)
{
    const std::string& file = *loc.begin.filename;
    const uint32_t line = loc.begin.line;
    const uint32_t pos = loc.begin.column;
    return (isc::data::Element::Position(file, line, pos));
}

void
Parser4Context::enter(const ParserContext& ctx)
{
    cstack_.push_back(ctx_);
    ctx_ = ctx;
}

void
Parser4Context::leave()
{
#if 1
    if (cstack_.empty()) {
        fatal("unbalanced syntactic context");
    }
#endif
    ctx_ = cstack_.back();
    cstack_.pop_back();
}

const std::string
Parser4Context::contextName()
{
    switch (ctx_) {
    case NO_KEYWORD:
        return ("__no keyword__");
    case CONFIG:
        return ("toplevel");
    case DHCP4:
        return ("Dhcp4");
    case LOGGING:
        return ("Logging");
    case INTERFACES_CONFIG:
        return ("interfaces-config");
    case LEASE_DATABASE:
        return ("lease-database");
    case HOSTS_DATABASE:
        return ("hosts-database");
    case HOST_RESERVATION_IDENTIFIERS:
        return ("host-reservation-identifiers");
    case HOOKS_LIBRARIES:
        return ("hooks-librairies");
    case SUBNET4:
        return ("subnet4");
    case OPTION_DEF:
        return ("option-def");
    case OPTION_DATA:
        return ("option-data");
    case CLIENT_CLASSES:
        return ("client-classes");
    case SERVER_ID:
        return ("server-id");
    case CONTROL_SOCKET:
        return ("control-socket");
    case POOLS:
        return ("pools");
    case RESERVATIONS:
        return ("reservations");
    case RELAY:
        return ("relay");
    case CLIENT_CLASS:
        return ("client-class");
    case LOGGERS:
        return ("loggers");
    case OUTPUT_OPTIONS:
        return ("output-options");
    default:
        return ("__unknown__");
    }
}

};
};