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.hxx,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 ************************************************************************/
35 #include <tools/gen.hxx>
36 #include <tools/stream.hxx>
38 /******************** enum ***********************************************/
39 enum SVTOKEN_ENUM
{ SVTOKEN_EMPTY
, SVTOKEN_COMMENT
,
40 SVTOKEN_INTEGER
, SVTOKEN_STRING
,
41 SVTOKEN_BOOL
, SVTOKEN_IDENTIFIER
,
42 SVTOKEN_CHAR
, SVTOKEN_RTTIBASE
,
43 SVTOKEN_EOF
, SVTOKEN_HASHID
};
45 /******************** class SvToken **************************************/
49 friend class SvTokenStream
;
58 // SvRttiBase * pComplexObj;
59 SvStringHashEntry
* pHash
;
63 SvToken( const SvToken
& rObj
);
65 SvToken( SVTOKEN_ENUM nTypeP
, BOOL b
);
67 SvToken( SVTOKEN_ENUM nTypeP
, const ByteString
& rStr
);
68 // SvToken( SvRttiBase * pComplexObj );
69 SvToken( SVTOKEN_ENUM nTypeP
);
71 SvToken
& operator = ( const SvToken
& rObj
);
73 ByteString
GetTokenAsString() const;
74 SVTOKEN_ENUM
GetType() const { return nType
; }
76 void SetLine( ULONG nLineP
) { nLine
= nLineP
; }
77 ULONG
GetLine() const { return nLine
; }
79 void SetColumn( ULONG nColumnP
) { nColumn
= nColumnP
; }
80 ULONG
GetColumn() const { return nColumn
; }
82 BOOL
IsEmpty() const { return nType
== SVTOKEN_EMPTY
; }
83 BOOL
IsComment() const { return nType
== SVTOKEN_COMMENT
; }
84 BOOL
IsInteger() const { return nType
== SVTOKEN_INTEGER
; }
85 BOOL
IsString() const { return nType
== SVTOKEN_STRING
; }
86 BOOL
IsBool() const { return nType
== SVTOKEN_BOOL
; }
87 BOOL
IsIdentifierHash() const
88 { return nType
== SVTOKEN_HASHID
; }
89 BOOL
IsIdentifier() const
91 return nType
== SVTOKEN_IDENTIFIER
92 || nType
== SVTOKEN_HASHID
;
94 BOOL
IsChar() const { return nType
== SVTOKEN_CHAR
; }
95 BOOL
IsRttiBase() const { return nType
== SVTOKEN_RTTIBASE
; }
96 BOOL
IsEof() const { return nType
== SVTOKEN_EOF
; }
98 const ByteString
& GetString() const
100 return IsIdentifierHash()
104 ULONG
GetNumber() const { return nLong
; }
105 BOOL
GetBool() const { return bBool
; }
106 char GetChar() const { return cChar
; }
107 // SvRttiBase *GetObject() const { return pComplexObj; }
109 void SetHash( SvStringHashEntry
* pHashP
)
110 { pHash
= pHashP
; nType
= SVTOKEN_HASHID
; }
112 { return nType
== SVTOKEN_HASHID
; }
113 SvStringHashEntry
* GetHash() const { return pHash
; }
114 BOOL
Is( SvStringHashEntry
* pEntry
) const
115 { return IsIdentifierHash() && pHash
== pEntry
; }
118 inline SvToken::SvToken()
119 : nType( SVTOKEN_EMPTY
) {}
121 inline SvToken::SvToken( ULONG n
)
122 : nType( SVTOKEN_INTEGER
), nLong( n
) {}
124 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
, BOOL b
)
125 : nType( nTypeP
), bBool( b
) {}
127 inline SvToken::SvToken( char c
)
128 : nType( SVTOKEN_CHAR
), cChar( c
) {}
130 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
, const ByteString
& rStr
)
131 : nType( nTypeP
), aString( rStr
) {}
134 inline SvToken::SvToken( SvRttiBase * pObj )
135 : nType( SVTOKEN_RTTIBASE ), pComplexObj( pObj )
139 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
)
142 DECLARE_LIST( SvTokenList
, SvToken
* )
144 /******************** class SvTokenStream ********************************/
147 ULONG nLine
, nColumn
;
149 int c
; // naechstes Zeichen
151 char * pCharTab
; // Zeiger auf die Konverierungstabelle
152 USHORT nTabSize
; // Tabulator Laenge
154 ByteString aStrFalse
;
157 SvFileStream
* pInStream
;
158 SvStream
& rInStream
;
160 SvTokenList aTokList
;
167 int GetFastNextChar()
169 return aBufStr
.GetChar((USHORT
)nBufPos
++);
172 void FillTokenList();
174 BOOL
MakeToken( SvToken
& );
175 BOOL
IsEof() const { return rInStream
.IsEof(); }
184 // wenn Zeilenende berechnung sparen
190 nColumn
+= aBufStr
.GetChar(n
++) == '\t' ? nTabSize
: 1;
194 SvTokenStream( const String
& rFileName
);
195 SvTokenStream( SvStream
& rInStream
, const String
& rFileName
);
198 const String
& GetFileName() const { return aFileName
; }
199 SvStream
& GetStream() { return rInStream
; }
201 void SetCharSet( CharSet nSet
);
202 CharSet
GetCharSet() const { return nCharSet
; }
204 void SetTabSize( USHORT nTabSizeP
)
205 { nTabSize
= nTabSizeP
; }
206 USHORT
GetTabSize() const { return nTabSize
; }
208 SvToken
* GetToken_PrevAll()
210 SvToken
* pRetToken
= pCurToken
;
211 if( NULL
== (pCurToken
= aTokList
.Prev()) )
212 // Current Zeiger nie Null
213 pCurToken
= pRetToken
;
217 SvToken
* GetToken_NextAll()
219 SvToken
* pRetToken
= pCurToken
;
220 if( NULL
== (pCurToken
= aTokList
.Next()) )
221 // Current Zeiger nie Null
222 pCurToken
= pRetToken
;
226 SvToken
* GetToken_Next()
228 // Kommentare werden initial entfernt
229 return GetToken_NextAll();
231 SvToken
* GetToken() const { return pCurToken
; }
232 BOOL
Read( char cChar
)
234 if( pCurToken
->IsChar()
235 && cChar
== pCurToken
->GetChar() )
245 if( pCurToken
->IsChar()
246 && (';' == pCurToken
->GetChar()
247 || ',' == pCurToken
->GetChar()) )
254 { return aTokList
.GetCurPos(); }
255 void Seek( UINT32 nPos
)
257 pCurToken
= aTokList
.Seek( nPos
);
260 void SeekRel( INT32 nRelPos
)
262 pCurToken
= aTokList
.Seek( Tell() + nRelPos
);
267 pCurToken
= aTokList
.Seek( nMaxPos
);