Kea  2.5.2
netconf/parser_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-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 <cc/data.h>
10 #include <exceptions/exceptions.h>
11 #include <netconf/netconf_log.h>
12 #include <netconf/netconf_parser.h>
13 #include <netconf/parser_context.h>
14 
15 #include <fstream>
16 #include <limits>
17 #include <sstream>
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(string const& str, ParserType parser_type)
33 {
34  scanStringBegin(str, parser_type);
35  return (parseCommon());
36 }
37 
39 ParserContext::parseFile(string const& 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  string const& 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(string const& what)
87 {
88  isc_throw(ParseError, what);
89 }
90 
91 void
92 ParserContext::fatal(string const& what)
93 {
94  isc_throw(ParseError, what);
95 }
96 
98 ParserContext::loc2pos(isc::netconf::location& loc)
99 {
100  string const& 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(string const& 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(string const& 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 string const
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  string const& what) {
188  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
Evaluation error exception raised when trying to parse.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
ParserContext()
Default constructor.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
const std::string contextName()
Get the syntactic context name.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
LexerContext ctx_
Current syntactic context.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
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.
void warning(const isc::netconf::location &loc, const std::string &what)
Warning handler.
isc::data::Element::Position loc2pos(isc::netconf::location &loc)
Converts bison's position to one understandable by isc::data::Element.
void enter(const LexerContext &ctx)
Enter a new syntactic context.
LexerContext
Defines syntactic contexts for lexical tie-ins.
@ NO_KEYWORDS
This one is used in pure JSON mode.
@ HOOKS_LIBRARIES
Used while parsing Netconf/loggers structures.
@ CONFIG
Used while parsing content of Netconf.
@ MANAGED_SERVERS
Used while parsing Netconf/managed-servers.
@ SERVER
Used while parsing Netconf/manages-servers/*‍/control-socket.
@ LOGGERS
Used while parsing Netconf/loggers/output-options structures.
@ SOCKET_TYPE
Used while parsing Netconf/hooks-libraries.
@ CONTROL_SOCKET
Used while parsing Netconf/managed-servers/*‍/control-socket/socket-type.
static void fatal(const std::string &what)
Fatal error handler.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
ParserType
Defines currently supported scopes.
void warnAboutExtraCommas(const isc::netconf::location &loc)
Warning for extra commas.
void error(const isc::netconf::location &loc, const std::string &what, size_t pos=0)
Error handler.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void leave()
Leave a syntactic context.
void scanEnd()
Method called after the last tokens are scanned.
#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
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
const isc::log::MessageID NETCONF_CONFIG_SYNTAX_WARNING
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
Defines the logger used by the top-level component of kea-lfc.
Contains declarations for loggers used by the Kea netconf agent.
Define the isc::netconf::parser class.
Represents the position of the data element within a configuration string.
Definition: data.h:94