bump product version to 5.0.4.1
[LibreOffice.git] / idl / inc / lex.hxx
bloba11bd6183ea9669b163e49340a18b69ff62eed74
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #include <hash.hxx>
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 };
35 class SvToken
37 friend class SvTokenStream;
38 sal_uLong nLine, nColumn;
39 SVTOKEN_ENUM nType;
40 OString aString;
41 union
43 sal_uLong nLong;
44 bool bBool;
45 char cChar;
46 SvStringHashEntry * pHash;
48 public:
49 SvToken();
50 SvToken( const SvToken & rObj );
51 SvToken( sal_uLong n );
52 SvToken( SVTOKEN_ENUM nTypeP, bool b );
53 SvToken( char c );
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()
87 ? pHash->GetName()
88 : aString;
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; }
96 bool HasHash() const
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()
104 : nLine(0)
105 , nColumn(0)
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 )
123 : nType( nTypeP ) {}
125 class SvTokenStream
127 sal_uLong nLine, nColumn;
128 int nBufPos;
129 int c; // next character
130 sal_uInt16 nTabSize; // length of tabulator
131 OString aStrTrue;
132 OString aStrFalse;
133 sal_uLong nMaxPos;
135 SvFileStream * pInStream;
136 SvStream & rInStream;
137 OUString aFileName;
138 boost::ptr_vector<SvToken> aTokList;
139 boost::ptr_vector<SvToken>::iterator pCurToken;
141 OString aBufStr;
143 void InitCtor();
145 int GetNextChar();
146 int GetFastNextChar()
148 return (nBufPos < aBufStr.getLength())
149 ? aBufStr[nBufPos++]
150 : '\0';
153 void FillTokenList();
154 sal_uLong GetNumber();
155 bool MakeToken( SvToken & );
156 bool IsEof() const { return rInStream.IsEof(); }
157 void SetMax()
159 sal_uLong n = Tell();
160 if( n > nMaxPos )
161 nMaxPos = n;
163 void CalcColumn()
165 // if end of line spare calculation
166 if( 0 != c )
168 sal_uInt16 n = 0;
169 nColumn = 0;
170 while( n < nBufPos )
171 nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
174 public:
175 SvTokenStream( const OUString & rFileName );
176 SvTokenStream( SvStream & rInStream, const OUString & rFileName );
177 ~SvTokenStream();
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())
192 --pCurToken;
194 return &(*pRetToken);
197 SvToken* GetToken_NextAll()
199 boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken++;
201 if (pCurToken == aTokList.end())
202 pCurToken = pRetToken;
204 SetMax();
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() )
222 GetToken_Next();
223 return true;
225 else
226 return false;
229 void ReadDelemiter()
231 if( pCurToken->IsChar()
232 && (';' == pCurToken->GetChar()
233 || ',' == pCurToken->GetChar()) )
235 GetToken_Next();
239 sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
241 void Seek( sal_uInt32 nPos )
243 pCurToken = aTokList.begin() + nPos;
244 SetMax();
247 void SeekRel( sal_uInt32 nRelPos )
249 sal_uInt32 relIdx = Tell() + nRelPos;
251 if ( relIdx < aTokList.size())
253 pCurToken = aTokList.begin()+ (Tell() + nRelPos );
254 SetMax();
258 void SeekEnd()
260 pCurToken = aTokList.begin()+nMaxPos;
266 #endif // INCLUDED_IDL_INC_LEX_HXX
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */