Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1 | // Copyright 2020 The Wuffs Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // ---------------- |
| 16 | |
| 17 | /* |
| 18 | jsonptr is a JSON formatter (pretty-printer). |
| 19 | |
Nigel Tao | c5b3a9e | 2020-02-24 11:54:35 +1100 | [diff] [blame] | 20 | As of 2020-02-24, this program passes all 318 "test_parsing" cases from the |
| 21 | JSON test suite (https://github.com/nst/JSONTestSuite), an appendix to the |
| 22 | "Parsing JSON is a Minefield" article (http://seriot.ch/parsing_json.php) that |
| 23 | was first published on 2016-10-26 and updated on 2018-03-30. |
| 24 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 25 | This example program differs from most other example Wuffs programs in that it |
| 26 | is written in C++, not C. |
| 27 | |
| 28 | $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out |
| 29 | |
| 30 | for a C++ compiler $CXX, such as clang++ or g++. |
Nigel Tao | 569a294 | 2020-02-23 23:13:51 +1100 | [diff] [blame] | 31 | |
| 32 | After modifying this program, run "build-example.sh example/jsonptr/" and then |
| 33 | "script/run-json-test-suite.sh" to catch correctness regressions. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <inttypes.h> |
| 37 | #include <stdio.h> |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 38 | #include <string.h> |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 39 | |
| 40 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 41 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 42 | // |
| 43 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 44 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 45 | // compiling it. |
| 46 | #define WUFFS_IMPLEMENTATION |
| 47 | |
| 48 | // Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of |
| 49 | // release/c/etc.c whitelist which parts of Wuffs to build. That file contains |
| 50 | // the entire Wuffs standard library, implementing a variety of codecs and file |
| 51 | // formats. Without this macro definition, an optimizing compiler or linker may |
| 52 | // very well discard Wuffs code for unused codecs, but listing the Wuffs |
| 53 | // modules we use makes that process explicit. Preprocessing means that such |
| 54 | // code simply isn't compiled. |
| 55 | #define WUFFS_CONFIG__MODULES |
| 56 | #define WUFFS_CONFIG__MODULE__BASE |
| 57 | #define WUFFS_CONFIG__MODULE__JSON |
| 58 | |
| 59 | // If building this program in an environment that doesn't easily accommodate |
| 60 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 61 | // program to generate a stand-alone C++ file. |
| 62 | #include "../../release/c/wuffs-unsupported-snapshot.c" |
| 63 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 64 | #define TRY(error_msg) \ |
| 65 | do { \ |
| 66 | const char* z = error_msg; \ |
| 67 | if (z) { \ |
| 68 | return z; \ |
| 69 | } \ |
| 70 | } while (false) |
| 71 | |
| 72 | static const char* eod = "main: end of data"; |
| 73 | |
| 74 | // ---- |
| 75 | |
| 76 | #define MAX_INDENT 8 |
| 77 | #define INDENT_STRING " " |
| 78 | size_t indent; |
| 79 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 80 | #ifndef DST_BUFFER_SIZE |
| 81 | #define DST_BUFFER_SIZE (32 * 1024) |
| 82 | #endif |
| 83 | #ifndef SRC_BUFFER_SIZE |
| 84 | #define SRC_BUFFER_SIZE (32 * 1024) |
| 85 | #endif |
| 86 | #ifndef TOKEN_BUFFER_SIZE |
| 87 | #define TOKEN_BUFFER_SIZE (4 * 1024) |
| 88 | #endif |
| 89 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 90 | uint8_t dst_array[DST_BUFFER_SIZE]; |
| 91 | uint8_t src_array[SRC_BUFFER_SIZE]; |
| 92 | wuffs_base__token tok_array[TOKEN_BUFFER_SIZE]; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 93 | |
| 94 | wuffs_base__io_buffer dst; |
| 95 | wuffs_base__io_buffer src; |
| 96 | wuffs_base__token_buffer tok; |
| 97 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 98 | // curr_token_end_src_index is the src.data.ptr index of the end of the current |
| 99 | // token. An invariant is that (curr_token_end_src_index <= src.meta.ri). |
| 100 | size_t curr_token_end_src_index; |
| 101 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 102 | uint64_t depth; |
| 103 | |
| 104 | enum class context { |
| 105 | none, |
| 106 | in_list_after_bracket, |
| 107 | in_list_after_value, |
| 108 | in_dict_after_brace, |
| 109 | in_dict_after_key, |
| 110 | in_dict_after_value, |
| 111 | } ctx; |
| 112 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 113 | wuffs_json__decoder dec; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 114 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 115 | const char* // |
| 116 | initialize_globals(int argc, char** argv) { |
| 117 | indent = 4; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 118 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 119 | dst = wuffs_base__make_io_buffer( |
| 120 | wuffs_base__make_slice_u8(dst_array, DST_BUFFER_SIZE), |
| 121 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 122 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 123 | src = wuffs_base__make_io_buffer( |
| 124 | wuffs_base__make_slice_u8(src_array, SRC_BUFFER_SIZE), |
| 125 | wuffs_base__empty_io_buffer_meta()); |
| 126 | |
| 127 | tok = wuffs_base__make_token_buffer( |
| 128 | wuffs_base__make_slice_token(tok_array, TOKEN_BUFFER_SIZE), |
| 129 | wuffs_base__empty_token_buffer_meta()); |
| 130 | |
| 131 | curr_token_end_src_index = 0; |
| 132 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 133 | depth = 0; |
| 134 | |
| 135 | ctx = context::none; |
| 136 | |
| 137 | return dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
| 138 | .message(); |
| 139 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 140 | |
| 141 | // ---- |
| 142 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 143 | const char* // |
| 144 | read_src() { |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 145 | if (src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 146 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 147 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 148 | src.compact(); |
| 149 | if (src.meta.wi >= src.data.len) { |
| 150 | return "main: src buffer is full"; |
| 151 | } |
| 152 | size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t), |
| 153 | src.data.len - src.meta.wi, stdin); |
| 154 | src.meta.wi += n; |
Nigel Tao | 6730656 | 2020-02-19 14:04:49 +1100 | [diff] [blame] | 155 | src.meta.closed = feof(stdin); |
| 156 | if ((n == 0) && !src.meta.closed) { |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 157 | return "main: read error"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 158 | } |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 162 | const char* // |
| 163 | flush_dst() { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 164 | size_t n = dst.meta.wi - dst.meta.ri; |
| 165 | if (n > 0) { |
| 166 | size_t i = fwrite(dst.data.ptr + dst.meta.ri, sizeof(uint8_t), n, stdout); |
| 167 | dst.meta.ri += i; |
| 168 | if (i != n) { |
| 169 | return "main: write error"; |
| 170 | } |
| 171 | dst.compact(); |
| 172 | } |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 176 | const char* // |
| 177 | write_dst(const void* s, size_t n) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 178 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 179 | while (n > 0) { |
| 180 | size_t i = dst.writer_available(); |
| 181 | if (i == 0) { |
| 182 | const char* z = flush_dst(); |
| 183 | if (z) { |
| 184 | return z; |
| 185 | } |
| 186 | i = dst.writer_available(); |
| 187 | if (i == 0) { |
| 188 | return "main: dst buffer is full"; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (i > n) { |
| 193 | i = n; |
| 194 | } |
| 195 | memcpy(dst.data.ptr + dst.meta.wi, p, i); |
| 196 | dst.meta.wi += i; |
| 197 | p += i; |
| 198 | n -= i; |
| 199 | } |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | // ---- |
| 204 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 205 | uint8_t // |
| 206 | hex_digit(uint8_t nibble) { |
Nigel Tao | b5461bd | 2020-02-21 14:13:37 +1100 | [diff] [blame] | 207 | nibble &= 0x0F; |
| 208 | if (nibble <= 9) { |
| 209 | return '0' + nibble; |
| 210 | } |
| 211 | return ('A' - 10) + nibble; |
| 212 | } |
| 213 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 214 | const char* // |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 215 | handle_unicode_code_point(uint32_t ucp) { |
| 216 | if (ucp < 0x0020) { |
| 217 | switch (ucp) { |
| 218 | case '\b': |
| 219 | return write_dst("\\b", 2); |
| 220 | case '\f': |
| 221 | return write_dst("\\f", 2); |
| 222 | case '\n': |
| 223 | return write_dst("\\n", 2); |
| 224 | case '\r': |
| 225 | return write_dst("\\r", 2); |
| 226 | case '\t': |
| 227 | return write_dst("\\t", 2); |
| 228 | default: { |
| 229 | // Other bytes less than 0x0020 are valid UTF-8 but not valid in a |
| 230 | // JSON string. They need to remain escaped. |
| 231 | uint8_t esc6[6]; |
| 232 | esc6[0] = '\\'; |
| 233 | esc6[1] = 'u'; |
| 234 | esc6[2] = '0'; |
| 235 | esc6[3] = '0'; |
| 236 | esc6[4] = hex_digit(ucp >> 4); |
| 237 | esc6[5] = hex_digit(ucp >> 0); |
| 238 | return write_dst(&esc6[0], 6); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | } else if (ucp <= 0x007F) { |
| 243 | switch (ucp) { |
| 244 | case '\"': |
| 245 | return write_dst("\\\"", 2); |
| 246 | case '\\': |
| 247 | return write_dst("\\\\", 2); |
| 248 | default: { |
| 249 | // The UTF-8 encoding takes 1 byte. |
| 250 | uint8_t esc0 = (uint8_t)(ucp); |
| 251 | return write_dst(&esc0, 1); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | } else if (ucp <= 0x07FF) { |
| 256 | // The UTF-8 encoding takes 2 bytes. |
| 257 | uint8_t esc2[2]; |
| 258 | esc2[0] = 0xC0 | (uint8_t)((ucp >> 6)); |
| 259 | esc2[1] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F); |
| 260 | return write_dst(&esc2[0], 2); |
| 261 | |
| 262 | } else if (ucp <= 0xFFFF) { |
| 263 | if ((0xD800 <= ucp) && (ucp <= 0xDFFF)) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 264 | return "main: internal error: unexpected Unicode surrogate"; |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 265 | } |
| 266 | // The UTF-8 encoding takes 3 bytes. |
| 267 | uint8_t esc3[3]; |
| 268 | esc3[0] = 0xE0 | (uint8_t)((ucp >> 12)); |
| 269 | esc3[1] = 0x80 | (uint8_t)((ucp >> 6) & 0x3F); |
| 270 | esc3[2] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F); |
| 271 | return write_dst(&esc3[0], 3); |
| 272 | |
| 273 | } else if (ucp <= 0x10FFFF) { |
| 274 | // The UTF-8 encoding takes 4 bytes. |
| 275 | uint8_t esc4[4]; |
| 276 | esc4[0] = 0xF0 | (uint8_t)((ucp >> 18)); |
| 277 | esc4[1] = 0x80 | (uint8_t)((ucp >> 12) & 0x3F); |
| 278 | esc4[2] = 0x80 | (uint8_t)((ucp >> 6) & 0x3F); |
| 279 | esc4[3] = 0x80 | (uint8_t)((ucp >> 0) & 0x3F); |
| 280 | return write_dst(&esc4[0], 4); |
| 281 | } |
| 282 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 283 | return "main: internal error: unexpected Unicode code point"; |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | const char* // |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 287 | handle_token(wuffs_base__token t) { |
| 288 | do { |
| 289 | uint64_t vbc = t.value_base_category(); |
| 290 | uint64_t vbd = t.value_base_detail(); |
| 291 | uint64_t len = t.length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 292 | |
| 293 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 294 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 295 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 296 | if (depth <= 0) { |
| 297 | return "main: internal error: inconsistent depth"; |
| 298 | } |
| 299 | depth--; |
| 300 | |
| 301 | // Write preceding whitespace. |
| 302 | if ((ctx != context::in_list_after_bracket) && |
| 303 | (ctx != context::in_dict_after_brace)) { |
| 304 | TRY(write_dst("\n", 1)); |
| 305 | for (size_t i = 0; i < depth; i++) { |
| 306 | TRY(write_dst(INDENT_STRING, indent)); |
| 307 | } |
| 308 | } |
| 309 | |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 310 | TRY(write_dst( |
| 311 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", 1)); |
| 312 | ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 313 | ? context::in_list_after_value |
| 314 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 315 | goto after_value; |
| 316 | } |
| 317 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 318 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
| 319 | // continuation of a multi-token chain. |
| 320 | if (t.link_prev()) { |
| 321 | // No-op. |
| 322 | } else if (ctx == context::in_dict_after_key) { |
| 323 | TRY(write_dst(": ", 2)); |
| 324 | } else if (ctx != context::none) { |
| 325 | if ((ctx != context::in_list_after_bracket) && |
| 326 | (ctx != context::in_dict_after_brace)) { |
| 327 | TRY(write_dst(",", 1)); |
| 328 | } |
| 329 | TRY(write_dst("\n", 1)); |
| 330 | for (size_t i = 0; i < depth; i++) { |
| 331 | TRY(write_dst(INDENT_STRING, indent)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | // Handle the token itself: either a container ('[' or '{') or a simple |
| 336 | // value (number, string or literal). |
| 337 | switch (vbc) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 338 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 339 | if (!t.link_prev()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 340 | TRY(write_dst("\"", 1)); |
| 341 | } |
Nigel Tao | cb37a56 | 2020-02-28 09:56:24 +1100 | [diff] [blame] | 342 | |
| 343 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) { |
| 344 | // No-op. |
| 345 | } else if (vbd & |
| 346 | WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
| 347 | TRY(write_dst(src.data.ptr + curr_token_end_src_index - len, len)); |
| 348 | } else { |
| 349 | return "main: internal error: unexpected string-token conversion"; |
| 350 | } |
| 351 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 352 | if (t.link_next()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 353 | return nullptr; |
| 354 | } |
| 355 | TRY(write_dst("\"", 1)); |
| 356 | goto after_value; |
| 357 | |
| 358 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
| 359 | return handle_unicode_code_point(vbd); |
| 360 | |
| 361 | case WUFFS_BASE__TOKEN__VBC__NUMBER: |
| 362 | TRY(write_dst(src.data.ptr + curr_token_end_src_index - len, len)); |
| 363 | goto after_value; |
| 364 | |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 365 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
| 366 | TRY(write_dst( |
| 367 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", 1)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 368 | depth++; |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 369 | ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 370 | ? context::in_list_after_bracket |
| 371 | : context::in_dict_after_brace; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 372 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // Return an error if we didn't match the (vbc, vbd) pair. |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 376 | return "main: internal error: unexpected token"; |
| 377 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 378 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 379 | // Book-keeping after completing a value (whether a container value or a |
| 380 | // simple value). Empty parent containers are no longer empty. If the parent |
| 381 | // container is a "{...}" object, toggle between keys and values. |
| 382 | after_value: |
| 383 | if (depth == 0) { |
| 384 | return eod; |
| 385 | } |
| 386 | switch (ctx) { |
| 387 | case context::in_list_after_bracket: |
| 388 | ctx = context::in_list_after_value; |
| 389 | break; |
| 390 | case context::in_dict_after_brace: |
| 391 | ctx = context::in_dict_after_key; |
| 392 | break; |
| 393 | case context::in_dict_after_key: |
| 394 | ctx = context::in_dict_after_value; |
| 395 | break; |
| 396 | case context::in_dict_after_value: |
| 397 | ctx = context::in_dict_after_key; |
| 398 | break; |
| 399 | } |
| 400 | return nullptr; |
| 401 | } |
| 402 | |
| 403 | const char* // |
| 404 | main1(int argc, char** argv) { |
| 405 | TRY(initialize_globals(argc, argv)); |
| 406 | |
| 407 | while (true) { |
| 408 | wuffs_base__status status = dec.decode_tokens(&tok, &src); |
| 409 | |
| 410 | while (tok.meta.ri < tok.meta.wi) { |
| 411 | wuffs_base__token t = tok.data.ptr[tok.meta.ri++]; |
| 412 | uint64_t n = t.length(); |
| 413 | if ((src.meta.ri - curr_token_end_src_index) < n) { |
| 414 | return "main: internal error: inconsistent src indexes"; |
| 415 | } |
| 416 | curr_token_end_src_index += n; |
| 417 | |
| 418 | if (t.value() == 0) { |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | const char* z = handle_token(t); |
| 423 | if (z == nullptr) { |
| 424 | continue; |
| 425 | } else if (z == eod) { |
| 426 | break; |
| 427 | } |
| 428 | return z; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 429 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 430 | |
| 431 | if (status.repr == nullptr) { |
| 432 | break; |
| 433 | } else if (status.repr == wuffs_base__suspension__short_read) { |
| 434 | if (curr_token_end_src_index != src.meta.ri) { |
| 435 | return "main: internal error: inconsistent src indexes"; |
| 436 | } |
| 437 | TRY(read_src()); |
| 438 | curr_token_end_src_index = src.meta.ri; |
| 439 | } else if (status.repr == wuffs_base__suspension__short_write) { |
| 440 | tok.compact(); |
| 441 | } else { |
| 442 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 443 | } |
| 444 | } |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 445 | |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 446 | // Consume an optional whitespace trailer. This isn't part of the JSON spec, |
| 447 | // but it works better with line oriented Unix tools (such as "echo 123 | |
| 448 | // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which |
| 449 | // can accidentally contain trailing whitespace. |
| 450 | // |
| 451 | // A whitespace trailer is zero or more ' ' and then zero or one '\n'. |
| 452 | while (true) { |
| 453 | if (src.meta.ri < src.meta.wi) { |
| 454 | uint8_t c = src.data.ptr[src.meta.ri]; |
| 455 | if (c == ' ') { |
| 456 | src.meta.ri++; |
| 457 | continue; |
| 458 | } else if (c == '\n') { |
| 459 | src.meta.ri++; |
| 460 | break; |
| 461 | } |
| 462 | // The "exhausted the input" check below will fail. |
| 463 | break; |
| 464 | } else if (src.meta.closed) { |
| 465 | break; |
| 466 | } |
| 467 | TRY(read_src()); |
| 468 | } |
| 469 | |
| 470 | // Check that we've exhausted the input. |
| 471 | if ((src.meta.ri < src.meta.wi) || !src.meta.closed) { |
| 472 | return "main: valid JSON followed by further (unexpected) data"; |
| 473 | } |
| 474 | |
| 475 | // Check that we've used all of the decoded tokens, other than trailing |
| 476 | // filler tokens. For example, a bare `"foo"` string is valid JSON, but even |
| 477 | // without a trailing '\n', the Wuffs JSON parser emits a filler token for |
| 478 | // the final '\"'. |
| 479 | for (; tok.meta.ri < tok.meta.wi; tok.meta.ri++) { |
| 480 | if (tok.data.ptr[tok.meta.ri].value_base_category() != |
| 481 | WUFFS_BASE__TOKEN__VBC__FILLER) { |
| 482 | return "main: internal error: decoded OK but unprocessed tokens remain"; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 487 | } |
| 488 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 489 | int // |
| 490 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 491 | if (!status_msg) { |
| 492 | return 0; |
| 493 | } |
| 494 | size_t n = strnlen(status_msg, 2047); |
| 495 | if (n >= 2047) { |
| 496 | status_msg = "main: internal error: error message is too long"; |
| 497 | n = strnlen(status_msg, 2047); |
| 498 | } |
| 499 | fprintf(stderr, "%s\n", status_msg); |
| 500 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 501 | // formatted or unsupported input. |
| 502 | // |
| 503 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 504 | // run-time checks found that an internal invariant did not hold. |
| 505 | // |
| 506 | // Automated testing, including badly formatted inputs, can therefore |
| 507 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 508 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 509 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 510 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 511 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 512 | } |
| 513 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 514 | int // |
| 515 | main(int argc, char** argv) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 516 | const char* z0 = main1(argc, argv); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 517 | const char* z1 = write_dst("\n", 1); |
| 518 | const char* z2 = flush_dst(); |
| 519 | int exit_code = compute_exit_code(z0 ? z0 : (z1 ? z1 : z2)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 520 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 521 | } |