1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lex.cxx,v $
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"
41 #include <globals.hxx>
42 #include <tools/bigint.hxx>
44 /****************** SvToken **********************************************/
45 /*************************************************************************
51 *************************************************************************/
52 ByteString
SvToken::GetTokenAsString() const
63 aStr
= ByteString::CreateFromInt64(nLong
);
69 aStr
= bBool
? "TRUE" : "FALSE";
71 case SVTOKEN_IDENTIFIER
:
77 case SVTOKEN_RTTIBASE
:
78 aStr
= "RTTIBASE";//(ULONG)pComplexObj;
88 /*************************************************************************
94 *************************************************************************/
95 SvToken::SvToken( const SvToken
& rObj
)
98 nColumn
= rObj
.nColumn
;
100 aString
= rObj
.aString
;
102 if( SVTOKEN_RTTIBASE = nType )
104 pComplexObj = rObj.pComplexObj;
105 pComplexObj->AddRef();
112 /*************************************************************************
114 |* SvToken::operator = ()
118 *************************************************************************/
119 SvToken
& SvToken::operator = ( const SvToken
& rObj
)
124 if( SVTOKEN_RTTIBASE = nType )
125 pComplexObj->ReleaseRef();
128 nColumn
= rObj
.nColumn
;
130 aString
= rObj
.aString
;
132 if( SVTOKEN_RTTIBASE = nType )
134 pComplexObj = rObj.pComplexObj;
135 pComplexObj->AddRef();
144 /****************** SvTokenStream ****************************************/
145 /*************************************************************************
146 |* SvTokenStream::InitCtor()
149 *************************************************************************/
150 void SvTokenStream::InitCtor()
153 SetCharSet( CHARSET_ANSI
);
155 SetCharSet( gsl_getSystemTextEncoding() );
168 /*************************************************************************
169 |* SvTokenStream::SvTokenStream()
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 )
182 /*************************************************************************
183 |* SvTokenStream::SvTokenStream()
186 *************************************************************************/
187 SvTokenStream::SvTokenStream( SvStream
& rStream
, const String
& rFileName
)
189 , rInStream( rStream
)
190 , aFileName( rFileName
)
191 , aTokList( 0x8000, 0x8000 )
196 /*************************************************************************
197 |* SvTokenStream::~SvTokenStream()
200 *************************************************************************/
201 SvTokenStream::~SvTokenStream()
204 SvToken
* pTok
= aTokList
.Last();
208 pTok
= aTokList
.Prev();
212 /*************************************************************************
213 |* SvTokenStream::FillTokenList()
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();
229 pToken
->SetLine( p
->GetLine() );
230 pToken
->SetColumn( p
->GetColumn() );
234 else if( pToken
->IsComment() )
236 else if( pToken
->IsEof() )
240 pToken
= new SvToken();
241 aTokList
.Insert( pToken
, LIST_APPEND
);
244 while( !pToken
->IsEof() );
245 pCurToken
= aTokList
.First();
248 /*************************************************************************
249 |* SvTokenStrem::SetCharSet()
252 *************************************************************************/
253 void SvTokenStream::SetCharSet( CharSet nSet
)
258 pCharTab
= SvChar::GetTable( nSet
, CHARSET_ANSI
);
260 pCharTab
= SvChar::GetTable( nSet
, gsl_getSystemTextEncoding() );
264 /*************************************************************************
265 |* SvTokeStream::GetNextChar()
268 *************************************************************************/
269 int SvTokenStream::GetNextChar()
272 if( (int)aBufStr
.Len() < nBufPos
)
274 if( rInStream
.ReadLine( aBufStr
) )
288 nChar
= aBufStr
.GetChar( (USHORT
)nBufPos
++ );
289 nColumn
+= nChar
== '\t' ? nTabSize
: 1;
293 /*************************************************************************
294 |* SvTokenStrem::GetNumber()
297 *************************************************************************/
298 ULONG
SvTokenStream::GetNumber()
305 c
= GetFastNextChar();
309 c
= GetFastNextChar();
315 while( isxdigit( c
) )
318 l
= l
* nLog
+ (c
- '0');
320 l
= l
* nLog
+ (toupper( c
) - 'A' + 10 );
321 c
= GetFastNextChar();
326 while( isdigit( c
) || 'x' == c
)
328 l
= l
* nLog
+ (c
- '0');
329 c
= GetFastNextChar();
336 /*************************************************************************
337 |* SvTokenStream::MakeToken()
340 *************************************************************************/
341 BOOL
SvTokenStream::MakeToken( SvToken
& rToken
)
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
;
364 // Zeit Optimierung, keine Kommentare
365 //ByteString aComment( (char)c );
367 c
= GetFastNextChar();
372 //aComment += (char)c;
373 c
= GetFastNextChar();
376 rToken
.nType
= SVTOKEN_COMMENT
;
377 //rToken.aString = aComment;
381 //aComment += (char)c;
382 c
= GetFastNextChar();
385 //aComment += (char)c;
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() ) )
403 //aComment += (char)c;
405 rToken
.nType
= SVTOKEN_COMMENT
;
406 //rToken.aString = aComment;
411 rToken
.nType
= SVTOKEN_CHAR
;
412 rToken
.cChar
= (char)c1
;
420 while( !bDone
&& !IsEof() && c
)
422 c
= GetFastNextChar();
425 // Strings auch "uber das Zeilenende hinauslesen
433 c
= GetFastNextChar();
445 c
= GetFastNextChar();
452 if( IsEof() || ( SVSTREAM_OK
!= rInStream
.GetError() ) )
454 char * pStr
= (char *)aStr
.GetBuffer();
457 *pStr
= pCharTab
[ (unsigned char)*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
== '_') )
473 while( isalnum( c
) || c
== '_' )
476 c
= GetFastNextChar();
478 if( aStr
.EqualsIgnoreCaseAscii( aStrTrue
) )
480 rToken
.nType
= SVTOKEN_BOOL
;
483 else if( aStr
.EqualsIgnoreCaseAscii( aStrFalse
) )
485 rToken
.nType
= SVTOKEN_BOOL
;
486 rToken
.bBool
= FALSE
;
491 if( IDLAPP
->pHashTable
->Test( aStr
, &nHashId
) )
492 rToken
.SetHash( IDLAPP
->pHashTable
->Get( nHashId
) );
495 rToken
.nType
= SVTOKEN_IDENTIFIER
;
496 rToken
.aString
= aStr
;
502 rToken
.nType
= SVTOKEN_EOF
;
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
;