1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
//$$FILE$$
//$$VERSION$$
//$$DATE$$
//$$LICENSE$$
/*!
** \file CifScannerBase.C
**
** \brief Implementation file for CifScanner class.
*/
/*
PURPOSE: A DDL 2.1 compliant CIF file parser.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CifScannerBase.h"
#include "CifScannerInt.h"
#include "CifParserBase.h"
#include "CifParser.h"
extern int cifparser_leng;
extern char* cifparser_text;
extern YYSTYPE cifparser_lval;
extern FILE* cifparser_in;
#define yyleng cifparser_leng
#define yytext cifparser_text
#define yylval cifparser_lval
#define yyin cifparser_in
extern "C" void cif_yy_less(int i);
extern CifParser* CifParserP;
using std::ios;
using std::endl;
CifScanner::CifScanner() {
Clear();
_verbose=false;
// VLAD: WATCH HERE
_tBuf = new string;
_tBuf->clear();
if (_verbose) log << "Default constructor called" << endl;
}
void CifScanner::OpenLog(const string& logName, bool verboseLevel)
{
_verbose = verboseLevel;
if (!logName.empty()) log.open(logName.c_str(),ios::out|ios::trunc);
}
/*
CifScanner::CifScanner(istream *in) {
Clear();
_verbose=false;
_tBuf = new CifString(1025,512);
_tBuf->Clear();
yyin=in;
if (_verbose) log << "CifScanner::CifScanner(istream *in, int verbose) constructor called" << endl;
}
*/
void CifScanner::Clear(void) {
_tBuf=NULL;
_isText = false;
errorLog.clear();
NDBlineNo=1;
}
void CifScanner::Reset(void) {
if (_tBuf) delete _tBuf;
_tBuf=NULL;
Clear();
}
int CifScanner::yylex()
{
return(0);
}
int CifScanner::ProcessNone()
{
#if DEBUG
log << "LS0: line "<< NDBlineNo << " length " << yyleng << " yytext=" << yytext << endl;
#endif
NDBlineNo++;
if (_isText == true) { /* end of text value */
#ifdef VLAD_DEBUG
cout << "DEBUG - In CifScanner::ProcessNone() - end of multi-line text \"" << yytext << "\"" << endl;
#endif
// Check if the first character is semicolon followed by a
// non-newline. This is considered invalid.
if ((yyleng > 1) && ((yytext[0] == ';') && (yytext[1] != '\n')))
{
log << "ERROR - Invalid syntax. Improperly placed semicolon in line " << NDBlineNo - 1 << endl;
errorLog += "ERROR - Invalid syntax. Improperly placed semicolon in line ";
errorLog += String::IntToString(NDBlineNo - 1);
errorLog += '\n';
}
for (_i=yyleng-1; _i >= 0; _i--) {
if ( yytext[_i] == ' ' || yytext[_i] == '\t' || yytext[_i] == '\n' || yytext[_i] == '\r') {
yytext[_i]='\0';
} else if ( yytext[_i] == ';') {
yytext[_i]='\0';
break;
} else
break;
}
(*_tBuf)+=yytext;
_tBuf->erase(strlen(_tBuf->c_str())-1,1);
yylval.cBuf=(char*)_tBuf->c_str();
_isText = false;
#if DEBUG
log << "LS1: String[" << strlen(yylval.cBuf) << "] " << yylval.cBuf << endl;
#endif
return(LSITEMVALUE_CIF);
} else { /* text value begins */
#ifdef VLAD_DEBUG
cout << "DEBUG - In CifScanner::ProcessNone() - begin of multi-line text \"" << yytext << "\"" << endl;
#endif
_isText = true;
for (_i=0; _i < yyleng; _i++) {
if (yytext[_i] == ';') { break; }
}
_tBuf->clear();
string tmpP;
for (unsigned int tmpI = 0; tmpI < strlen(&yytext[_i+1]); tmpI++)
{
if (yytext[_i+1+tmpI] != '\r')
{
tmpP.push_back(yytext[_i+1+tmpI]);
}
}
(*_tBuf) += tmpP;
}
return (0);
}
void CifScanner::ProcessWhiteSpace()
{
for (_i=0; _i < yyleng; _i++)
if (yytext[_i] == '\n') NDBlineNo++;
if (_isText)
(*_tBuf) += yytext;
}
int CifScanner::ProcessData()
{
if (_isText)
(*_tBuf) += yytext;
else {
yylval.cBuf=yytext;
return (DATABLOCK_CIF);
}
return (0);
}
int CifScanner::ProcessLoopScanner()
{
if (_isText)
(*_tBuf) += yytext;
else
return (LOOP_CIF);
return (0);
}
void CifScanner::ProcessStop()
{
if (_isText)
(*_tBuf) += yytext;
}
int CifScanner::ProcessDot()
{
if (_isText)
(*_tBuf) += yytext;
else
return (UNKNOWN_CIF);
return (0);
}
int CifScanner::ProcessQuestion()
{
if (_isText)
(*_tBuf) += yytext;
else
return (MISSING_CIF);
return (0);
}
void CifScanner::ProcessComment()
{
if (_isText)
(*_tBuf) += yytext;
}
int CifScanner::ProcessUnderscore()
{
if (_isText) {
(*_tBuf) += yytext;
} else {
/* If the beginning of text is in buffer yytext */
for (_i=0; _i<yyleng; _i++)
if (yytext[_i] == '_') break;
yylval.cBuf=yytext;
return(ITEMNAME_CIF);
}
return (0);
}
int CifScanner::ProcessBadStrings()
{
if (!_isText) {
/*
** The string is not part of a multiline CIF value, but a CIF value
** anywhere else, in a loop or out of the loop.
*/
_j=0;
yylval.cBuf=&yytext[_j];
#if DEBUG
log << "UQ: String " << yylval.cBuf << endl;
#endif
#ifdef REPORT_EMBEDDED_QUOTES
unsigned int cBufLen = strlen(yylval.cBuf);
for (unsigned int i = 0; i < cBufLen; ++i)
{
if ((yylval.cBuf[i] == '\'') || (yylval.cBuf[i] == '\"'))
{
log << "ERROR - Invalid character at line " <<
String::IntToString(NDBlineNo) << " in CIF value " <<
yylval.cBuf << endl;
errorLog += "ERROR - Invalid character at line ";
errorLog += String::IntToString(NDBlineNo);
errorLog += " in CIF value ";
errorLog += yylval.cBuf;
errorLog += '\n';
}
}
#endif // REPORT_EMBEDDED_QUOTES
return(ITEMVALUE_CIF);
}
else {
/*
** The string is part of a multiline CIF value. It is processed as is.
*/
#if DEBUG
log << "UQx: String " << yytext<< endl;
#endif
(*_tBuf) += yytext;
}
return (0);
}
int CifScanner::ProcessSQuotedStrings()
{
char * p;
if (!_isText) {
p=yytext;
p++;
while ((p=strchr(p,'\''))) {
p++;
if ( p[0] == ' ' || p[0] == '\t' || p[0] == '\n' || p[0] == '\r') {
_i=yyleng-strlen(p);
cif_yy_less(_i);
p=&yytext[yyleng];
}
}
yylval.cBuf=&yytext[1];
yylval.cBuf[_i-2]='\0';
return(ITEMVALUE_CIF);
}
else {
if (yytext[yyleng-1] == '\n')
{
if (yytext[yyleng-2] == '\r')
{
cif_yy_less(yyleng-2);
}
else
{
cif_yy_less(yyleng-1);
}
}
(*_tBuf) += yytext;
}
return (0);
}
int CifScanner::ProcessDQuotedStrings()
{
char * p;
if (!_isText) {
p=yytext;
p++;
while ((p=strchr(p,'\"'))) {
p++;
if ( p[0] == ' ' || p[0] == '\t' || p[0] == '\n' || p[0] == '\r') {
_i=yyleng-strlen(p);
cif_yy_less(_i);
p=&yytext[yyleng];
}
}
yylval.cBuf=&yytext[1];
yylval.cBuf[_i-2]='\0';
return(ITEMVALUE_CIF);
}
else {
if (yytext[yyleng-1] == '\n')
{
if (yytext[yyleng-2] == '\r')
{
cif_yy_less(yyleng-2);
}
else
{
cif_yy_less(yyleng-1);
}
}
(*_tBuf) += yytext;
}
return (0);
}
int CifScanner::ProcessEof()
{
if (_isText == true) {
_isText=false;
errorLog += "ERROR - String is not finished above line ";
errorLog += String::IntToString(NDBlineNo);
errorLog += '\n';
log<<"ERROR - String is not finished above line "<<NDBlineNo<< endl;
return(1);
}
else
return(0);
}
int ProcessNoneFromScanner()
{
return (CifParserP->ProcessNone());
}
void ProcessWhiteSpaceFromScanner()
{
CifParserP->ProcessWhiteSpace();
}
int ProcessDataFromScanner()
{
return (CifParserP->ProcessData());
}
int ProcessLoopFromScanner()
{
return (CifParserP->ProcessLoopScanner());
}
void ProcessStopFromScanner()
{
CifParserP->ProcessStop();
}
int ProcessDotFromScanner()
{
return (CifParserP->ProcessDot());
}
int ProcessQuestionFromScanner()
{
return (CifParserP->ProcessQuestion());
}
void ProcessCommentFromScanner()
{
CifParserP->ProcessComment();
}
int ProcessUnderscoreFromScanner()
{
return (CifParserP->ProcessUnderscore());
}
int ProcessBadStringsFromScanner()
{
return (CifParserP->ProcessBadStrings());
}
int ProcessSQuotedStringsFromScanner()
{
return (CifParserP->ProcessSQuotedStrings());
}
int ProcessDQuotedStringsFromScanner()
{
return (CifParserP->ProcessDQuotedStrings());
}
int ProcessEofFromScanner()
{
return (CifParserP->ProcessEof());
}
|