-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.cpp
More file actions
101 lines (76 loc) · 2.23 KB
/
parser.cpp
File metadata and controls
101 lines (76 loc) · 2.23 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
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFileInfo>
#include <fstream>
#include <iostream>
#include <sstream>
#include <time.h>
#include <private/qqmljsengine_p.h>
#include <private/qqmljslexer_p.h>
#include <private/qqmljsparser_p.h>
#include <private/qv4value_p.h>
#include <3rdparty/lz-string/src/lzstring.h>
#include "AstGenerator.h"
#include "parser.h"
using namespace std;
bool Foobar::debug = true;
void Foobar::setDebug(bool debug_) { Foobar::debug = debug_; }
int Foobar::InternalRun(const QString &code) {
QQmlJS::Engine engine;
QQmlJS::Lexer lexer(&engine);
lexer.setCode(code, 1, true);
QQmlJS::Parser parser(&engine);
bool success = parser.parse();
if (!success) {
const auto diagnosticMessages = parser.diagnosticMessages();
for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
qWarning("%d: %s", m.loc.startLine, qPrintable(m.message));
}
return 1;
}
const bool debug = this->m_options.testFlag(Option::Debug);
setDebug(debug);
AstGenerator generator(&engine, 0);
const json ast = generator(parser.ast());
if (debug) {
ofstream myfile;
myfile.open("sandbox/test.json");
myfile << ast.dump(2);
myfile.close();
} else {
cout << ast.dump(2);
}
return 0;
}
Foobar::Foobar(Options options) { this->m_options = options; }
int Foobar::Run(QStringList args) {
if (args.count() == 0) {
QTextStream(stderr) << "Please provide a path or a QML text\n";
return 1;
}
if (args.count() > 1) {
QTextStream(stderr) << "Please provide only one path or one QML text\n";
return 1;
}
int returnValue = 0;
const QString pathOrText = args[0];
QFileInfo fileInfo(pathOrText);
if (fileInfo.isDir()) {
QTextStream(stderr) << "qml-parser doesn't hangle path to directory\n";
return 1;
}
if (fileInfo.isFile()) {
QFile file(pathOrText);
file.open(QFile::ReadOnly | QFile::Text);
const QString code = QString::fromUtf8(file.readAll());
returnValue = this->InternalRun(code);
} else {
QString code = pathOrText;
if (!pathOrText.trimmed().startsWith("import")) {
code = LZString::decompressFromBase64(code);
}
returnValue = this->InternalRun(code);
}
return returnValue;
}