Kea  2.5.3
agent/parser_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-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 
9 #include <agent/parser_context.h>
10 #include <agent/agent_parser.h>
11 #include <agent/ca_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 agent {
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::agent::AgentParser 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::agent::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::agent::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 AGENT:
165  return ("Control-agent");
166  case AUTHENTICATION:
167  return ("authentication");
168  case AUTH_TYPE:
169  return ("auth-type");
170  case CLIENTS:
171  return ("clients");
172  case CONTROL_SOCKETS:
173  return ("control-sockets");
174  case SERVER:
175  return ("xxx-server");
176  case SOCKET_TYPE:
177  return ("socket-type");
178  case HOOKS_LIBRARIES:
179  return ("hooks-libraries");
180  case LOGGERS:
181  return ("loggers");
182  case OUTPUT_OPTIONS:
183  return ("output-options");
184  default:
185  return ("__unknown__");
186  }
187 }
188 
189 void
190 ParserContext::warning(const isc::agent::location& loc,
191  const std::string& what) {
192  std::ostringstream msg;
193  msg << loc << ": " << what;
195  .arg(msg.str());
196 }
197 
198 void
199 ParserContext::warnAboutExtraCommas(const isc::agent::location& loc) {
200  warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
201 }
202 
203 } // end of isc::eval namespace
204 } // end of isc namespace
Define the isc::agent::parser class.
Evaluation error exception raised when trying to parse.
void warning(const isc::agent::location &loc, const std::string &what)
Warning handler.
void warnAboutExtraCommas(const isc::agent::location &loc)
Warning for extra commas.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
ParserContext()
Default constructor.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
void unique(const std::string &name, isc::data::Element::Position loc)
Check if a parameter is already present.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
static void fatal(const std::string &what)
Fatal error handler.
virtual ~ParserContext()
destructor
const std::string contextName()
Get the syntactic context name.
isc::data::Element::Position loc2pos(isc::agent::location &loc)
Converts bison's position to one understandable by isc::data::Element.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
LexerContext ctx_
Current syntactic context.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
void leave()
Leave a syntactic context.
LexerContext
Defines syntactic contexts for lexical tie-ins.
@ CONFIG
Used while parsing content of Agent.
@ SERVER
Used while parsing Control-agent/control-socket/*-server/socket-type.
@ NO_KEYWORDS
This one is used in pure JSON mode.
@ HOOKS_LIBRARIES
Used while parsing Control-agent/loggers structures.
@ CONTROL_SOCKETS
Used while parsing Control-agent/control-socket/*-server.
@ SOCKET_TYPE
Used while parsing Control-agent/hooks-libraries.
@ AGENT
Used while parsing Control-agent/Authentication.
@ CLIENTS
Used while parsing Control-agent/control-sockets.
@ AUTHENTICATION
Used while parsing Control-agent/Authentication/type.
@ AUTH_TYPE
Used while parsing Control-agent/Authentication/clients.
@ LOGGERS
Used while parsing Control-agent/loggers/output-options structures.
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 error(const isc::agent::location &loc, const std::string &what, size_t pos=0)
Error handler.
ParserType
Defines currently supported scopes.
void enter(const LexerContext &ctx)
Enter a new 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
const isc::log::MessageID CTRL_AGENT_CONFIG_SYNTAX_WARNING
Definition: ca_messages.h:17
isc::log::Logger agent_logger("ctrl-agent")
Control Agent logger.
Definition: ca_log.h:18
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
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:94