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-08-02 15:21:42 -04:00
std : : cout < < " Current at is \" " < < input . substr ( currentPosition , input . length ( ) - 1 ) < < " \" currentPos is " < < currentPosition < < " out of " < < input . length ( ) < < std : : endl ;
2013-07-02 01:47:42 -04:00
//If we're at the end, return an eof
2013-08-02 15:21:42 -04:00
if ( currentPosition > = input . length ( ) - 1 )
2013-07-09 02:45:59 -04:00
return new Symbol ( " $EOF$ " , true ) ;
2013-07-02 13:14:40 -04:00
int longestMatch = - 1 ;
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-08-06 01:49:45 -04:00
//std::cout << "Trying regex " << regExs[i]->getPattern() << 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 ) {
2013-08-06 01:49:45 -04:00
std : : string eatenString = input . substr ( currentPosition , longestMatch + 1 ) ;
2013-07-02 01:47:42 -04:00
currentPosition + = longestMatch + 1 ;
2013-08-06 01:49:45 -04:00
//std::cout << "Current at is \"" << input.substr(currentPosition,input.length()-1) << "\" currentPos is " << currentPosition <<std::endl;
return new Symbol ( longestRegEx - > getPattern ( ) , true , eatenString ) ;
2013-07-02 01:47:42 -04:00
} else {
std : : cout < < " Found no applicable regex " < < std : : endl ;
2013-08-02 15:21:42 -04:00
std : : cout < < " Remaining is || " < < input . substr ( currentPosition , input . length ( ) - 1 ) < < " || " < < std : : endl ;
return NULL ;
2013-07-02 01:47:42 -04:00
}
2013-06-13 14:25:10 -04:00
}