parser.cpp
author insilmaril
Mon, 05 Mar 2007 23:22:51 +0000
changeset 432 f867269ab8a1
child 445 0796c5592f00
permissions -rw-r--r--
1.8.69 Some more scripting functionality (for testing)
insilmaril@432
     1
#include "parser.h"
insilmaril@432
     2
insilmaril@432
     3
#include <QRegExp>
insilmaril@432
     4
#include <iostream>
insilmaril@432
     5
insilmaril@432
     6
using namespace std;
insilmaril@432
     7
insilmaril@432
     8
Parser::Parser()
insilmaril@432
     9
{
insilmaril@432
    10
	initCommand();
insilmaril@432
    11
}
insilmaril@432
    12
insilmaril@432
    13
void Parser::initCommand()
insilmaril@432
    14
{
insilmaril@432
    15
	com="";
insilmaril@432
    16
	paramList.clear();
insilmaril@432
    17
	resetError();
insilmaril@432
    18
}
insilmaril@432
    19
insilmaril@432
    20
void Parser::parseAtom (const QString &s)
insilmaril@432
    21
{
insilmaril@432
    22
	initCommand();
insilmaril@432
    23
	input=s;
insilmaril@432
    24
	QRegExp re;
insilmaril@432
    25
	int pos;
insilmaril@432
    26
insilmaril@432
    27
	// Get command
insilmaril@432
    28
	re.setPattern ("\\b(.*)(\\s|\\()");
insilmaril@432
    29
	re.setMinimal (true);
insilmaril@432
    30
	pos=re.search (s);
insilmaril@432
    31
	if (pos>=0)
insilmaril@432
    32
		com=re.cap(1);
insilmaril@432
    33
insilmaril@432
    34
	// Get parameters
insilmaril@432
    35
	paramList.clear();
insilmaril@432
    36
	re.setPattern ("\\((.*)\\)");
insilmaril@432
    37
	pos=re.search (s);
insilmaril@432
    38
	//cout << "  s="<<s.ascii()<<endl;
insilmaril@432
    39
	//cout << "com="<<com.ascii()<<"  pos="<<pos<<endl<<endl;
insilmaril@432
    40
	if (pos>=0)
insilmaril@432
    41
	{
insilmaril@432
    42
		QString s=re.cap(1);
insilmaril@432
    43
		QString a;
insilmaril@432
    44
		bool inquote=false;
insilmaril@432
    45
		pos=0;
insilmaril@432
    46
		if (!s.isEmpty())
insilmaril@432
    47
		{
insilmaril@432
    48
			while (pos<s.length())
insilmaril@432
    49
			{
insilmaril@432
    50
				if (s.at(pos)=='\"') 
insilmaril@432
    51
				{
insilmaril@432
    52
					if (inquote)
insilmaril@432
    53
						inquote=false;
insilmaril@432
    54
					else	
insilmaril@432
    55
						inquote=true;
insilmaril@432
    56
				}
insilmaril@432
    57
insilmaril@432
    58
				if (s.at(pos)==',' && !inquote)
insilmaril@432
    59
				{
insilmaril@432
    60
					a=s.left(pos);
insilmaril@432
    61
					paramList.append(a);
insilmaril@432
    62
					s=s.right(s.length()-pos-1);
insilmaril@432
    63
					pos=0;
insilmaril@432
    64
				} else
insilmaril@432
    65
					pos++;
insilmaril@432
    66
				
insilmaril@432
    67
			}
insilmaril@432
    68
			paramList.append (s);
insilmaril@432
    69
		}	
insilmaril@432
    70
	}	
insilmaril@432
    71
}
insilmaril@432
    72
insilmaril@432
    73
QString Parser::command()
insilmaril@432
    74
{
insilmaril@432
    75
	return com;
insilmaril@432
    76
}
insilmaril@432
    77
insilmaril@432
    78
QStringList Parser::parameters()
insilmaril@432
    79
{
insilmaril@432
    80
	return paramList;
insilmaril@432
    81
}
insilmaril@432
    82
insilmaril@432
    83
int Parser::paramCount()
insilmaril@432
    84
{
insilmaril@432
    85
	return paramList.count();
insilmaril@432
    86
}
insilmaril@432
    87
insilmaril@432
    88
insilmaril@432
    89
QString Parser::errorMessage()
insilmaril@432
    90
{
insilmaril@432
    91
	QString l;
insilmaril@432
    92
	switch (errLevel)
insilmaril@432
    93
	{
insilmaril@432
    94
		case NoError: l="No Error";
insilmaril@432
    95
		case Warning: l="Warning";
insilmaril@432
    96
		case Aborted: l="Aborted";
insilmaril@432
    97
	}
insilmaril@432
    98
	return QString ("Error Level: %1\n    Command: %2\nDescription: %3")
insilmaril@432
    99
		.arg(l).arg(com).arg(errDescription);
insilmaril@432
   100
}
insilmaril@432
   101
insilmaril@432
   102
QString Parser::errorDescription()
insilmaril@432
   103
{
insilmaril@432
   104
	return errDescription;
insilmaril@432
   105
}
insilmaril@432
   106
insilmaril@432
   107
ErrorLevel Parser::errorLevel()
insilmaril@432
   108
{
insilmaril@432
   109
	return errLevel;
insilmaril@432
   110
}
insilmaril@432
   111
insilmaril@432
   112
void Parser::setError(ErrorLevel level, const QString &description)
insilmaril@432
   113
{
insilmaril@432
   114
	errDescription=description;
insilmaril@432
   115
	errLevel=level;
insilmaril@432
   116
}
insilmaril@432
   117
insilmaril@432
   118
void Parser::resetError ()
insilmaril@432
   119
{
insilmaril@432
   120
	errMessage="";
insilmaril@432
   121
	errDescription="";
insilmaril@432
   122
	errLevel=NoError;
insilmaril@432
   123
}
insilmaril@432
   124
insilmaril@432
   125
insilmaril@432
   126
bool Parser::checkParamCount (QList <int> plist)
insilmaril@432
   127
{
insilmaril@432
   128
	QStringList expList;
insilmaril@432
   129
	QString expected;
insilmaril@432
   130
	for (int i=0; i<plist.count();i++)
insilmaril@432
   131
	{
insilmaril@432
   132
		if (checkParamCount (plist[i])) 
insilmaril@432
   133
		{
insilmaril@432
   134
			resetError();
insilmaril@432
   135
			return true;
insilmaril@432
   136
		}
insilmaril@432
   137
		expList.append(QString().setNum(plist[i]));
insilmaril@432
   138
	}	
insilmaril@432
   139
	expected=expList.join(",");	
insilmaril@432
   140
	errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
insilmaril@432
   141
	return false;
insilmaril@432
   142
}
insilmaril@432
   143
insilmaril@432
   144
bool Parser::checkParamCount (const int &expected)
insilmaril@432
   145
{
insilmaril@432
   146
	if (paramList.count()!=expected)
insilmaril@432
   147
	{
insilmaril@432
   148
		errLevel=Aborted;
insilmaril@432
   149
		errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
insilmaril@432
   150
		return false;
insilmaril@432
   151
	} 
insilmaril@432
   152
	return true;	
insilmaril@432
   153
}
insilmaril@432
   154
insilmaril@432
   155
bool Parser::checkParamIsInt(const int &index)
insilmaril@432
   156
{
insilmaril@432
   157
	bool ok;
insilmaril@432
   158
	if (index > paramList.count())
insilmaril@432
   159
	{
insilmaril@432
   160
		errLevel=Aborted;
insilmaril@432
   161
		errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
insilmaril@432
   162
		return false;
insilmaril@432
   163
	} else
insilmaril@432
   164
	{
insilmaril@432
   165
		paramList[index].toInt (&ok, 10);
insilmaril@432
   166
		if (!ok)
insilmaril@432
   167
		{
insilmaril@432
   168
			errLevel=Aborted;
insilmaril@432
   169
			errDescription=QString("Parameter %1 is not an integer").arg(index);
insilmaril@432
   170
			return false;
insilmaril@432
   171
		} 
insilmaril@432
   172
	}	
insilmaril@432
   173
	return true;
insilmaril@432
   174
}
insilmaril@432
   175
insilmaril@432
   176
int Parser::parInt (bool &ok,const uint &index)
insilmaril@432
   177
{
insilmaril@432
   178
	if (checkParamIsInt (index))
insilmaril@432
   179
		return paramList[index].toInt (&ok, 10);
insilmaril@432
   180
	ok=false;
insilmaril@432
   181
	return 0;
insilmaril@432
   182
}
insilmaril@432
   183
insilmaril@432
   184
QString Parser::parString (bool &ok,const int &index)
insilmaril@432
   185
{
insilmaril@432
   186
	// return the string at index, this could be also stored in
insilmaril@432
   187
	// a variable later
insilmaril@432
   188
	QString r;
insilmaril@432
   189
	QRegExp re("\"(.*)\"");
insilmaril@432
   190
	int pos=re.search (paramList[index]);
insilmaril@432
   191
	if (pos>=0)
insilmaril@432
   192
		r=re.cap (1);
insilmaril@432
   193
	else	
insilmaril@432
   194
		r="";
insilmaril@432
   195
	ok=true;
insilmaril@432
   196
	return r;
insilmaril@432
   197
}
insilmaril@432
   198
insilmaril@432
   199
bool Parser::parBool (bool &ok,const int &index)
insilmaril@432
   200
{
insilmaril@432
   201
	// return the bool at index, this could be also stored in
insilmaril@432
   202
	// a variable later
insilmaril@432
   203
	QString r;
insilmaril@432
   204
	ok=true;
insilmaril@432
   205
	QString p=paramList[index];
insilmaril@432
   206
	if (p=="true" || p=="1")
insilmaril@432
   207
		return true;
insilmaril@432
   208
	else if	(p=="false" || p=="0")
insilmaril@432
   209
		return false;
insilmaril@432
   210
	ok=false;
insilmaril@432
   211
	return ok;
insilmaril@432
   212
}
insilmaril@432
   213
insilmaril@432
   214
QColor Parser::parColor(bool &ok,const int &index)
insilmaril@432
   215
{
insilmaril@432
   216
	// return the QColor at index
insilmaril@432
   217
	ok=true;
insilmaril@432
   218
	return QColor (paramList[index]);
insilmaril@432
   219
}
insilmaril@432
   220
insilmaril@432
   221
void Parser::setScript(const QString &s)
insilmaril@432
   222
{
insilmaril@432
   223
	script=s;
insilmaril@432
   224
}	
insilmaril@432
   225
insilmaril@432
   226
QString Parser::getScript()
insilmaril@432
   227
{
insilmaril@432
   228
	return script;
insilmaril@432
   229
}	
insilmaril@432
   230
insilmaril@432
   231
void Parser::startScript()
insilmaril@432
   232
{
insilmaril@432
   233
}	
insilmaril@432
   234