fix literal int parser to be more strict
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Mon, 15 Nov 2010 07:54:46 +0000 (07:54 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Mon, 15 Nov 2010 07:54:46 +0000 (07:54 +0000)
git-svn-id: https://silmor.de/svn/softmagic/elam/trunk@630 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/elamintengine.cpp
tests/parser/parser.cpp

index 1504a16..849c8a4 100644 (file)
@@ -16,41 +16,21 @@ IntEngine::IntEngine()
 // decimal: [1-9][0-9]*
 // octal: 0[0-7]*
 // hex: 0x[0-9a-fA-F]+
+// end of expression: anything not a nameClass.second
 QPair<QString,QVariant> IntLiteralParser(const QString&expr,Engine&engine,int start)
 {
        QString ls;
-       //shortcut: single char?
-       if(start==(expr.size()-1)){
-               ls=expr.mid(start,1);
-               return QPair<QString,QVariant>(ls,ls.toInt());
+       //parse to end of expression
+       QString nc=engine.characterClasses().nameClass().second;
+       for(int i=start;i<expr.size();i++){
+               if(nc.contains(expr[i]))ls+=expr[i];
+               else break;
        }
-       //check type
-       if(expr[start]>'0' && expr[start]<='9'){
-               //decimal
-               for(int i=start;i<expr.size();i++){
-                       if(expr[i]<'0' || expr[i]>'9')break;
-                       ls+=expr[i];
-               }
-       }else //oct or hex
-       if(expr[start+1]=='x'){
-               //hex
-               ls="0x";
-               for(int i=start+2;i<expr.size();i++){
-                       if((expr[i]>='0' && expr[i]<='9')&&
-                          (expr[i]>='a' && expr[i]<='f')&&
-                          (expr[i]>='A' && expr[i]<='F'))
-                               ls+=expr[i];
-                       else
-                               break;
-               }
-       }else{
-               //oct
-               for(int i=start;i<expr.size();i++){
-                       if(expr[i]<'0' || expr[i]>'7')break;
-                       ls+=expr[i];
-               }
-       }
-       return QPair<QString,QVariant>(ls,ls.toLongLong(0,0));
+       //is it integer?
+       bool ok;
+       qlonglong r=ls.toLongLong(&ok,0);
+       if(ok)return QPair<QString,QVariant>(ls,r);
+       else QPair<QString,QVariant>();
 }
 
 void IntEngine::configureIntEngine(ELAM::Engine& eng)
index 857f639..84c7771 100644 (file)
@@ -76,6 +76,23 @@ void ElamTest::tokenizer()
        for(int i=0;i<tl.size();i++)
                qDebug()<<" token"<<i<<tl[i];*/
        QCOMPARE(tl.size(),15);
+       QCOMPARE(tl[0].type(),Token::Name);//a
+       QCOMPARE(tl[1].type(),Token::Operator);//=
+       QCOMPARE(tl[2].type(),Token::Name);//bcd
+       QCOMPARE(tl[3].type(),Token::Operator);//+
+       QCOMPARE(tl[4].type(),Token::Literal);//345
+       QCOMPARE(tl[4].literalValue().toInt(),345);//345
+       QCOMPARE(tl[4].literalValue().typeName(),"qlonglong");//345
+       QCOMPARE(tl[5].type(),Token::Operator);//*
+       QCOMPARE(tl[6].type(),Token::Name);//efg
+       QCOMPARE(tl[7].type(),Token::Operator);//*
+       QCOMPARE(tl[8].type(),Token::ParOpen);//(
+       QCOMPARE(tl[9].type(),Token::Literal);//65
+       QCOMPARE(tl[10].type(),Token::Operator);// /
+       QCOMPARE(tl[11].type(),Token::ParOpen);//(
+       QCOMPARE(tl[12].type(),Token::Literal);//5
+       QCOMPARE(tl[13].type(),Token::ParClose);//)
+       QCOMPARE(tl[14].type(),Token::ParClose);//)
 }