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
138 lines (106 loc) · 3.24 KB
/
Copy pathtoken.hpp
File metadata and controls
138 lines (106 loc) · 3.24 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
#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.
*/
#pragma once
#include <map>
#include <optional>
#include <string>
#include "data/text/unicode.hpp"
#include "data/text/ustring.hpp"
namespace HTML::Tokenizer {
enum class TokenType { CHARACTER, COMMENT, DOCTYPE, ENDTAG, EOF_TYPE, INVALID, STARTTAG };
// From context.hpp:
class Context;
extern std::map<TokenType, std::string> tokenTypeNames;
inline std::ostream &
operator<<(std::ostream &stream, const TokenType &type) {
return stream << tokenTypeNames[type];
}
struct Token {
private:
TokenType internalType;
public:
inline explicit Token(TokenType inType) : internalType(inType) {
}
virtual ~Token() = default;
inline TokenType
type() {
return internalType;
}
};
// DOCTYPE, start tag, end tag, comment, character, end-of-file
struct DoctypeToken : public Token {
// Initial state is missing
std::optional<Unicode::UString> name;
std::optional<Unicode::UString> publicIdentifier;
std::optional<Unicode::UString> systemIdentifier;
// Initial state is off (= false)
bool forceQuirks = false;
inline DoctypeToken() : Token(TokenType::DOCTYPE) {
}
};
/* Don't use the following, its just for conveniance */
struct AmbiguousTagToken : public Token {
protected:
std::map<Unicode::UString, Unicode::UString> attributes;
public:
Unicode::UString tagName;
bool selfClosing = false;
// For the tokenizer:
Unicode::UString attributeName;
Unicode::UString attributeValue;
inline explicit AmbiguousTagToken(TokenType type) : Token(type) {
}
/**
* The HTML Living Standard ensures that this function wont be called
* when attributeName is empty. It is incorrect behavior if this does
* happen, and a proper Logger::Crash will be triggered if it detects
* this.
*
* This crash is in fact covered by an appropriate test.
*/
void
AddTokenAttribute(Context &);
[[nodiscard]] inline const std::map<Unicode::UString, Unicode::UString> &
GetAttributes() const noexcept {
return attributes;
}
inline void
ClearAttributes() {
attributes.clear();
}
static const AmbiguousTagToken INVALID_TYPE;
};
struct StartTagToken : public AmbiguousTagToken {
inline StartTagToken() : AmbiguousTagToken(TokenType::STARTTAG) {
}
[[nodiscard]] std::optional<Unicode::UString>
GetAttribute(const Unicode::UString &name);
static const StartTagToken INVALID_TYPE_START_TAG;
};
struct EndTagToken : public AmbiguousTagToken {
inline EndTagToken() : AmbiguousTagToken(TokenType::ENDTAG) {
}
static const EndTagToken INVALID_TYPE_END_TAG;
};
struct CommentToken : public Token {
Unicode::UString contents;
inline explicit CommentToken(const Unicode::UString &inContents)
: Token(TokenType::COMMENT), contents(inContents) {
}
static const CommentToken INVALID_TYPE;
};
struct CharacterToken : public Token {
Unicode::CodePoint character;
inline explicit CharacterToken(Unicode::CodePoint character)
: Token(TokenType::CHARACTER), character(character) {
}
};
struct EOFToken : public Token {
inline EOFToken() : Token(TokenType::EOF_TYPE) {
}
};
} // namespace HTML::Tokenizer