Token.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <AK/StringBuilder.h>
  10. namespace Web::CSS {
  11. class Token {
  12. friend class Parser;
  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. enum class NumberType {
  48. Integer,
  49. Number,
  50. };
  51. struct Position {
  52. size_t line { 0 };
  53. size_t column { 0 };
  54. };
  55. Type type() const { return m_type; }
  56. bool is(Type type) const { return m_type == type; }
  57. StringView ident() const
  58. {
  59. VERIFY(m_type == Type::Ident);
  60. return m_value.string_view();
  61. }
  62. StringView delim() const
  63. {
  64. VERIFY(m_type == Type::Delim);
  65. return m_value.string_view();
  66. }
  67. StringView string() const
  68. {
  69. VERIFY(m_type == Type::String);
  70. return m_value.string_view();
  71. }
  72. StringView url() const
  73. {
  74. VERIFY(m_type == Type::Url);
  75. return m_value.string_view();
  76. }
  77. StringView at_keyword() const
  78. {
  79. VERIFY(m_type == Type::AtKeyword);
  80. return m_value.string_view();
  81. }
  82. HashType hash_type() const
  83. {
  84. VERIFY(m_type == Type::Hash);
  85. return m_hash_type;
  86. }
  87. StringView hash_value() const
  88. {
  89. VERIFY(m_type == Type::Hash);
  90. return m_value.string_view();
  91. }
  92. bool is(NumberType number_type) const { return is(Token::Type::Number) && m_number_type == number_type; }
  93. StringView number_string_value() const
  94. {
  95. VERIFY(m_type == Type::Number);
  96. return m_value.string_view();
  97. }
  98. i64 to_integer() const
  99. {
  100. VERIFY(m_type == Type::Number && m_number_type == NumberType::Integer);
  101. return number_string_value().to_int().value();
  102. }
  103. bool is_integer_value_signed() const { return number_string_value().starts_with('-') || number_string_value().starts_with('+'); }
  104. StringView dimension_unit() const
  105. {
  106. VERIFY(m_type == Type::Dimension);
  107. return m_unit.string_view();
  108. }
  109. StringView dimension_value() const
  110. {
  111. VERIFY(m_type == Type::Dimension);
  112. return m_value.string_view();
  113. }
  114. i64 dimension_value_int() const { return dimension_value().to_int().value(); }
  115. NumberType number_type() const
  116. {
  117. VERIFY((m_type == Type::Number) || (m_type == Type::Dimension));
  118. return m_number_type;
  119. }
  120. Type mirror_variant() const;
  121. String bracket_string() const;
  122. String bracket_mirror_string() const;
  123. String to_debug_string() const;
  124. Position const& start_position() const { return m_start_position; }
  125. Position const& end_position() const { return m_end_position; }
  126. private:
  127. Type m_type { Type::Invalid };
  128. StringBuilder m_value;
  129. StringBuilder m_unit;
  130. HashType m_hash_type { HashType::Unrestricted };
  131. NumberType m_number_type { NumberType::Integer };
  132. Position m_start_position;
  133. Position m_end_position;
  134. };
  135. }