xref: /trunk/main/idl/inc/lex.hxx (revision 67e470da)
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;
GetType() const67     SVTOKEN_ENUM    GetType() const { return nType; }
68 
SetLine(sal_uLong nLineP)69     void        SetLine( sal_uLong nLineP )     { nLine = nLineP;       }
GetLine() const70     sal_uLong       GetLine() const             { return nLine;         }
71 
SetColumn(sal_uLong nColumnP)72     void        SetColumn( sal_uLong nColumnP ) { nColumn = nColumnP;   }
GetColumn() const73     sal_uLong       GetColumn() const           { return nColumn;       }
74 
IsEmpty() const75     sal_Bool        IsEmpty() const     { return nType == SVTOKEN_EMPTY; }
IsComment() const76     sal_Bool        IsComment() const   { return nType == SVTOKEN_COMMENT; }
IsInteger() const77     sal_Bool        IsInteger() const   { return nType == SVTOKEN_INTEGER; }
IsString() const78     sal_Bool        IsString() const    { return nType == SVTOKEN_STRING; }
IsBool() const79     sal_Bool        IsBool() const      { return nType == SVTOKEN_BOOL; }
IsIdentifierHash() const80     sal_Bool        IsIdentifierHash() const
81                 { return nType == SVTOKEN_HASHID; }
IsIdentifier() const82     sal_Bool        IsIdentifier() const
83 				{
84 					return nType == SVTOKEN_IDENTIFIER
85 							|| nType == SVTOKEN_HASHID;
86 				}
IsChar() const87     sal_Bool        IsChar() const      { return nType == SVTOKEN_CHAR; }
IsRttiBase() const88     sal_Bool        IsRttiBase() const  { return nType == SVTOKEN_RTTIBASE; }
IsEof() const89     sal_Bool        IsEof() const       { return nType == SVTOKEN_EOF; }
90 
GetString() const91     const ByteString & GetString() const
92 				{
93 					return IsIdentifierHash()
94 						? pHash->GetName()
95 						: aString;
96 				}
GetNumber() const97     sal_uLong       GetNumber() const       { return nLong;         }
GetBool() const98     sal_Bool        GetBool() const         { return bBool;         }
GetChar() const99     char        GetChar() const         { return cChar;         }
100 //    SvRttiBase *GetObject() const       { return pComplexObj;   }
101 
SetHash(SvStringHashEntry * pHashP)102     void        SetHash( SvStringHashEntry * pHashP )
103 				{ pHash = pHashP; nType = SVTOKEN_HASHID; }
HasHash() const104     sal_Bool        HasHash() const
105                 { return nType == SVTOKEN_HASHID; }
GetHash() const106     SvStringHashEntry * GetHash() const { return pHash; }
Is(SvStringHashEntry * pEntry) const107     sal_Bool        Is( SvStringHashEntry * pEntry ) const
108                 { return IsIdentifierHash() && pHash == pEntry; }
109 };
110 
SvToken()111 inline SvToken::SvToken()
112     : nType( SVTOKEN_EMPTY ) {}
113 
SvToken(sal_uLong n)114 inline SvToken::SvToken( sal_uLong n )
115     : nType( SVTOKEN_INTEGER ), nLong( n ) {}
116 
SvToken(SVTOKEN_ENUM nTypeP,sal_Bool b)117 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b )
118     : nType( nTypeP ), bBool( b ) {}
119 
SvToken(char c)120 inline SvToken::SvToken( char c )
121     : nType( SVTOKEN_CHAR ), cChar( c ) {}
122 
SvToken(SVTOKEN_ENUM nTypeP,const ByteString & rStr)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 
SvToken(SVTOKEN_ENUM nTypeP)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();
GetFastNextChar()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 & );
IsEof() const168 	sal_Bool			IsEof() const { return rInStream.IsEof(); }
SetMax()169 	void			SetMax()
170 					{
171 						sal_uLong n = Tell();
172 						if( n > nMaxPos )
173 							nMaxPos = n;
174 					}
CalcColumn()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 
GetFileName() const191     const String &  GetFileName() const { return aFileName; }
GetStream()192     SvStream &      GetStream() { return rInStream; }
193 
194     void            SetCharSet( CharSet nSet );
GetCharSet() const195     CharSet         GetCharSet() const { return nCharSet; }
196 
SetTabSize(sal_uInt16 nTabSizeP)197     void            SetTabSize( sal_uInt16 nTabSizeP )
198                     { nTabSize = nTabSizeP; }
GetTabSize() const199     sal_uInt16          GetTabSize() const { return nTabSize; }
200 
GetToken_PrevAll()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 					}
GetToken_NextAll()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 					}
GetToken_Next()219     SvToken *       GetToken_Next()
220 					{
221 						// Kommentare werden initial entfernt
222 						return GetToken_NextAll();
223 					}
GetToken() const224     SvToken *       GetToken() const { return pCurToken; }
Read(char cChar)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 					}
ReadDelemiter()236     void            ReadDelemiter()
237                     {
238 					    if( pCurToken->IsChar()
239 						  && (';' == pCurToken->GetChar()
240 						  	  || ',' == pCurToken->GetChar()) )
241 						{
242 							GetToken_Next();
243 						}
244 					}
245 
Tell() const246     sal_uInt32          Tell() const
247                     { return aTokList.GetCurPos(); }
Seek(sal_uInt32 nPos)248     void            Seek( sal_uInt32 nPos )
249                     {
250                         pCurToken = aTokList.Seek( nPos );
251 						SetMax();
252                     }
SeekRel(sal_Int32 nRelPos)253     void            SeekRel( sal_Int32 nRelPos )
254                     {
255                         pCurToken = aTokList.Seek( Tell() + nRelPos );
256 						SetMax();
257                     }
SeekEnd()258     void            SeekEnd()
259                     {
260                         pCurToken = aTokList.Seek( nMaxPos );
261                     }
262 };
263 
264 
265 
266 #endif // _LEX_HXX
267 
268