Kea 1.5.0
dhcp4/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10#include <dhcp4/dhcp4_parser.h>
12#include <cc/data.h>
13#include <boost/lexical_cast.hpp>
14#include <fstream>
15#include <limits>
16
17namespace isc {
18namespace dhcp {
19
21 : ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false)
22{
23}
24
26{
27}
28
30Parser4Context::parseString(const std::string& str, ParserType parser_type)
31{
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
37Parser4Context::parseFile(const std::string& filename, ParserType parser_type) {
38 FILE* f = fopen(filename.c_str(), "r");
39 if (!f) {
40 isc_throw(Dhcp4ParseError, "Unable to open file " << filename);
41 }
42 scanFileBegin(f, filename, parser_type);
43 return (parseCommon());
44}
45
47Parser4Context::parseCommon() {
48 isc::dhcp::Dhcp4Parser parser(*this);
49 // Uncomment this to get detailed parser logs.
50 // trace_parsing_ = true;
51 parser.set_debug_level(trace_parsing_);
52 try {
53 int res = parser.parse();
54 if (res != 0) {
55 isc_throw(Dhcp4ParseError, "Parser abort");
56 }
57 scanEnd();
58 }
59 catch (...) {
60 scanEnd();
61 throw;
62 }
63 if (stack_.size() == 1) {
64 return (stack_[0]);
65 } else {
66 isc_throw(Dhcp4ParseError, "Expected exactly one terminal Element expected, found "
67 << stack_.size());
68 }
69}
70
71
72void
73Parser4Context::error(const isc::dhcp::location& loc, const std::string& what)
74{
75 isc_throw(Dhcp4ParseError, loc << ": " << what);
76}
77
78void
79Parser4Context::error(const std::string& what)
80{
82}
83
84void
85Parser4Context::fatal(const std::string& what)
86{
88}
89
91Parser4Context::loc2pos(isc::dhcp::location& loc)
92{
93 const std::string& file = *loc.begin.filename;
94 const uint32_t line = loc.begin.line;
95 const uint32_t pos = loc.begin.column;
96 return (isc::data::Element::Position(file, line, pos));
97}
98
99void
100Parser4Context::require(const std::string& name,
103{
104 ConstElementPtr value = stack_.back()->get(name);
105 if (!value) {
107 "missing parameter '" << name << "' ("
108 << stack_.back()->getPosition() << ") ["
109 << contextName() << " map between "
110 << open_loc << " and " << close_loc << "]");
111 }
112}
113
114void
116{
117 cstack_.push_back(ctx_);
118 ctx_ = ctx;
119}
120
121void
123{
124#if 1
125 if (cstack_.empty()) {
126 fatal("unbalanced syntactic context");
127 }
128#endif
129 ctx_ = cstack_.back();
130 cstack_.pop_back();
131}
132
133const std::string
135{
136 switch (ctx_) {
137 case NO_KEYWORD:
138 return ("__no keyword__");
139 case CONFIG:
140 return ("toplevel");
141 case DHCP4:
142 return ("Dhcp4");
143 case LOGGING:
144 return ("Logging");
146 return ("interfaces-config");
147 case DHCP_SOCKET_TYPE:
148 return ("dhcp-socket-type");
150 return ("outbound-interface");
151 case LEASE_DATABASE:
152 return ("lease-database");
153 case HOSTS_DATABASE:
154 return ("hosts-database");
155 case DATABASE_TYPE:
156 return ("database-type");
158 return ("host-reservation-identifiers");
159 case HOOKS_LIBRARIES:
160 return ("hooks-libraries");
161 case SUBNET4:
162 return ("subnet4");
163 case RESERVATION_MODE:
164 return ("reservation-mode");
165 case OPTION_DEF:
166 return ("option-def");
167 case OPTION_DATA:
168 return ("option-data");
169 case CLIENT_CLASSES:
170 return ("client-classes");
172 return ("expired-leases-processing");
173 case SERVER_ID:
174 return ("server-id");
175 case CONTROL_SOCKET:
176 return ("control-socket");
178 return ("dhcp-queue-control");
179 case POOLS:
180 return ("pools");
181 case RESERVATIONS:
182 return ("reservations");
183 case RELAY:
184 return ("relay");
185 case LOGGERS:
186 return ("loggers");
187 case OUTPUT_OPTIONS:
188 return ("output-options");
189 case DHCP_DDNS:
190 return ("dhcp-ddns");
191 case NCR_PROTOCOL:
192 return ("ncr-protocol");
193 case NCR_FORMAT:
194 return ("ncr-format");
196 return ("replace-client-name");
197 case SHARED_NETWORK:
198 return ("shared-networks");
199 case SANITY_CHECKS:
200 return ("sanity-checks");
201 case CONFIG_CONTROL:
202 return ("config-control");
203 case CONFIG_DATABASE:
204 return ("config-database");
205 default:
206 return ("__unknown__");
207 }
208}
209
210};
211};
Evaluation error exception raised when trying to parse.
A Bison parser.
Definition: dhcp4_parser.h:496
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
void error(const isc::dhcp::location &loc, const std::string &what)
Error handler.
void require(const std::string &name, isc::data::Element::Position open_loc, isc::data::Element::Position close_loc)
Check if a required parameter is present.
isc::data::Element::Position loc2pos(isc::dhcp::location &loc)
Converts bison's position to one understandable by isc::data::Element.
void leave()
Leave a syntactic context.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void scanEnd()
Method called after the last tokens are scanned.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Parser4Context()
Default constructor.
const std::string contextName()
Get the syntactic context name.
ParserContext
Defines syntactic contexts for lexical tie-ins.
@ SUBNET4
Used while parsing Dhcp4/Subnet4 structures.
@ DATABASE_TYPE
Used while parsing Dhcp4/*-database/type.
@ POOLS
Used while parsing Dhcp4/subnet4/pools structures.
@ CLIENT_CLASSES
Used while parsing Dhcp4/client-classes structures.
@ RESERVATIONS
Used while parsing Dhcp4/reservations structures.
@ HOSTS_DATABASE
Used while parsing Dhcp4/hosts-database[s] structures.
@ NCR_PROTOCOL
Used while parsing Dhcp4/dhcp-ddns/ncr-protocol.
@ LOGGERS
Used while parsing Logging/loggers structures.
@ NCR_FORMAT
Used while parsing Dhcp4/dhcp-ddns/ncr-format.
@ SERVER_ID
Used while parsing Dhcp4/server-id structures.
@ OUTBOUND_INTERFACE
Used while parsing Dhcp4/interfaces/outbound-interface structures.
@ CONFIG
Used while parsing content of Dhcp4.
@ OUTPUT_OPTIONS
Used while parsing Logging/loggers/output_options structures.
@ RESERVATION_MODE
Used while parsing Dhcp4/reservation-mode.
@ CONTROL_SOCKET
Used while parsing Dhcp4/control-socket structures.
@ LOGGING
Used while parsing content of Logging.
@ DHCP_DDNS
Used while parsing Dhcp4/dhcp-ddns.
@ OPTION_DATA
Used while parsing Dhcp4/option-data, Dhcp4/subnet4/option-data or anywhere option-data is present (c...
@ LEASE_DATABASE
Used while parsing Dhcp4/lease-database structures.
@ SHARED_NETWORK
Used while parsing shared-networks structures.
@ EXPIRED_LEASES_PROCESSING
Used while parsing Dhcp4/expired-leases-processing.
@ CONFIG_CONTROL
Used while parsing Dhcp4/config-control.
@ INTERFACES_CONFIG
Used while parsing Dhcp4/interfaces structures.
@ HOST_RESERVATION_IDENTIFIERS
Used while parsing Dhcp4/host-reservation-identifiers.
@ OPTION_DEF
Used while parsing Dhcp4/option-def structures.
@ CONFIG_DATABASE
Used while parsing config-control/config-databases.
@ NO_KEYWORD
This one is used in pure JSON mode.
@ HOOKS_LIBRARIES
Used while parsing Dhcp4/hooks-libraries.
@ DHCP_QUEUE_CONTROL
Used while parsing Dhcp4/dhcp-queue-control structures.
@ REPLACE_CLIENT_NAME
Used while parsing Dhcp4/dhcp-ddns/replace-client-name.
@ RELAY
Used while parsing Dhcp4/subnet4relay structures.
@ DHCP_SOCKET_TYPE
Used while parsing Dhcp4/interfaces/dhcp-socket-type structures.
ParserType
Defines currently supported scopes.
static void fatal(const std::string &what)
Fatal error handler.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
ParserContext ctx_
Current syntactic context.
virtual ~Parser4Context()
destructor
Define the isc::dhcp::parser class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
Defines the logger used by the top-level component of kea-dhcp-ddns.
Represents the position of the data element within a configuration string.
Definition: data.h:88