insilmaril@432: #include "parser.h" insilmaril@432: insilmaril@432: #include insilmaril@432: #include insilmaril@432: insilmaril@432: using namespace std; insilmaril@432: insilmaril@432: Parser::Parser() insilmaril@432: { insilmaril@447: initParser(); insilmaril@432: } insilmaril@432: insilmaril@447: void Parser::initParser() insilmaril@432: { insilmaril@447: initAtom(); insilmaril@447: current=-1; insilmaril@447: } insilmaril@447: insilmaril@447: void Parser::initAtom() insilmaril@447: { insilmaril@447: atom=""; insilmaril@432: com=""; insilmaril@432: paramList.clear(); insilmaril@432: resetError(); insilmaril@432: } insilmaril@432: insilmaril@447: void Parser::parseAtom (QString s) insilmaril@432: { insilmaril@447: initAtom(); insilmaril@447: atom=s; insilmaril@432: QRegExp re; insilmaril@432: int pos; insilmaril@432: insilmaril@447: // Strip WS at beginning insilmaril@447: re.setPattern ("\\w"); insilmaril@447: re.setMinimal (true); insilmaril@447: pos=re.search (atom); insilmaril@447: if (pos>=0) insilmaril@447: s=s.right(s.length()-pos); insilmaril@447: insilmaril@432: // Get command insilmaril@432: re.setPattern ("\\b(.*)(\\s|\\()"); insilmaril@432: pos=re.search (s); insilmaril@432: if (pos>=0) insilmaril@432: com=re.cap(1); insilmaril@432: insilmaril@432: // Get parameters insilmaril@432: paramList.clear(); insilmaril@432: re.setPattern ("\\((.*)\\)"); insilmaril@432: pos=re.search (s); insilmaril@432: //cout << " s="< plist) insilmaril@432: { insilmaril@432: QStringList expList; insilmaril@432: QString expected; insilmaril@432: for (int i=0; i paramList.count()) insilmaril@432: { insilmaril@432: errLevel=Aborted; insilmaril@432: errDescription=QString("Parameter index %1 is outside of parameter list").arg(index); insilmaril@432: return false; insilmaril@432: } else insilmaril@432: { insilmaril@432: paramList[index].toInt (&ok, 10); insilmaril@432: if (!ok) insilmaril@432: { insilmaril@432: errLevel=Aborted; insilmaril@432: errDescription=QString("Parameter %1 is not an integer").arg(index); insilmaril@432: return false; insilmaril@432: } insilmaril@432: } insilmaril@432: return true; insilmaril@432: } insilmaril@432: insilmaril@432: int Parser::parInt (bool &ok,const uint &index) insilmaril@432: { insilmaril@514: if (checkParIsInt (index)) insilmaril@432: return paramList[index].toInt (&ok, 10); insilmaril@432: ok=false; insilmaril@432: return 0; insilmaril@432: } insilmaril@432: insilmaril@432: QString Parser::parString (bool &ok,const int &index) insilmaril@432: { insilmaril@432: // return the string at index, this could be also stored in insilmaril@432: // a variable later insilmaril@432: QString r; insilmaril@432: QRegExp re("\"(.*)\""); insilmaril@432: int pos=re.search (paramList[index]); insilmaril@432: if (pos>=0) insilmaril@432: r=re.cap (1); insilmaril@432: else insilmaril@432: r=""; insilmaril@432: ok=true; insilmaril@432: return r; insilmaril@432: } insilmaril@432: insilmaril@432: bool Parser::parBool (bool &ok,const int &index) insilmaril@432: { insilmaril@432: // return the bool at index, this could be also stored in insilmaril@432: // a variable later insilmaril@432: QString r; insilmaril@432: ok=true; insilmaril@432: QString p=paramList[index]; insilmaril@432: if (p=="true" || p=="1") insilmaril@432: return true; insilmaril@432: else if (p=="false" || p=="0") insilmaril@432: return false; insilmaril@432: ok=false; insilmaril@432: return ok; insilmaril@432: } insilmaril@432: insilmaril@432: QColor Parser::parColor(bool &ok,const int &index) insilmaril@432: { insilmaril@432: // return the QColor at index insilmaril@445: ok=false; insilmaril@445: QString r; insilmaril@445: QColor c; insilmaril@445: QRegExp re("\"(.*)\""); insilmaril@445: int pos=re.search (paramList[index]); insilmaril@445: if (pos>=0) insilmaril@445: { insilmaril@445: r=re.cap (1); insilmaril@445: c.setNamedColor(r); insilmaril@445: ok=c.isValid(); insilmaril@445: } insilmaril@445: return c; insilmaril@432: } insilmaril@432: insilmaril@432: void Parser::setScript(const QString &s) insilmaril@432: { insilmaril@432: script=s; insilmaril@432: } insilmaril@432: insilmaril@432: QString Parser::getScript() insilmaril@432: { insilmaril@432: return script; insilmaril@432: } insilmaril@432: insilmaril@445: void Parser::runScript() insilmaril@432: { insilmaril@447: current=0; insilmaril@432: } insilmaril@432: insilmaril@447: bool Parser::next() insilmaril@445: { insilmaril@447: int start=current; insilmaril@447: if (current<0) runScript(); insilmaril@447: if (current>=script.length()-1) return false; insilmaril@447: insilmaril@447: bool inBracket=false; insilmaril@447: while (true) insilmaril@447: { insilmaril@447: //cout <<"current="<=script.length()) insilmaril@447: return false; insilmaril@447: } insilmaril@447: start=current; insilmaril@447: } insilmaril@447: insilmaril@447: // Check for end of atom insilmaril@447: if (!inBracket && script.at(current)==';') insilmaril@447: { insilmaril@447: atom=script.mid(start,current-start); insilmaril@447: current++; insilmaril@447: return true; insilmaril@447: } insilmaril@447: insilmaril@447: // Check for end of script insilmaril@447: if (current==script.length() ) insilmaril@447: { insilmaril@447: if (inBracket) insilmaril@447: { insilmaril@447: setError (Aborted,"Runaway string"); insilmaril@447: return false; insilmaril@447: } else insilmaril@447: { insilmaril@447: atom=script.mid(start); insilmaril@447: return true; insilmaril@447: } insilmaril@447: } insilmaril@447: current++; insilmaril@447: } insilmaril@445: } insilmaril@445: