forked from Caltech-CS3-2014/nginx-configparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_parser.cc
239 lines (225 loc) · 6.97 KB
/
config_parser.cc
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
// An nginx config file parser.
//
// See:
// http://wiki.nginx.org/Configuration
// http://blog.martinfjordvald.com/2010/07/nginx-primer/
//
// How Nginx does it:
// http://lxr.nginx.org/source/src/core/ngx_conf_file.c
#include <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <stack>
#include <string>
#include <vector>
#include "config_parser.h"
std::string NginxConfig::ToString(int depth) {
std::string serialized_config;
for (const auto& statement : statements_) {
serialized_config.append(statement->ToString(depth));
}
return serialized_config;
}
std::string NginxConfigStatement::ToString(int depth) {
std::string serialized_statement;
for (int i = 0; i < depth; ++i) {
serialized_statement.append(" ");
}
for (unsigned int i = 0; i < tokens_.size(); ++i) {
if (i != 0) {
serialized_statement.append(" ");
}
serialized_statement.append(tokens_[i]);
}
if (child_block_.get() != nullptr) {
serialized_statement.append(" {\n");
serialized_statement.append(child_block_->ToString(depth + 1));
for (int i = 0; i < depth; ++i) {
serialized_statement.append(" ");
}
serialized_statement.append("}");
} else {
serialized_statement.append(";");
}
serialized_statement.append("\n");
return serialized_statement;
}
const char* NginxConfigParser::TokenTypeAsString(TokenType type) {
switch (type) {
case TOKEN_TYPE_START: return "TOKEN_TYPE_START";
case TOKEN_TYPE_NORMAL: return "TOKEN_TYPE_NORMAL";
case TOKEN_TYPE_START_BLOCK: return "TOKEN_TYPE_START_BLOCK";
case TOKEN_TYPE_END_BLOCK: return "TOKEN_TYPE_END_BLOCK";
case TOKEN_TYPE_COMMENT: return "TOKEN_TYPE_COMMENT";
case TOKEN_TYPE_STATEMENT_END: return "TOKEN_TYPE_STATEMENT_END";
case TOKEN_TYPE_EOF: return "TOKEN_TYPE_EOF";
case TOKEN_TYPE_ERROR: return "TOKEN_TYPE_ERROR";
default: return "Unknown token type";
}
}
NginxConfigParser::TokenType NginxConfigParser::ParseToken(std::istream* input,
std::string* value) {
TokenParserState state = TOKEN_STATE_INITIAL_WHITESPACE;
while (input->good()) {
const char c = input->get();
if (!input->good()) {
break;
}
switch (state) {
case TOKEN_STATE_INITIAL_WHITESPACE:
switch (c) {
case '{':
*value = c;
return TOKEN_TYPE_START_BLOCK;
case '}':
*value = c;
return TOKEN_TYPE_END_BLOCK;
case '#':
*value = c;
state = TOKEN_STATE_TOKEN_TYPE_COMMENT;
continue;
case '"':
*value = c;
state = TOKEN_STATE_DOUBLE_QUOTE;
continue;
case '\'':
*value = c;
state = TOKEN_STATE_SINGLE_QUOTE;
continue;
case ';':
*value = c;
return TOKEN_TYPE_STATEMENT_END;
case ' ':
case '\t':
case '\n':
case '\r':
continue;
default:
*value += c;
state = TOKEN_STATE_TOKEN_TYPE_NORMAL;
continue;
}
case TOKEN_STATE_SINGLE_QUOTE:
// TODO: the end of a quoted token should be followed by whitespace.
// TODO: Maybe also define a QUOTED_STRING token type.
// TODO: Allow for backslash-escaping within strings.
*value += c;
if (c == '\'') {
return TOKEN_TYPE_NORMAL;
}
continue;
case TOKEN_STATE_DOUBLE_QUOTE:
*value += c;
if (c == '"') {
return TOKEN_TYPE_NORMAL;
}
continue;
case TOKEN_STATE_TOKEN_TYPE_COMMENT:
if (c == '\n' || c == '\r') {
return TOKEN_TYPE_COMMENT;
}
*value += c;
continue;
case TOKEN_STATE_TOKEN_TYPE_NORMAL:
if (c == ' ' || c == '\t' || c == '\n' || c == '\t' ||
c == ';' || c == '{' || c == '}') {
input->unget();
return TOKEN_TYPE_NORMAL;
}
*value += c;
continue;
}
}
// If we get here, we reached the end of the file.
if (state == TOKEN_STATE_SINGLE_QUOTE ||
state == TOKEN_STATE_DOUBLE_QUOTE) {
return TOKEN_TYPE_ERROR;
}
return TOKEN_TYPE_EOF;
}
bool NginxConfigParser::Parse(std::istream* config_file, NginxConfig* config) {
std::stack<NginxConfig*> config_stack;
config_stack.push(config);
TokenType last_token_type = TOKEN_TYPE_START;
TokenType token_type;
while (true) {
std::string token;
token_type = ParseToken(config_file, &token);
printf ("%s: %s\n", TokenTypeAsString(token_type), token.c_str());
if (token_type == TOKEN_TYPE_ERROR) {
break;
}
if (token_type == TOKEN_TYPE_COMMENT) {
// Skip comments.
continue;
}
if (token_type == TOKEN_TYPE_START) {
// Error.
break;
} else if (token_type == TOKEN_TYPE_NORMAL) {
if (last_token_type == TOKEN_TYPE_START ||
last_token_type == TOKEN_TYPE_STATEMENT_END ||
last_token_type == TOKEN_TYPE_START_BLOCK ||
last_token_type == TOKEN_TYPE_END_BLOCK ||
last_token_type == TOKEN_TYPE_NORMAL) {
if (last_token_type != TOKEN_TYPE_NORMAL) {
config_stack.top()->statements_.emplace_back(
new NginxConfigStatement);
}
config_stack.top()->statements_.back().get()->tokens_.push_back(
token);
} else {
// Error.
break;
}
} else if (token_type == TOKEN_TYPE_STATEMENT_END) {
if (last_token_type != TOKEN_TYPE_NORMAL) {
// Error.
break;
}
} else if (token_type == TOKEN_TYPE_START_BLOCK) {
if (last_token_type != TOKEN_TYPE_NORMAL) {
// Error.
break;
}
NginxConfig* const new_config = new NginxConfig;
config_stack.top()->statements_.back().get()->child_block_.reset(
new_config);
config_stack.push(new_config);
} else if (token_type == TOKEN_TYPE_END_BLOCK) {
if (last_token_type != TOKEN_TYPE_STATEMENT_END) {
// Error.
break;
}
config_stack.pop();
} else if (token_type == TOKEN_TYPE_EOF) {
if (last_token_type != TOKEN_TYPE_STATEMENT_END &&
last_token_type != TOKEN_TYPE_END_BLOCK) {
// Error.
break;
}
return true;
} else {
// Error. Unknown token.
break;
}
last_token_type = token_type;
}
printf ("Bad transition from %s to %s\n",
TokenTypeAsString(last_token_type),
TokenTypeAsString(token_type));
return false;
}
bool NginxConfigParser::Parse(const char* file_name, NginxConfig* config) {
std::ifstream config_file;
config_file.open(file_name);
if (!config_file.good()) {
printf ("Failed to open config file: %s\n", file_name);
return false;
}
const bool return_value =
Parse(dynamic_cast<std::istream*>(&config_file), config);
config_file.close();
return return_value;
}