yajl_lex.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
  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. #ifndef __YAJL_LEX_H__
  17. #define __YAJL_LEX_H__
  18. #include "api/yajl_common.h"
  19. typedef enum {
  20. yajl_tok_bool,
  21. yajl_tok_colon,
  22. yajl_tok_comma,
  23. yajl_tok_eof,
  24. yajl_tok_error,
  25. yajl_tok_left_brace,
  26. yajl_tok_left_bracket,
  27. yajl_tok_null,
  28. yajl_tok_inf,
  29. yajl_tok_minus_inf,
  30. yajl_tok_right_brace,
  31. yajl_tok_right_bracket,
  32. /* we differentiate between integers and doubles to allow the
  33. * parser to interpret the number without re-scanning */
  34. yajl_tok_integer,
  35. yajl_tok_double,
  36. /* we differentiate between strings which require further processing,
  37. * and strings that do not */
  38. yajl_tok_string,
  39. yajl_tok_string_with_escapes,
  40. /* comment tokens are not currently returned to the parser, ever */
  41. yajl_tok_comment
  42. } yajl_tok;
  43. typedef struct yajl_lexer_t * yajl_lexer;
  44. yajl_lexer yajl_lex_alloc(yajl_alloc_funcs * alloc,
  45. unsigned int allowComments,
  46. unsigned int validateUTF8);
  47. void yajl_lex_free(yajl_lexer lexer);
  48. /**
  49. * run/continue a lex. "offset" is an input/output parameter.
  50. * It should be initialized to zero for a
  51. * new chunk of target text, and upon subsetquent calls with the same
  52. * target text should passed with the value of the previous invocation.
  53. *
  54. * the client may be interested in the value of offset when an error is
  55. * returned from the lexer. This allows the client to render useful
  56. * error messages.
  57. *
  58. * When you pass the next chunk of data, context should be reinitialized
  59. * to zero.
  60. *
  61. * Finally, the output buffer is usually just a pointer into the jsonText,
  62. * however in cases where the entity being lexed spans multiple chunks,
  63. * the lexer will buffer the entity and the data returned will be
  64. * a pointer into that buffer.
  65. *
  66. * This behavior is abstracted from client code except for the performance
  67. * implications which require that the client choose a reasonable chunk
  68. * size to get adequate performance.
  69. */
  70. yajl_tok yajl_lex_lex(yajl_lexer lexer, const unsigned char * jsonText,
  71. size_t jsonTextLen, size_t * offset,
  72. const unsigned char ** outBuf, size_t * outLen);
  73. /** have a peek at the next token, but don't move the lexer forward */
  74. yajl_tok yajl_lex_peek(yajl_lexer lexer, const unsigned char * jsonText,
  75. size_t jsonTextLen, size_t offset);
  76. typedef enum {
  77. yajl_lex_e_ok = 0,
  78. yajl_lex_string_invalid_utf8,
  79. yajl_lex_string_invalid_escaped_char,
  80. yajl_lex_string_invalid_json_char,
  81. yajl_lex_string_invalid_hex_char,
  82. yajl_lex_invalid_char,
  83. yajl_lex_invalid_string,
  84. yajl_lex_missing_integer_after_decimal,
  85. yajl_lex_missing_integer_after_exponent,
  86. yajl_lex_missing_integer_after_minus,
  87. yajl_lex_invalid_infinity,
  88. yajl_lex_unallowed_comment
  89. } yajl_lex_error;
  90. const char * yajl_lex_error_to_string(yajl_lex_error error);
  91. /** allows access to more specific information about the lexical
  92. * error when yajl_lex_lex returns yajl_tok_error. */
  93. yajl_lex_error yajl_lex_get_error(yajl_lexer lexer);
  94. /** get the current offset into the most recently lexed json string. */
  95. size_t yajl_lex_current_offset(yajl_lexer lexer);
  96. /** get the number of lines lexed by this lexer instance */
  97. size_t yajl_lex_current_line(yajl_lexer lexer);
  98. /** get the number of chars lexed by this lexer instance since the last
  99. * \n or \r */
  100. size_t yajl_lex_current_char(yajl_lexer lexer);
  101. /** get size of allocated buffer */
  102. size_t yajl_lex_buf_capacity(yajl_lexer lexer);
  103. #endif