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