From af0c1f0a81cae312206c2d0e17b354af192c1184 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 11 Aug 2013 01:15:26 -0400 Subject: [PATCH] Fixed some more DOT generation stuff. --- include/NodeTree.h | 6 +++--- include/util.h | 4 ++-- src/RNGLRParser.cpp | 6 ++---- src/util.cpp | 17 +++++++++++++++-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/NodeTree.h b/include/NodeTree.h index f11fd16..dadf998 100644 --- a/include/NodeTree.h +++ b/include/NodeTree.h @@ -225,10 +225,10 @@ template std::string NodeTree::getDOTName() { std::string DOTName = ""; if (data != NULL) - DOTName = "\"" + name + "-" + data->toString() + "_" + intToString(id) + "\""; //Note that terminals already have a quote in the front of their name, so we don't need to add one + DOTName = "\"" + replaceExEscape(name + "-" + data->toString(), "\"", "\\\"") + "_" + intToString(id) + "\""; //Note that terminals already have a quote in the front of their name, so we don't need to add one else - DOTName = "\"" + name + "_" + intToString(id) + "\""; - return(replace(DOTName, "\n", "\\n")); + DOTName = "\"" + replaceExEscape(name, "\"", " \\\"") + "_" + intToString(id) + "\""; + return(replaceExEscape(DOTName, "\n", "\\n")); } #endif \ No newline at end of file diff --git a/include/util.h b/include/util.h index 80ab9de..2aa779d 100644 --- a/include/util.h +++ b/include/util.h @@ -5,13 +5,13 @@ #define NULL 0 #endif -//#include +#include #include #include std::string intToString(int theInt); std::string truncateEnd(std::string to_truncate); std::string removeBeginning(std::string to_remove); -std::string replace(std::string first, std::string search, std::string replace); +std::string replaceExEscape(std::string first, std::string search, std::string replace); #endif \ No newline at end of file diff --git a/src/RNGLRParser.cpp b/src/RNGLRParser.cpp index 0f3270f..7004e5a 100644 --- a/src/RNGLRParser.cpp +++ b/src/RNGLRParser.cpp @@ -83,8 +83,6 @@ NodeTree* RNGLRParser::parseInput(std::string inputString) { } //Clear the vector of SPPF nodes created every step - // for (std::vector*>::size_type j = 0; j < SPPFStepNodes.size(); j++) - // SPPFStepNodes[j] = NULL; SPPFStepNodes.clear(); while (toReduce.size() != 0) { @@ -244,14 +242,14 @@ void RNGLRParser::addChildren(NodeTree* parent, std::vectoraddChildren(children); } else { if (!arePacked(parent->getChildren())) { - NodeTree* subParent = new NodeTree(); + NodeTree* subParent = new NodeTree("AmbiguityPackInner", NULL); setPacked(subParent, true); std::vector*> tmp = parent->getChildren(); subParent->addChildren(&tmp); parent->clearChildren(); parent->addChild(subParent); } - NodeTree* t = new NodeTree(); + NodeTree* t = new NodeTree("AmbiguityPackOuter", NULL); setPacked(t, true); parent->addChild(t); t->addChildren(children); diff --git a/src/util.cpp b/src/util.cpp index 3553868..5b298b5 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -21,15 +21,28 @@ std::string removeBeginning(std::string to_remove) return to_return; } -std::string replace(std::string first, std::string search, std::string replace) { +std::string replaceExEscape(std::string first, std::string search, std::string replace) { size_t pos = 0; while (pos < first.size()-search.size()) { pos = first.find(search, pos); if (pos == std::string::npos) break; //std::cout << "Position is " << pos << " size of first is " << first.size() << " size of replace is " << replace.size() << std::endl; + //If excaped, don't worry about it. + if (pos > 0) { + int numBackslashes = 0; + int countBack = 1; + while (pos-countBack >= 0 && first[pos-countBack] == '\\') { + numBackslashes++; + countBack++; + } + if (numBackslashes % 2 == 1) { + pos++; + continue; + } + } first = first.replace(pos, search.size(), replace); - pos++; + pos += replace.size(); } return first; }