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