| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-28 22:46:44 +02:00
										 |  |  |  * Copyright (c) 2020-2021, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2023-02-11 15:54:53 +00:00
										 |  |  |  * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-13 13:57:40 +00:00
										 |  |  | #include <AK/FlyString.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  | #include <LibWeb/CSS/Number.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 15:10:08 +01:00
										 |  |  | namespace Web::CSS::Parser { | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Token { | 
					
						
							|  |  |  |     friend class Tokenizer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-07-09 20:54:06 +01:00
										 |  |  |     enum class Type { | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  |         Invalid, | 
					
						
							|  |  |  |         EndOfFile, | 
					
						
							|  |  |  |         Ident, | 
					
						
							|  |  |  |         Function, | 
					
						
							|  |  |  |         AtKeyword, | 
					
						
							|  |  |  |         Hash, | 
					
						
							|  |  |  |         String, | 
					
						
							|  |  |  |         BadString, | 
					
						
							|  |  |  |         Url, | 
					
						
							|  |  |  |         BadUrl, | 
					
						
							|  |  |  |         Delim, | 
					
						
							|  |  |  |         Number, | 
					
						
							|  |  |  |         Percentage, | 
					
						
							|  |  |  |         Dimension, | 
					
						
							|  |  |  |         Whitespace, | 
					
						
							|  |  |  |         CDO, | 
					
						
							|  |  |  |         CDC, | 
					
						
							|  |  |  |         Colon, | 
					
						
							|  |  |  |         Semicolon, | 
					
						
							|  |  |  |         Comma, | 
					
						
							|  |  |  |         OpenSquare, | 
					
						
							|  |  |  |         CloseSquare, | 
					
						
							|  |  |  |         OpenParen, | 
					
						
							|  |  |  |         CloseParen, | 
					
						
							|  |  |  |         OpenCurly, | 
					
						
							|  |  |  |         CloseCurly | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class HashType { | 
					
						
							|  |  |  |         Id, | 
					
						
							|  |  |  |         Unrestricted, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 21:25:14 +01:00
										 |  |  |     struct Position { | 
					
						
							|  |  |  |         size_t line { 0 }; | 
					
						
							|  |  |  |         size_t column { 0 }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Type type() const { return m_type; } | 
					
						
							| 
									
										
										
										
											2021-07-09 20:54:06 +01:00
										 |  |  |     bool is(Type type) const { return m_type == type; } | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-07 00:22:33 +13:00
										 |  |  |     FlyString const& ident() const | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-07-03 15:06:53 +01:00
										 |  |  |         VERIFY(m_type == Type::Ident); | 
					
						
							| 
									
										
										
										
											2023-11-07 00:22:33 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |     FlyString const& function() const | 
					
						
							| 
									
										
										
										
											2021-11-18 12:04:05 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Function); | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-11-18 12:04:05 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:34:08 +01:00
										 |  |  |     u32 delim() const | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-07-03 15:06:53 +01:00
										 |  |  |         VERIFY(m_type == Type::Delim); | 
					
						
							| 
									
										
										
										
											2023-12-09 09:16:55 +13:00
										 |  |  |         return *m_value.code_points().begin(); | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |     FlyString const& string() const | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-07-03 15:06:53 +01:00
										 |  |  |         VERIFY(m_type == Type::String); | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-07-01 14:52:12 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |     FlyString const& url() const | 
					
						
							| 
									
										
										
										
											2021-07-22 13:00:29 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Url); | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-07-22 13:00:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |     FlyString const& at_keyword() const | 
					
						
							| 
									
										
										
										
											2021-07-22 17:51:07 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::AtKeyword); | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-07-22 17:51:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:13:07 +01:00
										 |  |  |     HashType hash_type() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Hash); | 
					
						
							|  |  |  |         return m_hash_type; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |     FlyString const& hash_value() const | 
					
						
							| 
									
										
										
										
											2021-07-23 16:13:07 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Hash); | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:13:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |     Number const& number() const | 
					
						
							| 
									
										
										
										
											2021-07-24 21:22:44 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |         VERIFY(m_type == Type::Number || m_type == Type::Dimension || m_type == Type::Percentage); | 
					
						
							|  |  |  |         return m_number_value; | 
					
						
							| 
									
										
										
										
											2021-07-24 21:22:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-20 12:41:13 +01:00
										 |  |  |     double number_value() const | 
					
						
							| 
									
										
										
										
											2021-11-18 11:46:27 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Number); | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |         return m_number_value.value(); | 
					
						
							| 
									
										
										
										
											2021-11-18 11:46:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-19 16:43:56 +01:00
										 |  |  |     i64 to_integer() const | 
					
						
							| 
									
										
										
										
											2021-07-09 21:04:34 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |         VERIFY(m_type == Type::Number && m_number_value.is_integer()); | 
					
						
							|  |  |  |         return m_number_value.integer_value(); | 
					
						
							| 
									
										
										
										
											2021-07-24 21:22:44 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |     FlyString const& dimension_unit() const | 
					
						
							| 
									
										
										
										
											2021-07-24 21:22:44 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Dimension); | 
					
						
							| 
									
										
										
										
											2023-11-26 10:40:08 +13:00
										 |  |  |         return m_value; | 
					
						
							| 
									
										
										
										
											2021-07-24 21:22:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-20 12:41:13 +01:00
										 |  |  |     double dimension_value() const | 
					
						
							| 
									
										
										
										
											2021-07-24 21:22:44 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Dimension); | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |         return m_number_value.value(); | 
					
						
							| 
									
										
										
										
											2021-11-18 11:46:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |     i64 dimension_value_int() const { return m_number_value.integer_value(); } | 
					
						
							| 
									
										
										
										
											2021-11-18 11:46:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 12:41:13 +01:00
										 |  |  |     double percentage() const | 
					
						
							| 
									
										
										
										
											2021-11-18 11:46:27 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(m_type == Type::Percentage); | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |         return m_number_value.value(); | 
					
						
							| 
									
										
										
										
											2021-07-09 21:04:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-09 20:54:06 +01:00
										 |  |  |     Type mirror_variant() const; | 
					
						
							| 
									
										
										
										
											2023-02-11 15:54:53 +00:00
										 |  |  |     StringView bracket_string() const; | 
					
						
							|  |  |  |     StringView bracket_mirror_string() const; | 
					
						
							| 
									
										
										
										
											2021-07-07 21:29:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 13:00:28 +01:00
										 |  |  |     String to_string() const; | 
					
						
							|  |  |  |     String to_debug_string() const; | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 15:25:20 +00:00
										 |  |  |     String const& representation() const { return m_representation; } | 
					
						
							| 
									
										
										
										
											2021-10-21 21:25:14 +01:00
										 |  |  |     Position const& start_position() const { return m_start_position; } | 
					
						
							|  |  |  |     Position const& end_position() const { return m_end_position; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 14:29:09 +01:00
										 |  |  |     static Token create_string(FlyString str) | 
					
						
							| 
									
										
										
										
											2022-03-29 23:33:50 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         Token token; | 
					
						
							|  |  |  |         token.m_type = Type::String; | 
					
						
							|  |  |  |         token.m_value = move(str); | 
					
						
							|  |  |  |         return token; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 12:41:13 +01:00
										 |  |  |     static Token create_number(double value) | 
					
						
							| 
									
										
										
										
											2022-11-02 20:24:44 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         Token token; | 
					
						
							|  |  |  |         token.m_type = Type::Number; | 
					
						
							|  |  |  |         token.m_number_value = Number(Number::Type::Number, value); | 
					
						
							|  |  |  |         return token; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 12:41:13 +01:00
										 |  |  |     static Token create_percentage(double value) | 
					
						
							| 
									
										
										
										
											2022-11-02 20:24:44 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         Token token; | 
					
						
							|  |  |  |         token.m_type = Type::Percentage; | 
					
						
							|  |  |  |         token.m_number_value = Number(Number::Type::Number, value); | 
					
						
							|  |  |  |         return token; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 16:46:17 +01:00
										 |  |  |     static Token create_dimension(double value, FlyString unit) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Token token; | 
					
						
							|  |  |  |         token.m_type = Type::Dimension; | 
					
						
							|  |  |  |         token.m_number_value = Number(Number::Type::Number, value); | 
					
						
							|  |  |  |         token.m_value = move(unit); | 
					
						
							|  |  |  |         return token; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 14:29:09 +01:00
										 |  |  |     static Token create_ident(FlyString ident) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Token token; | 
					
						
							|  |  |  |         token.m_type = Type::Ident; | 
					
						
							|  |  |  |         token.m_value = move(ident); | 
					
						
							|  |  |  |         return token; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Token create_url(FlyString url) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Token token; | 
					
						
							|  |  |  |         token.m_type = Type::Url; | 
					
						
							|  |  |  |         token.m_value = move(url); | 
					
						
							|  |  |  |         return token; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-07-09 20:54:06 +01:00
										 |  |  |     Type m_type { Type::Invalid }; | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-13 13:57:40 +00:00
										 |  |  |     FlyString m_value; | 
					
						
							| 
									
										
										
										
											2022-03-21 21:01:27 +00:00
										 |  |  |     Number m_number_value; | 
					
						
							| 
									
										
										
										
											2022-03-21 17:22:05 +00:00
										 |  |  |     HashType m_hash_type { HashType::Unrestricted }; | 
					
						
							| 
									
										
										
										
											2021-10-21 21:25:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 15:25:20 +00:00
										 |  |  |     String m_representation; | 
					
						
							| 
									
										
										
										
											2021-10-21 21:25:14 +01:00
										 |  |  |     Position m_start_position; | 
					
						
							|  |  |  |     Position m_end_position; | 
					
						
							| 
									
										
										
										
											2021-03-09 17:18:08 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |