Kea  1.5.0
netconf/parser_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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 <netconf/netconf_parser.h>
11 #include <exceptions/exceptions.h>
12 //#include <cc/dhcp_config_error.h>
13 #include <cc/data.h>
14 #include <fstream>
15 #include <limits>
16 
17 namespace isc {
18 namespace netconf {
19 
21  : sfile_(0), ctx_(NO_KEYWORDS), trace_scanning_(false), trace_parsing_(false)
22 {
23 }
24 
26 {
27 }
28 
30 ParserContext::parseString(const std::string& str, ParserType parser_type)
31 {
32  scanStringBegin(str, parser_type);
33  return (parseCommon());
34 }
35 
37 ParserContext::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 
47 ParserContext::parseCommon() {
48  isc::netconf::NetconfParser 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 
71 
72 void
73 ParserContext::error(const isc::netconf::location& loc, const std::string& what)
74 {
75  isc_throw(ParseError, loc << ": " << what);
76 }
77 
78 void
79 ParserContext::error(const std::string& what)
80 {
81  isc_throw(ParseError, what);
82 }
83 
84 void
85 ParserContext::fatal(const std::string& what)
86 {
87  isc_throw(ParseError, what);
88 }
89 
91 ParserContext::loc2pos(isc::netconf::location& loc)
92 {
93  const std::string& file = *loc.begin.filename;
94  const uint32_t line = loc.begin.line;
95  const uint32_t pos = loc.begin.column;
96  return (isc::data::Element::Position(file, line, pos));
97 }
98 
99 void
100 ParserContext::require(const std::string& name,
103 {
104  ConstElementPtr value = stack_.back()->get(name);
105  if (!value) {
107  "missing parameter '" << name << "' ("
108  << stack_.back()->getPosition() << ") ["
109  << contextName() << " map between "
110  << open_loc << " and " << close_loc << "]");
111  }
112 }
113 
114 void
116 {
117  cstack_.push_back(ctx_);
118  ctx_ = ctx;
119 }
120 
121 void
123 {
124  if (cstack_.empty()) {
125  fatal("unbalanced syntactic context");
126  }
127  ctx_ = cstack_.back();
128  cstack_.pop_back();
129 }
130 
131 const std::string
133 {
134  switch (ctx_) {
135  case NO_KEYWORDS:
136  return ("__no keywords__");
137  case CONFIG:
138  return ("toplevel");
139  case NETCONF:
140  return ("Netconf");
141  case LOGGING:
142  return ("Logging");
143  case MANAGED_SERVERS:
144  return ("managed-servers");
145  case SERVER:
146  return ("managed-servers/*");
147  case CONTROL_SOCKET:
148  return ("control-socket");
149  case SOCKET_TYPE:
150  return ("socket-type");
151  case HOOKS_LIBRARIES:
152  return ("hooks-libraries");
153  case LOGGERS:
154  return ("loggers");
155  case OUTPUT_OPTIONS:
156  return ("output-options");
157  default:
158  return ("__unknown__");
159  }
160 }
161 
162 };
163 };
isc::netconf::ParserContext::enter
void enter(const LexerContext &ctx)
Enter a new syntactic context.
Definition: netconf/parser_context.cc:115
isc::netconf::ParserContext::parseString
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
Definition: netconf/parser_context.cc:30
isc::netconf::ParserContext::MANAGED_SERVERS
@ MANAGED_SERVERS
Used while parsing Netconf/managed-servers.
Definition: netconf/parser_context.h:179
parser_context.h
isc::netconf::ParserContext::stack_
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Definition: netconf/parser_context.h:67
isc::netconf::ParserContext::LOGGING
@ LOGGING
Definition: netconf/parser_context.h:176
isc::netconf::ParserContext::LexerContext
LexerContext
Defines syntactic contexts for lexical tie-ins.
Definition: netconf/parser_context.h:165
isc::netconf::ParserContext::NETCONF
@ NETCONF
Used while parsing content of Logging.
Definition: netconf/parser_context.h:173
isc::netconf::ParserContext::ParserType
ParserType
Defines currently supported scopes.
Definition: netconf/parser_context.h:48
isc::ParseError
Evaluation error exception raised when trying to parse.
Definition: dhcp_config_error.h:18
isc::netconf::ParserContext::SERVER
@ SERVER
Used while parsing Netconf/manages-servers/*‍/control-socket.
Definition: netconf/parser_context.h:182
isc::netconf::ParserContext::SOCKET_TYPE
@ SOCKET_TYPE
Used while parsing Netconf/hooks-libraries.
Definition: netconf/parser_context.h:188
isc::netconf::ParserContext::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition: netconf/parser_context.cc:85
isc::netconf::ParserContext::HOOKS_LIBRARIES
@ HOOKS_LIBRARIES
Used while parsing Logging/loggers structures.
Definition: netconf/parser_context.h:191
isc::netconf::ParserContext::require
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.
Definition: netconf/parser_context.cc:100
isc::netconf::ParserContext::~ParserContext
virtual ~ParserContext()
destructor
Definition: netconf/parser_context.cc:25
isc::netconf::ParserContext::ParserContext
ParserContext()
Default constructor.
Definition: netconf/parser_context.cc:20
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::netconf::ParserContext::NO_KEYWORDS
@ NO_KEYWORDS
This one is used in pure JSON mode.
Definition: netconf/parser_context.h:167
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::netconf::ParserContext::error
void error(const isc::netconf::location &loc, const std::string &what)
Error handler.
Definition: netconf/parser_context.cc:73
isc::netconf::ParserContext::ctx_
LexerContext ctx_
Current syntactic context.
Definition: netconf/parser_context.h:230
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition: data.h:88
isc::netconf::ParserContext::scanFileBegin
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition: netconf_lexer.cc:3803
isc::netconf::ParserContext::contextName
const std::string contextName()
Get the syntactic context name.
Definition: netconf/parser_context.cc:132
isc::netconf::ParserContext::scanStringBegin
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition: netconf_lexer.cc:3785
isc::netconf::ParserContext::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Definition: netconf/parser_context.h:197
isc::netconf::ParserContext::CONTROL_SOCKET
@ CONTROL_SOCKET
Used while parsing Netconf/managed-servers/*‍/control-socket/socket-type.
Definition: netconf/parser_context.h:185
isc::netconf::ParserContext::loc2pos
isc::data::Element::Position loc2pos(isc::netconf::location &loc)
Converts bison's position to one understandable by isc::data::Element.
Definition: netconf/parser_context.cc:91
data.h
exceptions.h
isc::netconf::NetconfParser
A Bison parser.
Definition: netconf_parser.h:495
isc::netconf::ParserContext::LOGGERS
@ LOGGERS
Used while parsing Logging/loggers/output_options structures.
Definition: netconf/parser_context.h:194
netconf_parser.h
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::netconf::ParserContext::CONFIG
@ CONFIG
Used while parsing content of Netconf.
Definition: netconf/parser_context.h:170
isc::netconf::ParserContext::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition: netconf_lexer.cc:3825
isc::netconf::ParserContext::leave
void leave()
Leave a syntactic context.
Definition: netconf/parser_context.cc:122
isc::netconf::ParserContext::parseFile
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Definition: netconf/parser_context.cc:37