WsParser_VS/WsNovelParser/main.cpp

127 lines
4.0 KiB
C++
Raw Normal View History

2024-03-17 07:58:28 +00:00
#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfoList>
#include <ast_gen.h>
#include <ast_novel.h>
2025-02-02 12:54:32 +00:00
#include <libtokens.h>
2024-03-17 07:58:28 +00:00
#include <syntax_novel.h>
#include <tokens_novel.h>
2024-03-31 05:59:17 +00:00
#include <QDir>
2024-04-04 05:18:44 +00:00
#include <QTextStream>
2024-04-20 03:34:33 +00:00
#include <iostream>
2024-06-15 01:18:33 +00:00
#include <QTime>
2024-06-22 10:53:51 +00:00
#include <argsparser.h>
2024-03-17 07:58:28 +00:00
#include "novelparser.h"
2024-03-31 05:59:17 +00:00
#include "htmlprint.h"
2024-03-17 07:58:28 +00:00
using namespace example_novel;
2024-10-01 15:23:54 +00:00
using namespace std;
using namespace args_parse;
2024-03-17 07:58:28 +00:00
2024-10-02 02:03:19 +00:00
2024-03-17 07:58:28 +00:00
int main(int argc, char* argv[]) {
2024-04-05 12:51:20 +00:00
QCoreApplication a(argc, argv);
2024-03-17 07:58:28 +00:00
2024-10-01 15:23:54 +00:00
ArgsParser args_parser;
2025-02-15 15:47:42 +00:00
auto help_mode = make_shared<MatchMode>(0x000Au, "打印帮助信息");
2024-10-01 15:23:54 +00:00
args_parser << help_mode;
2025-02-15 15:47:42 +00:00
*help_mode << make_shared<IndexParam>("nsc", "程序名称")
<< make_shared<FloatOption>("help", "帮助");
2024-06-22 10:53:51 +00:00
2025-02-15 15:47:42 +00:00
auto build_mode = make_shared<MatchMode>(0x000Bu, "执行故事线编译任务");
2024-10-01 15:23:54 +00:00
args_parser << build_mode;
2025-02-15 15:47:42 +00:00
*build_mode << make_shared<IndexParam>("nsc", "程序名称")
<< make_shared<FloatKeyValue>("path", "指定源代码目录")
<< make_shared<FloatKeyValue>("dest", "指定生成目录")
<< make_shared<FloatOption>("html", "生成html文件格式取代AST", true);
2024-06-05 04:12:31 +00:00
2024-06-22 10:53:51 +00:00
2024-10-01 15:23:54 +00:00
auto p_result = args_parser.parse(argc, argv);
if (!p_result) {
2025-02-15 15:47:42 +00:00
qDebug().noquote() << "命令行参数错误!";
2024-10-04 05:12:16 +00:00
qDebug().noquote() << args_parser.helperDoc();
2024-10-01 15:23:54 +00:00
}
else {
switch (p_result->modeCode()) {
case 0xBu:
{
2025-02-15 15:47:42 +00:00
auto src_dir = dynamic_pointer_cast<FloatKeyValue>(p_result->getUnitViaKey("path"));
auto dst_dir = dynamic_pointer_cast<FloatKeyValue>(p_result->getUnitViaKey("dest"));
auto html_opt = dynamic_pointer_cast<FloatOption>(p_result->getUnitViaKey("html"));
2024-06-22 10:53:51 +00:00
2024-10-01 16:03:59 +00:00
auto source_dir = QDir(src_dir->value().toString());
2024-10-01 15:23:54 +00:00
if (!source_dir.exists()) {
2025-02-15 15:47:42 +00:00
cout << "%编译指定的源代码目录不存在!" << endl;
2024-10-01 15:23:54 +00:00
exit(0);
}
auto destination_dir = QDir::current();
auto target_output = dst_dir->value();
2024-10-01 16:03:59 +00:00
if (!target_output.isNull() && QDir(target_output.toString()).exists()) {
destination_dir = QDir(target_output.toString());
2024-10-01 15:23:54 +00:00
}
else {
2025-02-15 15:47:42 +00:00
cout << "%编译指定的生成目录不存在,重置为:" << destination_dir.absolutePath().toLocal8Bit().data() << endl;
2024-10-01 15:23:54 +00:00
}
2024-03-17 07:58:28 +00:00
2024-10-01 15:23:54 +00:00
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);
2024-07-12 09:35:35 +00:00
2024-10-01 15:23:54 +00:00
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);
}
2025-02-15 15:47:42 +00:00
catch (lib_syntax::SyntaxException* e) {
2024-10-01 15:23:54 +00:00
qDebug().noquote() << e->message();
delete e;
exit(0);
}
catch (lib_parse::CheckException* e) {
qDebug().noquote() << e->message();
delete e;
exit(0);
}
2024-07-12 09:35:35 +00:00
}
2024-06-15 01:18:33 +00:00
2024-10-01 15:23:54 +00:00
if (html_opt->value().toInt() == 1 && access_ptr) {
printer::tools_printer tool;
tool.plain_html_output(access_ptr, destination_dir);
}
else if (access_ptr) {
QTime time_stamp = QTime::currentTime();
2025-02-15 15:47:42 +00:00
lib_parse::VisitEntry control;
2024-10-01 15:23:54 +00:00
auto visitor = make_shared<printer::AstGenerate>(source_dir);
2025-02-15 15:47:42 +00:00
control.visitWith(access_ptr, visitor);
2024-10-01 15:23:54 +00:00
auto dom_result = visitor->content();
2025-02-15 15:47:42 +00:00
QFile file(destination_dir.absoluteFilePath("storyline.xast"));
2024-10-01 15:23:54 +00:00
if (file.open(QIODevice::Text | QIODevice::WriteOnly)) {
QTextStream tout(&file);
tout.setCodec("UTF-8");
tout << dom_result;
tout.flush();
}
auto current_stamp = QTime::currentTime();
2025-02-15 15:47:42 +00:00
qDebug().noquote() << QString("%AST构建消耗时间%1 ms。").arg(time_stamp.msecsTo(current_stamp));
qDebug().noquote() << QString("%编译成功:%1。").arg(QFileInfo(file).absoluteFilePath());
2024-10-01 15:23:54 +00:00
}
}break;
2024-10-03 12:37:24 +00:00
case 0xAu:
default:
2024-10-04 05:12:16 +00:00
qDebug().noquote() << args_parser.helperDoc();
2024-10-03 12:37:24 +00:00
break;
2024-06-22 15:19:14 +00:00
}
2024-04-05 12:51:20 +00:00
}
2024-05-26 04:11:45 +00:00
//return a.exec();
2024-06-23 02:50:38 +00:00
return 0;
2024-03-17 07:58:28 +00:00
}