tools/linguist/lupdate/java.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    48 #include <QtCore/QRegExp>
    48 #include <QtCore/QRegExp>
    49 #include <QtCore/QStack>
    49 #include <QtCore/QStack>
    50 #include <QtCore/QStack>
    50 #include <QtCore/QStack>
    51 #include <QtCore/QString>
    51 #include <QtCore/QString>
    52 #include <QtCore/QTextCodec>
    52 #include <QtCore/QTextCodec>
       
    53 #include <QtCore/QCoreApplication>
    53 
    54 
    54 #include <iostream>
    55 #include <iostream>
    55 
    56 
    56 #include <ctype.h>
    57 #include <ctype.h>
    57 
    58 
    58 QT_BEGIN_NAMESPACE
    59 QT_BEGIN_NAMESPACE
       
    60 
       
    61 class LU {
       
    62     Q_DECLARE_TR_FUNCTIONS(LUpdate)
       
    63 };
    59 
    64 
    60 enum { Tok_Eof, Tok_class, Tok_return, Tok_tr,
    65 enum { Tok_Eof, Tok_class, Tok_return, Tok_tr,
    61        Tok_translate, Tok_Ident, Tok_Package,
    66        Tok_translate, Tok_Ident, Tok_Package,
    62        Tok_Comment, Tok_String, Tok_Colon, Tok_Dot,
    67        Tok_Comment, Tok_String, Tok_Colon, Tok_Dot,
    63        Tok_LeftBrace, Tok_RightBrace, Tok_LeftParen,
    68        Tok_LeftBrace, Tok_RightBrace, Tok_LeftParen,
   194                     bool metAsterSlash = false;
   199                     bool metAsterSlash = false;
   195 
   200 
   196                     while ( !metAsterSlash ) {
   201                     while ( !metAsterSlash ) {
   197                         yyCh = getChar();
   202                         yyCh = getChar();
   198                         if ( yyCh == EOF ) {
   203                         if ( yyCh == EOF ) {
   199                             yyMsg() << "Unterminated Java comment.\n";
   204                             yyMsg() << qPrintable(LU::tr("Unterminated Java comment.\n"));
   200                             return Tok_Comment;
   205                             return Tok_Comment;
   201                         }
   206                         }
   202 
   207 
   203                         yyComment.append( yyCh );
   208                         yyComment.append( yyCh );
   204 
   209 
   230                                     unicode += yyCh.digitValue();
   235                                     unicode += yyCh.digitValue();
   231                                 }
   236                                 }
   232                                 else {
   237                                 else {
   233                                     int sub(yyCh.toLower().toAscii() - 87);
   238                                     int sub(yyCh.toLower().toAscii() - 87);
   234                                     if( sub > 15 || sub < 10) {
   239                                     if( sub > 15 || sub < 10) {
   235                                         yyMsg() << "Invalid Unicode value.\n";
   240                                         yyMsg() << qPrintable(LU::tr("Invalid Unicode value.\n"));
   236                                         break;
   241                                         break;
   237                                     }
   242                                     }
   238                                     unicode += sub;
   243                                     unicode += sub;
   239                                 }
   244                                 }
   240                                 yyCh = getChar();
   245                                 yyCh = getChar();
   253                         yyCh = getChar();
   258                         yyCh = getChar();
   254                     }
   259                     }
   255                 }
   260                 }
   256 
   261 
   257                 if ( yyCh != QLatin1Char('"') )
   262                 if ( yyCh != QLatin1Char('"') )
   258                     yyMsg() << "Unterminated string.\n";
   263                     yyMsg() << qPrintable(LU::tr("Unterminated string.\n"));
   259 
   264 
   260                 yyCh = getChar();
   265                 yyCh = getChar();
   261 
   266 
   262                 return Tok_String;
   267                 return Tok_String;
   263 
   268 
   366     while ( yyTok == Tok_Plus ) {
   371     while ( yyTok == Tok_Plus ) {
   367         yyTok = getToken();
   372         yyTok = getToken();
   368         if (yyTok == Tok_String)
   373         if (yyTok == Tok_String)
   369             s += yyString;
   374             s += yyString;
   370         else {
   375         else {
   371             yyMsg() << "String used in translation can contain only literals"
   376             yyMsg() << qPrintable(LU::tr(
   372                        " concatenated with other literals, not expressions or numbers.\n";
   377                 "String used in translation can contain only literals"
       
   378                 " concatenated with other literals, not expressions or numbers.\n"));
   373             return false;
   379             return false;
   374         }
   380         }
   375         yyTok = getToken();
   381         yyTok = getToken();
   376     }
   382     }
   377     return true;
   383     return true;
   475             yyTok = getToken();
   481             yyTok = getToken();
   476             if(yyTok == Tok_Ident) {
   482             if(yyTok == Tok_Ident) {
   477                 yyScope.push(new Scope(yyIdent, Scope::Clazz, yyLineNo));
   483                 yyScope.push(new Scope(yyIdent, Scope::Clazz, yyLineNo));
   478             }
   484             }
   479             else {
   485             else {
   480                 yyMsg() << "'class' must be followed by a class name.\n";
   486                 yyMsg() << qPrintable(LU::tr("'class' must be followed by a class name.\n"));
   481                 break;
   487                 break;
   482             }
   488             }
   483             while (!match(Tok_LeftBrace)) {
   489             while (!match(Tok_LeftBrace)) {
   484                 yyTok = getToken();
   490                 yyTok = getToken();
   485             }
   491             }
   547             yyTok = getToken();
   553             yyTok = getToken();
   548             break;
   554             break;
   549 
   555 
   550         case Tok_RightBrace:
   556         case Tok_RightBrace:
   551             if ( yyScope.isEmpty() ) {
   557             if ( yyScope.isEmpty() ) {
   552                 yyMsg() << "Excess closing brace.\n";
   558                 yyMsg() << qPrintable(LU::tr("Excess closing brace.\n"));
   553             }
   559             }
   554             else
   560             else
   555                 delete (yyScope.pop());
   561                 delete (yyScope.pop());
   556             extracomment.clear();
   562             extracomment.clear();
   557             yyTok = getToken();
   563             yyTok = getToken();
   576                         break;
   582                         break;
   577                     case Tok_Dot:
   583                     case Tok_Dot:
   578                         yyPackage.append(QLatin1String("."));
   584                         yyPackage.append(QLatin1String("."));
   579                         break;
   585                         break;
   580                     default:
   586                     default:
   581                          yyMsg() << "'package' must be followed by package name.\n";
   587                          yyMsg() << qPrintable(LU::tr("'package' must be followed by package name.\n"));
   582                          break;
   588                          break;
   583                 }
   589                 }
   584                 yyTok = getToken();
   590                 yyTok = getToken();
   585             }
   591             }
   586             break;
   592             break;
   589             yyTok = getToken();
   595             yyTok = getToken();
   590         }
   596         }
   591     }
   597     }
   592 
   598 
   593     if ( !yyScope.isEmpty() )
   599     if ( !yyScope.isEmpty() )
   594         yyMsg(yyScope.top()->line) << "Unbalanced opening brace.\n";
   600         yyMsg(yyScope.top()->line) << qPrintable(LU::tr("Unbalanced opening brace.\n"));
   595     else if ( yyParenDepth != 0 )
   601     else if ( yyParenDepth != 0 )
   596         yyMsg(yyParenLineNo) << "Unbalanced opening parenthesis.\n";
   602         yyMsg(yyParenLineNo) << qPrintable(LU::tr("Unbalanced opening parenthesis.\n"));
   597 }
   603 }
   598 
   604 
   599 
   605 
   600 bool loadJava(Translator &translator, const QString &filename, ConversionData &cd)
   606 bool loadJava(Translator &translator, const QString &filename, ConversionData &cd)
   601 {
   607 {
   602     QFile file(filename);
   608     QFile file(filename);
   603     if (!file.open(QIODevice::ReadOnly)) {
   609     if (!file.open(QIODevice::ReadOnly)) {
   604         cd.appendError(QString::fromLatin1("Cannot open %1: %2")
   610         cd.appendError(LU::tr("Cannot open %1: %2").arg(filename, file.errorString()));
   605             .arg(filename, file.errorString()));
       
   606         return false;
   611         return false;
   607     }
   612     }
   608 
   613 
   609     yyDefaultContext = cd.m_defaultContext;
   614     yyDefaultContext = cd.m_defaultContext;
   610     yyInPos = -1;
   615     yyInPos = -1;