Update ooo320-m1
[ooovba.git] / idl / source / cmptools / lex.cxx
blobd76bc880940597bccdc17fb1726e911ea730a069
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lex.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idl.hxx"
35 #include <ctype.h>
36 #include <stdio.h>
38 #include <char.hxx>
39 #include <hash.hxx>
40 #include <lex.hxx>
41 #include <globals.hxx>
42 #include <tools/bigint.hxx>
44 /****************** SvToken **********************************************/
45 /*************************************************************************
47 |* SvToken::Print()
49 |* Beschreibung
51 *************************************************************************/
52 ByteString SvToken::GetTokenAsString() const
54 ByteString aStr;
55 switch( nType )
57 case SVTOKEN_EMPTY:
58 break;
59 case SVTOKEN_COMMENT:
60 aStr = aString;
61 break;
62 case SVTOKEN_INTEGER:
63 aStr = ByteString::CreateFromInt64(nLong);
64 break;
65 case SVTOKEN_STRING:
66 aStr = aString;
67 break;
68 case SVTOKEN_BOOL:
69 aStr = bBool ? "TRUE" : "FALSE";
70 break;
71 case SVTOKEN_IDENTIFIER:
72 aStr = aString;
73 break;
74 case SVTOKEN_CHAR:
75 aStr = cChar;
76 break;
77 case SVTOKEN_RTTIBASE:
78 aStr = "RTTIBASE";//(ULONG)pComplexObj;
79 break;
80 case SVTOKEN_EOF:
81 case SVTOKEN_HASHID:
82 break;
85 return aStr;
88 /*************************************************************************
90 |* SvToken::SvToken()
92 |* Beschreibung
94 *************************************************************************/
95 SvToken::SvToken( const SvToken & rObj )
97 nLine = rObj.nLine;
98 nColumn = rObj.nColumn;
99 nType = rObj.nType;
100 aString = rObj.aString;
102 if( SVTOKEN_RTTIBASE = nType )
104 pComplexObj = rObj.pComplexObj;
105 pComplexObj->AddRef();
107 else
109 nLong = rObj.nLong;
112 /*************************************************************************
114 |* SvToken::operator = ()
116 |* Beschreibung
118 *************************************************************************/
119 SvToken & SvToken::operator = ( const SvToken & rObj )
121 if( this != &rObj )
124 if( SVTOKEN_RTTIBASE = nType )
125 pComplexObj->ReleaseRef();
127 nLine = rObj.nLine;
128 nColumn = rObj.nColumn;
129 nType = rObj.nType;
130 aString = rObj.aString;
132 if( SVTOKEN_RTTIBASE = nType )
134 pComplexObj = rObj.pComplexObj;
135 pComplexObj->AddRef();
137 else
139 nLong = rObj.nLong;
141 return *this;
144 /****************** SvTokenStream ****************************************/
145 /*************************************************************************
146 |* SvTokenStream::InitCtor()
148 |* Beschreibung
149 *************************************************************************/
150 void SvTokenStream::InitCtor()
152 #ifdef DOS
153 SetCharSet( CHARSET_ANSI );
154 #else
155 SetCharSet( gsl_getSystemTextEncoding() );
156 #endif
157 aStrTrue = "TRUE";
158 aStrFalse = "FALSE";
159 nLine = nColumn = 0;
160 nBufPos = 0;
161 nTabSize = 4;
162 pCurToken = NULL;
163 nMaxPos = 0;
164 c = GetNextChar();
165 FillTokenList();
168 /*************************************************************************
169 |* SvTokenStream::SvTokenStream()
171 |* Beschreibung
172 *************************************************************************/
173 SvTokenStream::SvTokenStream( const String & rFileName )
174 : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
175 , rInStream( *pInStream )
176 , aFileName( rFileName )
177 , aTokList( 0x8000, 0x8000 )
179 InitCtor();
182 /*************************************************************************
183 |* SvTokenStream::SvTokenStream()
185 |* Beschreibung
186 *************************************************************************/
187 SvTokenStream::SvTokenStream( SvStream & rStream, const String & rFileName )
188 : pInStream( NULL )
189 , rInStream( rStream )
190 , aFileName( rFileName )
191 , aTokList( 0x8000, 0x8000 )
193 InitCtor();
196 /*************************************************************************
197 |* SvTokenStream::~SvTokenStream()
199 |* Beschreibung
200 *************************************************************************/
201 SvTokenStream::~SvTokenStream()
203 delete pInStream;
204 SvToken * pTok = aTokList.Last();
205 while( pTok )
207 delete pTok;
208 pTok = aTokList.Prev();
212 /*************************************************************************
213 |* SvTokenStream::FillTokenList()
215 |* Beschreibung
216 *************************************************************************/
217 void SvTokenStream::FillTokenList()
219 SvToken * pToken = new SvToken();
220 aTokList.Insert( pToken, LIST_APPEND );
223 if( !MakeToken( *pToken ) )
225 SvToken * p = aTokList.Prev();
226 *pToken = SvToken();
227 if( p )
229 pToken->SetLine( p->GetLine() );
230 pToken->SetColumn( p->GetColumn() );
232 break;
234 else if( pToken->IsComment() )
235 *pToken = SvToken();
236 else if( pToken->IsEof() )
237 break;
238 else
240 pToken = new SvToken();
241 aTokList.Insert( pToken, LIST_APPEND );
244 while( !pToken->IsEof() );
245 pCurToken = aTokList.First();
248 /*************************************************************************
249 |* SvTokenStrem::SetCharSet()
251 |* Beschreibung
252 *************************************************************************/
253 void SvTokenStream::SetCharSet( CharSet nSet )
255 nCharSet = nSet;
257 #ifdef DOS
258 pCharTab = SvChar::GetTable( nSet, CHARSET_ANSI );
259 #else
260 pCharTab = SvChar::GetTable( nSet, gsl_getSystemTextEncoding() );
261 #endif
264 /*************************************************************************
265 |* SvTokeStream::GetNextChar()
267 |* Beschreibung
268 *************************************************************************/
269 int SvTokenStream::GetNextChar()
271 int nChar;
272 if( (int)aBufStr.Len() < nBufPos )
274 if( rInStream.ReadLine( aBufStr ) )
276 nLine++;
277 nColumn = 0;
278 nBufPos = 0;
280 else
282 aBufStr.Erase();
283 nColumn = 0;
284 nBufPos = 0;
285 return '\0';
288 nChar = aBufStr.GetChar( (USHORT)nBufPos++ );
289 nColumn += nChar == '\t' ? nTabSize : 1;
290 return nChar;
293 /*************************************************************************
294 |* SvTokenStrem::GetNumber()
296 |* Beschreibung
297 *************************************************************************/
298 ULONG SvTokenStream::GetNumber()
300 ULONG l = 0;
301 short nLog = 10;
303 if( '0' == c )
305 c = GetFastNextChar();
306 if( 'x' == c )
308 nLog = 16;
309 c = GetFastNextChar();
313 if( nLog == 16 )
315 while( isxdigit( c ) )
317 if( isdigit( c ) )
318 l = l * nLog + (c - '0');
319 else
320 l = l * nLog + (toupper( c ) - 'A' + 10 );
321 c = GetFastNextChar();
324 else
326 while( isdigit( c ) || 'x' == c )
328 l = l * nLog + (c - '0');
329 c = GetFastNextChar();
333 return( l );
336 /*************************************************************************
337 |* SvTokenStream::MakeToken()
339 |* Beschreibung
340 *************************************************************************/
341 BOOL SvTokenStream::MakeToken( SvToken & rToken )
343 int c1;
344 USHORT i;
348 if( 0 == c )
349 c = GetNextChar();
350 // Leerzeichen ueberlesen
351 while( isspace( c ) || 26 == c )
353 c = GetFastNextChar();
354 nColumn += c == '\t' ? nTabSize : 1;
357 while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
359 ULONG nLastLine = nLine;
360 ULONG nLastColumn = nColumn;
361 // Kommentar
362 if( '/' == c )
364 // Zeit Optimierung, keine Kommentare
365 //ByteString aComment( (char)c );
366 c1 = c;
367 c = GetFastNextChar();
368 if( '/' == c )
370 while( '\0' != c )
372 //aComment += (char)c;
373 c = GetFastNextChar();
375 c = GetNextChar();
376 rToken.nType = SVTOKEN_COMMENT;
377 //rToken.aString = aComment;
379 else if( '*' == c )
381 //aComment += (char)c;
382 c = GetFastNextChar();
385 //aComment += (char)c;
386 while( '*' != c )
388 if( '\0' == c )
390 c = GetNextChar();
391 if( IsEof() )
392 return FALSE;
394 else
395 c = GetFastNextChar();
396 //aComment += (char)c;
398 c = GetFastNextChar();
400 while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
401 if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
402 return FALSE;
403 //aComment += (char)c;
404 c = GetNextChar();
405 rToken.nType = SVTOKEN_COMMENT;
406 //rToken.aString = aComment;
407 CalcColumn();
409 else
411 rToken.nType = SVTOKEN_CHAR;
412 rToken.cChar = (char)c1;
415 else if( c == '"' )
417 ByteString aStr;
418 i = 0;
419 BOOL bDone = FALSE;
420 while( !bDone && !IsEof() && c )
422 c = GetFastNextChar();
423 if( '\0' == c )
425 // Strings auch "uber das Zeilenende hinauslesen
426 aStr += '\n';
427 c = GetNextChar();
428 if( IsEof() )
429 return FALSE;
431 if( c == '"' )
433 c = GetFastNextChar();
434 if( c == '"' )
436 aStr += '"';
437 aStr += '"';
439 else
440 bDone = TRUE;
442 else if( c == '\\' )
444 aStr += '\\';
445 c = GetFastNextChar();
446 if( c )
447 aStr += (char)c;
449 else
450 aStr += (char)c;
452 if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
453 return FALSE;
454 char * pStr = (char *)aStr.GetBuffer();
455 while( *pStr )
457 *pStr = pCharTab[ (unsigned char)*pStr ];
458 pStr++;
460 rToken.nType = SVTOKEN_STRING;
461 rToken.aString = aStr;
463 else if( isdigit( c ) )
465 rToken.nType = SVTOKEN_INTEGER;
466 rToken.nLong = GetNumber();
469 else if( isalpha (c) || (c == '_') )
471 ByteString aStr;
473 while( isalnum( c ) || c == '_' )
475 aStr += (char)c;
476 c = GetFastNextChar();
478 if( aStr.EqualsIgnoreCaseAscii( aStrTrue ) )
480 rToken.nType = SVTOKEN_BOOL;
481 rToken.bBool = TRUE;
483 else if( aStr.EqualsIgnoreCaseAscii( aStrFalse ) )
485 rToken.nType = SVTOKEN_BOOL;
486 rToken.bBool = FALSE;
488 else
490 UINT32 nHashId;
491 if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
492 rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
493 else
495 rToken.nType = SVTOKEN_IDENTIFIER;
496 rToken.aString = aStr;
500 else if( IsEof() )
502 rToken.nType = SVTOKEN_EOF;
504 else
506 rToken.nType = SVTOKEN_CHAR;
507 rToken.cChar = (char)c;
508 c = GetFastNextChar();
510 rToken.SetLine( nLastLine );
511 rToken.SetColumn( nLastColumn );
512 return rInStream.GetError() == SVSTREAM_OK;