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

parse_util.hh

Go to the documentation of this file.
00001 /*
00002  *  File:       parse_util.hh
00003  *              $Id: parse_util.hh,v 1.7 2002/06/26 20:49:08 alec Exp $
00004  *
00005  *  Author:     Alec Panoviciu (alecu@email.com)
00006  *
00007  *  Comments:
00008  *
00009  *  Revision history:
00010  *
00011  *  $Log: parse_util.hh,v $
00012  *  Revision 1.7  2002/06/26 20:49:08  alec
00013  *  g++ 3.x happy
00014  *
00015  *  Revision 1.6  2002/06/23 23:29:21  alec
00016  *  profile-based optimization stuff
00017  *
00018  *  Revision 1.5  2002/06/13 11:40:48  alec
00019  *  added #line stuff
00020  *
00021  *  Revision 1.4  2002/04/29 17:55:41  alec
00022  *  regexps almost done
00023  *
00024  *  Revision 1.3  2002/04/29 09:34:10  alec
00025  *  scanner ptree building compiles
00026  *
00027  */
00028 
00029 
00030 /* 
00031   Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00032 
00033   This program is free software; you can redistribute it and/or modify
00034   it under the terms of the GNU General Public License as published by
00035   the Free Software Foundation; either version 2 of the License, or
00036   (at your option) any later version.
00037 
00038   This program is distributed in the hope that it will be useful,
00039   but WITHOUT ANY WARRANTY; without even the implied warranty of
00040   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00041   GNU General Public License for more details.
00042 
00043   You should have received a copy of the GNU General Public License
00044   along with this program; if not, write to the Free Software
00045   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00046 
00047  */
00048 
00049 #ifndef __PARSE_UTIL_HH__
00050 #define __PARSE_UTIL_HH__
00051 
00052 #include <stdexcept>
00053 #include <iostream>
00054 #include <string>
00055 using namespace std;
00056 
00060 typedef struct t_Position
00061 {
00062   int ln;
00063   int col;
00064 
00065   t_Position (int ln_, int col_ = 0) :
00066     ln(ln_), col(col_)
00067   {}
00068 
00069   t_Position () : ln(0), col(0)
00070   {}
00071   
00072 } Position;
00073 
00074 
00080 typedef struct t_CodeChunk
00081 {
00082   Position pos;
00083   string code;
00084 
00085   t_CodeChunk () {}
00086 
00087   t_CodeChunk (const t_CodeChunk &o) :
00088     pos(o.pos), code(o.code)
00089   {}
00090 
00091   t_CodeChunk (const Position &pos_, const string &code_) :
00092     pos(pos_), code(code_)
00093   {}
00094 
00095   t_CodeChunk& operator = (const struct t_CodeChunk &o)
00096   {
00097     pos = o.pos;
00098     code = o.code;
00099     return *this;
00100   }
00101 
00102   void set (const string &code_, const Position &pos_)
00103   {
00104     code = code_;
00105     pos = pos_;
00106   }
00107 
00108   int countLines () const
00109   {
00110     int res = 1, i = 0;
00111     while ((i = code.find('\n', i)) != string::npos) res++, i++;
00112     return res;
00113   }
00114 
00115   bool empty() const { return code.size() == 0; }
00116   
00117 } CodeChunk;
00118 
00119 inline ostream& operator << (ostream &os, const Position &pos)
00120 {
00121   os << pos.ln;
00122   if (pos.col != 0) os << ":" << pos.col;
00123   return os;
00124 }
00125 
00130 class ParseException : public exception
00131 {
00132 public:
00133 
00134   ParseException (const string &message_) :
00135     message(message_),
00136     pos()
00137   {}
00138   
00139   ParseException () :
00140     message("Bad option"),
00141     pos()
00142   {}
00143 
00144   ParseException (const Position &pos_, const string &message_) :
00145     message(message_),
00146     pos(pos_)
00147   {}
00148 
00149   ~ParseException () throw ()
00150   {}
00151 
00152   virtual const char* what() const throw()
00153   {
00154     return message.c_str();
00155   }
00156 
00157   virtual operator string() const
00158   {
00159     return message;
00160   }
00161 
00162   const Position& where() const
00163   {
00164     return pos;
00165   }
00166 
00167   
00168 protected:
00169   
00170   string message;
00171   Position pos;
00172 };
00173 
00174 
00182 string formatError (const Position& pos, const string &message);
00183 
00184 
00190 string formatError (const string &message);
00191 
00192 
00198 string formatError (const ParseException &ex);
00199 
00207 string formatWarning (const Position& pos, const string &message);
00208 
00214 string formatWarning (const string &message);
00215 
00216 
00224 void die (const string &message);
00225 
00226 #endif /* #ifndef __PARSE_UTIL_HH__ */

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