LCOV - code coverage report
Current view: top level - idl/source/cmptools - lex.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 161 198 81.3 %
Date: 2014-04-11 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <ctype.h>
      21             : #include <stdio.h>
      22             : 
      23             : #include <hash.hxx>
      24             : #include <lex.hxx>
      25             : #include <globals.hxx>
      26             : #include <rtl/strbuf.hxx>
      27             : 
      28        5904 : OString SvToken::GetTokenAsString() const
      29             : {
      30        5904 :     OString aStr;
      31        5904 :     switch( nType )
      32             :     {
      33             :         case SVTOKEN_EMPTY:
      34           0 :             break;
      35             :         case SVTOKEN_COMMENT:
      36           0 :             aStr = aString;
      37           0 :             break;
      38             :         case SVTOKEN_INTEGER:
      39           0 :             aStr = OString::number(nLong);
      40           0 :             break;
      41             :         case SVTOKEN_STRING:
      42           0 :             aStr = aString;
      43           0 :             break;
      44             :         case SVTOKEN_BOOL:
      45           0 :             aStr = bBool ? "TRUE" : "FALSE";
      46           0 :             break;
      47             :         case SVTOKEN_IDENTIFIER:
      48         492 :             aStr = aString;
      49         492 :             break;
      50             :         case SVTOKEN_CHAR:
      51        5279 :             aStr = OString(cChar);
      52        5279 :             break;
      53             :         case SVTOKEN_RTTIBASE:
      54           0 :             aStr = "RTTIBASE";
      55           0 :             break;
      56             :         case SVTOKEN_EOF:
      57             :         case SVTOKEN_HASHID:
      58         133 :             break;
      59             :     }
      60             : 
      61        5904 :     return aStr;
      62             : }
      63             : 
      64           0 : SvToken::SvToken( const SvToken & rObj )
      65             : {
      66           0 :     nLine = rObj.nLine;
      67           0 :     nColumn = rObj.nColumn;
      68           0 :     nType = rObj.nType;
      69           0 :     aString = rObj.aString;
      70           0 :     nLong = rObj.nLong;
      71           0 : }
      72             : 
      73       23513 : SvToken & SvToken::operator = ( const SvToken & rObj )
      74             : {
      75       23513 :     if( this != &rObj )
      76             :     {
      77       23513 :         nLine = rObj.nLine;
      78       23513 :         nColumn = rObj.nColumn;
      79       23513 :         nType = rObj.nType;
      80       23513 :         aString = rObj.aString;
      81       23513 :         nLong = rObj.nLong;
      82             :     }
      83       23513 :     return *this;
      84             : }
      85             : 
      86         236 : void SvTokenStream::InitCtor()
      87             : {
      88         236 :     aStrTrue = OString("TRUE");
      89         236 :     aStrFalse = OString("FALSE");
      90         236 :     nLine       = nColumn = 0;
      91         236 :     nBufPos     = 0;
      92         236 :     nTabSize    = 4;
      93         236 :     nMaxPos     = 0;
      94         236 :     c           = GetNextChar();
      95         236 :     FillTokenList();
      96         236 : }
      97             : 
      98         228 : SvTokenStream::SvTokenStream( const OUString & rFileName )
      99         228 :     : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
     100             :     , rInStream( *pInStream )
     101         456 :     , aFileName( rFileName )
     102             : {
     103         228 :     InitCtor();
     104         228 : }
     105             : 
     106           8 : SvTokenStream::SvTokenStream( SvStream & rStream, const OUString & rFileName )
     107             :     : pInStream( NULL )
     108             :     , rInStream( rStream )
     109           8 :     , aFileName( rFileName )
     110             : {
     111           8 :     InitCtor();
     112           8 : }
     113             : 
     114         472 : SvTokenStream::~SvTokenStream()
     115             : {
     116         236 :     delete pInStream;
     117         236 : }
     118             : 
     119         236 : void SvTokenStream::FillTokenList()
     120             : {
     121         236 :     SvToken * pToken = new SvToken();
     122         236 :     aTokList.push_back(pToken);
     123      814861 :     do
     124             :     {
     125      815097 :         if( !MakeToken( *pToken ) )
     126             :         {
     127           0 :             if (!aTokList.empty())
     128             :             {
     129           0 :                 *pToken = SvToken();
     130           0 :                 boost::ptr_vector<SvToken>::const_iterator it = aTokList.begin();
     131             : 
     132           0 :                 pToken->SetLine(it->GetLine());
     133           0 :                 pToken->SetColumn(it->GetColumn());
     134             :             }
     135           0 :             break;
     136             :         }
     137      815097 :         else if( pToken->IsComment() )
     138       23513 :             *pToken = SvToken();
     139      791584 :         else if( pToken->IsEof() )
     140         236 :             break;
     141             :         else
     142             :         {
     143      791348 :             pToken = new SvToken();
     144      791348 :             aTokList.push_back(pToken);
     145             :         }
     146             :     }
     147      814861 :     while( !pToken->IsEof() );
     148         236 :     pCurToken = aTokList.begin();
     149         236 : }
     150             : 
     151      236701 : int SvTokenStream::GetNextChar()
     152             : {
     153             :     int nChar;
     154      748088 :     while (aBufStr.getLength() <= nBufPos)
     155             :     {
     156      275035 :         if (rInStream.ReadLine(aBufStr))
     157             :         {
     158      274686 :             nLine++;
     159      274686 :             nColumn = 0;
     160      274686 :             nBufPos = 0;
     161             :         }
     162             :         else
     163             :         {
     164         349 :             aBufStr = OString();
     165         349 :             nColumn = 0;
     166         349 :             nBufPos = 0;
     167         349 :             return '\0';
     168             :         }
     169             :     }
     170      236352 :     nChar = aBufStr[nBufPos++];
     171      236352 :     nColumn += nChar == '\t' ? nTabSize : 1;
     172      236352 :     return nChar;
     173             : }
     174             : 
     175       19043 : sal_uLong SvTokenStream::GetNumber()
     176             : {
     177       19043 :     sal_uLong   l = 0;
     178       19043 :     short   nLog = 10;
     179             : 
     180       19043 :     if( '0' == c )
     181             :     {
     182         928 :         c = GetFastNextChar();
     183         928 :         if( 'x' == c )
     184             :         {
     185         646 :             nLog = 16;
     186         646 :             c = GetFastNextChar();
     187             :         }
     188             :     };
     189             : 
     190       19043 :     if( nLog == 16 )
     191             :     {
     192        2624 :         while( isxdigit( c ) )
     193             :         {
     194        1332 :             if( isdigit( c ) )
     195        1084 :                 l = l * nLog + (c - '0');
     196             :             else
     197         248 :                 l = l * nLog + (toupper( c ) - 'A' + 10 );
     198        1332 :             c = GetFastNextChar();
     199             :         }
     200             :     }
     201             :     else
     202             :     {
     203       86659 :         while( isdigit( c ) || 'x' == c )
     204             :         {
     205       49865 :             l = l * nLog + (c - '0');
     206       49865 :             c = GetFastNextChar();
     207             :         }
     208             :     }
     209             : 
     210       19043 :     return( l );
     211             : }
     212             : 
     213      815097 : sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
     214             : {
     215      815097 :     do
     216             :     {
     217      815097 :         if( 0 == c )
     218      208174 :             c = GetNextChar();
     219             :         // skip whitespace
     220     3147433 :         while( isspace( c ) || 26 == c )
     221             :         {
     222     1517239 :             c = GetFastNextChar();
     223     1517239 :             nColumn += c == '\t' ? nTabSize : 1;
     224             :         }
     225             :     }
     226      815097 :     while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
     227             : 
     228      815097 :     sal_uLong nLastLine     = nLine;
     229      815097 :     sal_uLong nLastColumn   = nColumn;
     230             :     // comment
     231      815097 :     if( '/' == c )
     232             :     {
     233             :         // time optimization, no comments
     234       23673 :         int c1 = c;
     235       23673 :         c = GetFastNextChar();
     236       23673 :         if( '/' == c )
     237             :         {
     238      162736 :             while( '\0' != c )
     239             :             {
     240      152114 :                 c = GetFastNextChar();
     241             :             }
     242        5311 :             c = GetNextChar();
     243        5311 :             rToken.nType    = SVTOKEN_COMMENT;
     244             :         }
     245       18362 :         else if( '*' == c )
     246             :         {
     247       18202 :             c = GetFastNextChar();
     248       22296 :             do
     249             :             {
     250      465812 :                 while( '*' != c )
     251             :                 {
     252      421220 :                     if( '\0' == c )
     253             :                     {
     254        4778 :                         c = GetNextChar();
     255        4778 :                         if( IsEof() )
     256           0 :                             return sal_False;
     257             :                     }
     258             :                     else
     259      416442 :                         c = GetFastNextChar();
     260             :                 }
     261       22296 :                 c = GetFastNextChar();
     262             :             }
     263       22296 :             while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
     264       18202 :             if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
     265           0 :                 return sal_False;
     266       18202 :             c = GetNextChar();
     267       18202 :             rToken.nType = SVTOKEN_COMMENT;
     268       18202 :             CalcColumn();
     269             :         }
     270             :         else
     271             :         {
     272         160 :             rToken.nType = SVTOKEN_CHAR;
     273         160 :             rToken.cChar = (char)c1;
     274             :         }
     275             :     }
     276      791424 :     else if( c == '"' )
     277             :     {
     278        2019 :         OStringBuffer aStr;
     279        2019 :         sal_Bool bDone = sal_False;
     280       54488 :         while( !bDone && !IsEof() && c )
     281             :         {
     282       50450 :             c = GetFastNextChar();
     283       50450 :             if( '\0' == c )
     284             :             {
     285             :                 // read strings beyond end of line
     286           0 :                 aStr.append('\n');
     287           0 :                 c = GetNextChar();
     288           0 :                 if( IsEof() )
     289           0 :                     return sal_False;
     290             :             }
     291       50450 :             if( c == '"' )
     292             :             {
     293        2019 :                 c = GetFastNextChar();
     294        2019 :                 if( c == '"' )
     295             :                 {
     296           0 :                     aStr.append('"');
     297           0 :                     aStr.append('"');
     298             :                 }
     299             :                 else
     300        2019 :                     bDone = sal_True;
     301             :             }
     302       48431 :             else if( c == '\\' )
     303             :             {
     304           0 :                 aStr.append('\\');
     305           0 :                 c = GetFastNextChar();
     306           0 :                 if( c )
     307           0 :                     aStr.append(static_cast<char>(c));
     308             :             }
     309             :             else
     310       48431 :                 aStr.append(static_cast<char>(c));
     311             :         }
     312        2019 :         if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
     313           0 :             return sal_False;
     314        2019 :         rToken.nType   = SVTOKEN_STRING;
     315        2019 :         rToken.aString = aStr.makeStringAndClear();
     316             :     }
     317      789405 :     else if( isdigit( c ) )
     318             :     {
     319       19043 :         rToken.nType = SVTOKEN_INTEGER;
     320       19043 :         rToken.nLong = GetNumber();
     321             : 
     322             :     }
     323      770362 :     else if( isalpha (c) || (c == '_') )
     324             :     {
     325      383266 :         OStringBuffer aBuf;
     326     4380262 :         while( isalnum( c ) || c == '_' )
     327             :         {
     328     3613730 :             aBuf.append(static_cast<char>(c));
     329     3613730 :             c = GetFastNextChar();
     330             :         }
     331      766532 :         OString aStr = aBuf.makeStringAndClear();
     332      383266 :         if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
     333             :         {
     334       25608 :             rToken.nType = SVTOKEN_BOOL;
     335       25608 :             rToken.bBool = sal_True;
     336             :         }
     337      357658 :         else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
     338             :         {
     339       77618 :             rToken.nType = SVTOKEN_BOOL;
     340       77618 :             rToken.bBool = sal_False;
     341             :         }
     342             :         else
     343             :         {
     344             :             sal_uInt32 nHashId;
     345      280040 :             if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
     346      178856 :                 rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
     347             :             else
     348             :             {
     349      101184 :                 rToken.nType   = SVTOKEN_IDENTIFIER;
     350      101184 :                 rToken.aString = aStr;
     351             :             }
     352      383266 :         }
     353             :     }
     354      387096 :     else if( IsEof() )
     355             :     {
     356         236 :         rToken.nType = SVTOKEN_EOF;
     357             :     }
     358             :     else
     359             :     {
     360      386860 :         rToken.nType = SVTOKEN_CHAR;
     361      386860 :         rToken.cChar = (char)c;
     362      386860 :         c = GetFastNextChar();
     363             :     }
     364      815097 :     rToken.SetLine( nLastLine );
     365      815097 :     rToken.SetColumn( nLastColumn );
     366      815097 :     return rInStream.GetError() == SVSTREAM_OK;
     367             : }
     368             : 
     369             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10