예제 #1
0
SyntaxTree::Childs* Parser::m_parse_infix_common(Parser* exp, Parser* op, bool rep)
{
    // Exp (Op Exp)* のパース
    // rep が false なら Exp (Op Exp)? のパース
    // 結果はSyntaxTreeの配列で [exp op exp op exp ....]
    m_ErrorPos startPos = m_curErrPos();
    SyntaxTree* st = exp->parse();
    if (st->isError()) {
        m_fail(startPos);
        return m_INFIX_COMMON_FATAL_ERROR;
    }
    if (st->isFail()) {
        m_fail(startPos);
        return NULL;
    }
    SyntaxTree::Childs* childs = new SyntaxTree::Childs(3);
    childs->setContentsMemID("pcif");
    childs->add(st);
    //exp->addSyntaxTreeToParent(childs, st);
    for (;;) {
        m_ErrorPos midPos = m_curErrPos();
        SyntaxTree* sto = op->parse();
        if (sto->isError()) {
            m_fail(startPos);
            if (! m_bUseMemoize) {
                SyntaxTree::Childs::deleteRecursively(childs);
            } else {
                delete childs;
            }
            return m_INFIX_COMMON_FATAL_ERROR;
        }
        if (sto->isFail()) {
            m_back(midPos);
            break;
        }
        SyntaxTree* ste = exp->parse();
        if (ste->isError()) {
            m_fail(startPos);
            if (! m_bUseMemoize) {
                SyntaxTree::Childs::deleteRecursively(childs);
            } else {
                delete childs;
            }
            return m_INFIX_COMMON_FATAL_ERROR;
        }
        if (ste->isFail()) {
            if (! m_bUseMemoize)
                SyntaxTree::deleteRecursively(sto);
            m_back(midPos);
            break;
        }
        op->addSyntaxTreeToParent(childs, sto);
        exp->addSyntaxTreeToParent(childs, ste);
        if (!rep)
            break;
    }
    return childs;
}
예제 #2
0
SyntaxTree* Parser::m_parse_op_postfix(Parser* my, Parser* exp, Parser* op, bool allowRepeat)
{
    m_ErrorPos startPos = m_curErrPos();
    SyntaxTree* ste = exp->parse();
    if (ste->isFail()) {
        m_fail(startPos);
        return ste;
    }

    for (;;) {
        m_ErrorPos midPos = m_curErrPos();
        SyntaxTree* sto = op->parse();
        if (sto->isError()) {
            m_fail(startPos);
            SyntaxTree::deleteRecursively(ste);
            return sto;
        }
        if (sto->isFail()) {
            m_back(midPos);
            return ste;
        }

        SyntaxTree::Childs* childs = new SyntaxTree::Childs(2);
        childs->setContentsMemID("pcpo");
        exp->addSyntaxTreeToParent(childs, ste);
        op->addSyntaxTreeToParent(childs, sto);
        ste = my->createSyntaxTree(startPos.parsePos, childs);

        if (! allowRepeat)
            return ste;
    }
}
예제 #3
0
SyntaxTree* Parser::m_parse_m2n(Parser* p, int m, int n)
{
    int count = 0;
    m_ErrorPos startPos = m_curErrPos();
    SyntaxTree::Childs* childs = new SyntaxTree::Childs(m > 0 ? m : 1);
    childs->setContentsMemID("pcmn");
    for (;;) {
        SyntaxTree* st = p->parse();
        if (st->isError()) {
            m_fail(startPos);
            return st;
        }
        if (! st->isFail()) {
            p->addSyntaxTreeToParent(childs, st);
            if (++count >= MAX_MANY) count = MAX_MANY - 1;
            if (count >= n)
                break;      // all ok
        } else {
            break;
        }
    }
    if (count < m) {
        m_fail(startPos);
        if (! m_bUseMemoize) {
            SyntaxTree::Childs::deleteRecursively(childs);
        } else {
            delete childs;
        }
        return m_PARSE_FAILED;
    }
    m_errors->clear();
    return createSyntaxTree(startPos.parsePos, childs);
}
예제 #4
0
SyntaxTree* Parser::m_parse_orderedChoice(TArray<Parser*>* ps)
{
    m_ErrorPos startPos = m_curErrPos();

    int i = 0;
    for (TArrayIterator<Parser*> itr(ps); itr.hasMore(); ++i) {
        Parser* p = itr.next();
        SyntaxTree* st = p->parse();
        if (st->isError()) {
            m_fail(startPos);
            return st;
        }
        if (! st->isFail()) {
            SyntaxTree::Childs* childs = new SyntaxTree::Childs(1);
            childs->setContentsMemID("pcoc");
            p->addSyntaxTreeToParent(childs, st);
            SyntaxTree* ordst = createSyntaxTree(startPos.parsePos, childs);
            ordst->chooseNum = i;
            return ordst;
        }
        if (itr.hasMore())
            m_back(startPos);
    }
    m_fail(startPos);
    return m_PARSE_FAILED;
}
예제 #5
0
SyntaxTree* OperatorParser::parse(void)
{
    HMD_ASSERT(m_parser);
    m_ErrorPos startPos = m_curErrPos();
    SyntaxTree* st = m_parser->parse();
    const char* n = name();
    if (st->isError()) {
        m_fail(startPos);
        if (! st->isFatalError()) {
            char pbuf[128];
            gpInp->sprintSourceInfo(pbuf, 128, startPos.parsePos);
            HMD_PRINTF("%s %s -> ERROR %d\n", n, pbuf, st->errorCutId);
        }
        return m_FATAL_PARSER_ERROR;
    }
    if (st->isFail()) {
        if (m_printIntermediateLevel > 2) {
            char pbuf[128];
            gpInp->sprintSourceInfo(pbuf, 128, startPos.parsePos);
            HMD_PRINTF("%s %s -> fail\n", n, pbuf);
        }
        m_fail(startPos);
        return m_PARSE_FAILED;
    }

    if (m_printIntermediateLevel > 1) {
        char pbuf[128];
        gpInp->sprintSourceInfo(pbuf, 128, startPos.parsePos);
        if (st->isValidTree()) {
            char b[44];
            gpInp->copySummary(b, 40, st->str);
            HMD_PRINTF("%s %s -> '%s'\n", n, pbuf, b);
        } else {
            HMD_PRINTF("%s %s -> (notree)\n", n, pbuf);
        }
    }
        
#if 0
    if (st->isValidTree()) {
        if (st->numChild() == 1) {
            // no operator, so that this node is not needed
            SyntaxTree* tmp = st->replace(0, NULL);
            if (! m_bUseMemoize)
                delete st;
            return tmp;
        }
    }
#endif
    return st;
}
예제 #6
0
SyntaxTree* Parser::m_parse_op_prefix(Parser* my, Parser* exp, Parser* op, bool allowRepeat)
{
    m_ErrorPos startPos = m_curErrPos();
    SyntaxTree::Childs* firstChilds = new SyntaxTree::Childs(2);
    firstChilds->setContentsMemID("pcpr");
    SyntaxTree::Childs* lastChilds = firstChilds;
    for (;;) {
        m_ErrorPos midPos = m_curErrPos();
        SyntaxTree* sto = op->parse();
        if (sto->isError()) {
            m_fail(startPos);
            SyntaxTree::Childs::deleteRecursively(firstChilds);
            return sto;
        }
        if (sto->isFail()) {
            m_back(midPos);
            break;
        } else {
            if (lastChilds->size() > 0) {
                SyntaxTree::Childs* newChilds = new SyntaxTree::Childs(2);
                newChilds->setContentsMemID("pcpr");
                lastChilds->add(my->createSyntaxTree(m_curPos(), newChilds));
                lastChilds = newChilds;
            }
            op->addSyntaxTreeToParent(lastChilds, sto);
            if (! allowRepeat)
                break;
        }
    }
    SyntaxTree* ste = exp->parse();
    if (ste->isFail()) {
        m_fail(startPos);
        SyntaxTree::Childs::deleteRecursively(firstChilds);
        return ste;
    }

    if (lastChilds->size() == 0) {
        // no op
        SyntaxTree::Childs::deleteRecursively(firstChilds);
        return ste;
    }

    lastChilds->add(ste);
    return my->createSyntaxTree(startPos.parsePos, firstChilds);
}