teptris 0.3.19.1-arm-linux → 0.3.20.0-arm-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Steepfile +1 -0
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/schema.h +107 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +440 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.h +34 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.c +1090 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.h +25 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +601 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +179 -0
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +863 -0
- data/ext/teptris_ext/materialize.c +87 -1
- data/lib/teptris/3.3/teptris_ext.so +0 -0
- data/lib/teptris/3.4/teptris_ext.so +0 -0
- data/lib/teptris/4.0/teptris_ext.so +0 -0
- data/lib/teptris/schema.rb +64 -0
- data/lib/teptris/version.rb +1 -1
- data/lib/teptris.rb +1 -0
- data/sig/teptris.rbs +32 -0
- metadata +10 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9201c2f6c4e0da55e47d85c6b5533eee1da14cd5fabdce23b7fe9b9cccb341d3
|
|
4
|
+
data.tar.gz: 5c9956a2c1e8c7943f144041f9457a8710d7064673fbe55a131c60fd9d3cc1a6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2a650a24681268136a253ce11469681d038c9d31f6b9ccf693c0b317f19aebaf1b5aa4825530b79b6126db26f6bec584581598fb6024a0d975c951ed3b859d11
|
|
7
|
+
data.tar.gz: 5c154830784c8ef1c11bdd1a5d953030ca10b3c149f6eceed114924dcdfcb79d78c2432be1a0bb8fea086401c8f18bf1986acb676cd99b3c387efae5410a1e97
|
data/Steepfile
CHANGED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#ifndef TEPTRIS_SCHEMA_H
|
|
2
|
+
#define TEPTRIS_SCHEMA_H
|
|
3
|
+
|
|
4
|
+
/* JSON Schema validation for TOML documents (draft 2020-12, practical
|
|
5
|
+
* subset). Compile a schema once, validate many documents. One
|
|
6
|
+
* implementation, every runtime.
|
|
7
|
+
*
|
|
8
|
+
* ------------------------------------------------------------- policies --
|
|
9
|
+
* TOML and JSON differ in exactly four places; each difference has a
|
|
10
|
+
* documented policy:
|
|
11
|
+
*
|
|
12
|
+
* 1. null. TOML has no null. A schema branch requiring null (a bare
|
|
13
|
+
* `"type": "null"`, or an enum/const entry of null) matches no TOML
|
|
14
|
+
* value. Optionality stays with `required` + absence, so the common
|
|
15
|
+
* JSON idiom `"type": ["string", "null"]` behaves as an optional
|
|
16
|
+
* string: absent passes, a present string passes, any other present
|
|
17
|
+
* value fails. No special-casing: null-typed branches simply never
|
|
18
|
+
* match.
|
|
19
|
+
*
|
|
20
|
+
* 2. integer. TOML's integer is a DISTINCT type from float (`1` and
|
|
21
|
+
* `1.0` differ). `"type": "integer"` matches only TOML integers - a
|
|
22
|
+
* float never validates as integer, even with zero fractional part.
|
|
23
|
+
* `"type": "number"` matches both.
|
|
24
|
+
*
|
|
25
|
+
* 3. datetimes. TOML's four datetime shapes validate as `type:
|
|
26
|
+
* "string"` (their JSON view), and the `format` keywords
|
|
27
|
+
* "date-time", "date" and "time" check the datetime SHAPE: a TOML
|
|
28
|
+
* datetime value of the matching shape passes, and so does a string
|
|
29
|
+
* whose content parses as that shape. Other formats are annotations
|
|
30
|
+
* (checked by the host, if at all) - draft 2020-12 makes format
|
|
31
|
+
* annotation-only by default.
|
|
32
|
+
*
|
|
33
|
+
* 4. non-finite floats. `nan`, `inf`, `-inf` are TOML floats: they
|
|
34
|
+
* match `type: "number"`. nan compares false against everything,
|
|
35
|
+
* so it fails every range/multipleOf constraint; inf violates any
|
|
36
|
+
* `maximum`. No special-casing.
|
|
37
|
+
*
|
|
38
|
+
* ---------------------------------------------------------- semantics --
|
|
39
|
+
* - `$ref` resolves local JSON Pointers ("#/...", "#", plain-name
|
|
40
|
+
* "#anchor" via `$anchor`), including recursive definitions. Remote
|
|
41
|
+
* references (absolute URIs) are REJECTED at compile: a parser
|
|
42
|
+
* library never touches the network.
|
|
43
|
+
* - Keywords that would silently change validation results when
|
|
44
|
+
* ignored (`unevaluatedProperties`, `unevaluatedItems`,
|
|
45
|
+
* `$dynamicRef`, `$dynamicAnchor`, `$recursiveRef`, `$recursiveAnchor`,
|
|
46
|
+
* backreferences / lookahead / lookbehind in patterns) are REJECTED
|
|
47
|
+
* at compile with a diagnostic. Unknown keywords are ignored (they
|
|
48
|
+
* are extensions per the spec).
|
|
49
|
+
* - Patterns run on a linear-time NFA (Thompson construction): immune
|
|
50
|
+
* to ReDoS by construction. Practical ECMA subset: literals, '.',
|
|
51
|
+
* classes, groups, alternation, greedy quantifiers, anchors, \b,
|
|
52
|
+
* \d \w \s (and negations), \xNN, \uNNNN. Lazy quantifiers are
|
|
53
|
+
* accepted (boolean match semantics are identical).
|
|
54
|
+
* - Validation collects every error it finds, up to 256, and reports
|
|
55
|
+
* them as a JSON array of {"path", "schemaPath", "message"}; an
|
|
56
|
+
* empty array means the document is valid. Paths are dotted TOML
|
|
57
|
+
* paths with array indices ("items.0.name"); keys containing a dot
|
|
58
|
+
* are quoted ("\"my.key\"").
|
|
59
|
+
*
|
|
60
|
+
* Memory: every buffer handed out (*buf, *diag_buf) is malloc'd; the
|
|
61
|
+
* caller frees it with free(). */
|
|
62
|
+
#include <stddef.h>
|
|
63
|
+
|
|
64
|
+
#include "teptris/teptris.h"
|
|
65
|
+
|
|
66
|
+
#ifdef __cplusplus
|
|
67
|
+
extern "C" {
|
|
68
|
+
#endif
|
|
69
|
+
|
|
70
|
+
typedef struct teptris_schema teptris_schema;
|
|
71
|
+
|
|
72
|
+
/* Compile a schema from JSON text. On failure *out stays unset and
|
|
73
|
+
* *diag_buf (if non-NULL) receives a malloc'd JSON diagnostic
|
|
74
|
+
* {"error": "...", "path": "..."} locating the problem; free it with
|
|
75
|
+
* teptris_diag_free. Either out or diag_buf may be NULL if the caller
|
|
76
|
+
* does not want it. */
|
|
77
|
+
TEPTRIS_API teptris_status teptris_schema_compile(const char *json, size_t len,
|
|
78
|
+
teptris_schema **out,
|
|
79
|
+
char **diag_buf,
|
|
80
|
+
size_t *diag_len);
|
|
81
|
+
|
|
82
|
+
/* Free a compile diagnostic. */
|
|
83
|
+
TEPTRIS_API void teptris_diag_free(char *buf);
|
|
84
|
+
|
|
85
|
+
/* Validate a parsed document against the schema. *buf receives a
|
|
86
|
+
* malloc'd JSON array of error objects (empty array = valid); free it
|
|
87
|
+
* with free(). At most 256 errors are collected. */
|
|
88
|
+
TEPTRIS_API teptris_status teptris_schema_validate_doc(
|
|
89
|
+
const teptris_schema *sch, const teptris_document *doc, char **buf,
|
|
90
|
+
size_t *out_len);
|
|
91
|
+
|
|
92
|
+
/* Parse TOML text and validate it. A malformed document returns
|
|
93
|
+
* TEPTRIS_ERR_SYNTAX (with *out unchanged - parse it separately with
|
|
94
|
+
* teptris_parse to read the position). */
|
|
95
|
+
TEPTRIS_API teptris_status teptris_schema_validate(const teptris_schema *sch,
|
|
96
|
+
const char *toml, size_t len,
|
|
97
|
+
char **buf, size_t *out_len);
|
|
98
|
+
|
|
99
|
+
/* Free a compiled schema. Compiled schemas are independent of any
|
|
100
|
+
* document; reuse across documents is the intended pattern. */
|
|
101
|
+
TEPTRIS_API void teptris_schema_free(teptris_schema *sch);
|
|
102
|
+
|
|
103
|
+
#ifdef __cplusplus
|
|
104
|
+
}
|
|
105
|
+
#endif
|
|
106
|
+
|
|
107
|
+
#endif /* TEPTRIS_SCHEMA_H */
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/* The schema-side JSON reader: a small, complete RFC 8259 parser
|
|
2
|
+
* producing an arena-owned DOM for schema compilation. Numbers keep
|
|
3
|
+
* their integer identity when the token is exact and fits int64 (enum
|
|
4
|
+
* and const need exactness against TOML integers); depth is capped so
|
|
5
|
+
* hostile nesting cannot blow the C stack. */
|
|
6
|
+
#include <errno.h>
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
|
|
11
|
+
#include "json_read.h"
|
|
12
|
+
#include "teptris/memory/arena.h"
|
|
13
|
+
|
|
14
|
+
#define JSON_MAX_DEPTH 128
|
|
15
|
+
|
|
16
|
+
struct jv {
|
|
17
|
+
jv_kind kind;
|
|
18
|
+
bool b;
|
|
19
|
+
int64_t i;
|
|
20
|
+
double num;
|
|
21
|
+
/* string */
|
|
22
|
+
const char *str;
|
|
23
|
+
size_t str_len;
|
|
24
|
+
/* object (order preserved) */
|
|
25
|
+
struct jv_member {
|
|
26
|
+
const char *key;
|
|
27
|
+
jv *val;
|
|
28
|
+
} *members;
|
|
29
|
+
size_t n_members;
|
|
30
|
+
/* array */
|
|
31
|
+
jv **items;
|
|
32
|
+
size_t n_items;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
jv_kind jv_kind_of(const jv *v) { return v->kind; }
|
|
36
|
+
bool jv_bool(const jv *v) { return v->b; }
|
|
37
|
+
int64_t jv_int(const jv *v) { return v->i; }
|
|
38
|
+
double jv_num(const jv *v) { return v->kind == JV_INT ? (double)v->i : v->num; }
|
|
39
|
+
const char *jv_str(const jv *v) { return v->str; }
|
|
40
|
+
size_t jv_str_len(const jv *v) { return v->str_len; }
|
|
41
|
+
|
|
42
|
+
size_t jv_member_count(const jv *v) { return v->n_members; }
|
|
43
|
+
const char *jv_member_key(const jv *v, size_t i) { return v->members[i].key; }
|
|
44
|
+
jv *jv_member_val(const jv *v, size_t i) { return v->members[i].val; }
|
|
45
|
+
|
|
46
|
+
/* last duplicate key wins (the common practice) */
|
|
47
|
+
jv *jv_member_get(const jv *v, const char *key) {
|
|
48
|
+
jv *found = NULL;
|
|
49
|
+
for (size_t i = 0; i < v->n_members; i++) {
|
|
50
|
+
if (strcmp(v->members[i].key, key) == 0) {
|
|
51
|
+
found = v->members[i].val;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return found;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
size_t jv_item_count(const jv *v) { return v->n_items; }
|
|
58
|
+
jv *jv_item(const jv *v, size_t i) { return v->items[i]; }
|
|
59
|
+
|
|
60
|
+
typedef struct {
|
|
61
|
+
teptris_arena *a;
|
|
62
|
+
const char *src;
|
|
63
|
+
size_t len;
|
|
64
|
+
size_t pos;
|
|
65
|
+
char *err;
|
|
66
|
+
size_t err_cap;
|
|
67
|
+
int depth;
|
|
68
|
+
} jctx;
|
|
69
|
+
|
|
70
|
+
static void jfail(jctx *c, const char *msg) {
|
|
71
|
+
if (c->err != NULL && c->err[0] == '\0') {
|
|
72
|
+
snprintf(c->err, c->err_cap, "%s at byte %zu", msg, c->pos);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void jskip_ws(jctx *c) {
|
|
77
|
+
while (c->pos < c->len) {
|
|
78
|
+
char ch = c->src[c->pos];
|
|
79
|
+
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') {
|
|
80
|
+
c->pos++;
|
|
81
|
+
} else {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static int jhex(char ch) {
|
|
88
|
+
if (ch >= '0' && ch <= '9') return ch - '0';
|
|
89
|
+
if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
|
|
90
|
+
if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
|
|
91
|
+
return -1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* decode one \uXXXX (or a surrogate pair) to a code point; -1 invalid */
|
|
95
|
+
static long jescape_u(jctx *c) {
|
|
96
|
+
/* c->pos sits after the backslash-u's first digit? Contract: after 'u' */
|
|
97
|
+
if (c->pos + 4 > c->len) {
|
|
98
|
+
jfail(c, "incomplete \\u escape");
|
|
99
|
+
return -1;
|
|
100
|
+
}
|
|
101
|
+
long cp = 0;
|
|
102
|
+
for (int k = 0; k < 4; k++) {
|
|
103
|
+
int d = jhex(c->src[c->pos + (size_t)k]);
|
|
104
|
+
if (d < 0) {
|
|
105
|
+
jfail(c, "invalid \\u escape");
|
|
106
|
+
return -1;
|
|
107
|
+
}
|
|
108
|
+
cp = cp * 16 + d;
|
|
109
|
+
}
|
|
110
|
+
c->pos += 4;
|
|
111
|
+
if (cp >= 0xD800 && cp <= 0xDBFF && c->pos + 6 <= c->len &&
|
|
112
|
+
c->src[c->pos] == '\\' && c->src[c->pos + 1] == 'u') {
|
|
113
|
+
size_t save = c->pos;
|
|
114
|
+
c->pos += 2;
|
|
115
|
+
long lo = 0;
|
|
116
|
+
for (int k = 0; k < 4; k++) {
|
|
117
|
+
int d = jhex(c->src[c->pos + (size_t)k]);
|
|
118
|
+
if (d < 0) {
|
|
119
|
+
c->pos = save;
|
|
120
|
+
return cp; /* lone high surrogate: let the caller reject */
|
|
121
|
+
}
|
|
122
|
+
lo = lo * 16 + d;
|
|
123
|
+
}
|
|
124
|
+
if (lo >= 0xDC00 && lo <= 0xDFFF) {
|
|
125
|
+
c->pos += 4;
|
|
126
|
+
return 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
|
|
127
|
+
}
|
|
128
|
+
c->pos = save;
|
|
129
|
+
}
|
|
130
|
+
return cp;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static size_t jput_utf8(char *out, long cp) {
|
|
134
|
+
if (cp < 0x80) {
|
|
135
|
+
out[0] = (char)cp;
|
|
136
|
+
return 1;
|
|
137
|
+
}
|
|
138
|
+
if (cp < 0x800) {
|
|
139
|
+
out[0] = (char)(0xC0 | (cp >> 6));
|
|
140
|
+
out[1] = (char)(0x80 | (cp & 0x3F));
|
|
141
|
+
return 2;
|
|
142
|
+
}
|
|
143
|
+
if (cp < 0x10000) {
|
|
144
|
+
out[0] = (char)(0xE0 | (cp >> 12));
|
|
145
|
+
out[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
|
146
|
+
out[2] = (char)(0x80 | (cp & 0x3F));
|
|
147
|
+
return 3;
|
|
148
|
+
}
|
|
149
|
+
out[0] = (char)(0xF0 | (cp >> 18));
|
|
150
|
+
out[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
|
|
151
|
+
out[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
|
|
152
|
+
out[3] = (char)(0x80 | (cp & 0x3F));
|
|
153
|
+
return 4;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* parse a string; c->pos sits on the opening quote. The result is
|
|
157
|
+
* always arena-owned and NUL-terminated (members are looked up with
|
|
158
|
+
* strcmp); str_len carries the exact length. */
|
|
159
|
+
static const char *jstring(jctx *c, size_t *out_len) {
|
|
160
|
+
c->pos++; /* opening quote */
|
|
161
|
+
/* fast path: no escapes, copy the raw span */
|
|
162
|
+
size_t start = c->pos;
|
|
163
|
+
bool plain = true;
|
|
164
|
+
while (c->pos < c->len) {
|
|
165
|
+
unsigned char ch = (unsigned char)c->src[c->pos];
|
|
166
|
+
if (ch == '"') {
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
if (ch == '\\' || ch < 0x20) {
|
|
170
|
+
plain = false;
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
c->pos++;
|
|
174
|
+
}
|
|
175
|
+
if (c->pos >= c->len) {
|
|
176
|
+
jfail(c, "unterminated string");
|
|
177
|
+
return NULL;
|
|
178
|
+
}
|
|
179
|
+
if (plain) {
|
|
180
|
+
size_t slen = c->pos - start;
|
|
181
|
+
char *s = teptris_arena_alloc(c->a, slen + 1);
|
|
182
|
+
memcpy(s, c->src + start, slen);
|
|
183
|
+
s[slen] = '\0';
|
|
184
|
+
*out_len = slen;
|
|
185
|
+
c->pos++; /* closing quote */
|
|
186
|
+
return s;
|
|
187
|
+
}
|
|
188
|
+
/* escaped string: rebuild into arena memory */
|
|
189
|
+
size_t cap = (c->pos - start) * 4 + 16;
|
|
190
|
+
char *out = teptris_arena_alloc(c->a, cap);
|
|
191
|
+
size_t n = 0;
|
|
192
|
+
for (;;) {
|
|
193
|
+
if (c->pos >= c->len) {
|
|
194
|
+
jfail(c, "unterminated string");
|
|
195
|
+
return NULL;
|
|
196
|
+
}
|
|
197
|
+
unsigned char ch = (unsigned char)c->src[c->pos];
|
|
198
|
+
if (ch == '"') {
|
|
199
|
+
c->pos++;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
if (ch < 0x20) {
|
|
203
|
+
jfail(c, "control character in string");
|
|
204
|
+
return NULL;
|
|
205
|
+
}
|
|
206
|
+
if (ch != '\\') {
|
|
207
|
+
out[n++] = (char)ch;
|
|
208
|
+
c->pos++;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
c->pos++;
|
|
212
|
+
if (c->pos >= c->len) {
|
|
213
|
+
jfail(c, "unterminated escape");
|
|
214
|
+
return NULL;
|
|
215
|
+
}
|
|
216
|
+
char e = c->src[c->pos];
|
|
217
|
+
switch (e) {
|
|
218
|
+
case '"': out[n++] = '"'; c->pos++; break;
|
|
219
|
+
case '\\': out[n++] = '\\'; c->pos++; break;
|
|
220
|
+
case '/': out[n++] = '/'; c->pos++; break;
|
|
221
|
+
case 'b': out[n++] = '\b'; c->pos++; break;
|
|
222
|
+
case 'f': out[n++] = '\f'; c->pos++; break;
|
|
223
|
+
case 'n': out[n++] = '\n'; c->pos++; break;
|
|
224
|
+
case 'r': out[n++] = '\r'; c->pos++; break;
|
|
225
|
+
case 't': out[n++] = '\t'; c->pos++; break;
|
|
226
|
+
case 'u': {
|
|
227
|
+
c->pos++;
|
|
228
|
+
long cp = jescape_u(c);
|
|
229
|
+
if (cp < 0 || (cp >= 0xD800 && cp <= 0xDFFF)) {
|
|
230
|
+
if (cp >= 0) jfail(c, "lone surrogate in \\u escape");
|
|
231
|
+
return NULL;
|
|
232
|
+
}
|
|
233
|
+
n += jput_utf8(out + n, cp);
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
default:
|
|
237
|
+
jfail(c, "invalid escape");
|
|
238
|
+
return NULL;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
*out_len = n;
|
|
242
|
+
out[n] = '\0';
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
static jv *jvalue(jctx *c);
|
|
247
|
+
|
|
248
|
+
static jv *jobject(jctx *c) {
|
|
249
|
+
c->pos++; /* '{' */
|
|
250
|
+
jv *v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
251
|
+
*v = (jv){ .kind = JV_OBJECT };
|
|
252
|
+
size_t cap = 8;
|
|
253
|
+
v->members = teptris_arena_alloc(c->a, cap * sizeof(*v->members));
|
|
254
|
+
jskip_ws(c);
|
|
255
|
+
if (c->pos < c->len && c->src[c->pos] == '}') {
|
|
256
|
+
c->pos++;
|
|
257
|
+
return v;
|
|
258
|
+
}
|
|
259
|
+
for (;;) {
|
|
260
|
+
jskip_ws(c);
|
|
261
|
+
if (c->pos >= c->len || c->src[c->pos] != '"') {
|
|
262
|
+
jfail(c, "expected string key");
|
|
263
|
+
return NULL;
|
|
264
|
+
}
|
|
265
|
+
size_t klen = 0;
|
|
266
|
+
const char *key = jstring(c, &klen);
|
|
267
|
+
if (key == NULL) return NULL;
|
|
268
|
+
jskip_ws(c);
|
|
269
|
+
if (c->pos >= c->len || c->src[c->pos] != ':') {
|
|
270
|
+
jfail(c, "expected ':'");
|
|
271
|
+
return NULL;
|
|
272
|
+
}
|
|
273
|
+
c->pos++;
|
|
274
|
+
jv *val = jvalue(c);
|
|
275
|
+
if (val == NULL) return NULL;
|
|
276
|
+
if (v->n_members == cap) {
|
|
277
|
+
cap *= 2;
|
|
278
|
+
void *nm = teptris_arena_alloc(c->a, cap * sizeof(*v->members));
|
|
279
|
+
memcpy(nm, v->members, v->n_members * sizeof(*v->members));
|
|
280
|
+
v->members = nm;
|
|
281
|
+
}
|
|
282
|
+
v->members[v->n_members].key = key;
|
|
283
|
+
v->members[v->n_members].val = val;
|
|
284
|
+
v->n_members++;
|
|
285
|
+
jskip_ws(c);
|
|
286
|
+
if (c->pos < c->len && c->src[c->pos] == ',') {
|
|
287
|
+
c->pos++;
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (c->pos < c->len && c->src[c->pos] == '}') {
|
|
291
|
+
c->pos++;
|
|
292
|
+
return v;
|
|
293
|
+
}
|
|
294
|
+
jfail(c, "expected ',' or '}'");
|
|
295
|
+
return NULL;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static jv *jarray(jctx *c) {
|
|
300
|
+
c->pos++; /* '[' */
|
|
301
|
+
jv *v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
302
|
+
*v = (jv){ .kind = JV_ARRAY };
|
|
303
|
+
size_t cap = 8;
|
|
304
|
+
v->items = teptris_arena_alloc(c->a, cap * sizeof(*v->items));
|
|
305
|
+
jskip_ws(c);
|
|
306
|
+
if (c->pos < c->len && c->src[c->pos] == ']') {
|
|
307
|
+
c->pos++;
|
|
308
|
+
return v;
|
|
309
|
+
}
|
|
310
|
+
for (;;) {
|
|
311
|
+
jv *val = jvalue(c);
|
|
312
|
+
if (val == NULL) return NULL;
|
|
313
|
+
if (v->n_items == cap) {
|
|
314
|
+
cap *= 2;
|
|
315
|
+
void *ni = teptris_arena_alloc(c->a, cap * sizeof(*v->items));
|
|
316
|
+
memcpy(ni, v->items, v->n_items * sizeof(*v->items));
|
|
317
|
+
v->items = ni;
|
|
318
|
+
}
|
|
319
|
+
v->items[v->n_items++] = val;
|
|
320
|
+
jskip_ws(c);
|
|
321
|
+
if (c->pos < c->len && c->src[c->pos] == ',') {
|
|
322
|
+
c->pos++;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (c->pos < c->len && c->src[c->pos] == ']') {
|
|
326
|
+
c->pos++;
|
|
327
|
+
return v;
|
|
328
|
+
}
|
|
329
|
+
jfail(c, "expected ',' or ']'");
|
|
330
|
+
return NULL;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static jv *jvalue(jctx *c) {
|
|
335
|
+
if (++c->depth > JSON_MAX_DEPTH) {
|
|
336
|
+
jfail(c, "schema nesting too deep");
|
|
337
|
+
c->depth--;
|
|
338
|
+
return NULL;
|
|
339
|
+
}
|
|
340
|
+
jskip_ws(c);
|
|
341
|
+
if (c->pos >= c->len) {
|
|
342
|
+
jfail(c, "unexpected end of schema");
|
|
343
|
+
c->depth--;
|
|
344
|
+
return NULL;
|
|
345
|
+
}
|
|
346
|
+
char ch = c->src[c->pos];
|
|
347
|
+
jv *v = NULL;
|
|
348
|
+
if (ch == '{') {
|
|
349
|
+
v = jobject(c);
|
|
350
|
+
} else if (ch == '[') {
|
|
351
|
+
v = jarray(c);
|
|
352
|
+
} else if (ch == '"') {
|
|
353
|
+
size_t slen = 0;
|
|
354
|
+
const char *s = jstring(c, &slen);
|
|
355
|
+
if (s != NULL) {
|
|
356
|
+
v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
357
|
+
*v = (jv){ .kind = JV_STRING, .str = s, .str_len = slen };
|
|
358
|
+
}
|
|
359
|
+
} else if (c->len - c->pos >= 4 &&
|
|
360
|
+
strncmp(c->src + c->pos, "true", 4) == 0) {
|
|
361
|
+
v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
362
|
+
*v = (jv){ .kind = JV_BOOL, .b = true };
|
|
363
|
+
c->pos += 4;
|
|
364
|
+
} else if (c->len - c->pos >= 5 &&
|
|
365
|
+
strncmp(c->src + c->pos, "false", 5) == 0) {
|
|
366
|
+
v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
367
|
+
*v = (jv){ .kind = JV_BOOL };
|
|
368
|
+
c->pos += 5;
|
|
369
|
+
} else if (c->len - c->pos >= 4 &&
|
|
370
|
+
strncmp(c->src + c->pos, "null", 4) == 0) {
|
|
371
|
+
v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
372
|
+
*v = (jv){ .kind = JV_NULL };
|
|
373
|
+
c->pos += 4;
|
|
374
|
+
} else if (ch == '-' || (ch >= '0' && ch <= '9')) {
|
|
375
|
+
size_t start = c->pos;
|
|
376
|
+
if (ch == '-') c->pos++;
|
|
377
|
+
bool integral = true;
|
|
378
|
+
while (c->pos < c->len) {
|
|
379
|
+
char d = c->src[c->pos];
|
|
380
|
+
if (d >= '0' && d <= '9') {
|
|
381
|
+
c->pos++;
|
|
382
|
+
} else if (d == '.' || d == 'e' || d == 'E' || d == '+' || d == '-') {
|
|
383
|
+
integral = false;
|
|
384
|
+
c->pos++;
|
|
385
|
+
} else {
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
char tmp[64];
|
|
390
|
+
size_t tlen = c->pos - start;
|
|
391
|
+
if (tlen == 0 || tlen >= sizeof tmp) {
|
|
392
|
+
jfail(c, "invalid number");
|
|
393
|
+
c->depth--;
|
|
394
|
+
return NULL;
|
|
395
|
+
}
|
|
396
|
+
memcpy(tmp, c->src + start, tlen);
|
|
397
|
+
tmp[tlen] = '\0';
|
|
398
|
+
v = teptris_arena_alloc(c->a, sizeof(jv));
|
|
399
|
+
*v = (jv){ .num = strtod(tmp, NULL) };
|
|
400
|
+
if (integral) {
|
|
401
|
+
errno = 0;
|
|
402
|
+
char *end = NULL;
|
|
403
|
+
long long i = strtoll(tmp, &end, 10);
|
|
404
|
+
if (errno == 0 && end != NULL && *end == '\0') {
|
|
405
|
+
v->kind = JV_INT;
|
|
406
|
+
v->i = (int64_t)i;
|
|
407
|
+
} else {
|
|
408
|
+
v->kind = JV_NUM;
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
v->kind = JV_NUM;
|
|
412
|
+
}
|
|
413
|
+
} else {
|
|
414
|
+
jfail(c, "unexpected character");
|
|
415
|
+
}
|
|
416
|
+
c->depth--;
|
|
417
|
+
return v;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
jv *json_read(teptris_arena *a, const char *src, size_t len, char *err,
|
|
421
|
+
size_t err_cap) {
|
|
422
|
+
jctx c;
|
|
423
|
+
memset(&c, 0, sizeof(c));
|
|
424
|
+
c.a = a;
|
|
425
|
+
c.src = src;
|
|
426
|
+
c.len = len;
|
|
427
|
+
c.err = err;
|
|
428
|
+
c.err_cap = err_cap;
|
|
429
|
+
if (err != NULL && err_cap > 0) err[0] = '\0';
|
|
430
|
+
jv *v = jvalue(&c);
|
|
431
|
+
if (v == NULL) return NULL;
|
|
432
|
+
jskip_ws(&c);
|
|
433
|
+
if (c.pos != c.len) {
|
|
434
|
+
if (err != NULL && err_cap > 0) {
|
|
435
|
+
snprintf(err, err_cap, "trailing content at byte %zu", c.pos);
|
|
436
|
+
}
|
|
437
|
+
return NULL;
|
|
438
|
+
}
|
|
439
|
+
return v;
|
|
440
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* The schema-side JSON DOM (arena-owned). Internal to the schema
|
|
2
|
+
* subsystem; see json_read.c for the reader contract. */
|
|
3
|
+
#ifndef TEPTRIS_SCHEMA_JSON_READ_H
|
|
4
|
+
#define TEPTRIS_SCHEMA_JSON_READ_H
|
|
5
|
+
|
|
6
|
+
#include <stdbool.h>
|
|
7
|
+
#include <stddef.h>
|
|
8
|
+
#include <stdint.h>
|
|
9
|
+
|
|
10
|
+
#include "teptris/memory/arena.h"
|
|
11
|
+
|
|
12
|
+
typedef enum { JV_NULL, JV_BOOL, JV_INT, JV_NUM, JV_STRING, JV_ARRAY, JV_OBJECT } jv_kind;
|
|
13
|
+
|
|
14
|
+
typedef struct jv jv;
|
|
15
|
+
|
|
16
|
+
jv *json_read(teptris_arena *a, const char *src, size_t len, char *err,
|
|
17
|
+
size_t err_cap);
|
|
18
|
+
|
|
19
|
+
jv_kind jv_kind_of(const jv *v);
|
|
20
|
+
bool jv_bool(const jv *v);
|
|
21
|
+
int64_t jv_int(const jv *v);
|
|
22
|
+
double jv_num(const jv *v); /* the double view of INT or NUM */
|
|
23
|
+
const char *jv_str(const jv *v);
|
|
24
|
+
size_t jv_str_len(const jv *v);
|
|
25
|
+
|
|
26
|
+
size_t jv_member_count(const jv *v);
|
|
27
|
+
const char *jv_member_key(const jv *v, size_t i);
|
|
28
|
+
jv *jv_member_val(const jv *v, size_t i);
|
|
29
|
+
jv *jv_member_get(const jv *v, const char *key); /* last duplicate wins */
|
|
30
|
+
|
|
31
|
+
size_t jv_item_count(const jv *v);
|
|
32
|
+
jv *jv_item(const jv *v, size_t i);
|
|
33
|
+
|
|
34
|
+
#endif
|