Kea 3.3.3
d2/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2026 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
9#include <d2/d2_parser.h>
10#include <d2/parser_context.h>
11#include <d2srv/d2_log.h>
13#include <cc/data.h>
14#include <util/str.h>
15#include <boost/lexical_cast.hpp>
16#include <fstream>
17#include <sstream>
18#include <limits>
19
20namespace isc {
21namespace d2 {
22
24 : sfile_(0), ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false) {
25}
26
29
31D2ParserContext::parseString(const std::string& str, ParserType parser_type) {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
37D2ParserContext::parseFile(const std::string& filename, ParserType parser_type) {
38 FILE* f = fopen(filename.c_str(), "r");
39 if (!f) {
40 isc_throw(D2ParseError, "Unable to open file " << filename);
41 }
42 scanFileBegin(f, filename, parser_type);
43 return (parseCommon());
44}
45
47D2ParserContext::parseCommon() {
48 isc::d2::D2Parser 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(D2ParseError, "Parser abort");
56 }
57 scanEnd();
58 } catch (...) {
59 scanEnd();
60 throw;
61 }
62 if (stack_.size() == 1) {
63 return (stack_[0]);
64 } else {
65 isc_throw(D2ParseError, "Expected exactly one terminal Element, found "
66 << stack_.size());
67 }
68}
69
70void
71D2ParserContext::error(const isc::d2::location& loc,
72 const std::string& what,
73 size_t pos) {
74 std::string escaped = util::str::escapeNulls(what);
75 if (pos == 0) {
76 isc_throw(D2ParseError, loc << ": " << escaped);
77 } else {
78 isc_throw(D2ParseError, loc << " (near " << pos << "): " << escaped);
79 }
80}
81
82void
83D2ParserContext::error(const std::string& what) {
85}
86
87void
88D2ParserContext::fatal(const std::string& what) {
90}
91
93D2ParserContext::loc2pos(isc::d2::location& loc) {
94 const std::string& file = *loc.begin.filename;
95 const uint32_t line = loc.begin.line;
96 const uint32_t pos = loc.begin.column;
97 return (isc::data::Element::Position(file, line, pos));
98}
99
100void
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
115D2ParserContext::unique(const std::string& name,
117 ConstElementPtr value = stack_.back()->get(name);
118 if (value) {
119 if (ctx_ != NO_KEYWORD) {
120 isc_throw(D2ParseError, loc << ": duplicate " << name
121 << " entries in " << contextName()
122 << " map (previous at " << value->getPosition() << ")");
123 } else {
124 isc_throw(D2ParseError, loc << ": duplicate " << name
125 << " entries in JSON"
126 << " map (previous at " << value->getPosition() << ")");
127 }
128 }
129}
130
131void
133 cstack_.push_back(ctx_);
134 ctx_ = ctx;
135}
136
137void
139 if (cstack_.empty()) {
140 fatal("unbalanced syntactic context");
141 }
142
143 ctx_ = cstack_.back();
144 cstack_.pop_back();
145}
146
147const std::string
149 switch (ctx_) {
150 case NO_KEYWORD:
151 return ("__no keyword__");
152 case CONFIG:
153 return ("toplevel");
154 case DHCPDDNS:
155 return ("DhcpDdns");
156 case TSIG_KEY:
157 return ("tsig-key");
158 case TSIG_KEYS:
159 return ("tsig-keys");
160 case ALGORITHM:
161 return ("algorithm");
162 case DIGEST_BITS:
163 return ("digest-bits");
164 case SECRET:
165 return ("secret");
166 case FORWARD_DDNS:
167 return ("forward-ddns");
168 case REVERSE_DDNS:
169 return ("reverse-ddns");
170 case DDNS_DOMAIN:
171 return ("ddns-domain");
172 case DDNS_DOMAINS:
173 return ("ddns-domains");
174 case DNS_SERVER:
175 return ("dns-server");
176 case DNS_SERVERS:
177 return ("dns-servers");
178 case CONTROL_SOCKETS:
179 return ("control-sockets");
181 return ("control-socket-type");
182 case AUTHENTICATION:
183 return ("authentication");
184 case AUTH_TYPE:
185 return ("auth-type");
186 case CLIENTS:
187 return ("clients");
188 case LOGGERS:
189 return ("loggers");
190 case OUTPUT_OPTIONS:
191 return ("output-options");
192 case NCR_PROTOCOL:
193 return ("ncr-protocol");
194 case NCR_FORMAT:
195 return ("ncr-format");
196 case HOOKS_LIBRARIES:
197 return ("hooks-libraries");
198 default:
199 return ("__unknown__");
200 }
201}
202
203void
204D2ParserContext::warning(const isc::d2::location& loc,
205 const std::string& what) {
206 std::ostringstream msg;
207 msg << loc << ": " << what;
209 .arg(msg.str());
210}
211
212void
213D2ParserContext::warnAboutExtraCommas(const isc::d2::location& loc) {
214 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
215}
216
217} // namespace d2
218} // namespace isc
Evaluation error exception raised when trying to parse.
ParserType
Defines currently supported scopes.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
D2ParserContext()
Default constructor.
ParserContext
Defines syntactic contexts for lexical tie-ins.
@ AUTHENTICATION
Used while parsing content of an authentication type.
@ DDNS_DOMAIN
Used while parsing a list of ddns-domains.
@ DNS_SERVERS
Used while parsing content of a control-sockets.
@ DDNS_DOMAINS
Used while parsing content of a dns-server.
@ AUTH_TYPE
Used while parsing content of a client.
@ NCR_FORMAT
Used while parsing DhcpDdns/ncr-format.
@ NCR_PROTOCOL
Used while parsing DhcpDdns/ncr-protocol.
@ NO_KEYWORD
This one is used in pure JSON mode.
@ DHCPDDNS
Used while parsing content of a tsig-key.
@ SECRET
Used while parsing content of DhcpDdns/forward-ddns.
@ ALGORITHM
Used while parsing content of DhcpDdns/tsig-keys/digest-bits.
@ OUTPUT_OPTIONS
Used while parsing DhcpDdns/loggers/output-options structures.
@ FORWARD_DDNS
Used while parsing content of DhcpDdns/reverse-ddns.
@ CONTROL_SOCKET_TYPE
Used while parsing content of an authentication.
@ TSIG_KEY
Used while parsing a list of tsig-keys.
@ REVERSE_DDNS
Used while parsing content of a ddns-domain.
@ LOGGERS
Used while parsing DhcpDdns/loggers structures.
@ HOOKS_LIBRARIES
Used while parsing DhcpDdns/hooks-libraries.
@ TSIG_KEYS
Used while parsing content of DhcpDdns/tsig-keys/algorithm.
@ DIGEST_BITS
Used while parsing content of DhcpDdns/tsig-keys/secret.
@ CONFIG
Used while parsing content of DhcpDdns.
@ DNS_SERVER
Used while parsing content of list of dns-servers.
@ CONTROL_SOCKETS
Used while parsing content of a socket-type.
void warning(const isc::d2::location &loc, const std::string &what)
Warning handler.
FILE * sfile_
sFile (aka FILE)
const std::string contextName()
Get the syntax context name.
void error(const isc::d2::location &loc, const std::string &what, size_t pos=0)
Error handler.
void leave()
Leave a syntactic context.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition d2_lexer.cc:3987
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
void warnAboutExtraCommas(const isc::d2::location &loc)
Warning for extra commas.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition d2_lexer.cc:4005
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
virtual ~D2ParserContext()
destructor.
ParserContext ctx_
Current syntactic context.
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.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
static void fatal(const std::string &what)
Fatal error handler.
void scanEnd()
Method called after the last tokens are scanned.
Definition d2_lexer.cc:4027
isc::data::Element::Position loc2pos(isc::d2::location &loc)
Converts bison's position to one understood by isc::data::Element.
A Bison parser.
Definition d2_parser.h:217
Define the isc::d2::parser class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
isc::log::Logger d2_to_dns_logger("d2-to-dns")
Definition d2_log.h:20
const isc::log::MessageID DHCP_DDNS_CONFIG_SYNTAX_WARNING
Definition d2_messages.h:19
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
std::string escapeNulls(std::string str)
Escape null characters.
Definition str.cc:442
Defines the logger used by the top-level component of kea-lfc.
Represents the position of the data element within a configuration string.
Definition data.h:107