| 1 | /* |
| 2 | * Copyright (c) 2007-2014, Lloyd Hilaiel <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __YAJL_LEX_H__ |
| 18 | #define __YAJL_LEX_H__ |
| 19 | |
| 20 | #include "api/yajl_common.h" |
| 21 | |
| 22 | typedef enum { |
| 23 | yajl_tok_bool, |
| 24 | yajl_tok_colon, |
| 25 | yajl_tok_comma, |
| 26 | yajl_tok_eof, |
| 27 | yajl_tok_error, |
| 28 | yajl_tok_left_brace, |
| 29 | yajl_tok_left_bracket, |
| 30 | yajl_tok_null, |
| 31 | yajl_tok_right_brace, |
| 32 | yajl_tok_right_bracket, |
| 33 | |
| 34 | /* we differentiate between integers and doubles to allow the |
| 35 | * parser to interpret the number without re-scanning */ |
| 36 | yajl_tok_integer, |
| 37 | yajl_tok_double, |
| 38 | |
| 39 | /* we differentiate between strings which require further processing, |
| 40 | * and strings that do not */ |
| 41 | yajl_tok_string, |
| 42 | yajl_tok_string_with_escapes, |
| 43 | |
| 44 | /* comment tokens are not currently returned to the parser, ever */ |
| 45 | yajl_tok_comment |
| 46 | } yajl_tok; |
| 47 | |
| 48 | typedef struct yajl_lexer_t * yajl_lexer; |
| 49 | |
| 50 | yajl_lexer yajl_lex_alloc(yajl_alloc_funcs * alloc, |
| 51 | unsigned int allowComments, |
| 52 | unsigned int validateUTF8); |
| 53 | |
| 54 | void yajl_lex_free(yajl_lexer lexer); |
| 55 | |
| 56 | /** |
| 57 | * run/continue a lex. "offset" is an input/output parameter. |
| 58 | * It should be initialized to zero for a |
| 59 | * new chunk of target text, and upon subsetquent calls with the same |
| 60 | * target text should passed with the value of the previous invocation. |
| 61 | * |
| 62 | * the client may be interested in the value of offset when an error is |
| 63 | * returned from the lexer. This allows the client to render useful |
| 64 | * error messages. |
| 65 | * |
| 66 | * When you pass the next chunk of data, context should be reinitialized |
| 67 | * to zero. |
| 68 | * |
| 69 | * Finally, the output buffer is usually just a pointer into the jsonText, |
| 70 | * however in cases where the entity being lexed spans multiple chunks, |
| 71 | * the lexer will buffer the entity and the data returned will be |
| 72 | * a pointer into that buffer. |
| 73 | * |
| 74 | * This behavior is abstracted from client code except for the performance |
| 75 | * implications which require that the client choose a reasonable chunk |
| 76 | * size to get adequate performance. |
| 77 | */ |
| 78 | yajl_tok yajl_lex_lex(yajl_lexer lexer, const unsigned char * jsonText, |
| 79 | size_t jsonTextLen, size_t * offset, |
| 80 | const unsigned char ** outBuf, size_t * outLen); |
| 81 | |
| 82 | /** have a peek at the next token, but don't move the lexer forward */ |
| 83 | yajl_tok yajl_lex_peek(yajl_lexer lexer, const unsigned char * jsonText, |
| 84 | size_t jsonTextLen, size_t offset); |
| 85 | |
| 86 | |
| 87 | typedef enum { |
| 88 | yajl_lex_e_ok = 0, |
| 89 | yajl_lex_string_invalid_utf8, |
| 90 | yajl_lex_string_invalid_escaped_char, |
| 91 | yajl_lex_string_invalid_json_char, |
| 92 | yajl_lex_string_invalid_hex_char, |
| 93 | yajl_lex_invalid_char, |
| 94 | yajl_lex_invalid_string, |
| 95 | yajl_lex_missing_integer_after_decimal, |
| 96 | yajl_lex_missing_integer_after_exponent, |
| 97 | yajl_lex_missing_integer_after_minus, |
| 98 | yajl_lex_unallowed_comment |
| 99 | } yajl_lex_error; |
| 100 | |
| 101 | const char * yajl_lex_error_to_string(yajl_lex_error error); |
| 102 | |
| 103 | /** allows access to more specific information about the lexical |
| 104 | * error when yajl_lex_lex returns yajl_tok_error. */ |
| 105 | yajl_lex_error yajl_lex_get_error(yajl_lexer lexer); |
| 106 | |
| 107 | /** get the current offset into the most recently lexed json string. */ |
| 108 | size_t yajl_lex_current_offset(yajl_lexer lexer); |
| 109 | |
| 110 | /** get the number of lines lexed by this lexer instance */ |
| 111 | size_t yajl_lex_current_line(yajl_lexer lexer); |
| 112 | |
| 113 | /** get the number of chars lexed by this lexer instance since the last |
| 114 | * \n or \r */ |
| 115 | size_t yajl_lex_current_char(yajl_lexer lexer); |
| 116 | |
| 117 | #endif |