Menu

[557893]: / parser.cpp  Maximize  Restore  History

Download this file

347 lines (308 with data), 6.2 kB

  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
#include "parser.h"
#include <QRegExp>
#include <iostream>
using namespace std;
Parser::Parser()
{
initParser();
}
void Parser::initParser()
{
initAtom();
current=-1;
}
void Parser::initAtom()
{
atom="";
com="";
paramList.clear();
resetError();
}
void Parser::parseAtom (QString s)
{
initAtom();
atom=s;
QRegExp re;
int pos;
// Strip WS at beginning
re.setPattern ("\\w");
re.setMinimal (true);
pos=re.indexIn (atom);
if (pos>=0)
s=s.right(s.length()-pos);
// Get command
re.setPattern ("\\b(.*)(\\s|\\()");
pos=re.indexIn (s);
if (pos>=0)
com=re.cap(1);
// Get parameters
paramList.clear();
re.setPattern ("\\((.*)\\)");
pos=re.indexIn (s);
//cout << " s="<<qPrintable(s)<<endl;
//cout << "com="<<qPrintable(com)<<" pos="<<pos<<endl<<endl;
if (pos>=0)
{
QString s=re.cap(1);
QString a;
bool inquote=false;
pos=0;
if (!s.isEmpty())
{
while (pos<s.length())
{
if (s.at(pos)=='\"')
{
if (inquote)
inquote=false;
else
inquote=true;
}
if (s.at(pos)==',' && !inquote)
{
a=s.left(pos);
paramList.append(a);
s=s.right(s.length()-pos-1);
pos=0;
} else
pos++;
}
paramList.append (s);
}
}
}
QString Parser::getAtom()
{
return atom;
}
QString Parser::getCommand()
{
return com;
}
QStringList Parser::getParameters()
{
return paramList;
}
int Parser::parCount()
{
return paramList.count();
}
QString Parser::errorMessage()
{
QString l;
switch (errLevel)
{
case NoError: l="No Error";
case Warning: l="Warning";
case Aborted: l="Aborted";
}
return QString ("Error Level: %1\n Command: %2\nDescription: %3")
.arg(l).arg(com).arg(errDescription);
}
QString Parser::errorDescription()
{
return errDescription;
}
ErrorLevel Parser::errorLevel()
{
return errLevel;
}
void Parser::setError(ErrorLevel level, const QString &description)
{
errDescription=description;
errLevel=level;
}
void Parser::resetError ()
{
errMessage="";
errDescription="";
errLevel=NoError;
}
bool Parser::checkParCount (QList <int> plist)
{
QStringList expList;
QString expected;
for (int i=0; i<plist.count();i++)
{
if (checkParCount (plist[i]))
{
resetError();
return true;
}
expList.append(QString().setNum(plist[i]));
}
expected=expList.join(",");
errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
return false;
}
bool Parser::checkParCount (const int &expected)
{
if (paramList.count()!=expected)
{
errLevel=Aborted;
errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
return false;
}
return true;
}
bool Parser::checkParIsInt(const int &index)
{
bool ok;
if (index > paramList.count())
{
errLevel=Aborted;
errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
return false;
} else
{
paramList[index].toInt (&ok, 10);
if (!ok)
{
errLevel=Aborted;
errDescription=QString("Parameter %1 is not an integer").arg(index);
return false;
}
}
return true;
}
bool Parser::checkParIsDouble(const int &index)
{
bool ok;
if (index > paramList.count())
{
errLevel=Aborted;
errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
return false;
} else
{
paramList[index].toDouble (&ok);
if (!ok)
{
errLevel=Aborted;
errDescription=QString("Parameter %1 is not double").arg(index);
return false;
}
}
return true;
}
int Parser::parInt (bool &ok,const uint &index)
{
if (checkParIsInt (index))
return paramList[index].toInt (&ok, 10);
ok=false;
return 0;
}
QString Parser::parString (bool &ok,const int &index)
{
// return the string at index, this could be also stored in
// a variable later
QString r;
QRegExp re("\"(.*)\"");
int pos=re.indexIn (paramList[index]);
if (pos>=0)
r=re.cap (1);
else
r="";
ok=true;
return r;
}
bool Parser::parBool (bool &ok,const int &index)
{
// return the bool at index, this could be also stored in
// a variable later
QString r;
ok=true;
QString p=paramList[index];
if (p=="true" || p=="1")
return true;
else if (p=="false" || p=="0")
return false;
ok=false;
return ok;
}
QColor Parser::parColor(bool &ok,const int &index)
{
// return the QColor at index
ok=false;
QString r;
QColor c;
QRegExp re("\"(.*)\"");
int pos=re.indexIn (paramList[index]);
if (pos>=0)
{
r=re.cap (1);
c.setNamedColor(r);
ok=c.isValid();
}
return c;
}
double Parser::parDouble (bool &ok,const int &index)
{
if (checkParIsDouble (index))
return paramList[index].toDouble (&ok);
ok=false;
return 0;
}
void Parser::setScript(const QString &s)
{
script=s;
}
QString Parser::getScript()
{
return script;
}
void Parser::runScript()
{
current=0;
}
bool Parser::next()
{
int start=current;
if (current<0) runScript();
if (current+1>=script.length()) return false;
bool inBracket=false;
while (true)
{
//cout <<"current="<<current<< " start="<<start<<" length="<<script.length()<<endl;
// Check if we are inside a string
if (script.at(current)=='"')
{
if (inBracket)
inBracket=false;
else
inBracket=true;
}
// Check if we are in a comment
if (!inBracket && script.at(current)=='#')
{
while (script.at(current)!='\n')
{
current++;
if (current+1>=script.length())
return false;
}
start=current;
}
// Check for end of atom
if (!inBracket && script.at(current)==';')
{
atom=script.mid(start,current-start);
current++;
return true;
}
// Check for end of script
if (current+1>=script.length() )
{
if (inBracket)
{
setError (Aborted,"Runaway string");
return false;
} else
{
atom=script.mid(start);
return true;
}
}
current++;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.