update credits
[LibreOffice.git] / rsc / source / parser / rsclex.cxx
blob3f795ea7eba7d8092c1da3bb111b3e57081ea11c
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 #ifdef _RSCERROR_H
27 #include <rscerror.h>
28 #endif
29 #include <rschash.hxx>
30 #include <rscdb.hxx>
31 #include <rsctop.hxx>
32 #include <rsckey.hxx>
33 #include <rscpar.hxx>
34 #include <rscdef.hxx>
36 #include <rsclex.hxx>
37 #include <rscyacc.hxx>
39 #include <rtl/textcvt.h>
40 #include <rtl/textenc.h>
43 const char* StringContainer::putString( const char* pString )
45 OString aString( static_cast<const sal_Char*>(pString) );
46 std::pair<
47 boost::unordered_set< OString, OStringHash >::iterator,
48 bool > aInsert =
49 m_aStrings.insert( aString );
51 return aInsert.first->getStr();
54 int c;
55 sal_Bool bLastInclude;// War letztes Symbol INCLUDE
56 RscFileInst* pFI;
57 RscTypCont* pTC;
58 RscExpression * pExp;
59 struct KeyVal {
60 int nKeyWord;
61 YYSTYPE aYYSType;
62 } aKeyVal[ 1 ];
63 sal_Bool bTargetDefined;
65 StringContainer* pStringContainer = NULL;
68 sal_uInt32 GetNumber(){
69 sal_uInt32 l = 0;
70 sal_uInt32 nLog = 10;
72 if( '0' == c ){
73 c = pFI->GetFastChar();
74 if( 'x' == c ){
75 nLog = 16;
76 c = pFI->GetFastChar();
80 if( nLog == 16 ){
81 while( isxdigit( c ) ){
82 if( isdigit( c ) )
83 l = l * nLog + (c - '0');
84 else
85 l = l * nLog + (toupper( c ) - 'A' + 10 );
86 c = pFI->GetFastChar();
89 else{
90 while( isdigit( c ) || 'x' == c ){
91 l = l * nLog + (c - '0');
92 c = pFI->GetFastChar();
96 while( c=='U' || c=='u' || c=='l' || c=='L' ) //Wg. Unsigned Longs
97 c = pFI->GetFastChar();
99 if( l > 0x7fffffff ) //Oberstes bit gegebenenfalls abschneiden;
100 l &= 0x7fffffff;
102 return( l );
105 int MakeToken( YYSTYPE * pTokenVal ){
106 int c1;
108 while( true ){ // Kommentare und Leerzeichen ueberlesen
109 while( isspace( c ) )
110 c = pFI->GetFastChar();
111 if( '/' == c ){
112 c1 = c;
113 c = pFI->GetFastChar();
114 if( '/' == c ){
115 while( '\n' != c && !pFI->IsEof() )
116 c = pFI->GetFastChar();
117 c = pFI->GetFastChar();
119 else if( '*' == c ){
120 c = pFI->GetFastChar();
121 do {
122 while( '*' != c && !pFI->IsEof() )
123 c = pFI->GetFastChar();
124 c = pFI->GetFastChar();
125 } while( '/' != c && !pFI->IsEof() );
126 c = pFI->GetFastChar();
128 else
129 return( c1 );
131 else
132 break;
135 if( c == pFI->IsEof() ){
136 return( 0 );
139 if( bLastInclude ){
140 bLastInclude = sal_False; //Zuruecksetzten
141 if( '<' == c ){
142 OStringBuffer aBuf( 256 );
143 c = pFI->GetFastChar();
144 while( '>' != c && !pFI->IsEof() )
146 aBuf.append( sal_Char(c) );
147 c = pFI->GetFastChar();
149 c = pFI->GetFastChar();
150 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
151 return( INCLUDE_STRING );
155 if( c == '"' )
157 OStringBuffer aBuf( 256 );
158 sal_Bool bDone = sal_False;
159 while( !bDone && !pFI->IsEof() && c )
161 c = pFI->GetFastChar();
162 if( c == '"' )
166 c = pFI->GetFastChar();
168 while( c == ' ' || c == '\t' );
169 if( c == '"' )
171 // this is a continued string
172 // note: multiline string continuations are handled by the parser
173 // see rscyacc.y
175 else
176 bDone = sal_True;
178 else if( c == '\\' )
180 aBuf.append( '\\' );
181 c = pFI->GetFastChar();
182 if( c )
183 aBuf.append( sal_Char(c) );
185 else
186 aBuf.append( sal_Char(c) );
188 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
189 return( STRING );
191 if (isdigit (c)){
192 pTokenVal->value = GetNumber();
193 return( NUMBER );
196 if( isalpha (c) || (c == '_') ){
197 Atom nHashId;
198 OStringBuffer aBuf( 256 );
200 while( isalnum (c) || (c == '_') || (c == '-') )
202 aBuf.append( sal_Char(c) );
203 c = pFI->GetFastChar();
206 nHashId = pHS->getID( aBuf.getStr(), true );
207 if( InvalidAtom != nHashId )
209 KEY_STRUCT aKey;
211 // Suche nach dem Schluesselwort
212 if( pTC->aNmTb.Get( nHashId, &aKey ) )
215 // Schluesselwort gefunden
216 switch( aKey.nTyp )
218 case CLASSNAME:
219 pTokenVal->pClass = (RscTop *)aKey.yylval;
220 break;
221 case VARNAME:
222 pTokenVal->varid = aKey.nName;
223 break;
224 case CONSTNAME:
225 pTokenVal->constname.hashid = aKey.nName;
226 pTokenVal->constname.nValue = aKey.yylval;
227 break;
228 case BOOLEAN:
229 pTokenVal->svbool = (sal_Bool)aKey.yylval;
230 break;
231 case INCLUDE:
232 bLastInclude = sal_True;
233 default:
234 pTokenVal->value = aKey.yylval;
237 return( aKey.nTyp );
239 else
241 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
242 return( SYMBOL );
245 else{ // Symbol
246 RscDefine * pDef;
248 pDef = pTC->aFileTab.FindDef( aBuf.getStr() );
249 if( pDef ){
250 pTokenVal->defineele = pDef;
252 return( RSCDEFINE );
255 pTokenVal->string = const_cast<char*>(pStringContainer->putString( aBuf.getStr() ));
256 return( SYMBOL );
260 if( c=='<' )
262 c = pFI->GetFastChar();
263 if( c=='<' )
265 c = pFI->GetFastChar();
266 return LEFTSHIFT;
268 else
269 return '<';
272 if( c=='>' )
274 c = pFI->GetFastChar();
275 if( c=='>' )
277 c = pFI->GetFastChar();
278 return RIGHTSHIFT;
280 else
281 return '>';
284 c1 = c;
285 c = pFI->GetFastChar();
286 return( c1 );
289 #if defined( RS6000 )
290 extern "C" int yylex()
291 #else
292 int yylex()
293 #endif
295 if( bTargetDefined )
296 bTargetDefined = sal_False;
297 else
298 aKeyVal[ 0 ].nKeyWord =
299 MakeToken( &aKeyVal[ 0 ].aYYSType );
301 yylval = aKeyVal[ 0 ].aYYSType;
302 return( aKeyVal[ 0 ].nKeyWord );
305 #ifdef RS6000
306 extern "C" void yyerror( char* pMessage )
307 #elif defined SOLARIS
308 extern "C" void yyerror( const char* pMessage )
309 #else
310 void yyerror( char* pMessage )
311 #endif
313 pTC->pEH->Error( ERR_YACC, NULL, RscId(), pMessage );
316 void InitParser( RscFileInst * pFileInst )
318 pTC = pFileInst->pTypCont; // Datenkontainer setzten
319 pFI = pFileInst;
320 pStringContainer = new StringContainer();
321 pExp = NULL; //fuer MacroParser
322 bTargetDefined = sal_False;
324 // Anfangszeichen initialisieren
325 bLastInclude = sal_False;
326 c = pFI->GetFastChar();
329 void EndParser(){
330 // Stack abraeumen
331 while( ! S.IsEmpty() )
332 S.Pop();
334 // free string container
335 delete pStringContainer;
336 pStringContainer = NULL;
338 if( pExp )
339 delete pExp;
340 pTC = NULL;
341 pFI = NULL;
342 pExp = NULL;
346 void IncludeParser( RscFileInst * pFileInst )
348 int nToken; // Wert des Tokens
349 YYSTYPE aYYSType; // Daten des Tokens
350 RscFile * pFName; // Filestruktur
351 sal_uLong lKey; // Fileschluessel
352 RscTypCont * pTypCon = pFileInst->pTypCont;
354 pFName = pTypCon->aFileTab.Get( pFileInst->GetFileIndex() );
355 InitParser( pFileInst );
357 nToken = MakeToken( &aYYSType );
358 while( 0 != nToken && CLASSNAME != nToken ){
359 if( '#' == nToken ){
360 if( INCLUDE == (nToken = MakeToken( &aYYSType )) ){
361 if( STRING == (nToken = MakeToken( &aYYSType )) ){
362 lKey = pTypCon->aFileTab.NewIncFile( aYYSType.string,
363 aYYSType.string );
364 pFName->InsertDependFile( lKey, ULONG_MAX );
366 else if( INCLUDE_STRING == nToken ){
367 lKey = pTypCon->aFileTab.NewIncFile( aYYSType.string,
368 OString() );
369 pFName->InsertDependFile( lKey, ULONG_MAX );
373 nToken = MakeToken( &aYYSType );
376 EndParser();
379 ERRTYPE parser( RscFileInst * pFileInst )
381 ERRTYPE aError;
383 InitParser( pFileInst );
385 aError = yyparse();
387 EndParser();
389 // yyparser gibt 0 zurueck, wenn erfolgreich
390 if( 0 == aError )
391 aError.Clear();
392 if( pFileInst->pTypCont->pEH->nErrors )
393 aError = ERR_ERROR;
394 pFileInst->SetError( aError );
395 return( aError );
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */