Example #1
0
SyntaxTree* Parser::m_parse_sequence(TArray<Parser*>* ps)
{
    m_ErrorPos startPos = m_curErrPos();
    SyntaxTree::Childs* childs = new SyntaxTree::Childs(ps->size());
    childs->setContentsMemID("pcsq");
    int errorCutId = 0;
    hyu32 errorCutPos = 0;

    for (TArrayIterator<Parser*> itr(ps); itr.hasMore(); ) {
        Parser* p = itr.next();
        SyntaxTree* st = p->parse();
        if (st->isErrorCut()) {
            errorCutId = st->errorCutId;
            errorCutPos = st->str.startPos;
            delete st;
        } else if (! st->isFail()) {
            p->addSyntaxTreeToParent(childs, st);
        } else {
            if (st->isFailNotError() && errorCutId > 0) {
                SyntaxTree* e = new SyntaxTree(errorCutPos, errorCutId, this);
                if (! m_bUseMemoize)
                    delete st;
                st = e;
            }
            m_fail(startPos);
            if (! m_bUseMemoize) {
                SyntaxTree::Childs::deleteRecursively(childs);
            } else {
                delete childs;
            }
            return st;
        }
    }
    return createSyntaxTree(startPos.parsePos, childs);
}
Example #2
0
SyntaxTree* UserParser::m_parse1(m_ErrorPos startPos)
{
    const char* n = name();
    SyntaxTree* st;
        
    if (m_bUseMemoize) {
        st = m_memo.getAt(startPos.parsePos);
        if (st == m_PARSING) {
            // left recursion
            char pbuf[128];
            gpInp->sprintSourceInfo(pbuf, 128, startPos.parsePos);
            HMD_PRINTF("LEFT RECURSION DETECTED: %s %s\n", n, pbuf);
            return m_FATAL_PARSER_ERROR;
        } else if (st != m_NOT_PARSED_YET) {
            if (st->isValidTree())
                gpInp->setPos(st->str.endPos);
            if (m_printIntermediateLevel > 1 ||
                (m_printIntermediateLevel > 0 && !st->isFail())) {
                char pbuf[128];
                gpInp->sprintSourceInfo(pbuf, 128, startPos.parsePos);
                HMD_PRINTF("%s %s -> memoized ", n, pbuf);
                if (st->isValidTree()) {
                    char b[44];
                    gpInp->copySummary(b, 40, st->str);
                    HMD_PRINTF("pos=%d-%d '%s'\n", st->str.startPos, st->str.endPos, b);
                } else {
                    if (st == m_NO_SYNTAX_TREE)
                        HMD_PRINTF("no syntax tree\n");
                    else if (st == m_PARSE_FAILED)
                        HMD_PRINTF("parse failed\n");
                    else if (st->isErrorCut())
                        HMD_PRINTF("ErrorCut(%d)\n",st->errorCutId);
                    else
                        HMD_PRINTF("(UNKNOWN BUG?)\n");
                }
            }
            return st;
        }

        // not parsed yet
        m_memo.setAt(startPos.parsePos, m_PARSING);
    }
        
    if (m_printIntermediateLevel > 1) {
        char pbuf[128];
        gpInp->sprintSourceInfo(pbuf, 128, startPos.parsePos);
        HMD_PRINTF("try %s %s pos=%d:\n", n, pbuf, startPos.parsePos);
    }

    return NULL;
}