LCOV - code coverage report
Current view: top level - libreoffice/idl/source/cmptools - lex.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 198 0.0 %
Date: 2012-12-17 Functions: 0 11 0.0 %
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           0 : rtl::OString SvToken::GetTokenAsString() const
      29             : {
      30           0 :     rtl::OString aStr;
      31           0 :     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 = rtl::OString::valueOf(static_cast<sal_Int64>(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           0 :             aStr = aString;
      49           0 :             break;
      50             :         case SVTOKEN_CHAR:
      51           0 :             aStr = rtl::OString(cChar);
      52           0 :             break;
      53             :         case SVTOKEN_RTTIBASE:
      54           0 :             aStr = "RTTIBASE";
      55           0 :             break;
      56             :         case SVTOKEN_EOF:
      57             :         case SVTOKEN_HASHID:
      58           0 :             break;
      59             :     }
      60             : 
      61           0 :     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           0 : SvToken & SvToken::operator = ( const SvToken & rObj )
      74             : {
      75           0 :     if( this != &rObj )
      76             :     {
      77           0 :         nLine = rObj.nLine;
      78           0 :         nColumn = rObj.nColumn;
      79           0 :         nType = rObj.nType;
      80           0 :         aString = rObj.aString;
      81           0 :         nLong = rObj.nLong;
      82             :     }
      83           0 :     return *this;
      84             : }
      85             : 
      86           0 : void SvTokenStream::InitCtor()
      87             : {
      88           0 :     aStrTrue = rtl::OString(RTL_CONSTASCII_STRINGPARAM("TRUE"));
      89           0 :     aStrFalse = rtl::OString(RTL_CONSTASCII_STRINGPARAM("FALSE"));
      90           0 :     nLine       = nColumn = 0;
      91           0 :     nBufPos     = 0;
      92           0 :     nTabSize    = 4;
      93           0 :     nMaxPos     = 0;
      94           0 :     c           = GetNextChar();
      95           0 :     FillTokenList();
      96           0 : }
      97             : 
      98           0 : SvTokenStream::SvTokenStream( const String & rFileName )
      99           0 :     : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
     100             :     , rInStream( *pInStream )
     101           0 :     , aFileName( rFileName )
     102             : {
     103           0 :     InitCtor();
     104           0 : }
     105             : 
     106           0 : SvTokenStream::SvTokenStream( SvStream & rStream, const String & rFileName )
     107             :     : pInStream( NULL )
     108             :     , rInStream( rStream )
     109           0 :     , aFileName( rFileName )
     110             : {
     111           0 :     InitCtor();
     112           0 : }
     113             : 
     114           0 : SvTokenStream::~SvTokenStream()
     115             : {
     116           0 :     delete pInStream;
     117           0 : }
     118             : 
     119           0 : void SvTokenStream::FillTokenList()
     120             : {
     121           0 :     SvToken * pToken = new SvToken();
     122           0 :     aTokList.push_back(pToken);
     123           0 :     do
     124             :     {
     125           0 :         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           0 :         else if( pToken->IsComment() )
     138           0 :             *pToken = SvToken();
     139           0 :         else if( pToken->IsEof() )
     140           0 :             break;
     141             :         else
     142             :         {
     143           0 :             pToken = new SvToken();
     144           0 :             aTokList.push_back(pToken);
     145             :         }
     146             :     }
     147           0 :     while( !pToken->IsEof() );
     148           0 :     pCurToken = aTokList.begin();
     149           0 : }
     150             : 
     151           0 : int SvTokenStream::GetNextChar()
     152             : {
     153             :     int nChar;
     154           0 :     if( aBufStr.getLength() < nBufPos )
     155             :     {
     156           0 :         if( rInStream.ReadLine( aBufStr ) )
     157             :         {
     158           0 :             nLine++;
     159           0 :             nColumn = 0;
     160           0 :             nBufPos = 0;
     161             :         }
     162             :         else
     163             :         {
     164           0 :             aBufStr = rtl::OString();
     165           0 :             nColumn = 0;
     166           0 :             nBufPos = 0;
     167           0 :             return '\0';
     168             :         }
     169             :     }
     170           0 :     nChar = aBufStr[nBufPos++];
     171           0 :     nColumn += nChar == '\t' ? nTabSize : 1;
     172           0 :     return nChar;
     173             : }
     174             : 
     175           0 : sal_uLong SvTokenStream::GetNumber()
     176             : {
     177           0 :     sal_uLong   l = 0;
     178           0 :     short   nLog = 10;
     179             : 
     180           0 :     if( '0' == c )
     181             :     {
     182           0 :         c = GetFastNextChar();
     183           0 :         if( 'x' == c )
     184             :         {
     185           0 :             nLog = 16;
     186           0 :             c = GetFastNextChar();
     187             :         }
     188             :     };
     189             : 
     190           0 :     if( nLog == 16 )
     191             :     {
     192           0 :         while( isxdigit( c ) )
     193             :         {
     194           0 :             if( isdigit( c ) )
     195           0 :                 l = l * nLog + (c - '0');
     196             :             else
     197           0 :                 l = l * nLog + (toupper( c ) - 'A' + 10 );
     198           0 :             c = GetFastNextChar();
     199             :         }
     200             :     }
     201             :     else
     202             :     {
     203           0 :         while( isdigit( c ) || 'x' == c )
     204             :         {
     205           0 :             l = l * nLog + (c - '0');
     206           0 :             c = GetFastNextChar();
     207             :         }
     208             :     }
     209             : 
     210           0 :     return( l );
     211             : }
     212             : 
     213           0 : sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
     214             : {
     215           0 :     do
     216             :     {
     217           0 :         if( 0 == c )
     218           0 :             c = GetNextChar();
     219             :         // skip whitespace
     220           0 :         while( isspace( c ) || 26 == c )
     221             :         {
     222           0 :             c = GetFastNextChar();
     223           0 :             nColumn += c == '\t' ? nTabSize : 1;
     224             :         }
     225             :     }
     226           0 :     while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
     227             : 
     228           0 :     sal_uLong nLastLine     = nLine;
     229           0 :     sal_uLong nLastColumn   = nColumn;
     230             :     // comment
     231           0 :     if( '/' == c )
     232             :     {
     233             :         // time optimization, no comments
     234           0 :         int c1 = c;
     235           0 :         c = GetFastNextChar();
     236           0 :         if( '/' == c )
     237             :         {
     238           0 :             while( '\0' != c )
     239             :             {
     240           0 :                 c = GetFastNextChar();
     241             :             }
     242           0 :             c = GetNextChar();
     243           0 :             rToken.nType    = SVTOKEN_COMMENT;
     244             :         }
     245           0 :         else if( '*' == c )
     246             :         {
     247           0 :             c = GetFastNextChar();
     248           0 :             do
     249             :             {
     250           0 :                 while( '*' != c )
     251             :                 {
     252           0 :                     if( '\0' == c )
     253             :                     {
     254           0 :                         c = GetNextChar();
     255           0 :                         if( IsEof() )
     256           0 :                             return sal_False;
     257             :                     }
     258             :                     else
     259           0 :                         c = GetFastNextChar();
     260             :                 }
     261           0 :                 c = GetFastNextChar();
     262             :             }
     263           0 :             while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
     264           0 :             if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
     265           0 :                 return sal_False;
     266           0 :             c = GetNextChar();
     267           0 :             rToken.nType = SVTOKEN_COMMENT;
     268           0 :             CalcColumn();
     269             :         }
     270             :         else
     271             :         {
     272           0 :             rToken.nType = SVTOKEN_CHAR;
     273           0 :             rToken.cChar = (char)c1;
     274             :         }
     275             :     }
     276           0 :     else if( c == '"' )
     277             :     {
     278           0 :         rtl::OStringBuffer aStr;
     279           0 :         sal_Bool bDone = sal_False;
     280           0 :         while( !bDone && !IsEof() && c )
     281             :         {
     282           0 :             c = GetFastNextChar();
     283           0 :             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           0 :             if( c == '"' )
     292             :             {
     293           0 :                 c = GetFastNextChar();
     294           0 :                 if( c == '"' )
     295             :                 {
     296           0 :                     aStr.append('"');
     297           0 :                     aStr.append('"');
     298             :                 }
     299             :                 else
     300           0 :                     bDone = sal_True;
     301             :             }
     302           0 :             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           0 :                 aStr.append(static_cast<char>(c));
     311             :         }
     312           0 :         if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
     313           0 :             return sal_False;
     314           0 :         rToken.nType   = SVTOKEN_STRING;
     315           0 :         rToken.aString = aStr.makeStringAndClear();
     316             :     }
     317           0 :     else if( isdigit( c ) )
     318             :     {
     319           0 :         rToken.nType = SVTOKEN_INTEGER;
     320           0 :         rToken.nLong = GetNumber();
     321             : 
     322             :     }
     323           0 :     else if( isalpha (c) || (c == '_') )
     324             :     {
     325           0 :         rtl::OStringBuffer aBuf;
     326           0 :         while( isalnum( c ) || c == '_' )
     327             :         {
     328           0 :             aBuf.append(static_cast<char>(c));
     329           0 :             c = GetFastNextChar();
     330             :         }
     331           0 :         rtl::OString aStr = aBuf.makeStringAndClear();
     332           0 :         if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
     333             :         {
     334           0 :             rToken.nType = SVTOKEN_BOOL;
     335           0 :             rToken.bBool = sal_True;
     336             :         }
     337           0 :         else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
     338             :         {
     339           0 :             rToken.nType = SVTOKEN_BOOL;
     340           0 :             rToken.bBool = sal_False;
     341             :         }
     342             :         else
     343             :         {
     344             :             sal_uInt32 nHashId;
     345           0 :             if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
     346           0 :                 rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
     347             :             else
     348             :             {
     349           0 :                 rToken.nType   = SVTOKEN_IDENTIFIER;
     350           0 :                 rToken.aString = aStr;
     351             :             }
     352           0 :         }
     353             :     }
     354           0 :     else if( IsEof() )
     355             :     {
     356           0 :         rToken.nType = SVTOKEN_EOF;
     357             :     }
     358             :     else
     359             :     {
     360           0 :         rToken.nType = SVTOKEN_CHAR;
     361           0 :         rToken.cChar = (char)c;
     362           0 :         c = GetFastNextChar();
     363             :     }
     364           0 :     rToken.SetLine( nLastLine );
     365           0 :     rToken.SetColumn( nLastColumn );
     366           0 :     return rInStream.GetError() == SVSTREAM_OK;
     367             : }
     368             : 
     369             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10