1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_IDL_INC_LEX_HXX
21 #define INCLUDED_IDL_INC_LEX_HXX
23 #include <boost/ptr_container/ptr_vector.hpp>
25 #include <sal/types.h>
27 #include <tools/stream.hxx>
29 enum SVTOKEN_ENUM
{ SVTOKEN_EMPTY
, SVTOKEN_COMMENT
,
30 SVTOKEN_INTEGER
, SVTOKEN_STRING
,
31 SVTOKEN_BOOL
, SVTOKEN_IDENTIFIER
,
32 SVTOKEN_CHAR
, SVTOKEN_RTTIBASE
,
33 SVTOKEN_EOF
, SVTOKEN_HASHID
};
37 friend class SvTokenStream
;
38 sal_uLong nLine
, nColumn
;
46 SvStringHashEntry
* pHash
;
50 SvToken( const SvToken
& rObj
);
51 SvToken( sal_uLong n
);
52 SvToken( SVTOKEN_ENUM nTypeP
, bool b
);
54 SvToken( SVTOKEN_ENUM nTypeP
, const OString
& rStr
);
55 SvToken( SVTOKEN_ENUM nTypeP
);
57 SvToken
& operator = ( const SvToken
& rObj
);
59 OString
GetTokenAsString() const;
60 SVTOKEN_ENUM
GetType() const { return nType
; }
62 void SetLine( sal_uLong nLineP
) { nLine
= nLineP
; }
63 sal_uLong
GetLine() const { return nLine
; }
65 void SetColumn( sal_uLong nColumnP
) { nColumn
= nColumnP
; }
66 sal_uLong
GetColumn() const { return nColumn
; }
68 bool IsEmpty() const { return nType
== SVTOKEN_EMPTY
; }
69 bool IsComment() const { return nType
== SVTOKEN_COMMENT
; }
70 bool IsInteger() const { return nType
== SVTOKEN_INTEGER
; }
71 bool IsString() const { return nType
== SVTOKEN_STRING
; }
72 bool IsBool() const { return nType
== SVTOKEN_BOOL
; }
73 bool IsIdentifierHash() const
74 { return nType
== SVTOKEN_HASHID
; }
75 bool IsIdentifier() const
77 return nType
== SVTOKEN_IDENTIFIER
78 || nType
== SVTOKEN_HASHID
;
80 bool IsChar() const { return nType
== SVTOKEN_CHAR
; }
81 bool IsRttiBase() const { return nType
== SVTOKEN_RTTIBASE
; }
82 bool IsEof() const { return nType
== SVTOKEN_EOF
; }
84 const OString
& GetString() const
86 return IsIdentifierHash()
90 sal_uLong
GetNumber() const { return nLong
; }
91 bool GetBool() const { return bBool
; }
92 char GetChar() const { return cChar
; }
94 void SetHash( SvStringHashEntry
* pHashP
)
95 { pHash
= pHashP
; nType
= SVTOKEN_HASHID
; }
97 { return nType
== SVTOKEN_HASHID
; }
98 SvStringHashEntry
* GetHash() const { return pHash
; }
99 bool Is( SvStringHashEntry
* pEntry
) const
100 { return IsIdentifierHash() && pHash
== pEntry
; }
103 inline SvToken::SvToken()
106 , nType( SVTOKEN_EMPTY
)
110 inline SvToken::SvToken( sal_uLong n
)
111 : nType( SVTOKEN_INTEGER
), nLong( n
) {}
113 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
, bool b
)
114 : nType( nTypeP
), bBool( b
) {}
116 inline SvToken::SvToken( char c
)
117 : nType( SVTOKEN_CHAR
), cChar( c
) {}
119 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
, const OString
& rStr
)
120 : nType( nTypeP
), aString( rStr
) {}
122 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP
)
127 sal_uLong nLine
, nColumn
;
129 int c
; // next character
130 sal_uInt16 nTabSize
; // length of tabulator
135 SvFileStream
* pInStream
;
136 SvStream
& rInStream
;
138 boost::ptr_vector
<SvToken
> aTokList
;
139 boost::ptr_vector
<SvToken
>::iterator pCurToken
;
146 int GetFastNextChar()
148 return (nBufPos
< aBufStr
.getLength())
153 void FillTokenList();
154 sal_uLong
GetNumber();
155 bool MakeToken( SvToken
& );
156 bool IsEof() const { return rInStream
.IsEof(); }
159 sal_uLong n
= Tell();
165 // if end of line spare calculation
171 nColumn
+= aBufStr
[n
++] == '\t' ? nTabSize
: 1;
175 SvTokenStream( const OUString
& rFileName
);
176 SvTokenStream( SvStream
& rInStream
, const OUString
& rFileName
);
179 const OUString
& GetFileName() const { return aFileName
; }
180 SvStream
& GetStream() { return rInStream
; }
182 void SetTabSize( sal_uInt16 nTabSizeP
)
183 { nTabSize
= nTabSizeP
; }
184 sal_uInt16
GetTabSize() const { return nTabSize
; }
186 SvToken
* GetToken_PrevAll()
188 boost::ptr_vector
<SvToken
>::iterator pRetToken
= pCurToken
;
190 // current iterator always valid
191 if(pCurToken
!= aTokList
.begin())
194 return &(*pRetToken
);
197 SvToken
* GetToken_NextAll()
199 boost::ptr_vector
<SvToken
>::iterator pRetToken
= pCurToken
++;
201 if (pCurToken
== aTokList
.end())
202 pCurToken
= pRetToken
;
206 return &(*pRetToken
);
209 SvToken
* GetToken_Next()
211 // comments get removed initially
212 return GetToken_NextAll();
215 SvToken
* GetToken() const { return &(*pCurToken
); }
217 bool Read( char cChar
)
219 if( pCurToken
->IsChar()
220 && cChar
== pCurToken
->GetChar() )
231 if( pCurToken
->IsChar()
232 && (';' == pCurToken
->GetChar()
233 || ',' == pCurToken
->GetChar()) )
239 sal_uInt32
Tell() const { return pCurToken
-aTokList
.begin(); }
241 void Seek( sal_uInt32 nPos
)
243 pCurToken
= aTokList
.begin() + nPos
;
247 void SeekRel( sal_uInt32 nRelPos
)
249 sal_uInt32 relIdx
= Tell() + nRelPos
;
251 if ( relIdx
< aTokList
.size())
253 pCurToken
= aTokList
.begin()+ (Tell() + nRelPos
);
260 pCurToken
= aTokList
.begin()+nMaxPos
;
266 #endif // INCLUDED_IDL_INC_LEX_HXX
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */