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; }
/** Transform ^(nil x) to x */ static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root) { pANTLR3_BASE_TREE saveRoot; // Keep track of the root we are given. If it is a nilNode, then we // can reuse it rather than orphaning it! // saveRoot = root; if (root != NULL && root->isNilNode(root)) { if (root->getChildCount(root) == 0) { root = NULL; } else if (root->getChildCount(root) == 1) { root = (pANTLR3_BASE_TREE)root->getChild(root, 0); root->setParent(root, NULL); root->setChildIndex(root, -1); // The root we were given was a nil node, wiht one child, which means it has // been abandoned and would be lost in the node factory. However // nodes can be flagged as resuable to prevent this terrible waste // saveRoot->reuse(saveRoot); } } return root; }
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 pANTLR3_STRING toString (pANTLR3_BASE_TREE tree) { if (tree->isNilNode(tree) == ANTLR3_TRUE) { pANTLR3_STRING nilNode; nilNode = tree->strFactory->newPtr(tree->strFactory, (pANTLR3_UINT8)"nil", 3); return nilNode; } return ((pANTLR3_COMMON_TREE)(tree->super))->token->getText(((pANTLR3_COMMON_TREE)(tree->super))->token); }
/** If oldRoot is a nil root, just copy or move the children to newRoot. * If not a nil root, make oldRoot a child of newRoot. * * \code * old=^(nil a b c), new=r yields ^(r a b c) * old=^(a b c), new=r yields ^(r ^(a b c)) * \endcode * * If newRoot is a nil-rooted single child tree, use the single * child as the new root node. * * \code * old=^(nil a b c), new=^(nil r) yields ^(r a b c) * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) * \endcode * * If oldRoot was null, it's ok, just return newRoot (even if isNilNode). * * \code * old=null, new=r yields r * old=null, new=^(nil r) yields ^(nil r) * \endcode * * Return newRoot. Throw an exception if newRoot is not a * simple node or nil root with a single child node--it must be a root * node. If newRoot is <code>^(nil x)</endcode> return x as newRoot. * * Be advised that it's ok for newRoot to point at oldRoot's * children; i.e., you don't have to copy the list. We are * constructing these nodes so we should have this control for * efficiency. */ static pANTLR3_BASE_TREE becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree) { /* Protect against tree rewrites if we are in some sort of error * state, but have tried to recover. In C we can end up with a null pointer * for a tree that was not produced. */ if (newRootTree == NULL) { return oldRootTree; } /* root is just the new tree as is if there is no * current root tree. */ if (oldRootTree == NULL) { return newRootTree; } /* Produce ^(nil real-node) */ if (newRootTree->isNilNode(newRootTree)) { if (newRootTree->getChildCount(newRootTree) > 1) { /* TODO: Handle tree exceptions */ ANTLR3_FPRINTF(stderr, "More than one node as root! TODO: Create tree exception hndling\n"); return newRootTree; } /* The new root is the first child */ newRootTree = newRootTree->getChild(newRootTree, 0); } /* Add old root into new root. addChild takes care of the case where oldRoot * is a flat list (nill rooted tree). All children of oldroot are added to * new root. */ newRootTree->addChild(newRootTree, oldRootTree); /* Always returns new root structure */ return newRootTree; }
/** Transform ^(nil x) to x */ static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root) { if (root != NULL && root->isNilNode(root)) { if (root->getChildCount(root) == 0) { root = NULL; } else if (root->getChildCount(root) == 1) { root = root->getChild(root, 0); root->setParent(root, NULL); root->setChildIndex(root, -1); } } return root; }
static ANTLR3_BOOLEAN isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) { return t->isNilNode(t); }
/** If oldRoot is a nil root, just copy or move the children to newRoot. * If not a nil root, make oldRoot a child of newRoot. * * \code * old=^(nil a b c), new=r yields ^(r a b c) * old=^(a b c), new=r yields ^(r ^(a b c)) * \endcode * * If newRoot is a nil-rooted single child tree, use the single * child as the new root node. * * \code * old=^(nil a b c), new=^(nil r) yields ^(r a b c) * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) * \endcode * * If oldRoot was null, it's ok, just return newRoot (even if isNilNode). * * \code * old=null, new=r yields r * old=null, new=^(nil r) yields ^(nil r) * \endcode * * Return newRoot. Throw an exception if newRoot is not a * simple node or nil root with a single child node--it must be a root * node. If newRoot is <code>^(nil x)</endcode> return x as newRoot. * * Be advised that it's ok for newRoot to point at oldRoot's * children; i.e., you don't have to copy the list. We are * constructing these nodes so we should have this control for * efficiency. */ static pANTLR3_BASE_TREE becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree) { pANTLR3_BASE_TREE saveRoot; /* Protect against tree rewrites if we are in some sort of error * state, but have tried to recover. In C we can end up with a null pointer * for a tree that was not produced. */ if (newRootTree == NULL) { return oldRootTree; } /* root is just the new tree as is if there is no * current root tree. */ if (oldRootTree == NULL) { return newRootTree; } /* Produce ^(nil real-node) */ if (newRootTree->isNilNode(newRootTree)) { if (newRootTree->getChildCount(newRootTree) > 1) { /* TODO: Handle tree exceptions */ ANTLR3_FPRINTF(stderr, "More than one node as root! TODO: Create tree exception handling\n"); return newRootTree; } /* The new root is the first child, keep track of the original newRoot * because if it was a Nil Node, then we can reuse it now. */ saveRoot = newRootTree; newRootTree = (pANTLR3_BASE_TREE)newRootTree->getChild(newRootTree, 0); // Reclaim the old nilNode() // saveRoot->reuse(saveRoot); } /* Add old root into new root. addChild takes care of the case where oldRoot * is a flat list (nill rooted tree). All children of oldroot are added to * new root. */ newRootTree->addChild(newRootTree, oldRootTree); // If the oldroot tree was a nil node, then we know at this point // it has become orphaned by the rewrite logic, so we tell it to do // whatever it needs to do to be reused. // if (oldRootTree->isNilNode(oldRootTree)) { // We have taken an old Root Tree and appended all its children to the new // root. In addition though it was a nil node, which means the generated code // will not reuse it again, so we will reclaim it here. First we want to zero out // any pointers it was carrying around. We are just the baseTree handler so we // don't know necessarilly know how to do this for the real node, we just ask the tree itself // to do it. // oldRootTree->reuse(oldRootTree); } /* Always returns new root structure */ return newRootTree; }
/// Delete children from start to stop and replace with t even if t is /// a list (nil-root tree). Num of children can increase or decrease. /// For huge child lists, inserting children can force walking rest of /// children to set their child index; could be slow. /// static void replaceChildren (pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE newTree) { ANTLR3_INT32 replacingHowMany; // How many nodes will go away ANTLR3_INT32 replacingWithHowMany; // How many nodes will replace them ANTLR3_INT32 numNewChildren; // Tracking variable ANTLR3_INT32 delta; // Difference in new vs existing count ANTLR3_INT32 i; ANTLR3_INT32 j; pANTLR3_VECTOR newChildren; // Iterator for whatever we are going to add in ANTLR3_BOOLEAN freeNewChildren; // Whether we created the iterator locally or reused it if (parent->children == NULL) { ANTLR3_FPRINTF(stderr, "replaceChildren call: Indexes are invalid; no children in list for %s", parent->getText(parent)->chars); return; } // Either use the existing list of children in the supplied nil node, or build a vector of the // tree we were given if it is not a nil node, then we treat both situations exactly the same // if (newTree->isNilNode(newTree)) { newChildren = newTree->children; freeNewChildren = ANTLR3_FALSE; // We must NO free this memory } else { newChildren = antlr3VectorNew(1); if (newChildren == NULL) { ANTLR3_FPRINTF(stderr, "replaceChildren: out of memory!!"); exit(1); } newChildren->add(newChildren, (void *)newTree, NULL); freeNewChildren = ANTLR3_TRUE; // We must free this memory } // Initialize // replacingHowMany = stopChildIndex - startChildIndex + 1; replacingWithHowMany = newChildren->size(newChildren); delta = replacingHowMany - replacingWithHowMany; numNewChildren = newChildren->size(newChildren); // If it is the same number of nodes, then do a direct replacement // if (delta == 0) { pANTLR3_BASE_TREE child; // Same number of nodes // j = 0; for (i = startChildIndex; i <= stopChildIndex; i++) { child = (pANTLR3_BASE_TREE) newChildren->get(newChildren, j); parent->children->set(parent->children, i, child, NULL, ANTLR3_FALSE); child->setParent(child, parent); child->setChildIndex(child, i); } } else if (delta > 0) { ANTLR3_UINT32 indexToDelete; // Less nodes than there were before // reuse what we have then delete the rest // for (j = 0; j < numNewChildren; j++) { parent->children->set(parent->children, startChildIndex + j, newChildren->get(newChildren, j), NULL, ANTLR3_FALSE); } // We just delete the same index position until done // indexToDelete = startChildIndex + numNewChildren; for (j = indexToDelete; j <= (ANTLR3_INT32)stopChildIndex; j++) { parent->children->remove(parent->children, indexToDelete); } parent->freshenPACIndexes(parent, startChildIndex); } else { ANTLR3_UINT32 numToInsert; // More nodes than there were before // Use what we can, then start adding // for (j = 0; j < replacingHowMany; j++) { parent->children->set(parent->children, startChildIndex + j, newChildren->get(newChildren, j), NULL, ANTLR3_FALSE); } numToInsert = replacingWithHowMany - replacingHowMany; for (j = replacingHowMany; j < replacingWithHowMany; j++) { parent->children->add(parent->children, newChildren->get(newChildren, j), NULL); } parent->freshenPACIndexes(parent, startChildIndex); } if (freeNewChildren == ANTLR3_TRUE) { ANTLR3_FREE(newChildren->elements); newChildren->elements = NULL; newChildren->size = 0; ANTLR3_FREE(newChildren); // Will not free the nodes } }
void addChild (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE child) { ANTLR3_UINT32 n; ANTLR3_UINT32 i; if (child == NULL) { return; } if (child->isNilNode(child) == ANTLR3_TRUE) { if (child->children != NULL && child->children == tree->children) { // TODO: Change to exception rather than ANTLR3_FPRINTF? // ANTLR3_FPRINTF(stderr, "ANTLR3: An attempt was made to add a child list to itself!\n"); return; } // Add all of the children's children to this list // if (child->children != NULL) { if (tree->children == NULL) { // We are build ing the tree structure here, so we need not // worry about duplication of pointers as the tree node // factory will only clean up each node once. So we just // copy in the child's children pointer as the child is // a nil node (has not root itself). // tree->children = child->children; child->children = NULL; freshenPACIndexesAll(tree); } else { // Need to copy the children // n = child->children->size(child->children); for (i = 0; i < n; i++) { pANTLR3_BASE_TREE entry; entry = child->children->get(child->children, i); // ANTLR3 lists can be sparse, unlike Array Lists // if (entry != NULL) { tree->children->add(tree->children, entry, (void (ANTLR3_CDECL *) (void *))child->free); } } } } } else { // Tree we are adding is not a Nil and might have children to copy // if (tree->children == NULL) { // No children in the tree we are adding to, so create a new list on // the fly to hold them. // tree->createChildrenList(tree); } tree->children->add(tree->children, child, (void (ANTLR3_CDECL *)(void *))child->free); } }