2024-03-17 07:58:28 +00:00
|
|
|
#include "novelparser.h"
|
|
|
|
|
|
|
|
#include <syntax_novel.h>
|
|
|
|
#include <ast_novel.h>
|
|
|
|
#include <parse_novel.h>
|
2024-06-15 01:18:33 +00:00
|
|
|
#include <QTime>
|
|
|
|
#include <QDebug>
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
using namespace example_novel;
|
2024-06-20 14:13:06 +00:00
|
|
|
using namespace lib_parse;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
NovelParser::NovelParser() {
|
|
|
|
checker_list << std::make_shared<FragmentExistsCheck>();
|
|
|
|
checker_list << std::make_shared<StoryOrderCheck>();
|
|
|
|
checker_list << std::make_shared<PointGraphCheck>();
|
|
|
|
|
|
|
|
analyzer_ref = std::make_shared<Analyzer>(checker_list);
|
2024-06-20 14:13:06 +00:00
|
|
|
}
|
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
QList<std::shared_ptr<const PointGraphHelper>> NovelParser::fragmentsSorted() const {
|
|
|
|
return std::dynamic_pointer_cast<const PointGraphCheck>(checker_list[1])->pointsSequence();
|
2024-03-17 07:58:28 +00:00
|
|
|
}
|
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
QString NovelParser::version() const {
|
|
|
|
return "1.0.0";
|
2024-03-17 07:58:28 +00:00
|
|
|
}
|
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
std::shared_ptr<example_novel::NGlobalElement> NovelParser::parserContext() const {
|
|
|
|
return context;
|
2024-07-12 22:47:28 +00:00
|
|
|
}
|
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
std::shared_ptr<const example_novel::NGlobalElement> NovelParser::parse(const QFileInfoList source_list) const {
|
|
|
|
const_cast<NovelParser*>(this)->context = std::make_shared<example_novel::NGlobalElement>("小说");
|
|
|
|
|
|
|
|
auto time_stamp = QTime::currentTime();
|
|
|
|
for (auto finfo : source_list) {
|
|
|
|
// 载入指定文件内容
|
|
|
|
QFile file_target(finfo.absoluteFilePath());
|
|
|
|
if (!file_target.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
throw new lib_parse::CheckException(QString("DeviceError[0x00001]指定文件无法打开:%1。").arg(finfo.absoluteFilePath()));
|
|
|
|
}
|
|
|
|
QTextStream intext(&file_target);
|
|
|
|
intext.setCodec("UTF-8");
|
|
|
|
auto word_reader = std::make_shared<lib_words::WordReader>();
|
|
|
|
auto word_head = word_reader->wordsFrom(intext, finfo.canonicalFilePath());
|
|
|
|
|
|
|
|
// 文件语法解析
|
|
|
|
ast_gen::SyntaxParser proc(example_novel::NovalSyntax::getSyntaxTree());
|
|
|
|
auto rst = proc.parse(word_head);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
if (rst.first()->totalErrorCount()) {
|
|
|
|
auto pos_mark = rst.first()->token()->position();
|
|
|
|
for (auto item : rst)
|
|
|
|
if (item->token()->position() == pos_mark)
|
|
|
|
const_cast<NovelParser*>(this)->context->appendError(item->totalErrors());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
proc.getAst(rst.first(), const_cast<NovelParser*>(this)->context);
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 04:53:43 +00:00
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
auto current_stamp = QTime::currentTime();
|
|
|
|
qDebug().noquote() << QString("%词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
return this->context;
|
2024-07-12 22:47:28 +00:00
|
|
|
}
|
|
|
|
|
2025-02-15 15:47:42 +00:00
|
|
|
std::shared_ptr<const ast_gen::ElementAccess> NovelParser::validsApply(std::shared_ptr<const example_novel::NGlobalElement> ast_root) const {
|
|
|
|
auto time_stamp = QTime::currentTime();
|
|
|
|
|
|
|
|
auto access_root = std::make_shared<ast_gen::ElementAccess>(ast_root);
|
|
|
|
auto results = analyzer_ref->validCheckWith(access_root);
|
|
|
|
|
|
|
|
auto current_stamp = QTime::currentTime();
|
|
|
|
qDebug().noquote() << QString("%程序校验消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
|
|
|
return results;
|
2024-03-17 07:58:28 +00:00
|
|
|
}
|
|
|
|
|