-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.cpp
More file actions
313 lines (236 loc) · 9.11 KB
/
processor.cpp
File metadata and controls
313 lines (236 loc) · 9.11 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
// C++ Macro Processor
// TO DO :
// - program /expandafter
// - program /defgroup
// /defgroup{hello, obama, snow}{cow, meat, likes you}
// - program /undefgroup
#include <iostream>
#include <string>
#include "hashmap.cpp"
#define CAPACITY 50000
#define INVALID "@10NULL01@"
#define EMPTY ""
enum State {
plaintext,
reading_macro,
reading_arg,
};
// /def{hello}{/def{obama}{44}}
int process(std::string input, State s);
#define MACRO '/'
std::string output_buffer;
hashmap * user_macros = new hashmap(CAPACITY);
void clear_expansion(string *macro, std::vector<string> *args) {
for (int i = 0; i < (*args).size(); i++) {
(*args)[i].clear();
}
(*macro).clear();
}
int obtain_num_of_expected_args(string macro) {
int expected_num_of_args;
if (macro == "def") {
expected_num_of_args = 2;
} else if (macro == "undef") {
expected_num_of_args = 1;
} else if (macro == "ifdef") {
expected_num_of_args = 3;
} else if (macro == "if") {
expected_num_of_args = 3;
} else if (macro == "defgroup") {
expected_num_of_args = 2;
} else if (macro == "undefgrouo") {
expected_num_of_args = 2;
} else {
// user macro
expected_num_of_args = 1;
}
return expected_num_of_args;
}
string disperse_parameter(string input, string param) {
int len = input.length();
string new_input;
for (int i = 0; i < len; i++) {
if (input[i] == '#') {
new_input += param;
} else {
new_input += input[i];
}
}
return new_input;
}
string evaluate_macro(string *macro, std::vector<string> *args) {
if (*macro == "def") {
// add the mapping to the table.
user_macros->add_mapping((*args)[2], (*args)[1]);
} else if (*macro == "undef") {
// remove the mapping
user_macros->remove_mapping((*args)[1]);
} else if (*macro == "ifdef") {
// if the macro is mapped, then process THEN plaintext, otherwise process ELSE as plaintext
if (user_macros->is_mapped((*args)[3])) {
return (*args)[2];
} else {
return (*args)[1];
}
} else if (*macro == "if") {
if ((*args)[3].empty()) {
return (*args)[2];
} else {
return (*args)[1];
}
} else {
// otherwise, it is a user_macro
string user_macro_def = user_macros->get_mapping(*macro);
if (user_macro_def != INVALID) {
// the user_macro definiton is processed, with the parameter passed.
string correct_mapping = disperse_parameter(user_macro_def, (*args)[1]);
return correct_mapping;
} else {
output_buffer.clear();
cout << "[Invalid Macro Call] \"" << (*macro) << "\" is not defined";
return INVALID;
}
}
return EMPTY;
}
int process(string input, State s) {
string macro_sub_buffer;
vector<string> args;
int expected_num_of_args;
string macro_expansion;
// the string iself that is paused .
string paused_str;
int paused_str_index;
// a pointer to the current string that we are processing.
string *current_str = &input;
int args_balanced = 0;
// def{{hello}}{/def{}}
for (int i = 0; i < (*current_str).length(); i++) {
char p_letter = (*current_str)[i];
switch(s) {
case plaintext:
if (p_letter == MACRO) {
s = reading_macro;
} else {
if (!isalnum(p_letter) && !isspace(p_letter)) {
// legal, non-macro letters must be a space or alphanumeric
cout << "[Invalid Plaintext Character] : " << p_letter << " is not alphanumeric.";
output_buffer.clear();
return 1;
}
output_buffer += p_letter;
}
break;
case reading_macro:
if (p_letter == '{') {
// evaluluate which macro it is and set the correct number of args.
args_balanced += 1;
expected_num_of_args = obtain_num_of_expected_args(macro_sub_buffer);
if (args.empty()) {
args.resize(3);
}
if (expected_num_of_args > args.size() - 1) {
args.resize(args.size() * 2);
}
s = reading_arg;
} else if (isspace(p_letter)) {
// we are done processing the macro, we ran into a space. Spaces of any sort are illegal in macros and denote its end.
output_buffer += p_letter;
string evaluated_macro = evaluate_macro(¯o_sub_buffer, &args);
clear_expansion(¯o_sub_buffer, &args);
if (evaluated_macro == INVALID) {
return 1;
} else if (evaluated_macro != EMPTY) {
paused_str = *current_str;
paused_str_index = i;
current_str = &evaluated_macro;
i = -1;
}
s = plaintext;
} else {
// setting up the macro_buffer
if (!isalnum(p_letter)) {
// macro characters must be alphanumeric
cout << "[Invalid Macro Character] : " << p_letter << " is not alphanumeric.";
output_buffer.clear();
return 1;
}
macro_sub_buffer.push_back(p_letter);
}
break;
case reading_arg:
if (p_letter == '{') {
args_balanced += 1;
} else if (p_letter == '}') {
args_balanced -= 1;
}
if (expected_num_of_args > 0) {
if (p_letter == '}') {
expected_num_of_args -= 1;
} else {
// if ((((p_letter == '{' || ) && isalnum(p_letter))) || (isspace(p_letter)) || (p_letter == '#') || (p_letter == MACRO)) {
if (!isalnum(p_letter) && p_letter != '{' && p_letter != '}' && p_letter != '#' && p_letter != MACRO) {
break;
}
//def{{hello}}{/def{{hello}}{picknick}}
if ((p_letter == '{') && args_balanced == 1) {
break;
}
// argument letters must be alphanumeric or a space.
// as we process multiple args, we will run into a '{', which we should not add
// to the macro def.
args[expected_num_of_args].push_back(p_letter);
}
} else {
// time to evaluate the macro if we reach here
// handling the letter itself we are processing.
if (isalnum(p_letter) || isspace(p_letter)) {
output_buffer += p_letter;
}
string evaluated_macro = evaluate_macro(¯o_sub_buffer, &args);
clear_expansion(¯o_sub_buffer, &args);
if (evaluated_macro == INVALID) {
return 1;
} else if (evaluated_macro != EMPTY) {
paused_str = *current_str;
paused_str_index = i;
current_str = &evaluated_macro;
i = -1;
}
s = plaintext;
}
break;
default:
break;
}
// returning to the string we paused initially to evaluate.
if (i == ((*current_str).length() - 1)) {
if (!paused_str.empty()) {
(*current_str) = paused_str;
paused_str.clear();
i = paused_str_index - 1;
}
}
}
if (s != plaintext) {
// if we are still in the reading_macro state when we exit, we evaluate the macro since the macro
// call was up to E0F.
// Thus, we should process what there is.
string evaluated_macro = evaluate_macro(¯o_sub_buffer, &args);
clear_expansion(¯o_sub_buffer, &args);
if (evaluated_macro == INVALID) {
return 1;
} else {
process(evaluated_macro, plaintext);
}
}
return 0;
}
int main() {
// obtaining user input
string input = "/def{kaity}{hello} /def{obama}{snow} /kaity /obama";
// getline(std::cin, input);
process(input, plaintext);
cout << output_buffer;
return 0;
}