Bump version to 4.1-6
[LibreOffice.git] / idl / inc / lex.hxx
blob7dd36f4e530e453f11aa26f545b374597d6db205
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 _LEX_HXX
21 #define _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 sal_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, sal_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 sal_Bool IsEmpty() const { return nType == SVTOKEN_EMPTY; }
69 sal_Bool IsComment() const { return nType == SVTOKEN_COMMENT; }
70 sal_Bool IsInteger() const { return nType == SVTOKEN_INTEGER; }
71 sal_Bool IsString() const { return nType == SVTOKEN_STRING; }
72 sal_Bool IsBool() const { return nType == SVTOKEN_BOOL; }
73 sal_Bool IsIdentifierHash() const
74 { return nType == SVTOKEN_HASHID; }
75 sal_Bool IsIdentifier() const
77 return nType == SVTOKEN_IDENTIFIER
78 || nType == SVTOKEN_HASHID;
80 sal_Bool IsChar() const { return nType == SVTOKEN_CHAR; }
81 sal_Bool IsRttiBase() const { return nType == SVTOKEN_RTTIBASE; }
82 sal_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 sal_Bool GetBool() const { return bBool; }
92 char GetChar() const { return cChar; }
94 void SetHash( SvStringHashEntry * pHashP )
95 { pHash = pHashP; nType = SVTOKEN_HASHID; }
96 sal_Bool HasHash() const
97 { return nType == SVTOKEN_HASHID; }
98 SvStringHashEntry * GetHash() const { return pHash; }
99 sal_Bool Is( SvStringHashEntry * pEntry ) const
100 { return IsIdentifierHash() && pHash == pEntry; }
103 inline SvToken::SvToken()
104 : nType( SVTOKEN_EMPTY ) {}
106 inline SvToken::SvToken( sal_uLong n )
107 : nType( SVTOKEN_INTEGER ), nLong( n ) {}
109 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b )
110 : nType( nTypeP ), bBool( b ) {}
112 inline SvToken::SvToken( char c )
113 : nType( SVTOKEN_CHAR ), cChar( c ) {}
115 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, const OString& rStr )
116 : nType( nTypeP ), aString( rStr ) {}
118 inline SvToken::SvToken( SVTOKEN_ENUM nTypeP )
119 : nType( nTypeP ) {}
121 class SvTokenStream
123 sal_uLong nLine, nColumn;
124 int nBufPos;
125 int c; // next character
126 sal_uInt16 nTabSize; // length of tabulator
127 OString aStrTrue;
128 OString aStrFalse;
129 sal_uLong nMaxPos;
131 SvFileStream * pInStream;
132 SvStream & rInStream;
133 String aFileName;
134 boost::ptr_vector<SvToken> aTokList;
135 boost::ptr_vector<SvToken>::iterator pCurToken;
137 void InitCtor();
139 OString aBufStr;
140 int GetNextChar();
141 int GetFastNextChar()
143 return aBufStr[nBufPos++];
146 void FillTokenList();
147 sal_uLong GetNumber();
148 sal_Bool MakeToken( SvToken & );
149 sal_Bool IsEof() const { return rInStream.IsEof(); }
150 void SetMax()
152 sal_uLong n = Tell();
153 if( n > nMaxPos )
154 nMaxPos = n;
156 void CalcColumn()
158 // if end of line spare calculation
159 if( 0 != c )
161 sal_uInt16 n = 0;
162 nColumn = 0;
163 while( n < nBufPos )
164 nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
167 public:
168 SvTokenStream( const String & rFileName );
169 SvTokenStream( SvStream & rInStream, const String & rFileName );
170 ~SvTokenStream();
172 const String & GetFileName() const { return aFileName; }
173 SvStream & GetStream() { return rInStream; }
175 void SetTabSize( sal_uInt16 nTabSizeP )
176 { nTabSize = nTabSizeP; }
177 sal_uInt16 GetTabSize() const { return nTabSize; }
179 SvToken* GetToken_PrevAll()
181 boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken;
183 // current iterator always valid
184 if(pCurToken != aTokList.begin())
185 --pCurToken;
187 return &(*pRetToken);
190 SvToken* GetToken_NextAll()
192 boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken++;
194 if (pCurToken == aTokList.end())
195 pCurToken = pRetToken;
197 SetMax();
199 return &(*pRetToken);
202 SvToken* GetToken_Next()
204 // comments get removed initially
205 return GetToken_NextAll();
208 SvToken* GetToken() const { return &(*pCurToken); }
210 sal_Bool Read( char cChar )
212 if( pCurToken->IsChar()
213 && cChar == pCurToken->GetChar() )
215 GetToken_Next();
216 return sal_True;
218 else
219 return sal_False;
222 void ReadDelemiter()
224 if( pCurToken->IsChar()
225 && (';' == pCurToken->GetChar()
226 || ',' == pCurToken->GetChar()) )
228 GetToken_Next();
232 sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
234 void Seek( sal_uInt32 nPos )
236 pCurToken = aTokList.begin() + nPos;
237 SetMax();
240 void SeekRel( sal_uInt32 nRelPos )
242 sal_uInt32 relIdx = Tell() + nRelPos;
244 if ( relIdx < aTokList.size())
246 pCurToken = aTokList.begin()+ (Tell() + nRelPos );
247 SetMax();
251 void SeekEnd()
253 pCurToken = aTokList.begin()+nMaxPos;
259 #endif // _LEX_HXX
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */