1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/Utf8View.h>
#include <LibWeb/CSS/Number.h>
namespace Web::CSS::Parser {
class Token {
friend class Tokenizer;
public:
enum class Type {
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,
};
struct Position {
size_t line { 0 };
size_t column { 0 };
};
Type type() const { return m_type; }
bool is(Type type) const { return m_type == type; }
StringView ident() const
{
VERIFY(m_type == Type::Ident);
return m_value.view();
}
StringView function() const
{
VERIFY(m_type == Type::Function);
return m_value.view();
}
u32 delim() const
{
VERIFY(m_type == Type::Delim);
return *Utf8View(m_value.view()).begin();
}
StringView string() const
{
VERIFY(m_type == Type::String);
return m_value.view();
}
StringView url() const
{
VERIFY(m_type == Type::Url);
return m_value.view();
}
StringView at_keyword() const
{
VERIFY(m_type == Type::AtKeyword);
return m_value.view();
}
HashType hash_type() const
{
VERIFY(m_type == Type::Hash);
return m_hash_type;
}
StringView hash_value() const
{
VERIFY(m_type == Type::Hash);
return m_value.view();
}
Number const& number() const
{
VERIFY(m_type == Type::Number || m_type == Type::Dimension || m_type == Type::Percentage);
return m_number_value;
}
float number_value() const
{
VERIFY(m_type == Type::Number);
return m_number_value.value();
}
i64 to_integer() const
{
VERIFY(m_type == Type::Number && m_number_value.is_integer());
return m_number_value.integer_value();
}
StringView dimension_unit() const
{
VERIFY(m_type == Type::Dimension);
return m_value.view();
}
float dimension_value() const
{
VERIFY(m_type == Type::Dimension);
return m_number_value.value();
}
i64 dimension_value_int() const { return m_number_value.integer_value(); }
float percentage() const
{
VERIFY(m_type == Type::Percentage);
return m_number_value.value();
}
Type mirror_variant() const;
StringView bracket_string() const;
StringView bracket_mirror_string() const;
DeprecatedString to_deprecated_string() const;
DeprecatedString to_debug_string() const;
Position const& start_position() const { return m_start_position; }
Position const& end_position() const { return m_end_position; }
static Token of_string(DeprecatedFlyString str)
{
Token token;
token.m_type = Type::String;
token.m_value = move(str);
return token;
}
static Token create_number(float value)
{
Token token;
token.m_type = Type::Number;
token.m_number_value = Number(Number::Type::Number, value);
return token;
}
static Token create_percentage(float value)
{
Token token;
token.m_type = Type::Percentage;
token.m_number_value = Number(Number::Type::Number, value);
return token;
}
private:
Type m_type { Type::Invalid };
DeprecatedFlyString m_value;
Number m_number_value;
HashType m_hash_type { HashType::Unrestricted };
Position m_start_position;
Position m_end_position;
};
}
|