Kea  2.1.7-git
netconf/parser_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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 <netconf/netconf_parser.h>
11 #include <netconf/netconf_log.h>
12 #include <exceptions/exceptions.h>
13 //#include <cc/dhcp_config_error.h>
14 #include <cc/data.h>
15 #include <fstream>
16 #include <sstream>
17 #include <limits>
18 
19 namespace isc {
20 namespace netconf {
21 
23  : sfile_(0), ctx_(NO_KEYWORDS), trace_scanning_(false), trace_parsing_(false)
24 {
25 }
26 
28 {
29 }
30 
32 ParserContext::parseString(const std::string& str, ParserType parser_type)
33 {
34  scanStringBegin(str, parser_type);
35  return (parseCommon());
36 }
37 
39 ParserContext::parseFile(const std::string& filename, ParserType parser_type) {
40  FILE* f = fopen(filename.c_str(), "r");
41  if (!f) {
42  isc_throw(ParseError, "Unable to open file " << filename);
43  }
44  scanFileBegin(f, filename, parser_type);
45  return (parseCommon());
46 }
47 
49 ParserContext::parseCommon() {
50  isc::netconf::NetconfParser 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(ParseError, "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(ParseError, "Expected exactly one terminal Element expected, found "
69  << stack_.size());
70  }
71 }
72 
73 void
74 ParserContext::error(const isc::netconf::location& loc,
75  const std::string& what,
76  size_t pos)
77 {
78  if (pos == 0) {
79  isc_throw(ParseError, loc << ": " << what);
80  } else {
81  isc_throw(ParseError, loc << " (near " << pos << "): " << what);
82  }
83 }
84 
85 void
86 ParserContext::error(const std::string& what)
87 {
88  isc_throw(ParseError, what);
89 }
90 
91 void
92 ParserContext::fatal(const std::string& what)
93 {
94  isc_throw(ParseError, what);
95 }
96 
98 ParserContext::loc2pos(isc::netconf::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 ParserContext::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 ParserContext::unique(const std::string& name,
124 {
125  ConstElementPtr value = stack_.back()->get(name);
126  if (value) {
127  if (ctx_ != NO_KEYWORDS) {
128  isc_throw(ParseError, loc << ": duplicate " << name
129  << " entries in " << contextName()
130  << " map (previous at " << value->getPosition() << ")");
131  } else {
132  isc_throw(ParseError, 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  ctx_ = cstack_.back();
153  cstack_.pop_back();
154 }
155 
156 const std::string
158 {
159  switch (ctx_) {
160  case NO_KEYWORDS:
161  return ("__no keywords__");
162  case CONFIG:
163  return ("toplevel");
164  case NETCONF:
165  return ("Netconf");
166  case MANAGED_SERVERS:
167  return ("managed-servers");
168  case SERVER:
169  return ("managed-servers entry");
170  case CONTROL_SOCKET:
171  return ("control-socket");
172  case SOCKET_TYPE:
173  return ("socket-type");
174  case HOOKS_LIBRARIES:
175  return ("hooks-libraries");
176  case LOGGERS:
177  return ("loggers");
178  case OUTPUT_OPTIONS:
179  return ("output-options");
180  default:
181  return ("__unknown__");
182  }
183 }
184 
185 void
186 ParserContext::warning(const isc::netconf::location& loc,
187  const std::string& what) {
188  std::ostringstream msg;
189  msg << loc << ": " << what;
191  .arg(msg.str());
192 }
193 
194 void
195 ParserContext::warnAboutExtraCommas(const isc::netconf::location& loc) {
196  warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
197 }
198 
199 } // namespace netconf
200 } // namespace isc
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
ParserContext()
Default constructor.
static void fatal(const std::string &what)
Fatal error handler.
Used while parsing Netconf/loggers/output_options structures.
Used while parsing Netconf/managed-servers/*/control-socket/socket-type.
LexerContext
Defines syntactic contexts for lexical tie-ins.
void scanEnd()
Method called after the last tokens are scanned.
void leave()
Leave a syntactic context.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
LexerContext ctx_
Current syntactic context.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
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.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void set_debug_level(debug_level_type l)
Set the current debugging level.
virtual int parse()
Parse.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Evaluation error exception raised when trying to parse.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
void warning(const isc::netconf::location &loc, const std::string &what)
Warning handler.
void warnAboutExtraCommas(const isc::netconf::location &loc)
Warning for extra commas.
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
Used while parsing Netconf/hooks-libraries.
isc::data::Element::Position loc2pos(isc::netconf::location &loc)
Converts bison&#39;s position to one understandable by isc::data::Element.
void error(const isc::netconf::location &loc, const std::string &what, size_t pos=0)
Error handler.
Used while parsing Netconf/loggers structures.
Used while parsing Netconf/manages-servers/*/control-socket.
Represents the position of the data element within a configuration string.
Definition: data.h:92
const isc::log::MessageID NETCONF_CONFIG_SYNTAX_WARNING
Defines the logger used by the top-level component of kea-lfc.
Used while parsing content of Netconf.
This one is used in pure JSON mode.
Define the isc::netconf::parser class.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
void enter(const LexerContext &ctx)
Enter a new syntactic context.
ParserType
Defines currently supported scopes.
Contains declarations for loggers used by the Kea netconf agent.
Used while parsing Netconf/managed-servers.
const std::string contextName()
Get the syntactic context name.