blob: 7cbc4f2e84c5eadc3ce09f2bb123c1ee120f64ad (
plain)
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
|
/*
* 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/Format.h>
#include <AK/Vector.h>
#include <LibWeb/CSS/Parser/ComponentValue.h>
#include <LibWeb/CSS/Parser/Tokenizer.h>
namespace Web::CSS::Parser {
template<typename T>
class TokenStream {
public:
class StateTransaction {
public:
explicit StateTransaction(TokenStream<T>& token_stream)
: m_token_stream(token_stream)
, m_saved_iterator_offset(token_stream.m_iterator_offset)
{
}
~StateTransaction()
{
if (!m_commit)
m_token_stream.m_iterator_offset = m_saved_iterator_offset;
}
StateTransaction create_child() { return StateTransaction(*this); }
void commit()
{
m_commit = true;
if (m_parent)
m_parent->commit();
}
private:
explicit StateTransaction(StateTransaction& parent)
: m_parent(&parent)
, m_token_stream(parent.m_token_stream)
, m_saved_iterator_offset(parent.m_token_stream.m_iterator_offset)
{
}
StateTransaction* m_parent { nullptr };
TokenStream<T>& m_token_stream;
int m_saved_iterator_offset { 0 };
bool m_commit { false };
};
explicit TokenStream(Vector<T> const& tokens)
: m_tokens(tokens)
, m_eof(make_eof())
{
}
TokenStream(TokenStream<T> const&) = delete;
TokenStream(TokenStream<T>&&) = default;
bool has_next_token()
{
return (size_t)(m_iterator_offset + 1) < m_tokens.size();
}
T const& next_token()
{
if (!has_next_token())
return m_eof;
++m_iterator_offset;
return m_tokens.at(m_iterator_offset);
}
T const& peek_token(int offset = 0)
{
if (!has_next_token())
return m_eof;
return m_tokens.at(m_iterator_offset + offset + 1);
}
T const& current_token()
{
if ((size_t)m_iterator_offset >= m_tokens.size())
return m_eof;
return m_tokens.at(m_iterator_offset);
}
void reconsume_current_input_token()
{
if (m_iterator_offset >= 0)
--m_iterator_offset;
}
StateTransaction begin_transaction() { return StateTransaction(*this); }
void skip_whitespace()
{
while (peek_token().is(Token::Type::Whitespace))
next_token();
}
size_t token_count() const { return m_tokens.size(); }
size_t remaining_token_count() const { return token_count() - m_iterator_offset - 1; }
void dump_all_tokens()
{
dbgln("Dumping all tokens:");
for (size_t i = 0; i < m_tokens.size(); ++i) {
auto& token = m_tokens[i];
if ((i - 1) == (size_t)m_iterator_offset)
dbgln("-> {}", token.to_debug_string());
else
dbgln(" {}", token.to_debug_string());
}
}
void copy_state(Badge<Parser>, TokenStream<T> const& other)
{
m_iterator_offset = other.m_iterator_offset;
}
private:
Vector<T> const& m_tokens;
int m_iterator_offset { -1 };
T make_eof()
{
if constexpr (IsSame<T, Token>) {
return Tokenizer::create_eof_token();
}
if constexpr (IsSame<T, ComponentValue>) {
return ComponentValue(Tokenizer::create_eof_token());
}
}
T m_eof;
};
}
|