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 5930 : OString SvToken::GetTokenAsString() const
29 : {
30 5930 : OString aStr;
31 5930 : 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 = OString::number(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 516 : aStr = aString;
49 516 : break;
50 : case SVTOKEN_CHAR:
51 5280 : aStr = OString(cChar);
52 5280 : break;
53 : case SVTOKEN_RTTIBASE:
54 0 : aStr = "RTTIBASE";
55 0 : break;
56 : case SVTOKEN_EOF:
57 : case SVTOKEN_HASHID:
58 134 : break;
59 : }
60 :
61 5930 : 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 32025 : SvToken & SvToken::operator = ( const SvToken & rObj )
74 : {
75 32025 : if( this != &rObj )
76 : {
77 32025 : nLine = rObj.nLine;
78 32025 : nColumn = rObj.nColumn;
79 32025 : nType = rObj.nType;
80 32025 : aString = rObj.aString;
81 32025 : nLong = rObj.nLong;
82 : }
83 32025 : return *this;
84 : }
85 :
86 245 : void SvTokenStream::InitCtor()
87 : {
88 245 : aStrTrue = OString(RTL_CONSTASCII_STRINGPARAM("TRUE"));
89 245 : aStrFalse = OString(RTL_CONSTASCII_STRINGPARAM("FALSE"));
90 245 : nLine = nColumn = 0;
91 245 : nBufPos = 0;
92 245 : nTabSize = 4;
93 245 : nMaxPos = 0;
94 245 : c = GetNextChar();
95 245 : FillTokenList();
96 245 : }
97 :
98 237 : SvTokenStream::SvTokenStream( const String & rFileName )
99 237 : : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
100 : , rInStream( *pInStream )
101 474 : , aFileName( rFileName )
102 : {
103 237 : InitCtor();
104 237 : }
105 :
106 8 : SvTokenStream::SvTokenStream( SvStream & rStream, const String & rFileName )
107 : : pInStream( NULL )
108 : , rInStream( rStream )
109 8 : , aFileName( rFileName )
110 : {
111 8 : InitCtor();
112 8 : }
113 :
114 490 : SvTokenStream::~SvTokenStream()
115 : {
116 245 : delete pInStream;
117 245 : }
118 :
119 245 : void SvTokenStream::FillTokenList()
120 : {
121 245 : SvToken * pToken = new SvToken();
122 245 : aTokList.push_back(pToken);
123 826222 : do
124 : {
125 826467 : 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 826467 : else if( pToken->IsComment() )
138 32025 : *pToken = SvToken();
139 794442 : else if( pToken->IsEof() )
140 245 : break;
141 : else
142 : {
143 794197 : pToken = new SvToken();
144 794197 : aTokList.push_back(pToken);
145 : }
146 : }
147 826222 : while( !pToken->IsEof() );
148 245 : pCurToken = aTokList.begin();
149 245 : }
150 :
151 294839 : int SvTokenStream::GetNextChar()
152 : {
153 : int nChar;
154 294839 : if( aBufStr.getLength() < nBufPos )
155 : {
156 276383 : if( rInStream.ReadLine( aBufStr ) )
157 : {
158 276138 : nLine++;
159 276138 : nColumn = 0;
160 276138 : nBufPos = 0;
161 : }
162 : else
163 : {
164 245 : aBufStr = OString();
165 245 : nColumn = 0;
166 245 : nBufPos = 0;
167 245 : return '\0';
168 : }
169 : }
170 294594 : nChar = aBufStr[nBufPos++];
171 294594 : nColumn += nChar == '\t' ? nTabSize : 1;
172 294594 : return nChar;
173 : }
174 :
175 19590 : sal_uLong SvTokenStream::GetNumber()
176 : {
177 19590 : sal_uLong l = 0;
178 19590 : short nLog = 10;
179 :
180 19590 : if( '0' == c )
181 : {
182 928 : c = GetFastNextChar();
183 928 : if( 'x' == c )
184 : {
185 646 : nLog = 16;
186 646 : c = GetFastNextChar();
187 : }
188 : };
189 :
190 19590 : if( nLog == 16 )
191 : {
192 2624 : while( isxdigit( c ) )
193 : {
194 1332 : if( isdigit( c ) )
195 1084 : l = l * nLog + (c - '0');
196 : else
197 248 : l = l * nLog + (toupper( c ) - 'A' + 10 );
198 1332 : c = GetFastNextChar();
199 : }
200 : }
201 : else
202 : {
203 88672 : while( isdigit( c ) || 'x' == c )
204 : {
205 50784 : l = l * nLog + (c - '0');
206 50784 : c = GetFastNextChar();
207 : }
208 : }
209 :
210 19590 : return( l );
211 : }
212 :
213 855836 : sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
214 : {
215 855836 : do
216 : {
217 855836 : if( 0 == c )
218 257628 : c = GetNextChar();
219 : // skip whitespace
220 3236741 : while( isspace( c ) || 26 == c )
221 : {
222 1525069 : c = GetFastNextChar();
223 1525069 : nColumn += c == '\t' ? nTabSize : 1;
224 : }
225 : }
226 855836 : while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
227 :
228 826467 : sal_uLong nLastLine = nLine;
229 826467 : sal_uLong nLastColumn = nColumn;
230 : // comment
231 826467 : if( '/' == c )
232 : {
233 : // time optimization, no comments
234 32193 : int c1 = c;
235 32193 : c = GetFastNextChar();
236 32193 : if( '/' == c )
237 : {
238 802178 : while( '\0' != c )
239 : {
240 774522 : c = GetFastNextChar();
241 : }
242 13828 : c = GetNextChar();
243 13828 : rToken.nType = SVTOKEN_COMMENT;
244 : }
245 18365 : else if( '*' == c )
246 : {
247 18197 : c = GetFastNextChar();
248 22442 : do
249 : {
250 474148 : while( '*' != c )
251 : {
252 429264 : if( '\0' == c )
253 : {
254 4941 : c = GetNextChar();
255 4941 : if( IsEof() )
256 0 : return sal_False;
257 : }
258 : else
259 424323 : c = GetFastNextChar();
260 : }
261 22442 : c = GetFastNextChar();
262 : }
263 22442 : while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
264 18197 : if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
265 0 : return sal_False;
266 18197 : c = GetNextChar();
267 18197 : rToken.nType = SVTOKEN_COMMENT;
268 18197 : CalcColumn();
269 : }
270 : else
271 : {
272 168 : rToken.nType = SVTOKEN_CHAR;
273 168 : rToken.cChar = (char)c1;
274 : }
275 : }
276 794274 : else if( c == '"' )
277 : {
278 1988 : OStringBuffer aStr;
279 1988 : sal_Bool bDone = sal_False;
280 53654 : while( !bDone && !IsEof() && c )
281 : {
282 49678 : c = GetFastNextChar();
283 49678 : 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 49678 : if( c == '"' )
292 : {
293 1988 : c = GetFastNextChar();
294 1988 : if( c == '"' )
295 : {
296 0 : aStr.append('"');
297 0 : aStr.append('"');
298 : }
299 : else
300 1988 : bDone = sal_True;
301 : }
302 47690 : 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 47690 : aStr.append(static_cast<char>(c));
311 : }
312 1988 : if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
313 0 : return sal_False;
314 1988 : rToken.nType = SVTOKEN_STRING;
315 1988 : rToken.aString = aStr.makeStringAndClear();
316 : }
317 792286 : else if( isdigit( c ) )
318 : {
319 19590 : rToken.nType = SVTOKEN_INTEGER;
320 19590 : rToken.nLong = GetNumber();
321 :
322 : }
323 772696 : else if( isalpha (c) || (c == '_') )
324 : {
325 384170 : OStringBuffer aBuf;
326 4395446 : while( isalnum( c ) || c == '_' )
327 : {
328 3627106 : aBuf.append(static_cast<char>(c));
329 3627106 : c = GetFastNextChar();
330 : }
331 768340 : OString aStr = aBuf.makeStringAndClear();
332 384170 : if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
333 : {
334 25517 : rToken.nType = SVTOKEN_BOOL;
335 25517 : rToken.bBool = sal_True;
336 : }
337 358653 : else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
338 : {
339 77446 : rToken.nType = SVTOKEN_BOOL;
340 77446 : rToken.bBool = sal_False;
341 : }
342 : else
343 : {
344 : sal_uInt32 nHashId;
345 281207 : if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
346 179044 : rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
347 : else
348 : {
349 102163 : rToken.nType = SVTOKEN_IDENTIFIER;
350 102163 : rToken.aString = aStr;
351 : }
352 384170 : }
353 : }
354 388526 : else if( IsEof() )
355 : {
356 245 : rToken.nType = SVTOKEN_EOF;
357 : }
358 : else
359 : {
360 388281 : rToken.nType = SVTOKEN_CHAR;
361 388281 : rToken.cChar = (char)c;
362 388281 : c = GetFastNextChar();
363 : }
364 826467 : rToken.SetLine( nLastLine );
365 826467 : rToken.SetColumn( nLastColumn );
366 826467 : return rInStream.GetError() == SVSTREAM_OK;
367 : }
368 :
369 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|