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>
12#include <cc/data.h>
13#include <boost/lexical_cast.hpp>
14#include <fstream>
15#include <limits>
16
17namespace isc {
18namespace d2 {
19
21 : sfile_(0), ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false)
22{
23}
24
26D2ParserContext::parseString(const std::string& str, ParserType parser_type)
27{
28 scanStringBegin(str, parser_type);
29 return (parseCommon());
30}
31
33D2ParserContext::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
43D2ParserContext::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
68void
69D2ParserContext::error(const isc::d2::location& loc, const std::string& what)
70{
71 isc_throw(D2ParseError, loc << ": " << what);
72}
73
74void
75D2ParserContext::error (const std::string& what)
76{
78}
79
80void
81D2ParserContext::fatal (const std::string& what)
82{
84}
85
87D2ParserContext::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
95void
97{
98 cstack_.push_back(ctx_);
99 ctx_ = ctx;
100}
101
102void
104{
105 if (cstack_.empty()) {
106 fatal("unbalanced syntactic context");
107 }
108
109 ctx_ = cstack_.back();
110 cstack_.pop_back();
111}
112
113const 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};
Evaluation error exception raised when trying to parse.
ParserType
Defines currently supported scopes.
isc::data::ElementPtr parseString(const std::string &str, ParserType parser_type)
Run the parser on the string specified.
D2ParserContext()
Default constructor.
ParserContext
Defines syntactic contexts for lexical tie-ins.
@ DDNS_DOMAIN
Used while parsing a list of ddns-domains.
@ DNS_SERVERS
Used while parsing content of Logging.
@ DDNS_DOMAINS
Used while parsing content of a dns-server.
@ NCR_FORMAT
Used while parsing DhcpDdns/ncr-format.
@ NCR_PROTOCOL
Used while parsing DhcpDdns/ncr-protocol.
@ NO_KEYWORD
This one is used in pure JSON mode.
@ DHCPDDNS
Used while parsing content of a tsig-key.
@ SECRET
Used while parsing content of DhcpDdns/forward-ddns.
@ ALGORITHM
Used while parsing content of DhcpDdns/tsig-keys/digest-bits.
@ OUTPUT_OPTIONS
Used while parsing Logging/loggers/output_options structures.
@ FORWARD_DDNS
Used while parsing content of DhcpDdns/reverse-ddns.
@ TSIG_KEY
Used while parsing a list of tsig-keys.
@ REVERSE_DDNS
Used while parsing content of a ddns-domain.
@ LOGGERS
Used while parsing Logging/loggers structures.
@ TSIG_KEYS
Used while parsing content of DhcpDdns/tsig-keys/algorithm.
@ DIGEST_BITS
Used while parsing content of DhcpDdns/tsig-keys/secret.
@ CONFIG
Used while parsing content of DhcpDdns.
@ DNS_SERVER
Used while parsing content of list of dns-servers.
const std::string contextName()
Get the syntax context name.
void leave()
Leave a syntactic context.
void scanStringBegin(const std::string &str, ParserType type)
Method called before scanning starts on a string.
Definition: d2_lexer.cc:3451
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
void scanFileBegin(FILE *f, const std::string &filename, ParserType type)
Method called before scanning starts on a file.
Definition: d2_lexer.cc:3469
ParserContext ctx_
Current syntactic context.
void error(const isc::d2::location &loc, const std::string &what)
Error handler.
std::vector< isc::data::ElementPtr > stack_
JSON elements being parsed.
void enter(const ParserContext &ctx)
Enter a new syntactic context.
static void fatal(const std::string &what)
Fatal error handler.
void scanEnd()
Method called after the last tokens are scanned.
Definition: d2_lexer.cc:3491
isc::data::Element::Position loc2pos(isc::d2::location &loc)
Converts bison's position to one understood by isc::data::Element.
A Bison parser.
Definition: d2_parser.h:496
Define the isc::d2::parser class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
Defines the logger used by the top-level component of kea-dhcp-ddns.
Represents the position of the data element within a configuration string.
Definition: data.h:88