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 };
Used while parsing a list of tsig-keys.
A Bison parser.
Definition: d2_parser.h:495
Define the isc::d2::parser class.
Used while parsing Logging/loggers/output_options structures.
Used while parsing content of a dns-server.
Used while parsing content of DhcpDdns/tsig-keys/algorithm.
Used while parsing content of DhcpDdns/forward-ddns.
This one is used in pure JSON mode.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
void scanEnd()
Method called after the last tokens are scanned.
Definition: d2_lexer.cc:3491
Used while parsing content of a tsig-key.
Used while parsing Logging/loggers structures.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
static void fatal(const std::string &what)
Fatal error handler.
Used while parsing content of DhcpDdns/tsig-keys/secret.
Used while parsing content of a ddns-domain.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Used while parsing content of DhcpDdns/reverse-ddns.
void leave()
Leave a syntactic context.
Used while parsing content of Logging.
Used while parsing DhcpDdns/ncr-protocol.
ParserType
Defines currently supported scopes.
Used while parsing DhcpDdns/ncr-format.
Represents the position of the data element within a configuration string.
Definition: data.h:88
Defines the logger used by the top-level component of kea-dhcp-ddns.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
Evaluation error exception raised when trying to parse.
Used while parsing content of DhcpDdns.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition: d2_lexer.cc:3469
Used while parsing a list of ddns-domains.
ParserContext
Defines syntactic contexts for lexical tie-ins.
D2ParserContext()
Default constructor.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Used while parsing content of DhcpDdns/tsig-keys/digest-bits.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition: d2_lexer.cc:3451
ParserContext ctx_
Current syntactic context.
Used while parsing content of list of dns-servers.
isc::data::Element::Position loc2pos(isc::d2::location &loc)
Converts bison's position to one understood by isc::data::Element.
void error(const isc::d2::location &loc, const std::string &what)
Error handler.
const std::string contextName()
Get the syntax context name.