#include "ast_gen.h" using namespace ast_gen; using namespace ast_basic; using namespace lib_token; using namespace lib_syntax; using namespace lib_words; SyntaxParser::SyntaxParser(std::shared_ptr rule) : _rule_bind(rule) { } QList> SyntaxParser::parse(std::shared_ptr words) { auto cursor = std::make_shared(words->file()); cursor->setCurrent(nullptr, words); QList> out; auto list = this->_rule_bind->parse(cursor, out); if (out.size()) list = out; QHash> result_map; std::for_each(list.begin(), list.end(), [&](std::shared_ptr ins) { result_map[ins->parseSyntax()] = ins; }); list = result_map.values(); std::sort(list.begin(), list.end(), [&](std::shared_ptr a, std::shared_ptr b) { auto mark_a = a->currentToken()->position() * 100 - a->totalErrorCount(); auto mark_b = b->currentToken()->position() * 100 - b->totalErrorCount(); return mark_a > mark_b; }); return list; } #include "tokens_impl.h" std::shared_ptr ast_gen::SyntaxParser::getAst( std::shared_ptr cursor, std::shared_ptr root) { QList> token_seqs; std::shared_ptr action_token = cursor->currentToken(); while (action_token) { token_seqs.prepend(action_token); action_token = action_token->prevToken(); } auto expr_inst = root; for (auto ins_t : token_seqs) { expr_inst = std::const_pointer_cast(ins_t)->makeSure(expr_inst); } return expr_inst; }