コード例 #1
0
ファイル: lpeg_tester.cpp プロジェクト: cluo/lpeg_tester
void Tester::LoadGrammar()
{
    string h = internalPathNextPart("/share/");
    fs::path input = fs::path(appRoot()) / "share" / (h + ".data");
    mInput->setText("");
    mGrammar->setText("");
    mResult->clear();

    ifstream data_fs(input.string().c_str());
    if (data_fs) {
        data_fs.unsetf(std::ios::skipws);
        string data((istream_iterator<char>(data_fs)), istream_iterator<char>());
        mInput->setText(data);
        data_fs.close();
    } else {
        stringstream ss;
        ss << "share not found: " << h;
        Wt::WText* t = new Wt::WText(ss.str(), mResult);
        t->setStyleClass("result_error");
        Wt::log("info") << ss.str();
        return;
    }

    fs::path grammar = fs::path(appRoot()) / "share" / (h + ".lua");
    ifstream grammar_fs(grammar.string().c_str());
    if (grammar_fs) {
        grammar_fs.unsetf(std::ios::skipws);
        string data((istream_iterator<char>(grammar_fs)), istream_iterator<char>());
        mGrammar->setText(data);
        grammar_fs.close();
    }
}
コード例 #2
0
ファイル: lpeg_tester.cpp プロジェクト: cluo/lpeg_tester
void Tester::TestGrammar(bool benchmark)
{
    mResult->clear();

    fs::path grammar = fs::path("/tmp") / (sessionId() + ".lua");
    ofstream ofs(grammar.string().c_str());
    if (ofs) {
        ofs << mGrammar->text();
        ofs.close();
    } else {
        stringstream ss;
        ss << "failed to open: " << grammar;
        Wt::WText* t = new Wt::WText(ss.str(), mResult);
        t->setStyleClass("result_error");
        Wt::log("error") << ss.str();
        return;
    }
    lua_sandbox* sb = lsb_create(NULL, grammar.string().c_str(), "modules",
                                 8*1024*1024, 1e6, 1024*63);
    if (!sb) {
        stringstream ss;
        ss << "lsb_create() failed";
        Wt::WText* t = new Wt::WText(ss.str(), mResult);
        t->setStyleClass("result_error");
        Wt::log("error") << ss.str();
        return;
    }
    if (lsb_init(sb, nullptr)) {
        stringstream ss;
        string error = lsb_get_error(sb);
        size_t pos = error.find_first_of(':');
        if (pos != string::npos) {
            ss << "line " << error.substr(pos + 1);
        } else {
            ss << error;
        }
        Wt::WText* t = new Wt::WText(ss.str(), mResult);
        t->setStyleClass("result_error");
        Wt::log("info") << ss.str();
        return;

    }
    if (benchmark) {
        Benchmark(sb, mInput->text().narrow());
    } else {
        Match(sb, mInput->text().narrow());
    }
    char* e = lsb_destroy(sb, nullptr);
    if (e) {
        stringstream ss;
        ss << "lsb_destroy() failed: " << e;
        Wt::WText* t = new Wt::WText(ss.str(), mResult);
        t->setStyleClass("result_error");
        Wt::log("info") << ss.str();
        free(e);
    }
}