WsParser_VS/WsNovelParser/novelparser.cpp

80 lines
2.7 KiB
C++

#include "novelparser.h"
#include <syntax_novel.h>
#include <ast_novel.h>
#include <parse_novel.h>
#include <QTime>
#include <QDebug>
using namespace example_novel;
using namespace lib_parse;
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);
}
QList<std::shared_ptr<const PointGraphHelper>> NovelParser::fragmentsSorted() const {
return std::dynamic_pointer_cast<const PointGraphCheck>(checker_list[1])->pointsSequence();
}
QString NovelParser::version() const {
return "1.0.0";
}
std::shared_ptr<example_novel::NGlobalElement> NovelParser::parserContext() const {
return context;
}
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);
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);
}
}
auto current_stamp = QTime::currentTime();
qDebug().noquote() << QString("%词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
return this->context;
}
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;
}