-
Notifications
You must be signed in to change notification settings - Fork 11
/
js.h
90 lines (72 loc) · 1.7 KB
/
js.h
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
/*
* JSON parser
*****************************************************************************/
#ifndef JS_H
#define JS_H
#include <stddef.h>
#include "buffer.h"
#include "util.h"
typedef enum {
JS_NONE,
JS_OBJECT,
JS_ARRAY,
JS_NULL,
JS_NUMBER,
JS_TRUE,
JS_FALSE,
JS_STRING,
JS_PAIR,
JS_ITEM
} jstype_t;
typedef enum {
JS_OKAY,
JS_EBUG,
JS_EPARSE,
JS_EDONE
} jserr_t;
typedef struct {
jstype_t type;
size_t start;
size_t end;
size_t idx;
size_t parent;
size_t first_child;
size_t next_sibling;
size_t parsed;
} jstok_t;
typedef struct {
FILE *in;
Buffer *js;
size_t pos;
jstok_t *toks;
size_t curtok;
size_t toks_size;
size_t depth;
} jsparser_t;
jstok_t *js_tok(jsparser_t *p, size_t t);
char *js_buf(jsparser_t *p, size_t t);
size_t js_len(jsparser_t *p, size_t t);
// predicates
int js_is_string(jstok_t *tok);
int js_is_array(jstok_t *tok);
int js_is_object(jstok_t *tok);
int js_is_collection(jstok_t *tok);
int js_is_empty(jstok_t *tok);
int js_is_pair(jstok_t *tok);
int js_is_item(jstok_t *tok);
// parser
void js_alloc(jsparser_t **p, FILE *in, size_t toks_size);
void js_free(jsparser_t **p);
jserr_t js_parse(jsparser_t *p, size_t t);
jserr_t js_parse_one(jsparser_t *p, size_t *t);
void js_reset(jsparser_t *p);
// accessors
size_t js_obj_get(jsparser_t *p, size_t obj, const char *key);
size_t js_array_get(jsparser_t *p, size_t ary, size_t idx);
// create new nodes
size_t js_create_index(jsparser_t *p, size_t idx);
// printing, unescaping
jserr_t js_print(jsparser_t *p, size_t t, Buffer *b, int json, int csv);
jserr_t js_print_info(jsparser_t *p, size_t t, Buffer *buf);
void js_unescape_string(Buffer *b, char *in, size_t len, int csv);
#endif