Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

token_writer.cc

Go to the documentation of this file.
00001 /*
00002  *  File:       token_writer.cc
00003  *              $Id: token_writer.cc,v 1.9 2002/07/09 08:54:51 alec Exp $
00004  *
00005  *  Author:     Alec Panoviciu (alecu@email.com)
00006  *
00007  *  Comments:
00008  *
00009  *  Revision history:
00010  *
00011  *  $Log: token_writer.cc,v $
00012  *  Revision 1.9  2002/07/09 08:54:51  alec
00013  *  0.0.3 stuff
00014  *
00015  *  Revision 1.8  2002/07/09 03:04:58  alec
00016  *  OWN_STRINGS bu*beep*it finally vanished
00017  *  gcc 3.1&mingw - related cleanups
00018  *
00019  *  Revision 1.7  2002/06/26 20:51:16  alec
00020  *  g++ 3.x happy
00021  *
00022  *  Revision 1.6  2002/06/13 11:43:17  alec
00023  *  added #line stuff
00024  *
00025  *  Revision 1.5  2002/05/31 12:11:09  alec
00026  *  *** empty log message ***
00027  *
00028  *  Revision 1.4  2002/05/27 03:04:02  alec
00029  *  doc update
00030  *
00031  *  Revision 1.3  2002/05/16 21:46:27  alec
00032  *  8x generated code speed improvement (almost 2x flex) weeepeeee!!!
00033  *
00034  *  Revision 1.2  2002/05/08 10:37:25  alec
00035  *  added keyword tokens support
00036  *
00037  *  Revision 1.1  2002/05/04 17:46:53  alec
00038  *  *** empty log message ***
00039  *
00040  */
00041 
00042 /* 
00043   Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00044 
00045   This program is free software; you can redistribute it and/or modify
00046   it under the terms of the GNU General Public License as published by
00047   the Free Software Foundation; either version 2 of the License, or
00048   (at your option) any later version.
00049 
00050   This program is distributed in the hope that it will be useful,
00051   but WITHOUT ANY WARRANTY; without even the implied warranty of
00052   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00053   GNU General Public License for more details.
00054 
00055   You should have received a copy of the GNU General Public License
00056   along with this program; if not, write to the Free Software
00057   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   // first dump the Position class
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   // now dump the token's class
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 }

Generated at Tue Jul 9 21:05:46 2002 for CppCC by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001