WsParser_VS/WsNovelParser/main.cpp

127 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfoList>
#include <ast_gen.h>
#include <ast_novel.h>
#include <libtoken.h>
#include <syntax_novel.h>
#include <tokens_novel.h>
#include <QDir>
#include <QTextStream>
#include <iostream>
#include <QTime>
#include <argsparser.h>
#include "novelparser.h"
#include "htmlprint.h"
using namespace example_novel;
/*
* nsc --help
* nsc -[pw] --path path-to-dir [--out path-to-dir]
* opts:
* p print-struct 输出整体结构
* w print-web 基础的web页面输出格式
*/
int main(int argc, char* argv[]) {
QCoreApplication a(argc, argv);
args_parse::ArgsParser args_parser;
QList<std::shared_ptr<args_parse::ArgvPack>> args_mode;
args_mode << std::make_shared<args_parse::IndexParam>(u8"程序名称");
args_mode << std::make_shared<args_parse::FloatOption>(u8"--help", u8"帮助");
args_parser.loadMode(0x000Au, args_mode);
args_mode.clear();
args_mode << std::make_shared<args_parse::IndexParam>(u8"程序名称");
args_mode << std::make_shared<args_parse::FloatArgvPack>(u8"--path", u8"源代码目录");
args_mode << std::make_shared<args_parse::FloatArgvPack>(u8"--dest", u8"生成目录");
args_mode << std::make_shared<args_parse::FloatOption>(u8"--html", u8"生成html文件格式取代AST", true);
args_parser.loadMode(0x000Bu, args_mode);
auto parse_result = args_parser.parse(argc, argv);
switch (std::get<0>(parse_result)) {
case 0xBu: {
auto args = std::get<1>(parse_result);
auto src_dir = std::dynamic_pointer_cast<args_parse::FloatArgvPack>(args_mode[1]);
auto dst_dir = std::dynamic_pointer_cast<args_parse::FloatArgvPack>(args_mode[2]);
auto html_opt = std::dynamic_pointer_cast<args_parse::FloatOption>(args_mode[3]);
auto source_dir = QDir(src_dir->value());
if (!source_dir.exists()) {
std::cout << "%编译指定的源代码目录不存在!" << std::endl;
exit(0);
}
auto destination_dir = QDir::current();
auto target_output = dst_dir->value();
if (!target_output.isEmpty() && QDir(target_output).exists()) {
destination_dir = QDir(target_output);
}
else {
std::cout << "%编译指定的生成目录不存在,重置为:" << destination_dir.absolutePath().toLocal8Bit().data() << std::endl;
}
auto files = source_dir.entryInfoList(QStringList() << "*.story");
std::shared_ptr<const ast_gen::ElementAccess> access_ptr = nullptr;
if (files.size()) {
try {
auto parser = std::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 (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();
lib_parse::VisitorControl control;
auto visitor = std::make_shared<printer::AstGenerate>();
control.visitWith(access_ptr, visitor);;
auto dom_result = visitor->content();
QFile file(destination_dir.absoluteFilePath(u8"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(u8"%AST构建消耗时间%1 ms。").arg(time_stamp.msecsTo(current_stamp));
qDebug().noquote() << QString(u8"%编译成功:%1。").arg(QFileInfo(file).absoluteFilePath());
}
}break;
case 0xAu:
default: {
std::cout << "nsc(WsNovelStoryCompiler故事线编译器)" << std::endl;
std::cout << "版本V1.0.0" << std::endl;
std::cout << "nsc --path path-to-dir --dest path-to-output [--html]" << std::endl;
}break;
}
//return a.exec();
return 0;
}