Tokenizer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Types.h>
  11. #include <AK/Utf8View.h>
  12. #include <LibWeb/CSS/Parser/Token.h>
  13. #include <LibWeb/Forward.h>
  14. namespace Web::CSS::Parser {
  15. class U32Twin {
  16. public:
  17. void set(size_t index, u32 value)
  18. {
  19. if (index == 0)
  20. first = value;
  21. if (index == 1)
  22. second = value;
  23. }
  24. u32 first {};
  25. u32 second {};
  26. };
  27. class U32Triplet {
  28. public:
  29. void set(size_t index, u32 value)
  30. {
  31. if (index == 0)
  32. first = value;
  33. if (index == 1)
  34. second = value;
  35. if (index == 2)
  36. third = value;
  37. }
  38. U32Twin to_twin_12()
  39. {
  40. return { first, second };
  41. }
  42. U32Twin to_twin_23()
  43. {
  44. return { second, third };
  45. }
  46. u32 first {};
  47. u32 second {};
  48. u32 third {};
  49. };
  50. class Tokenizer {
  51. public:
  52. static ErrorOr<Vector<Token>> tokenize(StringView input, StringView encoding);
  53. [[nodiscard]] static Token create_eof_token();
  54. private:
  55. explicit Tokenizer(String decoded_input);
  56. [[nodiscard]] ErrorOr<Vector<Token>> tokenize();
  57. size_t current_byte_offset() const;
  58. ErrorOr<String> input_since(size_t offset) const;
  59. [[nodiscard]] u32 next_code_point();
  60. [[nodiscard]] u32 peek_code_point(size_t offset = 0) const;
  61. [[nodiscard]] U32Twin peek_twin() const;
  62. [[nodiscard]] U32Triplet peek_triplet() const;
  63. [[nodiscard]] U32Twin start_of_input_stream_twin();
  64. [[nodiscard]] U32Triplet start_of_input_stream_triplet();
  65. [[nodiscard]] static Token create_new_token(Token::Type);
  66. [[nodiscard]] static Token create_value_token(Token::Type, FlyString&& value, String&& representation);
  67. [[nodiscard]] static Token create_value_token(Token::Type, u32 value, String&& representation);
  68. [[nodiscard]] ErrorOr<Token> consume_a_token();
  69. [[nodiscard]] ErrorOr<Token> consume_string_token(u32 ending_code_point);
  70. [[nodiscard]] ErrorOr<Token> consume_a_numeric_token();
  71. [[nodiscard]] ErrorOr<Token> consume_an_ident_like_token();
  72. [[nodiscard]] Number consume_a_number();
  73. [[nodiscard]] double convert_a_string_to_a_number(StringView);
  74. [[nodiscard]] ErrorOr<FlyString> consume_an_ident_sequence();
  75. [[nodiscard]] u32 consume_escaped_code_point();
  76. [[nodiscard]] ErrorOr<Token> consume_a_url_token();
  77. void consume_the_remnants_of_a_bad_url();
  78. void consume_comments();
  79. void consume_as_much_whitespace_as_possible();
  80. void reconsume_current_input_code_point();
  81. [[nodiscard]] static bool is_valid_escape_sequence(U32Twin);
  82. [[nodiscard]] static bool would_start_an_ident_sequence(U32Triplet);
  83. [[nodiscard]] static bool would_start_a_number(U32Triplet);
  84. String m_decoded_input;
  85. Utf8View m_utf8_view;
  86. AK::Utf8CodePointIterator m_utf8_iterator;
  87. AK::Utf8CodePointIterator m_prev_utf8_iterator;
  88. Token::Position m_position;
  89. Token::Position m_prev_position;
  90. };
  91. }