1 /*
2 SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include <KAboutData>
8
9 #include <language/util/debuglanguageparserhelper.h>
10
11 #include "../duchain/debugvisitor.h"
12 #include "../duchain/parsesession.h"
13
14 using namespace KDevelop;
15 using namespace KDevelopUtils;
16
17 class QmlParser {
18 using TextStreamFunction = QTextStream& (*)(QTextStream&);
19 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
20 static constexpr TextStreamFunction endl = Qt::endl;
21 #else
22 static constexpr TextStreamFunction endl = ::endl;
23 #endif
24
25 public:
QmlParser(const bool printAst,const bool printTokens)26 QmlParser(const bool printAst, const bool printTokens)
27 : m_printAst(printAst), m_printTokens(printTokens)
28 {
29 }
30
31 /// parse contents of a file
parseFile(const QString & fileName)32 void parseFile( const QString &fileName )
33 {
34 QFile file(fileName);
35 file.open(QIODevice::ReadOnly);
36 m_session.reset(new ParseSession(IndexedString(fileName), file.readAll(), 0));
37 runSession();
38 }
39
40 /// parse code directly
parseCode(const QString & code)41 void parseCode( const QString &code )
42 {
43 m_session.reset(new ParseSession(IndexedString("-"), code, 0));
44 runSession();
45 }
46
47 private:
48 /**
49 * actually run the parse session
50 */
runSession()51 void runSession()
52 {
53 if (!m_session->isParsedCorrectly()) {
54 qerr << "failed to parse code" << endl;
55 }
56 if (m_printTokens) {
57 qerr << "token cannot be printed for qml/js source..." << endl;
58 }
59
60 if (!m_session->ast()) {
61 qerr << "no AST tree could be generated" << endl;
62 } else {
63 qout << "AST tree successfully generated" << endl;
64 if (m_printAst) {
65 ///FIXME:
66 DebugVisitor visitor(m_session.data());
67 visitor.startVisiting(m_session->ast());
68 }
69 }
70 if (!m_session->problems().isEmpty()) {
71 qerr << endl << "problems encountered during parsing:" << endl;
72 foreach(const ProblemPointer problem, m_session->problems()) {
73 qerr << problem->toString();
74 }
75 } else {
76 qout << "no problems encountered during parsing" << endl;
77 }
78
79 if (!m_session->ast()) {
80 exit(255);
81 }
82 }
83
84 QScopedPointer<ParseSession> m_session;
85 const bool m_printAst;
86 const bool m_printTokens;
87 };
88
main(int argc,char * argv[])89 int main(int argc, char* argv[])
90 {
91 KAboutData aboutData(QStringLiteral("cpp-parser"), i18n( "cpp-parser" ),
92 QStringLiteral("1"), i18n("KDevelop CPP parser debugging utility"), KAboutLicense::GPL,
93 i18n( "2011 Milian Wolff" ), QString(), QStringLiteral("https://www.kdevelop.org/") );
94
95 return KDevelopUtils::initAndRunParser<QmlParser>(aboutData, argc, argv);
96 }
97