#include "novelparser.h" #include #include #include #include #include using namespace example_novel; using namespace lib_parse; NovelParser::NovelParser() { checker_list << std::make_shared(); checker_list << std::make_shared(); checker_list << std::make_shared(); checker_list << std::make_shared(); analyzer_ref = std::make_shared(checker_list); } QList> NovelParser::fragmentsSorted() const { return std::dynamic_pointer_cast(checker_list[1])->pointsSequence(); } QString NovelParser::version() const { return "1.0.0"; } std::shared_ptr NovelParser::parserContext() const { return context; } std::shared_ptr NovelParser::parse(const QFileInfoList source_list) const { const_cast(this)->context = std::make_shared("小说"); 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(); 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(this)->context->appendError(item->totalErrors()); break; } else { proc.getAst(rst.first(), const_cast(this)->context); } } auto current_stamp = QTime::currentTime(); qDebug().noquote() << QString("%词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp)); return this->context; } std::shared_ptr NovelParser::validsApply(std::shared_ptr ast_root) const { auto time_stamp = QTime::currentTime(); auto access_root = std::make_shared(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; }