File: json.5c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (400 lines) | stat: -rw-r--r-- 7,765 bytes parent folder | download | duplicates (2)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * Emit and parse JSON
 */

autoload Sort;
autoload Ctype;

namespace JSON {

    public void to_json_file(file output, poly arg)
    {
	void do_indent(int indent) {
	    File::putc('\n', output);
	    for (int i = 0; i < indent; i++)
		File::putc('\t', output);
	}

	void json_string(string a) {
	    File::putc('"', output);
	    for (int i = 0; i < String::length(a); i++) {
		int c = a[i];
		switch (c) {
		case '\n':
		    c = 'n';
		case '"':
		case '\\':
		    File::putc('\\', output);
		    break;
		default:
		    break;
		}
		File::putc(c, output);
	    }
	    File::putc('"', output);
	}

	void putc(int c) {
	    File::putc(c, output);
	}

	void puts(string s) {
	    File::fprintf(output, "%s", s);
	}

	void json(poly arg, int indent);

	void json_hash(poly[string] a, int indent) {
	    string[] keys = hash_keys(a);
	    putc('{');
	    bool first = true;

	    Sort::qsort(&keys, bool func(poly a, poly b) { return a > b; });
	    for (int i = 0; i < dim(keys); i++) {
		string key = keys[i];

		if (!first)
		    putc(',');
		first = false;
		do_indent(indent+1);
		json_string(key);
		puts(": ");
		json(a[key], indent+1);
	    }
	    do_indent(indent);
	    putc('}');
	}

	void json_array(poly[] a, int indent) {
	    putc('[');
	    bool first = true;

	    for (int i = 0; i < dim(a); i++) {
		if (!first)
		    putc(',');
		first = false;
		do_indent(indent + 1);
		json(a[i], indent + 1);
	    }
	    do_indent(indent);
	    putc(']');
	}

	void json_int(int arg) {
	    File::fprintf (output, "%d", arg);
	}

	void json_float(real arg) {
	    File::fprintf(output, "%.13g", arg);
	}

	void json_bool(bool arg) {
	    File::fprintf(output, "%s", arg ? "true" : "false");
	}

	void json_null(void arg) {
	    File::fprintf(output, "null");
	}

	json = void func(poly arg, int indent) {
	    if (is_hash(arg))
		return json_hash(arg, indent);
	    if (is_array(arg))
		return json_array(arg, indent);
	    if (is_string(arg))
		return json_string(arg);
	    if (is_int(arg))
		return json_int(arg);
	    if (is_number(arg))
		return json_float(arg);
	    if (is_bool(arg))
		return json_bool(arg);
	    if (is_void(arg))
		return json_null(arg);
	};

	json(arg, 0);
    }

    public string to_json(poly arg)
    {
	file output = File::string_write();
	to_json_file(output, arg);
	return File::string_string(output);
    }

    typedef enum {
	_string,
	_int,
	_float,
	_bool,
	_null,
	_oc,
	_cc,
	_os,
	_cs,
	_comma,
	_colon,
	_end,
	_error
    } term_t;

    typedef struct {
	union {
	    int		ival;
	    string	sval;
	    real	fval;
	    bool	bval;
	    void	nval;
	} val;
	term_t	token;
    } token_t;

    typedef struct {
	file	f;
	int	line;
	string	token;
    } json_input_t;

    json_input_t start_json(string s) {
	return (json_input_t) { .f = File::string_read(s), .line = 1 };
    }

    public exception json_parse_error(int line, string token);

    token_t lex(*json_input_t f) {

	int ch() {
	    int c = File::getc(f->f);
	    if (c == '\n')
		f->line++;
	    f->token = f->token + String::new(c);
	    return c;
	}

	void unch(int c) {
	    if (c == '\n')
		f->line--;
	    f->token = String::substr(f->token, 0, String::length(f->token) - 1);
	    File::ungetc(c, f->f);
	}

	bool is_int_char(int c) {
	    if (Ctype::isdigit(c))
		return true;
	    if (c == '-')
		return true;
	    return false;
	}

	bool is_float_char(int c) {
	    if (Ctype::isdigit(c))
		return true;
	    if (c == '-')
		return true;
	    if (c == '.')
		return true;
	    if (c == 'e' || c == 'E')
		return true;
	    return false;
	}

	bool match_keyword(string keyword) {
	    int l = String::length(keyword);
	    for (int i = 1; i < l; i++) {
		int c = ch();
		if (keyword[i] != c)
		    return false;
	    }
	    return true;
	}

	try {
	    f->token = "";
	    for (;;) {
		int	c = ch();

		switch (c) {
		case '\n':
		case ' ':
		case '\t':
		    continue;
		case '{':
		    return (token_t) { .token = term_t._oc };
		case '}':
		    return (token_t) { .token = term_t._cc };
		case '[':
		    return (token_t) { .token = term_t._os };
		case ']':
		    return (token_t) { .token = term_t._cs };
		case ',':
		    return (token_t) { .token = term_t._comma };
		case ':':
		    return (token_t) { .token = term_t._colon };
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
		case '-': case '.':
		    string nval = "";
		    bool has_float = false;
		    while (is_float_char(c)) {
			nval = nval + String::new(c);
			if (!is_int_char(c))
			    has_float = true;
			if (File::end(f->f))
			    break;
			c = ch();
		    }
		    unch(c);
		    if (has_float)
			return (token_t) { .val = { .fval = atof(nval) }, .token = term_t._float };
		    return (token_t) { .val = { .ival = atoi(nval) }, .token = term_t._int };
		case '"':
		    string sval = "";
		    for (;;) {
			c = ch();
			if (c == '"')
			    break;
			if (c == '\\') {
			    c = ch();
			    switch (c) {
			    case 'n':
				c = '\n';
				break;
			    case 't':
				c = '\t';
				break;
			    default:
				break;
			    }
			}
			sval = sval + String::new(c);
		    }
		    return (token_t) { .val = { .sval = sval }, .token = term_t._string };
		case 't':
		    if (match_keyword("true"))
			return (token_t) { .val = { .bval = true }, .token = term_t._bool };
		    raise json_parse_error(f->line, f->token);
		    break;
		case 'f':
		    if (match_keyword("false"))
			return (token_t) { .val = { .bval = false }, .token = term_t._bool };
		    raise json_parse_error(f->line, f->token);
		    break;
		case 'n':
		    if (match_keyword("null"))
			return (token_t) { .val = { .nval = <> }, .token = term_t._null };
		    raise json_parse_error(f->line, f->token);
		    break;
		default:
		    break;
		}
		break;
	    }
	}
	catch File::io_eof(file f) {
	    return (token_t) { .token = term_t._end };
	}
	return (token_t) { .token = term_t._error };
    }

    public poly from_json_file(file input) {
	json_input_t f = { .f = input, .line = 1 };
	token_t	token;

	void next() {
	    token = lex(&f);
	}

	void expect(term_t t) {
	    if (token.token != t)
		raise json_parse_error(f.line, f.token);
	}

	poly value();

	poly[string] hash() {
	    poly[string] ret = {};
	    for (;;) {
		if (token.token == term_t._cc) {
		    next();
		    return ret;
		}
		expect(term_t._string);
		string name = token.val.sval;
		next();
		expect(term_t._colon);
		next();
		poly val = value();
		ret[name] = val;
		switch (token.token) {
		case term_t._comma:
		    next();
		    continue;
		case term_t._cc:
		    break;
		default:
		    raise json_parse_error(f.line, f.token);
		}
	    }
	}

	poly[] array() {
	    poly[...] ret = {};
	    for (;;) {
		if (token.token == term_t._cs) {
		    next();
		    return ret;
		}
		poly val = value();
		ret[dim(ret)] = val;
		switch (token.token) {
		case term_t._comma:
		    next();
		    continue;
		case term_t._cs:
		    break;
		default:
		    raise json_parse_error(f.line, f.token);
		}
	    }
	}

	value = poly func() {
	    switch (token.token) {
	    case term_t._oc:
		    next();
		    return hash();
	    case term_t._os:
		    next();
		    return array();
	    case term_t._int:
		    int ival = token.val.ival;
		    next();
		    return ival;
	    case term_t._float:
		    real fval = token.val.fval;
		    next();
		    return fval;
	    case term_t._string:
		    string sval = token.val.sval;
		    next();
		    return sval;
	    case term_t._bool:
		    bool bval = token.val.bval;
		    next();
		    return bval;
	    case term_t._null:
		    void nval = token.val.nval;
		    next();
		    return nval;
	    }
	};

	next();
	return value();
    }

    public poly from_json(string arg) {
	return from_json_file(File::string_read(arg));
    }
}