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