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 858 : rtl::OString SvToken::GetTokenAsString() const
29 : {
30 858 : rtl::OString aStr;
31 858 : 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 516 : aStr = aString;
49 516 : break;
50 : case SVTOKEN_CHAR:
51 342 : aStr = rtl::OString(cChar);
52 342 : 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 858 : 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 31015 : SvToken & SvToken::operator = ( const SvToken & rObj )
74 : {
75 31015 : if( this != &rObj )
76 : {
77 31015 : nLine = rObj.nLine;
78 31015 : nColumn = rObj.nColumn;
79 31015 : nType = rObj.nType;
80 31015 : aString = rObj.aString;
81 31015 : nLong = rObj.nLong;
82 : }
83 31015 : return *this;
84 : }
85 :
86 244 : void SvTokenStream::InitCtor()
87 : {
88 244 : aStrTrue = rtl::OString(RTL_CONSTASCII_STRINGPARAM("TRUE"));
89 244 : aStrFalse = rtl::OString(RTL_CONSTASCII_STRINGPARAM("FALSE"));
90 244 : nLine = nColumn = 0;
91 244 : nBufPos = 0;
92 244 : nTabSize = 4;
93 244 : nMaxPos = 0;
94 244 : c = GetNextChar();
95 244 : FillTokenList();
96 244 : }
97 :
98 236 : SvTokenStream::SvTokenStream( const String & rFileName )
99 236 : : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
100 : , rInStream( *pInStream )
101 472 : , aFileName( rFileName )
102 : {
103 236 : InitCtor();
104 236 : }
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 488 : SvTokenStream::~SvTokenStream()
115 : {
116 244 : delete pInStream;
117 244 : }
118 :
119 244 : void SvTokenStream::FillTokenList()
120 : {
121 244 : SvToken * pToken = new SvToken();
122 244 : aTokList.push_back(pToken);
123 803564 : do
124 : {
125 803808 : 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 803808 : else if( pToken->IsComment() )
138 31015 : *pToken = SvToken();
139 772793 : else if( pToken->IsEof() )
140 244 : break;
141 : else
142 : {
143 772549 : pToken = new SvToken();
144 772549 : aTokList.push_back(pToken);
145 : }
146 : }
147 803564 : while( !pToken->IsEof() );
148 244 : pCurToken = aTokList.begin();
149 244 : }
150 :
151 286282 : int SvTokenStream::GetNextChar()
152 : {
153 : int nChar;
154 286282 : if( aBufStr.getLength() < nBufPos )
155 : {
156 268422 : if( rInStream.ReadLine( aBufStr ) )
157 : {
158 268178 : nLine++;
159 268178 : nColumn = 0;
160 268178 : nBufPos = 0;
161 : }
162 : else
163 : {
164 244 : aBufStr = rtl::OString();
165 244 : nColumn = 0;
166 244 : nBufPos = 0;
167 244 : return '\0';
168 : }
169 : }
170 286038 : nChar = aBufStr[nBufPos++];
171 286038 : nColumn += nChar == '\t' ? nTabSize : 1;
172 286038 : return nChar;
173 : }
174 :
175 19281 : sal_uLong SvTokenStream::GetNumber()
176 : {
177 19281 : sal_uLong l = 0;
178 19281 : short nLog = 10;
179 :
180 19281 : if( '0' == c )
181 : {
182 922 : c = GetFastNextChar();
183 922 : if( 'x' == c )
184 : {
185 646 : nLog = 16;
186 646 : c = GetFastNextChar();
187 : }
188 : };
189 :
190 19281 : 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 86992 : while( isdigit( c ) || 'x' == c )
204 : {
205 49722 : l = l * nLog + (c - '0');
206 49722 : c = GetFastNextChar();
207 : }
208 : }
209 :
210 19281 : return( l );
211 : }
212 :
213 832317 : sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
214 : {
215 889579 : do
216 : {
217 832317 : if( 0 == c )
218 250093 : c = GetNextChar();
219 : // skip whitespace
220 3152858 : while( isspace( c ) || 26 == c )
221 : {
222 1488224 : c = GetFastNextChar();
223 1488224 : nColumn += c == '\t' ? nTabSize : 1;
224 : }
225 : }
226 57262 : while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
227 :
228 803808 : sal_uLong nLastLine = nLine;
229 803808 : sal_uLong nLastColumn = nColumn;
230 : // comment
231 803808 : if( '/' == c )
232 : {
233 : // time optimization, no comments
234 31183 : int c1 = c;
235 31183 : c = GetFastNextChar();
236 31183 : if( '/' == c )
237 : {
238 781525 : while( '\0' != c )
239 : {
240 754677 : c = GetFastNextChar();
241 : }
242 13424 : c = GetNextChar();
243 13424 : rToken.nType = SVTOKEN_COMMENT;
244 : }
245 17759 : else if( '*' == c )
246 : {
247 17591 : c = GetFastNextChar();
248 29879 : do
249 : {
250 457954 : while( '*' != c )
251 : {
252 414580 : if( '\0' == c )
253 : {
254 4930 : c = GetNextChar();
255 4930 : if( IsEof() )
256 0 : return sal_False;
257 : }
258 : else
259 409650 : c = GetFastNextChar();
260 : }
261 21687 : c = GetFastNextChar();
262 : }
263 8192 : while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
264 17591 : if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
265 0 : return sal_False;
266 17591 : c = GetNextChar();
267 17591 : rToken.nType = SVTOKEN_COMMENT;
268 17591 : CalcColumn();
269 : }
270 : else
271 : {
272 168 : rToken.nType = SVTOKEN_CHAR;
273 168 : rToken.cChar = (char)c1;
274 : }
275 : }
276 772625 : else if( c == '"' )
277 : {
278 1934 : rtl::OStringBuffer aStr;
279 1934 : sal_Bool bDone = sal_False;
280 51263 : while( !bDone && !IsEof() && c )
281 : {
282 47395 : c = GetFastNextChar();
283 47395 : 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 47395 : if( c == '"' )
292 : {
293 1934 : c = GetFastNextChar();
294 1934 : if( c == '"' )
295 : {
296 0 : aStr.append('"');
297 0 : aStr.append('"');
298 : }
299 : else
300 1934 : bDone = sal_True;
301 : }
302 45461 : 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 45461 : aStr.append(static_cast<char>(c));
311 : }
312 1934 : if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
313 0 : return sal_False;
314 1934 : rToken.nType = SVTOKEN_STRING;
315 1934 : rToken.aString = aStr.makeStringAndClear();
316 : }
317 770691 : else if( isdigit( c ) )
318 : {
319 19281 : rToken.nType = SVTOKEN_INTEGER;
320 19281 : rToken.nLong = GetNumber();
321 :
322 : }
323 751410 : else if( isalpha (c) || (c == '_') )
324 : {
325 373578 : rtl::OStringBuffer aBuf;
326 4267695 : while( isalnum( c ) || c == '_' )
327 : {
328 3520539 : aBuf.append(static_cast<char>(c));
329 3520539 : c = GetFastNextChar();
330 : }
331 373578 : rtl::OString aStr = aBuf.makeStringAndClear();
332 373578 : if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
333 : {
334 25126 : rToken.nType = SVTOKEN_BOOL;
335 25126 : rToken.bBool = sal_True;
336 : }
337 348452 : else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
338 : {
339 74775 : rToken.nType = SVTOKEN_BOOL;
340 74775 : rToken.bBool = sal_False;
341 : }
342 : else
343 : {
344 : sal_uInt32 nHashId;
345 273677 : if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
346 174010 : rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
347 : else
348 : {
349 99667 : rToken.nType = SVTOKEN_IDENTIFIER;
350 99667 : rToken.aString = aStr;
351 : }
352 373578 : }
353 : }
354 377832 : else if( IsEof() )
355 : {
356 244 : rToken.nType = SVTOKEN_EOF;
357 : }
358 : else
359 : {
360 377588 : rToken.nType = SVTOKEN_CHAR;
361 377588 : rToken.cChar = (char)c;
362 377588 : c = GetFastNextChar();
363 : }
364 803808 : rToken.SetLine( nLastLine );
365 803808 : rToken.SetColumn( nLastColumn );
366 803808 : return rInStream.GetError() == SVSTREAM_OK;
367 : }
368 :
369 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|