Kea 3.3.3
netconf/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2026 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#include <util/str.h>
15
16#include <fstream>
17#include <limits>
18#include <sstream>
19
20namespace isc {
21namespace netconf {
22
24 : sfile_(0), ctx_(NO_KEYWORDS), trace_scanning_(false), trace_parsing_(false)
25{
26}
27
31
33ParserContext::parseString(string const& str, ParserType parser_type)
34{
35 scanStringBegin(str, parser_type);
36 return (parseCommon());
37}
38
40ParserContext::parseFile(string const& filename, ParserType parser_type) {
41 FILE* f = fopen(filename.c_str(), "r");
42 if (!f) {
43 isc_throw(ParseError, "Unable to open file " << filename);
44 }
45 scanFileBegin(f, filename, parser_type);
46 return (parseCommon());
47}
48
50ParserContext::parseCommon() {
51 isc::netconf::NetconfParser parser(*this);
52 // Uncomment this to get detailed parser logs.
53 // trace_parsing_ = true;
54 parser.set_debug_level(trace_parsing_);
55 try {
56 int res = parser.parse();
57 if (res != 0) {
58 isc_throw(ParseError, "Parser abort");
59 }
60 scanEnd();
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, found "
69 << stack_.size());
70 }
71}
72
73void
74ParserContext::error(const isc::netconf::location& loc,
75 string const& what,
76 size_t pos)
77{
78 std::string escaped = util::str::escapeNulls(what);
79 if (pos == 0) {
80 isc_throw(ParseError, loc << ": " << escaped);
81 } else {
82 isc_throw(ParseError, loc << " (near " << pos << "): " << escaped);
83 }
84}
85
86void
87ParserContext::error(string const& what)
88{
90}
91
92void
93ParserContext::fatal(string const& what)
94{
96}
97
99ParserContext::loc2pos(isc::netconf::location& loc)
100{
101 string const& file = *loc.begin.filename;
102 const uint32_t line = loc.begin.line;
103 const uint32_t pos = loc.begin.column;
104 return (isc::data::Element::Position(file, line, pos));
105}
106
107void
108ParserContext::require(string const& name,
111{
112 ConstElementPtr value = stack_.back()->get(name);
113 if (!value) {
115 "missing parameter '" << name << "' ("
116 << stack_.back()->getPosition() << ") ["
117 << contextName() << " map between "
118 << open_loc << " and " << close_loc << "]");
119 }
120}
121
122void
123ParserContext::unique(string const& name,
125{
126 ConstElementPtr value = stack_.back()->get(name);
127 if (value) {
128 if (ctx_ != NO_KEYWORDS) {
129 isc_throw(ParseError, loc << ": duplicate " << name
130 << " entries in " << contextName()
131 << " map (previous at " << value->getPosition() << ")");
132 } else {
133 isc_throw(ParseError, loc << ": duplicate " << name
134 << " entries in JSON"
135 << " map (previous at " << value->getPosition() << ")");
136 }
137 }
138}
139
140void
142{
143 cstack_.push_back(ctx_);
144 ctx_ = ctx;
145}
146
147void
149{
150 if (cstack_.empty()) {
151 fatal("unbalanced syntactic context");
152 }
153 ctx_ = cstack_.back();
154 cstack_.pop_back();
155}
156
157string const
159{
160 switch (ctx_) {
161 case NO_KEYWORDS:
162 return ("__no keywords__");
163 case CONFIG:
164 return ("toplevel");
165 case NETCONF:
166 return ("Netconf");
167 case MANAGED_SERVERS:
168 return ("managed-servers");
169 case SERVER:
170 return ("managed-servers entry");
171 case CONTROL_SOCKET:
172 return ("control-socket");
173 case SOCKET_TYPE:
174 return ("socket-type");
175 case HOOKS_LIBRARIES:
176 return ("hooks-libraries");
177 case LOGGERS:
178 return ("loggers");
179 case OUTPUT_OPTIONS:
180 return ("output-options");
181 default:
182 return ("__unknown__");
183 }
184}
185
186void
187ParserContext::warning(const isc::netconf::location& loc,
188 string const& what) {
189 ostringstream msg;
190 msg << loc << ": " << what;
192 .arg(msg.str());
193}
194
195void
196ParserContext::warnAboutExtraCommas(const isc::netconf::location& loc) {
197 warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
198}
199
200} // namespace netconf
201} // 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:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
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
std::string escapeNulls(std::string str)
Escape null characters.
Definition str.cc:442
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:107