xref: /aoo42x/main/idl/inc/lex.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _LEX_HXX
29 #define _LEX_HXX
30 
31 #include <hash.hxx>
32 #include <tools/gen.hxx>
33 #include <tools/stream.hxx>
34 
35 /******************** enum ***********************************************/
36 enum SVTOKEN_ENUM { SVTOKEN_EMPTY,      SVTOKEN_COMMENT,
37                     SVTOKEN_INTEGER,    SVTOKEN_STRING,
38                     SVTOKEN_BOOL,       SVTOKEN_IDENTIFIER,
39                     SVTOKEN_CHAR,       SVTOKEN_RTTIBASE,
40                     SVTOKEN_EOF,		SVTOKEN_HASHID };
41 
42 /******************** class SvToken **************************************/
43 class BigInt;
44 class SvToken
45 {
46 friend class SvTokenStream;
47     sal_uLong               	nLine, nColumn;
48     SVTOKEN_ENUM        	nType;
49     ByteString              	aString;
50     union
51 	{
52         sal_uLong           	nLong;
53         sal_Bool            	bBool;
54         char            	cChar;
55 //	    SvRttiBase *    	pComplexObj;
56 	    SvStringHashEntry *	pHash;
57     };
58 public:
59             SvToken();
60             SvToken( const SvToken & rObj );
61             SvToken( sal_uLong n );
62             SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b );
63             SvToken( char c );
64             SvToken( SVTOKEN_ENUM nTypeP, const ByteString & rStr );
65 //            SvToken( SvRttiBase * pComplexObj );
66             SvToken( SVTOKEN_ENUM nTypeP );
67 
68     SvToken & operator = ( const SvToken & rObj );
69 
70     ByteString          GetTokenAsString() const;
71     SVTOKEN_ENUM    GetType() const { return nType; }
72 
73     void        SetLine( sal_uLong nLineP )     { nLine = nLineP;       }
74     sal_uLong       GetLine() const             { return nLine;         }
75 
76     void        SetColumn( sal_uLong nColumnP ) { nColumn = nColumnP;   }
77     sal_uLong       GetColumn() const           { return nColumn;       }
78 
79     sal_Bool        IsEmpty() const     { return nType == SVTOKEN_EMPTY; }
80     sal_Bool        IsComment() const   { return nType == SVTOKEN_COMMENT; }
81     sal_Bool        IsInteger() const   { return nType == SVTOKEN_INTEGER; }
82     sal_Bool        IsString() const    { return nType == SVTOKEN_STRING; }
83     sal_Bool        IsBool() const      { return nType == SVTOKEN_BOOL; }
84     sal_Bool        IsIdentifierHash() const
85                 { return nType == SVTOKEN_HASHID; }
86     sal_Bool        IsIdentifier() const
87 				{
88 					return nType == SVTOKEN_IDENTIFIER
89 							|| nType == SVTOKEN_HASHID;
90 				}
91     sal_Bool        IsChar() const      { return nType == SVTOKEN_CHAR; }
92     sal_Bool        IsRttiBase() const  { return nType == SVTOKEN_RTTIBASE; }
93     sal_Bool        IsEof() const       { return nType == SVTOKEN_EOF; }
94 
95     const ByteString & GetString() const
96 				{
97 					return IsIdentifierHash()
98 						? pHash->GetName()
99 						: aString;
100 				}
101     sal_uLong       GetNumber() const       { return nLong;         }
102     sal_Bool        GetBool() const         { return bBool;         }
103     char        GetChar() const         { return cChar;         }
104 //    SvRttiBase *GetObject() const       { return pComplexObj;   }
105 
106     void        SetHash( SvStringHashEntry * pHashP )
107 				{ pHash = pHashP; nType = SVTOKEN_HASHID; }
108     sal_Bool        HasHash() const
109                 { return nType == SVTOKEN_HASHID; }
110     SvStringHashEntry * GetHash() const { return pHash; }
111     sal_Bool        Is( SvStringHashEntry * pEntry ) const
112                 { return IsIdentifierHash() && pHash == pEntry; }
113 };
114 
115 inline SvToken::SvToken()
116     : nType( SVTOKEN_EMPTY ) {}
117 
118 inline SvToken::SvToken( sal_uLong n )
119     : nType( SVTOKEN_INTEGER ), nLong( n ) {}
120 
121 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b )
122     : nType( nTypeP ), bBool( b ) {}
123 
124 inline SvToken::SvToken( char c )
125     : nType( SVTOKEN_CHAR ), cChar( c ) {}
126 
127 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, const ByteString & rStr )
128     : nType( nTypeP ), aString( rStr ) {}
129 
130 /*
131 inline SvToken::SvToken( SvRttiBase * pObj )
132     : nType( SVTOKEN_RTTIBASE ), pComplexObj( pObj )
133 		{ pObj->AddRef(); }
134 */
135 
136 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP )
137 : nType( nTypeP ) {}
138 
139 DECLARE_LIST( SvTokenList, SvToken * )
140 
141 /******************** class SvTokenStream ********************************/
142 class SvTokenStream
143 {
144     sal_uLong       nLine, nColumn;
145     int			nBufPos;
146     int         c;          // naechstes Zeichen
147     CharSet     nCharSet;
148     char *      pCharTab;   // Zeiger auf die Konverierungstabelle
149     sal_uInt16      nTabSize;   // Tabulator Laenge
150 	ByteString		aStrTrue;
151 	ByteString		aStrFalse;
152 	sal_uLong		nMaxPos;
153 
154     SvFileStream *  pInStream;
155     SvStream &      rInStream;
156     String          aFileName;
157     SvTokenList     aTokList;
158 	SvToken *		pCurToken;
159 
160     void        InitCtor();
161 
162     ByteString      	aBufStr;
163     int         	GetNextChar();
164     int         	GetFastNextChar()
165 					{
166                         return aBufStr.GetChar((sal_uInt16)nBufPos++);
167 					}
168 
169 	void			FillTokenList();
170     sal_uLong       	GetNumber();
171     sal_Bool        	MakeToken( SvToken & );
172 	sal_Bool			IsEof() const { return rInStream.IsEof(); }
173 	void			SetMax()
174 					{
175 						sal_uLong n = Tell();
176 						if( n > nMaxPos )
177 							nMaxPos = n;
178 					}
179 	void			CalcColumn()
180 					{
181 						// wenn Zeilenende berechnung sparen
182 						if( 0 != c )
183 						{
184 							sal_uInt16 n = 0;
185 							nColumn = 0;
186 							while( n < nBufPos )
187                                 nColumn += aBufStr.GetChar(n++) == '\t' ? nTabSize : 1;
188 						}
189 					}
190 public:
191                     SvTokenStream( const String & rFileName );
192                     SvTokenStream( SvStream & rInStream, const String & rFileName );
193                     ~SvTokenStream();
194 
195     const String &  GetFileName() const { return aFileName; }
196     SvStream &      GetStream() { return rInStream; }
197 
198     void            SetCharSet( CharSet nSet );
199     CharSet         GetCharSet() const { return nCharSet; }
200 
201     void            SetTabSize( sal_uInt16 nTabSizeP )
202                     { nTabSize = nTabSizeP; }
203     sal_uInt16          GetTabSize() const { return nTabSize; }
204 
205     SvToken *       GetToken_PrevAll()
206 					{
207 					    SvToken * pRetToken = pCurToken;
208 					    if( NULL == (pCurToken = aTokList.Prev()) )
209 							// Current Zeiger nie Null
210 							pCurToken = pRetToken;
211 
212 					    return pRetToken;
213 					}
214     SvToken *       GetToken_NextAll()
215 					{
216 					    SvToken * pRetToken = pCurToken;
217 					    if( NULL == (pCurToken = aTokList.Next()) )
218 							// Current Zeiger nie Null
219 							pCurToken = pRetToken;
220 						SetMax();
221 					    return pRetToken;
222 					}
223     SvToken *       GetToken_Next()
224 					{
225 						// Kommentare werden initial entfernt
226 						return GetToken_NextAll();
227 					}
228     SvToken *       GetToken() const { return pCurToken; }
229     sal_Bool            Read( char cChar )
230                     {
231 					    if( pCurToken->IsChar()
232 						  && cChar == pCurToken->GetChar() )
233 						{
234 							GetToken_Next();
235 							return sal_True;
236 						}
237 						else
238 							return sal_False;
239 					}
240     void            ReadDelemiter()
241                     {
242 					    if( pCurToken->IsChar()
243 						  && (';' == pCurToken->GetChar()
244 						  	  || ',' == pCurToken->GetChar()) )
245 						{
246 							GetToken_Next();
247 						}
248 					}
249 
250     sal_uInt32          Tell() const
251                     { return aTokList.GetCurPos(); }
252     void            Seek( sal_uInt32 nPos )
253                     {
254                         pCurToken = aTokList.Seek( nPos );
255 						SetMax();
256                     }
257     void            SeekRel( sal_Int32 nRelPos )
258                     {
259                         pCurToken = aTokList.Seek( Tell() + nRelPos );
260 						SetMax();
261                     }
262     void            SeekEnd()
263                     {
264                         pCurToken = aTokList.Seek( nMaxPos );
265                     }
266 };
267 
268 
269 
270 #endif // _LEX_HXX
271 
272