bump product version to 5.0.4.1
[LibreOffice.git] / rsc / source / parser / rsclex.cxx
blob6f906e67a746d51417b3f84b46c6f0d2c8ca0281
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 <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <limits.h>
26 #include <rscerror.h>
27 #include <rschash.hxx>
28 #include <rscdb.hxx>
29 #include <rsctop.hxx>
30 #include <rsckey.hxx>
31 #include <rscpar.hxx>
32 #include <rscdef.hxx>
34 #include <rsclex.hxx>
35 #include <rscyacc.hxx>
37 #include <rtl/textcvt.h>
38 #include <rtl/textenc.h>
41 const char* StringContainer::putString( const char* pString )
43 OString aString( static_cast<const sal_Char*>(pString) );
44 std::pair<
45 std::unordered_set< OString, OStringHash >::iterator,
46 bool > aInsert =
47 m_aStrings.insert( aString );
49 return aInsert.first->getStr();
52 int c;
53 bool bLastInclude;// War letztes Symbol INCLUDE
54 RscFileInst* pFI;
55 RscTypCont* pTC;
56 RscExpression * pExp;
57 struct KeyVal
59 int nKeyWord;
60 YYSTYPE aYYSType;
61 } aKeyVal[ 1 ];
62 bool bTargetDefined;
64 StringContainer* pStringContainer = NULL;
67 sal_uInt32 GetNumber()
69 sal_uInt32 l = 0;
70 sal_uInt32 nLog = 10;
72 if( '0' == c )
74 c = pFI->GetFastChar();
75 if( 'x' == c )
77 nLog = 16;
78 c = pFI->GetFastChar();
82 if( nLog == 16 )
84 while( isxdigit( c ) )
86 if( isdigit( c ) )
87 l = l * nLog + (c - '0');
88 else
89 l = l * nLog + (toupper( c ) - 'A' + 10 );
91 c = pFI->GetFastChar();
94 else
96 while( isdigit( c ) || 'x' == c )
98 l = l * nLog + (c - '0');
99 c = pFI->GetFastChar();
103 while( c=='U' || c=='u' || c=='l' || c=='L' ) //Wg. Unsigned Longs
104 c = pFI->GetFastChar();
106 if( l > 0x7fffffff ) //Oberstes bit gegebenenfalls abschneiden;
107 l &= 0x7fffffff;
109 return l;
112 int MakeToken( YYSTYPE * pTokenVal )
114 int c1;
116 while( true ) // Kommentare und Leerzeichen ueberlesen
118 while( isspace( c ) )
119 c = pFI->GetFastChar();
121 if( '/' == c )
123 c1 = c;
124 c = pFI->GetFastChar();
125 if( '/' == c )
127 while( '\n' != c && !pFI->IsEof() )
128 c = pFI->GetFastChar();
130 c = pFI->GetFastChar();
132 else if( '*' == c )
134 c = pFI->GetFastChar();
137 while( '*' != c && !pFI->IsEof() )
138 c = pFI->GetFastChar();
140 c = pFI->GetFastChar();
142 while( '/' != c && !pFI->IsEof() );
143 c = pFI->GetFastChar();
145 else
146 return c1;
148 else
149 break;
152 // FIXME: wtf is this supposed to do?
153 if( (c != 0) == pFI->IsEof() )
155 return 0;
158 if( bLastInclude )
160 bLastInclude = false; //Zuruecksetzten
161 if( '<' == c )
163 OStringBuffer aBuf( 256 );
164 c = pFI->GetFastChar();
165 while( '>' != c && !pFI->IsEof() )
167 aBuf.append( sal_Char(c) );
168 c = pFI->GetFastChar();
170 c = pFI->GetFastChar();
171 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
172 return INCLUDE_STRING;
176 if( c == '"' )
178 OStringBuffer aBuf( 256 );
179 bool bDone = false;
180 while( !bDone && !pFI->IsEof() && c )
182 c = pFI->GetFastChar();
183 if( c == '"' )
187 c = pFI->GetFastChar();
189 while( c == ' ' || c == '\t' );
191 if( c == '"' )
193 // this is a continued string
194 // note: multiline string continuations are handled by the parser
195 // see rscyacc.y
197 else
198 bDone = true;
200 else if( c == '\\' )
202 aBuf.append( '\\' );
203 c = pFI->GetFastChar();
204 if( c )
205 aBuf.append( sal_Char(c) );
207 else
208 aBuf.append( sal_Char(c) );
210 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
211 return STRING;
213 if (isdigit (c))
215 pTokenVal->value = GetNumber();
216 return NUMBER;
219 if( isalpha (c) || (c == '_') )
221 Atom nHashId;
222 OStringBuffer aBuf( 256 );
224 while( isalnum (c) || (c == '_') || (c == '-') )
226 aBuf.append( sal_Char(c) );
227 c = pFI->GetFastChar();
230 nHashId = pHS->getID( aBuf.getStr(), true );
231 if( InvalidAtom != nHashId )
233 KEY_STRUCT aKey;
235 // Suche nach dem Schluesselwort
236 if( pTC->aNmTb.Get( nHashId, &aKey ) )
239 // Schluesselwort gefunden
240 switch( aKey.nTyp )
242 case CLASSNAME:
243 pTokenVal->pClass = reinterpret_cast<RscTop *>(aKey.yylval);
244 break;
245 case VARNAME:
246 pTokenVal->varid = aKey.nName;
247 break;
248 case CONSTNAME:
249 pTokenVal->constname.hashid = aKey.nName;
250 pTokenVal->constname.nValue = aKey.yylval;
251 break;
252 case BOOLEAN:
253 pTokenVal->svbool = (bool)aKey.yylval;
254 break;
255 case INCLUDE:
256 bLastInclude = true;
257 //fall-through
258 default:
259 pTokenVal->value = aKey.yylval;
262 return aKey.nTyp;
264 else
266 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
267 return SYMBOL;
270 else
272 // Symbol
273 RscDefine * pDef;
275 pDef = pTC->aFileTab.FindDef( aBuf.getStr() );
276 if( pDef )
278 pTokenVal->defineele = pDef;
280 return RSCDEFINE;
283 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
284 return SYMBOL;
288 if( c=='<' )
290 c = pFI->GetFastChar();
291 if( c=='<' )
293 c = pFI->GetFastChar();
294 return LEFTSHIFT;
296 else
297 return '<';
300 if( c=='>' )
302 c = pFI->GetFastChar();
303 if( c=='>' )
305 c = pFI->GetFastChar();
306 return RIGHTSHIFT;
308 else
309 return '>';
312 c1 = c;
313 c = pFI->GetFastChar();
314 return c1;
317 int yylex()
319 if( bTargetDefined )
320 bTargetDefined = false;
321 else
322 aKeyVal[ 0 ].nKeyWord = MakeToken( &aKeyVal[ 0 ].aYYSType );
324 yylval = aKeyVal[ 0 ].aYYSType;
325 return aKeyVal[ 0 ].nKeyWord;
328 #if defined SOLARIS
329 extern "C" void yyerror( const char* pMessage )
330 #else
331 void yyerror( char* pMessage )
332 #endif
334 pTC->pEH->Error( ERR_YACC, NULL, RscId(), pMessage );
337 void InitParser( RscFileInst * pFileInst )
339 pTC = pFileInst->pTypCont; // Datenkontainer setzten
340 pFI = pFileInst;
341 pStringContainer = new StringContainer();
342 pExp = NULL; //fuer MacroParser
343 bTargetDefined = false;
345 // Anfangszeichen initialisieren
346 bLastInclude = false;
347 c = pFI->GetFastChar();
350 void EndParser()
352 // Stack abraeumen
353 while( ! S.IsEmpty() )
354 S.Pop();
356 // free string container
357 delete pStringContainer;
358 pStringContainer = NULL;
360 if( pExp )
361 delete pExp;
362 pTC = NULL;
363 pFI = NULL;
364 pExp = NULL;
368 void IncludeParser( RscFileInst * pFileInst )
370 int nToken; // Wert des Tokens
371 YYSTYPE aYYSType; // Daten des Tokens
372 RscFile * pFName; // Filestruktur
373 sal_uLong lKey; // Fileschluessel
374 RscTypCont * pTypCon = pFileInst->pTypCont;
376 pFName = pTypCon->aFileTab.Get( pFileInst->GetFileIndex() );
377 InitParser( pFileInst );
379 nToken = MakeToken( &aYYSType );
380 while( 0 != nToken && CLASSNAME != nToken )
382 if( '#' == nToken )
384 if( INCLUDE == (nToken = MakeToken( &aYYSType )) )
386 if( STRING == (nToken = MakeToken( &aYYSType )) )
388 lKey = pTypCon->aFileTab.NewIncFile( aYYSType.string,
389 aYYSType.string );
390 pFName->InsertDependFile( lKey, ULONG_MAX );
392 else if( INCLUDE_STRING == nToken )
394 lKey = pTypCon->aFileTab.NewIncFile( aYYSType.string,
395 OString() );
396 pFName->InsertDependFile( lKey, ULONG_MAX );
400 nToken = MakeToken( &aYYSType );
403 EndParser();
406 ERRTYPE parser( RscFileInst * pFileInst )
408 ERRTYPE aError;
410 InitParser( pFileInst );
412 aError = yyparse();
414 EndParser();
416 // yyparser gibt 0 zurueck, wenn erfolgreich
417 if( 0 == aError )
418 aError.Clear();
419 if( pFileInst->pTypCont->pEH->nErrors )
420 aError = ERR_ERROR;
421 pFileInst->SetError( aError );
422 return aError;
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */