This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.hpp
More file actions
142 lines (117 loc) · 3.13 KB
/
Copy pathtoken.hpp
File metadata and controls
142 lines (117 loc) · 3.13 KB
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
#pragma once
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "data/text/unicode.hpp"
#include <type_traits>
#include <variant>
namespace CSS {
using IntegerType = std::int64_t;
using NumberType = double;
enum class TokenHashType {
ID,
UNRESTRICTED
};
enum class TokenNumericType {
INTEGER,
NUMBER
};
struct TokenCodePointsData {
std::vector<Unicode::CodePoint> codePoints;
};
struct TokenHashData : public TokenCodePointsData {
TokenHashType type { TokenHashType::UNRESTRICTED };
};
struct TokenNumericData {
TokenNumericType type { TokenNumericType::INTEGER };
union {
IntegerType integer;
NumberType number;
};
};
struct TokenDimensionData : public TokenNumericData, public TokenCodePointsData {
};
struct Token {
enum class Type {
IDENT, // <ident-token>
FUNCTION, // <function-token>
AT_KEYWORD, // <at-keyword-token>
HASH, // <hash-token>
STRING, // <string-token>
BAD_STRING, //<bad-string-token>
URL, // <url-token>
BAD_URL, // <bad-url-token>
DELIM, // <delim-token>
NUMBER, // <number-token>
PERCENTAGE, //<percentage-token>
DIMENSION, // <dimension-token>
WHITESPACE, // <whitespace-token>
CDO, // <CDO-token>
CDC, // <CDC-token>
COLON, // <colon-token>
SEMICOLON, // <semicolon-token>
COMMA, // <comma-token>
SQUARE_OPEN, // <[-token>
SQUARE_CLOSE, // <]-token>
PAREN_OPEN, // <(-token>
PAREN_CLOSE, // <)-token>
CURLY_OPEN, // <{-token>
CURLY_CLOSE // <}-token>
};
Type type;
std::variant<
// For any other type:
std::nullptr_t,
// For:
// - <ident-token>
// - <function-token>
// - <at-keyword-token>
// - <string-token>
// - <url-token>
TokenCodePointsData,
// For <hash-token>:
TokenHashData,
// For <delim-token>:
Unicode::CodePoint,
// For:
// - <number-token>
// - <percentage-token>
TokenNumericData,
// For <dimension-token>:
TokenDimensionData> data;
template<typename T>
inline Token(Type type, T data) noexcept
: type(type), data(data) {
}
inline explicit Token(Type type) noexcept
: type(type), data(nullptr) {
}
inline explicit Token(Unicode::CodePoint codePoint) noexcept
: type(Type::DELIM), data(codePoint) {
}
};
template<Token::Type type> Token
MakeToken() noexcept {
switch (type) {
case Token::Type::IDENT:
case Token::Type::FUNCTION:
case Token::Type::AT_KEYWORD:
case Token::Type::STRING:
case Token::Type::URL:
return Token { type, TokenCodePointsData() };
case Token::Type::HASH:
return Token { type, TokenHashData() };
case Token::Type::DELIM:
return Token { type, Unicode::REPLACEMENT_CHARACTER };
case Token::Type::NUMBER:
case Token::Type::PERCENTAGE:
return Token { type, TokenNumericData() };
case Token::Type::DIMENSION:
return Token { type, TokenDimensionData() };
default:
return Token { type, nullptr };
}
}
} // namespace CSS