1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef _LEX_HXX 25 #define _LEX_HXX 26 27 #include <hash.hxx> 28 #include <tools/gen.hxx> 29 #include <tools/stream.hxx> 30 31 /******************** enum ***********************************************/ 32 enum SVTOKEN_ENUM { SVTOKEN_EMPTY, SVTOKEN_COMMENT, 33 SVTOKEN_INTEGER, SVTOKEN_STRING, 34 SVTOKEN_BOOL, SVTOKEN_IDENTIFIER, 35 SVTOKEN_CHAR, SVTOKEN_RTTIBASE, 36 SVTOKEN_EOF, SVTOKEN_HASHID }; 37 38 /******************** class SvToken **************************************/ 39 class BigInt; 40 class SvToken 41 { 42 friend class SvTokenStream; 43 sal_uLong nLine, nColumn; 44 SVTOKEN_ENUM nType; 45 ByteString aString; 46 union 47 { 48 sal_uLong nLong; 49 sal_Bool bBool; 50 char cChar; 51 // SvRttiBase * pComplexObj; 52 SvStringHashEntry * pHash; 53 }; 54 public: 55 SvToken(); 56 SvToken( const SvToken & rObj ); 57 SvToken( sal_uLong n ); 58 SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b ); 59 SvToken( char c ); 60 SvToken( SVTOKEN_ENUM nTypeP, const ByteString & rStr ); 61 // SvToken( SvRttiBase * pComplexObj ); 62 SvToken( SVTOKEN_ENUM nTypeP ); 63 64 SvToken & operator = ( const SvToken & rObj ); 65 66 ByteString GetTokenAsString() const; 67 SVTOKEN_ENUM GetType() const { return nType; } 68 69 void SetLine( sal_uLong nLineP ) { nLine = nLineP; } 70 sal_uLong GetLine() const { return nLine; } 71 72 void SetColumn( sal_uLong nColumnP ) { nColumn = nColumnP; } 73 sal_uLong GetColumn() const { return nColumn; } 74 75 sal_Bool IsEmpty() const { return nType == SVTOKEN_EMPTY; } 76 sal_Bool IsComment() const { return nType == SVTOKEN_COMMENT; } 77 sal_Bool IsInteger() const { return nType == SVTOKEN_INTEGER; } 78 sal_Bool IsString() const { return nType == SVTOKEN_STRING; } 79 sal_Bool IsBool() const { return nType == SVTOKEN_BOOL; } 80 sal_Bool IsIdentifierHash() const 81 { return nType == SVTOKEN_HASHID; } 82 sal_Bool IsIdentifier() const 83 { 84 return nType == SVTOKEN_IDENTIFIER 85 || nType == SVTOKEN_HASHID; 86 } 87 sal_Bool IsChar() const { return nType == SVTOKEN_CHAR; } 88 sal_Bool IsRttiBase() const { return nType == SVTOKEN_RTTIBASE; } 89 sal_Bool IsEof() const { return nType == SVTOKEN_EOF; } 90 91 const ByteString & GetString() const 92 { 93 return IsIdentifierHash() 94 ? pHash->GetName() 95 : aString; 96 } 97 sal_uLong GetNumber() const { return nLong; } 98 sal_Bool GetBool() const { return bBool; } 99 char GetChar() const { return cChar; } 100 // SvRttiBase *GetObject() const { return pComplexObj; } 101 102 void SetHash( SvStringHashEntry * pHashP ) 103 { pHash = pHashP; nType = SVTOKEN_HASHID; } 104 sal_Bool HasHash() const 105 { return nType == SVTOKEN_HASHID; } 106 SvStringHashEntry * GetHash() const { return pHash; } 107 sal_Bool Is( SvStringHashEntry * pEntry ) const 108 { return IsIdentifierHash() && pHash == pEntry; } 109 }; 110 111 inline SvToken::SvToken() 112 : nType( SVTOKEN_EMPTY ) {} 113 114 inline SvToken::SvToken( sal_uLong n ) 115 : nType( SVTOKEN_INTEGER ), nLong( n ) {} 116 117 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b ) 118 : nType( nTypeP ), bBool( b ) {} 119 120 inline SvToken::SvToken( char c ) 121 : nType( SVTOKEN_CHAR ), cChar( c ) {} 122 123 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, const ByteString & rStr ) 124 : nType( nTypeP ), aString( rStr ) {} 125 126 /* 127 inline SvToken::SvToken( SvRttiBase * pObj ) 128 : nType( SVTOKEN_RTTIBASE ), pComplexObj( pObj ) 129 { pObj->AddRef(); } 130 */ 131 132 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP ) 133 : nType( nTypeP ) {} 134 135 DECLARE_LIST( SvTokenList, SvToken * ) 136 137 /******************** class SvTokenStream ********************************/ 138 class SvTokenStream 139 { 140 sal_uLong nLine, nColumn; 141 int nBufPos; 142 int c; // naechstes Zeichen 143 CharSet nCharSet; 144 char * pCharTab; // Zeiger auf die Konverierungstabelle 145 sal_uInt16 nTabSize; // Tabulator Laenge 146 ByteString aStrTrue; 147 ByteString aStrFalse; 148 sal_uLong nMaxPos; 149 150 SvFileStream * pInStream; 151 SvStream & rInStream; 152 String aFileName; 153 SvTokenList aTokList; 154 SvToken * pCurToken; 155 156 void InitCtor(); 157 158 ByteString aBufStr; 159 int GetNextChar(); 160 int GetFastNextChar() 161 { 162 return aBufStr.GetChar((sal_uInt16)nBufPos++); 163 } 164 165 void FillTokenList(); 166 sal_uLong GetNumber(); 167 sal_Bool MakeToken( SvToken & ); 168 sal_Bool IsEof() const { return rInStream.IsEof(); } 169 void SetMax() 170 { 171 sal_uLong n = Tell(); 172 if( n > nMaxPos ) 173 nMaxPos = n; 174 } 175 void CalcColumn() 176 { 177 // wenn Zeilenende berechnung sparen 178 if( 0 != c ) 179 { 180 sal_uInt16 n = 0; 181 nColumn = 0; 182 while( n < nBufPos ) 183 nColumn += aBufStr.GetChar(n++) == '\t' ? nTabSize : 1; 184 } 185 } 186 public: 187 SvTokenStream( const String & rFileName ); 188 SvTokenStream( SvStream & rInStream, const String & rFileName ); 189 ~SvTokenStream(); 190 191 const String & GetFileName() const { return aFileName; } 192 SvStream & GetStream() { return rInStream; } 193 194 void SetCharSet( CharSet nSet ); 195 CharSet GetCharSet() const { return nCharSet; } 196 197 void SetTabSize( sal_uInt16 nTabSizeP ) 198 { nTabSize = nTabSizeP; } 199 sal_uInt16 GetTabSize() const { return nTabSize; } 200 201 SvToken * GetToken_PrevAll() 202 { 203 SvToken * pRetToken = pCurToken; 204 if( NULL == (pCurToken = aTokList.Prev()) ) 205 // Current Zeiger nie Null 206 pCurToken = pRetToken; 207 208 return pRetToken; 209 } 210 SvToken * GetToken_NextAll() 211 { 212 SvToken * pRetToken = pCurToken; 213 if( NULL == (pCurToken = aTokList.Next()) ) 214 // Current Zeiger nie Null 215 pCurToken = pRetToken; 216 SetMax(); 217 return pRetToken; 218 } 219 SvToken * GetToken_Next() 220 { 221 // Kommentare werden initial entfernt 222 return GetToken_NextAll(); 223 } 224 SvToken * GetToken() const { return pCurToken; } 225 sal_Bool Read( char cChar ) 226 { 227 if( pCurToken->IsChar() 228 && cChar == pCurToken->GetChar() ) 229 { 230 GetToken_Next(); 231 return sal_True; 232 } 233 else 234 return sal_False; 235 } 236 void ReadDelemiter() 237 { 238 if( pCurToken->IsChar() 239 && (';' == pCurToken->GetChar() 240 || ',' == pCurToken->GetChar()) ) 241 { 242 GetToken_Next(); 243 } 244 } 245 246 sal_uInt32 Tell() const 247 { return aTokList.GetCurPos(); } 248 void Seek( sal_uInt32 nPos ) 249 { 250 pCurToken = aTokList.Seek( nPos ); 251 SetMax(); 252 } 253 void SeekRel( sal_Int32 nRelPos ) 254 { 255 pCurToken = aTokList.Seek( Tell() + nRelPos ); 256 SetMax(); 257 } 258 void SeekEnd() 259 { 260 pCurToken = aTokList.Seek( nMaxPos ); 261 } 262 }; 263 264 265 266 #endif // _LEX_HXX 267 268