Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #ifndef _LEX_HXX
21 : : #define _LEX_HXX
22 : :
23 : : #include <boost/ptr_container/ptr_vector.hpp>
24 : :
25 : : #include <hash.hxx>
26 : : #include <tools/gen.hxx>
27 : : #include <tools/stream.hxx>
28 : :
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 };
34 : :
35 : 1605086 : class SvToken
36 : : {
37 : : friend class SvTokenStream;
38 : : sal_uLong nLine, nColumn;
39 : : SVTOKEN_ENUM nType;
40 : : rtl::OString aString;
41 : : union
42 : : {
43 : : sal_uLong nLong;
44 : : sal_Bool bBool;
45 : : char cChar;
46 : : SvStringHashEntry * pHash;
47 : : };
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 rtl::OString& rStr );
55 : : SvToken( SVTOKEN_ENUM nTypeP );
56 : :
57 : : SvToken & operator = ( const SvToken & rObj );
58 : :
59 : : rtl::OString GetTokenAsString() const;
60 : : SVTOKEN_ENUM GetType() const { return nType; }
61 : :
62 : 1605086 : void SetLine( sal_uLong nLineP ) { nLine = nLineP; }
63 : 30120 : sal_uLong GetLine() const { return nLine; }
64 : :
65 : 1605086 : void SetColumn( sal_uLong nColumnP ) { nColumn = nColumnP; }
66 : 10040 : sal_uLong GetColumn() const { return nColumn; }
67 : :
68 : 16 : sal_Bool IsEmpty() const { return nType == SVTOKEN_EMPTY; }
69 : 1605086 : sal_Bool IsComment() const { return nType == SVTOKEN_COMMENT; }
70 : 40600 : sal_Bool IsInteger() const { return nType == SVTOKEN_INTEGER; }
71 : 2282 : sal_Bool IsString() const { return nType == SVTOKEN_STRING; }
72 : 215604 : sal_Bool IsBool() const { return nType == SVTOKEN_BOOL; }
73 : 20021174 : sal_Bool IsIdentifierHash() const
74 : 20021174 : { return nType == SVTOKEN_HASHID; }
75 : 472362 : sal_Bool IsIdentifier() const
76 : : {
77 : : return nType == SVTOKEN_IDENTIFIER
78 [ + + ][ + + ]: 472362 : || nType == SVTOKEN_HASHID;
79 : : }
80 : 1385896 : sal_Bool IsChar() const { return nType == SVTOKEN_CHAR; }
81 : : sal_Bool IsRttiBase() const { return nType == SVTOKEN_RTTIBASE; }
82 : 3197334 : sal_Bool IsEof() const { return nType == SVTOKEN_EOF; }
83 : :
84 : 5650150 : const rtl::OString& GetString() const
85 : : {
86 : 5650150 : return IsIdentifierHash()
87 : 4140 : ? pHash->GetName()
88 [ + + ]: 5650150 : : aString;
89 : : }
90 : 38460 : sal_uLong GetNumber() const { return nLong; }
91 : 199598 : sal_Bool GetBool() const { return bBool; }
92 : 2646318 : char GetChar() const { return cChar; }
93 : :
94 : 347514 : void SetHash( SvStringHashEntry * pHashP )
95 : 347514 : { pHash = pHashP; nType = SVTOKEN_HASHID; }
96 : 71658 : sal_Bool HasHash() const
97 : 71658 : { return nType == SVTOKEN_HASHID; }
98 : : SvStringHashEntry * GetHash() const { return pHash; }
99 : 14371024 : sal_Bool Is( SvStringHashEntry * pEntry ) const
100 [ + + ][ + + ]: 14371024 : { return IsIdentifierHash() && pHash == pEntry; }
101 : : };
102 : :
103 : 1605086 : inline SvToken::SvToken()
104 : 1605086 : : nType( SVTOKEN_EMPTY ) {}
105 : :
106 : : inline SvToken::SvToken( sal_uLong n )
107 : : : nType( SVTOKEN_INTEGER ), nLong( n ) {}
108 : :
109 : : inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, sal_Bool b )
110 : : : nType( nTypeP ), bBool( b ) {}
111 : :
112 : : inline SvToken::SvToken( char c )
113 : : : nType( SVTOKEN_CHAR ), cChar( c ) {}
114 : :
115 : : inline SvToken::SvToken( SVTOKEN_ENUM nTypeP, const rtl::OString& rStr )
116 : : : nType( nTypeP ), aString( rStr ) {}
117 : :
118 : : inline SvToken::SvToken( SVTOKEN_ENUM nTypeP )
119 : : : nType( nTypeP ) {}
120 : :
121 : : class SvTokenStream
122 : : {
123 : : sal_uLong nLine, nColumn;
124 : : int nBufPos;
125 : : int c; // next character
126 : : sal_uInt16 nTabSize; // length of tabulator
127 : : rtl::OString aStrTrue;
128 : : rtl::OString aStrFalse;
129 : : sal_uLong nMaxPos;
130 : :
131 : : SvFileStream * pInStream;
132 : : SvStream & rInStream;
133 : : String aFileName;
134 : : boost::ptr_vector<SvToken> aTokList;
135 : : boost::ptr_vector<SvToken>::iterator pCurToken;
136 : :
137 : : void InitCtor();
138 : :
139 : : rtl::OString aBufStr;
140 : : int GetNextChar();
141 : 13456316 : int GetFastNextChar()
142 : : {
143 : 13456316 : return aBufStr[nBufPos++];
144 : : }
145 : :
146 : : void FillTokenList();
147 : : sal_uLong GetNumber();
148 : : sal_Bool MakeToken( SvToken & );
149 : 978326 : sal_Bool IsEof() const { return rInStream.IsEof(); }
150 : 29372866 : void SetMax()
151 : : {
152 : 29372866 : sal_uLong n = Tell();
153 [ + + ]: 29372866 : if( n > nMaxPos )
154 : 1542470 : nMaxPos = n;
155 : 29372866 : }
156 : 35134 : void CalcColumn()
157 : : {
158 : : // if end of line spare calculation
159 [ - + ]: 35134 : if( 0 != c )
160 : : {
161 : 0 : sal_uInt16 n = 0;
162 : 0 : nColumn = 0;
163 [ # # ]: 0 : while( n < nBufPos )
164 [ # # ]: 0 : nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
165 : : }
166 : 35134 : }
167 : : public:
168 : : SvTokenStream( const String & rFileName );
169 : : SvTokenStream( SvStream & rInStream, const String & rFileName );
170 : : ~SvTokenStream();
171 : :
172 : 16 : const String & GetFileName() const { return aFileName; }
173 : 472 : SvStream & GetStream() { return rInStream; }
174 : :
175 : : void SetTabSize( sal_uInt16 nTabSizeP )
176 : : { nTabSize = nTabSizeP; }
177 : : sal_uInt16 GetTabSize() const { return nTabSize; }
178 : :
179 : 0 : SvToken* GetToken_PrevAll()
180 : : {
181 : 0 : boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken;
182 : :
183 : : // current iterator always valid
184 [ # # ][ # # ]: 0 : if(pCurToken != aTokList.begin())
[ # # ]
185 [ # # ]: 0 : --pCurToken;
186 : :
187 [ # # ]: 0 : return &(*pRetToken);
188 : : }
189 : :
190 : 15451790 : SvToken* GetToken_NextAll()
191 : : {
192 [ + - ]: 15451790 : boost::ptr_vector<SvToken>::iterator pRetToken = pCurToken++;
193 : :
194 [ + - ][ + - ]: 15451790 : if (pCurToken == aTokList.end())
[ + + ]
195 : 1004 : pCurToken = pRetToken;
196 : :
197 [ + - ]: 15451790 : SetMax();
198 : :
199 [ + - ]: 15451790 : return &(*pRetToken);
200 : : }
201 : :
202 : 15451790 : SvToken* GetToken_Next()
203 : : {
204 : : // comments get removed initially
205 : 15451790 : return GetToken_NextAll();
206 : : }
207 : :
208 : 506152 : SvToken* GetToken() const { return &(*pCurToken); }
209 : :
210 : 713586 : sal_Bool Read( char cChar )
211 : : {
212 [ + + + + ]: 1403502 : if( pCurToken->IsChar()
[ + + ]
213 : 689916 : && cChar == pCurToken->GetChar() )
214 : : {
215 : 317532 : GetToken_Next();
216 : 317532 : return sal_True;
217 : : }
218 : : else
219 : 713586 : return sal_False;
220 : : }
221 : :
222 : 371508 : void ReadDelemiter()
223 : : {
224 [ + + + + : 959254 : if( pCurToken->IsChar()
+ + ][ + + ]
225 : 329426 : && (';' == pCurToken->GetChar()
226 : 258320 : || ',' == pCurToken->GetChar()) )
227 : : {
228 : 291780 : GetToken_Next();
229 : : }
230 : 371508 : }
231 : :
232 : 44933440 : sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
233 : :
234 : 13921076 : void Seek( sal_uInt32 nPos )
235 : : {
236 : 13921076 : pCurToken = aTokList.begin() + nPos;
237 : 13921076 : SetMax();
238 : 13921076 : }
239 : :
240 : : void SeekRel( sal_uInt32 nRelPos )
241 : : {
242 : : sal_uInt32 relIdx = Tell() + nRelPos;
243 : :
244 : : if ( relIdx < aTokList.size())
245 : : {
246 : : pCurToken = aTokList.begin()+ (Tell() + nRelPos );
247 : : SetMax();
248 : : }
249 : : }
250 : :
251 : 0 : void SeekEnd()
252 : : {
253 : 0 : pCurToken = aTokList.begin()+nMaxPos;
254 : 0 : }
255 : : };
256 : :
257 : :
258 : :
259 : : #endif // _LEX_HXX
260 : :
261 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|