parser.cpp
changeset 435 bd71dfb2292c
child 445 0796c5592f00
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser.cpp	Wed Mar 21 11:51:38 2007 +0000
     1.3 @@ -0,0 +1,234 @@
     1.4 +#include "parser.h"
     1.5 +
     1.6 +#include <QRegExp>
     1.7 +#include <iostream>
     1.8 +
     1.9 +using namespace std;
    1.10 +
    1.11 +Parser::Parser()
    1.12 +{
    1.13 +	initCommand();
    1.14 +}
    1.15 +
    1.16 +void Parser::initCommand()
    1.17 +{
    1.18 +	com="";
    1.19 +	paramList.clear();
    1.20 +	resetError();
    1.21 +}
    1.22 +
    1.23 +void Parser::parseAtom (const QString &s)
    1.24 +{
    1.25 +	initCommand();
    1.26 +	input=s;
    1.27 +	QRegExp re;
    1.28 +	int pos;
    1.29 +
    1.30 +	// Get command
    1.31 +	re.setPattern ("\\b(.*)(\\s|\\()");
    1.32 +	re.setMinimal (true);
    1.33 +	pos=re.search (s);
    1.34 +	if (pos>=0)
    1.35 +		com=re.cap(1);
    1.36 +
    1.37 +	// Get parameters
    1.38 +	paramList.clear();
    1.39 +	re.setPattern ("\\((.*)\\)");
    1.40 +	pos=re.search (s);
    1.41 +	//cout << "  s="<<s.ascii()<<endl;
    1.42 +	//cout << "com="<<com.ascii()<<"  pos="<<pos<<endl<<endl;
    1.43 +	if (pos>=0)
    1.44 +	{
    1.45 +		QString s=re.cap(1);
    1.46 +		QString a;
    1.47 +		bool inquote=false;
    1.48 +		pos=0;
    1.49 +		if (!s.isEmpty())
    1.50 +		{
    1.51 +			while (pos<s.length())
    1.52 +			{
    1.53 +				if (s.at(pos)=='\"') 
    1.54 +				{
    1.55 +					if (inquote)
    1.56 +						inquote=false;
    1.57 +					else	
    1.58 +						inquote=true;
    1.59 +				}
    1.60 +
    1.61 +				if (s.at(pos)==',' && !inquote)
    1.62 +				{
    1.63 +					a=s.left(pos);
    1.64 +					paramList.append(a);
    1.65 +					s=s.right(s.length()-pos-1);
    1.66 +					pos=0;
    1.67 +				} else
    1.68 +					pos++;
    1.69 +				
    1.70 +			}
    1.71 +			paramList.append (s);
    1.72 +		}	
    1.73 +	}	
    1.74 +}
    1.75 +
    1.76 +QString Parser::command()
    1.77 +{
    1.78 +	return com;
    1.79 +}
    1.80 +
    1.81 +QStringList Parser::parameters()
    1.82 +{
    1.83 +	return paramList;
    1.84 +}
    1.85 +
    1.86 +int Parser::paramCount()
    1.87 +{
    1.88 +	return paramList.count();
    1.89 +}
    1.90 +
    1.91 +
    1.92 +QString Parser::errorMessage()
    1.93 +{
    1.94 +	QString l;
    1.95 +	switch (errLevel)
    1.96 +	{
    1.97 +		case NoError: l="No Error";
    1.98 +		case Warning: l="Warning";
    1.99 +		case Aborted: l="Aborted";
   1.100 +	}
   1.101 +	return QString ("Error Level: %1\n    Command: %2\nDescription: %3")
   1.102 +		.arg(l).arg(com).arg(errDescription);
   1.103 +}
   1.104 +
   1.105 +QString Parser::errorDescription()
   1.106 +{
   1.107 +	return errDescription;
   1.108 +}
   1.109 +
   1.110 +ErrorLevel Parser::errorLevel()
   1.111 +{
   1.112 +	return errLevel;
   1.113 +}
   1.114 +
   1.115 +void Parser::setError(ErrorLevel level, const QString &description)
   1.116 +{
   1.117 +	errDescription=description;
   1.118 +	errLevel=level;
   1.119 +}
   1.120 +
   1.121 +void Parser::resetError ()
   1.122 +{
   1.123 +	errMessage="";
   1.124 +	errDescription="";
   1.125 +	errLevel=NoError;
   1.126 +}
   1.127 +
   1.128 +
   1.129 +bool Parser::checkParamCount (QList <int> plist)
   1.130 +{
   1.131 +	QStringList expList;
   1.132 +	QString expected;
   1.133 +	for (int i=0; i<plist.count();i++)
   1.134 +	{
   1.135 +		if (checkParamCount (plist[i])) 
   1.136 +		{
   1.137 +			resetError();
   1.138 +			return true;
   1.139 +		}
   1.140 +		expList.append(QString().setNum(plist[i]));
   1.141 +	}	
   1.142 +	expected=expList.join(",");	
   1.143 +	errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
   1.144 +	return false;
   1.145 +}
   1.146 +
   1.147 +bool Parser::checkParamCount (const int &expected)
   1.148 +{
   1.149 +	if (paramList.count()!=expected)
   1.150 +	{
   1.151 +		errLevel=Aborted;
   1.152 +		errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
   1.153 +		return false;
   1.154 +	} 
   1.155 +	return true;	
   1.156 +}
   1.157 +
   1.158 +bool Parser::checkParamIsInt(const int &index)
   1.159 +{
   1.160 +	bool ok;
   1.161 +	if (index > paramList.count())
   1.162 +	{
   1.163 +		errLevel=Aborted;
   1.164 +		errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
   1.165 +		return false;
   1.166 +	} else
   1.167 +	{
   1.168 +		paramList[index].toInt (&ok, 10);
   1.169 +		if (!ok)
   1.170 +		{
   1.171 +			errLevel=Aborted;
   1.172 +			errDescription=QString("Parameter %1 is not an integer").arg(index);
   1.173 +			return false;
   1.174 +		} 
   1.175 +	}	
   1.176 +	return true;
   1.177 +}
   1.178 +
   1.179 +int Parser::parInt (bool &ok,const uint &index)
   1.180 +{
   1.181 +	if (checkParamIsInt (index))
   1.182 +		return paramList[index].toInt (&ok, 10);
   1.183 +	ok=false;
   1.184 +	return 0;
   1.185 +}
   1.186 +
   1.187 +QString Parser::parString (bool &ok,const int &index)
   1.188 +{
   1.189 +	// return the string at index, this could be also stored in
   1.190 +	// a variable later
   1.191 +	QString r;
   1.192 +	QRegExp re("\"(.*)\"");
   1.193 +	int pos=re.search (paramList[index]);
   1.194 +	if (pos>=0)
   1.195 +		r=re.cap (1);
   1.196 +	else	
   1.197 +		r="";
   1.198 +	ok=true;
   1.199 +	return r;
   1.200 +}
   1.201 +
   1.202 +bool Parser::parBool (bool &ok,const int &index)
   1.203 +{
   1.204 +	// return the bool at index, this could be also stored in
   1.205 +	// a variable later
   1.206 +	QString r;
   1.207 +	ok=true;
   1.208 +	QString p=paramList[index];
   1.209 +	if (p=="true" || p=="1")
   1.210 +		return true;
   1.211 +	else if	(p=="false" || p=="0")
   1.212 +		return false;
   1.213 +	ok=false;
   1.214 +	return ok;
   1.215 +}
   1.216 +
   1.217 +QColor Parser::parColor(bool &ok,const int &index)
   1.218 +{
   1.219 +	// return the QColor at index
   1.220 +	ok=true;
   1.221 +	return QColor (paramList[index]);
   1.222 +}
   1.223 +
   1.224 +void Parser::setScript(const QString &s)
   1.225 +{
   1.226 +	script=s;
   1.227 +}	
   1.228 +
   1.229 +QString Parser::getScript()
   1.230 +{
   1.231 +	return script;
   1.232 +}	
   1.233 +
   1.234 +void Parser::startScript()
   1.235 +{
   1.236 +}	
   1.237 +