// 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)
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);//)
}