00001
00011 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00012 #pragma once
00013 #endif
00014 #ifndef COMMON_TOKENIZER_H_
00015 #define COMMON_TOKENIZER_H_
00016
00017 #include "Error.hpp"
00018
00019 namespace common
00020 {
00021
00022 class Stream;
00023
00028
00030 class TokenizerError : public Error
00031 {
00032 private:
00033 size_t m_Char, m_Row, m_Col;
00034 public:
00035 TokenizerError(size_t Char, size_t Row, size_t Col, const tstring &Msg = _T(""), const tstring &File = _T(""), int a_Line = 0);
00036
00037 size_t GetCharNum() { return m_Char; }
00038 size_t GetRowNum() { return m_Row; }
00039 size_t GetColNum() { return m_Col; }
00040 };
00041
00043 class Tokenizer_pimpl;
00044
00046 class Tokenizer
00047 {
00048 private:
00049 scoped_ptr<Tokenizer_pimpl> pimpl;
00050
00051 public:
00053 static const uint FLAG_TOKEN_EOL = 0x01;
00055 static const uint FLAG_MULTILINE_STRINGS = 0x02;
00056
00058 enum TOKEN
00059 {
00060 TOKEN_EOF,
00061 TOKEN_EOL,
00062 TOKEN_SYMBOL,
00063 TOKEN_INTEGER,
00064 TOKEN_FLOAT,
00065 TOKEN_CHAR,
00066 TOKEN_IDENTIFIER,
00067 TOKEN_KEYWORD,
00068 TOKEN_STRING,
00069 };
00070
00072 Tokenizer(const tchar *Input, size_t NumChars, uint Flags);
00074 Tokenizer(const tstring *Input, uint Flags);
00076 Tokenizer(Stream *Input, uint Flags);
00077
00078 ~Tokenizer();
00079
00082 void RegisterKeyword(uint Id, const tstring &Keyword);
00084
00085 void RegisterKeywords(const tchar **Keywords, size_t KeywordCount);
00087
00091
00093 void Next();
00094
00096 TOKEN GetToken();
00097
00098 size_t GetCharNum();
00099 size_t GetRowNum();
00100 size_t GetColNum();
00101
00103 const tstring & GetString();
00105 tchar GetChar();
00107 uint GetId();
00109 bool GetUint1(uint1 *Out);
00110 bool GetUint2(uint2 *Out);
00111 bool GetUint4(uint4 *Out);
00112 bool GetUint8(uint8 *Out);
00113 bool GetInt1(int1 *Out);
00114 bool GetInt2(int2 *Out);
00115 bool GetInt4(int4 *Out);
00116 bool GetInt8(int8 *Out);
00117 uint1 MustGetUint1();
00118 uint2 MustGetUint2();
00119 uint4 MustGetUint4();
00120 uint8 MustGetUint8();
00121 int1 MustGetInt1();
00122 int2 MustGetInt2();
00123 int4 MustGetInt4();
00124 int8 MustGetInt8();
00126 bool GetFloat(float *Out);
00128 bool GetDouble(double *Out);
00129 float MustGetFloat();
00130 double MustGetDouble();
00131
00133
00135 void CreateError();
00137 void CreateError(const tstring &Msg);
00138
00142 bool QueryToken(TOKEN Token);
00143 bool QueryToken(TOKEN Token1, TOKEN Token2);
00144 bool QueryEOF();
00145 bool QueryEOL();
00146 bool QuerySymbol(tchar Symbol);
00147 bool QueryIdentifier(const tstring &Identifier);
00148 bool QueryKeyword(uint KeywordId);
00149 bool QueryKeyword(const tstring &Keyword);
00151
00155
00156 void AssertToken(TOKEN Token);
00158 void AssertToken(TOKEN Token1, TOKEN Token2);
00159 void AssertEOF();
00160 void AssertEOL();
00161 void AssertSymbol(tchar Symbol);
00162 void AssertIdentifier(const tstring &Identifier);
00163 void AssertKeyword(uint KeywordId);
00164 void AssertKeyword(const tstring &Keyword);
00166
00168 static const tchar * GetTokenName(TOKEN Token);
00169
00170 static const uint ESCAPE_EOL = 0x01;
00171 static const uint ESCAPE_OTHER = 0x02;
00172
00173
00174 static void Escape(tstring *Out, const tstring &In, uint EscapeFlags);
00175 };
00176
00178
00179
00180 }
00181
00182 #endif