Kea  1.5.0
agent/parser_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-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 
9 #include <agent/parser_context.h>
10 #include <agent/agent_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 agent {
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::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 
71 
72 void
73 ParserContext::error(const isc::agent::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::agent::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
101 {
102  cstack_.push_back(ctx_);
103  ctx_ = ctx;
104 }
105 
106 void
108 {
109  if (cstack_.empty()) {
110  fatal("unbalanced syntactic context");
111  }
112  ctx_ = cstack_.back();
113  cstack_.pop_back();
114 }
115 
116 const std::string
118 {
119  switch (ctx_) {
120  case NO_KEYWORDS:
121  return ("__no keywords__");
122  case CONFIG:
123  return ("toplevel");
124  case AGENT:
125  return ("Control-agent");
126  case LOGGING:
127  return ("Logging");
128  case CONTROL_SOCKETS:
129  return ("control-sockets");
130  case SERVER:
131  return ("xxx-server");
132  case SOCKET_TYPE:
133  return ("socket-type");
134  case HOOKS_LIBRARIES:
135  return ("hooks-libraries");
136  case LOGGERS:
137  return ("loggers");
138  case OUTPUT_OPTIONS:
139  return ("output-options");
140  default:
141  return ("__unknown__");
142  }
143 }
144 
145 };
146 };
isc::agent::ParserContext::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Definition: agent/parser_context.h:180
isc::agent::ParserContext::scanStringBegin
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition: agent_lexer.cc:3635
isc::agent::ParserContext::NO_KEYWORDS
@ NO_KEYWORDS
This one is used in pure JSON mode.
Definition: agent/parser_context.h:153
isc::agent::ParserContext::parseString
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
Definition: agent/parser_context.cc:30
isc::agent::ParserContext::parseFile
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Definition: agent/parser_context.cc:37
isc::agent::ParserContext::leave
void leave()
Leave a syntactic context.
Definition: agent/parser_context.cc:107
isc::agent::ParserContext::enter
void enter(const LexerContext &ctx)
Enter a new syntactic context.
Definition: agent/parser_context.cc:100
dhcp_config_error.h
isc::agent::ParserContext::scanFileBegin
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition: agent_lexer.cc:3653
isc::agent::ParserContext::ParserContext
ParserContext()
Default constructor.
Definition: agent/parser_context.cc:20
isc::ParseError
Evaluation error exception raised when trying to parse.
Definition: dhcp_config_error.h:18
isc::agent::ParserContext::ParserType
ParserType
Defines currently supported scopes.
Definition: agent/parser_context.h:47
isc::agent::ParserContext::loc2pos
isc::data::Element::Position loc2pos(isc::agent::location &loc)
Converts bison's position to one understandable by isc::data::Element.
Definition: agent/parser_context.cc:91
isc::agent::ParserContext::AGENT
@ AGENT
Used while parsing content of Logging.
Definition: agent/parser_context.h:159
isc::agent::ParserContext::contextName
const std::string contextName()
Get the syntactic context name.
Definition: agent/parser_context.cc:117
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::agent::ParserContext::error
void error(const isc::agent::location &loc, const std::string &what)
Error handler.
Definition: agent/parser_context.cc:73
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::agent::ParserContext::LOGGERS
@ LOGGERS
Used while parsing Logging/loggers/output_options structures.
Definition: agent/parser_context.h:177
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition: data.h:88
isc::agent::ParserContext::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition: agent/parser_context.cc:85
isc::agent::ParserContext::SOCKET_TYPE
@ SOCKET_TYPE
Used while parsing Control-agent/hooks-libraries.
Definition: agent/parser_context.h:171
agent_parser.h
isc::agent::ParserContext::SERVER
@ SERVER
Used while parsing Control-agent/control-socket/*-server/socket-type.
Definition: agent/parser_context.h:168
isc::agent::ParserContext::CONFIG
@ CONFIG
Used while parsing content of Agent.
Definition: agent/parser_context.h:156
data.h
exceptions.h
isc::agent::ParserContext::LexerContext
LexerContext
Defines syntactic contexts for lexical tie-ins.
Definition: agent/parser_context.h:151
isc::agent::ParserContext::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition: agent_lexer.cc:3675
isc::agent::ParserContext::ctx_
LexerContext ctx_
Current syntactic context.
Definition: agent/parser_context.h:213
isc::agent::ParserContext::~ParserContext
virtual ~ParserContext()
destructor
Definition: agent/parser_context.cc:25
parser_context.h
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::agent::ParserContext::HOOKS_LIBRARIES
@ HOOKS_LIBRARIES
Used while parsing Logging/loggers structures.
Definition: agent/parser_context.h:174
isc::agent::ParserContext::CONTROL_SOCKETS
@ CONTROL_SOCKETS
Used while parsing Control-agent/control-socket/*-server.
Definition: agent/parser_context.h:165
isc::agent::AgentParser
A Bison parser.
Definition: agent_parser.h:495
isc::agent::ParserContext::stack_
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Definition: agent/parser_context.h:66
isc::agent::ParserContext::LOGGING
@ LOGGING
Used while parsing Control-agent/control-sockets.
Definition: agent/parser_context.h:162