| 1 | |
|---|
| 2 | #if HAVE_CONFIG_H |
|---|
| 3 | #include <config.h> |
|---|
| 4 | #endif |
|---|
| 5 | |
|---|
| 6 | #include <yaml.h> |
|---|
| 7 | |
|---|
| 8 | #include <assert.h> |
|---|
| 9 | #include <limits.h> |
|---|
| 10 | |
|---|
| 11 | /* |
|---|
| 12 | * Memory management. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | YAML_DECLARE(void *) |
|---|
| 16 | yaml_malloc(size_t size); |
|---|
| 17 | |
|---|
| 18 | YAML_DECLARE(void *) |
|---|
| 19 | yaml_realloc(void *ptr, size_t size); |
|---|
| 20 | |
|---|
| 21 | YAML_DECLARE(void) |
|---|
| 22 | yaml_free(void *ptr); |
|---|
| 23 | |
|---|
| 24 | YAML_DECLARE(yaml_char_t *) |
|---|
| 25 | yaml_strdup(const yaml_char_t *); |
|---|
| 26 | |
|---|
| 27 | /* |
|---|
| 28 | * Error management. |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | #define MEMORY_ERROR_INIT(error) \ |
|---|
| 32 | (memset(&(error), 0, sizeof(error)), \ |
|---|
| 33 | (error).type = YAML_MEMORY_ERROR, \ |
|---|
| 34 | 0) |
|---|
| 35 | |
|---|
| 36 | #define READING_ERROR_INIT(error,error_type,error_problem,error_offset,error_value) \ |
|---|
| 37 | (memset(&(error), 0, sizeof(error)), \ |
|---|
| 38 | (error).type = (error_type), \ |
|---|
| 39 | (error).data.reading.problem = (error_problem), \ |
|---|
| 40 | (error).data.reading.offset = (error_offset), \ |
|---|
| 41 | (error).data.reading.value = (error_value), \ |
|---|
| 42 | 0) |
|---|
| 43 | |
|---|
| 44 | #define LOADING_ERROR_INIT(error,error_type,error_problem,error_problem_mark) \ |
|---|
| 45 | (memset(&(error), 0, sizeof(error)), \ |
|---|
| 46 | (error).type = (error_type), \ |
|---|
| 47 | (error).data.loading.context = NULL, \ |
|---|
| 48 | (error).data.loading.context_mark.index = 0, \ |
|---|
| 49 | (error).data.loading.context_mark.line = 0, \ |
|---|
| 50 | (error).data.loading.context_mark.column = 0, \ |
|---|
| 51 | (error).data.loading.problem = (error_problem), \ |
|---|
| 52 | (error).data.loading.problem_mark = (error_problem_mark), \ |
|---|
| 53 | 0) |
|---|
| 54 | |
|---|
| 55 | #define LOADING_ERROR_WITH_CONTEXT_INIT(error,error_type,error_context,error_context_mark,error_problem,error_problem_mark) \ |
|---|
| 56 | (memset(&(error), 0, sizeof(error)), \ |
|---|
| 57 | (error).type = (error_type), \ |
|---|
| 58 | (error).data.loading.context = (error_context), \ |
|---|
| 59 | (error).data.loading.context_mark = (error_context_mark), \ |
|---|
| 60 | (error).data.loading.problem = (error_problem), \ |
|---|
| 61 | (error).data.loading.problem_mark = (error_problem_mark), \ |
|---|
| 62 | 0) |
|---|
| 63 | |
|---|
| 64 | #define WRITING_ERROR_INIT(error,error_type,error_problem,error_offset) \ |
|---|
| 65 | (memset(&(error), 0, sizeof(error)), \ |
|---|
| 66 | (error).type = (error_type), \ |
|---|
| 67 | (error).data.writing.problem = (error_problem), \ |
|---|
| 68 | (error).data.writing.offset = (error_offset), \ |
|---|
| 69 | 0) |
|---|
| 70 | |
|---|
| 71 | #define DUMPING_ERROR_INIT(error,error_type,error_problem) \ |
|---|
| 72 | (memset(&(error), 0, sizeof(error)), \ |
|---|
| 73 | (error).type = (error_type), \ |
|---|
| 74 | (error).type.dumping.problem = (error_problem), \ |
|---|
| 75 | 0) |
|---|
| 76 | |
|---|
| 77 | #define READER_ERROR_INIT(error,problem,offset) \ |
|---|
| 78 | READING_ERROR_INIT(error,YAML_READER_ERROR,problem,offset,-1) |
|---|
| 79 | |
|---|
| 80 | #define DECODER_ERROR_INIT(error,problem,offset,value) \ |
|---|
| 81 | READING_ERROR_INIT(error,YAML_DECODER_ERROR,problem,offset,value) |
|---|
| 82 | |
|---|
| 83 | #define SCANNER_ERROR_INIT(error,problem,problem_mark) \ |
|---|
| 84 | LOADING_ERROR_INIT(error,YAML_SCANNER_ERROR,problem,problem_mark) |
|---|
| 85 | |
|---|
| 86 | #define SCANNER_ERROR_WITH_CONTEXT_INIT(error,context,context_mark,problem,problem_mark) \ |
|---|
| 87 | LOADING_ERROR_WITH_CONTEXT_INIT(error,YAML_SCANNER_ERROR,context,context_mark,problem,problem_mark) |
|---|
| 88 | |
|---|
| 89 | #define PARSER_ERROR_INIT(error,problem,problem_mark) \ |
|---|
| 90 | LOADING_ERROR_INIT(error,YAML_PARSER_ERROR,problem,problem_mark) |
|---|
| 91 | |
|---|
| 92 | #define PARSER_ERROR_WITH_CONTEXT_INIT(error,context,context_mark,problem,problem_mark) \ |
|---|
| 93 | LOADING_ERROR_WITH_CONTEXT_INIT(error,YAML_PARSER_ERROR,context,context_mark,problem,problem_mark) |
|---|
| 94 | |
|---|
| 95 | #define COMPOSER_ERROR_INIT(error,problem,problem_mark) \ |
|---|
| 96 | LOADING_ERROR_INIT(error,YAML_COMPOSER_ERROR,problem,problem_mark) |
|---|
| 97 | |
|---|
| 98 | #define COMPOSER_ERROR_WITH_CONTEXT_INIT(error,context,context_mark,problem,problem_mark) \ |
|---|
| 99 | LOADING_ERROR_WITH_CONTEXT_INIT(error,YAML_COMPOSER_ERROR,context,context_mark,problem,problem_mark) |
|---|
| 100 | |
|---|
| 101 | #define WRITER_ERROR_INIT(error,problem,offset) \ |
|---|
| 102 | WRITING_ERROR_INIT(error,YAML_WRITER_ERROR,problem,offset) |
|---|
| 103 | |
|---|
| 104 | #define EMITTER_ERROR_INIT(error,context,problem) \ |
|---|
| 105 | DUMPING_ERROR_INIT(error,YAML_EMITTER_ERROR,problem) |
|---|
| 106 | |
|---|
| 107 | #define SERIALIZER_ERROR_INIT(error,context) \ |
|---|
| 108 | DUMPING_ERROR_INIT(error,YAML_SERIALIZER_ERROR,problem) |
|---|
| 109 | |
|---|
| 110 | /* |
|---|
| 111 | * The size of the input raw buffer. |
|---|
| 112 | */ |
|---|
| 113 | |
|---|
| 114 | #define RAW_INPUT_BUFFER_CAPACITY 16384 |
|---|
| 115 | |
|---|
| 116 | /* |
|---|
| 117 | * The size of the input buffer. |
|---|
| 118 | * |
|---|
| 119 | * The input buffer should be large enough to hold the content of the raw |
|---|
| 120 | * buffer after it is decoded. |
|---|
| 121 | */ |
|---|
| 122 | |
|---|
| 123 | #define INPUT_BUFFER_CAPACITY (RAW_INPUT_BUFFER_CAPACITY*3) |
|---|
| 124 | |
|---|
| 125 | /* |
|---|
| 126 | * The size of the output buffer. |
|---|
| 127 | */ |
|---|
| 128 | |
|---|
| 129 | #define OUTPUT_BUFFER_CAPACITY 16384 |
|---|
| 130 | |
|---|
| 131 | /* |
|---|
| 132 | * The size of the output raw buffer. |
|---|
| 133 | * |
|---|
| 134 | * The raw buffer should be able to hold the content of the output buffer |
|---|
| 135 | * after it is encoded. |
|---|
| 136 | */ |
|---|
| 137 | |
|---|
| 138 | #define RAW_OUTPUT_BUFFER_CAPACITY (OUTPUT_BUFFER_CAPACITY*2+2) |
|---|
| 139 | |
|---|
| 140 | /* |
|---|
| 141 | * The size of other stacks and queues. |
|---|
| 142 | */ |
|---|
| 143 | |
|---|
| 144 | #define INITIAL_STACK_CAPACITY 16 |
|---|
| 145 | #define INITIAL_QUEUE_CAPACITY 16 |
|---|
| 146 | #define INITIAL_STRING_CAPACITY 16 |
|---|
| 147 | |
|---|
| 148 | /* |
|---|
| 149 | * Input/output buffer management. |
|---|
| 150 | */ |
|---|
| 151 | |
|---|
| 152 | #define STORAGE_INIT(self,storage,storage_capacity) \ |
|---|
| 153 | (((storage).buffer = yaml_malloc(storage_capacity)) ? \ |
|---|
| 154 | ((storage).pointer = (storage).length = 0, \ |
|---|
| 155 | (buffer).capacity = (storage_capacity) \ |
|---|
| 156 | 1) : \ |
|---|
| 157 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 158 | 0)) |
|---|
| 159 | |
|---|
| 160 | #define STORAGE_DEL(self,storage) \ |
|---|
| 161 | (yaml_free((storage).buffer), \ |
|---|
| 162 | (storage).pointer = (storage).length = (storage).capacity = 0) |
|---|
| 163 | |
|---|
| 164 | /* |
|---|
| 165 | * String management. |
|---|
| 166 | */ |
|---|
| 167 | |
|---|
| 168 | typedef struct yaml_string_s { |
|---|
| 169 | yaml_char_t *buffer; |
|---|
| 170 | size_t pointer; |
|---|
| 171 | size_t capacity; |
|---|
| 172 | } yaml_string_t; |
|---|
| 173 | |
|---|
| 174 | YAML_DECLARE(int) |
|---|
| 175 | yaml_string_extend(yaml_char_t **buffer, size_t *capacity); |
|---|
| 176 | |
|---|
| 177 | YAML_DECLARE(int) |
|---|
| 178 | yaml_string_join( |
|---|
| 179 | yaml_char_t **base_buffer, size_t *base_pointer, size_t *base_capacity, |
|---|
| 180 | yaml_char_t *adj_buffer, size_t adj_pointer, size_t adj_capacity); |
|---|
| 181 | |
|---|
| 182 | #define NULL_STRING { NULL, NULL, NULL } |
|---|
| 183 | |
|---|
| 184 | #define STRING(string,capacity) { (string), 0, (capacity) } |
|---|
| 185 | |
|---|
| 186 | #define STRING_INIT(self,string,string_capacity) \ |
|---|
| 187 | (((string).buffer = yaml_malloc(string_capacity)) ? \ |
|---|
| 188 | ((string).pointer = 0, \ |
|---|
| 189 | (string).capacity = (string_capacity), \ |
|---|
| 190 | memset((string).buffer, 0, (string_capacity)), \ |
|---|
| 191 | 1) : \ |
|---|
| 192 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 193 | 0)) |
|---|
| 194 | |
|---|
| 195 | #define STRING_DEL(self,string) \ |
|---|
| 196 | (yaml_free((string).buffer), \ |
|---|
| 197 | (string).buffer = NULL, \ |
|---|
| 198 | ((string).pointer = (string).capacity = 0)) |
|---|
| 199 | |
|---|
| 200 | #define STRING_EXTEND(self,string) \ |
|---|
| 201 | ((((string).pointer+5 < (string).capacity) \ |
|---|
| 202 | || yaml_string_extend(&(string).buffer, &(string).capacity)) ? \ |
|---|
| 203 | 1 : \ |
|---|
| 204 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 205 | 0)) |
|---|
| 206 | |
|---|
| 207 | #define CLEAR(self,string) \ |
|---|
| 208 | ((string).pointer = 0, \ |
|---|
| 209 | memset((string).buffer, 0, (string).capacity)) |
|---|
| 210 | |
|---|
| 211 | #define JOIN(self,base_string,adj_string) \ |
|---|
| 212 | ((yaml_string_join(&(base_string).buffer, &(base_string).pointer, \ |
|---|
| 213 | &(base_string).capacity, (adj_string).buffer, \ |
|---|
| 214 | (adj_string).pointer, (adj_string).capacity)) ? \ |
|---|
| 215 | ((adj_string).pointer = 0, \ |
|---|
| 216 | 1) : \ |
|---|
| 217 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 218 | 0)) |
|---|
| 219 | |
|---|
| 220 | /* |
|---|
| 221 | * String check operations. |
|---|
| 222 | */ |
|---|
| 223 | |
|---|
| 224 | /* |
|---|
| 225 | * Get the octet at the specified position. |
|---|
| 226 | */ |
|---|
| 227 | |
|---|
| 228 | #define OCTET_AT(string,offset) \ |
|---|
| 229 | ((string).buffer[(string).pointer+(offset)]) |
|---|
| 230 | |
|---|
| 231 | /* |
|---|
| 232 | * Get the current offset. |
|---|
| 233 | */ |
|---|
| 234 | |
|---|
| 235 | #define OCTET(string) OCTET_AT((string),0) |
|---|
| 236 | |
|---|
| 237 | /* |
|---|
| 238 | * Check the octet at the specified position. |
|---|
| 239 | */ |
|---|
| 240 | |
|---|
| 241 | #define CHECK_AT(string,octet,offset) \ |
|---|
| 242 | (OCTET_AT((string),(offset)) == (yaml_char_t)(octet)) |
|---|
| 243 | |
|---|
| 244 | /* |
|---|
| 245 | * Check the current octet in the buffer. |
|---|
| 246 | */ |
|---|
| 247 | |
|---|
| 248 | #define CHECK(string,octet) CHECK_AT((string),(octet),0) |
|---|
| 249 | |
|---|
| 250 | /* |
|---|
| 251 | * Check if the character at the specified position is an alphabetical |
|---|
| 252 | * character, a digit, '_', or '-'. |
|---|
| 253 | */ |
|---|
| 254 | |
|---|
| 255 | #define IS_ALPHA_AT(string,offset) \ |
|---|
| 256 | ((OCTET_AT((string),(offset)) >= (yaml_char_t) '0' && \ |
|---|
| 257 | OCTET_AT((string),(offset)) <= (yaml_char_t) '9') || \ |
|---|
| 258 | (OCTET_AT((string),(offset)) >= (yaml_char_t) 'A' && \ |
|---|
| 259 | OCTET_AT((string),(offset)) <= (yaml_char_t) 'Z') || \ |
|---|
| 260 | (OCTET_AT((string),(offset)) >= (yaml_char_t) 'a' && \ |
|---|
| 261 | OCTET_AT((string),(offset)) <= (yaml_char_t) 'z') || \ |
|---|
| 262 | OCTET_AT((string),(offset)) == '_' || \ |
|---|
| 263 | OCTET_AT((string),(offset)) == '-') |
|---|
| 264 | |
|---|
| 265 | #define IS_ALPHA(string) IS_ALPHA_AT((string),0) |
|---|
| 266 | |
|---|
| 267 | /* |
|---|
| 268 | * Check if the character at the specified position is a digit. |
|---|
| 269 | */ |
|---|
| 270 | |
|---|
| 271 | #define IS_DIGIT_AT(string,offset) \ |
|---|
| 272 | ((OCTET_AT((string),(offset)) >= (yaml_char_t) '0' && \ |
|---|
| 273 | OCTET_AT((string),(offset)) <= (yaml_char_t) '9')) |
|---|
| 274 | |
|---|
| 275 | #define IS_DIGIT(string) IS_DIGIT_AT((string),0) |
|---|
| 276 | |
|---|
| 277 | /* |
|---|
| 278 | * Get the value of a digit. |
|---|
| 279 | */ |
|---|
| 280 | |
|---|
| 281 | #define AS_DIGIT_AT(string,offset) \ |
|---|
| 282 | (OCTET_AT((string),(offset)) - (yaml_char_t) '0') |
|---|
| 283 | |
|---|
| 284 | #define AS_DIGIT(string) AS_DIGIT_AT((string),0) |
|---|
| 285 | |
|---|
| 286 | /* |
|---|
| 287 | * Check if the character at the specified position is a hex-digit. |
|---|
| 288 | */ |
|---|
| 289 | |
|---|
| 290 | #define IS_HEX_AT(string,offset) \ |
|---|
| 291 | ((OCTET_AT((string),(offset)) >= (yaml_char_t) '0' && \ |
|---|
| 292 | OCTET_AT((string),(offset)) <= (yaml_char_t) '9') || \ |
|---|
| 293 | (OCTET_AT((string),(offset)) >= (yaml_char_t) 'A' && \ |
|---|
| 294 | OCTET_AT((string),(offset)) <= (yaml_char_t) 'F') || \ |
|---|
| 295 | (OCTET_AT((string),(offset)) >= (yaml_char_t) 'a' && \ |
|---|
| 296 | OCTET_AT((string),(offset)) <= (yaml_char_t) 'f')) |
|---|
| 297 | |
|---|
| 298 | #define IS_HEX(string) IS_HEX_AT((string),0) |
|---|
| 299 | |
|---|
| 300 | /* |
|---|
| 301 | * Get the value of a hex-digit. |
|---|
| 302 | */ |
|---|
| 303 | |
|---|
| 304 | #define AS_HEX_AT(string,offset) \ |
|---|
| 305 | ((OCTET_AT((string),(offset)) >= (yaml_char_t) 'A' && \ |
|---|
| 306 | OCTET_AT((string),(offset)) <= (yaml_char_t) 'F') ? \ |
|---|
| 307 | (OCTET_AT((string),(offset)) - (yaml_char_t) 'A' + 10) : \ |
|---|
| 308 | (OCTET_AT((string),(offset)) >= (yaml_char_t) 'a' && \ |
|---|
| 309 | OCTET_AT((string),(offset)) <= (yaml_char_t) 'f') ? \ |
|---|
| 310 | (OCTET_AT((string),(offset)) - (yaml_char_t) 'a' + 10) : \ |
|---|
| 311 | (OCTET_AT((string),(offset)) - (yaml_char_t) '0')) |
|---|
| 312 | |
|---|
| 313 | #define AS_HEX(string) AS_HEX_AT((string),0) |
|---|
| 314 | |
|---|
| 315 | /* |
|---|
| 316 | * Check if the character is ASCII. |
|---|
| 317 | */ |
|---|
| 318 | |
|---|
| 319 | #define IS_ASCII_AT(string,offset) \ |
|---|
| 320 | (OCTET_AT((string),(offset)) <= (yaml_char_t) '\x7F') |
|---|
| 321 | |
|---|
| 322 | #define IS_ASCII(string) IS_ASCII_AT((string),0) |
|---|
| 323 | |
|---|
| 324 | /* |
|---|
| 325 | * Check if the character can be printed unescaped. |
|---|
| 326 | */ |
|---|
| 327 | |
|---|
| 328 | #define IS_PRINTABLE_AT(string,offset) \ |
|---|
| 329 | ((OCTET_AT((string),(offset)) == 0x0A) /* . == #x0A */ \ |
|---|
| 330 | || (OCTET_AT((string),(offset)) >= 0x20 /* #x20 <= . <= #x7E */ \ |
|---|
| 331 | && OCTET_AT((string),(offset)) <= 0x7E) \ |
|---|
| 332 | || (OCTET_AT((string),(offset)) == 0xC2 /* #0xA0 <= . <= #xD7FF */ \ |
|---|
| 333 | && OCTET_AT((string),(offset)+1) >= 0xA0) \ |
|---|
| 334 | || (OCTET_AT((string),(offset)) > 0xC2 \ |
|---|
| 335 | && OCTET_AT((string),(offset)) < 0xED) \ |
|---|
| 336 | || (OCTET_AT((string),(offset)) == 0xED \ |
|---|
| 337 | && OCTET_AT((string),(offset)+1) < 0xA0) \ |
|---|
| 338 | || (OCTET_AT((string),(offset)) == 0xEE) \ |
|---|
| 339 | || (OCTET_AT((string),(offset)) == 0xEF /* #xE000 <= . <= #xFFFD */ \ |
|---|
| 340 | && !(OCTET_AT((string),(offset)+1) == 0xBB /* && . != #xFEFF */ \ |
|---|
| 341 | && OCTET_AT((string),(offset)+2) == 0xBF) \ |
|---|
| 342 | && !(OCTET_AT((string),(offset)+1) == 0xBF \ |
|---|
| 343 | && (OCTET_AT((string),(offset)+2) == 0xBE \ |
|---|
| 344 | || OCTET_AT((string),(offset)+2) == 0xBF)))) |
|---|
| 345 | |
|---|
| 346 | #define IS_PRINTABLE(string) IS_PRINTABLE_AT((string),0) |
|---|
| 347 | |
|---|
| 348 | /* |
|---|
| 349 | * Check if the character at the specified position is NUL. |
|---|
| 350 | */ |
|---|
| 351 | |
|---|
| 352 | #define IS_Z_AT(string,offset) CHECK_AT((string),'\0',(offset)) |
|---|
| 353 | |
|---|
| 354 | #define IS_Z(string) IS_Z_AT((string),0) |
|---|
| 355 | |
|---|
| 356 | /* |
|---|
| 357 | * Check if the character at the specified position is BOM. |
|---|
| 358 | */ |
|---|
| 359 | |
|---|
| 360 | #define IS_BOM_AT(string,offset) \ |
|---|
| 361 | (CHECK_AT((string),'\xEF',(offset)) \ |
|---|
| 362 | && CHECK_AT((string),'\xBB',(offset)+1) \ |
|---|
| 363 | && CHECK_AT((string),'\xBF',(offset)+2)) /* BOM (#xFEFF) */ |
|---|
| 364 | |
|---|
| 365 | #define IS_BOM(string) IS_BOM_AT(string,0) |
|---|
| 366 | |
|---|
| 367 | /* |
|---|
| 368 | * Check if the character at the specified position is space. |
|---|
| 369 | */ |
|---|
| 370 | |
|---|
| 371 | #define IS_SPACE_AT(string,offset) CHECK_AT((string),' ',(offset)) |
|---|
| 372 | |
|---|
| 373 | #define IS_SPACE(string) IS_SPACE_AT((string),0) |
|---|
| 374 | |
|---|
| 375 | /* |
|---|
| 376 | * Check if the character at the specified position is tab. |
|---|
| 377 | */ |
|---|
| 378 | |
|---|
| 379 | #define IS_TAB_AT(string,offset) CHECK_AT((string),'\t',(offset)) |
|---|
| 380 | |
|---|
| 381 | #define IS_TAB(string) IS_TAB_AT((string),0) |
|---|
| 382 | |
|---|
| 383 | /* |
|---|
| 384 | * Check if the character at the specified position is blank (space or tab). |
|---|
| 385 | */ |
|---|
| 386 | |
|---|
| 387 | #define IS_BLANK_AT(string,offset) \ |
|---|
| 388 | (IS_SPACE_AT((string),(offset)) || IS_TAB_AT((string),(offset))) |
|---|
| 389 | |
|---|
| 390 | #define IS_BLANK(string) IS_BLANK_AT((string),0) |
|---|
| 391 | |
|---|
| 392 | /* |
|---|
| 393 | * Check if the character at the specified position is a line break. |
|---|
| 394 | */ |
|---|
| 395 | |
|---|
| 396 | #define IS_BREAK_AT(string,offset) \ |
|---|
| 397 | (CHECK_AT((string),'\r',(offset)) /* CR (#xD)*/ \ |
|---|
| 398 | || CHECK_AT((string),'\n',(offset)) /* LF (#xA) */ \ |
|---|
| 399 | || (CHECK_AT((string),'\xC2',(offset)) \ |
|---|
| 400 | && CHECK_AT((string),'\x85',(offset)+1)) /* NEL (#x85) */ \ |
|---|
| 401 | || (CHECK_AT((string),'\xE2',(offset)) \ |
|---|
| 402 | && CHECK_AT((string),'\x80',(offset)+1) \ |
|---|
| 403 | && CHECK_AT((string),'\xA8',(offset)+2)) /* LS (#x2028) */ \ |
|---|
| 404 | || (CHECK_AT((string),'\xE2',(offset)) \ |
|---|
| 405 | && CHECK_AT((string),'\x80',(offset)+1) \ |
|---|
| 406 | && CHECK_AT((string),'\xA9',(offset)+2))) /* PS (#x2029) */ |
|---|
| 407 | |
|---|
| 408 | #define IS_BREAK(string) IS_BREAK_AT((string),0) |
|---|
| 409 | |
|---|
| 410 | #define IS_CRLF_AT(string,offset) \ |
|---|
| 411 | (CHECK_AT((string),'\r',(offset)) && CHECK_AT((string),'\n',(offset)+1)) |
|---|
| 412 | |
|---|
| 413 | #define IS_CRLF(string) IS_CRLF_AT((string),0) |
|---|
| 414 | |
|---|
| 415 | /* |
|---|
| 416 | * Check if the character is a line break or NUL. |
|---|
| 417 | */ |
|---|
| 418 | |
|---|
| 419 | #define IS_BREAKZ_AT(string,offset) \ |
|---|
| 420 | (IS_BREAK_AT((string),(offset)) || IS_Z_AT((string),(offset))) |
|---|
| 421 | |
|---|
| 422 | #define IS_BREAKZ(string) IS_BREAKZ_AT((string),0) |
|---|
| 423 | |
|---|
| 424 | /* |
|---|
| 425 | * Check if the character is a line break, space, or NUL. |
|---|
| 426 | */ |
|---|
| 427 | |
|---|
| 428 | #define IS_SPACEZ_AT(string,offset) \ |
|---|
| 429 | (IS_SPACE_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset))) |
|---|
| 430 | |
|---|
| 431 | #define IS_SPACEZ(string) IS_SPACEZ_AT((string),0) |
|---|
| 432 | |
|---|
| 433 | /* |
|---|
| 434 | * Check if the character is a line break, space, tab, or NUL. |
|---|
| 435 | */ |
|---|
| 436 | |
|---|
| 437 | #define IS_BLANKZ_AT(string,offset) \ |
|---|
| 438 | (IS_BLANK_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset))) |
|---|
| 439 | |
|---|
| 440 | #define IS_BLANKZ(string) IS_BLANKZ_AT((string),0) |
|---|
| 441 | |
|---|
| 442 | /* |
|---|
| 443 | * Determine the width of the character. |
|---|
| 444 | */ |
|---|
| 445 | |
|---|
| 446 | #define WIDTH_AT(string,offset) \ |
|---|
| 447 | ((OCTET_AT((string),(offset)) & 0x80) == 0x00 ? 1 : \ |
|---|
| 448 | (OCTET_AT((string),(offset)) & 0xE0) == 0xC0 ? 2 : \ |
|---|
| 449 | (OCTET_AT((string),(offset)) & 0xF0) == 0xE0 ? 3 : \ |
|---|
| 450 | (OCTET_AT((string),(offset)) & 0xF8) == 0xF0 ? 4 : 0) |
|---|
| 451 | |
|---|
| 452 | #define WIDTH(string) WIDTH_AT((string),0) |
|---|
| 453 | |
|---|
| 454 | /* |
|---|
| 455 | * Move the string pointer to the next character. |
|---|
| 456 | */ |
|---|
| 457 | |
|---|
| 458 | #define MOVE(string) ((string).pointer += WIDTH((string))) |
|---|
| 459 | |
|---|
| 460 | /* |
|---|
| 461 | * Copy a single octet and bump the pointers. |
|---|
| 462 | */ |
|---|
| 463 | |
|---|
| 464 | #define COPY_OCTET(string_a,string_b) \ |
|---|
| 465 | ((string_a).buffer[(string_a).pointer++] \ |
|---|
| 466 | = (string_b).buffer[(string_b).pointer++]) |
|---|
| 467 | |
|---|
| 468 | /* |
|---|
| 469 | * Copy a character and move the pointers of both strings. |
|---|
| 470 | */ |
|---|
| 471 | |
|---|
| 472 | #define COPY(string_a,string_b) \ |
|---|
| 473 | ((OCTET(string_b) & 0x80) == 0x00 ? \ |
|---|
| 474 | COPY_OCTET((string_a),(string_b)) : \ |
|---|
| 475 | (OCTET(string_b) & 0xE0) == 0xC0 ? \ |
|---|
| 476 | (COPY_OCTET((string_a),(string_b)), \ |
|---|
| 477 | COPY_OCTET((string_a),(string_b))) : \ |
|---|
| 478 | (OCTET(string_b) & 0xF0) == 0xE0 ? \ |
|---|
| 479 | (COPY_OCTET((string_a),(string_b)), \ |
|---|
| 480 | COPY_OCTET((string_a),(string_b)), \ |
|---|
| 481 | COPY_OCTET((string_a),(string_b))) : \ |
|---|
| 482 | (OCTET(string_b) & 0xF8) == 0xF0 ? \ |
|---|
| 483 | (COPY_OCTET((string_a),(string_b)), \ |
|---|
| 484 | COPY_OCTET((string_a),(string_b)), \ |
|---|
| 485 | COPY_OCTET((string_a),(string_b)), \ |
|---|
| 486 | COPY_OCTET((string_a),(string_b))) : 0) \ |
|---|
| 487 | |
|---|
| 488 | /* |
|---|
| 489 | * Stack and queue management. |
|---|
| 490 | */ |
|---|
| 491 | |
|---|
| 492 | YAML_DECLARE(int) |
|---|
| 493 | yaml_stack_extend(void **list, size_t size, size_t *length, size_t *capacity); |
|---|
| 494 | |
|---|
| 495 | YAML_DECLARE(int) |
|---|
| 496 | yaml_queue_extend(void **list, size_t size, |
|---|
| 497 | size_t *head, size_t *tail, size_t *capacity); |
|---|
| 498 | |
|---|
| 499 | #define STACK_INIT(self,stack,stack_capacity) \ |
|---|
| 500 | (((stack).list = yaml_malloc((stack_capacity)*sizeof(*(stack).list))) ? \ |
|---|
| 501 | ((stack).length = 0, \ |
|---|
| 502 | (stack).capacity = (stack_capacity), \ |
|---|
| 503 | 1) : \ |
|---|
| 504 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 505 | 0)) |
|---|
| 506 | |
|---|
| 507 | #define STACK_DEL(self,stack) \ |
|---|
| 508 | (yaml_free((stack).list), \ |
|---|
| 509 | (stack).list = NULL, \ |
|---|
| 510 | (stack).length = (stack).capacity = 0) |
|---|
| 511 | |
|---|
| 512 | #define STACK_EMPTY(self,stack) \ |
|---|
| 513 | ((stack).length == 0) |
|---|
| 514 | |
|---|
| 515 | #define PUSH(self,stack,value) \ |
|---|
| 516 | (((stack).length < (stack).capacity \ |
|---|
| 517 | || yaml_stack_extend((void **)&(stack).list, sizeof(*(stack).list), \ |
|---|
| 518 | &(stack).length, &(stack).capacity)) ? \ |
|---|
| 519 | ((stack).list[(stack).length++] = (value), \ |
|---|
| 520 | 1) : \ |
|---|
| 521 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 522 | 0)) |
|---|
| 523 | |
|---|
| 524 | #define POP(self,stack) \ |
|---|
| 525 | ((stack).list[--(stack).length]) |
|---|
| 526 | |
|---|
| 527 | #define QUEUE_INIT(self,queue,queue_capacity) \ |
|---|
| 528 | (((queue).list = yaml_malloc((queue_capacity)*sizeof(*(queue).list))) ? \ |
|---|
| 529 | ((queue).head = (queue).tail = 0, \ |
|---|
| 530 | (queue).capacity = (queue_capacity), \ |
|---|
| 531 | 1) : \ |
|---|
| 532 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 533 | 0)) |
|---|
| 534 | |
|---|
| 535 | #define QUEUE_DEL(self,queue) \ |
|---|
| 536 | (yaml_free((queue).list), \ |
|---|
| 537 | (queue).list = NULL, \ |
|---|
| 538 | (queue).head = (queue).tail = (queue).capacity = 0) |
|---|
| 539 | |
|---|
| 540 | #define QUEUE_EMPTY(self,queue) \ |
|---|
| 541 | ((queue).head == (queue).tail) |
|---|
| 542 | |
|---|
| 543 | #define ENQUEUE(self,queue,value) \ |
|---|
| 544 | (((queue).tail != (queue).capacity \ |
|---|
| 545 | || yaml_queue_extend((void **)&(queue).list, sizeof(*(queue).list), \ |
|---|
| 546 | &(queue).head, &(queue).tail, &(queue).capacity)) ? \ |
|---|
| 547 | ((queue).list[(queue).tail++] = (value), \ |
|---|
| 548 | 1) : \ |
|---|
| 549 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 550 | 0)) |
|---|
| 551 | |
|---|
| 552 | #define DEQUEUE(self,queue) \ |
|---|
| 553 | ((queue).list[(queue).head++]) |
|---|
| 554 | |
|---|
| 555 | #define QUEUE_INSERT(self,queue,index,value) \ |
|---|
| 556 | (((queue).tail != (queue).capacity \ |
|---|
| 557 | || yaml_queue_extend((void **)&(queue).list, sizeof(*(queue).list), \ |
|---|
| 558 | &(queue).head, &(queue).tail, &(queue).capacity)) ? \ |
|---|
| 559 | (memmove((queue).list+(queue).head+(index)+1, \ |
|---|
| 560 | (queue).list+(queue).head+(index), \ |
|---|
| 561 | ((queue).tail-(queue).head-(index))*sizeof(*(queue).list)), \ |
|---|
| 562 | (queue).list[(queue).head+(index)] = (value), \ |
|---|
| 563 | (queue).tail++, \ |
|---|
| 564 | 1) : \ |
|---|
| 565 | ((self)->error.type = YAML_MEMORY_ERROR, \ |
|---|
| 566 | 0)) |
|---|
| 567 | |
|---|
| 568 | /* |
|---|
| 569 | * Token initializers. |
|---|
| 570 | */ |
|---|
| 571 | |
|---|
| 572 | #define TOKEN_INIT(token,token_type,token_start_mark,token_end_mark) \ |
|---|
| 573 | (memset(&(token), 0, sizeof(yaml_token_t)), \ |
|---|
| 574 | (token).type = (token_type), \ |
|---|
| 575 | (token).start_mark = (token_start_mark), \ |
|---|
| 576 | (token).end_mark = (token_end_mark)) |
|---|
| 577 | |
|---|
| 578 | #define STREAM_START_TOKEN_INIT(token,token_encoding,start_mark,end_mark) \ |
|---|
| 579 | (TOKEN_INIT((token),YAML_STREAM_START_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 580 | (token).data.stream_start.encoding = (token_encoding)) |
|---|
| 581 | |
|---|
| 582 | #define STREAM_END_TOKEN_INIT(token,start_mark,end_mark) \ |
|---|
| 583 | (TOKEN_INIT((token),YAML_STREAM_END_TOKEN,(start_mark),(end_mark))) |
|---|
| 584 | |
|---|
| 585 | #define ALIAS_TOKEN_INIT(token,token_value,start_mark,end_mark) \ |
|---|
| 586 | (TOKEN_INIT((token),YAML_ALIAS_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 587 | (token).data.alias.value = (token_value)) |
|---|
| 588 | |
|---|
| 589 | #define ANCHOR_TOKEN_INIT(token,token_value,start_mark,end_mark) \ |
|---|
| 590 | (TOKEN_INIT((token),YAML_ANCHOR_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 591 | (token).data.anchor.value = (token_value)) |
|---|
| 592 | |
|---|
| 593 | #define TAG_TOKEN_INIT(token,token_handle,token_suffix,start_mark,end_mark) \ |
|---|
| 594 | (TOKEN_INIT((token),YAML_TAG_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 595 | (token).data.tag.handle = (token_handle), \ |
|---|
| 596 | (token).data.tag.suffix = (token_suffix)) |
|---|
| 597 | |
|---|
| 598 | #define SCALAR_TOKEN_INIT(token,token_value,token_length,token_style,start_mark,end_mark) \ |
|---|
| 599 | (TOKEN_INIT((token),YAML_SCALAR_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 600 | (token).data.scalar.value = (token_value), \ |
|---|
| 601 | (token).data.scalar.length = (token_length), \ |
|---|
| 602 | (token).data.scalar.style = (token_style)) |
|---|
| 603 | |
|---|
| 604 | #define VERSION_DIRECTIVE_TOKEN_INIT(token,token_major,token_minor,start_mark,end_mark) \ |
|---|
| 605 | (TOKEN_INIT((token),YAML_VERSION_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 606 | (token).data.version_directive.major = (token_major), \ |
|---|
| 607 | (token).data.version_directive.minor = (token_minor)) |
|---|
| 608 | |
|---|
| 609 | #define TAG_DIRECTIVE_TOKEN_INIT(token,token_handle,token_prefix,start_mark,end_mark) \ |
|---|
| 610 | (TOKEN_INIT((token),YAML_TAG_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \ |
|---|
| 611 | (token).data.tag_directive.handle = (token_handle), \ |
|---|
| 612 | (token).data.tag_directive.prefix = (token_prefix)) |
|---|
| 613 | |
|---|
| 614 | /* |
|---|
| 615 | * Event initializers. |
|---|
| 616 | */ |
|---|
| 617 | |
|---|
| 618 | #define EVENT_INIT(event,event_type,event_start_mark,event_end_mark) \ |
|---|
| 619 | (memset(&(event), 0, sizeof(yaml_event_t)), \ |
|---|
| 620 | (event).type = (event_type), \ |
|---|
| 621 | (event).start_mark = (event_start_mark), \ |
|---|
| 622 | (event).end_mark = (event_end_mark)) |
|---|
| 623 | |
|---|
| 624 | #define STREAM_START_EVENT_INIT(event,event_encoding,start_mark,end_mark) \ |
|---|
| 625 | (EVENT_INIT((event),YAML_STREAM_START_EVENT,(start_mark),(end_mark)), \ |
|---|
| 626 | (event).data.stream_start.encoding = (event_encoding)) |
|---|
| 627 | |
|---|
| 628 | #define STREAM_END_EVENT_INIT(event,start_mark,end_mark) \ |
|---|
| 629 | (EVENT_INIT((event),YAML_STREAM_END_EVENT,(start_mark),(end_mark))) |
|---|
| 630 | |
|---|
| 631 | #define DOCUMENT_START_EVENT_INIT(event,event_version_directive, \ |
|---|
| 632 | event_tag_directives_list,event_tag_directives_length, \ |
|---|
| 633 | event_tag_directives_capacity,event_is_implicit,start_mark,end_mark) \ |
|---|
| 634 | (EVENT_INIT((event),YAML_DOCUMENT_START_EVENT,(start_mark),(end_mark)), \ |
|---|
| 635 | (event).data.document_start.version_directive = (event_version_directive), \ |
|---|
| 636 | (event).data.document_start.tag_directives.list = (event_tag_directives_list), \ |
|---|
| 637 | (event).data.document_start.tag_directives.length = (event_tag_directives_length), \ |
|---|
| 638 | (event).data.document_start.tag_directives.capacity = (event_tag_directives_capacity), \ |
|---|
| 639 | (event).data.document_start.is_implicit = (event_is_implicit)) |
|---|
| 640 | |
|---|
| 641 | #define DOCUMENT_END_EVENT_INIT(event,event_is_implicit,start_mark,end_mark) \ |
|---|
| 642 | (EVENT_INIT((event),YAML_DOCUMENT_END_EVENT,(start_mark),(end_mark)), \ |
|---|
| 643 | (event).data.document_end.is_implicit = (event_is_implicit)) |
|---|
| 644 | |
|---|
| 645 | #define ALIAS_EVENT_INIT(event,event_anchor,start_mark,end_mark) \ |
|---|
| 646 | (EVENT_INIT((event),YAML_ALIAS_EVENT,(start_mark),(end_mark)), \ |
|---|
| 647 | (event).data.alias.anchor = (event_anchor)) |
|---|
| 648 | |
|---|
| 649 | #define SCALAR_EVENT_INIT(event,event_anchor,event_tag,event_value, \ |
|---|
| 650 | event_length,event_is_plain_implicit,event_is_quoted_implicit, \ |
|---|
| 651 | event_style,start_mark,end_mark) \ |
|---|
| 652 | (EVENT_INIT((event),YAML_SCALAR_EVENT,(start_mark),(end_mark)), \ |
|---|
| 653 | (event).data.scalar.anchor = (event_anchor), \ |
|---|
| 654 | (event).data.scalar.tag = (event_tag), \ |
|---|
| 655 | (event).data.scalar.value = (event_value), \ |
|---|
| 656 | (event).data.scalar.length = (event_length), \ |
|---|
| 657 | (event).data.scalar.is_plain_implicit = (event_is_plain_implicit), \ |
|---|
| 658 | (event).data.scalar.is_quoted_implicit = (event_is_quoted_implicit), \ |
|---|
| 659 | (event).data.scalar.style = (event_style)) |
|---|
| 660 | |
|---|
| 661 | #define SEQUENCE_START_EVENT_INIT(event,event_anchor,event_tag, \ |
|---|
| 662 | event_is_implicit,event_style,start_mark,end_mark) \ |
|---|
| 663 | (EVENT_INIT((event),YAML_SEQUENCE_START_EVENT,(start_mark),(end_mark)), \ |
|---|
| 664 | (event).data.sequence_start.anchor = (event_anchor), \ |
|---|
| 665 | (event).data.sequence_start.tag = (event_tag), \ |
|---|
| 666 | (event).data.sequence_start.is_implicit = (event_is_implicit), \ |
|---|
| 667 | (event).data.sequence_start.style = (event_style)) |
|---|
| 668 | |
|---|
| 669 | #define SEQUENCE_END_EVENT_INIT(event,start_mark,end_mark) \ |
|---|
| 670 | (EVENT_INIT((event),YAML_SEQUENCE_END_EVENT,(start_mark),(end_mark))) |
|---|
| 671 | |
|---|
| 672 | #define MAPPING_START_EVENT_INIT(event,event_anchor,event_tag, \ |
|---|
| 673 | event_is_implicit,event_style,start_mark,end_mark) \ |
|---|
| 674 | (EVENT_INIT((event),YAML_MAPPING_START_EVENT,(start_mark),(end_mark)), \ |
|---|
| 675 | (event).data.mapping_start.anchor = (event_anchor), \ |
|---|
| 676 | (event).data.mapping_start.tag = (event_tag), \ |
|---|
| 677 | (event).data.mapping_start.is_implicit = (event_is_implicit), \ |
|---|
| 678 | (event).data.mapping_start.style = (event_style)) |
|---|
| 679 | |
|---|
| 680 | #define MAPPING_END_EVENT_INIT(event,start_mark,end_mark) \ |
|---|
| 681 | (EVENT_INIT((event),YAML_MAPPING_END_EVENT,(start_mark),(end_mark))) |
|---|
| 682 | |
|---|
| 683 | /* |
|---|
| 684 | * Document initializer. |
|---|
| 685 | */ |
|---|
| 686 | |
|---|
| 687 | #define DOCUMENT_INIT(document,document_nodes_start,document_nodes_end, \ |
|---|
| 688 | document_version_directive,document_tag_directives_start, \ |
|---|
| 689 | document_tag_directives_end,document_start_implicit, \ |
|---|
| 690 | document_end_implicit,document_start_mark,document_end_mark) \ |
|---|
| 691 | (memset(&(document), 0, sizeof(yaml_document_t)), \ |
|---|
| 692 | (document).nodes.start = (document_nodes_start), \ |
|---|
| 693 | (document).nodes.end = (document_nodes_end), \ |
|---|
| 694 | (document).nodes.top = (document_nodes_start), \ |
|---|
| 695 | (document).version_directive = (document_version_directive), \ |
|---|
| 696 | (document).tag_directives.start = (document_tag_directives_start), \ |
|---|
| 697 | (document).tag_directives.end = (document_tag_directives_end), \ |
|---|
| 698 | (document).start_implicit = (document_start_implicit), \ |
|---|
| 699 | (document).end_implicit = (document_end_implicit), \ |
|---|
| 700 | (document).start_mark = (document_start_mark), \ |
|---|
| 701 | (document).end_mark = (document_end_mark)) |
|---|
| 702 | |
|---|
| 703 | /* |
|---|
| 704 | * Node initializers. |
|---|
| 705 | */ |
|---|
| 706 | |
|---|
| 707 | #define NODE_INIT(node,node_type,node_tag,node_start_mark,node_end_mark) \ |
|---|
| 708 | (memset(&(node), 0, sizeof(yaml_node_t)), \ |
|---|
| 709 | (node).type = (node_type), \ |
|---|
| 710 | (node).tag = (node_tag), \ |
|---|
| 711 | (node).start_mark = (node_start_mark), \ |
|---|
| 712 | (node).end_mark = (node_end_mark)) |
|---|
| 713 | |
|---|
| 714 | #define SCALAR_NODE_INIT(node,node_tag,node_value,node_length, \ |
|---|
| 715 | node_style,start_mark,end_mark) \ |
|---|
| 716 | (NODE_INIT((node),YAML_SCALAR_NODE,(node_tag),(start_mark),(end_mark)), \ |
|---|
| 717 | (node).data.scalar.value = (node_value), \ |
|---|
| 718 | (node).data.scalar.length = (node_length), \ |
|---|
| 719 | (node).data.scalar.style = (node_style)) |
|---|
| 720 | |
|---|
| 721 | #define SEQUENCE_NODE_INIT(node,node_tag,node_items_start,node_items_end, \ |
|---|
| 722 | node_style,start_mark,end_mark) \ |
|---|
| 723 | (NODE_INIT((node),YAML_SEQUENCE_NODE,(node_tag),(start_mark),(end_mark)), \ |
|---|
| 724 | (node).data.sequence.items.start = (node_items_start), \ |
|---|
| 725 | (node).data.sequence.items.end = (node_items_end), \ |
|---|
| 726 | (node).data.sequence.items.top = (node_items_start), \ |
|---|
| 727 | (node).data.sequence.style = (node_style)) |
|---|
| 728 | |
|---|
| 729 | #define MAPPING_NODE_INIT(node,node_tag,node_pairs_start,node_pairs_end, \ |
|---|
| 730 | node_style,start_mark,end_mark) \ |
|---|
| 731 | (NODE_INIT((node),YAML_MAPPING_NODE,(node_tag),(start_mark),(end_mark)), \ |
|---|
| 732 | (node).data.mapping.pairs.start = (node_pairs_start), \ |
|---|
| 733 | (node).data.mapping.pairs.end = (node_pairs_end), \ |
|---|
| 734 | (node).data.mapping.pairs.top = (node_pairs_start), \ |
|---|
| 735 | (node).data.mapping.style = (node_style)) |
|---|
| 736 | |
|---|
| 737 | /* |
|---|
| 738 | * This structure holds information about a potential simple key. |
|---|
| 739 | */ |
|---|
| 740 | |
|---|
| 741 | typedef struct yaml_simple_key_s { |
|---|
| 742 | /* Is a simple key possible? */ |
|---|
| 743 | int is_possible; |
|---|
| 744 | /* Is a simple key required? */ |
|---|
| 745 | int is_required; |
|---|
| 746 | /* The number of the token. */ |
|---|
| 747 | size_t token_number; |
|---|
| 748 | /* The position mark. */ |
|---|
| 749 | yaml_mark_t mark; |
|---|
| 750 | } yaml_simple_key_t; |
|---|
| 751 | |
|---|
| 752 | /* |
|---|
| 753 | * The states of the parser. |
|---|
| 754 | */ |
|---|
| 755 | |
|---|
| 756 | typedef enum yaml_parser_state_e { |
|---|
| 757 | /* Expect STREAM-START. */ |
|---|
| 758 | YAML_PARSE_STREAM_START_STATE, |
|---|
| 759 | /* Expect the beginning of an implicit document. */ |
|---|
| 760 | YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE, |
|---|
| 761 | /* Expect DOCUMENT-START. */ |
|---|
| 762 | YAML_PARSE_DOCUMENT_START_STATE, |
|---|
| 763 | /* Expect the content of a document. */ |
|---|
| 764 | YAML_PARSE_DOCUMENT_CONTENT_STATE, |
|---|
| 765 | /* Expect DOCUMENT-END. */ |
|---|
| 766 | YAML_PARSE_DOCUMENT_END_STATE, |
|---|
| 767 | /* Expect a block node. */ |
|---|
| 768 | YAML_PARSE_BLOCK_NODE_STATE, |
|---|
| 769 | /* Expect a block node or indentless sequence. */ |
|---|
| 770 | YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE, |
|---|
| 771 | /* Expect a flow node. */ |
|---|
| 772 | YAML_PARSE_FLOW_NODE_STATE, |
|---|
| 773 | /* Expect the first entry of a block sequence. */ |
|---|
| 774 | YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE, |
|---|
| 775 | /* Expect an entry of a block sequence. */ |
|---|
| 776 | YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE, |
|---|
| 777 | /* Expect an entry of an indentless sequence. */ |
|---|
| 778 | YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE, |
|---|
| 779 | /* Expect the first key of a block mapping. */ |
|---|
| 780 | YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE, |
|---|
| 781 | /* Expect a block mapping key. */ |
|---|
| 782 | YAML_PARSE_BLOCK_MAPPING_KEY_STATE, |
|---|
| 783 | /* Expect a block mapping value. */ |
|---|
| 784 | YAML_PARSE_BLOCK_MAPPING_VALUE_STATE, |
|---|
| 785 | /* Expect the first entry of a flow sequence. */ |
|---|
| 786 | YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE, |
|---|
| 787 | /* Expect an entry of a flow sequence. */ |
|---|
| 788 | YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE, |
|---|
| 789 | /* Expect a key of an ordered mapping. */ |
|---|
| 790 | YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE, |
|---|
| 791 | /* Expect a value of an ordered mapping. */ |
|---|
| 792 | YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE, |
|---|
| 793 | /* Expect the and of an ordered mapping entry. */ |
|---|
| 794 | YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE, |
|---|
| 795 | /* Expect the first key of a flow mapping. */ |
|---|
| 796 | YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE, |
|---|
| 797 | /* Expect a key of a flow mapping. */ |
|---|
| 798 | YAML_PARSE_FLOW_MAPPING_KEY_STATE, |
|---|
| 799 | /* Expect a value of a flow mapping. */ |
|---|
| 800 | YAML_PARSE_FLOW_MAPPING_VALUE_STATE, |
|---|
| 801 | /* Expect an empty value of a flow mapping. */ |
|---|
| 802 | YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE, |
|---|
| 803 | /* Expect nothing. */ |
|---|
| 804 | YAML_PARSE_END_STATE |
|---|
| 805 | } yaml_parser_state_t; |
|---|
| 806 | |
|---|
| 807 | /* |
|---|
| 808 | * This structure holds aliases data. |
|---|
| 809 | */ |
|---|
| 810 | |
|---|
| 811 | typedef struct yaml_alias_data_s { |
|---|
| 812 | /* The anchor. */ |
|---|
| 813 | yaml_char_t *anchor; |
|---|
| 814 | /* The node id. */ |
|---|
| 815 | int index; |
|---|
| 816 | /* The anchor mark. */ |
|---|
| 817 | yaml_mark_t mark; |
|---|
| 818 | } yaml_alias_data_t; |
|---|
| 819 | |
|---|
| 820 | /* |
|---|
| 821 | * The structure that holds data used by the file and string readers. |
|---|
| 822 | */ |
|---|
| 823 | |
|---|
| 824 | typedef union yaml_standard_reader_data_u { |
|---|
| 825 | /* String input data. */ |
|---|
| 826 | yaml_string_t string; |
|---|
| 827 | /* File input data. */ |
|---|
| 828 | FILE *file; |
|---|
| 829 | } yaml_standard_reader_data_t; |
|---|
| 830 | |
|---|
| 831 | /* |
|---|
| 832 | * The internal parser structure. |
|---|
| 833 | */ |
|---|
| 834 | |
|---|
| 835 | struct yaml_parser_s { |
|---|
| 836 | |
|---|
| 837 | /* |
|---|
| 838 | * Error stuff. |
|---|
| 839 | */ |
|---|
| 840 | |
|---|
| 841 | yaml_error_t error; |
|---|
| 842 | |
|---|
| 843 | /* |
|---|
| 844 | * Reader stuff. |
|---|
| 845 | */ |
|---|
| 846 | |
|---|
| 847 | /* The read handler. */ |
|---|
| 848 | yaml_reader_t *reader; |
|---|
| 849 | |
|---|
| 850 | /* The application data to be passed to the reader. */ |
|---|
| 851 | void *reader_data; |
|---|
| 852 | |
|---|
| 853 | /* Standard (string or file) input data. */ |
|---|
| 854 | yaml_standard_reader_data_t standard_reader_data; |
|---|
| 855 | |
|---|
| 856 | /* EOF flag. */ |
|---|
| 857 | int is_eof; |
|---|
| 858 | |
|---|
| 859 | /* The working buffer. */ |
|---|
| 860 | struct { |
|---|
| 861 | yaml_char_t *buffer; |
|---|
| 862 | size_t pointer; |
|---|
| 863 | size_t length; |
|---|
| 864 | size_t capacity; |
|---|
| 865 | } input; |
|---|
| 866 | |
|---|
| 867 | /* The number of unread characters in the buffer. */ |
|---|
| 868 | size_t unread; |
|---|
| 869 | |
|---|
| 870 | /* The raw buffer. */ |
|---|
| 871 | struct { |
|---|
| 872 | unsigned char *buffer; |
|---|
| 873 | size_t pointer; |
|---|
| 874 | size_t length; |
|---|
| 875 | size_t capacity; |
|---|
| 876 | } raw_input; |
|---|
| 877 | |
|---|
| 878 | /* The input encoding. */ |
|---|
| 879 | yaml_encoding_t encoding; |
|---|
| 880 | |
|---|
| 881 | /* The offset of the current position (in bytes). */ |
|---|
| 882 | size_t offset; |
|---|
| 883 | |
|---|
| 884 | /* The mark of the current position. */ |
|---|
| 885 | yaml_mark_t mark; |
|---|
| 886 | |
|---|
| 887 | /* |
|---|
| 888 | * Scanner stuff. |
|---|
| 889 | */ |
|---|
| 890 | |
|---|
| 891 | /* Have we started to scan the input stream? */ |
|---|
| 892 | int is_stream_start_produced; |
|---|
| 893 | |
|---|
| 894 | /* Have we reached the end of the input stream? */ |
|---|
| 895 | int is_stream_end_produced; |
|---|
| 896 | |
|---|
| 897 | /* The number of unclosed '[' and '{' indicators. */ |
|---|
| 898 | int flow_level; |
|---|
| 899 | |
|---|
| 900 | /* The tokens queue. */ |
|---|
| 901 | struct { |
|---|
| 902 | yaml_token_t *list; |
|---|
| 903 | size_t head; |
|---|
| 904 | size_t tail; |
|---|
| 905 | size_t capacity; |
|---|
| 906 | } tokens; |
|---|
| 907 | |
|---|
| 908 | /* The number of tokens fetched from the queue. */ |
|---|
| 909 | size_t tokens_parsed; |
|---|
| 910 | |
|---|
| 911 | /* Does the tokens queue contain a token ready for dequeueing. */ |
|---|
| 912 | int is_token_available; |
|---|
| 913 | |
|---|
| 914 | /* The indentation levels stack. */ |
|---|
| 915 | struct { |
|---|
| 916 | int *list; |
|---|
| 917 | size_t length; |
|---|
| 918 | size_t capacity; |
|---|
| 919 | } indents; |
|---|
| 920 | |
|---|
| 921 | /* The current indentation level. */ |
|---|
| 922 | int indent; |
|---|
| 923 | |
|---|
| 924 | /* May a simple key occur at the current position? */ |
|---|
| 925 | int is_simple_key_allowed; |
|---|
| 926 | |
|---|
| 927 | /* The stack of simple keys. */ |
|---|
| 928 | struct { |
|---|
| 929 | yaml_simple_key_t *list; |
|---|
| 930 | size_t length; |
|---|
| 931 | size_t capacity; |
|---|
| 932 | } simple_keys; |
|---|
| 933 | |
|---|
| 934 | /* |
|---|
| 935 | * Parser stuff. |
|---|
| 936 | */ |
|---|
| 937 | |
|---|
| 938 | /* The parser states stack. */ |
|---|
| 939 | struct { |
|---|
| 940 | yaml_parser_state_t *list; |
|---|
| 941 | size_t length; |
|---|
| 942 | size_t capacity; |
|---|
| 943 | } states; |
|---|
| 944 | |
|---|
| 945 | /* The current parser state. */ |
|---|
| 946 | yaml_parser_state_t state; |
|---|
| 947 | |
|---|
| 948 | /* The stack of marks. */ |
|---|
| 949 | struct { |
|---|
| 950 | yaml_mark_t *list; |
|---|
| 951 | size_t length; |
|---|
| 952 | size_t capacity; |
|---|
| 953 | } marks; |
|---|
| 954 | |
|---|
| 955 | /* The list of TAG directives. */ |
|---|
| 956 | struct { |
|---|
| 957 | yaml_tag_directive_t *list; |
|---|
| 958 | size_t length; |
|---|
| 959 | size_t capacity; |
|---|
| 960 | } tag_directives; |
|---|
| 961 | |
|---|
| 962 | /* |
|---|
| 963 | * Dumper stuff. |
|---|
| 964 | */ |
|---|
| 965 | |
|---|
| 966 | /* The resolve handler. */ |
|---|
| 967 | yaml_resolver_t *resolver; |
|---|
| 968 | |
|---|
| 969 | /* The application data to be passed to the resolver. */ |
|---|
| 970 | void *resolver_data; |
|---|
| 971 | |
|---|
| 972 | /* The alias data. */ |
|---|
| 973 | struct { |
|---|
| 974 | yaml_alias_data_t *list; |
|---|
| 975 | size_t length; |
|---|
| 976 | size_t capacity; |
|---|
| 977 | } aliases; |
|---|
| 978 | |
|---|
| 979 | /* The currently parsed document. */ |
|---|
| 980 | yaml_document_t *document; |
|---|
| 981 | |
|---|
| 982 | }; |
|---|
| 983 | |
|---|
| 984 | /* |
|---|
| 985 | * The emitter states. |
|---|
| 986 | */ |
|---|
| 987 | |
|---|
| 988 | typedef enum yaml_emitter_state_e { |
|---|
| 989 | /** Expect STREAM-START. */ |
|---|
| 990 | YAML_EMIT_STREAM_START_STATE, |
|---|
| 991 | /** Expect the first DOCUMENT-START or STREAM-END. */ |
|---|
| 992 | YAML_EMIT_FIRST_DOCUMENT_START_STATE, |
|---|
| 993 | /** Expect DOCUMENT-START or STREAM-END. */ |
|---|
| 994 | YAML_EMIT_DOCUMENT_START_STATE, |
|---|
| 995 | /** Expect the content of a document. */ |
|---|
| 996 | YAML_EMIT_DOCUMENT_CONTENT_STATE, |
|---|
| 997 | /** Expect DOCUMENT-END. */ |
|---|
| 998 | YAML_EMIT_DOCUMENT_END_STATE, |
|---|
| 999 | /** Expect the first item of a flow sequence. */ |
|---|
| 1000 | YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE, |
|---|
| 1001 | /** Expect an item of a flow sequence. */ |
|---|
| 1002 | YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE, |
|---|
| 1003 | /** Expect the first key of a flow mapping. */ |
|---|
| 1004 | YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE, |
|---|
| 1005 | /** Expect a key of a flow mapping. */ |
|---|
| 1006 | YAML_EMIT_FLOW_MAPPING_KEY_STATE, |
|---|
| 1007 | /** Expect a value for a simple key of a flow mapping. */ |
|---|
| 1008 | YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE, |
|---|
| 1009 | /** Expect a value of a flow mapping. */ |
|---|
| 1010 | YAML_EMIT_FLOW_MAPPING_VALUE_STATE, |
|---|
| 1011 | /** Expect the first item of a block sequence. */ |
|---|
| 1012 | YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE, |
|---|
| 1013 | /** Expect an item of a block sequence. */ |
|---|
| 1014 | YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE, |
|---|
| 1015 | /** Expect the first key of a block mapping. */ |
|---|
| 1016 | YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE, |
|---|
| 1017 | /** Expect the key of a block mapping. */ |
|---|
| 1018 | YAML_EMIT_BLOCK_MAPPING_KEY_STATE, |
|---|
| 1019 | /** Expect a value for a simple key of a block mapping. */ |
|---|
| 1020 | YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE, |
|---|
| 1021 | /** Expect a value of a block mapping. */ |
|---|
| 1022 | YAML_EMIT_BLOCK_MAPPING_VALUE_STATE, |
|---|
| 1023 | /** Expect nothing. */ |
|---|
| 1024 | YAML_EMIT_END_STATE |
|---|
| 1025 | } yaml_emitter_state_t; |
|---|
| 1026 | |
|---|
| 1027 | /* |
|---|
| 1028 | * The structure that holds data used by the file and string readers. |
|---|
| 1029 | */ |
|---|
| 1030 | |
|---|
| 1031 | typedef union yaml_standard_writer_data_u { |
|---|
| 1032 | /* String output data. */ |
|---|
| 1033 | yaml_string_t string; |
|---|
| 1034 | size_t *length; |
|---|
| 1035 | /* File output data. */ |
|---|
| 1036 | FILE *file; |
|---|
| 1037 | } yaml_standard_writer_data_t; |
|---|
| 1038 | |
|---|
| 1039 | /* |
|---|
| 1040 | * The internals emitter structure. |
|---|
| 1041 | */ |
|---|
| 1042 | |
|---|
| 1043 | struct yaml_emitter_s { |
|---|
| 1044 | |
|---|
| 1045 | /* |
|---|
| 1046 | * Error stuff. |
|---|
| 1047 | */ |
|---|
| 1048 | |
|---|
| 1049 | yaml_error_t error; |
|---|
| 1050 | |
|---|
| 1051 | /* |
|---|
| 1052 | * Writer stuff. |
|---|
| 1053 | */ |
|---|
| 1054 | |
|---|
| 1055 | /* Write handler. */ |
|---|
| 1056 | yaml_writer_t *writer; |
|---|
| 1057 | |
|---|
| 1058 | /* A pointer for passing to the white handler. */ |
|---|
| 1059 | void *writer_data; |
|---|
| 1060 | |
|---|
| 1061 | /* Standard (string or file) output data. */ |
|---|
| 1062 | yaml_standard_writer_data_t standard_writer_data; |
|---|
| 1063 | |
|---|
| 1064 | /* The working buffer. */ |
|---|
| 1065 | struct { |
|---|
| 1066 | yaml_char_t *buffer; |
|---|
| 1067 | size_t pointer; |
|---|
| 1068 | size_t length; |
|---|
| 1069 | size_t capacity; |
|---|
| 1070 | } output; |
|---|
| 1071 | |
|---|
| 1072 | /* The raw buffer. */ |
|---|
| 1073 | struct { |
|---|
| 1074 | yaml_char_t *buffer; |
|---|
| 1075 | size_t pointer; |
|---|
| 1076 | size_t length; |
|---|
| 1077 | size_t capacity; |
|---|
| 1078 | } raw_output; |
|---|
| 1079 | |
|---|
| 1080 | /* The stream encoding. */ |
|---|
| 1081 | yaml_encoding_t encoding; |
|---|
| 1082 | |
|---|
| 1083 | /* |
|---|
| 1084 | * Emitter stuff. |
|---|
| 1085 | */ |
|---|
| 1086 | |
|---|
| 1087 | /* If the output is in the canonical style? */ |
|---|
| 1088 | int is_canonical; |
|---|
| 1089 | /* The number of indentation spaces. */ |
|---|
| 1090 | int best_indent; |
|---|
| 1091 | /* The preferred width of the output lines. */ |
|---|
| 1092 | int best_width; |
|---|
| 1093 | /* Allow unescaped non-ASCII characters? */ |
|---|
| 1094 | int is_unicode; |
|---|
| 1095 | /* The preferred line break. */ |
|---|
| 1096 | yaml_break_t line_break; |
|---|
| 1097 | |
|---|
| 1098 | /* The stack of states. */ |
|---|
| 1099 | struct { |
|---|
| 1100 | yaml_emitter_state_t *list; |
|---|
| 1101 | size_t length; |
|---|
| 1102 | size_t capacity; |
|---|
| 1103 | } states; |
|---|
| 1104 | |
|---|
| 1105 | /* The current emitter state. */ |
|---|
| 1106 | yaml_emitter_state_t state; |
|---|
| 1107 | |
|---|
| 1108 | /* The event queue. */ |
|---|
| 1109 | struct { |
|---|
| 1110 | yaml_event_t *list; |
|---|
| 1111 | size_t head; |
|---|
| 1112 | size_t tail; |
|---|
| 1113 | size_t capacity; |
|---|
| 1114 | } events; |
|---|
| 1115 | |
|---|
| 1116 | /* The stack of indentation levels. */ |
|---|
| 1117 | struct { |
|---|
| 1118 | int *list; |
|---|
| 1119 | size_t length; |
|---|
| 1120 | size_t capacity; |
|---|
| 1121 | } indents; |
|---|
| 1122 | |
|---|
| 1123 | /* The list of tag directives. */ |
|---|
| 1124 | struct { |
|---|
| 1125 | yaml_tag_directive_t *list; |
|---|
| 1126 | size_t length; |
|---|
| 1127 | size_t capacity; |
|---|
| 1128 | } tag_directives; |
|---|
| 1129 | |
|---|
| 1130 | /* The current indentation level. */ |
|---|
| 1131 | int indent; |
|---|
| 1132 | |
|---|
| 1133 | /* The current flow level. */ |
|---|
| 1134 | int flow_level; |
|---|
| 1135 | |
|---|
| 1136 | /* Is it the document root context? */ |
|---|
| 1137 | int is_root_context; |
|---|
| 1138 | /* Is it a sequence context? */ |
|---|
| 1139 | int is_sequence_context; |
|---|
| 1140 | /* Is it a mapping context? */ |
|---|
| 1141 | int is_mapping_context; |
|---|
| 1142 | /* Is it a simple mapping key context? */ |
|---|
| 1143 | int is_simple_key_context; |
|---|
| 1144 | |
|---|
| 1145 | /* The current line. */ |
|---|
| 1146 | int line; |
|---|
| 1147 | /* The current column. */ |
|---|
| 1148 | int column; |
|---|
| 1149 | /* If the last character was a whitespace? */ |
|---|
| 1150 | int whitespace; |
|---|
| 1151 | /* If the last character was an indentation character (' ', '-', '?', ':')? */ |
|---|
| 1152 | int indention; |
|---|
| 1153 | |
|---|
| 1154 | /* Anchor analysis. */ |
|---|
| 1155 | struct { |
|---|
| 1156 | /* The anchor value. */ |
|---|
| 1157 | yaml_char_t *anchor; |
|---|
| 1158 | /* The anchor length. */ |
|---|
| 1159 | size_t anchor_length; |
|---|
| 1160 | /* Is it an alias? */ |
|---|
| 1161 | int is_alias; |
|---|
| 1162 | } anchor_data; |
|---|
| 1163 | |
|---|
| 1164 | /* Tag analysis. */ |
|---|
| 1165 | struct { |
|---|
| 1166 | /* The tag handle. */ |
|---|
| 1167 | yaml_char_t *handle; |
|---|
| 1168 | /* The tag handle length. */ |
|---|
| 1169 | size_t handle_length; |
|---|
| 1170 | /* The tag suffix. */ |
|---|
| 1171 | yaml_char_t *suffix; |
|---|
| 1172 | /* The tag suffix length. */ |
|---|
| 1173 | size_t suffix_length; |
|---|
| 1174 | } tag_data; |
|---|
| 1175 | |
|---|
| 1176 | /* Scalar analysis. */ |
|---|
| 1177 | struct { |
|---|
| 1178 | /* The scalar value. */ |
|---|
| 1179 | yaml_char_t *value; |
|---|
| 1180 | /* The scalar length. */ |
|---|
| 1181 | size_t length; |
|---|
| 1182 | /* Does the scalar contain line breaks? */ |
|---|
| 1183 | int is_multiline; |
|---|
| 1184 | /* Can the scalar be expessed in the flow plain style? */ |
|---|
| 1185 | int is_flow_plain_allowed; |
|---|
| 1186 | /* Can the scalar be expressed in the block plain style? */ |
|---|
| 1187 | int is_block_plain_allowed; |
|---|
| 1188 | /* Can the scalar be expressed in the single quoted style? */ |
|---|
| 1189 | int is_single_quoted_allowed; |
|---|
| 1190 | /* Can the scalar be expressed in the literal or folded styles? */ |
|---|
| 1191 | int is_block_allowed; |
|---|
| 1192 | /* The output style. */ |
|---|
| 1193 | yaml_scalar_style_t style; |
|---|
| 1194 | } scalar_data; |
|---|
| 1195 | |
|---|
| 1196 | /* |
|---|
| 1197 | * Dumper stuff. |
|---|
| 1198 | */ |
|---|
| 1199 | |
|---|
| 1200 | /* If the stream was already opened? */ |
|---|
| 1201 | int is_opened; |
|---|
| 1202 | /* If the stream was already closed? */ |
|---|
| 1203 | int is_closed; |
|---|
| 1204 | |
|---|
| 1205 | /* The information associated with the document nodes. */ |
|---|
| 1206 | struct { |
|---|
| 1207 | /* The number of references. */ |
|---|
| 1208 | size_t references; |
|---|
| 1209 | /* The anchor id. */ |
|---|
| 1210 | int anchor; |
|---|
| 1211 | /* If the node has been emitted? */ |
|---|
| 1212 | int is_serialized; |
|---|
| 1213 | } *anchors; |
|---|
| 1214 | |
|---|
| 1215 | /* The last assigned anchor id. */ |
|---|
| 1216 | int last_anchor_id; |
|---|
| 1217 | |
|---|
| 1218 | /* The currently emitted document. */ |
|---|
| 1219 | yaml_document_t *document; |
|---|
| 1220 | |
|---|
| 1221 | }; |
|---|
| 1222 | |
|---|
| 1223 | /* |
|---|
| 1224 | * Reader: Ensure that the buffer contains at least `length` characters. |
|---|
| 1225 | */ |
|---|
| 1226 | |
|---|
| 1227 | YAML_DECLARE(int) |
|---|
| 1228 | yaml_parser_update_buffer(yaml_parser_t *parser, size_t length); |
|---|
| 1229 | |
|---|
| 1230 | /* |
|---|
| 1231 | * Scanner: Ensure that the token stack contains at least one token ready. |
|---|
| 1232 | */ |
|---|
| 1233 | |
|---|
| 1234 | YAML_DECLARE(int) |
|---|
| 1235 | yaml_parser_fetch_more_tokens(yaml_parser_t *parser); |
|---|
| 1236 | |
|---|