VDL.ccGo to the documentation of this file.00001
00002 #ifdef POLARIS_GNU_PRAGMAS
00003 #pragma implementation
00004 #endif
00005
00006 #include <ctype.h>
00007
00008 #include "VDL.h"
00009 #include "BinRep.h"
00010
00011 #include "p-assert.h"
00012
00013 const char LTP = '[';
00014 const char RTP = ']';
00015 const char LST = '{';
00016 const char RST = '}';
00017
00018 VDL::VDL()
00019 {
00020 #ifdef CLASS_INSTANCE_REGISTRY
00021 register_instance(VDL_CLASS, sizeof(VDL), this);
00022 #endif
00023
00024 _bs = 0;
00025 ifs = 0;
00026 }
00027
00028 VDL::~VDL()
00029 {
00030 #ifdef CLASS_INSTANCE_REGISTRY
00031 unregister_instance(VDL_CLASS, this);
00032 #endif
00033 }
00034
00035 const BinRep *
00036 VDL::data_ref() const
00037 {
00038 return _bs;
00039 }
00040
00041 BinRep *
00042 VDL::give_up_data()
00043 {
00044 BinRep *b = _bs;
00045
00046 _bs = 0;
00047
00048 return b;
00049 }
00050
00051 int
00052 VDL::_parse_set()
00053 {
00054 start_set();
00055
00056 while (! ifs->eof() ) {
00057 if (! _parse())
00058 if (_c != ',')
00059 break;
00060 }
00061
00062 end_set();
00063
00064 return 1;
00065 }
00066 int
00067 VDL::_parse_tuple()
00068 {
00069 start_tuple();
00070
00071 while (! ifs->eof() ) {
00072 if (! _parse())
00073 if (_c != ',')
00074 break;
00075 }
00076
00077 end_tuple();
00078
00079 return 1;
00080 }
00081
00082 int
00083 VDL::_parse_digit()
00084 {
00085 int value;
00086
00087 value = (_c - '0');
00088
00089 while (! ifs->eof() ) {
00090 ifs->get( _c );
00091
00092 if (! isdigit(_c)) {
00093 ifs->putback( _c );
00094 break;
00095 }
00096 value = (value * 10) + _c - '0';
00097 }
00098
00099 append_integer( value );
00100
00101 return 1;
00102 }
00103
00104 int
00105 VDL::_parse_alpha()
00106 {
00107 char buffer[ 1024 ];
00108 char *bp = buffer;
00109
00110 while (! ifs->eof() ) {
00111 ifs->get( _c );
00112
00113 if (! isalnum(_c) && _c != '_' && _c != '<' && _c != '>') {
00114 ifs->putback( _c );
00115 break;
00116 }
00117
00118 *bp++ = _c;
00119 }
00120
00121 *bp++ = '\0';
00122
00123 if (strcmp( "OM", buffer ) == 0)
00124 append_omega();
00125 else if (strcmp( "om", buffer ) == 0)
00126 append_omega();
00127 else if (strcmp( "<om>", buffer ) == 0)
00128 append_omega();
00129 else if (strcmp( "FALSE", buffer ) == 0)
00130 append_boolean( 0 );
00131 else if (strcmp( "false", buffer ) == 0)
00132 append_boolean( 0 );
00133 else if (strcmp( "TRUE", buffer ) == 0)
00134 append_boolean( 1 );
00135 else if (strcmp( "true", buffer ) == 0)
00136 append_boolean( 1 );
00137 else {
00138 cout << "UNKNOWN: " << buffer << endl;
00139 p_abort( "VDL::_parse_alpha: unknown ID" );
00140 }
00141
00142 return 1;
00143 }
00144
00145 int
00146 VDL::_parse_string()
00147 {
00148 char buffer[ 1024 ];
00149 char *bp = buffer;
00150
00151 while (! ifs->eof() ) {
00152 ifs->get( _c );
00153
00154 if (_c == '\\') {
00155 if (ifs->eof())
00156 break;
00157 ifs->get( _c );
00158 }
00159 else if (_c == '"')
00160 break;
00161
00162 *bp++ = _c;
00163 }
00164 *bp++ = '\0';
00165
00166 append_string( buffer );
00167
00168 return 1;
00169 }
00170
00171 int
00172 VDL::_parse()
00173 {
00174 while (! ifs->eof()) {
00175 ifs->get( _c );
00176
00177 switch ( _c ) {
00178 case '"':
00179 return _parse_string();
00180 case LST:
00181 return _parse_set();
00182 case RST:
00183 return 0;
00184 case LTP:
00185 return _parse_tuple();
00186 case RTP:
00187 return 0;
00188 case ',':
00189 return 0;
00190 default:
00191 if (isdigit(_c) || _c == '-')
00192 return _parse_digit();
00193 else if (! (isspace(_c) || _c == ';')) {
00194 ifs->putback( _c );
00195 return _parse_alpha();
00196 }
00197 break;
00198 }
00199 }
00200
00201 return 0;
00202 }
00203
00204 void
00205 VDL::open( const char *fname )
00206 {
00207 ifs = new ifstream( fname );
00208 }
00209
00210 void
00211 VDL::close()
00212 {
00213 delete ifs;
00214
00215 ifs = 0;
00216 }
00217
00218 Boolean
00219 VDL::eof()
00220 {
00221 return ((ifs == 0) || (ifs->eof()));
00222 }
00223
00224 BinRep *
00225 VDL::read()
00226 {
00227 p_assert( ifs, "Error: VDL::open not called." );
00228
00229 start_object();
00230
00231 _parse();
00232
00233 end_object();
00234
00235 BinRep *b = _bs;
00236
00237 _bs = 0;
00238
00239 return b;
00240 }
00241
00242
00243
00244 void
00245 VDL::_append( BinRep *b )
00246 {
00247 if (_stack.entries() == 0)
00248 _bs = b;
00249 else {
00250 BinRep &t = _stack[0];
00251
00252 if (t.is_set())
00253 t.to_set().ins( b );
00254 else if (t.is_tuple())
00255 t.to_tuple().ins_last( b );
00256 else
00257 p_abort( "not a set or tuple at top-level" );
00258 }
00259 }
00260
00261 void
00262 VDL::start_object()
00263 {
00264 if (_bs)
00265 delete _bs;
00266
00267 _bs = 0;
00268 _stack.clear();
00269 }
00270
00271 void
00272 VDL::end_object()
00273 {
00274 p_assert( _stack.entries() == 0, "not all objects are closed" );
00275 _stack.clear();
00276 }
00277
00278 void
00279 VDL::start_set()
00280 {
00281 BinRep *b = new BinRep;
00282 Set<BinRep> *S = new Set<BinRep>;
00283
00284 b->put_set( S );
00285 _stack.ins_first( b );
00286 }
00287
00288 void
00289 VDL::end_set()
00290 {
00291 BinRep *b = _stack.grab(0);
00292
00293 p_assert( b->is_set(), "not ending a set" );
00294 _append( b );
00295 }
00296
00297 void
00298 VDL::start_tuple()
00299 {
00300 BinRep *b = new BinRep;
00301 List<BinRep> *L = new List<BinRep>;
00302
00303 b->put_tuple( L );
00304 _stack.ins_first( b );
00305 }
00306
00307 void
00308 VDL::end_tuple()
00309 {
00310 BinRep *b = _stack.grab(0);
00311
00312 p_assert( b->is_tuple(), "not ending a tuple" );
00313 _append( b );
00314 }
00315
00316 void
00317 VDL::append_omega()
00318 {
00319 BinRep *b = new BinRep;
00320
00321 b->put_omega();
00322 _append( b );
00323 }
00324
00325 void
00326 VDL::append_integer(int value)
00327 {
00328 BinRep *b = new BinRep;
00329
00330 b->put_integer( value );
00331 _append( b );
00332 }
00333
00334 void
00335 VDL::append_boolean(Boolean value)
00336 {
00337 BinRep *b = new BinRep;
00338
00339 b->put_boolean( value );
00340 _append( b );
00341 }
00342
00343 void
00344 VDL::append_string(String &s)
00345 {
00346 BinRep *b = new BinRep;
00347
00348 b->put_string( s );
00349 _append( b );
00350 }
00351
00352
00353 void
00354 VDL::append_string( const char *p )
00355 {
00356 String s = p;
00357 append_string( s );
00358 }
00359
00360
00361
00362 int
00363 VDL::look_up_expr( Expression & ex )
00364 {
00365 IntElem *p = _map.find_ref( ex );
00366
00367 return (p ? p->value() : -1);
00368 }
00369
00370 int
00371 VDL::install_expr( Expression & ex, int index )
00372 {
00373 _map.ins( ex, new IntElem( index ) );
00374
00375 return index;
00376 }
00377
00378
|