-
Notifications
You must be signed in to change notification settings - Fork 3
/
BG_Analysis.h
348 lines (309 loc) · 7.23 KB
/
BG_Analysis.h
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once
#include <weh.h>
#include "QThread"
#include "ParseData.h"
#include <QMutex>
#include <QSet>
#include <atomic>
#include <algorithm>
//#include "ParserHelper/parser/parser.h"
#include "ParserHelper/Bagel/Bagel_Include.h"
#include "ScopePointer.h"
using std::atomic_bool;
class BkeScintilla;
//变量在所有脚本中穿透还是只在宏文件中穿透
#define VAR_CROSS_ALL true
struct BKEMacros
{
QString name;
QString definefile;
QString comment;
int32_t pos;
vector<pair<QString, QString>> paramqueue;
};
/// <summary>
/// Analysis Project Background
/// </summary>
class BG_Analysis :
public QThread
{
Q_OBJECT
public:
BG_Analysis(const QString &p);
~BG_Analysis();
QString pdir;
virtual void run() override;
private:
volatile enum
{
STATE_IDLE,
STATE_PARSEFILE,
STATE_PARSEMACRO,
}cur_state;
Bagel_Handler<Bagel_Closure> backup_topclo;
Bagel_Handler<Bagel_Closure> topclo;
/// <summary>
/// Global macro data.
/// </summary>
QHash<QString, BKEMacros> macrodata;
/// <summary>
/// The backup_macrodata, updated by sub thread
/// </summary>
QHash<QString, BKEMacros> backup_macrodata;
/// <summary>
/// The macrofiles.
/// </summary>
QStringList macrofiles;
/// <summary>
/// The backup_macrofiles.
/// </summary>
QStringList backup_macrofiles;
/// <summary>
/// Whether macro has changed.
/// </summary>
atomic_bool newmacrofile;
/// <summary>
/// Internal analysis data.
/// </summary>
QHash<QString, ParseData*> data;
/// <summary>
/// File Buffer
/// </summary>
QHash<QString, QByteArray> filebuf;
/// <summary>
/// File which is being analysised.
/// </summary>
QString curfile;
/// <summary>
/// Files to be analysised.
/// </summary>
QStringList listfile;
/// <summary>
/// The mutex to lock message.
/// </summary>
QMutex msgmutex;
/// <summary>
/// The mutex to lock analysis.
/// </summary>
QMutex analysis_mutex;
/// <summary>
/// Whether stop the thread.
/// </summary>
atomic_bool stop;
/// <summary>
/// Whether cancel the current analysising.
/// </summary>
atomic_bool cancel;
/// <summary>
/// Searches the full relativ name by its filename.
/// </summary>
/// <param name="file">The file.</param>
/// <returns></returns>
QString searchFile(QString file)
{
if (file.isEmpty())
return QString();
if (file[0] == '\"' && file.length() > 1 && file[file.length() - 1] == '\"')
file = file.mid(1, file.length() - 2);
if (!file.endsWith(".bkscr"))
file += ".bkscr";
for (auto it = data.begin(); it != data.end(); it++)
{
if (it.key().endsWith(file))
return it.key();
}
if (curfile.endsWith(file))
return curfile;
for (auto &it : listfile)
{
if (it.endsWith(file))
return it;
}
return QString();
}
void parseMacro(const QString &file, bool regardAsNormalScripts = false);
void notifyExit();
public:
/// <summary>
/// Adds the file to analysis immediately.
/// If the file is analysising, abort it and analysis immediately.
/// </summary>
/// <param name="file">The file.</param>
void pushFile(const QString &file, const QByteArray *buffer = nullptr)
{
msgmutex.lock();
if (curfile == file)
{
cancel = true;
}
//deleted in child-thread
//auto p = data[file];
//delete p;
//data[file] = NULL;
listfile.push_front(file);
if (buffer)
{
//update buffer
filebuf[file] = *buffer;
}
if (VAR_CROSS_ALL || file == "macro.bkscr" || std::find(macrofiles.begin(), macrofiles.end(), file) != macrofiles.end())
{
newmacrofile = true;
if (cur_state == STATE_PARSEMACRO)
cancel = true;
}
msgmutex.unlock();
}
/// <summary>
/// Adds files to analysis.
/// If one file is analysising, abort it and analysis later.
/// </summary>
/// <param name="files">Files.</param>
void addFiles(const QStringList &files)
{
msgmutex.lock();
listfile.append(files);
for (auto & file : files)
{
if (VAR_CROSS_ALL || file == "macro.bkscr" || std::find(macrofiles.begin(), macrofiles.end(), file) != macrofiles.end())
{
newmacrofile = true;
if (cur_state == STATE_PARSEMACRO)
cancel = true;
break;
}
}
msgmutex.unlock();
}
/// <summary>
/// Check given file
/// </summary>
/// <param name="file">File.</param>
bool isMacroFile(const QString &file)
{
QMutexLocker ml(&msgmutex);
return macrofiles.contains(file);
}
/// <summary>
/// Inform a file inserts the chars.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="p">The p.</param>
/// <param name="offset">The offset.</param>
void insertChars(const QString &file, Pos p, Pos offset);
/// <summary>
/// Inform a file deletes the chars.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="p">The p.</param>
/// <param name="offset">The offset.</param>
void deleteChars(const QString &file, Pos p, Pos offset);
/// <summary>
/// Finds the label.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="label">The label.</param>
/// <returns>pos</returns>
int findLabel(const QString &file, const QString &label)
{
int n = -1;
msgmutex.lock();
auto d = data[file];
if (d)
{
n = d->findLabel(label);
}
msgmutex.unlock();
return n;
}
/// <summary>
/// Finds the node.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="p">position.</param>
/// <returns>Nodes finded.</returns>
ScopePointer<BaseNode> findNode(const QString &file, int p)
{
BaseNode *n = NULL;
msgmutex.lock();
auto d = data[file];
if (d)
{
n = d->findNode(p);
}
return ScopePointer<BaseNode>(n, [this](BaseNode *) {msgmutex.unlock(); });
}
/// <summary>
/// Finds the last label node.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="p">position.</param>
/// <returns>Last label nodes finded.</returns>
ScopePointer<BaseNode> findLastLabelNode(const QString &file, int p)
{
BaseNode *n = NULL;
msgmutex.lock();
auto d = data[file];
if (d)
{
n = d->findLastLabelNode(p);
}
return ScopePointer<BaseNode>(n, [this](BaseNode *) {msgmutex.unlock(); });
}
/// <summary>
/// Gets the labels.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="l">Lables.</param>
/// <returns>whether success</returns>
bool getLabels(const QString &file, QSortedSet<QString> &l)
{
bool res = false;
msgmutex.lock();
auto d = data[file];
if (d)
{
res = true;
d->getLabels(l);
}
msgmutex.unlock();
return res;
}
/// <summary>
/// whether the file is OK, if OK, lockFile.
/// </summary>
/// <param name="file">The file.</param>
/// <returns></returns>
ScopePointer<ParseData> lockFile(const QString &file)
{
msgmutex.lock();
ScopePointer<ParseData> res(data[file], [this](ParseData *) {msgmutex.unlock(); });
return std::move(res);
}
/// <summary>
/// Unlocks the file.
/// </summary>
/// <param name="file">The file.</param>
//void unlockFile()
//{
// msgmutex.unlock();
//}
/// <summary>
/// Gets all the commands and macros.
/// </summary>
/// <returns></returns>
QList<QPair<QString, int>> getCmdList();
bool findMacro(const QString &name, BKEMacros *m)
{
msgmutex.lock();
auto it = macrodata.find(name);
if (it == macrodata.end())
{
msgmutex.unlock();
return false;
}
*m = it.value();
msgmutex.unlock();
return true;
}
};