-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPSToken.cpp
More file actions
275 lines (227 loc) · 7.32 KB
/
MPSToken.cpp
File metadata and controls
275 lines (227 loc) · 7.32 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* This file is part of MemphisNow.
*
* MemphisNow is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MemphisNow is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MemphisNow. If not, see <https://www.gnu.org/licenses/>.
*/
#include "MPSToken.hpp"
#include "MPSCommon.hpp"
MPSToken::MPSToken( MPSToken* parent,
const std::wstring& text,
const std::wstring& separators,
bool discard) :
m_parent(parent),
m_text(text),
m_separators(separators),
m_discard(discard)
{}
MPSToken::~MPSToken ()
{
cleanup_token(*this);
}
void MPSToken::set_parent (MPSToken* parent)
{
if (!parent)
return;
if (parent->is_subtoken(this))
m_parent = parent;
}
void MPSToken::shift_subtoken (MPSToken* sub_token, EMPSDirection direction)
{
if (!sub_token)
return;
if (m_subtokens.empty())
return;
MPSTokensContainer::iterator iter = m_subtokens.begin();
for ( ; iter != m_subtokens.end(); ++iter) {
if (*iter == sub_token) {
if (ELeft == direction) {
if (iter == m_subtokens.begin())
return; // shift left requested on first subtoken, do nothing
MPSTokensContainer::iterator left = iter;
--left;
std::swap (*left, *iter);
return;
} else if (ERight == direction) {
MPSTokensContainer::iterator right = iter;
++right;
if (right == m_subtokens.end())
return; // shift right requested on last subtoken, do nothing
std::swap (*iter, *right);
return;
}
}
}
}
void MPSToken::split()
{
if (m_separators.empty())
return;
BoostSeparator separ (m_separators.c_str());
BoostTokenizer tokenizer (m_text, separ);
cleanup_token (*this);
// does it have only one token?
BoostTokenizer::iterator iter = tokenizer.begin();
for ( ; iter != tokenizer.end (); ) {
bool equals = (m_text == std::wstring (*iter));
++iter;
if (iter == tokenizer.end() && equals) {
return; // no subtokens, bail out
} else {
break; // has more than one token, add them
}
}
iter = tokenizer.begin();
int count = 0;
for ( ; iter != tokenizer.end(); ++iter) {
// create sub-tokens
boost::shared_ptr<MPSToken> subToken (new MPSToken(this, *iter, L"", m_discard));
m_subtokens_shared.push_back(subToken);
MPSToken* sub_token = new MPSToken (this, *iter, L"", m_discard);
m_subtokens.push_back(sub_token);
++count;
}
}
void MPSToken::clear_subtokens ()
{
cleanup_token (*this);
}
void MPSToken::insert_subtoken (const std::wstring& text, size_t pos)
{
if (pos == KLastSubtokenPosition) {
// add on last position
m_subtokens.push_back(new MPSToken (this, text));
} else {
size_t idx = 0;
MPSTokensContainer::iterator iter = m_subtokens.begin();
for ( ; (iter != m_subtokens.end()) && (idx != pos); ++iter, ++idx);
if (iter == m_subtokens.end())
m_subtokens.push_back(new MPSToken (this, text));
else
m_subtokens.insert(iter, new MPSToken (this, text));
}
}
void MPSToken::set_text (const std::wstring& text)
{
m_text = text;
}
void MPSToken::set_separators (const std::wstring& separ)
{
m_separators = separ;
if (m_separators.empty()) {
cleanup_token(*this);
}
}
void MPSToken::set_discard (bool discard)
{
discard_token(*this, discard);
}
void MPSToken::cleanup_token (MPSToken& token)
{
if (!token.m_subtokens.empty()) {
MPSTokensContainer::iterator iter = token.m_subtokens.begin();
// we have some sub-tokens, need to delete them
for ( ; iter != token.m_subtokens.end(); ++iter) {
if (*iter != 0) {
cleanup_token (*(*iter));
delete (*iter);
*iter = 0;
}
}
token.m_subtokens.clear();
}
}
void MPSToken::discard_token (MPSToken& token, bool discard)
{
token.m_discard = discard;
MPSTokensContainer::iterator iter = token.sub_tokens_begin();
for ( ; iter != token.sub_tokens_end(); ++iter) {
if (*iter != 0)
discard_token(*(*iter), discard);
}
}
bool MPSToken::is_subtoken (const MPSToken* token) const
{
MPSTokensContainer::const_iterator pos;
pos = std::find(m_subtokens.begin(), m_subtokens.end(), token);
return (pos != m_subtokens.end());
}
const MPSToken* MPSToken::last_subtoken () const
{
if (m_subtokens.empty())
return 0;
const MPSToken* last = 0;
MPSTokensContainer::const_iterator iter = m_subtokens.begin();
for ( ; iter != m_subtokens.end();) {
last = (*iter);
++iter;
}
return last;
}
const MPSToken* MPSToken::find_first_leaf_subtoken (bool include_discarded) const
{
return find_first_leaf_subtoken(this, include_discarded);
}
const MPSToken* MPSToken::find_last_leaf_subtoken (bool include_discarded) const
{
return find_last_leaf_subtoken(this, include_discarded);
}
const MPSToken* MPSToken::find_last_leaf_subtoken (const MPSToken* token, bool include_discarded) const
{
if (!token)
return 0;
if (token->sub_tokens_empty())
return token;
const MPSToken* last = 0;
MPSTokensContainer::const_iterator iter = token->subtokens_const_begin();
for ( ; iter != token->subtokens_const_end(); ) {
// find only tokens which are not discarded
bool discard_cond = (include_discarded) || (!include_discarded && !(*iter)->is_discard());
if (*iter != 0 && discard_cond) {
last = *iter;
}
++iter;
// was it the last subttoken?
if (iter == token->subtokens_const_end()) {
if (last != 0 && !last->sub_tokens_empty()) {
last = find_last_leaf_subtoken(last, include_discarded);
}
}
}
return last;
}
const MPSToken* MPSToken::find_first_leaf_subtoken (const MPSToken* token, bool include_discarded) const
{
if (!token)
return 0;
if (token->sub_tokens_empty())
return token;
const MPSToken* first = 0;
// find first token which is not discarded
MPSTokensContainer::const_iterator iter = token->subtokens_const_begin();
for ( ; iter != token->subtokens_const_end(); ++ iter) {
bool discard_cond = (include_discarded) || (!include_discarded && !(*iter)->is_discard());
if (*iter && discard_cond) {
first = *iter;
break;
}
}
// if it has subtokens, find first undiscarded token in that group
if (iter != token->subtokens_const_end() &&
*iter &&
!(*iter)->sub_tokens_empty())
{
first = find_first_leaf_subtoken(*iter, include_discarded);
}
return first;
}