Token.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. bool is(NumberType number_type) const { return is(Token::Type::Number) && m_number_type == number_type; }
  73. int integer() const
  74. {
  75. VERIFY(m_type == Type::Number && m_number_type == NumberType::Integer);
  76. return m_value.string_view().to_int().value();
  77. }
  78. Type mirror_variant() const;
  79. String bracket_string() const;
  80. String bracket_mirror_string() const;
  81. String to_debug_string() const;
  82. private:
  83. Type m_type { Type::Invalid };
  84. StringBuilder m_value;
  85. StringBuilder m_unit;
  86. HashType m_hash_type { HashType::Unrestricted };
  87. NumberType m_number_type { NumberType::Integer };
  88. };
  89. }