int CubeLexer::IsStringInteger(char *pString) { int Dot=FALSE; if (pString==NULL) { return FALSE; } if (strlen(pString)==0) { return FALSE; } unsigned int CurrentCharIndex; if (!IsCharNumeric(pString[0])&&!(pString[0]==('-'))) { if(pString[0]!='.') return FALSE; } for (CurrentCharIndex=1; CurrentCharIndex<strlen(pString); CurrentCharIndex++) { if (!IsCharNumeric(pString[CurrentCharIndex])) { if (pString[CurrentCharIndex]=='e'&&(pString[CurrentCharIndex+1]=='+'||pString[CurrentCharIndex+1]=='-')) { CurrentCharIndex++; continue; } if (pString[CurrentCharIndex]=='.') { if (Dot) { return FALSE; } else { Dot=TRUE; continue; } } return FALSE; } } return TRUE; }
int IsStringInt(const char* str) { if (!str) return FALSE; if (strlen(str) == 0) return FALSE; for (int i = 0; i < strlen(str); i++) { if (!IsCharNumeric(str[i]) && str[i] != '-') return FALSE; } for (int i = 1; i < strlen(str); i++) { if (str[i] == '-') return FALSE; } return TRUE; }
int IsStringFloat(const char* str) { if (!str) return FALSE; if (strlen(str) == 0) return FALSE; for (int i = 0; i < strlen(str); i++) { if (!IsCharNumeric(str[i]) && str[i] != '-' && str[i] != '.') return FALSE; } int isRadixPointFound = 0; for (int i = 0; i < strlen(str); i++) { if (str[i] == '.') { if (isRadixPointFound) return FALSE; else isRadixPointFound = 1; } } for (int i = 1; i < strlen(str); i++) { if (str[i] == '-') return FALSE; } if (isRadixPointFound) return TRUE; else return FALSE; }
Token GetNextToken() { CopyLexerState(&compiler.prevLexerState, &compiler.currLexerState); compiler.currLexerState.currentLexemeStart = compiler.currLexerState.currentLexemeEnd; int currentLexemeState = CL_LEX_STATE_START; int currentOpCharIndex = 0; int currentOpStateIndex = 0; OpState currentOpState; char currentChar; int nextLexemeCharIndex = 0; int isAddCurrentChar; int isLexemeDone = FALSE; int isEscapeChar = FALSE; while (TRUE) { currentChar = GetNextChar(); if (currentChar == '\0') break; isAddCurrentChar = TRUE; switch (currentLexemeState) { case CL_LEX_STATE_START: { if (IsCharWhiteSpace_CL(currentChar)) { compiler.currLexerState.currentLexemeStart++; isAddCurrentChar = FALSE; } else if (IsCharNumeric(currentChar)) { currentLexemeState = CL_LEX_STATE_INT; } else if (currentChar == '.') { currentLexemeState = CL_LEX_STATE_FLOAT; } else if (IsCharIdent(currentChar)) { currentLexemeState = CL_LEX_STATE_IDENT; } else if (IsCharDelimiter_CL(currentChar)) { currentLexemeState = CL_LEX_STATE_DELIMITER; } else if (IsCharOpChar(currentChar, 0)) { currentOpStateIndex = GetOpStateIndex(currentChar, 0, 0, 0); if (currentOpStateIndex == -1) ExitOnInvalidInputError(currentChar); currentOpState = GetOpState(0, currentOpStateIndex); currentOpCharIndex = 1; compiler.currLexerState.currentOp = currentOpState.iIndex; currentLexemeState = CL_LEX_STATE_OP; } else if (currentChar == '"') { isAddCurrentChar = FALSE; currentLexemeState = CL_LEX_STATE_STRING; } else if (currentChar == -51) { isAddCurrentChar = FALSE; currentLexemeState = CL_LEX_STATE_END; isLexemeDone = TRUE; } else { ExitOnInvalidInputError(currentChar); } break; } case CL_LEX_STATE_INT: { if (IsCharNumeric(currentChar)) currentLexemeState = CL_LEX_STATE_INT; else if (currentChar == '.') currentLexemeState = CL_LEX_STATE_FLOAT; else if (IsCharWhiteSpace_CL(currentChar) || IsCharDelimiter_CL(currentChar)) { isAddCurrentChar = FALSE; isLexemeDone = TRUE; } else ExitOnInvalidInputError(currentChar); break; } case CL_LEX_STATE_FLOAT: { if (IsCharNumeric(currentChar)) currentLexemeState = CL_LEX_STATE_FLOAT; else if (IsCharWhiteSpace_CL(currentChar) || IsCharDelimiter_CL(currentChar)) { isAddCurrentChar = FALSE; isLexemeDone = TRUE; } else ExitOnInvalidInputError(currentChar); break; } case CL_LEX_STATE_IDENT: { if (IsCharIdent(currentChar)) currentLexemeState = CL_LEX_STATE_IDENT; else if (IsCharWhiteSpace_CL(currentChar) || IsCharDelimiter_CL(currentChar) || IsCharOpChar(currentChar, 0)) { isAddCurrentChar = FALSE; isLexemeDone = TRUE; } else ExitOnInvalidInputError(currentChar); break; } case CL_LEX_STATE_OP: { if (currentOpState.iSubStateCount == 0) { isAddCurrentChar = FALSE; isLexemeDone = TRUE; break; } if (IsCharOpChar(currentChar, currentOpCharIndex)) { currentOpStateIndex = GetOpStateIndex(currentChar, currentOpCharIndex, currentOpState.iSubStateIndex, currentOpState.iSubStateCount); if (currentOpStateIndex == -1) ExitOnInvalidInputError(currentChar); currentOpState = GetOpState(currentOpCharIndex, currentOpStateIndex); currentOpCharIndex++; compiler.currLexerState.currentOp = currentOpState.iIndex; } else { isAddCurrentChar = FALSE; isLexemeDone = TRUE; break; } break; } case CL_LEX_STATE_DELIMITER: { isAddCurrentChar = FALSE; isLexemeDone = TRUE; break; } case CL_LEX_STATE_STRING: { if (isEscapeChar) { isEscapeChar = FALSE; isAddCurrentChar = TRUE; } else { if (currentChar == '"') { isAddCurrentChar = FALSE; currentLexemeState = CL_LEX_STATE_STRING_CLOSE_QUOTE; } else if (currentChar == '\\') { isEscapeChar = TRUE; isAddCurrentChar = TRUE; } else { isAddCurrentChar = TRUE; } } break; } case CL_LEX_STATE_STRING_ESCAPE: { /*switch (currentChar) { case 'n': default: break; } isEscapeChar = TRUE; currentLexemeState = CL_LEX_STATE_STRING;*/ break; } case CL_LEX_STATE_STRING_CLOSE_QUOTE: { isAddCurrentChar = FALSE; isLexemeDone = TRUE; break; } default: break; } if (isAddCurrentChar) { compiler.currLexerState.currentLexeme[nextLexemeCharIndex] = currentChar; nextLexemeCharIndex++; } if (isLexemeDone) break; } compiler.currLexerState.currentLexeme[nextLexemeCharIndex] = '\0'; compiler.currLexerState.currentLexemeEnd--; Token tokenType; switch (currentLexemeState) { case CL_LEX_STATE_INT: { tokenType = CL_TOKEN_TYPE_INT; break; } case CL_LEX_STATE_FLOAT: { tokenType = CL_TOKEN_TYPE_FLOAT; break; } case CL_LEX_STATE_IDENT: { tokenType = CL_TOKEN_TYPE_IDENT; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_VAR) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_VAR; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_TRUE) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_TRUE; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_FALSE) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_FALSE; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_IF) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_IF; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_ELSE) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_ELSE; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_BREAK) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_BREAK; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_CONTINUE) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_CONTINUE; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_FOR) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_FOR; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_WHILE) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_WHILE; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_FUNC) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_FUNC; if (strcmp(compiler.currLexerState.currentLexeme, CL_KW_RETURN) == 0) tokenType = CL_TOKEN_TYPE_KEYWORD_RETURN; break; } case CL_LEX_STATE_DELIMITER: { switch (compiler.currLexerState.currentLexeme[0]) { case ',': tokenType = CL_TOKEN_TYPE_DELIM_COMMA; break; case '(': tokenType = CL_TOKEN_TYPE_DELIM_OPEN_PAREN; break; case ')': tokenType = CL_TOKEN_TYPE_DELIM_CLOSE_PAREN; break; case '[': tokenType = CL_TOKEN_TYPE_DELIM_OPEN_BRACE; break; case ']': tokenType = CL_TOKEN_TYPE_DELIM_CLOSE_BRACE; break; case '{': tokenType = CL_TOKEN_TYPE_DELIM_OPEN_CURLY_BRACE; break; case '}': tokenType = CL_TOKEN_TYPE_DELIM_CLOSE_CURLY_BRACE; break; case ';': tokenType = CL_TOKEN_TYPE_DELIM_SEMICOLON; break; } break; } case CL_LEX_STATE_OP: { tokenType = CL_TOKEN_TYPE_OPERATOR; break; } case CL_LEX_STATE_STRING_CLOSE_QUOTE: { tokenType = CL_TOKEN_TYPE_STRING; break; } case CL_LEX_STATE_END: { tokenType = CL_TOKEN_TYPE_END_OF_STREAM; break; } default: tokenType = CL_TOKEN_TYPE_END_OF_STREAM; break; } return tokenType; }
int CStringUtils::CompareNumerical(LPCTSTR x_str, LPCTSTR y_str) { LPCTSTR num_x_begin = x_str, num_y_begin = y_str; DWORD num_x_cnt = 0, num_y_cnt = 0; int cs_cmp_result = 2; // skip same chars and remember last numeric part of strings while ((*x_str || *y_str) && CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE | NORM_IGNOREWIDTH, x_str, 1, y_str, 1) == 2 /* equal */ ) { if (IsCharNumeric(*x_str)) { ++num_x_cnt; ++num_y_cnt; } else { num_x_begin = CharNext(x_str); num_y_begin = CharNext(y_str); num_x_cnt = 0; num_y_cnt = 0; if (cs_cmp_result == 2) cs_cmp_result = CompareString(LOCALE_USER_DEFAULT, NORM_IGNOREWIDTH, x_str, 1, y_str, 1); } x_str = CharNext(x_str); y_str = CharNext(y_str); } // parse numeric part of first arg if (num_x_cnt || IsCharNumeric(*x_str)) { LPCTSTR x_str_tmp = x_str; while (IsCharNumeric(*x_str_tmp)) { ++num_x_cnt; x_str_tmp = CharNext(x_str_tmp); } // parse numeric part of second arg if (num_y_cnt || IsCharNumeric(*y_str)) { LPCTSTR y_str_tmp = y_str; while (IsCharNumeric(*y_str_tmp)) { ++num_y_cnt; y_str_tmp = CharNext(y_str_tmp); } DWORD num_x_cnt_with_zeros = num_x_cnt, num_y_cnt_with_zeros = num_y_cnt; while (num_x_cnt < num_y_cnt) { if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNOREWIDTH, num_y_begin, 1, TEXT("0"), 1) != 2 /* not equal to '0' */ ) return -1; num_y_begin = CharNext(num_y_begin); --num_y_cnt; } while (num_x_cnt > num_y_cnt) { if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNOREWIDTH, num_x_begin, 1, TEXT("0"), 1) != 2 /* not equal to '0' */ ) return 1; num_x_begin = CharNext(num_x_begin); --num_x_cnt; } // here num_x_cnt == num_y_cnt int cmp_result = CompareString(LOCALE_USER_DEFAULT, NORM_IGNOREWIDTH, num_x_begin, num_x_cnt, num_y_begin, num_y_cnt); if (cmp_result != 2) return cmp_result - 2; if (num_x_cnt_with_zeros != num_y_cnt_with_zeros) return num_x_cnt_with_zeros < num_y_cnt_with_zeros ? -1 : 1; if (cs_cmp_result != 2) return cs_cmp_result - 2; } } // otherwise, compare literally int cmp_result = CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE | NORM_IGNOREWIDTH, x_str, -1, y_str, -1); if (cmp_result != 2) return cmp_result - 2; if (cs_cmp_result == 2) cs_cmp_result = CompareString(LOCALE_USER_DEFAULT, NORM_IGNOREWIDTH, x_str, -1, y_str, -1); return cs_cmp_result - 2; }