-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathParser.cpp
More file actions
339 lines (277 loc) · 10.2 KB
/
Parser.cpp
File metadata and controls
339 lines (277 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "Parser.h"
#include "CheckerListener.h"
#include "ExpanderListener.h"
#include "ExternalResolverListener.h"
#include "ReferencesListener.h"
#include "LocalResolverListener.h"
#include "SymbolsListener.h"
#include "TreeDecorations.h"
#include "UndefinedListener.h"
#include "UnresolvableListener.h"
#include "DotenvLexer.h"
#include "DotenvParser.h"
#include "LineLexer.h"
#include "LineParser.h"
#include "environ.h"
#include "errors.h"
#include <utility>
using namespace antlr4;
using namespace dotenv;
using namespace std;
dotenv::Parser::Parser():
unresolved(0)
{
}
void dotenv::Parser::parse(istream& is, const bool overwrite, const bool interpolate)
{
// Some initialization in case a parser is reused
unresolved = 0;
symbols_table.clear();
errors::clear();
parse_dotenv(is, overwrite);
// Interpolation is the resolution of nested variables
if (interpolate)
{
parse_line();
resolve_vars();
}
expand_escape();
register_env(overwrite);
errors::flush();
}
void dotenv::Parser::parse_dotenv(istream& is, const bool overwrite)
{
ANTLRInputStream input(is);
DotenvLexer lexer(&input);
CommonTokenStream tokens(&lexer);
tokens.fill();
DotenvParser parser(&tokens);
tree::ParseTree* tree = parser.dotenv();
// Decorations on the dotenv parse tree for storing several info
// throughout listeners
TreeDecorations dotenv_decorations;
// Check for errors on the tree
CheckerListener checker_listener(dotenv_decorations);
walker.walk(&checker_listener, tree);
// Extract raw key-value pairs
SymbolsListener symbols_listener(overwrite, symbols_table, dotenv_decorations);
walker.walk(&symbols_listener, tree);
}
void dotenv::Parser::parse_line()
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
// std::pair.second returns a copy of the second element, and a
// reference is needed to check the evolution of the symbol's state,
// so take it directly from the symbols table
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// If the symbol is local (defined in the .env file being treated),
// check for dependency on other symbols
if (record.local())
{
ReferencesListener references_listener(key, references_table, symbols_table);
walk_line(record.value(), references_listener);
// If after the check the symbol has dependency on other symbols,
// take not of it for later resolving
if (not record.complete())
{
++unresolved;
}
}
}
}
void dotenv::Parser::resolve_vars()
{
// First resolve external variables, since they will all be solved on a
// single iteration given that they are not resolved nor expanded
resolve_external_vars();
// External undefined vars will not be resolved in the loop, so solve them
// at the beginning so other variables depending on them can be resolved
report_undefined_vars();
resolve_undefined_vars();
// If there are no circular dependencies, each iteration should at
// least resolve one variable, so the loop is expected to finish
size_t old_unresolved;
while (unresolved > 0)
{
old_unresolved = unresolved;
resolve_local_vars();
// If there are no new variables resolved in an iteration, it means
// there is at least one circular dependency and thus it cannot be
// resolved
// Solve them by erasing the references on the string
if (old_unresolved == unresolved)
{
break;
}
}
report_unresolvable_vars();
resolve_unresolvable_vars();
}
void dotenv::Parser::expand_escape()
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
// std::pair.second returns a copy of the second element, and a
// reference is needed to check the evolution of the symbol's state,
// so take it directly from the symbols table
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// Expand only escaped sequences in local symbols
if (record.local())
{
ExpanderListener expander_listener(key, symbols_table);
walk_line(record.value(), expander_listener);
}
}
}
void dotenv::Parser::register_env(const bool overwrite) const
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// Register only local symbols (those defined in the .env file)
if (record.local())
{
setenv(key, record.value(), overwrite);
}
}
}
void dotenv::Parser::resolve_local_vars()
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
// std::pair.second returns a copy of the second element, and a
// reference is needed to check the evolution of the symbol's state,
// so take it directly from the symbols table
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// If the symbol is local and is not yet resolved, try to resolve
// it by walking through its dependencies again
if (record.local() and not record.complete())
{
LocalResolverListener resolver_listener(key, symbols_table);
walk_line(record.value(), resolver_listener);
// If the symbol is now completed, note it
if (record.complete())
{
--unresolved;
}
}
}
}
void dotenv::Parser::resolve_external_vars()
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
// std::pair.second returns a copy of the second element, and a
// reference is needed to check the evolution of the symbol's state,
// so take it directly from the symbols table
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// If the symbol is local and is not yet resolved, try to resolve
// it by walking through its dependencies again
if (record.local() and not record.complete())
{
ExternalResolverListener resolver_listener(key, symbols_table);
walk_line(record.value(), resolver_listener);
// If the symbol is now completed, note it
if (record.complete())
{
--unresolved;
}
}
}
}
void dotenv::Parser::resolve_undefined_vars()
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
// std::pair.second returns a copy of the second element, and a
// reference is needed to check the evolution of the symbol's state,
// so take it directly from the symbols table
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// If the symbol is local and is not yet resolved, try to resolve
// it by walking through its dependencies again
if (record.local() and not record.complete())
{
UndefinedListener undefined_listener(key, symbols_table);
walk_line(record.value(), undefined_listener);
// If the symbol is now completed, note it
if (record.complete())
{
--unresolved;
}
}
}
}
void dotenv::Parser::resolve_unresolvable_vars()
{
for (const pair<string, SymbolRecord>& symbol: symbols_table)
{
// std::pair.second returns a copy of the second element, and a
// reference is needed to check the evolution of the symbol's state,
// so take it directly from the symbols table
const string& key = symbol.first;
const SymbolRecord& record = symbols_table.at(key);
// If the symbol is local and is not yet resolved, try to resolve
// it by walking through its dependencies again
if (record.local() and not record.complete())
{
UnresolvableListener unresolvable_listener(key, symbols_table);
walk_line(record.value(), unresolvable_listener);
// If the symbol is now completed, note it
if (record.complete())
{
--unresolved;
}
}
}
}
void dotenv::Parser::report_undefined_vars()
{
// Iterate over all the original existing references (for having access to
// original location data)
for (const pair<string, ReferenceRecord>& reference: references_table)
{
const string& ref_key = reference.first;
const ReferenceRecord& reference_record = reference.second;
const SymbolRecord& symbol_record = symbols_table.at(ref_key);
// If after all the process the reference symbol is still not resolved,
// it means it is part of a circular reference
if (not symbol_record.complete() and not symbol_record.local())
{
errors::undefined_reference_error(ref_key, reference_record.line(), reference_record.pos());
}
}
}
void dotenv::Parser::report_unresolvable_vars()
{
// Iterate over all the original existing references (for having access to
// original location data)
for (const pair<string, ReferenceRecord>& reference: references_table)
{
const string& ref_key = reference.first;
const ReferenceRecord& reference_record = reference.second;
const SymbolRecord& symbol_record = symbols_table.at(ref_key);
// If after all the process the reference symbol is still not resolved,
// it means it is part of a circular reference
if (not symbol_record.complete() and symbol_record.local())
{
errors::circular_reference_error(ref_key, reference_record.line(), reference_record.pos());
}
}
}
void dotenv::Parser::walk_line(const string& line, tree::ParseTreeListener& listener)
{
ANTLRInputStream input(line);
LineLexer lexer(&input);
CommonTokenStream tokens(&lexer);
tokens.fill();
LineParser parser(&tokens);
tree::ParseTree* tree = parser.line();
walker.walk(&listener, tree);
}