|
| 1 | +#ifdef _WINDOWS |
| 2 | +#define _CRTDBG_MAP_ALLOC |
| 3 | +#include <crtdbg.h> |
| 4 | +#endif |
| 5 | +#include "leptjson.h" |
| 6 | +#include <assert.h> /* assert() */ |
| 7 | +#include <errno.h> /* errno, ERANGE */ |
| 8 | +#include <math.h> /* HUGE_VAL */ |
| 9 | +#include <stdlib.h> /* NULL, malloc(), realloc(), free(), strtod() */ |
| 10 | +#include <string.h> /* memcpy() */ |
| 11 | + |
| 12 | +#ifndef LEPT_PARSE_STACK_INIT_SIZE |
| 13 | +#define LEPT_PARSE_STACK_INIT_SIZE 256 |
| 14 | +#endif |
| 15 | + |
| 16 | +#define EXPECT(c, ch) do { assert(*c->json == (ch)); c->json++; } while(0) |
| 17 | +#define ISDIGIT(ch) ((ch) >= '0' && (ch) <= '9') |
| 18 | +#define ISDIGIT1TO9(ch) ((ch) >= '1' && (ch) <= '9') |
| 19 | +#define PUTC(c, ch) do { *(char*)lept_context_push(c, sizeof(char)) = (ch); } while(0) |
| 20 | + |
| 21 | +typedef struct { |
| 22 | + const char* json; |
| 23 | + char* stack; |
| 24 | + size_t size, top; |
| 25 | +}lept_context; |
| 26 | + |
| 27 | +static void* lept_context_push(lept_context* c, size_t size) { |
| 28 | + void* ret; |
| 29 | + assert(size > 0); |
| 30 | + if (c->top + size >= c->size) { |
| 31 | + if (c->size == 0) |
| 32 | + c->size = LEPT_PARSE_STACK_INIT_SIZE; |
| 33 | + while (c->top + size >= c->size) |
| 34 | + c->size += c->size >> 1; /* c->size * 1.5 */ |
| 35 | + c->stack = (char*)realloc(c->stack, c->size); |
| 36 | + } |
| 37 | + ret = c->stack + c->top; |
| 38 | + c->top += size; |
| 39 | + return ret; |
| 40 | +} |
| 41 | + |
| 42 | +static void* lept_context_pop(lept_context* c, size_t size) { |
| 43 | + assert(c->top >= size); |
| 44 | + return c->stack + (c->top -= size); |
| 45 | +} |
| 46 | + |
| 47 | +static void lept_parse_whitespace(lept_context* c) { |
| 48 | + const char *p = c->json; |
| 49 | + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') |
| 50 | + p++; |
| 51 | + c->json = p; |
| 52 | +} |
| 53 | + |
| 54 | +static int lept_parse_literal(lept_context* c, lept_value* v, const char* literal, lept_type type) { |
| 55 | + size_t i; |
| 56 | + EXPECT(c, literal[0]); |
| 57 | + for (i = 0; literal[i + 1]; i++) |
| 58 | + if (c->json[i] != literal[i + 1]) |
| 59 | + return LEPT_PARSE_INVALID_VALUE; |
| 60 | + c->json += i; |
| 61 | + v->type = type; |
| 62 | + return LEPT_PARSE_OK; |
| 63 | +} |
| 64 | + |
| 65 | +static int lept_parse_number(lept_context* c, lept_value* v) { |
| 66 | + const char* p = c->json; |
| 67 | + if (*p == '-') p++; |
| 68 | + if (*p == '0') p++; |
| 69 | + else { |
| 70 | + if (!ISDIGIT1TO9(*p)) return LEPT_PARSE_INVALID_VALUE; |
| 71 | + for (p++; ISDIGIT(*p); p++); |
| 72 | + } |
| 73 | + if (*p == '.') { |
| 74 | + p++; |
| 75 | + if (!ISDIGIT(*p)) return LEPT_PARSE_INVALID_VALUE; |
| 76 | + for (p++; ISDIGIT(*p); p++); |
| 77 | + } |
| 78 | + if (*p == 'e' || *p == 'E') { |
| 79 | + p++; |
| 80 | + if (*p == '+' || *p == '-') p++; |
| 81 | + if (!ISDIGIT(*p)) return LEPT_PARSE_INVALID_VALUE; |
| 82 | + for (p++; ISDIGIT(*p); p++); |
| 83 | + } |
| 84 | + errno = 0; |
| 85 | + v->u.n = strtod(c->json, NULL); |
| 86 | + if (errno == ERANGE && (v->u.n == HUGE_VAL || v->u.n == -HUGE_VAL)) |
| 87 | + return LEPT_PARSE_NUMBER_TOO_BIG; |
| 88 | + v->type = LEPT_NUMBER; |
| 89 | + c->json = p; |
| 90 | + return LEPT_PARSE_OK; |
| 91 | +} |
| 92 | + |
| 93 | +static const char* lept_parse_hex4(const char* p, unsigned* u) { |
| 94 | + /* \TODO */ |
| 95 | + return p; |
| 96 | +} |
| 97 | + |
| 98 | +static void lept_encode_utf8(lept_context* c, unsigned u) { |
| 99 | + /* \TODO */ |
| 100 | +} |
| 101 | + |
| 102 | +#define STRING_ERROR(ret) do { c->top = head; return ret; } while(0) |
| 103 | + |
| 104 | +static int lept_parse_string(lept_context* c, lept_value* v) { |
| 105 | + size_t head = c->top, len; |
| 106 | + unsigned u; |
| 107 | + const char* p; |
| 108 | + EXPECT(c, '\"'); |
| 109 | + p = c->json; |
| 110 | + for (;;) { |
| 111 | + char ch = *p++; |
| 112 | + switch (ch) { |
| 113 | + case '\"': |
| 114 | + len = c->top - head; |
| 115 | + lept_set_string(v, (const char*)lept_context_pop(c, len), len); |
| 116 | + c->json = p; |
| 117 | + return LEPT_PARSE_OK; |
| 118 | + case '\\': |
| 119 | + switch (*p++) { |
| 120 | + case '\"': PUTC(c, '\"'); break; |
| 121 | + case '\\': PUTC(c, '\\'); break; |
| 122 | + case '/': PUTC(c, '/' ); break; |
| 123 | + case 'b': PUTC(c, '\b'); break; |
| 124 | + case 'f': PUTC(c, '\f'); break; |
| 125 | + case 'n': PUTC(c, '\n'); break; |
| 126 | + case 'r': PUTC(c, '\r'); break; |
| 127 | + case 't': PUTC(c, '\t'); break; |
| 128 | + case 'u': |
| 129 | + if (!(p = lept_parse_hex4(p, &u))) |
| 130 | + STRING_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX); |
| 131 | + /* \TODO surrogate handling */ |
| 132 | + lept_encode_utf8(c, u); |
| 133 | + break; |
| 134 | + default: |
| 135 | + STRING_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE); |
| 136 | + } |
| 137 | + break; |
| 138 | + case '\0': |
| 139 | + STRING_ERROR(LEPT_PARSE_MISS_QUOTATION_MARK); |
| 140 | + default: |
| 141 | + if ((unsigned char)ch < 0x20) |
| 142 | + STRING_ERROR(LEPT_PARSE_INVALID_STRING_CHAR); |
| 143 | + PUTC(c, ch); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +static int lept_parse_value(lept_context* c, lept_value* v) { |
| 149 | + switch (*c->json) { |
| 150 | + case 't': return lept_parse_literal(c, v, "true", LEPT_TRUE); |
| 151 | + case 'f': return lept_parse_literal(c, v, "false", LEPT_FALSE); |
| 152 | + case 'n': return lept_parse_literal(c, v, "null", LEPT_NULL); |
| 153 | + default: return lept_parse_number(c, v); |
| 154 | + case '"': return lept_parse_string(c, v); |
| 155 | + case '\0': return LEPT_PARSE_EXPECT_VALUE; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +int lept_parse(lept_value* v, const char* json) { |
| 160 | + lept_context c; |
| 161 | + int ret; |
| 162 | + assert(v != NULL); |
| 163 | + c.json = json; |
| 164 | + c.stack = NULL; |
| 165 | + c.size = c.top = 0; |
| 166 | + lept_init(v); |
| 167 | + lept_parse_whitespace(&c); |
| 168 | + if ((ret = lept_parse_value(&c, v)) == LEPT_PARSE_OK) { |
| 169 | + lept_parse_whitespace(&c); |
| 170 | + if (*c.json != '\0') { |
| 171 | + v->type = LEPT_NULL; |
| 172 | + ret = LEPT_PARSE_ROOT_NOT_SINGULAR; |
| 173 | + } |
| 174 | + } |
| 175 | + assert(c.top == 0); |
| 176 | + free(c.stack); |
| 177 | + return ret; |
| 178 | +} |
| 179 | + |
| 180 | +void lept_free(lept_value* v) { |
| 181 | + assert(v != NULL); |
| 182 | + if (v->type == LEPT_STRING) |
| 183 | + free(v->u.s.s); |
| 184 | + v->type = LEPT_NULL; |
| 185 | +} |
| 186 | + |
| 187 | +lept_type lept_get_type(const lept_value* v) { |
| 188 | + assert(v != NULL); |
| 189 | + return v->type; |
| 190 | +} |
| 191 | + |
| 192 | +int lept_get_boolean(const lept_value* v) { |
| 193 | + assert(v != NULL && (v->type == LEPT_TRUE || v->type == LEPT_FALSE)); |
| 194 | + return v->type == LEPT_TRUE; |
| 195 | +} |
| 196 | + |
| 197 | +void lept_set_boolean(lept_value* v, int b) { |
| 198 | + lept_free(v); |
| 199 | + v->type = b ? LEPT_TRUE : LEPT_FALSE; |
| 200 | +} |
| 201 | + |
| 202 | +double lept_get_number(const lept_value* v) { |
| 203 | + assert(v != NULL && v->type == LEPT_NUMBER); |
| 204 | + return v->u.n; |
| 205 | +} |
| 206 | + |
| 207 | +void lept_set_number(lept_value* v, double n) { |
| 208 | + lept_free(v); |
| 209 | + v->u.n = n; |
| 210 | + v->type = LEPT_NUMBER; |
| 211 | +} |
| 212 | + |
| 213 | +const char* lept_get_string(const lept_value* v) { |
| 214 | + assert(v != NULL && v->type == LEPT_STRING); |
| 215 | + return v->u.s.s; |
| 216 | +} |
| 217 | + |
| 218 | +size_t lept_get_string_length(const lept_value* v) { |
| 219 | + assert(v != NULL && v->type == LEPT_STRING); |
| 220 | + return v->u.s.len; |
| 221 | +} |
| 222 | + |
| 223 | +void lept_set_string(lept_value* v, const char* s, size_t len) { |
| 224 | + assert(v != NULL && (s != NULL || len == 0)); |
| 225 | + lept_free(v); |
| 226 | + v->u.s.s = (char*)malloc(len + 1); |
| 227 | + memcpy(v->u.s.s, s, len); |
| 228 | + v->u.s.s[len] = '\0'; |
| 229 | + v->u.s.len = len; |
| 230 | + v->type = LEPT_STRING; |
| 231 | +} |
0 commit comments