Fixed some bugs, including modifing StringReader to treat quoted strings as whole words.
This commit is contained in:
@@ -11,6 +11,7 @@ class RegExState {
|
|||||||
public:
|
public:
|
||||||
RegExState(RegExState* inInnerState);
|
RegExState(RegExState* inInnerState);
|
||||||
RegExState(char inCharacter);
|
RegExState(char inCharacter);
|
||||||
|
RegExState();
|
||||||
|
|
||||||
~RegExState();
|
~RegExState();
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ Symbol* Lexer::next() {
|
|||||||
//If we're at the end, return an eof
|
//If we're at the end, return an eof
|
||||||
if (currentPosition == input.length()-1)
|
if (currentPosition == input.length()-1)
|
||||||
return new Symbol("$EOF$", false);
|
return new Symbol("$EOF$", false);
|
||||||
int longestMatch = 0;
|
int longestMatch = -1;
|
||||||
RegEx* longestRegEx = NULL;
|
RegEx* longestRegEx = NULL;
|
||||||
std::string remainingString = input.substr(currentPosition,input.length()-1);
|
std::string remainingString = input.substr(currentPosition,input.length()-1);
|
||||||
for (std::vector<RegEx*>::size_type i = 0; i < regExs.size(); i++) {
|
for (std::vector<RegEx*>::size_type i = 0; i < regExs.size(); i++) {
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
RegEx::RegEx(std::string inPattern) {
|
RegEx::RegEx(std::string inPattern) {
|
||||||
pattern = inPattern;
|
pattern = inPattern;
|
||||||
RegExState* current;
|
RegExState* current;
|
||||||
begin = new RegExState(pattern[0]);
|
begin = new RegExState();
|
||||||
current = begin;
|
current = begin;
|
||||||
for (int i = 1; i < pattern.length(); i++) {
|
for (int i = 0; i < pattern.length(); i++) {
|
||||||
RegExState* next = new RegExState(pattern.at(i));
|
RegExState* next = new RegExState(pattern[i]);
|
||||||
current->addNext(next);
|
current->addNext(next);
|
||||||
current = next;
|
current = next;
|
||||||
}
|
}
|
||||||
@@ -18,14 +18,11 @@ RegEx::~RegEx() {
|
|||||||
|
|
||||||
int RegEx::longMatch(std::string stringToMatch) {
|
int RegEx::longMatch(std::string stringToMatch) {
|
||||||
//If the beginning character is wrong, exit immediantly. Otherwise, get all the states we can get from adding the second character to the state where we accepted the first
|
//If the beginning character is wrong, exit immediantly. Otherwise, get all the states we can get from adding the second character to the state where we accepted the first
|
||||||
if (!begin->characterIs(stringToMatch[0]))
|
int lastMatch = -1;
|
||||||
return -1;
|
currentStates = *(begin->advance(stringToMatch[0]));
|
||||||
std::cout << "Matched first character: " << stringToMatch[0] << std::endl;
|
|
||||||
int lastMatch = 0;
|
|
||||||
currentStates = *(begin->advance(stringToMatch[1]));
|
|
||||||
std::vector<RegExState*> nextStates;
|
std::vector<RegExState*> nextStates;
|
||||||
|
|
||||||
for (int i = 2; i < stringToMatch.size(); i++) {
|
for (int i = 1; i < stringToMatch.size(); i++) {
|
||||||
//Go through every current state. Check to see if it is goal, if so update last goal.
|
//Go through every current state. Check to see if it is goal, if so update last goal.
|
||||||
//Also, add each state's advance to nextStates
|
//Also, add each state's advance to nextStates
|
||||||
for (std::vector<RegExState*>::size_type j = 0; j < currentStates.size(); j++) {
|
for (std::vector<RegExState*>::size_type j = 0; j < currentStates.size(); j++) {
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ RegExState::RegExState(char inCharacter) {
|
|||||||
inner = NULL;
|
inner = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RegExState::RegExState() {
|
||||||
|
character = 0;
|
||||||
|
inner = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
RegExState::~RegExState() {
|
RegExState::~RegExState() {
|
||||||
//No cleanup necessary
|
//No cleanup necessary
|
||||||
}
|
}
|
||||||
@@ -36,7 +41,7 @@ bool RegExState::isGoal() {
|
|||||||
|
|
||||||
std::string RegExState::toString() {
|
std::string RegExState::toString() {
|
||||||
std::string string = "";
|
std::string string = "";
|
||||||
string += character;
|
string += std::string("\"") + character + "\"";
|
||||||
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++)
|
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++)
|
||||||
string += "->" + nextStates[i]->toString() + " EC ";
|
string += "->" + nextStates[i]->toString() + " EC ";
|
||||||
//std::cout << "inner = " << inner << " nextStates size = " << nextStates.size() <<std::endl;
|
//std::cout << "inner = " << inner << " nextStates size = " << nextStates.size() <<std::endl;
|
||||||
|
|||||||
@@ -66,6 +66,10 @@ std::string StringReader::getTokens(std::vector<std::string> stop_chars, bool tr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rd_string[str_pos] == '\"') {
|
||||||
|
found_pos = rd_string.find("\"", str_pos+1);
|
||||||
|
}
|
||||||
|
|
||||||
if (found_pos == str_pos) //We are at the endline
|
if (found_pos == str_pos) //We are at the endline
|
||||||
{
|
{
|
||||||
str_pos++;
|
str_pos++;
|
||||||
@@ -78,16 +82,25 @@ std::string StringReader::getTokens(std::vector<std::string> stop_chars, bool tr
|
|||||||
return "";
|
return "";
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
std::string string_section;
|
|
||||||
|
|
||||||
if (truncateEnd) //If we want to get rid of the delimiting character, which is the default, don't add the last char. Note we have to increase str_pos by one manually later
|
if (truncateEnd) //If we want to get rid of the delimiting character, which is the default, don't add the last char. Note we have to increase str_pos by one manually later
|
||||||
found_pos -= 1;
|
found_pos -= 1;
|
||||||
|
|
||||||
|
if (rd_string[str_pos] == '\"')
|
||||||
|
found_pos++;
|
||||||
|
|
||||||
|
std::string string_section;
|
||||||
|
|
||||||
for (; str_pos <= found_pos; str_pos++)
|
for (; str_pos <= found_pos; str_pos++)
|
||||||
{
|
{
|
||||||
string_section += rd_string[str_pos];
|
string_section += rd_string[str_pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (str_pos <= found_pos) {
|
||||||
|
// string_section = rd_string.substr(str_pos, found_pos+1);
|
||||||
|
// str_pos = found_pos+1;
|
||||||
|
// }
|
||||||
|
// std::cout << string_section << " - " << str_pos << " - " << found_pos << std::endl;
|
||||||
|
|
||||||
if (truncateEnd) //Ok, we didn't add the last char, but str_pos now points at that char. So we move it one ahead.
|
if (truncateEnd) //Ok, we didn't add the last char, but str_pos now points at that char. So we move it one ahead.
|
||||||
str_pos++;
|
str_pos++;
|
||||||
return string_section;
|
return string_section;
|
||||||
|
|||||||
Reference in New Issue
Block a user