Token.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. namespace Web::CSS {
  10. class Token {
  11. friend class Parser;
  12. friend class Tokenizer;
  13. public:
  14. enum class TokenType {
  15. Invalid,
  16. EndOfFile,
  17. Ident,
  18. Function,
  19. AtKeyword,
  20. Hash,
  21. String,
  22. BadString,
  23. Url,
  24. BadUrl,
  25. Delim,
  26. Number,
  27. Percentage,
  28. Dimension,
  29. Whitespace,
  30. CDO,
  31. CDC,
  32. Colon,
  33. Semicolon,
  34. Comma,
  35. OpenSquare,
  36. CloseSquare,
  37. OpenParen,
  38. CloseParen,
  39. OpenCurly,
  40. CloseCurly
  41. };
  42. enum class HashType {
  43. Id,
  44. Unrestricted,
  45. };
  46. enum class NumberType {
  47. Integer,
  48. Number,
  49. };
  50. bool is_eof() const { return m_type == TokenType::EndOfFile; }
  51. bool is_whitespace() const { return m_type == TokenType::Whitespace; }
  52. bool is_cdo() const { return m_type == TokenType::CDO; }
  53. bool is_cdc() const { return m_type == TokenType::CDC; }
  54. bool is_at() const { return m_type == TokenType::AtKeyword; }
  55. bool is_semicolon() const { return m_type == TokenType::Semicolon; }
  56. bool is_open_curly() const { return m_type == TokenType::OpenCurly; }
  57. bool is_open_square() const { return m_type == TokenType::OpenSquare; }
  58. bool is_open_paren() const { return m_type == TokenType::OpenParen; }
  59. bool is_close_paren() const { return m_type == TokenType::CloseParen; }
  60. bool is_close_square() const { return m_type == TokenType::CloseSquare; }
  61. bool is_close_curly() const { return m_type == TokenType::CloseCurly; }
  62. bool is_function() const { return m_type == TokenType::Function; }
  63. bool is_colon() const { return m_type == TokenType::Colon; }
  64. bool is_ident() const { return m_type == TokenType::Ident; }
  65. bool is_delim() const { return m_type == TokenType::Delim; }
  66. bool is_comma() const { return m_type == TokenType::Comma; }
  67. TokenType mirror_variant() const;
  68. String bracket_string() const;
  69. String bracket_mirror_string() const;
  70. String to_string() const;
  71. private:
  72. TokenType m_type { TokenType::Invalid };
  73. StringBuilder m_value;
  74. StringBuilder m_unit;
  75. HashType m_hash_type { HashType::Unrestricted };
  76. NumberType m_number_type { NumberType::Integer };
  77. };
  78. }