Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 2 | * $Id: json_tokener.h,v 1.10 2006/07/25 03:24:50 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the MIT license. See COPYING for details. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #ifndef _json_tokener_h_ |
| 13 | #define _json_tokener_h_ |
| 14 | |
Michael Clark | a327254 | 2008-01-26 01:10:47 +0000 | [diff] [blame] | 15 | #include <stddef.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 16 | #include "json_object.h" |
| 17 | |
| 18 | enum json_tokener_error { |
| 19 | json_tokener_success, |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 20 | json_tokener_continue, |
| 21 | json_tokener_error_depth, |
| 22 | json_tokener_error_parse_eof, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 23 | json_tokener_error_parse_unexpected, |
| 24 | json_tokener_error_parse_null, |
| 25 | json_tokener_error_parse_boolean, |
| 26 | json_tokener_error_parse_number, |
| 27 | json_tokener_error_parse_array, |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 28 | json_tokener_error_parse_object_key_name, |
| 29 | json_tokener_error_parse_object_key_sep, |
| 30 | json_tokener_error_parse_object_value_sep, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 31 | json_tokener_error_parse_string, |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 32 | json_tokener_error_parse_comment |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | enum json_tokener_state { |
| 36 | json_tokener_state_eatws, |
| 37 | json_tokener_state_start, |
| 38 | json_tokener_state_finish, |
| 39 | json_tokener_state_null, |
| 40 | json_tokener_state_comment_start, |
| 41 | json_tokener_state_comment, |
| 42 | json_tokener_state_comment_eol, |
| 43 | json_tokener_state_comment_end, |
| 44 | json_tokener_state_string, |
| 45 | json_tokener_state_string_escape, |
| 46 | json_tokener_state_escape_unicode, |
| 47 | json_tokener_state_boolean, |
| 48 | json_tokener_state_number, |
| 49 | json_tokener_state_array, |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 50 | json_tokener_state_array_add, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 51 | json_tokener_state_array_sep, |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 52 | json_tokener_state_object_field_start, |
| 53 | json_tokener_state_object_field, |
| 54 | json_tokener_state_object_field_end, |
| 55 | json_tokener_state_object_value, |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 56 | json_tokener_state_object_value_add, |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 57 | json_tokener_state_object_sep |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 60 | struct json_tokener_srec |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 61 | { |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 62 | enum json_tokener_state state, saved_state; |
| 63 | struct json_object *obj; |
| 64 | struct json_object *current; |
| 65 | char *obj_field_name; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 68 | #define JSON_TOKENER_MAX_DEPTH 32 |
| 69 | |
| 70 | struct json_tokener |
| 71 | { |
| 72 | char *str; |
| 73 | struct printbuf *pb; |
| 74 | int depth, is_double, st_pos, char_offset; |
Michael Clark | c8f4a6e | 2007-12-07 02:44:24 +0000 | [diff] [blame] | 75 | ptrdiff_t err; |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 76 | unsigned int ucs_char; |
| 77 | char quote_char; |
| 78 | struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH]; |
| 79 | }; |
| 80 | |
| 81 | extern const char* json_tokener_errors[]; |
| 82 | |
Michael Clark | 14862b1 | 2007-12-07 02:50:42 +0000 | [diff] [blame] | 83 | extern struct json_tokener* json_tokener_new(void); |
Michael Clark | a850f8e | 2007-03-13 08:26:26 +0000 | [diff] [blame] | 84 | extern void json_tokener_free(struct json_tokener *tok); |
| 85 | extern void json_tokener_reset(struct json_tokener *tok); |
| 86 | extern struct json_object* json_tokener_parse(char *str); |
| 87 | extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok, |
| 88 | char *str, int len); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 89 | |
| 90 | #endif |