Token.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/FlyString.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibWeb/CSS/Number.h>
  11. namespace Web::CSS::Parser {
  12. class Token {
  13. friend class Tokenizer;
  14. public:
  15. enum class Type {
  16. Invalid,
  17. EndOfFile,
  18. Ident,
  19. Function,
  20. AtKeyword,
  21. Hash,
  22. String,
  23. BadString,
  24. Url,
  25. BadUrl,
  26. Delim,
  27. Number,
  28. Percentage,
  29. Dimension,
  30. Whitespace,
  31. CDO,
  32. CDC,
  33. Colon,
  34. Semicolon,
  35. Comma,
  36. OpenSquare,
  37. CloseSquare,
  38. OpenParen,
  39. CloseParen,
  40. OpenCurly,
  41. CloseCurly
  42. };
  43. enum class HashType {
  44. Id,
  45. Unrestricted,
  46. };
  47. struct Position {
  48. size_t line { 0 };
  49. size_t column { 0 };
  50. };
  51. Type type() const { return m_type; }
  52. bool is(Type type) const { return m_type == type; }
  53. StringView ident() const
  54. {
  55. VERIFY(m_type == Type::Ident);
  56. return m_value.bytes_as_string_view();
  57. }
  58. StringView function() const
  59. {
  60. VERIFY(m_type == Type::Function);
  61. return m_value.bytes_as_string_view();
  62. }
  63. u32 delim() const
  64. {
  65. VERIFY(m_type == Type::Delim);
  66. return *Utf8View(m_value.bytes_as_string_view()).begin();
  67. }
  68. StringView string() const
  69. {
  70. VERIFY(m_type == Type::String);
  71. return m_value.bytes_as_string_view();
  72. }
  73. StringView url() const
  74. {
  75. VERIFY(m_type == Type::Url);
  76. return m_value.bytes_as_string_view();
  77. }
  78. StringView at_keyword() const
  79. {
  80. VERIFY(m_type == Type::AtKeyword);
  81. return m_value.bytes_as_string_view();
  82. }
  83. HashType hash_type() const
  84. {
  85. VERIFY(m_type == Type::Hash);
  86. return m_hash_type;
  87. }
  88. StringView hash_value() const
  89. {
  90. VERIFY(m_type == Type::Hash);
  91. return m_value.bytes_as_string_view();
  92. }
  93. Number const& number() const
  94. {
  95. VERIFY(m_type == Type::Number || m_type == Type::Dimension || m_type == Type::Percentage);
  96. return m_number_value;
  97. }
  98. float number_value() const
  99. {
  100. VERIFY(m_type == Type::Number);
  101. return m_number_value.value();
  102. }
  103. i64 to_integer() const
  104. {
  105. VERIFY(m_type == Type::Number && m_number_value.is_integer());
  106. return m_number_value.integer_value();
  107. }
  108. StringView dimension_unit() const
  109. {
  110. VERIFY(m_type == Type::Dimension);
  111. return m_value.bytes_as_string_view();
  112. }
  113. float dimension_value() const
  114. {
  115. VERIFY(m_type == Type::Dimension);
  116. return m_number_value.value();
  117. }
  118. i64 dimension_value_int() const { return m_number_value.integer_value(); }
  119. float percentage() const
  120. {
  121. VERIFY(m_type == Type::Percentage);
  122. return m_number_value.value();
  123. }
  124. Type mirror_variant() const;
  125. StringView bracket_string() const;
  126. StringView bracket_mirror_string() const;
  127. ErrorOr<String> to_string() const;
  128. ErrorOr<String> to_debug_string() const;
  129. String const& representation() const { return m_representation; }
  130. Position const& start_position() const { return m_start_position; }
  131. Position const& end_position() const { return m_end_position; }
  132. static Token of_string(FlyString str)
  133. {
  134. Token token;
  135. token.m_type = Type::String;
  136. token.m_value = move(str);
  137. return token;
  138. }
  139. static Token create_number(float value)
  140. {
  141. Token token;
  142. token.m_type = Type::Number;
  143. token.m_number_value = Number(Number::Type::Number, value);
  144. return token;
  145. }
  146. static Token create_percentage(float value)
  147. {
  148. Token token;
  149. token.m_type = Type::Percentage;
  150. token.m_number_value = Number(Number::Type::Number, value);
  151. return token;
  152. }
  153. private:
  154. Type m_type { Type::Invalid };
  155. FlyString m_value;
  156. Number m_number_value;
  157. HashType m_hash_type { HashType::Unrestricted };
  158. String m_representation;
  159. Position m_start_position;
  160. Position m_end_position;
  161. };
  162. }