File: GMLParser.h

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (387 lines) | stat: -rwxr-xr-x 9,444 bytes parent folder | download
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
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
#include <iostream>
#include <string>
#include <list>
#include <string.h>

/*
  Il faut ajouter la gestion correcte des erreurs et cest nikel.
 */

struct ParserError {
  ParserError(int err=0,int lin=0,int cha=0):errorNumber(err),lineInFile(lin),charInLine(cha) {}
  int errorNumber;
  int lineInFile;
  int charInLine;
};

struct GMLValue {
  std::string str;
  long integer;
  double real;
  bool boolean;
};

enum GMLToken { BOOLTOKEN,ENDOFSTREAM,STRINGTOKEN,INTTOKEN,DOUBLETOKEN,ERRORINFILE,OPENTOKEN,CLOSETOKEN };
//=====================================================================================
struct GMLTokenParser {
  int curLine;
  int curChar;
  std::istream &is;
  GMLTokenParser(std::istream &i):curLine(0),curChar(0),is(i) {}
  GMLToken nextToken(GMLValue &val) {
    val.str.erase();
    bool endOfStream=false,strGet=false,slashMode=false,started=false,stop=false;
    char ch;

    while ( (!stop) && (endOfStream=!(is.get(ch)).fail()) ) {
      curChar++;

      if (strGet)
        switch (ch) {
        case 13 :
          break;

        case '\n':
          curChar=0;
          curLine++;
          val.str+=ch;
          break;

        case '\\':

          if (!slashMode) {
            slashMode=true;
          }
          else {
            val.str+=ch;
            slashMode=false;
          }

          break;

        case '"':

          if (!slashMode) {
            return STRINGTOKEN;
          }
          else {
            val.str+=ch;
            slashMode=false;
          }

          break;

        default:
          slashMode=false;
          val.str+=ch;
          break;
        }
      else
        switch (ch) {
        case 13 :
          break;

        case ' ' :

          if (started) stop=true;

          break;

        case '\t':

          if (started) stop=true;

          break;

        case '\n':
          curChar=0;
          curLine++;

          if (started) stop=true;

          break;

        case '[':

          if (!started) return OPENTOKEN;
          else {
            is.unget();
            stop=true;
          }

          break;

        case ']':

          if (!started) return CLOSETOKEN;
          else {
            is.unget();
            stop=true;
          }

          break;

        case '"':
          strGet=true;

          if (started) {
            is.unget();
            stop=true;
          }
          else started=true;

          break;

        default :
          val.str+=ch;
          started=true;
          break;
        }
    }

    if (!started && !endOfStream) return ENDOFSTREAM;

    char *endPtr=0;
    long resultl=strtol(val.str.c_str(),&endPtr,10);

    if (endPtr==(val.str.c_str()+val.str.length())) {
      val.integer=resultl;
      return INTTOKEN;
    }

    endPtr=0;
    double resultd=strtod(val.str.c_str(),&endPtr);

    if (endPtr==(val.str.c_str()+val.str.length())) {
      val.real=resultd;
      return DOUBLETOKEN;
    }

    if (strcasecmp(val.str.c_str(),"true")==0) {
      val.boolean=true;
      return BOOLTOKEN;
    }

    if (strcasecmp(val.str.c_str(),"false")==0) {
      val.boolean=false;
      return BOOLTOKEN;
    }

    if (started) return STRINGTOKEN;

    return ERRORINFILE;
  }
};
//=====================================================================================
struct GMLBuilder {
  virtual ~GMLBuilder() {}
  virtual bool addBool(const std::string &,const bool)=0;
  virtual bool addInt(const std::string &,const int)=0;
  virtual bool addDouble(const std::string &,const double)=0;
  virtual bool addString(const std::string &,const std::string &)=0;
  virtual bool addStruct(const std::string&,GMLBuilder*&)=0;
  virtual bool close() =0;
};

struct GMLTrue:public GMLBuilder {
  bool addBool(const std::string &,const bool) {
    return true;
  }
  bool addInt(const std::string &,const int) {
    return true;
  }
  bool addDouble(const std::string &,const double) {
    return true;
  }
  bool addString(const std::string &,const std::string &) {
    return true;
  }
  bool addStruct(const std::string&,GMLBuilder*&newBuilder) {
    newBuilder=new GMLTrue();
    return true;
  }
  bool close() {
    return true;
  }
};

struct GMLFalse:public GMLBuilder {
  bool addBool(const std::string &,const bool) {
    return false;
  }
  bool addInt(const std::string &,const int) {
    return false;
  }
  bool addDouble(const std::string &,const double) {
    return false;
  }
  bool addString(const std::string &,const std::string &) {
    return false;
  }
  bool addStruct(const std::string&,GMLBuilder*&newBuilder) {
    newBuilder=new GMLFalse();
    return false;
  }
  bool close() {
    return true;
  }
};

struct GMLWriter:public GMLBuilder {
  bool addBool(const std::string &st,const bool boolean)  {
    std::cout << st << " ==> " << "bool::" << boolean << std::endl;
    return true;
  }
  bool addInt(const std::string &st,const int integer)  {
    std::cout << st << " ==> " << "int::" << integer << std::endl;
    return true;
  }
  bool addDouble(const std::string &st,const double real) {
    std::cout.flags(std::ios::scientific);
    std::cout << st << " ==> " << "real::" << real << std::endl;
    return true;
  }
  bool addString(const std::string &st,const std::string &str)  {
    std::cout << st << " ==> " << "string::" << str << std::endl;
    return true;
  }
  bool addStruct(const std::string& structName,GMLBuilder*&newBuilder) {
    std::cout << "struct::" << structName << std::endl;
    newBuilder=new GMLWriter();
    return true;
  }
  bool close() {
    std::cout << "EndStruct::"<< std::endl;
    return true;
  }
};
//=====================================================================================
template <bool displayComment>
struct GMLParser {
  std::list<GMLBuilder *> builderStack;
  std::istream &inputStream;

  GMLParser(std::istream &inputStream,GMLBuilder *builder):inputStream(inputStream) {
    builderStack.push_front(builder);
  }
  ~GMLParser() {
    while (!builderStack.empty()) {
      delete builderStack.front();
      builderStack.pop_front();
    }
  }
  bool parse() {
    GMLTokenParser tokenParser(inputStream);
    GMLToken currentToken;
    GMLToken nextToken;
    GMLValue currentValue;
    GMLValue nextValue;

    while ((currentToken=tokenParser.nextToken(currentValue))!=ENDOFSTREAM) {
      switch (currentToken) {
      case STRINGTOKEN:
        nextToken=tokenParser.nextToken(nextValue);

        switch (nextToken) {
        case OPENTOKEN:
          GMLBuilder *newBuilder;

          if (builderStack.front()->addStruct(currentValue.str,newBuilder)) {
            builderStack.push_front(newBuilder);
          }
          else {
            return false;
          }

          break;

        case BOOLTOKEN:

          if (!builderStack.front()->addBool(currentValue.str,nextValue.boolean)) {
            std::cerr << "Error parsing stream line :" << tokenParser.curLine << " char : " << tokenParser.curChar << std::endl;
            return false;
          }

          break;

        case INTTOKEN:

          if (!builderStack.front()->addInt(currentValue.str,nextValue.integer)) {
            std::cerr << "Error parsing stream line :" << tokenParser.curLine << " char : " << tokenParser.curChar << std::endl;
            return false;
          }

          break;

        case DOUBLETOKEN:

          if (!builderStack.front()->addDouble(currentValue.str,nextValue.real)) {
            std::cerr << "Error parsing stream line :" << tokenParser.curLine << " char : " << tokenParser.curChar << std::endl;
            return false;
          }

          break;

        case STRINGTOKEN:

          if (!builderStack.front()->addString(currentValue.str,nextValue.str)) {
            std::cerr << "Error parsing stream line :" << tokenParser.curLine << " char : " << tokenParser.curChar << std::endl;
            return false;
          }

          break;

        case ERRORINFILE:
          return false;
          break;

        case ENDOFSTREAM:
          return true;
          break;

        default:
          break;
        }

        break;

      case CLOSETOKEN:

        if (builderStack.front()->close())
          delete builderStack.front();
        else {
          std::cerr << "Error parsing stream line :" << tokenParser.curLine << " char : " << tokenParser.curChar << std::endl;
          return false;
        }

        builderStack.pop_front();
        break;

      default:
        std::cerr << "Error parsing stream line :" << tokenParser.curLine << " char : " << tokenParser.curChar << std::endl;
        return false;
      }
    }

    return true;
  }
};
//=====================================================================================