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 |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 77 | #define INDENT_SPACES_STRING " " |
| 78 | #define INDENT_TABS_STRING "\t\t\t\t\t\t\t\t" |
| 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 | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 115 | struct { |
| 116 | int remaining_argc; |
| 117 | char** remaining_argv; |
| 118 | |
| 119 | bool compact; |
| 120 | size_t indent; |
| 121 | bool tabs; |
| 122 | } flags = {0}; |
| 123 | |
| 124 | const char* // |
| 125 | parse_flags(int argc, char** argv) { |
| 126 | bool explicit_indent = false; |
| 127 | |
| 128 | int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. |
| 129 | for (; c < argc; c++) { |
| 130 | char* arg = argv[c]; |
| 131 | if (*arg++ != '-') { |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | // A double-dash "--foo" is equivalent to a single-dash "-foo". As special |
| 136 | // cases, a bare "-" is not a flag (some programs may interpret it as |
| 137 | // stdin) and a bare "--" means to stop parsing flags. |
| 138 | if (*arg == '\x00') { |
| 139 | break; |
| 140 | } else if (*arg == '-') { |
| 141 | arg++; |
| 142 | if (*arg == '\x00') { |
| 143 | c++; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (!strcmp(arg, "c") || !strcmp(arg, "compact")) { |
| 149 | flags.compact = true; |
| 150 | continue; |
| 151 | } |
| 152 | if (!strncmp(arg, "i=", 2) || !strncmp(arg, "indent=", 7)) { |
| 153 | while (*arg++ != '=') { |
| 154 | } |
| 155 | if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) { |
| 156 | flags.indent = arg[0] - '0'; |
| 157 | explicit_indent = true; |
| 158 | continue; |
| 159 | } |
| 160 | } |
| 161 | if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) { |
| 162 | flags.tabs = true; |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | return "main: unrecognized flag argument"; |
| 167 | } |
| 168 | |
| 169 | flags.remaining_argc = argc - c; |
| 170 | flags.remaining_argv = argv + c; |
| 171 | if (!explicit_indent) { |
| 172 | flags.indent = flags.tabs ? 1 : 4; |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 177 | const char* // |
| 178 | initialize_globals(int argc, char** argv) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 179 | dst = wuffs_base__make_io_buffer( |
| 180 | wuffs_base__make_slice_u8(dst_array, DST_BUFFER_SIZE), |
| 181 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 182 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 183 | src = wuffs_base__make_io_buffer( |
| 184 | wuffs_base__make_slice_u8(src_array, SRC_BUFFER_SIZE), |
| 185 | wuffs_base__empty_io_buffer_meta()); |
| 186 | |
| 187 | tok = wuffs_base__make_token_buffer( |
| 188 | wuffs_base__make_slice_token(tok_array, TOKEN_BUFFER_SIZE), |
| 189 | wuffs_base__empty_token_buffer_meta()); |
| 190 | |
| 191 | curr_token_end_src_index = 0; |
| 192 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 193 | depth = 0; |
| 194 | |
| 195 | ctx = context::none; |
| 196 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 197 | TRY(parse_flags(argc, argv)); |
| 198 | if (flags.remaining_argc > 0) { |
| 199 | return "main: bad argument: use \"program < input\", not \"program input\""; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 200 | } |
| 201 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 202 | return dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
| 203 | .message(); |
| 204 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 205 | |
| 206 | // ---- |
| 207 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 208 | const char* // |
| 209 | read_src() { |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 210 | if (src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 211 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 212 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 213 | src.compact(); |
| 214 | if (src.meta.wi >= src.data.len) { |
| 215 | return "main: src buffer is full"; |
| 216 | } |
| 217 | size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t), |
| 218 | src.data.len - src.meta.wi, stdin); |
| 219 | src.meta.wi += n; |
Nigel Tao | 6730656 | 2020-02-19 14:04:49 +1100 | [diff] [blame] | 220 | src.meta.closed = feof(stdin); |
| 221 | if ((n == 0) && !src.meta.closed) { |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 222 | return "main: read error"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 223 | } |
| 224 | return nullptr; |
| 225 | } |
| 226 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 227 | const char* // |
| 228 | flush_dst() { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 229 | size_t n = dst.meta.wi - dst.meta.ri; |
| 230 | if (n > 0) { |
| 231 | size_t i = fwrite(dst.data.ptr + dst.meta.ri, sizeof(uint8_t), n, stdout); |
| 232 | dst.meta.ri += i; |
| 233 | if (i != n) { |
| 234 | return "main: write error"; |
| 235 | } |
| 236 | dst.compact(); |
| 237 | } |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 241 | const char* // |
| 242 | write_dst(const void* s, size_t n) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 243 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 244 | while (n > 0) { |
| 245 | size_t i = dst.writer_available(); |
| 246 | if (i == 0) { |
| 247 | const char* z = flush_dst(); |
| 248 | if (z) { |
| 249 | return z; |
| 250 | } |
| 251 | i = dst.writer_available(); |
| 252 | if (i == 0) { |
| 253 | return "main: dst buffer is full"; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (i > n) { |
| 258 | i = n; |
| 259 | } |
| 260 | memcpy(dst.data.ptr + dst.meta.wi, p, i); |
| 261 | dst.meta.wi += i; |
| 262 | p += i; |
| 263 | n -= i; |
| 264 | } |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
| 268 | // ---- |
| 269 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 270 | uint8_t // |
| 271 | hex_digit(uint8_t nibble) { |
Nigel Tao | b5461bd | 2020-02-21 14:13:37 +1100 | [diff] [blame] | 272 | nibble &= 0x0F; |
| 273 | if (nibble <= 9) { |
| 274 | return '0' + nibble; |
| 275 | } |
| 276 | return ('A' - 10) + nibble; |
| 277 | } |
| 278 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 279 | const char* // |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 280 | handle_unicode_code_point(uint32_t ucp) { |
| 281 | if (ucp < 0x0020) { |
| 282 | switch (ucp) { |
| 283 | case '\b': |
| 284 | return write_dst("\\b", 2); |
| 285 | case '\f': |
| 286 | return write_dst("\\f", 2); |
| 287 | case '\n': |
| 288 | return write_dst("\\n", 2); |
| 289 | case '\r': |
| 290 | return write_dst("\\r", 2); |
| 291 | case '\t': |
| 292 | return write_dst("\\t", 2); |
| 293 | default: { |
| 294 | // Other bytes less than 0x0020 are valid UTF-8 but not valid in a |
| 295 | // JSON string. They need to remain escaped. |
| 296 | uint8_t esc6[6]; |
| 297 | esc6[0] = '\\'; |
| 298 | esc6[1] = 'u'; |
| 299 | esc6[2] = '0'; |
| 300 | esc6[3] = '0'; |
| 301 | esc6[4] = hex_digit(ucp >> 4); |
| 302 | esc6[5] = hex_digit(ucp >> 0); |
| 303 | return write_dst(&esc6[0], 6); |
| 304 | } |
| 305 | } |
| 306 | |
Nigel Tao | b9ad34f | 2020-03-03 12:44:01 +1100 | [diff] [blame^] | 307 | } else if (ucp == '\"') { |
| 308 | return write_dst("\\\"", 2); |
| 309 | |
| 310 | } else if (ucp == '\\') { |
| 311 | return write_dst("\\\\", 2); |
| 312 | |
| 313 | } else { |
| 314 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 315 | size_t n = wuffs_base__utf_8__encode( |
| 316 | wuffs_base__make_slice_u8(&u[0], |
| 317 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 318 | ucp); |
| 319 | if (n > 0) { |
| 320 | return write_dst(&u[0], n); |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 321 | } |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 322 | } |
| 323 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 324 | return "main: internal error: unexpected Unicode code point"; |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | const char* // |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 328 | handle_token(wuffs_base__token t) { |
| 329 | do { |
| 330 | uint64_t vbc = t.value_base_category(); |
| 331 | uint64_t vbd = t.value_base_detail(); |
| 332 | uint64_t len = t.length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 333 | |
| 334 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 335 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 336 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 337 | if (depth <= 0) { |
| 338 | return "main: internal error: inconsistent depth"; |
| 339 | } |
| 340 | depth--; |
| 341 | |
| 342 | // Write preceding whitespace. |
| 343 | if ((ctx != context::in_list_after_bracket) && |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 344 | (ctx != context::in_dict_after_brace) && !flags.compact) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 345 | TRY(write_dst("\n", 1)); |
| 346 | for (size_t i = 0; i < depth; i++) { |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 347 | TRY(write_dst(flags.tabs ? INDENT_TABS_STRING : INDENT_SPACES_STRING, |
| 348 | flags.indent)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 352 | TRY(write_dst( |
| 353 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", 1)); |
| 354 | ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 355 | ? context::in_list_after_value |
| 356 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 357 | goto after_value; |
| 358 | } |
| 359 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 360 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
| 361 | // continuation of a multi-token chain. |
| 362 | if (t.link_prev()) { |
| 363 | // No-op. |
| 364 | } else if (ctx == context::in_dict_after_key) { |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 365 | TRY(write_dst(": ", flags.compact ? 1 : 2)); |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 366 | } else if (ctx != context::none) { |
| 367 | if ((ctx != context::in_list_after_bracket) && |
| 368 | (ctx != context::in_dict_after_brace)) { |
| 369 | TRY(write_dst(",", 1)); |
| 370 | } |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 371 | if (!flags.compact) { |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 372 | TRY(write_dst("\n", 1)); |
| 373 | for (size_t i = 0; i < depth; i++) { |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 374 | TRY(write_dst(flags.tabs ? INDENT_TABS_STRING : INDENT_SPACES_STRING, |
| 375 | flags.indent)); |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 376 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | // Handle the token itself: either a container ('[' or '{') or a simple |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 381 | // value: string (a chain of raw or escaped parts), literal or number. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 382 | switch (vbc) { |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 383 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
| 384 | TRY(write_dst( |
| 385 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", 1)); |
| 386 | depth++; |
| 387 | ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 388 | ? context::in_list_after_bracket |
| 389 | : context::in_dict_after_brace; |
| 390 | return nullptr; |
| 391 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 392 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 393 | if (!t.link_prev()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 394 | TRY(write_dst("\"", 1)); |
| 395 | } |
Nigel Tao | cb37a56 | 2020-02-28 09:56:24 +1100 | [diff] [blame] | 396 | |
| 397 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) { |
| 398 | // No-op. |
| 399 | } else if (vbd & |
| 400 | WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
| 401 | TRY(write_dst(src.data.ptr + curr_token_end_src_index - len, len)); |
| 402 | } else { |
| 403 | return "main: internal error: unexpected string-token conversion"; |
| 404 | } |
| 405 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 406 | if (t.link_next()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 407 | return nullptr; |
| 408 | } |
| 409 | TRY(write_dst("\"", 1)); |
| 410 | goto after_value; |
| 411 | |
| 412 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
| 413 | return handle_unicode_code_point(vbd); |
| 414 | |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 415 | case WUFFS_BASE__TOKEN__VBC__LITERAL: |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 416 | case WUFFS_BASE__TOKEN__VBC__NUMBER: |
| 417 | TRY(write_dst(src.data.ptr + curr_token_end_src_index - len, len)); |
| 418 | goto after_value; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // Return an error if we didn't match the (vbc, vbd) pair. |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 422 | return "main: internal error: unexpected token"; |
| 423 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 424 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 425 | // Book-keeping after completing a value (whether a container value or a |
| 426 | // simple value). Empty parent containers are no longer empty. If the parent |
| 427 | // container is a "{...}" object, toggle between keys and values. |
| 428 | after_value: |
| 429 | if (depth == 0) { |
| 430 | return eod; |
| 431 | } |
| 432 | switch (ctx) { |
| 433 | case context::in_list_after_bracket: |
| 434 | ctx = context::in_list_after_value; |
| 435 | break; |
| 436 | case context::in_dict_after_brace: |
| 437 | ctx = context::in_dict_after_key; |
| 438 | break; |
| 439 | case context::in_dict_after_key: |
| 440 | ctx = context::in_dict_after_value; |
| 441 | break; |
| 442 | case context::in_dict_after_value: |
| 443 | ctx = context::in_dict_after_key; |
| 444 | break; |
| 445 | } |
| 446 | return nullptr; |
| 447 | } |
| 448 | |
| 449 | const char* // |
| 450 | main1(int argc, char** argv) { |
| 451 | TRY(initialize_globals(argc, argv)); |
| 452 | |
| 453 | while (true) { |
| 454 | wuffs_base__status status = dec.decode_tokens(&tok, &src); |
| 455 | |
| 456 | while (tok.meta.ri < tok.meta.wi) { |
| 457 | wuffs_base__token t = tok.data.ptr[tok.meta.ri++]; |
| 458 | uint64_t n = t.length(); |
| 459 | if ((src.meta.ri - curr_token_end_src_index) < n) { |
| 460 | return "main: internal error: inconsistent src indexes"; |
| 461 | } |
| 462 | curr_token_end_src_index += n; |
| 463 | |
| 464 | if (t.value() == 0) { |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | const char* z = handle_token(t); |
| 469 | if (z == nullptr) { |
| 470 | continue; |
| 471 | } else if (z == eod) { |
| 472 | break; |
| 473 | } |
| 474 | return z; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 475 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 476 | |
| 477 | if (status.repr == nullptr) { |
| 478 | break; |
| 479 | } else if (status.repr == wuffs_base__suspension__short_read) { |
| 480 | if (curr_token_end_src_index != src.meta.ri) { |
| 481 | return "main: internal error: inconsistent src indexes"; |
| 482 | } |
| 483 | TRY(read_src()); |
| 484 | curr_token_end_src_index = src.meta.ri; |
| 485 | } else if (status.repr == wuffs_base__suspension__short_write) { |
| 486 | tok.compact(); |
| 487 | } else { |
| 488 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 489 | } |
| 490 | } |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 491 | |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 492 | // Consume an optional whitespace trailer. This isn't part of the JSON spec, |
| 493 | // but it works better with line oriented Unix tools (such as "echo 123 | |
| 494 | // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which |
| 495 | // can accidentally contain trailing whitespace. |
| 496 | // |
| 497 | // A whitespace trailer is zero or more ' ' and then zero or one '\n'. |
| 498 | while (true) { |
| 499 | if (src.meta.ri < src.meta.wi) { |
| 500 | uint8_t c = src.data.ptr[src.meta.ri]; |
| 501 | if (c == ' ') { |
| 502 | src.meta.ri++; |
| 503 | continue; |
| 504 | } else if (c == '\n') { |
| 505 | src.meta.ri++; |
| 506 | break; |
| 507 | } |
| 508 | // The "exhausted the input" check below will fail. |
| 509 | break; |
| 510 | } else if (src.meta.closed) { |
| 511 | break; |
| 512 | } |
| 513 | TRY(read_src()); |
| 514 | } |
| 515 | |
| 516 | // Check that we've exhausted the input. |
| 517 | if ((src.meta.ri < src.meta.wi) || !src.meta.closed) { |
| 518 | return "main: valid JSON followed by further (unexpected) data"; |
| 519 | } |
| 520 | |
| 521 | // Check that we've used all of the decoded tokens, other than trailing |
| 522 | // filler tokens. For example, a bare `"foo"` string is valid JSON, but even |
| 523 | // without a trailing '\n', the Wuffs JSON parser emits a filler token for |
| 524 | // the final '\"'. |
| 525 | for (; tok.meta.ri < tok.meta.wi; tok.meta.ri++) { |
| 526 | if (tok.data.ptr[tok.meta.ri].value_base_category() != |
| 527 | WUFFS_BASE__TOKEN__VBC__FILLER) { |
| 528 | return "main: internal error: decoded OK but unprocessed tokens remain"; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 533 | } |
| 534 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 535 | int // |
| 536 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 537 | if (!status_msg) { |
| 538 | return 0; |
| 539 | } |
| 540 | size_t n = strnlen(status_msg, 2047); |
| 541 | if (n >= 2047) { |
| 542 | status_msg = "main: internal error: error message is too long"; |
| 543 | n = strnlen(status_msg, 2047); |
| 544 | } |
| 545 | fprintf(stderr, "%s\n", status_msg); |
| 546 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 547 | // formatted or unsupported input. |
| 548 | // |
| 549 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 550 | // run-time checks found that an internal invariant did not hold. |
| 551 | // |
| 552 | // Automated testing, including badly formatted inputs, can therefore |
| 553 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 554 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 555 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 556 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 557 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 558 | } |
| 559 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 560 | int // |
| 561 | main(int argc, char** argv) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 562 | const char* z0 = main1(argc, argv); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 563 | const char* z1 = write_dst("\n", 1); |
| 564 | const char* z2 = flush_dst(); |
| 565 | int exit_code = compute_exit_code(z0 ? z0 : (z1 ? z1 : z2)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 566 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 567 | } |