1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
void Lexer::Lex()
{
istringstream Stream(this->Source);
char Current = 0,
Temp = 0;
while(Stream)
{
do
{
if(!Stream.get(Current)) return;
} while(isspace(Current));
switch(Current)
{
//this starts testing for operators
case '<': case '>': case '*':
Stream.get(Temp);
if((Current + Temp + Stream.peek()) == Current + Temp + '=')
{
SymbolTable.PushBack("OPERTATOR", Current + Current + "=", -1, -1);
Stream.putback(Temp);
break;
}
Stream.putback(Temp);
case '/': case '+': case '-':
if(Current == Stream.peek())
{
SymbolTable.PushBack("OPERTATOR", this->ChToS(Current) + this->ChToS(Current), -1, -1);
break;
}
case '=': case '!': case '%': case '^': case '&': case '|':
if(Stream.peek() == '=')
{
SymbolTable.PushBack("OPERATOR", Current + "=", -1, -1);
break;
}
case ';': case '(': case ')': case ',': case '[': case ']': case '{': case '}':
SymbolTable.PushBack("OPERATOR", this->ChToS(Current), -1, -1);
break;
//this ends it.
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
{
string NumberValue;
NumberValue = Current;
while(Stream.get(Current) && isdigit(Current)) NumberValue.push_back(Current);
Stream.putback(Current);
SymbolTable.PushBack("NUMBER", NumberValue, -1, -1);
break;
}
case '\'': case '\"':
{
char Match = Current;
string QuoteValue;
QuoteValue = Current;
while(Stream.get(Current) && Current != Match) QuoteValue.push_back(Current);
QuoteValue.push_back(Current);
SymbolTable.PushBack("STRING", QuoteValue, -1, -1);
}
default:
{
string StringValue;
if(isalpha(Current))
{
StringValue = Current;
while(Stream.get(Current) && isalnum(Current)) StringValue.push_back(Current);
Stream.putback(Current);
if(this->IsKeyword(StringValue)) SymbolTable.PushBack("KEYWORD", StringValue, -1, -1);
else SymbolTable.PushBack("IDENTIFIER", StringValue, -1, -1);
}
}
}
}
}
|