00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <string>
00025 #include <vector>
00026 #include <map>
00027 #include "Parse.h"
00028 #include <assert.h>
00029 #include <ctype.h>
00030 #include "BaseParser.h"
00031
00032 #define DEB(x)
00033
00034
00035
00036 BaseParser::BaseParser()
00037 :m_level(0)
00038 ,m_plevel(0)
00039 ,m_glevel(0)
00040 ,m_filename("")
00041 ,m_line(0)
00042 ,m_pos(0)
00043 ,the_ptr(0)
00044 {
00045 *the_str = 0;
00046
00047 m_word["class"] = Class;
00048 m_word["namespace"] = Namespace;
00049 m_word["using"] = Using;
00050 m_word["template"] = Template;
00051 m_word["friend"] = Friend;
00052 m_word["typedef"] = Typedef;
00053 m_word["virtual"] = Virtual;
00054 m_word["public"] = Public;
00055 m_word["protected"] = Protected;
00056 m_word["private"] = Private;
00057 m_word["const"] = Const;
00058 m_word["struct"] = Struct;
00059 m_word["enum"] = Enum;
00060 }
00061
00062
00063 BaseParser::~BaseParser()
00064 {
00065 }
00066
00067
00068 int BaseParser::GetWord(const std::string &str)
00069 {
00070 stringint_m::iterator it = m_word.begin();
00071 while (it != m_word.end())
00072 {
00073 std::string strm = (*it).first;
00074 if (!strcmp(str.c_str(),strm.c_str()))
00075 {
00076 return (*it).second;
00077 }
00078
00079 it++;
00080 }
00081 return 0;
00082 }
00083
00084
00085 void BaseParser::preprocessor(std::string &val)
00086 {
00087 Parse pa( (char *)val.c_str() );
00088 int line;
00089 char slask[200];
00090
00091 strcpy(slask,pa.getword().c_str());
00092 assert(*slask == '#');
00093 if (!strcmp(slask,"#"))
00094 {
00095 strcpy(slask,pa.getword().c_str());
00096 if (isdigit(*slask))
00097 {
00098 line = atoi(slask);
00099 strcpy(slask,pa.getword().c_str());
00100 if (*slask == 34)
00101 {
00102 slask[strlen(slask) - 1] = 0;
00103 m_filename = (char *)(slask + 1);
00104 m_line = line;
00105 }
00106 }
00107 }
00108 }
00109
00110
00111 int BaseParser::ReadLine(FILE *fil)
00112 {
00113 fgets(the_str,500,fil);
00114 m_line++;
00115 if (feof(fil))
00116 return 0;
00117 while (strlen(the_str) && (Cx == 13 || Cx == 10))
00118 Cx = 0;
00119 while (Cx == '\\')
00120 {
00121 fgets(the_str + strlen(the_str) - 1,500,fil);
00122 m_line++;
00123 if (feof(fil))
00124 return 0;
00125 while (strlen(the_str) && (Cx == 13 || Cx == 10))
00126 Cx = 0;
00127 }
00128 the_ptr = 0;
00129 return 1;
00130 }
00131
00132
00133 int BaseParser::GetToken(FILE *fil,std::string &val)
00134 {
00135 bool bWhitespace = false;
00136 while (C == ' ' || C == '\t')
00137 {
00138 bWhitespace = true;
00139 the_ptr++;
00140 }
00141 while (!C)
00142 {
00143 if (!ReadLine(fil))
00144 return 0;
00145 if (C == '#')
00146 {
00147 val = the_str;
00148 C = 0;
00149 return CPP;
00150 }
00151 while (C == ' ' || C == '\t')
00152 {
00153 the_ptr++;
00154 }
00155 bWhitespace = true;
00156 }
00157 if (bWhitespace)
00158 {
00159 val = " ";
00160 return WHITESPACE;
00161 }
00162 m_pos = the_ptr;
00163 if (isdigit(C))
00164 {
00165 while (isdigit(C) || C == '.')
00166 {
00167 val += C;
00168 the_ptr++;
00169 }
00170 return NUMBER;
00171 }
00172 if (isalpha(C) || C == '_' || C == '~' || (C == ':' && C1 == ':' && (isalpha(C2) || C2 == '_')) )
00173 {
00174 if (C == '~')
00175 {
00176 val += C;
00177 the_ptr++;
00178 }
00179 while (isalnum(C) || C == '_' || (C == ':' && C1 == ':'))
00180 {
00181 if (C == ':' && C1 == ':')
00182 {
00183 val += C;
00184 the_ptr++;
00185 }
00186 val += C;
00187 the_ptr++;
00188 }
00189 {
00190 int id = GetWord(val);
00191 if (id >= 256)
00192 {
00193 return id;
00194 }
00195 }
00196 return IDENTIFIER;
00197 }
00198 if (C == '/' && C1 == '*')
00199 {
00200 while (C != '*' || C1 != '/')
00201 {
00202 val += C;
00203 the_ptr++;
00204 while (!C)
00205 {
00206 if (!ReadLine(fil))
00207 return 0;
00208 val += '\n';
00209 }
00210 }
00211 val += C;
00212 the_ptr++;
00213 val += C;
00214 the_ptr++;
00215 val += '\n';
00216 return COMMENT;
00217 }
00218 if (C == '/' && C1 == '/')
00219 {
00220 val = (char *)(the_str + the_ptr);
00221 C = 0;
00222 val += '\n';
00223 return COMMENT;
00224 }
00225 if (C == 34)
00226 {
00227 val += C;
00228 the_ptr++;
00229 while (C != 34 && C)
00230 {
00231 if (C == '\\')
00232 {
00233 val += C;
00234 the_ptr++;
00235 }
00236 val += C;
00237 the_ptr++;
00238 }
00239 if (C == 34)
00240 {
00241 val += C;
00242 the_ptr++;
00243 }
00244 else
00245 {
00246 error("Unterminated string constant");
00247 }
00248 return STRING;
00249 }
00250 if (C == '\'')
00251 {
00252 val += C;
00253 the_ptr++;
00254 while (C != '\'' && C)
00255 {
00256 if (C == '\\')
00257 {
00258 val += C;
00259 the_ptr++;
00260 }
00261 val += C;
00262 the_ptr++;
00263 }
00264 if (C == '\'')
00265 {
00266 val += C;
00267 the_ptr++;
00268 }
00269 else
00270 {
00271 error("Unterminated char constant");
00272 }
00273 return CHAR;
00274 }
00275
00276 #define dblcheck(x1,x2,y) { \
00277 if (C == x1 && C1 == x2) \
00278 { \
00279 val = x1; \
00280 val += x2; \
00281 the_ptr += 2; \
00282 return y; \
00283 } \
00284 }
00285
00286 dblcheck(':',':',CC);
00287 dblcheck('<','<',LTLT);
00288 dblcheck('>','>',GTGT);
00289 dblcheck('*','*',DPTR);
00290 dblcheck('<','=',LTEQ);
00291 dblcheck('!','=',NEQ);
00292 dblcheck('>','=',GTEQ);
00293 dblcheck('=','=',EQ);
00294 dblcheck('|','|',OR);
00295 dblcheck('&','&',AND);
00296 dblcheck('+','+',PP);
00297 dblcheck('-','-',MM);
00298 dblcheck('-','>',POINTER);
00299
00300 val += C;
00301 if (C == '{')
00302 {
00303 m_level++;
00304 }
00305 if (C == '(')
00306 m_plevel++;
00307 if (C == '<')
00308 m_glevel++;
00309 return the_str[the_ptr++];
00310 }
00311
00312
00313 void BaseParser::error(const std::string &str)
00314 {
00315 DEB( fprintf(stderr,"%s line %d: %s\n",m_currentfile.c_str(),m_line,str.c_str());)
00316 }
00317
00318
00319 void BaseParser::EndToken(char c)
00320 {
00321 if (c == '}')
00322 {
00323 m_level--;
00324 }
00325 if (c == ')')
00326 m_plevel--;
00327 if (c == '>')
00328 m_glevel--;
00329 }
00330
00331