xref: /AOO41X/main/idl/source/cmptools/lex.cxx (revision 79aad27f7f29270c03e208e3d687e8e3850af11d)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_idl.hxx"
26 
27 
28 #include <ctype.h>
29 #include <stdio.h>
30 
31 #include <char.hxx>
32 #include <hash.hxx>
33 #include <lex.hxx>
34 #include <globals.hxx>
35 #include <tools/bigint.hxx>
36 
37 /****************** SvToken **********************************************/
38 /*************************************************************************
39 |*
40 |*    SvToken::Print()
41 |*
42 |*    Beschreibung
43 |*
44 *************************************************************************/
GetTokenAsString() const45 ByteString SvToken::GetTokenAsString() const
46 {
47     ByteString aStr;
48     switch( nType )
49     {
50         case SVTOKEN_EMPTY:
51             break;
52         case SVTOKEN_COMMENT:
53             aStr = aString;
54             break;
55         case SVTOKEN_INTEGER:
56             aStr = ByteString::CreateFromInt64(nLong);
57             break;
58         case SVTOKEN_STRING:
59             aStr = aString;
60             break;
61         case SVTOKEN_BOOL:
62             aStr = bBool ? "TRUE" : "FALSE";
63             break;
64         case SVTOKEN_IDENTIFIER:
65             aStr = aString;
66             break;
67         case SVTOKEN_CHAR:
68             aStr = cChar;
69             break;
70         case SVTOKEN_RTTIBASE:
71             aStr = "RTTIBASE";//(ULONG)pComplexObj;
72             break;
73         case SVTOKEN_EOF:
74         case SVTOKEN_HASHID:
75             break;
76     }
77 
78     return aStr;
79 }
80 
81 /*************************************************************************
82 |*
83 |*    SvToken::SvToken()
84 |*
85 |*    Beschreibung
86 |*
87 *************************************************************************/
SvToken(const SvToken & rObj)88 SvToken::SvToken( const SvToken & rObj )
89 {
90     nLine = rObj.nLine;
91     nColumn = rObj.nColumn;
92     nType = rObj.nType;
93     aString = rObj.aString;
94 /*
95     if( SVTOKEN_RTTIBASE = nType )
96     {
97         pComplexObj = rObj.pComplexObj;
98         pComplexObj->AddRef();
99     }
100     else
101 */
102         nLong = rObj.nLong;
103 }
104 
105 /*************************************************************************
106 |*
107 |*    SvToken::operator = ()
108 |*
109 |*    Beschreibung
110 |*
111 *************************************************************************/
operator =(const SvToken & rObj)112 SvToken & SvToken::operator = ( const SvToken & rObj )
113 {
114     if( this != &rObj )
115     {
116 /*
117         if( SVTOKEN_RTTIBASE = nType )
118             pComplexObj->ReleaseRef();
119 */
120         nLine = rObj.nLine;
121         nColumn = rObj.nColumn;
122         nType = rObj.nType;
123         aString = rObj.aString;
124 /*
125         if( SVTOKEN_RTTIBASE = nType )
126         {
127             pComplexObj = rObj.pComplexObj;
128             pComplexObj->AddRef();
129         }
130         else
131 */
132             nLong = rObj.nLong;
133     }
134     return *this;
135 }
136 
137 /****************** SvTokenStream ****************************************/
138 /*************************************************************************
139 |*    SvTokenStream::InitCtor()
140 |*
141 |*    Beschreibung
142 *************************************************************************/
InitCtor()143 void SvTokenStream::InitCtor()
144 {
145     SetCharSet( gsl_getSystemTextEncoding() );
146     aStrTrue    = "TRUE";
147     aStrFalse   = "FALSE";
148     nLine       = nColumn = 0;
149     nBufPos     = 0;
150     nTabSize    = 4;
151     pCurToken   = NULL;
152     nMaxPos     = 0;
153     c           = GetNextChar();
154     FillTokenList();
155 }
156 
157 /*************************************************************************
158 |*    SvTokenStream::SvTokenStream()
159 |*
160 |*    Beschreibung
161 *************************************************************************/
SvTokenStream(const String & rFileName)162 SvTokenStream::SvTokenStream( const String & rFileName )
163     : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
164     , rInStream( *pInStream )
165     , aFileName( rFileName )
166     , aTokList( 0x8000, 0x8000 )
167 {
168     InitCtor();
169 }
170 
171 /*************************************************************************
172 |*    SvTokenStream::SvTokenStream()
173 |*
174 |*    Beschreibung
175 *************************************************************************/
SvTokenStream(SvStream & rStream,const String & rFileName)176 SvTokenStream::SvTokenStream( SvStream & rStream, const String & rFileName )
177     : pInStream( NULL )
178     , rInStream( rStream )
179     , aFileName( rFileName )
180     , aTokList( 0x8000, 0x8000 )
181 {
182     InitCtor();
183 }
184 
185 /*************************************************************************
186 |*    SvTokenStream::~SvTokenStream()
187 |*
188 |*    Beschreibung
189 *************************************************************************/
~SvTokenStream()190 SvTokenStream::~SvTokenStream()
191 {
192     delete pInStream;
193     SvToken * pTok = aTokList.Last();
194     while( pTok )
195     {
196         delete pTok;
197         pTok = aTokList.Prev();
198     }
199 }
200 
201 /*************************************************************************
202 |*    SvTokenStream::FillTokenList()
203 |*
204 |*    Beschreibung
205 *************************************************************************/
FillTokenList()206 void SvTokenStream::FillTokenList()
207 {
208     SvToken * pToken = new SvToken();
209     aTokList.Insert( pToken, LIST_APPEND );
210     do
211     {
212         if( !MakeToken( *pToken ) )
213         {
214             SvToken * p = aTokList.Prev();
215             *pToken = SvToken();
216             if( p )
217             {
218                 pToken->SetLine( p->GetLine() );
219                 pToken->SetColumn( p->GetColumn() );
220             }
221             break;
222         }
223         else if( pToken->IsComment() )
224             *pToken = SvToken();
225         else if( pToken->IsEof() )
226             break;
227         else
228         {
229             pToken = new SvToken();
230             aTokList.Insert( pToken, LIST_APPEND );
231         }
232     }
233     while( !pToken->IsEof() );
234     pCurToken = aTokList.First();
235 }
236 
237 /*************************************************************************
238 |*    SvTokenStrem::SetCharSet()
239 |*
240 |*    Beschreibung
241 *************************************************************************/
SetCharSet(CharSet nSet)242 void SvTokenStream::SetCharSet( CharSet nSet )
243 {
244     nCharSet = nSet;
245 
246     pCharTab = SvChar::GetTable( nSet, gsl_getSystemTextEncoding() );
247 }
248 
249 /*************************************************************************
250 |*    SvTokeStream::GetNextChar()
251 |*
252 |*    Beschreibung
253 *************************************************************************/
GetNextChar()254 int SvTokenStream::GetNextChar()
255 {
256     int nChar;
257     if( (int)aBufStr.Len() < nBufPos )
258     {
259         if( rInStream.ReadLine( aBufStr ) )
260         {
261             nLine++;
262             nColumn = 0;
263             nBufPos = 0;
264         }
265         else
266         {
267             aBufStr.Erase();
268             nColumn = 0;
269             nBufPos = 0;
270             return '\0';
271         }
272     }
273     nChar = aBufStr.GetChar( (sal_uInt16)nBufPos++ );
274     nColumn += nChar == '\t' ? nTabSize : 1;
275     return nChar;
276 }
277 
278 /*************************************************************************
279 |*    SvTokenStrem::GetNumber()
280 |*
281 |*    Beschreibung
282 *************************************************************************/
GetNumber()283 sal_uLong SvTokenStream::GetNumber()
284 {
285     sal_uLong   l = 0;
286     short   nLog = 10;
287 
288     if( '0' == c )
289     {
290         c = GetFastNextChar();
291         if( 'x' == c )
292         {
293             nLog = 16;
294             c = GetFastNextChar();
295         }
296     };
297 
298     if( nLog == 16 )
299     {
300         while( isxdigit( c ) )
301         {
302             if( isdigit( c ) )
303                 l = l * nLog + (c - '0');
304             else
305                 l = l * nLog + (toupper( c ) - 'A' + 10 );
306             c = GetFastNextChar();
307         }
308     }
309     else
310     {
311         while( isdigit( c ) || 'x' == c )
312         {
313             l = l * nLog + (c - '0');
314             c = GetFastNextChar();
315         }
316     }
317 
318     return( l );
319 }
320 
321 /*************************************************************************
322 |*    SvTokenStream::MakeToken()
323 |*
324 |*    Beschreibung
325 *************************************************************************/
MakeToken(SvToken & rToken)326 sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
327 {
328     int             c1;
329     sal_uInt16          i;
330 
331     do
332     {
333         if( 0 == c )
334             c = GetNextChar();
335         // Leerzeichen ueberlesen
336         while( isspace( c ) || 26 == c )
337         {
338             c = GetFastNextChar();
339             nColumn += c == '\t' ? nTabSize : 1;
340         }
341     }
342     while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
343 
344     sal_uLong nLastLine     = nLine;
345     sal_uLong nLastColumn   = nColumn;
346     // Kommentar
347     if( '/' == c )
348     {
349         // Zeit Optimierung, keine Kommentare
350         //ByteString aComment( (char)c );
351         c1 = c;
352         c = GetFastNextChar();
353         if( '/' == c )
354         {
355             while( '\0' != c )
356             {
357                 //aComment += (char)c;
358                 c = GetFastNextChar();
359             }
360             c = GetNextChar();
361             rToken.nType    = SVTOKEN_COMMENT;
362             //rToken.aString    = aComment;
363         }
364         else if( '*' == c )
365         {
366             //aComment += (char)c;
367             c = GetFastNextChar();
368             do
369             {
370                 //aComment += (char)c;
371                 while( '*' != c )
372                 {
373                     if( '\0' == c )
374                     {
375                         c = GetNextChar();
376                         if( IsEof() )
377                             return sal_False;
378                     }
379                     else
380                         c = GetFastNextChar();
381                     //aComment += (char)c;
382                 }
383                 c = GetFastNextChar();
384             }
385             while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
386             if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
387                 return sal_False;
388             //aComment += (char)c;
389             c = GetNextChar();
390             rToken.nType = SVTOKEN_COMMENT;
391             //rToken.aString = aComment;
392             CalcColumn();
393         }
394         else
395         {
396             rToken.nType = SVTOKEN_CHAR;
397             rToken.cChar = (char)c1;
398         }
399     }
400     else if( c == '"' )
401     {
402         ByteString          aStr;
403         i = 0;
404         sal_Bool bDone = sal_False;
405         while( !bDone && !IsEof() && c )
406         {
407             c = GetFastNextChar();
408             if( '\0' == c )
409             {
410                 // Strings auch "uber das Zeilenende hinauslesen
411                 aStr += '\n';
412                 c = GetNextChar();
413                 if( IsEof() )
414                     return sal_False;
415             }
416             if( c == '"' )
417             {
418                 c = GetFastNextChar();
419                 if( c == '"' )
420                 {
421                     aStr += '"';
422                     aStr += '"';
423                 }
424                 else
425                     bDone = sal_True;
426             }
427             else if( c == '\\' )
428             {
429                 aStr += '\\';
430                 c = GetFastNextChar();
431                 if( c )
432                     aStr += (char)c;
433             }
434             else
435                 aStr += (char)c;
436         }
437         if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
438             return sal_False;
439         char * pStr = (char *)aStr.GetBuffer();
440         while( *pStr )
441         {
442             *pStr = pCharTab[ (unsigned char)*pStr ];
443             pStr++;
444         };
445         rToken.nType   = SVTOKEN_STRING;
446         rToken.aString = aStr;
447     }
448     else if( isdigit( c ) )
449     {
450         rToken.nType = SVTOKEN_INTEGER;
451         rToken.nLong = GetNumber();
452 
453     }
454     else if( isalpha (c) || (c == '_') )
455     {
456         ByteString aStr;
457 
458         while( isalnum( c ) || c == '_' )
459         {
460             aStr += (char)c;
461             c = GetFastNextChar();
462         }
463         if( aStr.EqualsIgnoreCaseAscii( aStrTrue ) )
464         {
465             rToken.nType = SVTOKEN_BOOL;
466             rToken.bBool = sal_True;
467         }
468         else if( aStr.EqualsIgnoreCaseAscii( aStrFalse ) )
469         {
470             rToken.nType = SVTOKEN_BOOL;
471             rToken.bBool = sal_False;
472         }
473         else
474         {
475             sal_uInt32 nHashId;
476             if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
477                 rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
478             else
479             {
480                 rToken.nType   = SVTOKEN_IDENTIFIER;
481                 rToken.aString = aStr;
482             }
483         }
484     }
485     else if( IsEof() )
486     {
487         rToken.nType = SVTOKEN_EOF;
488     }
489     else
490     {
491         rToken.nType = SVTOKEN_CHAR;
492         rToken.cChar = (char)c;
493         c = GetFastNextChar();
494     }
495     rToken.SetLine( nLastLine );
496     rToken.SetColumn( nLastColumn );
497     return rInStream.GetError() == SVSTREAM_OK;
498 }
499 
500