00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #include "token_writer.hh"
00063 #include "token_spec.hh"
00064 #include "prop_registry.hh"
00065
00066 void TokenWriter::writeTokenClass (TokenSpec &tSpec,
00067 const string &scannerClasssName)
00068 {
00069 string includeMacro = className2macro(tSpec.className);
00070 openStream(fullPath(className2hh(tSpec.className)));
00071
00072 line() << "#ifndef " << includeMacro;
00073 line() << "#define " << includeMacro;
00074 line();
00075 line() << "#include <string>";
00076 line() << "#include <sstream>";
00077 line() << "using namespace std;";
00078 line();
00079 if (!tSpec.preambleCode.empty())
00080 writeChunk(tSpec.preambleCode);
00081
00082 line() << "namespace cppcc";
00083 line() << "{";
00084 line();
00085
00086 bool cc = registry["COUNT_COLUMNS"];
00087
00088
00089
00090 line() << "class Position";
00091 line() << "{";
00092 indent();
00093 line() << "public:";
00094 indent();
00095 line() << "Position () : ln(0)" << (cc ? ", col(0)" : "") <<" {}";
00096 line() << "Position (int ln_, int col_) : ln(ln_)"
00097 << (cc ? ", col(col_)" : "") << " {}";
00098 line() << "Position (const Position &o) : ln(o.ln)"
00099 << (cc ? ", col(o.col)" : "") << " {}";
00100 line();
00101 line() << "operator string () const";
00102 line() << "{";
00103 indent();
00104 line() << "ostringstream oss;";
00105 line() << "oss << ln << \":\"" << (cc ? " << col" : "") << ";";
00106 line() << "return oss.str();";
00107 unindent();
00108 line() << "}";
00109 line();
00110 line() << "int ln" << (cc ? ", col" : "") << ";";
00111 unindent();
00112 unindent();
00113 line() << "};";
00114 line();
00115
00116
00117
00118 line() << "class " << tSpec.className;
00119 if (!tSpec.inheritance.empty()) {
00120 ofs << ": ";
00121 writeChunk(tSpec.inheritance);
00122 }
00123 line() << "{";
00124 indent();
00125 line() << "friend class " << scannerClasssName << ";";
00126 line() << "public:";
00127 indent();
00128 line();
00129 line() << tSpec.className << " () : _img_valid(true) {}";
00130
00131 line() << tSpec.className << " (int id_, const string &_str_img_, ";
00132 line() << " const Position &bPos_, const Position &ePos_) :";
00133 line() << " _str_img(_str_img_), _img_valid(true), id(id_), bPos(bPos_), ePos(ePos_)";
00134 line() << "{}";
00135
00136 line() << tSpec.className << " (int id_, const Position &bPos_, const Position &ePos_) :";
00137 line() << " _img_valid(true), id(id_), bPos(bPos_), ePos(ePos_)";
00138 line() << "{}";
00139
00140 line() << tSpec.className << " (int id_) : ";
00141 line() << " _img_valid(true), id(id_)";
00142 line() << "{}";
00143
00144 line() << tSpec.className << " (const string &_str_img_) : ";
00145 line() << " _str_img(_str_img_), _img_valid(true)";
00146 line() << "{ length = _str_img.length(); }";
00147
00148 line() << tSpec.className << " (int id_, const Position &bPos_) :";
00149 line() << " _img_valid(true), id(id_), bPos(bPos_), ePos(bPos_)";
00150 line() << "{}";
00151
00152 unindent();
00153 line();
00154 line() << "public:";
00155 indent();
00156 line() << "Position bPos, ePos;";
00157 line() << "int id;";
00158 line() << "int length;";
00159 line() << "string& image()";
00160 line() << "{";
00161 indent();
00162 line() << "_cacheImg();";
00163 line() << "return _str_img;";
00164 unindent();
00165 line() << "}";
00166 unindent();
00167 line();
00168 line() << "private:";
00169 indent();
00170 line() << "const char *_img_start; //points into the scanner's buffer";
00171 line() << "bool _img_valid; //true if the string image is in sync with the buffer";
00172 line() << "string _str_img; //caches the image as a string";
00173 line();
00174 line() << "void _set (int id_, const Position &bPos_, const Position &ePos_,";
00175 line() << " const char *_img_start_, int length_)";
00176 line() << "{";
00177 indent();
00178 line() << "id = id_; bPos = bPos_; ePos = ePos_;";
00179 line() << "_img_start = _img_start_; length = length_;";
00180 line() << "_img_valid = false;";
00181 unindent();
00182 line() << "}";
00183
00184 line(); line() << "void _set (const char *_img_start_, int length_)";
00185 line() << "{";
00186 indent();
00187 line() << "_img_start = _img_start_; length = length_;";
00188 line() << "_img_valid = false;";
00189 unindent();
00190 line() << "}";
00191
00192 line() << "void _cacheImg()";
00193 line() << "{";
00194 indent();
00195 line() << "if (!_img_valid) {";
00196 indent();
00197 line() << "_str_img.assign(_img_start, length);";
00198 line() << "_img_valid = true;";
00199 unindent();
00200 line() << "}";
00201 unindent();
00202 line() << "}";
00203 line();
00204 unindent();
00205 if (!tSpec.userCode.empty()) {
00206 for (int i = 0; i < tSpec.userCode.size(); i++) {
00207 line() << "private: // user code";
00208 line();
00209 writeChunk(tSpec.userCode[i]);
00210 }
00211 line();
00212 }
00213 line() << " /* Token id constants: */";
00214 line() << "public:";
00215 line();
00216 indent();
00217 for (int i = 0; i < tSpec.count(); i++)
00218 line() << "static const int " << tSpec[i].name() << ";";
00219 line();
00220 if (registry["DEBUG_SCANNER"] || registry["DEBUG_PARSER"])
00221 line() << "static const char *tokenNames[];";
00222 unindent();
00223 unindent();
00224 line() << "}; // end of " << tSpec.className << " class";
00225 line();
00226 line();
00227
00228
00229 line() << "} // end namespace cppcc";
00230 line() << "#endif // ifndef " << includeMacro;
00231 closeStream();
00232
00233
00234 openStream(fullPath(className2cc(tSpec.className)));
00235 line() << "#include \"" << className2hh(tSpec.className) << "\"";
00236 line();
00237 line();
00238 for (int i = 0; i < tSpec.count(); i++)
00239 line() << "const int cppcc::" << tSpec.className << "::"
00240 << tSpec[i].name() << " = " << tSpec[i].id() << ";";
00241 line();
00242 if (registry["DEBUG_SCANNER"] || registry["DEBUG_PARSER"]) {
00243 line();
00244 line();
00245 line() << "const char *cppcc::" << tSpec.className << "::tokenNames[] = {";
00246 for (int i = 0; i < tSpec.count(); i++)
00247 line() << "\"" << tSpec[i].name() << "\",";
00248 line() << "};";
00249 line();
00250 }
00251 closeStream();
00252 }