Token.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020-2021, SerenityOS developers
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. namespace Web::CSS {
  30. class Token {
  31. friend class Parser;
  32. friend class Tokenizer;
  33. public:
  34. enum class TokenType {
  35. Invalid,
  36. EndOfFile,
  37. Ident,
  38. Function,
  39. AtKeyword,
  40. Hash,
  41. String,
  42. BadString,
  43. Url,
  44. BadUrl,
  45. Delim,
  46. Number,
  47. Percentage,
  48. Dimension,
  49. Whitespace,
  50. CDO,
  51. CDC,
  52. Colon,
  53. Semicolon,
  54. Comma,
  55. OpenSquare,
  56. CloseSquare,
  57. OpenParen,
  58. CloseParen,
  59. OpenCurly,
  60. CloseCurly
  61. };
  62. enum class HashType {
  63. Id,
  64. Unrestricted,
  65. };
  66. enum class NumberType {
  67. Integer,
  68. Number,
  69. };
  70. bool is_eof() const { return m_type == TokenType::EndOfFile; }
  71. bool is_whitespace() const { return m_type == TokenType::Whitespace; }
  72. bool is_cdo() const { return m_type == TokenType::CDO; }
  73. bool is_cdc() const { return m_type == TokenType::CDC; }
  74. bool is_at() const { return m_type == TokenType::AtKeyword; }
  75. bool is_semicolon() const { return m_type == TokenType::Semicolon; }
  76. bool is_open_curly() const { return m_type == TokenType::OpenCurly; }
  77. bool is_open_square() const { return m_type == TokenType::OpenSquare; }
  78. bool is_open_paren() const { return m_type == TokenType::OpenParen; }
  79. bool is_close_paren() const { return m_type == TokenType::CloseParen; }
  80. bool is_close_square() const { return m_type == TokenType::CloseSquare; }
  81. bool is_close_curly() const { return m_type == TokenType::CloseCurly; }
  82. bool is_function() const { return m_type == TokenType::Function; }
  83. bool is_colon() const { return m_type == TokenType::Colon; }
  84. bool is_ident() const { return m_type == TokenType::Ident; }
  85. bool is_delim() const { return m_type == TokenType::Delim; }
  86. bool is_comma() const { return m_type == TokenType::Comma; }
  87. TokenType mirror_variant() const;
  88. String bracket_string() const;
  89. String bracket_mirror_string() const;
  90. String to_string() const;
  91. private:
  92. TokenType m_type { TokenType::Invalid };
  93. StringBuilder m_value;
  94. StringBuilder m_unit;
  95. HashType m_hash_type { HashType::Unrestricted };
  96. NumberType m_number_type { NumberType::Integer };
  97. };
  98. }