Token.h 3.4 KB

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