static pANTLR3_STRING toStringTree (pANTLR3_BASE_TREE tree) { pANTLR3_STRING string; ANTLR3_UINT32 i; ANTLR3_UINT32 n; pANTLR3_BASE_TREE t; if (tree->children == NULL || tree->children->size(tree->children) == 0) { return tree->toString(tree); } /* Need a new string with nothing at all in it. */ string = tree->strFactory->newRaw(tree->strFactory); if (tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8 (string, "("); string->appendS (string, tree->toString(tree)); string->append8 (string, " "); } if (tree->children != NULL) { n = tree->children->size(tree->children); for (i = 0; i < n; i++) { t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i); if (i > 0) { string->append8(string, " "); } string->appendS(string, t->toStringTree(t)); } } if (tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8(string,")"); } return string; }
static void toStringWork (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE p, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf) { ANTLR3_UINT32 n; ANTLR3_UINT32 c; if (!p->isNilNode(p) ) { pANTLR3_STRING text; text = p->toString(p); if (text == NULL) { text = tns->ctns->stringFactory->newRaw(tns->ctns->stringFactory); text->addc (text, ' '); text->addi (text, p->getType(p)); } buf->appendS(buf, text); } if (p == stop) { return; /* Finished */ } n = p->getChildCount(p); if (n > 0 && ! p->isNilNode(p) ) { buf->addc (buf, ' '); buf->addi (buf, ANTLR3_TOKEN_DOWN); } for (c = 0; c<n ; c++) { pANTLR3_BASE_TREE child; child = p->getChild(p, c); tns->toStringWork(tns, child, stop, buf); } if (n > 0 && ! p->isNilNode(p) ) { buf->addc (buf, ' '); buf->addi (buf, ANTLR3_TOKEN_UP); } }
pANTLR3_STRING emerson_printAST(pANTLR3_BASE_TREE tree, pANTLR3_UINT8* parserTokenNames) { pANTLR3_STRING string; ANTLR3_UINT32 i; ANTLR3_UINT32 n; pANTLR3_BASE_TREE t; if(tree->children == NULL || tree->children->size(tree->children) == 0) { return tree->toString(tree); } // THis is how you get a new string. The string is blank string = tree->strFactory->newRaw(tree->strFactory); if(tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8 (string, "("); pANTLR3_COMMON_TOKEN token = tree->getToken(tree); ANTLR3_UINT32 type = token->type; string->append(string, (const char*)parserTokenNames[type]); string->append8(string, " "); } if(tree->children != NULL) { n = tree->children->size(tree->children); for (i = 0; i < n; i++) { t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i); if (i > 0) { string->append8(string, " "); } string->appendS(string, emerson_printAST(t,parserTokenNames)); } } if(tree->isNilNode(tree) == ANTLR3_FALSE) { string->append8(string,")"); } return string; }
static pANTLR3_STRING getText (pANTLR3_BASE_TREE tree) { return tree->toString(tree); }