Kea 2.7.4
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>
11#include <netconf/netconf_log.h>
14
15#include <fstream>
16#include <limits>
17#include <sstream>
18
19namespace isc {
20namespace netconf {
21
23 : sfile_(0), ctx_(NO_KEYWORDS), trace_scanning_(false), trace_parsing_(false)
24{
25}
26
30
32ParserContext::parseString(string const& str, ParserType parser_type)
33{
34 scanStringBegin(str, parser_type);
35 return (parseCommon());
36}
37
39ParserContext::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
49ParserContext::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 } catch (...) {
61 scanEnd();
62 throw;
63 }
64 if (stack_.size() == 1) {
65 return (stack_[0]);
66 } else {
67 isc_throw(ParseError, "Expected exactly one terminal Element, found "
68 << stack_.size());
69 }
70}
71
72void
73ParserContext::error(const isc::netconf::location& loc,
74 string const& what,
75 size_t pos)
76{
77 if (pos == 0) {
78 isc_throw(ParseError, loc << ": " << what);
79 } else {
80 isc_throw(ParseError, loc << " (near " << pos << "): " << what);
81 }
82}
83
84void
85ParserContext::error(string const& what)
86{
87 isc_throw(ParseError, what);
88}
89
90void
91ParserContext::fatal(string const& what)
92{
93 isc_throw(ParseError, what);
94}
95
97ParserContext::loc2pos(isc::netconf::location& loc)
98{
99 string const& file = *loc.begin.filename;
100 const uint32_t line = loc.begin.line;
101 const uint32_t pos = loc.begin.column;
102 return (isc::data::Element::Position(file, line, pos));
103}
104
105void
106ParserContext::require(string const& name,
109{
110 ConstElementPtr value = stack_.back()->get(name);
111 if (!value) {
113 "missing parameter '" << name << "' ("
114 << stack_.back()->getPosition() << ") ["
115 << contextName() << " map between "
116 << open_loc << " and " << close_loc << "]");
117 }
118}
119
120void
121ParserContext::unique(string const& name,
123{
124 ConstElementPtr value = stack_.back()->get(name);
125 if (value) {
126 if (ctx_ != NO_KEYWORDS) {
127 isc_throw(ParseError, loc << ": duplicate " << name
128 << " entries in " << contextName()
129 << " map (previous at " << value->getPosition() << ")");
130 } else {
131 isc_throw(ParseError, loc << ": duplicate " << name
132 << " entries in JSON"
133 << " map (previous at " << value->getPosition() << ")");
134 }
135 }
136}
137
138void
140{
141 cstack_.push_back(ctx_);
142 ctx_ = ctx;
143}
144
145void
147{
148 if (cstack_.empty()) {
149 fatal("unbalanced syntactic context");
150 }
151 ctx_ = cstack_.back();
152 cstack_.pop_back();
153}
154
155string const
157{
158 switch (ctx_) {
159 case NO_KEYWORDS:
160 return ("__no keywords__");
161 case CONFIG:
162 return ("toplevel");
163 case NETCONF:
164 return ("Netconf");
165 case MANAGED_SERVERS:
166 return ("managed-servers");
167 case SERVER:
168 return ("managed-servers entry");
169 case CONTROL_SOCKET:
170 return ("control-socket");
171 case SOCKET_TYPE:
172 return ("socket-type");
173 case HOOKS_LIBRARIES:
174 return ("hooks-libraries");
175 case LOGGERS:
176 return ("loggers");
177 case OUTPUT_OPTIONS:
178 return ("output-options");
179 default:
180 return ("__unknown__");
181 }
182}
183
184void
185ParserContext::warning(const isc::netconf::location& loc,
186 string const& what) {
187 ostringstream msg;
188 msg << loc << ": " << what;
190 .arg(msg.str());
191}
192
193void
194ParserContext::warnAboutExtraCommas(const isc::netconf::location& loc) {
195 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
196}
197
198} // namespace netconf
199} // 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.
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:28
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