Kea  1.5.0
d2/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 <d2/d2_parser.h>
10 #include <d2/parser_context.h>
11 #include <exceptions/exceptions.h>
12 #include <cc/data.h>
13 #include <boost/lexical_cast.hpp>
14 #include <fstream>
15 #include <limits>
16 
17 namespace isc {
18 namespace d2 {
19 
21  : sfile_(0), ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false)
22 {
23 }
24 
26 D2ParserContext::parseString(const std::string& str, ParserType parser_type)
27 {
28  scanStringBegin(str, parser_type);
29  return (parseCommon());
30 }
31 
33 D2ParserContext::parseFile(const std::string& filename, ParserType parser_type) {
34  FILE* f = fopen(filename.c_str(), "r");
35  if (!f) {
36  isc_throw(D2ParseError, "Unable to open file " << filename);
37  }
38  scanFileBegin(f, filename, parser_type);
39  return (parseCommon());
40 }
41 
43 D2ParserContext::parseCommon() {
44  isc::d2::D2Parser parser(*this);
45  // Uncomment this to get detailed parser logs.
46  // trace_parsing_ = true;
47  parser.set_debug_level(trace_parsing_);
48  try {
49  int res = parser.parse();
50  if (res != 0) {
51  isc_throw(D2ParseError, "Parser abort");
52  }
53  scanEnd();
54  }
55  catch (...) {
56  scanEnd();
57  throw;
58  }
59  if (stack_.size() == 1) {
60  return (stack_[0]);
61  } else {
62  isc_throw(D2ParseError, "Expected exactly one terminal Element expected, found "
63  << stack_.size());
64  }
65 }
66 
67 
68 void
69 D2ParserContext::error(const isc::d2::location& loc, const std::string& what)
70 {
71  isc_throw(D2ParseError, loc << ": " << what);
72 }
73 
74 void
75 D2ParserContext::error (const std::string& what)
76 {
77  isc_throw(D2ParseError, what);
78 }
79 
80 void
81 D2ParserContext::fatal (const std::string& what)
82 {
83  isc_throw(D2ParseError, what);
84 }
85 
87 D2ParserContext::loc2pos(isc::d2::location& loc)
88 {
89  const std::string& file = *loc.begin.filename;
90  const uint32_t line = loc.begin.line;
91  const uint32_t pos = loc.begin.column;
92  return (isc::data::Element::Position(file, line, pos));
93 }
94 
95 void
97 {
98  cstack_.push_back(ctx_);
99  ctx_ = ctx;
100 }
101 
102 void
104 {
105  if (cstack_.empty()) {
106  fatal("unbalanced syntactic context");
107  }
108 
109  ctx_ = cstack_.back();
110  cstack_.pop_back();
111 }
112 
113 const std::string
115 {
116  switch (ctx_) {
117  case NO_KEYWORD:
118  return ("__no keyword__");
119  case CONFIG:
120  return ("toplevel");
121  case DHCPDDNS:
122  return ("DhcpDdns");
123  case TSIG_KEY:
124  return ("tsig-key");
125  case TSIG_KEYS:
126  return ("tsig-keys");
127  case ALGORITHM:
128  return("algorithm");
129  case DIGEST_BITS:
130  return("digest-bits");
131  case SECRET:
132  return("secret");
133  case FORWARD_DDNS:
134  return("forward-ddns");
135  case REVERSE_DDNS:
136  return("reverse-ddns");
137  case DDNS_DOMAIN:
138  return("ddns-domain");
139  case DDNS_DOMAINS:
140  return("ddns-domains");
141  case DNS_SERVER:
142  return("dns-server");
143  case DNS_SERVERS:
144  return("dns-servers");
145  case LOGGING:
146  return ("Logging");
147  case LOGGERS:
148  return ("loggers");
149  case OUTPUT_OPTIONS:
150  return ("output-options");
151  case NCR_PROTOCOL:
152  return ("ncr-protocol");
153  case NCR_FORMAT:
154  return ("ncr-format");
155  default:
156  return ("__unknown__");
157  }
158 }
159 
160 };
161 };
isc::d2::D2ParserContext::fatal
static void fatal(const std::string &what)
Fatal error handler.
Definition: d2/parser_context.cc:81
isc::d2::D2ParserContext::DNS_SERVERS
@ DNS_SERVERS
Used while parsing content of Logging.
Definition: d2/parser_context.h:205
isc::d2::D2ParserContext::ParserType
ParserType
Defines currently supported scopes.
Definition: d2/parser_context.h:47
isc::d2::D2ParserContext::LOGGERS
@ LOGGERS
Used while parsing Logging/loggers structures.
Definition: d2/parser_context.h:211
isc::d2::D2ParserContext::leave
void leave()
Leave a syntactic context.
Definition: d2/parser_context.cc:103
isc::d2::D2ParserContext::ParserContext
ParserContext
Defines syntactic contexts for lexical tie-ins.
Definition: d2/parser_context.h:164
isc::d2::D2ParserContext::ALGORITHM
@ ALGORITHM
Used while parsing content of DhcpDdns/tsig-keys/digest-bits.
Definition: d2/parser_context.h:181
isc::d2::D2Parser
A Bison parser.
Definition: d2_parser.h:496
isc::d2::D2ParserContext::OUTPUT_OPTIONS
@ OUTPUT_OPTIONS
Used while parsing Logging/loggers/output_options structures.
Definition: d2/parser_context.h:214
isc::d2::D2ParserContext::NO_KEYWORD
@ NO_KEYWORD
This one is used in pure JSON mode.
Definition: d2/parser_context.h:166
isc::d2::D2ParserContext::DDNS_DOMAIN
@ DDNS_DOMAIN
Used while parsing a list of ddns-domains.
Definition: d2/parser_context.h:196
isc::d2::D2ParserContext::REVERSE_DDNS
@ REVERSE_DDNS
Used while parsing content of a ddns-domain.
Definition: d2/parser_context.h:193
isc::d2::D2ParserContext::D2ParserContext
D2ParserContext()
Default constructor.
Definition: d2/parser_context.cc:20
isc::d2::D2ParserContext::NCR_FORMAT
@ NCR_FORMAT
Used while parsing DhcpDdns/ncr-format.
Definition: d2/parser_context.h:220
isc::d2::D2ParserContext::DHCPDDNS
@ DHCPDDNS
Used while parsing content of a tsig-key.
Definition: d2/parser_context.h:172
isc::d2::D2ParserContext::stack_
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
Definition: d2/parser_context.h:80
isc::d2::D2ParserContext::CONFIG
@ CONFIG
Used while parsing content of DhcpDdns.
Definition: d2/parser_context.h:169
isc::d2::D2ParserContext::scanStringBegin
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition: d2_lexer.cc:3451
isc::d2::D2ParserContext::parseFile
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Definition: d2/parser_context.cc:33
isc::d2::D2ParserContext::contextName
const std::string contextName()
Get the syntax context name.
Definition: d2/parser_context.cc:114
isc::d2::D2ParserContext::DDNS_DOMAINS
@ DDNS_DOMAINS
Used while parsing content of a dns-server.
Definition: d2/parser_context.h:199
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::d2::D2ParserContext::LOGGING
@ LOGGING
Definition: d2/parser_context.h:208
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::d2::D2ParserContext::loc2pos
isc::data::Element::Position loc2pos(isc::d2::location &loc)
Converts bison's position to one understood by isc::data::Element.
Definition: d2/parser_context.cc:87
isc::d2::D2ParserContext::ctx_
ParserContext ctx_
Current syntactic context.
Definition: d2/parser_context.h:253
isc::d2::D2ParserContext::NCR_PROTOCOL
@ NCR_PROTOCOL
Used while parsing DhcpDdns/ncr-protocol.
Definition: d2/parser_context.h:217
isc::data::Element::Position
Represents the position of the data element within a configuration string.
Definition: data.h:88
isc::d2::D2ParserContext::error
void error(const isc::d2::location &loc, const std::string &what)
Error handler.
Definition: d2/parser_context.cc:69
isc::d2::D2ParserContext::FORWARD_DDNS
@ FORWARD_DDNS
Used while parsing content of DhcpDdns/reverse-ddns.
Definition: d2/parser_context.h:190
d2_parser.h
isc::d2::D2ParserContext::scanEnd
void scanEnd()
Method called after the last tokens are scanned.
Definition: d2_lexer.cc:3491
isc::d2::D2ParserContext::DNS_SERVER
@ DNS_SERVER
Used while parsing content of list of dns-servers.
Definition: d2/parser_context.h:202
parser_context.h
isc::d2::D2ParserContext::DIGEST_BITS
@ DIGEST_BITS
Used while parsing content of DhcpDdns/tsig-keys/secret.
Definition: d2/parser_context.h:184
isc::d2::D2ParserContext::TSIG_KEY
@ TSIG_KEY
Used while parsing a list of tsig-keys.
Definition: d2/parser_context.h:175
data.h
isc::d2::D2ParserContext::TSIG_KEYS
@ TSIG_KEYS
Used while parsing content of DhcpDdns/tsig-keys/algorithm.
Definition: d2/parser_context.h:178
exceptions.h
isc::d2::D2ParserContext::enter
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Definition: d2/parser_context.cc:96
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::d2::D2ParserContext::SECRET
@ SECRET
Used while parsing content of DhcpDdns/forward-ddns.
Definition: d2/parser_context.h:187
isc::d2::D2ParserContext::scanFileBegin
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition: d2_lexer.cc:3469
isc::d2::D2ParserContext::parseString
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
Definition: d2/parser_context.cc:26
isc::d2::D2ParseError
Evaluation error exception raised when trying to parse.
Definition: d2/parser_context.h:28