-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.cpp
97 lines (70 loc) · 2.45 KB
/
project.cpp
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
#include "project.h"
#include <QDebug>
#include <QRegularExpression>
#include <QFile>
Project::Project() {
//
keywords = new QMap<QString, Keyword>;
// TODO: DELETE!!
loadProject("../emerald");
}
Project::~Project() {
delete keywords;
}
void Project::loadProject(QString dir) {
//
qDebug() << "loading project from" << dir;
this->root = dir;
loadKeywords();
// TEST
for (auto key : keywords->keys())
qDebug() << key << keywords->value(key).description << keywords->value(key).arguments;
}
QString Project::loadTextFile(QString filepath) {
//
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << QString("Could not open file '%1' for reading.").arg(filepath) + file.errorString();
return QString();
}
QTextStream in(&file);
in.setCodec("UTF-8");
QString text = "";
while (!in.atEnd()) {
text += in.readLine() + "\n";
}
return text;
}
void Project::saveTextFile(QString filepath, QString text) {
QFile file(filepath);
if (file.open(QIODevice::WriteOnly)) {
file.write(text.toUtf8());
} else {
qDebug() << QString("Could not open file '%1' for writing.").arg(filepath) + file.errorString();
}
}
void Project::loadKeywords() {
//
QString events_inc = loadTextFile(root + "/asm/macros/event.inc");
if (events_inc.isEmpty()) return;
QRegularExpression re("[@]{0,1}\\s+(?<desc>.*)\\s+.macro\\s+(?<macro>[A-Za-z0-9_]+)\\s+(?<args>[A-Za-z0-9:,=_@\\\\ ]*)\\s+[A-Za-z0-9.,_\\s\\\\|&\\(\\)><\'\"=]*endm");//
QRegularExpressionMatchIterator iter = re.globalMatch(events_inc);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString name = match.captured("macro");
QString desc = match.captured("desc").remove("@").trimmed();
QString args = match.captured("args");
args.remove(QRegularExpression("@.*"));// remove comments captured in args
QStringList arguments;
QRegularExpression arg_re("(?<arg>[A-Za-z_]+)[:A-Za-z]*");
QRegularExpressionMatchIterator arg_iter = arg_re.globalMatch(args);
while (arg_iter.hasNext()) {
QRegularExpressionMatch arg_match = arg_iter.next();
QString arg = arg_match.captured("arg");
if (arg != "req")
arguments.append(arg);
}
Keyword keyword = {desc, arguments};
keywords->insert(name, keyword);
}
}