-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserUtils.cpp
More file actions
193 lines (154 loc) · 3.71 KB
/
Copy pathparserUtils.cpp
File metadata and controls
193 lines (154 loc) · 3.71 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
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
190
191
192
193
#include "parserUtils.h"
ParserUtils::ParserUtils(ILexerPtr lexer)
: m_lexer(lexer)
{
}
const Token &ParserUtils::token() const
{
return m_currentToken;
}
bool ParserUtils::next()
{
if (isStopSequenceReached())
return false;
if (isMaxLengthReached())
return false;
if (!m_nextToken.isUnknown())
{
m_currentToken = m_nextToken;
m_nextToken.clear();
handleStopSequence();
handleMaxLength(m_currentToken);
return true;
}
m_currentToken = m_lexer->getToken();
if (m_currentToken.isUnknown())
return false;
handleMaxLength(m_currentToken);
if (m_mergeStringSequence && !isMaxLengthReached())
{
// Merge a sequence of string tokens into one
if (m_currentToken.type == TokenType::string)
{
while (true)
{
auto token = m_lexer->getToken();
if (token.type != TokenType::string)
{
m_nextToken = token;
break;
}
m_currentToken.value.end = token.value.end;
handleMaxLength(token);
if (isMaxLengthReached())
break;
}
}
}
handleStopSequence();
return true;
}
void ParserUtils::setMergeStringSequence(bool value)
{
m_mergeStringSequence = value;
}
bool ParserUtils::expected(TokenType type)
{
if (!next())
return false;
if (token().type != type)
return false;
return true;
}
bool ParserUtils::expected(std::initializer_list<TokenType> tokens)
{
for (TokenType type : tokens)
{
if (!expected(type))
return false;
}
return true;
}
bool ParserUtils::skipAndStopAfterSequence(std::initializer_list<TokenType> tokens)
{
if (tokens.begin() == tokens.end())
return false;
auto currentMatchTokenType = tokens.begin();
auto matchToken = [&] (const Token &token) {
if (token.type == *currentMatchTokenType)
{
currentMatchTokenType++;
return true;
}
return false;
};
while (next())
{
if (!matchToken(token()))
{
currentMatchTokenType = tokens.begin();
matchToken(token());
}
if (currentMatchTokenType == tokens.end())
return true;
}
return false;
}
void ParserUtils::setStopSequence(const std::vector<TokenType> &tokens)
{
m_stopSequence = tokens;
clearStopSequenceReachedFlag();
}
bool ParserUtils::isStopSequenceReached() const
{
return (!m_stopSequence.empty()
&& m_stopSequenceMatchScore == m_stopSequence.size());
}
void ParserUtils::clearStopSequenceReachedFlag()
{
m_stopSequenceMatchScore = 0;
}
void ParserUtils::setMaxLength(std::size_t bytes)
{
m_maxLength = bytes;
m_checkLength = true;
}
void ParserUtils::disableMaxLengthCheck()
{
m_maxLength = 0;
m_checkLength = false;
}
bool ParserUtils::isMaxLengthReached() const
{
return m_checkLength && m_maxLength == 0;
}
bool ParserUtils::matchStopSequence()
{
if (m_currentToken.type == m_stopSequence[m_stopSequenceMatchScore])
{
m_stopSequenceMatchScore++;
return true;
}
return false;
}
void ParserUtils::handleStopSequence()
{
if (!m_stopSequence.empty())
{
if (!matchStopSequence())
{
m_stopSequenceMatchScore = 0;
matchStopSequence();
}
}
}
void ParserUtils::handleMaxLength(const Token &token)
{
if (!m_checkLength)
return;
std::size_t tokenLength = token.length();
if (tokenLength > m_maxLength)
m_maxLength = 0;
else
m_maxLength -= tokenLength;
}