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 : : #include <ctype.h>
21 : : #include <stdio.h>
22 : :
23 : : #include <hash.hxx>
24 : : #include <lex.hxx>
25 : : #include <globals.hxx>
26 : : #include <rtl/strbuf.hxx>
27 : :
28 : 1716 : rtl::OString SvToken::GetTokenAsString() const
29 : : {
30 : 1716 : rtl::OString aStr;
31 [ - - - - : 1716 : switch( nType )
- + + - -
- ]
32 : : {
33 : : case SVTOKEN_EMPTY:
34 : 0 : break;
35 : : case SVTOKEN_COMMENT:
36 : 0 : aStr = aString;
37 : 0 : break;
38 : : case SVTOKEN_INTEGER:
39 : 0 : aStr = rtl::OString::valueOf(static_cast<sal_Int64>(nLong));
40 : 0 : break;
41 : : case SVTOKEN_STRING:
42 : 0 : aStr = aString;
43 : 0 : break;
44 : : case SVTOKEN_BOOL:
45 [ # # ]: 0 : aStr = bBool ? "TRUE" : "FALSE";
46 : 0 : break;
47 : : case SVTOKEN_IDENTIFIER:
48 : 1032 : aStr = aString;
49 : 1032 : break;
50 : : case SVTOKEN_CHAR:
51 : 684 : aStr = rtl::OString(cChar);
52 : 684 : break;
53 : : case SVTOKEN_RTTIBASE:
54 : 0 : aStr = "RTTIBASE";
55 : 0 : break;
56 : : case SVTOKEN_EOF:
57 : : case SVTOKEN_HASHID:
58 : 0 : break;
59 : : }
60 : :
61 : 1716 : return aStr;
62 : : }
63 : :
64 : 0 : SvToken::SvToken( const SvToken & rObj )
65 : : {
66 : 0 : nLine = rObj.nLine;
67 : 0 : nColumn = rObj.nColumn;
68 : 0 : nType = rObj.nType;
69 : 0 : aString = rObj.aString;
70 : 0 : nLong = rObj.nLong;
71 : 0 : }
72 : :
73 : 62128 : SvToken & SvToken::operator = ( const SvToken & rObj )
74 : : {
75 [ + - ]: 62128 : if( this != &rObj )
76 : : {
77 : 62128 : nLine = rObj.nLine;
78 : 62128 : nColumn = rObj.nColumn;
79 : 62128 : nType = rObj.nType;
80 : 62128 : aString = rObj.aString;
81 : 62128 : nLong = rObj.nLong;
82 : : }
83 : 62128 : return *this;
84 : : }
85 : :
86 : 488 : void SvTokenStream::InitCtor()
87 : : {
88 : 488 : aStrTrue = rtl::OString(RTL_CONSTASCII_STRINGPARAM("TRUE"));
89 : 488 : aStrFalse = rtl::OString(RTL_CONSTASCII_STRINGPARAM("FALSE"));
90 : 488 : nLine = nColumn = 0;
91 : 488 : nBufPos = 0;
92 : 488 : nTabSize = 4;
93 : 488 : nMaxPos = 0;
94 : 488 : c = GetNextChar();
95 : 488 : FillTokenList();
96 : 488 : }
97 : :
98 : 472 : SvTokenStream::SvTokenStream( const String & rFileName )
99 [ + - ]: 472 : : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
100 : : , rInStream( *pInStream )
101 [ + - ][ + - ]: 944 : , aFileName( rFileName )
[ + - ][ + - ]
102 : : {
103 [ + - ]: 472 : InitCtor();
104 : 472 : }
105 : :
106 : 16 : SvTokenStream::SvTokenStream( SvStream & rStream, const String & rFileName )
107 : : : pInStream( NULL )
108 : : , rInStream( rStream )
109 [ + - ][ + - ]: 16 : , aFileName( rFileName )
[ + - ]
110 : : {
111 [ + - ]: 16 : InitCtor();
112 : 16 : }
113 : :
114 [ + - ][ + - ]: 488 : SvTokenStream::~SvTokenStream()
115 : : {
116 [ + + ][ + - ]: 488 : delete pInStream;
117 : 488 : }
118 : :
119 : 488 : void SvTokenStream::FillTokenList()
120 : : {
121 : 488 : SvToken * pToken = new SvToken();
122 : 488 : aTokList.push_back(pToken);
123 [ + - ]: 1604598 : do
124 : : {
125 [ - + ]: 1605086 : if( !MakeToken( *pToken ) )
126 : : {
127 [ # # ]: 0 : if (!aTokList.empty())
128 : : {
129 : 0 : *pToken = SvToken();
130 [ # # ][ # # ]: 0 : boost::ptr_vector<SvToken>::const_iterator it = aTokList.begin();
131 : :
132 [ # # ]: 0 : pToken->SetLine(it->GetLine());
133 [ # # ]: 0 : pToken->SetColumn(it->GetColumn());
134 : : }
135 : 0 : break;
136 : : }
137 [ + + ]: 1605086 : else if( pToken->IsComment() )
138 : 62128 : *pToken = SvToken();
139 [ + + ]: 1542958 : else if( pToken->IsEof() )
140 : 488 : break;
141 : : else
142 : : {
143 : 1542470 : pToken = new SvToken();
144 : 1542470 : aTokList.push_back(pToken);
145 : : }
146 : : }
147 : 1604598 : while( !pToken->IsEof() );
148 : 488 : pCurToken = aTokList.begin();
149 : 488 : }
150 : :
151 : 572696 : int SvTokenStream::GetNextChar()
152 : : {
153 : : int nChar;
154 [ + + ]: 572696 : if( aBufStr.getLength() < nBufPos )
155 : : {
156 [ + + ]: 537024 : if( rInStream.ReadLine( aBufStr ) )
157 : : {
158 : 536536 : nLine++;
159 : 536536 : nColumn = 0;
160 : 536536 : nBufPos = 0;
161 : : }
162 : : else
163 : : {
164 : 488 : aBufStr = rtl::OString();
165 : 488 : nColumn = 0;
166 : 488 : nBufPos = 0;
167 : 488 : return '\0';
168 : : }
169 : : }
170 : 572208 : nChar = aBufStr[nBufPos++];
171 [ + + ]: 572208 : nColumn += nChar == '\t' ? nTabSize : 1;
172 : 572696 : return nChar;
173 : : }
174 : :
175 : 38486 : sal_uLong SvTokenStream::GetNumber()
176 : : {
177 : 38486 : sal_uLong l = 0;
178 : 38486 : short nLog = 10;
179 : :
180 [ + + ]: 38486 : if( '0' == c )
181 : : {
182 : 1848 : c = GetFastNextChar();
183 [ + + ]: 1848 : if( 'x' == c )
184 : : {
185 : 1292 : nLog = 16;
186 : 1292 : c = GetFastNextChar();
187 : : }
188 : : };
189 : :
190 [ + + ]: 38486 : if( nLog == 16 )
191 : : {
192 [ + + ]: 3956 : while( isxdigit( c ) )
193 : : {
194 [ + + ]: 2664 : if( isdigit( c ) )
195 : 2168 : l = l * nLog + (c - '0');
196 : : else
197 : 496 : l = l * nLog + (toupper( c ) - 'A' + 10 );
198 : 2664 : c = GetFastNextChar();
199 : : }
200 : : }
201 : : else
202 : : {
203 [ + + ][ - + ]: 136254 : while( isdigit( c ) || 'x' == c )
[ + + ]
204 : : {
205 : 99060 : l = l * nLog + (c - '0');
206 : 99060 : c = GetFastNextChar();
207 : : }
208 : : }
209 : :
210 : 38486 : return( l );
211 : : }
212 : :
213 : 1662054 : sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
214 : : {
215 [ + + + + : 1776478 : do
+ - ][ + + ]
216 : : {
217 [ + + ]: 1662054 : if( 0 == c )
218 : 499470 : c = GetNextChar();
219 : : // skip whitespace
220 [ + + ][ - + ]: 4631604 : while( isspace( c ) || 26 == c )
[ + + ]
221 : : {
222 : 2969550 : c = GetFastNextChar();
223 [ + + ]: 2969550 : nColumn += c == '\t' ? nTabSize : 1;
224 : : }
225 : : }
226 : 114424 : while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
227 : :
228 : 1605086 : sal_uLong nLastLine = nLine;
229 : 1605086 : sal_uLong nLastColumn = nColumn;
230 : : // comment
231 [ + + ]: 1605086 : if( '/' == c )
232 : : {
233 : : // time optimization, no comments
234 : 62464 : int c1 = c;
235 : 62464 : c = GetFastNextChar();
236 [ + + ]: 62464 : if( '/' == c )
237 : : {
238 [ + + ]: 1544922 : while( '\0' != c )
239 : : {
240 : 1517928 : c = GetFastNextChar();
241 : : }
242 : 26994 : c = GetNextChar();
243 : 26994 : rToken.nType = SVTOKEN_COMMENT;
244 : : }
245 [ + + ]: 35470 : else if( '*' == c )
246 : : {
247 : 35134 : c = GetFastNextChar();
248 [ + + ][ + + : 102280 : do
+ - + - ]
249 : : {
250 [ + + ]: 897542 : while( '*' != c )
251 : : {
252 [ + + ]: 840026 : if( '\0' == c )
253 : : {
254 : 10610 : c = GetNextChar();
255 [ - + ]: 10610 : if( IsEof() )
256 : 0 : return sal_False;
257 : : }
258 : : else
259 : 829416 : c = GetFastNextChar();
260 : : }
261 : 57516 : c = GetFastNextChar();
262 : : }
263 : 44764 : while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
264 [ + - ][ - + ]: 35134 : if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
[ - + ]
265 : 0 : return sal_False;
266 : 35134 : c = GetNextChar();
267 : 35134 : rToken.nType = SVTOKEN_COMMENT;
268 : 35134 : CalcColumn();
269 : : }
270 : : else
271 : : {
272 : 336 : rToken.nType = SVTOKEN_CHAR;
273 : 336 : rToken.cChar = (char)c1;
274 : : }
275 : : }
276 [ + + ]: 1542622 : else if( c == '"' )
277 : : {
278 : 3850 : rtl::OStringBuffer aStr;
279 : 3850 : sal_Bool bDone = sal_False;
280 [ + + ][ + - ]: 98336 : while( !bDone && !IsEof() && c )
[ + - ][ + + ]
281 : : {
282 : 94486 : c = GetFastNextChar();
283 [ - + ]: 94486 : if( '\0' == c )
284 : : {
285 : : // read strings beyond end of line
286 [ # # ]: 0 : aStr.append('\n');
287 [ # # ]: 0 : c = GetNextChar();
288 [ # # ]: 0 : if( IsEof() )
289 : 0 : return sal_False;
290 : : }
291 [ + + ]: 94486 : if( c == '"' )
292 : : {
293 : 3850 : c = GetFastNextChar();
294 [ - + ]: 3850 : if( c == '"' )
295 : : {
296 [ # # ]: 0 : aStr.append('"');
297 [ # # ]: 0 : aStr.append('"');
298 : : }
299 : : else
300 : 3850 : bDone = sal_True;
301 : : }
302 [ - + ]: 90636 : else if( c == '\\' )
303 : : {
304 [ # # ]: 0 : aStr.append('\\');
305 : 0 : c = GetFastNextChar();
306 [ # # ]: 0 : if( c )
307 [ # # ]: 0 : aStr.append(static_cast<char>(c));
308 : : }
309 : : else
310 [ + - ]: 90636 : aStr.append(static_cast<char>(c));
311 : : }
312 [ + - ][ - + ]: 3850 : if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
[ - + ]
313 : 0 : return sal_False;
314 : 3850 : rToken.nType = SVTOKEN_STRING;
315 [ + - ]: 3850 : rToken.aString = aStr.makeStringAndClear();
316 : : }
317 [ + + ]: 1538772 : else if( isdigit( c ) )
318 : : {
319 : 38486 : rToken.nType = SVTOKEN_INTEGER;
320 : 38486 : rToken.nLong = GetNumber();
321 : :
322 : : }
323 [ + + ][ + + ]: 1500286 : else if( isalpha (c) || (c == '_') )
324 : : {
325 : 745878 : rtl::OStringBuffer aBuf;
326 [ + + ][ + + ]: 7773066 : while( isalnum( c ) || c == '_' )
[ + + ]
327 : : {
328 [ + - ]: 7027188 : aBuf.append(static_cast<char>(c));
329 : 7027188 : c = GetFastNextChar();
330 : : }
331 : 745878 : rtl::OString aStr = aBuf.makeStringAndClear();
332 [ + + ]: 745878 : if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
333 : : {
334 : 50290 : rToken.nType = SVTOKEN_BOOL;
335 : 50290 : rToken.bBool = sal_True;
336 : : }
337 [ + + ]: 695588 : else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
338 : : {
339 : 149308 : rToken.nType = SVTOKEN_BOOL;
340 : 149308 : rToken.bBool = sal_False;
341 : : }
342 : : else
343 : : {
344 : : sal_uInt32 nHashId;
345 [ + - ][ + - ]: 546280 : if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
[ + + ]
346 [ + - ][ + - ]: 347514 : rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
347 : : else
348 : : {
349 : 198766 : rToken.nType = SVTOKEN_IDENTIFIER;
350 : 546280 : rToken.aString = aStr;
351 : : }
352 : 745878 : }
353 : : }
354 [ + + ]: 754408 : else if( IsEof() )
355 : : {
356 : 488 : rToken.nType = SVTOKEN_EOF;
357 : : }
358 : : else
359 : : {
360 : 753920 : rToken.nType = SVTOKEN_CHAR;
361 : 753920 : rToken.cChar = (char)c;
362 : 753920 : c = GetFastNextChar();
363 : : }
364 : 1605086 : rToken.SetLine( nLastLine );
365 : 1605086 : rToken.SetColumn( nLastColumn );
366 : 1605086 : return rInStream.GetError() == SVSTREAM_OK;
367 : : }
368 : :
369 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|