Token.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  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. bool is(Type type) const { return m_type == type; }
  52. StringView ident() const
  53. {
  54. VERIFY(m_type == Type::Ident);
  55. return m_value.string_view();
  56. }
  57. StringView delim() const
  58. {
  59. VERIFY(m_type == Type::Delim);
  60. return m_value.string_view();
  61. }
  62. StringView string() const
  63. {
  64. VERIFY(m_type == Type::String);
  65. return m_value.string_view();
  66. }
  67. StringView url() const
  68. {
  69. VERIFY(m_type == Type::Url);
  70. return m_value.string_view();
  71. }
  72. StringView at_keyword() const
  73. {
  74. VERIFY(m_type == Type::AtKeyword);
  75. return m_value.string_view();
  76. }
  77. HashType hash_type() const
  78. {
  79. VERIFY(m_type == Type::Hash);
  80. return m_hash_type;
  81. }
  82. StringView hash_value() const
  83. {
  84. VERIFY(m_type == Type::Hash);
  85. return m_value.string_view();
  86. }
  87. bool is(NumberType number_type) const { return is(Token::Type::Number) && m_number_type == number_type; }
  88. StringView number_string_value() const
  89. {
  90. VERIFY(m_type == Type::Number);
  91. return m_value.string_view();
  92. }
  93. int to_integer() const
  94. {
  95. VERIFY(m_type == Type::Number && m_number_type == NumberType::Integer);
  96. return number_string_value().to_int().value();
  97. }
  98. bool is_integer_value_signed() const { return number_string_value().starts_with('-') || number_string_value().starts_with('+'); }
  99. StringView dimension_unit() const
  100. {
  101. VERIFY(m_type == Type::Dimension);
  102. return m_unit.string_view();
  103. }
  104. StringView dimension_value() const
  105. {
  106. VERIFY(m_type == Type::Dimension);
  107. return m_value.string_view();
  108. }
  109. int dimension_value_int() const { return dimension_value().to_int().value(); }
  110. NumberType number_type() const
  111. {
  112. VERIFY((m_type == Type::Number) || (m_type == Type::Dimension));
  113. return m_number_type;
  114. }
  115. Type mirror_variant() const;
  116. String bracket_string() const;
  117. String bracket_mirror_string() const;
  118. String to_debug_string() const;
  119. private:
  120. Type m_type { Type::Invalid };
  121. StringBuilder m_value;
  122. StringBuilder m_unit;
  123. HashType m_hash_type { HashType::Unrestricted };
  124. NumberType m_number_type { NumberType::Integer };
  125. };
  126. }