void SCsTranslator::dumpNode(pANTLR3_BASE_TREE node, std::ofstream &stream) { StringStream ss; ss << node; String s_root = ss.str().substr(3); pANTLR3_COMMON_TOKEN tok = node->getToken(node); if (tok) { stream << s_root << " [shape=box];" << std::endl; String label((const char*) node->getText(node)->chars); std::replace(label.begin(), label.end(), '"', '\''); stream << s_root << " [label=\"" << label << "\"];" << std::endl; } else stream << s_root << " [shape=circle];" << std::endl; uint32 n = node->getChildCount(node); for (uint32 i = 0; i < n; ++i) { pANTLR3_BASE_TREE child = (pANTLR3_BASE_TREE) node->getChild(node, i); StringStream s1; s1 << child; stream << s_root << " -> " << s1.str().substr(3) << ";" << std::endl; dumpNode(child, stream); } }
std::string MySQLRecognitionBase::dumpTree(pANTLR3_UINT8 *tokenNames, pANTLR3_BASE_TREE tree, const std::string &indentation) { std::string result; ANTLR3_UINT32 char_pos = tree->getCharPositionInLine(tree); ANTLR3_UINT32 line = tree->getLine(tree); pANTLR3_STRING token_text = tree->getText(tree); pANTLR3_COMMON_TOKEN token = tree->getToken(tree); const char* utf8 = (const char*)token_text->chars; if (token != NULL) { ANTLR3_UINT32 token_type = token->getType(token); pANTLR3_UINT8 token_name; if (token_type == EOF) token_name = (pANTLR3_UINT8)"EOF"; else token_name = tokenNames[token_type]; #ifdef ANTLR3_USE_64BIT result = base::strfmt("%s(line: %i, offset: %i, length: %" PRId64 ", index: %" PRId64 ", %s[%i]) %s\n", indentation.c_str(), line, char_pos, token->stop - token->start + 1, token->index, token_name, token_type, utf8); #else result = base::strfmt("%s(line: %i, offset: %i, length: %i, index: %i, %s[%i]) %s\n", indentation.c_str(), line, char_pos, token->stop - token->start + 1, token->index, token_name, token_type, utf8); #endif } else { result = base::strfmt("%s(line: %i, offset: %i, nil) %s\n", indentation.c_str(), line, char_pos, utf8); } for (ANTLR3_UINT32 index = 0; index < tree->getChildCount(tree); index++) { pANTLR3_BASE_TREE child = (pANTLR3_BASE_TREE)tree->getChild(tree, index); std::string child_text = dumpTree(tokenNames, child, indentation + "\t"); result += child_text; } return result; }
static pANTLR3_STRING getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t) { return t->getText(t); }
/** * Returns the text of the given node. The result depends on the type of the node. If it represents * a quoted text entity then two consecutive quote chars are replaced by a single one and if * escape sequence parsing is not switched off (as per sql mode) then such sequences are handled too. */ std::string MySQLRecognitionBase::token_text(pANTLR3_BASE_TREE node, bool keepQuotes) { pANTLR3_STRING text = node->getText(node); if (text == NULL) return ""; std::string chars; pANTLR3_COMMON_TOKEN token = node->getToken(node); ANTLR3_UINT32 type = (token != NULL) ? token->type : ANTLR3_TOKEN_INVALID; if (type == STRING_TOKEN) { // STRING is the grouping subtree for multiple consecutive string literals, which // must be concatenated. for (ANTLR3_UINT32 index = 0; index < node->getChildCount(node); index++) { pANTLR3_BASE_TREE child = (pANTLR3_BASE_TREE)node->getChild(node, index); chars += token_text(child, keepQuotes); } return chars; } chars = (const char*)text->chars; std::string quote_char; switch (type) { case BACK_TICK_QUOTED_ID: quote_char = "`"; break; case SINGLE_QUOTED_TEXT: quote_char = "'"; break; case DOUBLE_QUOTED_TEXT: quote_char = "\""; break; default: return chars; } // First unquote then handle escape squences and double quotes. if (chars.size() < 3) { if (keepQuotes) return chars; return ""; // Also handles an invalid single quote char gracefully. } // Need to unquote even if keepQuotes is true to avoid trouble with replacing the outer quotes. // Add them back below. chars = base::unquote(chars); if ((d->_sql_mode & SQL_MODE_NO_BACKSLASH_ESCAPES) == 0) chars = base::unescape_sql_string(chars, quote_char[0]); else if (token->user1 > 0) { // The field user1 is set by the parser to the number of quote char pairs it found. // So we can use it to shortcut our handling here. base::replace(chars, quote_char + quote_char, quote_char); } if (keepQuotes) return quote_char + chars + quote_char; return chars; }
/// 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 } }