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
|
/*
SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org>
SPDX-License-Identifier: LGPL-2.0-only
*/
#ifndef KDEVPLATFORM_DEBUGLANGUAGEPARSERHELPER_H
#define KDEVPLATFORM_DEBUGLANGUAGEPARSERHELPER_H
#include <cstdlib>
#include <cstdio>
#ifndef Q_OS_WIN
#include <unistd.h> // for isatty
#endif
#include <tests/autotestshell.h>
#include <language/duchain/duchain.h>
#include <language/duchain/problem.h>
#include <language/codegen/coderepresentation.h>
#include <tests/testcore.h>
#include <KAboutData>
#include <KLocalizedString>
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QTextStream>
namespace KDevelopUtils {
QTextStream qout(stdout);
QTextStream qerr(stderr);
QTextStream qin(stdin);
using TokenTextFunc = QString (*)(int);
/**
* This class is a pure helper to use for binaries that you can
* run on short snippets of test code or whole files and let
* it print the generated tokens or AST.
*
* It should work fine for any KDevelop-PG-Qt based parser.
*
*
* @tparam SessionT the parse session for your language.
* @tparam TokenStreamT the token stream for your language, based on KDevPG::TokenStreamBase.
* @tparam TokenT the token class for your language, based on KDevPG::Token.
* @tparam LexerT the Lexer for your language.
* @tparam StartAstT the AST node that is returned from @c SessionT::parse().
* @tparam DebugVisitorT the debug visitor for your language.
* @tparam TokenTextT function pointer to the function that returns a string representation for an integral token.
*/
template <class SessionT, class TokenStreamT, class TokenT, class LexerT,
class StartAstT, class DebugVisitorT, TokenTextFunc TokenTextT>
class DebugLanguageParserHelper
{
public:
DebugLanguageParserHelper(const bool printAst, const bool printTokens)
: m_printAst(printAst)
, m_printTokens(printTokens)
{
m_session.setDebug(printAst);
}
/// parse contents of a file
void parseFile(const QString& fileName)
{
if (!m_session.readFile(fileName, "utf-8")) {
qerr << "Can't open file " << fileName << Qt::endl;
std::exit(255);
} else {
qout << "Parsing file " << fileName << Qt::endl;
}
runSession();
}
/// parse code directly
void parseCode(const QString& code)
{
m_session.setContents(code);
qout << "Parsing input" << Qt::endl;
runSession();
}
private:
/**
* actually run the parse session
*/
void runSession()
{
if (m_printTokens) {
TokenStreamT tokenStream;
LexerT lexer(&tokenStream, m_session.contents());
int token;
while ((token = lexer.nextTokenKind())) {
TokenT& t = tokenStream.push();
t.begin = lexer.tokenBegin();
t.end = lexer.tokenEnd();
t.kind = token;
printToken(token, lexer);
}
printToken(token, lexer);
if (tokenStream.size() > 0) {
qint64 line;
qint64 column;
tokenStream.endPosition(tokenStream.size() - 1, &line, &column);
qDebug() << "last token endPosition: line" << line << "column" << column;
} else {
qDebug() << "empty token stream";
}
}
StartAstT* ast = 0;
if (!m_session.parse(&ast)) {
qerr << "no AST tree could be generated" << Qt::endl;
} else {
qout << "AST tree successfully generated" << Qt::endl;
if (m_printAst) {
DebugVisitorT debugVisitor(m_session.tokenStream(), m_session.contents());
debugVisitor.visitStart(ast);
}
}
const auto problems = m_session.problems();
if (!problems.isEmpty()) {
qout << endl << "problems encountered during parsing:" << Qt::endl;
for (auto& p : problems) {
qout << p->description() << Qt::endl;
}
} else {
qout << "no problems encountered during parsing" << Qt::endl;
}
if (!ast) {
exit(255);
}
}
void printToken(int token, const LexerT& lexer) const
{
int begin = lexer.tokenBegin();
int end = lexer.tokenEnd();
qout << m_session.contents().mid(begin, end - begin + 1).replace('\n', "\\n") << ' ' << TokenTextT(token)
<< Qt::endl;
}
SessionT m_session;
const bool m_printAst;
const bool m_printTokens;
};
template <class ParserT>
void setupCustomArgs(QCommandLineParser* parser)
{
Q_UNUSED(parser);
}
template <class ParserT>
void setCustomArgs(ParserT* parser, QCommandLineParser* commandLineParser)
{
Q_UNUSED(parser);
Q_UNUSED(commandLineParser);
}
/// call this after setting up @p aboutData in your @c main() function.
template <class ParserT>
int initAndRunParser(KAboutData& aboutData, int argc, char* argv[])
{
qout.setCodec("UTF-8");
qerr.setCodec("UTF-8");
qin.setCodec("UTF-8");
QCoreApplication app(argc, argv);
KAboutData::setApplicationData(aboutData);
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
parser.addPositionalArgument("files",
i18n(
"files or - to read from STDIN, the latter is the default if nothing is provided"),
"[FILE...]");
parser.addOption(QCommandLineOption{QStringList{"a", "print-ast"}, i18n("print generated AST tree")});
parser.addOption(QCommandLineOption{QStringList{"t", "print-tokens"}, i18n("print generated token stream")});
parser.addOption(QCommandLineOption{QStringList{"c", "code"}, i18n("code to parse"), "code"});
setupCustomArgs<ParserT>(&parser);
parser.process(app);
aboutData.processCommandLine(&parser);
QStringList files = parser.positionalArguments();
bool printAst = parser.isSet("print-ast");
bool printTokens = parser.isSet("print-tokens");
KDevelop::AutoTestShell::init();
KDevelop::TestCore::initialize(KDevelop::Core::NoUi);
KDevelop::DUChain::self()->disablePersistentStorage();
KDevelop::CodeRepresentation::setDiskChangesForbidden(true);
ParserT parserT(printAst, printTokens);
setCustomArgs(&parserT, &parser);
if (parser.isSet("code")) {
parserT.parseCode(parser.value("code"));
} else if (files.isEmpty()) {
files << "-";
}
for (const QString& fileName : qAsConst(files)) {
if (fileName == "-") {
#ifndef Q_OS_WIN
if (isatty(STDIN_FILENO)) {
qerr << "no STDIN given" << Qt::endl;
return 255;
}
#endif
parserT.parseCode(qin.readAll().toUtf8());
} else {
parserT.parseFile(QFileInfo(fileName).absoluteFilePath());
}
}
KDevelop::TestCore::shutdown();
return 0;
}
}
#endif // KDEVPLATFORM_DEBUGLANGUAGEPARSERHELPER_H
|