-
Notifications
You must be signed in to change notification settings - Fork 1
/
toyaml.c
350 lines (297 loc) · 8.14 KB
/
toyaml.c
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
340
341
342
343
344
345
346
347
348
349
350
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Size of the buffer
#define BUFFERSIZE 50
// Size of the char array where property name will be written
#define MAXPROPSIZE 15
// Size of the char array where property value will be written
#define MAXVALSIZE 30
// Size of the char array where position of selector will be written. 3 is
// enough to contain two number and terminator
#define MAXSELPOS 3
// Read n characters and check whether it's \t (if n==1) or \t\t (if n==2)
#define catchTabs(n) \
fgets(buffer, n+1, inpfile) && \
!strcmp(buffer, n == 1 ? "\t" : "\t\t")
#define catchBlock(name) \
!strcmp(buffer, name "\n")
// Prints this message: {indent}"{name}": {quote}{value}{quote}
#define printProperty(indent, name, quote, value) \
fprintf( \
outpfile, \
"%s%s: %s%s%s\n", \
indent, name, quote, value, quote \
);
// Prints this message: {indent}"{name}": {bracket}\n
#define printNamedBlock(indent, name, bracket) \
fprintf(outpfile, "%s%s: %s\n", indent, name, bracket);
// Prints this message: {indent}{bracket}\n
#define printBlock(indent, bracket) \
fprintf(outpfile, "%s%s\n", indent, bracket);
// Close block with bracket you given
#define closeBlock(indent, bracket) \
fprintf(outpfile, "%s%s,\n", indent, bracket);
#define deleteComma \
fseek(outpfile, -3, SEEK_CUR); \
fprintf(outpfile, "\n");
// Write array item '-' with indent
#define arrayItem(indent) \
fprintf(outpfile, "%s-\n", indent);
#define grabProperty \
char propertyName[MAXPROPSIZE]; \
char propertyValue[MAXVALSIZE]; \
getProperty( \
buffer, \
propertyName, \
propertyValue \
);
void clear(char *buffer){
/* Fills buffer with spaces. */
for (int i = 0; i < BUFFERSIZE; i++)
buffer[i] = ' ';
}
short readUntil (char what, FILE *fp, char *buffer) {
/* Read file until specific symbol, put the string into buffer and return
* its length.*/
char c;
short len = 0;
while ((c = fgetc(fp)) != EOF && c != what) {
buffer[len++] = c;
}
if (feof(fp)) {
fprintf(stderr, "Unexpected end of the file.\n");
exit(EXIT_FAILURE);
}
return len;
}
void pasteAfter (char *string, const char *substring, int a, int b, int n) {
/* Paste n characters from substring[a:] into string[b:] */
do
string[a++] = substring[b++];
while (n-- > 0);
}
void getSelector (char *buffer, char *position) {
/* Parse selectors to position and name. Example:
* "first next": buffer -> "next", position -> "1"
* "second previous": buffer -> "previous", position -> "2"
* "6th next": buffer -> "next", position -> "6"
* Write selector name to buffer, position to *position.
*
* Args:
* buffer: String to read.
* position: Char array to write position of selector.
*
*/
// Position of 'next', 'previous', 'token', etc.
short c = -1;
if (isdigit(buffer[0])) {
// String starts with [0-9]+th
// Copy all until 't'
while (buffer[c++ + 1] != 't')
position[c] = buffer[c];
position[c] = '\0';
// Selector name will start after 3 symbols: 'th '
c += 2;
} else {
// String starts with one of words: first, second, third, fourth,
// fifth, end, beginning, token.
// Third character in each of words is different, so we can recognize
// them using that.
switch(buffer[2]){
case 'r':
// 'fi -r- st'
strcpy(position, "1\0");
c = 5;
break;
case 'c':
// 'se -c- ond'
strcpy(position, "2\0");
c = 6;
break;
case 'i':
// 'th -i- rd'
strcpy(position, "3\0");
c = 5;
break;
case 'u':
// 'fo -u- rth'
strcpy(position, "4\0");
c = 6;
break;
case 'f':
// 'fi -f- th'
strcpy(position, "5\0");
c = 5;
break;
default:
// If there's no number-word, it's considered to be 'first'
strcpy(position, "1\0");
break;
}
}
if (c != 0) {
// Rewrite selector into buffer end terminate the word.
short i = -1;
do
buffer[i++] = buffer[c++];
while
(buffer[c] != ' ');
buffer[i] = '\0';
} else {
// String starts from 'end', 'beginning', or 'token' word. Put
// terminator after them.
switch(buffer[0]){
case 'e':
buffer[3] = '\t';
case 'b':
buffer[9] = '\t';
case 't':
buffer[5] = '\t';
}
}
}
void getProperty (char *buffer, char *name, char *value) {
/* Parse property to name and value. Example:
* "name is value": value -> '[true, "value"]', name -> 'name'
* "name is not value": value -> '[false, "value"]', name -> 'name'
* "name becomes value": value -> 'value', name -> 'name'
*
* Args:
* buffer: String to read.
* name: Char array to write property name in.
*
*/
// Position counter.
short c = -1;
// I've avoid using strncpy here in order to clarify the code.
while (buffer[c++ + 1] != ' ')
name[c] = buffer[c];
name[c] = '\0';
if (buffer[c+1] == 'b') {
// Next word is 'becomes'.
c += 8;
// Write value and terminate it.
short i = -1;
do
value[i++] = buffer[c++];
while
(buffer[c] != ' ');
buffer[i] = '\0';
} else {
// Next is 'is' or 'is not'. It should be written in the following
// format:
// [true, value] if "name is value"
// [false, value] if "name is not value"
unsigned char isTrue;
if (buffer[c+6] == 't' && buffer[c+7] == ' ')
// After 'is no-t -'
// isTrue becomes false
isTrue = !(c += 8);
else
// isTrue becomes true
isTrue = !!(c += 4);
strcpy(value, isTrue ? "[true, " : "[false, ");
short i = isTrue ? 7 : 8;
do
value[i++] = buffer[c++];
while
(buffer[c] != ' ');
pasteAfter(value, "]\0", i, 0, 3);
}
}
int main (int argc, char** argv) {
FILE *inpfile, *outpfile;
if (argc == 1) {
printf(
"This parser can convert .ctx19 files to .json, but some parameters are "
"needed.\n\n"
"Expected parameters:\n"
"Name Default Description\n"
"--file *requiered Name of the file to be converted. Type it with\n"
" .ctx19 extension. E.g.: \"rules.ctx19\".\n"
"--output *requiered Name of the file output to be written. If file\n"
" doesn't exists, if will be created. Otherwise,\n"
" it will be overwritten.\n"
);
exit(EXIT_SUCCESS);
} else if (argc == 5) {
for (int i = 1; i < argc; i+=2){
// Grab --file and --output. Assume that parameters was passed
// correctly.
if (!strcmp(argv[i], "--file"))
inpfile = fopen(argv[i+1], "r");
if (!strcmp(argv[i], "--output"))
outpfile = fopen(argv[i+1], "w");
}
} else {
fprintf(stderr, "No --file and --output specified.\n");
exit(EXIT_FAILURE);
}
if(inpfile == NULL || outpfile == NULL){
fprintf(stderr, "Error when creating file stream.\n");
exit(EXIT_FAILURE);
}
// Start of array of rules.
fprintf(outpfile, "---\n");
while (1) {
char buffer[BUFFERSIZE] = {' '};
arrayItem("");
// Catch 'if' block.
fgets(buffer, 4, inpfile);
if (catchBlock("if")) {
printNamedBlock(" ", "if", "");
// Catch selector.
while(catchTabs(1)){
clear(buffer);
// MAXSELPOS is enough to contain two numbers and '\0'
char selectorPosition[MAXSELPOS];
// Name of selector will be written into buffer.
readUntil('\n', inpfile, buffer);
getSelector(
buffer,
selectorPosition
);
printBlock(" ", "-");
printProperty(" ", "__position", "", selectorPosition);
printProperty(" ", "__name", "\"", buffer);
clear(buffer);
// Catch property
while(catchTabs(2)){
clear(buffer);
readUntil('\n', inpfile, buffer);
grabProperty;
printProperty(" ", propertyName, "", propertyValue);
clear(buffer);
}
fseek(inpfile, -2, SEEK_CUR);
}
// End of 'if' block.
fseek(inpfile, -1, SEEK_CUR);
clear(buffer);
// Catch 'then' block
fgets(buffer, 12, inpfile);
if (catchBlock("then")){
printNamedBlock(" ", "then", "");
clear(buffer);
// Caught property
while(catchTabs(1)){
clear(buffer);
readUntil('\n', inpfile, buffer);
grabProperty;
printProperty(" ", propertyName, "\"", buffer);
clear(buffer);
}
// End of 'then' block.
}
// End of rule block
}
if(feof(inpfile))
break;
}
// End of array of rules.
fclose(inpfile);
fclose(outpfile);
return 0;
}