Kea 1.9.11
agent/parser_context.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2020 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 <agent/agent_parser.h>
13#include <cc/data.h>
14#include <fstream>
15#include <limits>
16
17namespace isc {
18namespace agent {
19
21 : sfile_(0), ctx_(NO_KEYWORDS), trace_scanning_(false), trace_parsing_(false)
22{
23}
24
26{
27}
28
30ParserContext::parseString(const std::string& str, ParserType parser_type)
31{
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34}
35
37ParserContext::parseFile(const std::string& filename, ParserType parser_type) {
38 FILE* f = fopen(filename.c_str(), "r");
39 if (!f) {
40 isc_throw(ParseError, "Unable to open file " << filename);
41 }
42 scanFileBegin(f, filename, parser_type);
43 return (parseCommon());
44}
45
47ParserContext::parseCommon() {
48 isc::agent::AgentParser parser(*this);
49 // Uncomment this to get detailed parser logs.
50 // trace_parsing_ = true;
51 parser.set_debug_level(trace_parsing_);
52 try {
53 int res = parser.parse();
54 if (res != 0) {
55 isc_throw(ParseError, "Parser abort");
56 }
57 scanEnd();
58 }
59 catch (...) {
60 scanEnd();
61 throw;
62 }
63 if (stack_.size() == 1) {
64 return (stack_[0]);
65 } else {
66 isc_throw(ParseError, "Expected exactly one terminal Element expected, found "
67 << stack_.size());
68 }
69}
70
71void
72ParserContext::error(const isc::agent::location& loc,
73 const std::string& what,
74 size_t pos)
75{
76 if (pos == 0) {
77 isc_throw(ParseError, loc << ": " << what);
78 } else {
79 isc_throw(ParseError, loc << " (near " << pos << "): " << what);
80 }
81}
82
83void
84ParserContext::error(const std::string& what)
85{
86 isc_throw(ParseError, what);
87}
88
89void
90ParserContext::fatal(const std::string& what)
91{
92 isc_throw(ParseError, what);
93}
94
96ParserContext::loc2pos(isc::agent::location& loc)
97{
98 const std::string& file = *loc.begin.filename;
99 const uint32_t line = loc.begin.line;
100 const uint32_t pos = loc.begin.column;
101 return (isc::data::Element::Position(file, line, pos));
102}
103
104void
105ParserContext::require(const std::string& name,
108{
109 ConstElementPtr value = stack_.back()->get(name);
110 if (!value) {
112 "missing parameter '" << name << "' ("
113 << stack_.back()->getPosition() << ") ["
114 << contextName() << " map between "
115 << open_loc << " and " << close_loc << "]");
116 }
117}
118
119void
120ParserContext::unique(const std::string& name,
122{
123 ConstElementPtr value = stack_.back()->get(name);
124 if (value) {
125 if (ctx_ != NO_KEYWORDS) {
126 isc_throw(ParseError, loc << ": duplicate " << name
127 << " entries in " << contextName()
128 << " map (previous at " << value->getPosition() << ")");
129 } else {
130 isc_throw(ParseError, loc << ": duplicate " << name
131 << " entries in JSON"
132 << " map (previous at " << value->getPosition() << ")");
133 }
134 }
135}
136
137void
139{
140 cstack_.push_back(ctx_);
141 ctx_ = ctx;
142}
143
144void
146{
147 if (cstack_.empty()) {
148 fatal("unbalanced syntactic context");
149 }
150 ctx_ = cstack_.back();
151 cstack_.pop_back();
152}
153
154const std::string
156{
157 switch (ctx_) {
158 case NO_KEYWORDS:
159 return ("__no keywords__");
160 case CONFIG:
161 return ("toplevel");
162 case AGENT:
163 return ("Control-agent");
164 case AUTHENTICATION:
165 return ("authentication");
166 case AUTH_TYPE:
167 return ("auth-type");
168 case CLIENTS:
169 return ("clients");
170 case CONTROL_SOCKETS:
171 return ("control-sockets");
172 case SERVER:
173 return ("xxx-server");
174 case SOCKET_TYPE:
175 return ("socket-type");
176 case HOOKS_LIBRARIES:
177 return ("hooks-libraries");
178 case LOGGERS:
179 return ("loggers");
180 case OUTPUT_OPTIONS:
181 return ("output-options");
182 default:
183 return ("__unknown__");
184 }
185}
186
187} // end of isc::eval namespace
188} // end of isc namespace
Define the isc::agent::parser class.
Evaluation error exception raised when trying to parse.
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.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
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:92