Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_JSON_JSON_READER_H |
| 35 | #define GRPC_CORE_LIB_JSON_JSON_READER_H |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 36 | |
| 37 | #include <grpc/support/port_platform.h> |
| 38 | #include "src/core/json/json_common.h" |
| 39 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 40 | typedef enum { |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 41 | GRPC_JSON_STATE_OBJECT_KEY_BEGIN, |
| 42 | GRPC_JSON_STATE_OBJECT_KEY_STRING, |
| 43 | GRPC_JSON_STATE_OBJECT_KEY_END, |
| 44 | GRPC_JSON_STATE_VALUE_BEGIN, |
| 45 | GRPC_JSON_STATE_VALUE_STRING, |
| 46 | GRPC_JSON_STATE_STRING_ESCAPE, |
| 47 | GRPC_JSON_STATE_STRING_ESCAPE_U1, |
| 48 | GRPC_JSON_STATE_STRING_ESCAPE_U2, |
| 49 | GRPC_JSON_STATE_STRING_ESCAPE_U3, |
| 50 | GRPC_JSON_STATE_STRING_ESCAPE_U4, |
| 51 | GRPC_JSON_STATE_VALUE_NUMBER, |
| 52 | GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL, |
| 53 | GRPC_JSON_STATE_VALUE_NUMBER_ZERO, |
| 54 | GRPC_JSON_STATE_VALUE_NUMBER_DOT, |
| 55 | GRPC_JSON_STATE_VALUE_NUMBER_E, |
| 56 | GRPC_JSON_STATE_VALUE_NUMBER_EPM, |
| 57 | GRPC_JSON_STATE_VALUE_TRUE_R, |
| 58 | GRPC_JSON_STATE_VALUE_TRUE_U, |
| 59 | GRPC_JSON_STATE_VALUE_TRUE_E, |
| 60 | GRPC_JSON_STATE_VALUE_FALSE_A, |
| 61 | GRPC_JSON_STATE_VALUE_FALSE_L, |
| 62 | GRPC_JSON_STATE_VALUE_FALSE_S, |
| 63 | GRPC_JSON_STATE_VALUE_FALSE_E, |
| 64 | GRPC_JSON_STATE_VALUE_NULL_U, |
| 65 | GRPC_JSON_STATE_VALUE_NULL_L1, |
| 66 | GRPC_JSON_STATE_VALUE_NULL_L2, |
| 67 | GRPC_JSON_STATE_VALUE_END, |
| 68 | GRPC_JSON_STATE_END |
| 69 | } grpc_json_reader_state; |
| 70 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | enum { |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 72 | /* The first non-unicode value is 0x110000. But let's pick |
| 73 | * a value high enough to start our error codes from. These |
| 74 | * values are safe to return from the read_char function. |
| 75 | */ |
| 76 | GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0, |
| 77 | GRPC_JSON_READ_CHAR_EAGAIN, |
| 78 | GRPC_JSON_READ_CHAR_ERROR |
| 79 | }; |
| 80 | |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 81 | struct grpc_json_reader; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 82 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | typedef struct grpc_json_reader_vtable { |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 84 | /* Clears your internal string scratchpad. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | void (*string_clear)(void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 86 | /* Adds a char to the string scratchpad. */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 87 | void (*string_add_char)(void *userdata, uint32_t c); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 88 | /* Adds a utf32 char to the string scratchpad. */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 89 | void (*string_add_utf32)(void *userdata, uint32_t c); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 90 | /* Reads a character from your input. May be utf-8, 16 or 32. */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 91 | uint32_t (*read_char)(void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 92 | /* Starts a container of type GRPC_JSON_ARRAY or GRPC_JSON_OBJECT. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | void (*container_begins)(void *userdata, grpc_json_type type); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 94 | /* Ends the current container. Must return the type of its parent. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | grpc_json_type (*container_ends)(void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 96 | /* Your internal string scratchpad is an object's key. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | void (*set_key)(void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 98 | /* Your internal string scratchpad is a string value. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | void (*set_string)(void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 100 | /* Your internal string scratchpad is a numerical value. Return 1 if valid. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | int (*set_number)(void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 102 | /* Sets the values true, false or null. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | void (*set_true)(void *userdata); |
| 104 | void (*set_false)(void *userdata); |
| 105 | void (*set_null)(void *userdata); |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 106 | } grpc_json_reader_vtable; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 107 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | typedef struct grpc_json_reader { |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 109 | /* That structure is fully private, and initialized by grpc_json_reader_init. |
| 110 | * The definition is public so you can put it on your stack. |
| 111 | */ |
| 112 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 113 | void *userdata; |
| 114 | grpc_json_reader_vtable *vtable; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 115 | int depth; |
| 116 | int in_object; |
| 117 | int in_array; |
| 118 | int escaped_string_was_key; |
| 119 | int container_just_begun; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 120 | uint16_t unicode_char, unicode_high_surrogate; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 121 | grpc_json_reader_state state; |
| 122 | } grpc_json_reader; |
| 123 | |
| 124 | /* The return type of the parser. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | typedef enum { |
| 126 | GRPC_JSON_DONE, /* The parser finished successfully. */ |
| 127 | GRPC_JSON_EAGAIN, /* The parser yields to get more data. */ |
| 128 | GRPC_JSON_READ_ERROR, /* The parser passes through a read error. */ |
| 129 | GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */ |
| 130 | GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */ |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 131 | } grpc_json_reader_status; |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 132 | |
| 133 | /* Call this function to start parsing the input. It will return the following: |
| 134 | * . GRPC_JSON_DONE if the input got eof, and the parsing finished |
| 135 | * successfully. |
| 136 | * . GRPC_JSON_EAGAIN if the read_char function returned again. Call the |
| 137 | * parser again as needed. It is okay to call the parser in polling mode, |
| 138 | * although a bit dull. |
| 139 | * . GRPC_JSON_READ_ERROR if the read_char function returned an error. The |
| 140 | * state isn't broken however, and the function can be called again if the |
| 141 | * error has been corrected. But please use the EAGAIN feature instead for |
| 142 | * consistency. |
| 143 | * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid. |
| 144 | * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid |
| 145 | * internal state. |
| 146 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 148 | |
| 149 | /* Call this function to initialize the reader structure. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | void grpc_json_reader_init(grpc_json_reader *reader, |
| 151 | grpc_json_reader_vtable *vtable, void *userdata); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 152 | |
| 153 | /* You may call this from the read_char callback if you don't know where is the |
| 154 | * end of your input stream, and you'd like the json reader to hint you that it |
| 155 | * has completed reading its input, so you can return an EOF to it. Note that |
| 156 | * there might still be trailing whitespaces after that point. |
| 157 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | int grpc_json_reader_is_complete(grpc_json_reader *reader); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 159 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 160 | #endif /* GRPC_CORE_LIB_JSON_JSON_READER_H */ |