static int ScanIdentifier() { unsigned char* start = CURSOR; if('L' == *CURSOR) { if('\'' == CURSOR[1]) { return ScanCharLiteral(); } else if('\"' == CURSOR[1]) { return ScanStringLiteral(); } } CURSOR++; while(IsLetterOrDigit(*CURSOR)) { CURSOR++; } return FindKeyword((char *)start, (int)(CURSOR - start)); }
/* 标示符扫描函数 */ static int ScanIdentifierOR (int def) { unsigned char *start = CURRENT; int tok; /* 处理宽字符或宽字符串 */ if ('L' == *CURRENT) { /* 处理字符 */ if ('\'' == CURRENT[1]) return ScanCharLiteral (); /* 处理宽字符串 */ if ('"' == CURRENT[1]) return ScanStringLiteral (); } if ( !IsLetterOrDigit (*CURRENT) ) return TK_BEGIN; CURRENT++; /* 字母和数字下画线都符合标示符的要求 */ while (IsLetterOrDigit (*CURRENT)) CURRENT++; /* 判断是否是关键字 */ tok = FindKeyword ((char*)start, (int)(CURRENT - start)); if (TK_ID == tok) { Symbol p; /* 将字符串的值保存在字符串池中 */ TokenValue.p = InternStr ((char*)start, (int)(CURRENT - start)); // 该符号是define if (!def && (p = LookupDefine (TokenValue.p))) { LoadDefine (p); return GetNextToken (); } } else TokenValue.p = TokenString[tok]; return tok; }
static struct Token matchIdentifier(void) { char *start = CURSOR; CURSOR += 1; while (IsLetterOrDigit(*CURSOR)) { CURSOR += 1; } struct Token token; token.type = TOKEN_ID; char *pTokenVar = (char*)malloc(CURSOR - start); strncpy(pTokenVar,start,CURSOR - start); token.var.p = (void*)pTokenVar; return token; }