-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.c
More file actions
335 lines (299 loc) · 9.84 KB
/
Copy pathtokenizer.c
File metadata and controls
335 lines (299 loc) · 9.84 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
//
// Created by liujie52 on 2020/9/30.
//
/**
*
{
"a": 1,
"b": "string"
}
*
*/
#include "tokenizer.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
TOKEN* parseStringToTokens (char* json_string) {
int index = 0;
int *curPointerIndex = &index;
TOKEN* root = create_link_list();
TOKEN *temp;
printf("json_string = %s\n", json_string);
while (*curPointerIndex < strlen(json_string)) {
char character = json_string[*curPointerIndex];
if (isSpace(json_string, *curPointerIndex)) {
*curPointerIndex = *curPointerIndex + 1;
continue;
}
if (character == '{') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseBeginObject(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == '}') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseEndObject(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == '[') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseBeginArray(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == ']') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseEndArray(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == ':') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseSepColon(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == ',') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseSepComma(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == '"') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseStringValue(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (isNum(json_string, *curPointerIndex)) {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseNumberValue(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == 'f' || character == 't') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseBoolValue(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
if (character == 'n') {
temp = malloc(sizeof (TOKEN));
PARSE_VALUE_RESULT result = parseNullValue(json_string, curPointerIndex, temp);
if (result != SUCCESS) {
exit(result);
}
push_to_list(root, temp);
temp = NULL;
continue;
}
}
return root;
};
PARSE_VALUE_RESULT parseBeginObject(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->start_index = *curPointerIndex;
token->end_index = *curPointerIndex;
token->token_type = BEGIN_OBJECT;
token->next = NULL;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseEndObject(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->start_index = *curPointerIndex;
token->end_index = *curPointerIndex;
token->token_type = END_OBJECT;
token->next = NULL;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseBeginArray(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->start_index = *curPointerIndex;
token->end_index = *curPointerIndex;
token->token_type = BEGIN_ARRAY;
token->next = NULL;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseEndArray(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->start_index = *curPointerIndex;
token->end_index = *curPointerIndex;
token->token_type = END_ARRAY;
token->next = NULL;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseSepComma(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->start_index = *curPointerIndex;
token->end_index = *curPointerIndex;
token->token_type = SEP_COMMA;
token->next = NULL;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseSepColon(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->start_index = *curPointerIndex;
token->end_index = *curPointerIndex;
token->token_type = SEP_COLON;
token->next = NULL;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseBoolValue(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->token_type = BOOLEAN;
token->start_index = *curPointerIndex;
char character = json_string[*curPointerIndex];
char* true_value = "true";
char* false_value = "false";
char* val;
if (character == 't') { val = true_value; }
else { val = false_value; }
for (int index = 0; index < strlen(val) - 1; index ++) {
if (val[index] != json_string[*curPointerIndex]) {
printf("NOT_VALID_BOOL_VALUE");
return NOT_VALID_BOOL_VALUE;
}
*curPointerIndex = *curPointerIndex + 1;
}
token->end_index = *curPointerIndex;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseStringValue(char* json_string, int* curPointerIndex, TOKEN* token) {
token->token_type = STRING;
token->start_index = *curPointerIndex;
do {
if (*curPointerIndex >= strlen(json_string)) {
printf("NOT_VALID_STRING_VALUE");
return NOT_VALID_STRING_VALUE;
}
*curPointerIndex = *curPointerIndex + 1;
} while (json_string[*curPointerIndex] != '"');
token->end_index = *curPointerIndex;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseNullValue(const char* json_string, int* curPointerIndex, TOKEN* token) {
token->token_type = Null;
token->start_index = *curPointerIndex;
char* null_val = "null";
for (int index = 0; index < strlen(null_val) - 1; index ++) {
if (null_val[index] != json_string[*curPointerIndex]) {
printf("NOT_VALID_NULL_VALUE");
return NOT_VALID_NULL_VALUE;
}
*curPointerIndex = *curPointerIndex + 1;
}
token->end_index = *curPointerIndex;
*curPointerIndex = *curPointerIndex + 1;
get_token_value(json_string, token);
return SUCCESS;
}
PARSE_VALUE_RESULT parseNumberValue(char* json_string, int* curPointerIndex, TOKEN* token) {
token->token_type = NUMBER;
token->start_index = *curPointerIndex;
do {
if (*curPointerIndex >= strlen(json_string)) {
printf("NOT_VALID_NUMBER_VALUE");
return NOT_VALID_NUMBER_VALUE;
}
*curPointerIndex = *curPointerIndex + 1;
} while (json_string[*curPointerIndex] == '0'
|| json_string[*curPointerIndex] == '1'
|| json_string[*curPointerIndex] == '2'
|| json_string[*curPointerIndex] == '3'
|| json_string[*curPointerIndex] == '4'
|| json_string[*curPointerIndex] == '5'
|| json_string[*curPointerIndex] == '6'
|| json_string[*curPointerIndex] == '7'
|| json_string[*curPointerIndex] == '8'
|| json_string[*curPointerIndex] == '9'
|| json_string[*curPointerIndex] == '.');
token->end_index = *curPointerIndex - 1;
get_token_value(json_string, token);
return SUCCESS;
}
int isEscapeChar(const char* json_string, int index) {
char c = json_string[index];
if (c == '\\') {
return 1;
}
return 0;
}
int isSpace(const char* json_string, int index) {
char c = json_string[index];
if (c == '\t')
return 1;
if (c == '\n')
return 1;
if (c == '\r')
return 1;
if (c == '\0')
return 1;
if (c == ' ')
return 1;
return 0;
}
int isNum(const char* json_string, int index) {
char c = json_string[index];
if (c == '0'
|| c == '1'
|| c == '2'
|| c == '3'
|| c == '4'
|| c == '5'
|| c == '6'
|| c == '7'
|| c == '8'
|| c == '9'
|| c == '.') {
return 1;
} else {
return 0;
}
};