error.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. #include "error.h"
  17. #include <assert.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "gumbo.h"
  22. #include "parser.h"
  23. #include "string_buffer.h"
  24. #include "util.h"
  25. #include "vector.h"
  26. static const size_t kMessageBufferSize = 256;
  27. // Prints a formatted message to a StringBuffer. This automatically resizes the
  28. // StringBuffer as necessary to fit the message. Returns the number of bytes
  29. // written.
  30. static int print_message(GumboParser* parser, GumboStringBuffer* output,
  31. const char* format, ...) {
  32. va_list args;
  33. va_start(args, format);
  34. int remaining_capacity = output->capacity - output->length;
  35. int bytes_written = vsnprintf(output->data + output->length,
  36. remaining_capacity, format, args);
  37. if (bytes_written > remaining_capacity) {
  38. gumbo_string_buffer_reserve(
  39. parser, output->capacity + bytes_written, output);
  40. remaining_capacity = output->capacity - output->length;
  41. bytes_written = vsnprintf(output->data + output->length,
  42. remaining_capacity, format, args);
  43. }
  44. output->length += bytes_written;
  45. va_end(args);
  46. return bytes_written;
  47. }
  48. static void print_tag_stack(
  49. GumboParser* parser, const GumboParserError* error,
  50. GumboStringBuffer* output) {
  51. print_message(parser, output, " Currently open tags: ");
  52. for (int i = 0; i < error->tag_stack.length; ++i) {
  53. if (i) {
  54. print_message(parser, output, ", ");
  55. }
  56. GumboTag tag = (GumboTag) error->tag_stack.data[i];
  57. print_message(parser, output, gumbo_normalized_tagname(tag));
  58. }
  59. gumbo_string_buffer_append_codepoint(parser, '.', output);
  60. }
  61. static void handle_parser_error(GumboParser* parser,
  62. const GumboParserError* error,
  63. GumboStringBuffer* output) {
  64. if (error->parser_state == GUMBO_INSERTION_MODE_INITIAL &&
  65. error->input_type != GUMBO_TOKEN_DOCTYPE) {
  66. print_message(parser, output,
  67. "The doctype must be the first token in the document");
  68. return;
  69. }
  70. switch (error->input_type) {
  71. case GUMBO_TOKEN_DOCTYPE:
  72. print_message(parser, output, "This is not a legal doctype");
  73. return;
  74. case GUMBO_TOKEN_COMMENT:
  75. // Should never happen; comments are always legal.
  76. assert(0);
  77. // But just in case...
  78. print_message(parser, output, "Comments aren't legal here");
  79. return;
  80. case GUMBO_TOKEN_WHITESPACE:
  81. case GUMBO_TOKEN_CHARACTER:
  82. print_message(parser, output, "Character tokens aren't legal here");
  83. return;
  84. case GUMBO_TOKEN_NULL:
  85. print_message(parser, output, "Null bytes are not allowed in HTML5");
  86. return;
  87. case GUMBO_TOKEN_EOF:
  88. if (error->parser_state == GUMBO_INSERTION_MODE_INITIAL) {
  89. print_message(parser, output, "You must provide a doctype");
  90. } else {
  91. print_message(parser, output, "Premature end of file");
  92. print_tag_stack(parser, error, output);
  93. }
  94. return;
  95. case GUMBO_TOKEN_START_TAG:
  96. case GUMBO_TOKEN_END_TAG:
  97. print_message(parser, output, "That tag isn't allowed here");
  98. print_tag_stack(parser, error, output);
  99. // TODO(jdtang): Give more specific messaging.
  100. return;
  101. }
  102. }
  103. // Finds the preceding newline in an original source buffer from a given byte
  104. // location. Returns a character pointer to the character after that, or a
  105. // pointer to the beginning of the string if this is the first line.
  106. static const char* find_last_newline(
  107. const char* original_text, const char* error_location) {
  108. assert(error_location >= original_text);
  109. const char* c = error_location;
  110. for (; c != original_text && *c != '\n'; --c) {
  111. // There may be an error at EOF, which would be a nul byte.
  112. assert(*c || c == error_location);
  113. }
  114. return c == original_text ? c : c + 1;
  115. }
  116. // Finds the next newline in the original source buffer from a given byte
  117. // location. Returns a character pointer to that newline, or a pointer to the
  118. // terminating null byte if this is the last line.
  119. static const char* find_next_newline(
  120. const char* original_text, const char* error_location) {
  121. const char* c = error_location;
  122. for (; *c && *c != '\n'; ++c);
  123. return c;
  124. }
  125. GumboError* gumbo_add_error(GumboParser* parser) {
  126. int max_errors = parser->_options->max_errors;
  127. if (max_errors >= 0 && parser->_output->errors.length >= max_errors) {
  128. return NULL;
  129. }
  130. GumboError* error = gumbo_parser_allocate(parser, sizeof(GumboError));
  131. gumbo_vector_add(parser, error, &parser->_output->errors);
  132. return error;
  133. }
  134. void gumbo_error_to_string(
  135. GumboParser* parser, const GumboError* error, GumboStringBuffer* output) {
  136. print_message(parser, output, "@%d:%d: ",
  137. error->position.line, error->position.column);
  138. switch (error->type) {
  139. case GUMBO_ERR_UTF8_INVALID:
  140. print_message(parser, output, "Invalid UTF8 character 0x%x",
  141. error->v.codepoint);
  142. break;
  143. case GUMBO_ERR_UTF8_TRUNCATED:
  144. print_message(parser, output,
  145. "Input stream ends with a truncated UTF8 character 0x%x",
  146. error->v.codepoint);
  147. break;
  148. case GUMBO_ERR_NUMERIC_CHAR_REF_NO_DIGITS:
  149. print_message(parser, output,
  150. "No digits after &# in numeric character reference");
  151. break;
  152. case GUMBO_ERR_NUMERIC_CHAR_REF_WITHOUT_SEMICOLON:
  153. print_message(parser, output,
  154. "The numeric character reference &#%d should be followed "
  155. "by a semicolon", error->v.codepoint);
  156. break;
  157. case GUMBO_ERR_NUMERIC_CHAR_REF_INVALID:
  158. print_message(parser, output,
  159. "The numeric character reference &#%d; encodes an invalid "
  160. "unicode codepoint", error->v.codepoint);
  161. break;
  162. case GUMBO_ERR_NAMED_CHAR_REF_WITHOUT_SEMICOLON:
  163. // The textual data came from one of the literal strings in the table, and
  164. // so it'll be null-terminated.
  165. print_message(parser, output,
  166. "The named character reference &%.*s should be followed by a "
  167. "semicolon", (int) error->v.text.length, error->v.text.data);
  168. break;
  169. case GUMBO_ERR_NAMED_CHAR_REF_INVALID:
  170. print_message(parser, output,
  171. "The named character reference &%.*s; is not a valid entity name",
  172. (int) error->v.text.length, error->v.text.data);
  173. break;
  174. case GUMBO_ERR_DUPLICATE_ATTR:
  175. print_message(parser, output,
  176. "Attribute %s occurs multiple times, at positions %d and %d",
  177. error->v.duplicate_attr.name,
  178. error->v.duplicate_attr.original_index,
  179. error->v.duplicate_attr.new_index);
  180. break;
  181. case GUMBO_ERR_PARSER:
  182. case GUMBO_ERR_UNACKNOWLEDGED_SELF_CLOSING_TAG:
  183. handle_parser_error(parser, &error->v.parser, output);
  184. break;
  185. default:
  186. print_message(parser, output,
  187. "Tokenizer error with an unimplemented error message");
  188. break;
  189. }
  190. gumbo_string_buffer_append_codepoint(parser, '.', output);
  191. }
  192. void gumbo_caret_diagnostic_to_string(
  193. GumboParser* parser, const GumboError* error,
  194. const char* source_text, GumboStringBuffer* output) {
  195. gumbo_error_to_string(parser, error, output);
  196. const char* line_start =
  197. find_last_newline(source_text, error->original_text);
  198. const char* line_end =
  199. find_next_newline(source_text, error->original_text);
  200. GumboStringPiece original_line;
  201. original_line.data = line_start;
  202. original_line.length = line_end - line_start;
  203. gumbo_string_buffer_append_codepoint(parser, '\n', output);
  204. gumbo_string_buffer_append_string(parser, &original_line, output);
  205. gumbo_string_buffer_append_codepoint(parser, '\n', output);
  206. gumbo_string_buffer_reserve(
  207. parser, output->length + error->position.column, output);
  208. int num_spaces = error->position.column - 1;
  209. memset(output->data + output->length, ' ', num_spaces);
  210. output->length += num_spaces;
  211. gumbo_string_buffer_append_codepoint(parser, '^', output);
  212. gumbo_string_buffer_append_codepoint(parser, '\n', output);
  213. }
  214. void gumbo_print_caret_diagnostic(
  215. GumboParser* parser, const GumboError* error, const char* source_text) {
  216. GumboStringBuffer text;
  217. gumbo_string_buffer_init(parser, &text);
  218. gumbo_caret_diagnostic_to_string(parser, error, source_text, &text);
  219. printf("%.*s", (int) text.length, text.data);
  220. gumbo_string_buffer_destroy(parser, &text);
  221. }
  222. void gumbo_error_destroy(GumboParser* parser, GumboError* error) {
  223. if (error->type == GUMBO_ERR_PARSER ||
  224. error->type == GUMBO_ERR_UNACKNOWLEDGED_SELF_CLOSING_TAG) {
  225. gumbo_vector_destroy(parser, &error->v.parser.tag_stack);
  226. } else if (error->type == GUMBO_ERR_DUPLICATE_ATTR) {
  227. gumbo_parser_deallocate(parser, (void*) error->v.duplicate_attr.name);
  228. }
  229. gumbo_parser_deallocate(parser, error);
  230. }
  231. void gumbo_init_errors(GumboParser* parser) {
  232. gumbo_vector_init(parser, 5, &parser->_output->errors);
  233. }
  234. void gumbo_destroy_errors(GumboParser* parser) {
  235. for (int i = 0; i < parser->_output->errors.length; ++i) {
  236. gumbo_error_destroy(parser, parser->_output->errors.data[i]);
  237. }
  238. gumbo_vector_destroy(parser, &parser->_output->errors);
  239. }