tokenizer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. //
  17. // This contains an implementation of a tokenizer for HTML5. It consumes a
  18. // buffer of UTF-8 characters, and then emits a stream of tokens.
  19. #ifndef GUMBO_TOKENIZER_H_
  20. #define GUMBO_TOKENIZER_H_
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include "gumbo.h"
  24. #include "token_type.h"
  25. #include "tokenizer_states.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. struct GumboInternalParser;
  30. // Struct containing all information pertaining to doctype tokens.
  31. typedef struct GumboInternalTokenDocType {
  32. const char* name;
  33. const char* public_identifier;
  34. const char* system_identifier;
  35. bool force_quirks;
  36. // There's no way to tell a 0-length public or system ID apart from the
  37. // absence of a public or system ID, but they're handled different by the
  38. // spec, so we need bool flags for them.
  39. bool has_public_identifier;
  40. bool has_system_identifier;
  41. } GumboTokenDocType;
  42. // Struct containing all information pertaining to start tag tokens.
  43. typedef struct GumboInternalTokenStartTag {
  44. GumboTag tag;
  45. GumboVector /* GumboAttribute */ attributes;
  46. bool is_self_closing;
  47. } GumboTokenStartTag;
  48. // A data structure representing a single token in the input stream. This
  49. // contains an enum for the type, the source position, a GumboStringPiece
  50. // pointing to the original text, and then a union for any parsed data.
  51. typedef struct GumboInternalToken {
  52. GumboTokenType type;
  53. GumboSourcePosition position;
  54. GumboStringPiece original_text;
  55. union {
  56. GumboTokenDocType doc_type;
  57. GumboTokenStartTag start_tag;
  58. GumboTag end_tag;
  59. const char* text; // For comments.
  60. int character; // For character, whitespace, null, and EOF tokens.
  61. } v;
  62. } GumboToken;
  63. // Initializes the tokenizer state within the GumboParser object, setting up a
  64. // parse of the specified text.
  65. void gumbo_tokenizer_state_init(
  66. struct GumboInternalParser* parser, const char* text, size_t text_length);
  67. // Destroys the tokenizer state within the GumboParser object, freeing any
  68. // dynamically-allocated structures within it.
  69. void gumbo_tokenizer_state_destroy(struct GumboInternalParser* parser);
  70. // Sets the tokenizer state to the specified value. This is needed by some
  71. // parser states, which alter the state of the tokenizer in response to tags
  72. // seen.
  73. void gumbo_tokenizer_set_state(
  74. struct GumboInternalParser* parser, GumboTokenizerEnum state);
  75. // Flags whether the current node is a foreign content element. This is
  76. // necessary for the markup declaration open state, where the tokenizer must be
  77. // aware of the state of the parser to properly tokenize bad comment tags.
  78. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#markup-declaration-open-state
  79. void gumbo_tokenizer_set_is_current_node_foreign(
  80. struct GumboInternalParser* parser, bool is_foreign);
  81. // Lexes a single token from the specified buffer, filling the output with the
  82. // parsed GumboToken data structure. Returns true for a successful
  83. // tokenization, false if a parse error occurs.
  84. //
  85. // Example:
  86. // struct GumboInternalParser parser;
  87. // GumboToken output;
  88. // gumbo_tokenizer_state_init(&parser, text, strlen(text));
  89. // while (gumbo_lex(&parser, &output)) {
  90. // ...do stuff with output.
  91. // gumbo_token_destroy(&parser, &token);
  92. // }
  93. // gumbo_tokenizer_state_destroy(&parser);
  94. bool gumbo_lex(struct GumboInternalParser* parser, GumboToken* output);
  95. // Frees the internally-allocated pointers within an GumboToken. Note that this
  96. // doesn't free the token itself, since oftentimes it will be allocated on the
  97. // stack. A simple call to free() (or GumboParser->deallocator, if
  98. // appropriate) can handle that.
  99. //
  100. // Note that if you are handing over ownership of the internal strings to some
  101. // other data structure - for example, a parse tree - these do not need to be
  102. // freed.
  103. void gumbo_token_destroy(struct GumboInternalParser* parser, GumboToken* token);
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif // GUMBO_TOKENIZER_H_