File indexing completed on 2024-05-19 04:42:04

0001 /*
0002     SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KAboutData>
0008 
0009 #include <language/util/debuglanguageparserhelper.h>
0010 
0011 #include "../duchain/debugvisitor.h"
0012 #include "../duchain/parsesession.h"
0013 
0014 using namespace KDevelop;
0015 using namespace KDevelopUtils;
0016 
0017 class QmlParser
0018 {
0019 public:
0020     QmlParser(const bool printAst, const bool printTokens)
0021       : m_printAst(printAst), m_printTokens(printTokens)
0022     {
0023     }
0024 
0025     /// parse contents of a file
0026     void parseFile( const QString &fileName )
0027     {
0028         QFile file(fileName);
0029         file.open(QIODevice::ReadOnly);
0030         m_session.reset(new ParseSession(IndexedString(fileName), file.readAll(), 0));
0031         runSession();
0032     }
0033 
0034     /// parse code directly
0035     void parseCode( const QString &code )
0036     {
0037         m_session.reset(new ParseSession(IndexedString("-"), code, 0));
0038         runSession();
0039     }
0040 
0041 private:
0042     /**
0043      * actually run the parse session
0044      */
0045     void runSession()
0046     {
0047         if (!m_session->isParsedCorrectly()) {
0048             qerr << "failed to parse code" << Qt::endl;
0049         }
0050         if (m_printTokens) {
0051             qerr << "token cannot be printed for qml/js source..." << Qt::endl;
0052         }
0053 
0054         if (!m_session->ast()) {
0055             qerr << "no AST tree could be generated" << Qt::endl;
0056         } else {
0057             qout << "AST tree successfully generated" << Qt::endl;
0058             if (m_printAst) {
0059                 ///FIXME:
0060                 DebugVisitor visitor(m_session.data());
0061                 visitor.startVisiting(m_session->ast());
0062             }
0063         }
0064         if (!m_session->problems().isEmpty()) {
0065             qerr << "\nproblems encountered during parsing:" << Qt::endl;
0066             foreach(const ProblemPointer problem, m_session->problems()) {
0067                 qerr << problem->toString();
0068             }
0069         } else {
0070             qout << "no problems encountered during parsing" << Qt::endl;
0071         }
0072 
0073         if (!m_session->ast()) {
0074             exit(255);
0075         }
0076     }
0077 
0078     QScopedPointer<ParseSession> m_session;
0079     const bool m_printAst;
0080     const bool m_printTokens;
0081 };
0082 
0083 int main(int argc, char* argv[])
0084 {
0085     KAboutData aboutData(QStringLiteral("cpp-parser"), i18n( "cpp-parser" ),
0086                          QStringLiteral("1"), i18n("KDevelop CPP parser debugging utility"), KAboutLicense::GPL,
0087                          i18n( "2011 Milian Wolff" ), QString(), QStringLiteral("https://www.kdevelop.org/") );
0088 
0089     return KDevelopUtils::initAndRunParser<QmlParser>(aboutData, argc, argv);
0090 }