Branch libreoffice-5-0-4
[LibreOffice.git] / idl / source / cmptools / lex.cxx
blobef606941af2d61c76fb60f39e8c0d8d1155ce41b
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 #include <ctype.h>
21 #include <stdio.h>
23 #include <hash.hxx>
24 #include <lex.hxx>
25 #include <globals.hxx>
26 #include <rtl/strbuf.hxx>
28 OString SvToken::GetTokenAsString() const
30 OString aStr;
31 switch( nType )
33 case SVTOKEN_EMPTY:
34 break;
35 case SVTOKEN_COMMENT:
36 aStr = aString;
37 break;
38 case SVTOKEN_INTEGER:
39 aStr = OString::number(nLong);
40 break;
41 case SVTOKEN_STRING:
42 aStr = aString;
43 break;
44 case SVTOKEN_BOOL:
45 aStr = bBool ? "TRUE" : "FALSE";
46 break;
47 case SVTOKEN_IDENTIFIER:
48 aStr = aString;
49 break;
50 case SVTOKEN_CHAR:
51 aStr = OString(cChar);
52 break;
53 case SVTOKEN_RTTIBASE:
54 aStr = "RTTIBASE";
55 break;
56 case SVTOKEN_EOF:
57 case SVTOKEN_HASHID:
58 break;
61 return aStr;
64 SvToken::SvToken( const SvToken & rObj )
66 nLine = rObj.nLine;
67 nColumn = rObj.nColumn;
68 nType = rObj.nType;
69 aString = rObj.aString;
70 nLong = rObj.nLong;
73 SvToken & SvToken::operator = ( const SvToken & rObj )
75 if( this != &rObj )
77 nLine = rObj.nLine;
78 nColumn = rObj.nColumn;
79 nType = rObj.nType;
80 aString = rObj.aString;
81 nLong = rObj.nLong;
83 return *this;
86 void SvTokenStream::InitCtor()
88 aStrTrue = OString("TRUE");
89 aStrFalse = OString("FALSE");
90 nLine = nColumn = 0;
91 nBufPos = 0;
92 nTabSize = 4;
93 nMaxPos = 0;
94 c = GetNextChar();
95 FillTokenList();
98 SvTokenStream::SvTokenStream( const OUString & rFileName )
99 : pInStream( new SvFileStream( rFileName, STREAM_STD_READ | StreamMode::NOCREATE ) )
100 , rInStream( *pInStream )
101 , aFileName( rFileName )
103 InitCtor();
106 SvTokenStream::SvTokenStream( SvStream & rStream, const OUString & rFileName )
107 : pInStream( NULL )
108 , rInStream( rStream )
109 , aFileName( rFileName )
111 InitCtor();
114 SvTokenStream::~SvTokenStream()
116 delete pInStream;
119 void SvTokenStream::FillTokenList()
121 SvToken * pToken = new SvToken();
122 aTokList.push_back(pToken);
125 if( !MakeToken( *pToken ) )
127 if (!aTokList.empty())
129 *pToken = SvToken();
130 boost::ptr_vector<SvToken>::const_iterator it = aTokList.begin();
132 pToken->SetLine(it->GetLine());
133 pToken->SetColumn(it->GetColumn());
135 break;
137 else if( pToken->IsComment() )
138 *pToken = SvToken();
139 else if( pToken->IsEof() )
140 break;
141 else
143 pToken = new SvToken();
144 aTokList.push_back(pToken);
147 while( !pToken->IsEof() );
148 pCurToken = aTokList.begin();
151 int SvTokenStream::GetNextChar()
153 int nChar;
154 while (aBufStr.getLength() <= nBufPos)
156 if (rInStream.ReadLine(aBufStr))
158 nLine++;
159 nColumn = 0;
160 nBufPos = 0;
162 else
164 aBufStr.clear();
165 nColumn = 0;
166 nBufPos = 0;
167 return '\0';
170 nChar = aBufStr[nBufPos++];
171 nColumn += nChar == '\t' ? nTabSize : 1;
172 return nChar;
175 sal_uLong SvTokenStream::GetNumber()
177 sal_uLong l = 0;
178 short nLog = 10;
180 if( '0' == c )
182 c = GetFastNextChar();
183 if( 'x' == c )
185 nLog = 16;
186 c = GetFastNextChar();
190 if( nLog == 16 )
192 while( isxdigit( c ) )
194 if( isdigit( c ) )
195 l = l * nLog + (c - '0');
196 else
197 l = l * nLog + (toupper( c ) - 'A' + 10 );
198 c = GetFastNextChar();
201 else
203 while( isdigit( c ) || 'x' == c )
205 l = l * nLog + (c - '0');
206 c = GetFastNextChar();
210 return l;
213 bool SvTokenStream::MakeToken( SvToken & rToken )
217 if( 0 == c )
218 c = GetNextChar();
219 // skip whitespace
220 while( isspace( c ) || 26 == c )
222 c = GetFastNextChar();
223 nColumn += c == '\t' ? nTabSize : 1;
226 while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
228 sal_uLong nLastLine = nLine;
229 sal_uLong nLastColumn = nColumn;
230 // comment
231 if( '/' == c )
233 // time optimization, no comments
234 int c1 = c;
235 c = GetFastNextChar();
236 if( '/' == c )
238 while( '\0' != c )
240 c = GetFastNextChar();
242 c = GetNextChar();
243 rToken.nType = SVTOKEN_COMMENT;
245 else if( '*' == c )
247 c = GetFastNextChar();
250 while( '*' != c )
252 if( '\0' == c )
254 c = GetNextChar();
255 if( IsEof() )
256 return false;
258 else
259 c = GetFastNextChar();
261 c = GetFastNextChar();
263 while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
264 if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
265 return false;
266 c = GetNextChar();
267 rToken.nType = SVTOKEN_COMMENT;
268 CalcColumn();
270 else
272 rToken.nType = SVTOKEN_CHAR;
273 rToken.cChar = (char)c1;
276 else if( c == '"' )
278 OStringBuffer aStr;
279 bool bDone = false;
280 while( !bDone && !IsEof() && c )
282 c = GetFastNextChar();
283 if( '\0' == c )
285 // read strings beyond end of line
286 aStr.append('\n');
287 c = GetNextChar();
288 if( IsEof() )
289 return false;
291 if( c == '"' )
293 c = GetFastNextChar();
294 if( c == '"' )
296 aStr.append('"');
297 aStr.append('"');
299 else
300 bDone = true;
302 else if( c == '\\' )
304 aStr.append('\\');
305 c = GetFastNextChar();
306 if( c )
307 aStr.append(static_cast<char>(c));
309 else
310 aStr.append(static_cast<char>(c));
312 if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
313 return false;
314 rToken.nType = SVTOKEN_STRING;
315 rToken.aString = aStr.makeStringAndClear();
317 else if( isdigit( c ) )
319 rToken.nType = SVTOKEN_INTEGER;
320 rToken.nLong = GetNumber();
323 else if( isalpha (c) || (c == '_') )
325 OStringBuffer aBuf;
326 while( isalnum( c ) || c == '_' )
328 aBuf.append(static_cast<char>(c));
329 c = GetFastNextChar();
331 OString aStr = aBuf.makeStringAndClear();
332 if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
334 rToken.nType = SVTOKEN_BOOL;
335 rToken.bBool = true;
337 else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
339 rToken.nType = SVTOKEN_BOOL;
340 rToken.bBool = false;
342 else
344 sal_uInt32 nHashId;
345 if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
346 rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
347 else
349 rToken.nType = SVTOKEN_IDENTIFIER;
350 rToken.aString = aStr;
354 else if( IsEof() )
356 rToken.nType = SVTOKEN_EOF;
358 else
360 rToken.nType = SVTOKEN_CHAR;
361 rToken.cChar = (char)c;
362 c = GetFastNextChar();
364 rToken.SetLine( nLastLine );
365 rToken.SetColumn( nLastColumn );
366 return rInStream.GetError() == SVSTREAM_OK;
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */