2013-06-13 14:25:10 -04:00
|
|
|
#include "Lexer.h"
|
|
|
|
|
|
|
|
|
|
Lexer::Lexer() {
|
|
|
|
|
//Do nothing
|
2013-07-02 01:47:42 -04:00
|
|
|
currentPosition = 0;
|
2013-06-13 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Lexer::Lexer(std::string inputString) {
|
2013-07-01 22:45:33 -04:00
|
|
|
input = inputString;
|
|
|
|
|
currentPosition = 0;
|
2013-06-13 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Lexer::~Lexer() {
|
|
|
|
|
//No cleanup necessary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Lexer::setInput(std::string inputString) {
|
2013-07-01 22:45:33 -04:00
|
|
|
input = inputString;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-02 01:47:42 -04:00
|
|
|
void Lexer::addRegEx(std::string regExString) {
|
2013-07-01 22:45:33 -04:00
|
|
|
regExs.push_back(new RegEx(regExString));
|
2013-06-13 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol* Lexer::next() {
|
2013-07-02 01:47:42 -04:00
|
|
|
std::cout << "Current at is " << input.substr(currentPosition,input.length()-1) << " currentPos is " << currentPosition <<std::endl;
|
|
|
|
|
//If we're at the end, return an eof
|
|
|
|
|
if (currentPosition == input.length()-1)
|
|
|
|
|
return new Symbol("$EOF$", false);
|
2013-07-01 22:45:33 -04:00
|
|
|
int longestMatch = 0;
|
2013-07-02 01:47:42 -04:00
|
|
|
RegEx* longestRegEx = NULL;
|
2013-07-01 22:45:33 -04:00
|
|
|
std::string remainingString = input.substr(currentPosition,input.length()-1);
|
|
|
|
|
for (std::vector<RegEx*>::size_type i = 0; i < regExs.size(); i++) {
|
2013-07-02 01:47:42 -04:00
|
|
|
std::cout << "Trying regex " << regExs[i]->toString() << std::endl;
|
2013-07-01 22:45:33 -04:00
|
|
|
int currentMatch = regExs[i]->longMatch(remainingString);
|
|
|
|
|
if (currentMatch > longestMatch) {
|
|
|
|
|
longestMatch = currentMatch;
|
|
|
|
|
longestRegEx = regExs[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-02 01:47:42 -04:00
|
|
|
if (longestRegEx != NULL) {
|
|
|
|
|
currentPosition += longestMatch + 1;
|
|
|
|
|
std::cout << "Current at is " << input.substr(currentPosition,input.length()-1) << " currentPos is " << currentPosition <<std::endl;
|
|
|
|
|
return new Symbol(longestRegEx->getPattern(), true);
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "Found no applicable regex" << std::endl;
|
|
|
|
|
std::cout << "Remaining is " << input.substr(currentPosition,input.length()-1) << std::endl;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-06-13 14:25:10 -04:00
|
|
|
}
|