1 // Copyright (C) 2017-2021 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
D2ParserContext()20 D2ParserContext::D2ParserContext()
21 : sfile_(0), ctx_(NO_KEYWORD), trace_scanning_(false), trace_parsing_(false)
22 {
23 }
24
~D2ParserContext()25 D2ParserContext::~D2ParserContext()
26 {
27 }
28
29 isc::data::ElementPtr
parseString(const std::string & str,ParserType parser_type)30 D2ParserContext::parseString(const std::string& str, ParserType parser_type)
31 {
32 scanStringBegin(str, parser_type);
33 return (parseCommon());
34 }
35
36 isc::data::ElementPtr
parseFile(const std::string & filename,ParserType parser_type)37 D2ParserContext::parseFile(const std::string& filename, ParserType parser_type) {
38 FILE* f = fopen(filename.c_str(), "r");
39 if (!f) {
40 isc_throw(D2ParseError, "Unable to open file " << filename);
41 }
42 scanFileBegin(f, filename, parser_type);
43 return (parseCommon());
44 }
45
46 isc::data::ElementPtr
parseCommon()47 D2ParserContext::parseCommon() {
48 isc::d2::D2Parser 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(D2ParseError, "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(D2ParseError, "Expected exactly one terminal Element expected, found "
67 << stack_.size());
68 }
69 }
70
71 void
error(const isc::d2::location & loc,const std::string & what,size_t pos)72 D2ParserContext::error(const isc::d2::location& loc,
73 const std::string& what,
74 size_t pos)
75 {
76 if (pos == 0) {
77 isc_throw(D2ParseError, loc << ": " << what);
78 } else {
79 isc_throw(D2ParseError, loc << " (near " << pos << "): " << what);
80 }
81 }
82
83 void
error(const std::string & what)84 D2ParserContext::error (const std::string& what)
85 {
86 isc_throw(D2ParseError, what);
87 }
88
89 void
fatal(const std::string & what)90 D2ParserContext::fatal (const std::string& what)
91 {
92 isc_throw(D2ParseError, what);
93 }
94
95 isc::data::Element::Position
loc2pos(isc::d2::location & loc)96 D2ParserContext::loc2pos(isc::d2::location& loc)
97 {
98 const std::string& file = *loc.begin.filename;
99 const uint32_t line = loc.begin.line;
100 const uint32_t pos = loc.begin.column;
101 return (isc::data::Element::Position(file, line, pos));
102 }
103
104 void
require(const std::string & name,isc::data::Element::Position open_loc,isc::data::Element::Position close_loc)105 D2ParserContext::require(const std::string& name,
106 isc::data::Element::Position open_loc,
107 isc::data::Element::Position close_loc)
108 {
109 ConstElementPtr value = stack_.back()->get(name);
110 if (!value) {
111 isc_throw(D2ParseError,
112 "missing parameter '" << name << "' ("
113 << stack_.back()->getPosition() << ") ["
114 << contextName() << " map between "
115 << open_loc << " and " << close_loc << "]");
116 }
117 }
118
119 void
unique(const std::string & name,isc::data::Element::Position loc)120 D2ParserContext::unique(const std::string& name,
121 isc::data::Element::Position loc)
122 {
123 ConstElementPtr value = stack_.back()->get(name);
124 if (value) {
125 if (ctx_ != NO_KEYWORD) {
126 isc_throw(D2ParseError, loc << ": duplicate " << name
127 << " entries in " << contextName()
128 << " map (previous at " << value->getPosition() << ")");
129 } else {
130 isc_throw(D2ParseError, loc << ": duplicate " << name
131 << " entries in JSON"
132 << " map (previous at " << value->getPosition() << ")");
133 }
134 }
135 }
136
137 void
enter(const ParserContext & ctx)138 D2ParserContext::enter(const ParserContext& ctx)
139 {
140 cstack_.push_back(ctx_);
141 ctx_ = ctx;
142 }
143
144 void
leave()145 D2ParserContext::leave()
146 {
147 if (cstack_.empty()) {
148 fatal("unbalanced syntactic context");
149 }
150
151 ctx_ = cstack_.back();
152 cstack_.pop_back();
153 }
154
155 const std::string
contextName()156 D2ParserContext::contextName()
157 {
158 switch (ctx_) {
159 case NO_KEYWORD:
160 return ("__no keyword__");
161 case CONFIG:
162 return ("toplevel");
163 case DHCPDDNS:
164 return ("DhcpDdns");
165 case TSIG_KEY:
166 return ("tsig-key");
167 case TSIG_KEYS:
168 return ("tsig-keys");
169 case ALGORITHM:
170 return("algorithm");
171 case DIGEST_BITS:
172 return("digest-bits");
173 case SECRET:
174 return("secret");
175 case FORWARD_DDNS:
176 return("forward-ddns");
177 case REVERSE_DDNS:
178 return("reverse-ddns");
179 case DDNS_DOMAIN:
180 return("ddns-domain");
181 case DDNS_DOMAINS:
182 return("ddns-domains");
183 case DNS_SERVER:
184 return("dns-server");
185 case DNS_SERVERS:
186 return("dns-servers");
187 case CONTROL_SOCKET:
188 return("control-socket");
189 case LOGGERS:
190 return ("loggers");
191 case OUTPUT_OPTIONS:
192 return ("output-options");
193 case NCR_PROTOCOL:
194 return ("ncr-protocol");
195 case NCR_FORMAT:
196 return ("ncr-format");
197 case HOOKS_LIBRARIES:
198 return ("hooks-libraries");
199 default:
200 return ("__unknown__");
201 }
202 }
203
204 }
205 }
206