LCOV - code coverage report
Current view: top level - idl/inc - lex.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 78 86 90.7 %
Date: 2014-04-11 Functions: 35 37 94.6 %
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             : #ifndef _LEX_HXX
      21             : #define _LEX_HXX
      22             : 
      23             : #include <boost/ptr_container/ptr_vector.hpp>
      24             : 
      25             : #include <sal/types.h>
      26             : #include <hash.hxx>
      27             : #include <tools/stream.hxx>
      28             : 
      29             : enum SVTOKEN_ENUM { SVTOKEN_EMPTY,      SVTOKEN_COMMENT,
      30             :                     SVTOKEN_INTEGER,    SVTOKEN_STRING,
      31             :                     SVTOKEN_BOOL,       SVTOKEN_IDENTIFIER,
      32             :                     SVTOKEN_CHAR,       SVTOKEN_RTTIBASE,
      33             :                     SVTOKEN_EOF,        SVTOKEN_HASHID };
      34             : 
      35      815097 : class SvToken
      36             : {
      37             : friend class SvTokenStream;
      38             :     sal_uLong               nLine, nColumn;
      39             :     SVTOKEN_ENUM            nType;
      40             :     OString            aString;
      41             :     union
      42             :     {
      43             :         sal_uLong           nLong;
      44             :         sal_Bool            bBool;
      45             :         char                cChar;
      46             :         SvStringHashEntry * pHash;
      47             :     };
      48             : public:
      49             :             SvToken();
      50             :             SvToken( const SvToken & rObj );
      51             :             SvToken( sal_uLong n );
      52             :             SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b );
      53             :             SvToken( char c );
      54             :             SvToken( SVTOKEN_ENUM nTypeP, const OString& rStr );
      55             :             SvToken( SVTOKEN_ENUM nTypeP );
      56             : 
      57             :     SvToken & operator = ( const SvToken & rObj );
      58             : 
      59             :     OString GetTokenAsString() const;
      60             :     SVTOKEN_ENUM    GetType() const { return nType; }
      61             : 
      62      815097 :     void        SetLine( sal_uLong nLineP )     { nLine = nLineP;       }
      63       15258 :     sal_uLong       GetLine() const             { return nLine;         }
      64             : 
      65      815097 :     void        SetColumn( sal_uLong nColumnP ) { nColumn = nColumnP;   }
      66        5086 :     sal_uLong       GetColumn() const           { return nColumn;       }
      67             : 
      68           8 :     sal_Bool        IsEmpty() const     { return nType == SVTOKEN_EMPTY; }
      69      815097 :     sal_Bool        IsComment() const   { return nType == SVTOKEN_COMMENT; }
      70       20160 :     sal_Bool        IsInteger() const   { return nType == SVTOKEN_INTEGER; }
      71        1168 :     sal_Bool        IsString() const    { return nType == SVTOKEN_STRING; }
      72      111494 :     sal_Bool        IsBool() const      { return nType == SVTOKEN_BOOL; }
      73    10526223 :     sal_Bool        IsIdentifierHash() const
      74    10526223 :                 { return nType == SVTOKEN_HASHID; }
      75      238913 :     sal_Bool        IsIdentifier() const
      76             :                 {
      77      238913 :                     return nType == SVTOKEN_IDENTIFIER
      78      238913 :                             || nType == SVTOKEN_HASHID;
      79             :                 }
      80      711806 :     sal_Bool        IsChar() const      { return nType == SVTOKEN_CHAR; }
      81             :     sal_Bool        IsRttiBase() const  { return nType == SVTOKEN_RTTIBASE; }
      82     1630956 :     sal_Bool        IsEof() const       { return nType == SVTOKEN_EOF; }
      83             : 
      84     3083121 :     const OString& GetString() const
      85             :                 {
      86     3083121 :                     return IsIdentifierHash()
      87        2054 :                         ? pHash->GetName()
      88     3085175 :                         : aString;
      89             :                 }
      90       19030 :     sal_uLong       GetNumber() const       { return nLong;         }
      91      103226 :     sal_Bool        GetBool() const         { return bBool;         }
      92     1337998 :     char        GetChar() const         { return cChar;         }
      93             : 
      94      178856 :     void        SetHash( SvStringHashEntry * pHashP )
      95      178856 :                 { pHash = pHashP; nType = SVTOKEN_HASHID; }
      96       37057 :     sal_Bool        HasHash() const
      97       37057 :                 { return nType == SVTOKEN_HASHID; }
      98             :     SvStringHashEntry * GetHash() const { return pHash; }
      99     7443102 :     sal_Bool        Is( SvStringHashEntry * pEntry ) const
     100     7443102 :                 { return IsIdentifierHash() && pHash == pEntry; }
     101             : };
     102             : 
     103      815097 : inline SvToken::SvToken()
     104             :     : nLine(0)
     105             :     , nColumn(0)
     106      815097 :     , nType( SVTOKEN_EMPTY )
     107             : {
     108      815097 : }
     109             : 
     110             : inline SvToken::SvToken( sal_uLong n )
     111             :     : nType( SVTOKEN_INTEGER ), nLong( n ) {}
     112             : 
     113             : inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b )
     114             :     : nType( nTypeP ), bBool( b ) {}
     115             : 
     116             : inline SvToken::SvToken( char c )
     117             :     : nType( SVTOKEN_CHAR ), cChar( c ) {}
     118             : 
     119             : inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, const OString& rStr )
     120             :     : nType( nTypeP ), aString( rStr ) {}
     121             : 
     122             : inline SvToken::SvToken( SVTOKEN_ENUM nTypeP )
     123             : : nType( nTypeP ) {}
     124             : 
     125             : class SvTokenStream
     126             : {
     127             :     sal_uLong       nLine, nColumn;
     128             :     int         nBufPos;
     129             :     int         c;          // next character
     130             :     sal_uInt16      nTabSize;   // length of tabulator
     131             :     OString    aStrTrue;
     132             :     OString    aStrFalse;
     133             :     sal_uLong       nMaxPos;
     134             : 
     135             :     SvFileStream *  pInStream;
     136             :     SvStream &      rInStream;
     137             :     OUString        aFileName;
     138             :     boost::ptr_vector<SvToken> aTokList;
     139             :     boost::ptr_vector<SvToken>::iterator pCurToken;
     140             : 
     141             :     void        InitCtor();
     142             : 
     143             :     OString aBufStr;
     144             :     int             GetNextChar();
     145     6255796 :     int             GetFastNextChar()
     146             :                     {
     147     6255796 :                         return (nBufPos < aBufStr.getLength())
     148     6037646 :                             ? aBufStr[nBufPos++]
     149    12293442 :                             : '\0';
     150             :                     }
     151             : 
     152             :     void            FillTokenList();
     153             :     sal_uLong           GetNumber();
     154             :     sal_Bool            MakeToken( SvToken & );
     155      466875 :     sal_Bool            IsEof() const { return rInStream.IsEof(); }
     156    15208954 :     void            SetMax()
     157             :                     {
     158    15208954 :                         sal_uLong n = Tell();
     159    15208954 :                         if( n > nMaxPos )
     160      791348 :                             nMaxPos = n;
     161    15208954 :                     }
     162       18202 :     void            CalcColumn()
     163             :                     {
     164             :                         // if end of line spare calculation
     165       18202 :                         if( 0 != c )
     166             :                         {
     167       18105 :                             sal_uInt16 n = 0;
     168       18105 :                             nColumn = 0;
     169       54315 :                             while( n < nBufPos )
     170       18105 :                                 nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
     171             :                         }
     172       18202 :                     }
     173             : public:
     174             :                     SvTokenStream( const OUString & rFileName );
     175             :                     SvTokenStream( SvStream & rInStream, const OUString & rFileName );
     176             :                     ~SvTokenStream();
     177             : 
     178           8 :     const OUString &  GetFileName() const { return aFileName; }
     179         228 :     SvStream &        GetStream() { return rInStream; }
     180             : 
     181             :     void            SetTabSize( sal_uInt16 nTabSizeP )
     182             :                     { nTabSize = nTabSizeP; }
     183             :     sal_uInt16          GetTabSize() const { return nTabSize; }
     184             : 
     185           0 :     SvToken* GetToken_PrevAll()
     186             :     {
     187           0 :         boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken;
     188             : 
     189             :         // current iterator always valid
     190           0 :         if(pCurToken != aTokList.begin())
     191           0 :             --pCurToken;
     192             : 
     193           0 :         return &(*pRetToken);
     194             :     }
     195             : 
     196     7997170 :     SvToken* GetToken_NextAll()
     197             :     {
     198     7997170 :         boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken++;
     199             : 
     200     7997170 :         if (pCurToken == aTokList.end())
     201         494 :             pCurToken = pRetToken;
     202             : 
     203     7997170 :         SetMax();
     204             : 
     205     7997170 :         return &(*pRetToken);
     206             :     }
     207             : 
     208     7997170 :     SvToken* GetToken_Next()
     209             :     {
     210             :         // comments get removed initially
     211     7997170 :         return GetToken_NextAll();
     212             :     }
     213             : 
     214      261618 :     SvToken* GetToken() const { return &(*pCurToken); }
     215             : 
     216      369327 :     sal_Bool            Read( char cChar )
     217             :                     {
     218      738654 :                         if( pCurToken->IsChar()
     219      369327 :                           && cChar == pCurToken->GetChar() )
     220             :                         {
     221      164404 :                             GetToken_Next();
     222      164404 :                             return sal_True;
     223             :                         }
     224             :                         else
     225      204923 :                             return sal_False;
     226             :                     }
     227             : 
     228      192330 :     void            ReadDelemiter()
     229             :                     {
     230      384660 :                         if( pCurToken->IsChar()
     231      343376 :                           && (';' == pCurToken->GetChar()
     232      133542 :                                 || ',' == pCurToken->GetChar()) )
     233             :                         {
     234      151046 :                             GetToken_Next();
     235             :                         }
     236      192330 :                     }
     237             : 
     238    23269072 :     sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
     239             : 
     240     7211784 :     void Seek( sal_uInt32 nPos )
     241             :     {
     242     7211784 :         pCurToken = aTokList.begin() + nPos;
     243     7211784 :         SetMax();
     244     7211784 :     }
     245             : 
     246             :     void SeekRel( sal_uInt32 nRelPos )
     247             :     {
     248             :         sal_uInt32 relIdx = Tell() + nRelPos;
     249             : 
     250             :         if ( relIdx < aTokList.size())
     251             :         {
     252             :             pCurToken = aTokList.begin()+ (Tell() + nRelPos );
     253             :             SetMax();
     254             :         }
     255             :     }
     256             : 
     257           0 :     void SeekEnd()
     258             :     {
     259           0 :         pCurToken = aTokList.begin()+nMaxPos;
     260           0 :     }
     261             : };
     262             : 
     263             : 
     264             : 
     265             : #endif // _LEX_HXX
     266             : 
     267             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10