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 #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