00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_UTILITY_TOKEN_READER_H__
00009 #define __SP_UTILITY_TOKEN_READER_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #ifdef SP_COMPILE_WITH_TOKENPARSER
00015
00016
00017 #include "Base/spInputOutputString.hpp"
00018
00019
00020 namespace sp
00021 {
00022 namespace tool
00023 {
00024
00025
00027 enum ETokenTypes
00028 {
00029 TOKEN_UNKNOWN,
00030 TOKEN_NAME,
00031 TOKEN_STRING,
00032 TOKEN_NUMBER_INT,
00033 TOKEN_NUMBER_FLOAT,
00034 TOKEN_COMMA,
00035 TOKEN_DOT,
00036 TOKEN_COLON,
00037 TOKEN_SEMICOLON,
00038 TOKEN_EXCLAMATION_MARK,
00039 TOKEN_QUESTION_MARK,
00040 TOKEN_BRACKET_LEFT,
00041 TOKEN_BRACKET_RIGHT,
00042 TOKEN_SQUARED_BRACKET_LEFT,
00043 TOKEN_SQUARED_BRACKET_RIGHT,
00044 TOKEN_BRACE_LEFT,
00045 TOKEN_BRACE_RIGHT,
00046 TOKEN_GREATER_THAN,
00047 TOKEN_LESS_THAN,
00048 TOKEN_GREATER_THAN_OR_EQUAL,
00049 TOKEN_LESS_THAN_OR_RQUAL,
00050 TOKEN_EQUAL,
00051 TOKEN_ASSIGN,
00052 TOKEN_NOT_EQUAL,
00053 TOKEN_INC,
00054 TOKEN_DEC,
00055 TOKEN_ADD,
00056 TOKEN_SUB,
00057 TOKEN_MUL,
00058 TOKEN_DIV,
00059 TOKEN_MOD,
00060 TOKEN_ADD_ASSIGN,
00061 TOKEN_SUB_ASSIGN,
00062 TOKEN_MUL_ASSIGN,
00063 TOKEN_DIV_ASSIGN,
00064 TOKEN_MOD_ASSIGN,
00065 TOKEN_LOGIC_AND,
00066 TOKEN_LOGIC_OR,
00067 TOKEN_BITWISE_NOT,
00068 TOKEN_BITWISE_AND,
00069 TOKEN_BITWISE_OR,
00070 TOKEN_BITWISE_XOR,
00071 TOKEN_BITWISE_NOT_ASSIGN,
00072 TOKEN_BITWISE_AND_ASSIGN,
00073 TOKEN_BITWISE_OR_ASSIGN,
00074 TOKEN_BITWISE_XOR_ASSIGN,
00075 TOKEN_SHIFT_LEFT,
00076 TOKEN_SHIFT_RIGHT,
00077 TOKEN_SHIFT_LEFT_ASSIGN,
00078 TOKEN_SHIFT_RIGHT_ASSIGN,
00079 };
00080
00081
00083 struct SP_EXPORT SToken
00084 {
00085 SToken();
00086 SToken(const ETokenTypes TokenType, s32 TokenRow = 0, s32 TokenColumn = 0);
00087 SToken(
00088 const ETokenTypes TokenType, const io::stringc &TokenStr,
00089 s32 TokenRow = 0, s32 TokenColumn = 0
00090 );
00091 ~SToken();
00092
00093
00094 io::stringc getRowColumnString() const;
00095 bool isName(const io::stringc &Name) const;
00096
00097
00098 ETokenTypes Type;
00099 io::stringc Str;
00100 s32 Row;
00101 s32 Column;
00102 };
00103
00104
00111 class SP_EXPORT TokenParser
00112 {
00113
00114 public:
00115
00116 TokenParser();
00117 ~TokenParser();
00118
00119
00120
00129 bool readTokens(const c8* InputString, std::vector<SToken> &OutputTokens);
00130
00137 bool readFile(const io::stringc &InputFilename, std::vector<SToken> &OutputTokens);
00138
00139 private:
00140
00141
00142
00143 void nextChar();
00144
00145 bool exitWithError(const io::stringc &Message);
00146
00147 void addToken(const ETokenTypes TokenType);
00148 void addToken(const ETokenTypes TokenType, const io::stringc &TokenStr);
00149
00150 c8 getNextNextChar() const;
00151
00152
00153
00154 inline bool isChar(c8 Char) const
00155 {
00156 return CurrChar_ == Char;
00157 }
00158 inline bool isChar(c8 Char, c8 NextChar) const
00159 {
00160 return CurrChar_ == Char && NextChar_ == NextChar;
00161 }
00162 inline bool isChar(c8 Char, c8 NextChar, c8 NextNextChar) const
00163 {
00164 return CurrChar_ == Char && NextChar_ == NextChar && getNextNextChar() == NextNextChar;
00165 }
00166
00167 inline bool isNotNextChar(c8 Char) const
00168 {
00169 return NextChar_ != Char;
00170 }
00171
00172 inline bool isCharWhiteSpace(c8 Char) const
00173 {
00174 return Char == ' ' || Char == '\n' || Char == '\t';
00175 }
00176 inline bool isCharNamePart(c8 Char) const
00177 {
00178 return (Char >= 'A' && Char <= 'Z') || (Char >= 'a' && Char <= 'z') || Char == '_';
00179 }
00180 inline bool isCharNumber(c8 Char) const
00181 {
00182 return Char >= '0' && Char <= '9';
00183 }
00184
00185
00186
00187 std::vector<SToken>* OutputTokensRef_;
00188
00189 const c8* InputString_;
00190 c8 CurrChar_, NextChar_;
00191 s32 Row_, Column_;
00192
00193 };
00194
00195
00196 }
00197
00198 }
00199
00200
00201 #endif
00202
00203 #endif
00204
00205
00206
00207