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; }
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; }
void Parser::m_reduceSyntaxTreeToParent(SyntaxTree::Childs* arr, SyntaxTree* st) { if (! st->isValidTree()) return; for (TArrayIterator<SyntaxTree*> itr(st->childs); itr.hasMore(); ) { SyntaxTree* t = itr.next(); if (t->isValidTree()) { t->parser->addSyntaxTreeToParent(arr, t); } } if (! m_bUseMemoize) { // no more need if (st->childs != NULL) delete st->childs; delete st; } }