merge the formfield patch from ooo-build
[ooovba.git] / rsc / source / parser / rsclex.cxx
bloba375ce06f2ada04a3b1d16964d0c037d6683be2e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rsclex.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_rsc.hxx"
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <limits.h>
39 #ifdef _RSCERROR_H
40 #include <rscerror.h>
41 #endif
42 #include <rschash.hxx>
43 #include <rscdb.hxx>
44 #include <rsctop.hxx>
45 #include <rsckey.hxx>
46 #include <rscpar.hxx>
47 #include <rscdef.hxx>
49 #include "rsclex.hxx"
50 #include <yyrscyacc.hxx>
52 #include <rtl/textcvt.h>
53 #include <rtl/textenc.h>
55 using namespace rtl;
57 const char* StringContainer::putString( const char* pString )
59 OString aString( static_cast<const sal_Char*>(pString) );
60 std::pair<
61 std::hash_set< OString, OStringHash >::iterator,
62 bool > aInsert =
63 m_aStrings.insert( aString );
65 return aInsert.first->getStr();
68 /*************************************************************************/
69 int c;
70 BOOL bLastInclude;// War letztes Symbol INCLUDE
71 RscFileInst* pFI;
72 RscTypCont* pTC;
73 RscExpression * pExp;
74 struct KeyVal {
75 int nKeyWord;
76 YYSTYPE aYYSType;
77 } aKeyVal[ 1 ];
78 BOOL bTargetDefined;
80 StringContainer* pStringContainer = NULL;
83 /****************** C O D E **********************************************/
84 UINT32 GetNumber(){
85 UINT32 l = 0;
86 UINT32 nLog = 10;
88 if( '0' == c ){
89 c = pFI->GetFastChar();
90 if( 'x' == c ){
91 nLog = 16;
92 c = pFI->GetFastChar();
96 if( nLog == 16 ){
97 while( isxdigit( c ) ){
98 if( isdigit( c ) )
99 l = l * nLog + (c - '0');
100 else
101 l = l * nLog + (toupper( c ) - 'A' + 10 );
102 c = pFI->GetFastChar();
105 else{
106 while( isdigit( c ) || 'x' == c ){
107 l = l * nLog + (c - '0');
108 c = pFI->GetFastChar();
112 while( c=='U' || c=='u' || c=='l' || c=='L' ) //Wg. Unsigned Longs
113 c = pFI->GetFastChar();
115 if( l > 0x7fffffff ) //Oberstes bit gegebenenfalls abschneiden;
116 l &= 0x7fffffff;
118 return( l );
121 int MakeToken( YYSTYPE * pTokenVal ){
122 int c1;
123 char * pStr;
125 while( TRUE ){ // Kommentare und Leerzeichen ueberlesen
126 while( isspace( c ) )
127 c = pFI->GetFastChar();
128 if( '/' == c ){
129 c1 = c;
130 c = pFI->GetFastChar();
131 if( '/' == c ){
132 while( '\n' != c && !pFI->IsEof() )
133 c = pFI->GetFastChar();
134 c = pFI->GetFastChar();
136 else if( '*' == c ){
137 c = pFI->GetFastChar();
138 do {
139 while( '*' != c && !pFI->IsEof() )
140 c = pFI->GetFastChar();
141 c = pFI->GetFastChar();
142 } while( '/' != c && !pFI->IsEof() );
143 c = pFI->GetFastChar();
145 else
146 return( c1 );
148 else
149 break;
152 if( c == pFI->IsEof() ){
153 return( 0 );
156 if( bLastInclude ){
157 bLastInclude = FALSE; //Zuruecksetzten
158 if( '<' == c ){
159 OStringBuffer aBuf( 256 );
160 c = pFI->GetFastChar();
161 while( '>' != c && !pFI->IsEof() )
163 aBuf.append( sal_Char(c) );
164 c = pFI->GetFastChar();
166 c = pFI->GetFastChar();
167 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
168 return( INCLUDE_STRING );
172 if( c == '"' )
174 OStringBuffer aBuf( 256 );
175 BOOL bDone = FALSE;
176 while( !bDone && !pFI->IsEof() && c )
178 c = pFI->GetFastChar();
179 if( c == '"' )
181 c = pFI->GetFastChar();
182 if( c == '"' )
184 aBuf.append( '"' );
185 aBuf.append( '"' );
187 else
188 bDone = TRUE;
190 else if( c == '\\' )
192 aBuf.append( '\\' );
193 c = pFI->GetFastChar();
194 if( c )
195 aBuf.append( sal_Char(c) );
197 else
198 aBuf.append( sal_Char(c) );
200 pStr = pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
201 return( STRING );
203 if (isdigit (c)){
204 pTokenVal->value = GetNumber();
205 return( NUMBER );
208 if( isalpha (c) || (c == '_') ){
209 Atom nHashId;
210 OStringBuffer aBuf( 256 );
212 while( isalnum (c) || (c == '_') || (c == '-') )
214 aBuf.append( sal_Char(c) );
215 c = pFI->GetFastChar();
218 nHashId = pHS->getID( aBuf.getStr(), true );
219 if( InvalidAtom != nHashId )
221 KEY_STRUCT aKey;
223 // Suche nach dem Schluesselwort
224 if( pTC->aNmTb.Get( nHashId, &aKey ) )
227 // Schluesselwort gefunden
228 switch( aKey.nTyp )
230 case CLASSNAME:
231 pTokenVal->pClass = (RscTop *)aKey.yylval;
232 break;
233 case VARNAME:
234 pTokenVal->varid = aKey.nName;
235 break;
236 case CONSTNAME:
237 pTokenVal->constname.hashid = aKey.nName;
238 pTokenVal->constname.nValue = aKey.yylval;
239 break;
240 case BOOLEAN:
241 pTokenVal->svbool = (BOOL)aKey.yylval;
242 break;
243 case INCLUDE:
244 bLastInclude = TRUE;
245 default:
246 pTokenVal->value = aKey.yylval;
249 return( aKey.nTyp );
251 else
253 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
254 return( SYMBOL );
257 else{ // Symbol
258 RscDefine * pDef;
260 pDef = pTC->aFileTab.FindDef( aBuf.getStr() );
261 if( pDef ){
262 pTokenVal->defineele = pDef;
264 return( RSCDEFINE );
267 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
268 return( SYMBOL );
272 if( c=='<' )
274 c = pFI->GetFastChar();
275 if( c=='<' )
277 c = pFI->GetFastChar();
278 return LEFTSHIFT;
280 else
281 return '<';
284 if( c=='>' )
286 c = pFI->GetFastChar();
287 if( c=='>' )
289 c = pFI->GetFastChar();
290 return RIGHTSHIFT;
292 else
293 return '>';
296 c1 = c;
297 c = pFI->GetFastChar();
298 return( c1 );
301 #if defined( RS6000 ) || defined( HP9000 ) || defined( SCO )
302 extern "C" int yylex()
303 #else
304 int yylex()
305 #endif
307 if( bTargetDefined )
308 bTargetDefined = FALSE;
309 else
310 aKeyVal[ 0 ].nKeyWord =
311 MakeToken( &aKeyVal[ 0 ].aYYSType );
313 yylval = aKeyVal[ 0 ].aYYSType;
314 return( aKeyVal[ 0 ].nKeyWord );
317 /****************** yyerror **********************************************/
318 #ifdef RS6000
319 extern "C" void yyerror( char* pMessage )
320 #elif defined HP9000 || defined SCO || defined IRIX || defined SOLARIS
321 extern "C" void yyerror( const char* pMessage )
322 #else
323 void yyerror( char* pMessage )
324 #endif
326 pTC->pEH->Error( ERR_YACC, NULL, RscId(), pMessage );
329 /****************** parser start function ********************************/
330 void InitParser( RscFileInst * pFileInst )
332 pTC = pFileInst->pTypCont; // Datenkontainer setzten
333 pFI = pFileInst;
334 pStringContainer = new StringContainer();
335 pExp = NULL; //fuer MacroParser
336 bTargetDefined = FALSE;
338 // Anfangszeichen initialisieren
339 bLastInclude = FALSE;
340 c = pFI->GetFastChar();
343 void EndParser(){
344 // Stack abraeumen
345 while( ! S.IsEmpty() )
346 S.Pop();
348 // free string container
349 delete pStringContainer;
350 pStringContainer = NULL;
352 if( pExp )
353 delete pExp;
354 pTC = NULL;
355 pFI = NULL;
356 pExp = NULL;
360 void IncludeParser( RscFileInst * pFileInst )
362 int nToken; // Wert des Tokens
363 YYSTYPE aYYSType; // Daten des Tokens
364 RscFile * pFName; // Filestruktur
365 ULONG lKey; // Fileschluessel
366 RscTypCont * pTypCon = pFileInst->pTypCont;
368 pFName = pTypCon->aFileTab.Get( pFileInst->GetFileIndex() );
369 InitParser( pFileInst );
371 nToken = MakeToken( &aYYSType );
372 while( 0 != nToken && CLASSNAME != nToken ){
373 if( '#' == nToken ){
374 if( INCLUDE == (nToken = MakeToken( &aYYSType )) ){
375 if( STRING == (nToken = MakeToken( &aYYSType )) ){
376 lKey = pTypCon->aFileTab.NewIncFile( aYYSType.string,
377 aYYSType.string );
378 pFName->InsertDependFile( lKey, LIST_APPEND );
380 else if( INCLUDE_STRING == nToken ){
381 lKey = pTypCon->aFileTab.NewIncFile( aYYSType.string,
382 ByteString() );
383 pFName->InsertDependFile( lKey, LIST_APPEND );
387 nToken = MakeToken( &aYYSType );
390 EndParser();
393 ERRTYPE parser( RscFileInst * pFileInst )
395 ERRTYPE aError;
397 InitParser( pFileInst );
399 aError = yyparse();
401 EndParser();
403 // yyparser gibt 0 zurueck, wenn erfolgreich
404 if( 0 == aError )
405 aError.Clear();
406 if( pFileInst->pTypCont->pEH->nErrors )
407 aError = ERR_ERROR;
408 pFileInst->SetError( aError );
409 return( aError );
412 RscExpression * MacroParser( RscFileInst & rFileInst )
414 ERRTYPE aError;
415 RscExpression * pExpression;
417 InitParser( &rFileInst );
419 //Ziel auf macro_expression setzen
420 aKeyVal[ 0 ].nKeyWord = MACROTARGET;
421 bTargetDefined = TRUE;
422 aError = yyparse();
424 pExpression = pExp;
425 //EndParser() wuerde pExp loeschen
426 if( pExp )
427 pExp = NULL;
429 EndParser();
431 // yyparser gibt 0 zurueck, wenn erfolgreich
432 if( 0 == aError )
433 aError.Clear();
434 if( rFileInst.pTypCont->pEH->nErrors )
435 aError = ERR_ERROR;
436 rFileInst.SetError( aError );
438 //im Fehlerfall pExpression loeschen
439 if( aError.IsError() && pExpression ){
440 delete pExpression;
441 pExpression = NULL;
443 return( pExpression );