diff --git a/include/RegEx.h b/include/RegEx.h index 77db781..7a9e7de 100644 --- a/include/RegEx.h +++ b/include/RegEx.h @@ -21,9 +21,10 @@ class RegEx { int longMatch(std::string stringToMatch); std::string getPattern(); std::string toString(); + static void test(); private: std::string pattern; RegExState* begin; std::vector currentStates; }; -#endif \ No newline at end of file +#endif diff --git a/main.cpp b/main.cpp index fb254d1..a6d0fe0 100644 --- a/main.cpp +++ b/main.cpp @@ -19,6 +19,7 @@ int main(int argc, char* argv[]) { if (argc == 2 && std::string(argv[1]) == "--test") { StringReader::test(); + RegEx::test(); Lexer::test(); return 0; } diff --git a/src/Lexer.cpp b/src/Lexer.cpp index 1dcb4da..55b5720 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -40,8 +40,8 @@ Symbol Lexer::next() { } } if (longestRegEx != NULL) { - std::string eatenString = input.substr(currentPosition, longestMatch+1); - currentPosition += longestMatch + 1; + std::string eatenString = input.substr(currentPosition, longestMatch); + currentPosition += longestMatch; //std::cout << "Current at is \"" << input.substr(currentPosition,input.length()-1) << "\" currentPos is " << currentPosition <getPattern(), true, eatenString); } else { diff --git a/src/RegEx.cpp b/src/RegEx.cpp index 293acbe..7dd309f 100644 --- a/src/RegEx.cpp +++ b/src/RegEx.cpp @@ -1,4 +1,5 @@ #include "RegEx.h" +#include RegEx::RegEx(std::string inPattern) { pattern = inPattern; @@ -242,7 +243,7 @@ int RegEx::longMatch(std::string stringToMatch) { //Also, add each state's advance to nextStates for (std::vector::size_type j = 0; j < currentStates.size(); j++) { if (currentStates[j]->isGoal()) { - lastMatch = i-1; + lastMatch = i; //std::cout << "Hit goal at " << i << " character: " << stringToMatch[i-1] << std::endl; } else { //std::cout << "currentState " << j << ", " << currentStates[j]->toString() << " is not goal" <::size_type j = 0; j < currentStates.size(); j++) { if (currentStates[j]->isGoal()) - lastMatch = stringToMatch.size()-1; + lastMatch = stringToMatch.size(); } return lastMatch; } @@ -286,3 +287,12 @@ std::string RegEx::getPattern() { std::string RegEx::toString() { return pattern + " -> " + begin->toString(); } + +void RegEx::test() { + { + RegEx re("a*"); + assert(re.longMatch("aa") == 2); + } + + std::cout << "RegEx tests pass\n"; +}