121 lines
3.7 KiB
C++
121 lines
3.7 KiB
C++
#include <QtCore/QCoreApplication>
|
||
#include <QCoreApplication>
|
||
#include <QDebug>
|
||
#include <QFileInfoList>
|
||
#include <ast_gen.h>
|
||
#include <ast_novel.h>
|
||
#include <libtokens.h>
|
||
#include <syntax_novel.h>
|
||
#include <tokens_novel.h>
|
||
#include <QDir>
|
||
#include <QTextStream>
|
||
#include <iostream>
|
||
#include <QTime>
|
||
#include <argsparser.h>
|
||
|
||
#include "novelparser.h"
|
||
#include "astprint.h"
|
||
|
||
using namespace example_novel;
|
||
using namespace std;
|
||
using namespace args_parse;
|
||
|
||
|
||
int main(int argc, char* argv[]) {
|
||
QCoreApplication a(argc, argv);
|
||
|
||
ArgsParser args_parser;
|
||
auto help_mode = make_shared<MatchMode>(0x000Au, "打印帮助信息");
|
||
args_parser << help_mode;
|
||
*help_mode << make_shared<IndexParam>("nsc", "程序名称")
|
||
<< make_shared<FloatOption>("help", "帮助");
|
||
|
||
auto build_mode = make_shared<MatchMode>(0x000Bu, "执行故事线编译任务");
|
||
args_parser << build_mode;
|
||
*build_mode << make_shared<IndexParam>("nsc", "程序名称")
|
||
<< make_shared<FloatKeyValue>("path", "指定源代码目录")
|
||
<< make_shared<FloatKeyValue>("dest", "指定生成目录");
|
||
|
||
|
||
auto p_result = args_parser.parse(argc, argv);
|
||
if (!p_result) {
|
||
qDebug().noquote() << "命令行参数错误!";
|
||
qDebug().noquote() << args_parser.helperDoc();
|
||
}
|
||
else {
|
||
switch (p_result->modeCode()) {
|
||
case 0xBu:
|
||
{
|
||
auto src_dir = dynamic_pointer_cast<FloatKeyValue>(p_result->getUnitViaKey("path"));
|
||
auto dst_dir = dynamic_pointer_cast<FloatKeyValue>(p_result->getUnitViaKey("dest"));
|
||
|
||
auto source_dir = QDir(src_dir->value().toString());
|
||
if (!source_dir.exists()) {
|
||
qDebug() << "%编译指定的源代码目录不存在!" << source_dir.absolutePath() << endl;
|
||
exit(0);
|
||
}
|
||
auto destination_dir = QDir::current();
|
||
auto target_output = dst_dir->value();
|
||
if (!target_output.isNull() && QDir(target_output.toString()).exists()) {
|
||
destination_dir = QDir(target_output.toString());
|
||
}
|
||
else {
|
||
qDebug() << "%编译指定的生成目录不存在,重置为:" << destination_dir.absolutePath().toLocal8Bit().data() << endl;
|
||
}
|
||
|
||
auto files = source_dir.entryInfoList(QStringList() << "*.story");
|
||
shared_ptr<const ast_gen::ElementAccess> access_ptr = nullptr;
|
||
if (files.size()) {
|
||
try {
|
||
auto parser = make_shared<NovelParser>();
|
||
auto docs = parser->parse(files);
|
||
|
||
auto errors_list = parser->parserContext()->errors();
|
||
if (errors_list.size()) {
|
||
for (auto& err : errors_list) {
|
||
qDebug().noquote() << err;
|
||
}
|
||
exit(0);
|
||
}
|
||
access_ptr = parser->validsApply(docs);
|
||
}
|
||
catch (lib_syntax::SyntaxException* e) {
|
||
qDebug().noquote() << e->message();
|
||
delete e;
|
||
exit(0);
|
||
}
|
||
catch (lib_parse::CheckException* e) {
|
||
qDebug().noquote() << e->message();
|
||
delete e;
|
||
exit(0);
|
||
}
|
||
}
|
||
|
||
if (access_ptr) {
|
||
QTime time_stamp = QTime::currentTime();
|
||
lib_parse::VisitEntry control;
|
||
auto visitor = make_shared<printer::AstGenerate>(source_dir);
|
||
control.visitWith(access_ptr, visitor);
|
||
auto dom_result = visitor->content();
|
||
QFile file(destination_dir.absoluteFilePath("storyline.xast"));
|
||
if (file.open(QIODevice::Text | QIODevice::WriteOnly)) {
|
||
QTextStream tout(&file);
|
||
tout.setCodec("UTF-8");
|
||
tout << dom_result;
|
||
tout.flush();
|
||
}
|
||
auto current_stamp = QTime::currentTime();
|
||
qDebug().noquote() << QString("%AST构建消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
||
qDebug().noquote() << QString("%编译成功:%1。").arg(QFileInfo(file).absoluteFilePath());
|
||
}
|
||
}break;
|
||
case 0xAu:
|
||
default:
|
||
qDebug().noquote() << args_parser.helperDoc();
|
||
break;
|
||
}
|
||
}
|
||
//return a.exec();
|
||
return 0;
|
||
}
|