Hooked up punctuation detection.
This commit is contained in:
+45
-9
@@ -9,12 +9,14 @@ int Line = 0;
|
||||
int Position = 0;
|
||||
int SourceLength = 0;
|
||||
int ScannerAtEnd(void);
|
||||
int IsPunctuation(char);
|
||||
char PeekScanner(void);
|
||||
char AdvanceScanner(void);
|
||||
void AdvanceScanner(void);
|
||||
Token* ParseString(void);
|
||||
Token* ParseDirective(void);
|
||||
Token* ParseIdentifier(void);
|
||||
Token* ParseNumber(void);
|
||||
Token* ParsePunctuation(char);
|
||||
void IgnoreLine(void);
|
||||
|
||||
List* GenerateTokenList(const char* source) {
|
||||
@@ -50,16 +52,19 @@ List* GenerateTokenList(const char* source) {
|
||||
token = ParseString();
|
||||
if (token) AddListItem(token, sizeof(Token), tokens);
|
||||
break;
|
||||
case ',':
|
||||
AdvanceScanner();
|
||||
AddListItem(CreateToken(COMMA, ","), sizeof(Token), tokens);
|
||||
break;
|
||||
default:
|
||||
if (c >= '0' && c <= '9') {
|
||||
ParseNumber();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (IsPunctuation(c)) {
|
||||
AdvanceScanner();
|
||||
AddListItem(ParsePunctuation(c), sizeof(Token), tokens);
|
||||
break;
|
||||
}
|
||||
|
||||
token = ParseIdentifier();
|
||||
|
||||
if (token) AddListItem(token, sizeof(Token), tokens);
|
||||
@@ -152,7 +157,7 @@ Token* ParseString(void) {
|
||||
Token* ParseIdentifier(void) {
|
||||
int start = Position;
|
||||
|
||||
while(!ScannerAtEnd() && PeekScanner() != ',' && PeekScanner() != ' ' && PeekScanner() != '\n') {
|
||||
while(!ScannerAtEnd() && !IsPunctuation(PeekScanner()) && PeekScanner() != ' ' && PeekScanner() != '\n') {
|
||||
AdvanceScanner();
|
||||
}
|
||||
|
||||
@@ -174,12 +179,43 @@ char PeekScanner(void) {
|
||||
return SourceCode[Position];
|
||||
}
|
||||
|
||||
char AdvanceScanner(void) {
|
||||
if (ScannerAtEnd()) return '\0';
|
||||
void AdvanceScanner(void) {
|
||||
if (ScannerAtEnd()) return;
|
||||
|
||||
return SourceCode[Position++];
|
||||
Position++;
|
||||
}
|
||||
|
||||
int ScannerAtEnd(void) {
|
||||
return Position >= SourceLength;
|
||||
}
|
||||
|
||||
int IsPunctuation(char c) {
|
||||
switch(c) {
|
||||
case '[':
|
||||
case ']':
|
||||
case '(':
|
||||
case ')':
|
||||
case ',':
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Token* ParsePunctuation(char c) {
|
||||
switch(c) {
|
||||
case '[':
|
||||
return CreateToken(LBRACKET, "[");
|
||||
case ']':
|
||||
return CreateToken(RBracket, "]");
|
||||
case '(':
|
||||
return CreateToken(LPARAM, "(");
|
||||
case ')':
|
||||
return CreateToken(RPARAM, ")");
|
||||
case ',':
|
||||
return CreateToken(COMMA, ",");
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user