64 lines
2.3 KiB
C++
64 lines
2.3 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;
|
|
|
|
NovelParser::NovelParser()
|
|
{
|
|
this->syntax_defines = example_novel::NovalSyntax::getParseTree();
|
|
checker_list << std::make_shared<example_novel::FragmentExistsCheck>();
|
|
checker_list << std::make_shared<example_novel::FragmentOrdersCheck>();
|
|
|
|
analyzer_ref = std::make_shared<lib_parse::Analyzer>(checker_list);
|
|
}
|
|
|
|
QString NovelParser::version() const
|
|
{
|
|
return "1.0.0";
|
|
}
|
|
|
|
std::shared_ptr<const ast_gen::ElementAccess> NovelParser::parse(const QFileInfoList source_list) const
|
|
{
|
|
QList<std::shared_ptr<const ast_basic::TokenNode>> forst_root;
|
|
auto lex_reader = NovalSyntax::getLexReader();
|
|
|
|
auto time_stamp = QTime::currentTime();
|
|
for (auto& file : source_list) {
|
|
auto tokens = lex_reader->extractFrom(file.canonicalFilePath());
|
|
auto exprs_result = std::make_shared<lib_syntax::ExprsChecker>(this->syntax_defines)->parseFrom(tokens);
|
|
forst_root.append(exprs_result);
|
|
}
|
|
auto current_stamp = QTime::currentTime();
|
|
qDebug() << QString(u8"词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
|
|
|
|
|
std::function<QList<std::shared_ptr<const ast_basic::ExprNode>>(QList<std::shared_ptr<const ast_basic::TokenNode>> input)> summary =
|
|
[&](QList<std::shared_ptr<const ast_basic::TokenNode>> input) -> QList<std::shared_ptr<const ast_basic::ExprNode>> {
|
|
QList<std::shared_ptr<const ast_basic::ExprNode>> temp;
|
|
|
|
if (input.size()) {
|
|
auto item = std::dynamic_pointer_cast<const ast_basic::ExprNode>(input.first());
|
|
temp << item;
|
|
temp.append(summary(input.mid(1)));
|
|
}
|
|
|
|
return temp;
|
|
};
|
|
|
|
time_stamp = QTime::currentTime();
|
|
auto tree_visit = std::make_shared<example_novel::NovelExprsVisitor>();
|
|
auto vinst = std::make_shared<ast_gen::ExprTreeParser>(tree_visit);
|
|
auto resultx = vinst->parse("test", summary(forst_root));
|
|
auto novel_accesstree = vinst->tidy(resultx);
|
|
current_stamp = QTime::currentTime();
|
|
qDebug() << QString(u8"程序结构重建消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
|
|
|
return analyzer_ref->validCheckWith(novel_accesstree);
|
|
}
|
|
|