1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_basic.hxx"
24
25 #include "sbcomp.hxx"
26 #include <com/sun/star/script/ModuleType.hpp>
27
28 struct SbiParseStack { // "Stack" für Statement-Blocks
29 SbiParseStack* pNext; // Chain
30 SbiExprNode* pWithVar; // Variable für WITH
31 SbiToken eExitTok; // Exit-Token
32 sal_uInt32 nChain; // JUMP-Chain
33 };
34
35 struct SbiStatement {
36 SbiToken eTok;
37 void( SbiParser::*Func )(); // Verarbeitungsroutine
38 sal_Bool bMain; // sal_True: ausserhalb SUBs OK
39 sal_Bool bSubr; // sal_True: in SUBs OK
40 };
41
42 #define Y sal_True
43 #define N sal_False
44
45 static SbiStatement StmntTable [] = {
46 { CALL, &SbiParser::Call, N, Y, }, // CALL
47 { CLOSE, &SbiParser::Close, N, Y, }, // CLOSE
48 { _CONST_, &SbiParser::Dim, Y, Y, }, // CONST
49 { DECLARE, &SbiParser::Declare, Y, N, }, // DECLARE
50 { DEFBOOL, &SbiParser::DefXXX, Y, N, }, // DEFBOOL
51 { DEFCUR, &SbiParser::DefXXX, Y, N, }, // DEFCUR
52 { DEFDATE, &SbiParser::DefXXX, Y, N, }, // DEFDATE
53 { DEFDBL, &SbiParser::DefXXX, Y, N, }, // DEFDBL
54 { DEFERR, &SbiParser::DefXXX, Y, N, }, // DEFERR
55 { DEFINT, &SbiParser::DefXXX, Y, N, }, // DEFINT
56 { DEFLNG, &SbiParser::DefXXX, Y, N, }, // DEFLNG
57 { DEFOBJ, &SbiParser::DefXXX, Y, N, }, // DEFOBJ
58 { DEFSNG, &SbiParser::DefXXX, Y, N, }, // DEFSNG
59 { DEFSTR, &SbiParser::DefXXX, Y, N, }, // DEFSTR
60 { DEFVAR, &SbiParser::DefXXX, Y, N, }, // DEFVAR
61 { DIM, &SbiParser::Dim, Y, Y, }, // DIM
62 { DO, &SbiParser::DoLoop, N, Y, }, // DO
63 { ELSE, &SbiParser::NoIf, N, Y, }, // ELSE
64 { ELSEIF, &SbiParser::NoIf, N, Y, }, // ELSEIF
65 { ENDIF, &SbiParser::NoIf, N, Y, }, // ENDIF
66 { END, &SbiParser::Stop, N, Y, }, // END
67 { ENUM, &SbiParser::Enum, Y, N, }, // TYPE
68 { ERASE, &SbiParser::Erase, N, Y, }, // ERASE
69 { _ERROR_, &SbiParser::ErrorStmnt, N, Y, }, // ERROR
70 { EXIT, &SbiParser::Exit, N, Y, }, // EXIT
71 { FOR, &SbiParser::For, N, Y, }, // FOR
72 { FUNCTION, &SbiParser::SubFunc, Y, N, }, // FUNCTION
73 { GOSUB, &SbiParser::Goto, N, Y, }, // GOSUB
74 { GLOBAL, &SbiParser::Dim, Y, N, }, // GLOBAL
75 { GOTO, &SbiParser::Goto, N, Y, }, // GOTO
76 { IF, &SbiParser::If, N, Y, }, // IF
77 { IMPLEMENTS, &SbiParser::Implements, Y, N, }, // IMPLEMENTS
78 { INPUT, &SbiParser::Input, N, Y, }, // INPUT
79 { LET, &SbiParser::Assign, N, Y, }, // LET
80 { LINE, &SbiParser::Line, N, Y, }, // LINE, -> LINE INPUT (#i92642)
81 { LINEINPUT,&SbiParser::LineInput, N, Y, }, // LINE INPUT
82 { LOOP, &SbiParser::BadBlock, N, Y, }, // LOOP
83 { LSET, &SbiParser::LSet, N, Y, }, // LSET
84 { NAME, &SbiParser::Name, N, Y, }, // NAME
85 { NEXT, &SbiParser::BadBlock, N, Y, }, // NEXT
86 { ON, &SbiParser::On, N, Y, }, // ON
87 { OPEN, &SbiParser::Open, N, Y, }, // OPEN
88 { OPTION, &SbiParser::Option, Y, N, }, // OPTION
89 { PRINT, &SbiParser::Print, N, Y, }, // PRINT
90 { PRIVATE, &SbiParser::Dim, Y, N, }, // PRIVATE
91 { PROPERTY, &SbiParser::SubFunc, Y, N, }, // FUNCTION
92 { PUBLIC, &SbiParser::Dim, Y, N, }, // PUBLIC
93 { REDIM, &SbiParser::ReDim, N, Y, }, // DIM
94 { RESUME, &SbiParser::Resume, N, Y, }, // RESUME
95 { RETURN, &SbiParser::Return, N, Y, }, // RETURN
96 { RSET, &SbiParser::RSet, N, Y, }, // RSET
97 { SELECT, &SbiParser::Select, N, Y, }, // SELECT
98 { SET, &SbiParser::Set, N, Y, }, // SET
99 { STATIC, &SbiParser::Static, Y, Y, }, // STATIC
100 { STOP, &SbiParser::Stop, N, Y, }, // STOP
101 { SUB, &SbiParser::SubFunc, Y, N, }, // SUB
102 { TYPE, &SbiParser::Type, Y, N, }, // TYPE
103 { UNTIL, &SbiParser::BadBlock, N, Y, }, // UNTIL
104 { WHILE, &SbiParser::While, N, Y, }, // WHILE
105 { WEND, &SbiParser::BadBlock, N, Y, }, // WEND
106 { WITH, &SbiParser::With, N, Y, }, // WITH
107 { WRITE, &SbiParser::Write, N, Y, }, // WRITE
108 { PP_IF, &SbiParser::PPIf, Y, Y, }, // #IF
109 { PP_ELSEIF, &SbiParser::PPIf, Y, Y, }, // #ELSEIF
110 { PP_ELSE, &SbiParser::NoIf, Y, Y, }, // #ELSE
111 { PP_ENDIF, &SbiParser::NoIf, Y, Y, }, // #ENDIF
112 { PP_CONST, &SbiParser::PPConst, Y, Y, }, // #CONST
113
114 { NIL, NULL, N, N }
115 };
116
117
118 #ifdef _MSC_VER
119 // 'this' : used in base member initializer list
120 #pragma warning( disable: 4355 )
121 #endif
122
SbiParser(StarBASIC * pb,SbModule * pm)123 SbiParser::SbiParser( StarBASIC* pb, SbModule* pm )
124 : SbiTokenizer( pm->GetSource32(), pb ),
125 aGblStrings( this ),
126 aLclStrings( this ),
127 aGlobals( aGblStrings, SbGLOBAL ),
128 aPublics( aGblStrings, SbPUBLIC ),
129 aRtlSyms( aGblStrings, SbRTL ),
130 aGen( *pm, this, 1024 )
131 {
132 pBasic = pb;
133 eCurExpr = SbSYMBOL;
134 eEndTok = NIL;
135 pProc = NULL;
136 pStack = NULL;
137 pWithVar = NULL;
138 nBase = 0;
139 bText =
140 bGblDefs =
141 bNewGblDefs =
142 bSingleLineIf =
143 bExplicit = sal_False;
144 bClassModule = ( pm->GetModuleType() == com::sun::star::script::ModuleType::CLASS );
145 OSL_TRACE("Parser - %s, bClassModule %d", rtl::OUStringToOString( pm->GetName(), RTL_TEXTENCODING_UTF8 ).getStr(), bClassModule );
146 pPool = &aPublics;
147 for( short i = 0; i < 26; i++ )
148 eDefTypes[ i ] = SbxVARIANT; // Kein expliziter Defaulttyp
149
150 aPublics.SetParent( &aGlobals );
151 aGlobals.SetParent( &aRtlSyms );
152
153 // Die globale Chainkette faengt bei Adresse 0 an:
154 nGblChain = aGen.Gen( _JUMP, 0 );
155
156 rTypeArray = new SbxArray; // Array fuer Benutzerdefinierte Typen
157 rEnumArray = new SbxArray; // Array for Enum types
158 bVBASupportOn = pm->IsVBACompat();
159 if ( bVBASupportOn )
160 EnableCompatibility();
161
162 }
163
164
165 // Ist Teil der Runtime-Library?
CheckRTLForSym(const String & rSym,SbxDataType eType)166 SbiSymDef* SbiParser::CheckRTLForSym( const String& rSym, SbxDataType eType )
167 {
168 SbxVariable* pVar = GetBasic()->GetRtl()->Find( rSym, SbxCLASS_DONTCARE );
169 SbiSymDef* pDef = NULL;
170 if( pVar )
171 {
172 if( pVar->IsA( TYPE(SbxMethod) ) )
173 {
174 SbiProcDef* pProc_ = aRtlSyms.AddProc( rSym );
175 SbxMethod* pMethod = (SbxMethod*) pVar;
176 if ( pMethod && pMethod->IsRuntimeFunction() )
177 {
178 pProc_->SetType( pMethod->GetRuntimeFunctionReturnType() );
179 }
180 else
181 {
182 pProc_->SetType( pVar->GetType() );
183 }
184 pDef = pProc_;
185 }
186 else
187 {
188 pDef = aRtlSyms.AddSym( rSym );
189 pDef->SetType( eType );
190 }
191 }
192 return pDef;
193 }
194
195 // Globale Chainkette schliessen
196
HasGlobalCode()197 sal_Bool SbiParser::HasGlobalCode()
198 {
199 if( bGblDefs && nGblChain )
200 {
201 aGen.BackChain( nGblChain );
202 aGen.Gen( _LEAVE );
203 // aGen.Gen( _STOP );
204 nGblChain = 0;
205 }
206 return bGblDefs;
207 }
208
OpenBlock(SbiToken eTok,SbiExprNode * pVar)209 void SbiParser::OpenBlock( SbiToken eTok, SbiExprNode* pVar )
210 {
211 SbiParseStack* p = new SbiParseStack;
212 p->eExitTok = eTok;
213 p->nChain = 0;
214 p->pWithVar = pWithVar;
215 p->pNext = pStack;
216 pStack = p;
217 pWithVar = pVar;
218
219 // #29955 for-Schleifen-Ebene pflegen
220 if( eTok == FOR )
221 aGen.IncForLevel();
222 }
223
CloseBlock()224 void SbiParser::CloseBlock()
225 {
226 if( pStack )
227 {
228 SbiParseStack* p = pStack;
229
230 // #29955 for-Schleifen-Ebene pflegen
231 if( p->eExitTok == FOR )
232 aGen.DecForLevel();
233
234 aGen.BackChain( p->nChain );
235 pStack = p->pNext;
236 pWithVar = p->pWithVar;
237 delete p;
238 }
239 }
240
241 // EXIT ...
242
Exit()243 void SbiParser::Exit()
244 {
245 SbiToken eTok = Next();
246 for( SbiParseStack* p = pStack; p; p = p->pNext )
247 {
248 SbiToken eExitTok = p->eExitTok;
249 if( eTok == eExitTok ||
250 (eTok == PROPERTY && (eExitTok == GET || eExitTok == LET) ) ) // #i109051
251 {
252 p->nChain = aGen.Gen( _JUMP, p->nChain );
253 return;
254 }
255 }
256 if( pStack )
257 Error( SbERR_EXPECTED, pStack->eExitTok );
258 else
259 Error( SbERR_BAD_EXIT );
260 }
261
TestSymbol(sal_Bool bKwdOk)262 sal_Bool SbiParser::TestSymbol( sal_Bool bKwdOk )
263 {
264 Peek();
265 if( eCurTok == SYMBOL || ( bKwdOk && IsKwd( eCurTok ) ) )
266 {
267 Next(); return sal_True;
268 }
269 Error( SbERR_SYMBOL_EXPECTED );
270 return sal_False;
271 }
272
273 // Testen auf ein bestimmtes Token
274
TestToken(SbiToken t)275 sal_Bool SbiParser::TestToken( SbiToken t )
276 {
277 if( Peek() == t )
278 {
279 Next(); return sal_True;
280 }
281 else
282 {
283 Error( SbERR_EXPECTED, t );
284 return sal_False;
285 }
286 }
287
288 // Testen auf Komma oder EOLN
289
TestComma()290 sal_Bool SbiParser::TestComma()
291 {
292 SbiToken eTok = Peek();
293 if( IsEoln( eTok ) )
294 {
295 Next();
296 return sal_False;
297 }
298 else if( eTok != COMMA )
299 {
300 Error( SbERR_EXPECTED, COMMA );
301 return sal_False;
302 }
303 Next();
304 return sal_True;
305 }
306
307 // Testen, ob EOLN vorliegt
308
TestEoln()309 void SbiParser::TestEoln()
310 {
311 if( !IsEoln( Next() ) )
312 {
313 Error( SbERR_EXPECTED, EOLN );
314 while( !IsEoln( Next() ) ) {}
315 }
316 }
317
318 // Parsing eines Statement-Blocks
319 // Das Parsing läuft bis zum Ende-Token.
320
StmntBlock(SbiToken eEnd)321 void SbiParser::StmntBlock( SbiToken eEnd )
322 {
323 SbiToken xe = eEndTok;
324 eEndTok = eEnd;
325 while( !bAbort && Parse() ) {}
326 eEndTok = xe;
327 if( IsEof() )
328 {
329 Error( SbERR_BAD_BLOCK, eEnd );
330 bAbort = sal_True;
331 }
332 }
333
334 // Die Hauptroutine. Durch wiederholten Aufrufs dieser Routine wird
335 // die Quelle geparst. Returnwert sal_False bei Ende/Fehlern.
336
Parse()337 sal_Bool SbiParser::Parse()
338 {
339 if( bAbort ) return sal_False;
340
341 EnableErrors();
342
343 Peek();
344 // Dateiende?
345 if( IsEof() )
346 {
347 // AB #33133: Falls keine Sub angelegt wurde, muss hier
348 // der globale Chain abgeschlossen werden!
349 // AB #40689: Durch die neue static-Behandlung kann noch
350 // ein nGblChain vorhanden sein, daher vorher abfragen
351 if( bNewGblDefs && nGblChain == 0 )
352 nGblChain = aGen.Gen( _JUMP, 0 );
353 return sal_False;
354 }
355
356 // Leerstatement?
357 if( IsEoln( eCurTok ) )
358 {
359 Next(); return sal_True;
360 }
361
362 if( !bSingleLineIf && MayBeLabel( sal_True ) )
363 {
364 // Ist ein Label
365 if( !pProc )
366 Error( SbERR_NOT_IN_MAIN, aSym );
367 else
368 pProc->GetLabels().Define( aSym );
369 Next(); Peek();
370 // Leerstatement?
371 if( IsEoln( eCurTok ) )
372 {
373 Next(); return sal_True;
374 }
375 }
376
377 // Ende des Parsings?
378 if( eCurTok == eEndTok ||
379 ( bVBASupportOn && // #i109075
380 (eCurTok == ENDFUNC || eCurTok == ENDPROPERTY || eCurTok == ENDSUB) &&
381 (eEndTok == ENDFUNC || eEndTok == ENDPROPERTY || eEndTok == ENDSUB) ) )
382 {
383 Next();
384 if( eCurTok != NIL )
385 aGen.Statement();
386 return sal_False;
387 }
388
389 // Kommentar?
390 if( eCurTok == REM )
391 {
392 Next(); return sal_True;
393 }
394
395 // Kommt ein Symbol, ist es entweder eine Variable( LET )
396 // oder eine SUB-Prozedur( CALL ohne Klammern )
397 // DOT für Zuweisungen im WITH-Block: .A=5
398 if( eCurTok == SYMBOL || eCurTok == DOT )
399 {
400 if( !pProc )
401 Error( SbERR_EXPECTED, SUB );
402 else
403 {
404 // Damit Zeile & Spalte stimmen...
405 Next();
406 Push( eCurTok );
407 aGen.Statement();
408 Symbol();
409 }
410 }
411 else
412 {
413 Next();
414
415 // Hier folgen nun die Statement-Parser.
416
417 SbiStatement* p;
418 for( p = StmntTable; p->eTok != NIL; p++ )
419 if( p->eTok == eCurTok )
420 break;
421 if( p->eTok != NIL )
422 {
423 if( !pProc && !p->bMain )
424 Error( SbERR_NOT_IN_MAIN, eCurTok );
425 else if( pProc && !p->bSubr )
426 Error( SbERR_NOT_IN_SUBR, eCurTok );
427 else
428 {
429 // globalen Chain pflegen
430 // AB #41606/#40689: Durch die neue static-Behandlung kann noch
431 // ein nGblChain vorhanden sein, daher vorher abfragen
432 if( bNewGblDefs && nGblChain == 0 &&
433 ( eCurTok == SUB || eCurTok == FUNCTION || eCurTok == PROPERTY ) )
434 {
435 nGblChain = aGen.Gen( _JUMP, 0 );
436 bNewGblDefs = sal_False;
437 }
438 // Statement-Opcode bitte auch am Anfang einer Sub
439 if( ( p->bSubr && (eCurTok != STATIC || Peek() == SUB || Peek() == FUNCTION ) ) ||
440 eCurTok == SUB || eCurTok == FUNCTION )
441 aGen.Statement();
442 (this->*( p->Func ) )();
443 SbxError nSbxErr = SbxBase::GetError();
444 if( nSbxErr )
445 SbxBase::ResetError(), Error( (SbError)nSbxErr );
446 }
447 }
448 else
449 Error( SbERR_UNEXPECTED, eCurTok );
450 }
451
452 // Test auf Ende des Statements:
453 // Kann auch ein ELSE sein, da vor dem ELSE kein : stehen muss!
454
455 if( !IsEos() )
456 {
457 Peek();
458 if( !IsEos() && eCurTok != ELSE && eCurTok != PP_ENDIF && eCurTok != PP_ELSE && eCurTok != PP_ELSEIF )
459 {
460 // falls das Parsing abgebrochen wurde, bis zum ":" vorgehen:
461 Error( SbERR_UNEXPECTED, eCurTok );
462 while( !IsEos() ) Next();
463 }
464 }
465 // Der Parser bricht am Ende ab, das nächste Token ist noch nicht
466 // geholt!
467 return sal_True;
468 }
469
470 // Innerste With-Variable liefern
GetWithVar()471 SbiExprNode* SbiParser::GetWithVar()
472 {
473 if( pWithVar )
474 return pWithVar;
475
476 // Sonst im Stack suchen
477 SbiParseStack* p = pStack;
478 while( p )
479 {
480 // LoopVar kann zur Zeit nur für with sein
481 if( p->pWithVar )
482 return p->pWithVar;
483 p = p->pNext;
484 }
485 return NULL;
486 }
487
488
489 // Zuweisung oder Subroutine Call
490
Symbol(const KeywordSymbolInfo * pKeywordSymbolInfo)491 void SbiParser::Symbol( const KeywordSymbolInfo* pKeywordSymbolInfo )
492 {
493 SbiExprMode eMode = bVBASupportOn ? EXPRMODE_STANDALONE : EXPRMODE_STANDARD;
494 SbiExpression aVar( this, SbSYMBOL, eMode, pKeywordSymbolInfo );
495
496 bool bEQ = ( Peek() == EQ );
497 if( !bEQ && bVBASupportOn && aVar.IsBracket() )
498 Error( SbERR_EXPECTED, "=" );
499
500 RecursiveMode eRecMode = ( bEQ ? PREVENT_CALL : FORCE_CALL );
501 bool bSpecialMidHandling = false;
502 SbiSymDef* pDef = aVar.GetRealVar();
503 if( bEQ && pDef && pDef->GetScope() == SbRTL )
504 {
505 String aRtlName = pDef->GetName();
506 if( aRtlName.EqualsIgnoreCaseAscii("Mid") )
507 {
508 SbiExprNode* pExprNode = aVar.GetExprNode();
509 // SbiNodeType eNodeType;
510 if( pExprNode && pExprNode->GetNodeType() == SbxVARVAL )
511 {
512 SbiExprList* pPar = pExprNode->GetParameters();
513 short nParCount = pPar ? pPar->GetSize() : 0;
514 if( nParCount == 2 || nParCount == 3 )
515 {
516 if( nParCount == 2 )
517 pPar->addExpression( new SbiExpression( this, -1, SbxLONG ) );
518
519 TestToken( EQ );
520 pPar->addExpression( new SbiExpression( this ) );
521
522 bSpecialMidHandling = true;
523 }
524 }
525 }
526 }
527 aVar.Gen( eRecMode );
528 if( !bSpecialMidHandling )
529 {
530 if( !bEQ )
531 {
532 aGen.Gen( _GET );
533 }
534 else
535 {
536 // Dann muss es eine Zuweisung sein. Was anderes gibt es nicht!
537 if( !aVar.IsLvalue() )
538 Error( SbERR_LVALUE_EXPECTED );
539 TestToken( EQ );
540 SbiExpression aExpr( this );
541 aExpr.Gen();
542 SbiOpcode eOp = _PUT;
543 // SbiSymDef* pDef = aVar.GetRealVar();
544 if( pDef )
545 {
546 if( pDef->GetConstDef() )
547 Error( SbERR_DUPLICATE_DEF, pDef->GetName() );
548 if( pDef->GetType() == SbxOBJECT )
549 {
550 eOp = _SET;
551 if( pDef->GetTypeId() )
552 {
553 aGen.Gen( _SETCLASS, pDef->GetTypeId() );
554 return;
555 }
556 }
557 }
558 aGen.Gen( eOp );
559 }
560 }
561 }
562
563 // Zuweisungen
564
Assign()565 void SbiParser::Assign()
566 {
567 SbiExpression aLvalue( this, SbLVALUE );
568 TestToken( EQ );
569 SbiExpression aExpr( this );
570 aLvalue.Gen();
571 aExpr.Gen();
572 sal_uInt16 nLen = 0;
573 SbiSymDef* pDef = aLvalue.GetRealVar();
574 {
575 if( pDef->GetConstDef() )
576 Error( SbERR_DUPLICATE_DEF, pDef->GetName() );
577 nLen = aLvalue.GetRealVar()->GetLen();
578 }
579 if( nLen )
580 aGen.Gen( _PAD, nLen );
581 aGen.Gen( _PUT );
582 }
583
584 // Zuweisungen einer Objektvariablen
585
Set()586 void SbiParser::Set()
587 {
588 SbiExpression aLvalue( this, SbLVALUE );
589 SbxDataType eType = aLvalue.GetType();
590 if( eType != SbxOBJECT && eType != SbxEMPTY && eType != SbxVARIANT )
591 Error( SbERR_INVALID_OBJECT );
592 TestToken( EQ );
593 SbiSymDef* pDef = aLvalue.GetRealVar();
594 if( pDef && pDef->GetConstDef() )
595 Error( SbERR_DUPLICATE_DEF, pDef->GetName() );
596
597 SbiToken eTok = Peek();
598 if( eTok == NEW )
599 {
600 Next();
601 String aStr;
602 SbiSymDef* pTypeDef = new SbiSymDef( aStr );
603 TypeDecl( *pTypeDef, sal_True );
604
605 aLvalue.Gen();
606 // aGen.Gen( _CLASS, pDef->GetTypeId() | 0x8000 );
607 aGen.Gen( _CREATE, pDef->GetId(), pTypeDef->GetTypeId() );
608 aGen.Gen( _SETCLASS, pDef->GetTypeId() );
609 }
610 else
611 {
612 SbiExpression aExpr( this );
613 aLvalue.Gen();
614 aExpr.Gen();
615 // It's a good idea to distinguish between
616 // set something = another &
617 // something = another
618 // ( it's necessary for vba objects where set is object
619 // specific and also doesn't involve processing default params )
620 if( pDef->GetTypeId() )
621 {
622 if ( bVBASupportOn )
623 aGen.Gen( _VBASETCLASS, pDef->GetTypeId() );
624 else
625 aGen.Gen( _SETCLASS, pDef->GetTypeId() );
626 }
627 else
628 {
629 if ( bVBASupportOn )
630 aGen.Gen( _VBASET );
631 else
632 aGen.Gen( _SET );
633 }
634 }
635 // aGen.Gen( _SET );
636 }
637
638 // JSM 07.10.95
LSet()639 void SbiParser::LSet()
640 {
641 SbiExpression aLvalue( this, SbLVALUE );
642 if( aLvalue.GetType() != SbxSTRING )
643 Error( SbERR_INVALID_OBJECT );
644 TestToken( EQ );
645 SbiSymDef* pDef = aLvalue.GetRealVar();
646 if( pDef && pDef->GetConstDef() )
647 Error( SbERR_DUPLICATE_DEF, pDef->GetName() );
648 SbiExpression aExpr( this );
649 aLvalue.Gen();
650 aExpr.Gen();
651 aGen.Gen( _LSET );
652 }
653
654 // JSM 07.10.95
RSet()655 void SbiParser::RSet()
656 {
657 SbiExpression aLvalue( this, SbLVALUE );
658 if( aLvalue.GetType() != SbxSTRING )
659 Error( SbERR_INVALID_OBJECT );
660 TestToken( EQ );
661 SbiSymDef* pDef = aLvalue.GetRealVar();
662 if( pDef && pDef->GetConstDef() )
663 Error( SbERR_DUPLICATE_DEF, pDef->GetName() );
664 SbiExpression aExpr( this );
665 aLvalue.Gen();
666 aExpr.Gen();
667 aGen.Gen( _RSET );
668 }
669
670 // DEFINT, DEFLNG, DEFSNG, DEFDBL, DEFSTR und so weiter
671
DefXXX()672 void SbiParser::DefXXX()
673 {
674 sal_Unicode ch1, ch2;
675 SbxDataType t = SbxDataType( eCurTok - DEFINT + SbxINTEGER );
676
677 while( !bAbort )
678 {
679 if( Next() != SYMBOL ) break;
680 ch1 = aSym.ToUpperAscii().GetBuffer()[0];
681 ch2 = 0;
682 if( Peek() == MINUS )
683 {
684 Next();
685 if( Next() != SYMBOL ) Error( SbERR_SYMBOL_EXPECTED );
686 else
687 {
688 ch2 = aSym.ToUpperAscii().GetBuffer()[0];
689 //ch2 = aSym.Upper();
690 if( ch2 < ch1 ) Error( SbERR_SYNTAX ), ch2 = 0;
691 }
692 }
693 if (!ch2) ch2 = ch1;
694 ch1 -= 'A'; ch2 -= 'A';
695 for (; ch1 <= ch2; ch1++) eDefTypes[ ch1 ] = t;
696 if( !TestComma() ) break;
697 }
698 }
699
700 // STOP/SYSTEM
701
Stop()702 void SbiParser::Stop()
703 {
704 aGen.Gen( _STOP );
705 Peek(); // #35694: Nur Peek(), damit EOL in Single-Line-If erkannt wird
706 }
707
708 // IMPLEMENTS
709
Implements()710 void SbiParser::Implements()
711 {
712 if( !bClassModule )
713 {
714 Error( SbERR_UNEXPECTED, IMPLEMENTS );
715 return;
716 }
717
718 Peek();
719 if( eCurTok != SYMBOL )
720 {
721 Error( SbERR_SYMBOL_EXPECTED );
722 return;
723 }
724
725 String aImplementedIface = aSym;
726 Next();
727 if( Peek() == DOT )
728 {
729 String aDotStr( '.' );
730 while( Peek() == DOT )
731 {
732 aImplementedIface += aDotStr;
733 Next();
734 SbiToken ePeekTok = Peek();
735 if( ePeekTok == SYMBOL || IsKwd( ePeekTok ) )
736 {
737 Next();
738 aImplementedIface += aSym;
739 }
740 else
741 {
742 Next();
743 Error( SbERR_SYMBOL_EXPECTED );
744 break;
745 }
746 }
747 }
748 aIfaceVector.push_back( aImplementedIface );
749 }
750
EnableCompatibility()751 void SbiParser::EnableCompatibility()
752 {
753 if( !bCompatible )
754 AddConstants();
755 bCompatible = sal_True;
756 }
757
758 // OPTION
759
Option()760 void SbiParser::Option()
761 {
762 switch( Next() )
763 {
764 case BASIC_EXPLICIT:
765 bExplicit = sal_True; break;
766 case BASE:
767 if( Next() == NUMBER )
768 {
769 if( nVal == 0 || nVal == 1 )
770 {
771 nBase = (short) nVal;
772 break;
773 }
774 }
775 Error( SbERR_EXPECTED, "0/1" );
776 break;
777 case PRIVATE:
778 {
779 String aString = SbiTokenizer::Symbol(Next());
780 if( !aString.EqualsIgnoreCaseAscii("Module") )
781 Error( SbERR_EXPECTED, "Module" );
782 break;
783 }
784 case COMPARE:
785 {
786 SbiToken eTok = Next();
787 if( eTok == BINARY )
788 bText = sal_False;
789 else if( eTok == SYMBOL && GetSym().EqualsIgnoreCaseAscii("text") )
790 bText = sal_True;
791 else
792 Error( SbERR_EXPECTED, "Text/Binary" );
793 break;
794 }
795 case COMPATIBLE:
796 EnableCompatibility();
797 break;
798
799 case CLASSMODULE:
800 bClassModule = sal_True;
801 aGen.GetModule().SetModuleType( com::sun::star::script::ModuleType::CLASS );
802 break;
803 case VBASUPPORT:
804 if( Next() == NUMBER )
805 {
806 if ( nVal == 1 || nVal == 0 )
807 {
808 bVBASupportOn = ( nVal == 1 );
809 if ( bVBASupportOn )
810 EnableCompatibility();
811 // if the module setting is different
812 // reset it to what the Option tells us
813 if ( bVBASupportOn != aGen.GetModule().IsVBACompat() )
814 aGen.GetModule().SetVBACompat( bVBASupportOn );
815 break;
816 }
817 }
818 Error( SbERR_EXPECTED, "0/1" );
819 break;
820 default:
821 Error( SbERR_BAD_OPTION, eCurTok );
822 }
823 }
824
addStringConst(SbiSymPool & rPool,const char * pSym,const String & rStr)825 void addStringConst( SbiSymPool& rPool, const char* pSym, const String& rStr )
826 {
827 SbiConstDef* pConst = new SbiConstDef( String::CreateFromAscii( pSym ) );
828 pConst->SetType( SbxSTRING );
829 pConst->Set( rStr );
830 rPool.Add( pConst );
831 }
832
addStringConst(SbiSymPool & rPool,const char * pSym,const char * pStr)833 inline void addStringConst( SbiSymPool& rPool, const char* pSym, const char* pStr )
834 {
835 addStringConst( rPool, pSym, String::CreateFromAscii( pStr ) );
836 }
837
AddConstants(void)838 void SbiParser::AddConstants( void )
839 {
840 // #113063 Create constant RTL symbols
841 addStringConst( aPublics, "vbCr", "\x0D" );
842 addStringConst( aPublics, "vbCrLf", "\x0D\x0A" );
843 addStringConst( aPublics, "vbFormFeed", "\x0C" );
844 addStringConst( aPublics, "vbLf", "\x0A" );
845 #if defined(UNX)
846 addStringConst( aPublics, "vbNewLine", "\x0A" );
847 #else
848 addStringConst( aPublics, "vbNewLine", "\x0D\x0A" );
849 #endif
850 addStringConst( aPublics, "vbNullString", "" );
851 addStringConst( aPublics, "vbTab", "\x09" );
852 addStringConst( aPublics, "vbVerticalTab", "\x0B" );
853
854 // Force length 1 and make char 0 afterwards
855 String aNullCharStr( String::CreateFromAscii( " " ) );
856 aNullCharStr.SetChar( 0, 0 );
857 addStringConst( aPublics, "vbNullChar", aNullCharStr );
858 }
859
860 // ERROR n
861
ErrorStmnt()862 void SbiParser::ErrorStmnt()
863 {
864 SbiExpression aPar( this );
865 aPar.Gen();
866 aGen.Gen( _ERROR );
867 }
868
869 // MS Excel VBA: #If conditional compilation directive.
870 // Supports #If, #ElseIf, #Else, #End If with simple expressions:
871 // True, False, identifier references, Not, And, Or, =, numbers.
PPIf()872 void SbiParser::PPIf()
873 {
874 Next();
875 bool bCondition = evaluatePPCondition();
876
877 if( eCurTok == THEN )
878 Next();
879
880 sal_uInt16 nNesting = 0;
881 SbiToken eTok = Peek();
882 while( !bAbort && !IsEof() &&
883 !( eTok == PP_ELSEIF || eTok == PP_ELSE || eTok == PP_ENDIF ) )
884 {
885 if( eCurTok == PP_IF )
886 nNesting++;
887 else if( eCurTok == PP_ENDIF )
888 {
889 if( nNesting > 0 )
890 nNesting--;
891 }
892
893 if( bCondition )
894 Parse();
895 else
896 skipStatement();
897
898 // After Parse/skipStatement, check if we hit a branch delimiter
899 if( nNesting == 0 &&
900 ( eCurTok == PP_ELSEIF || eCurTok == PP_ELSE || eCurTok == PP_ENDIF ) )
901 {
902 eTok = eCurTok;
903 if( ePush == NIL )
904 ePush = eCurTok;
905 break;
906 }
907 if( nNesting == 0 && eCurTok == PP_IF )
908 {
909 eTok = eCurTok;
910 if( ePush == NIL )
911 ePush = eCurTok;
912 break;
913 }
914 eTok = Peek();
915 }
916
917 // After loop, handle branch transitions
918 if( bCondition )
919 {
920 if( eTok == PP_ELSE )
921 {
922 Next();
923 skipUntilEndIf();
924 }
925 else if( eTok == PP_ELSEIF )
926 {
927 Next();
928 skipUntilEndIf();
929 }
930 else if( eTok == PP_ENDIF || eTok == PP_IF )
931 {
932 Next();
933 }
934 }
935 else
936 {
937 if( eTok == PP_ELSEIF )
938 {
939 Next();
940 if( evaluatePPCondition() )
941 {
942 if( eCurTok == THEN )
943 Next();
944 SbiToken eTok2 = Peek();
945 while( !bAbort && !IsEof() &&
946 !( eTok2 == PP_ELSEIF || eTok2 == PP_ELSE || eTok2 == PP_ENDIF ) )
947 {
948 Parse();
949 if( eCurTok == PP_ELSEIF || eCurTok == PP_ELSE || eCurTok == PP_ENDIF )
950 {
951 eTok2 = eCurTok;
952 if( ePush == NIL )
953 ePush = eCurTok;
954 break;
955 }
956 eTok2 = Peek();
957 }
958 if( eTok2 == PP_ENDIF )
959 Next();
960 else if( eTok2 == PP_ELSE )
961 {
962 Next();
963 skipUntilEndIf();
964 }
965 }
966 else
967 {
968 if( eCurTok == THEN )
969 Next();
970 skipUntilEndIf();
971 if( eCurTok == PP_ELSE )
972 {
973 Next();
974 SbiToken eTok2 = Peek();
975 while( !bAbort && !IsEof() &&
976 !( eTok2 == PP_ELSEIF || eTok2 == PP_ELSE || eTok2 == PP_ENDIF ) )
977 {
978 Parse();
979 if( eCurTok == PP_ELSEIF || eCurTok == PP_ELSE || eCurTok == PP_ENDIF )
980 {
981 eTok2 = eCurTok;
982 if( ePush == NIL )
983 ePush = eCurTok;
984 break;
985 }
986 eTok2 = Peek();
987 }
988 if( eTok2 == PP_ENDIF )
989 Next();
990 }
991 else if( eCurTok == PP_ENDIF )
992 Next();
993 }
994 return;
995 }
996 else if( eTok == PP_ELSE )
997 {
998 Next();
999 SbiToken eTok2 = Peek();
1000 while( !bAbort && !IsEof() &&
1001 !( eTok2 == PP_ELSEIF || eTok2 == PP_ELSE || eTok2 == PP_ENDIF ) )
1002 {
1003 Parse();
1004 if( eCurTok == PP_ELSEIF || eCurTok == PP_ELSE || eCurTok == PP_ENDIF )
1005 {
1006 eTok2 = eCurTok;
1007 if( ePush == NIL )
1008 ePush = eCurTok;
1009 break;
1010 }
1011 eTok2 = Peek();
1012 }
1013 if( eTok2 == PP_ENDIF )
1014 Next();
1015 }
1016 else if( eTok == PP_ENDIF )
1017 {
1018 Next();
1019 }
1020 }
1021 }
1022
1023 // Helper: Check if identifier is a predefined VBA constant
isVBAPredefinedConst(const String & aName,bool & bValue)1024 bool SbiParser::isVBAPredefinedConst( const String& aName, bool& bValue )
1025 {
1026 if( aName.EqualsIgnoreCaseAscii( "VBA7" ) )
1027 {
1028 bValue = true;
1029 return true;
1030 }
1031 if( aName.EqualsIgnoreCaseAscii( "Win32" ) ||
1032 aName.EqualsIgnoreCaseAscii( "Win64" ) )
1033 {
1034 bValue = false;
1035 return true;
1036 }
1037 if( aName.EqualsIgnoreCaseAscii( "Win16" ) ||
1038 aName.EqualsIgnoreCaseAscii( "Mac" ) ||
1039 aName.EqualsIgnoreCaseAscii( "Win" ) )
1040 {
1041 bValue = false;
1042 return true;
1043 }
1044 return false;
1045 }
1046
1047 // Helper: Evaluate a single term in a #If condition (no And/Or).
1048 // Handles: True, False, identifier, Not expr, expr = expr, numbers.
1049 // The caller (PPIf or evaluatePPCondition's And/Or loop) must have already
1050 // called Next() to read the operand token into eCurTok.
1051 // After returning, eCurTok points to the token following the term
1052 // (Then, And, Or, etc.) so evaluatePPCondition can continue.
evaluatePPTerm()1053 bool SbiParser::evaluatePPTerm()
1054 {
1055 bool bResult = false;
1056 bool bNegate = false;
1057
1058 // Handle "Not" - aSym is already set by the caller's Next()
1059 if( aSym.EqualsIgnoreCaseAscii( "Not" ) )
1060 {
1061 bNegate = true;
1062 Next(); // read the negated operand
1063 }
1064
1065 // Check aSym BEFORE Peek(), since Peek() overwrites it
1066 if( aSym.EqualsIgnoreCaseAscii( "True" ) )
1067 {
1068 bResult = true;
1069 Next(); // advance past "True", read next token (Then or And/Or)
1070 }
1071 else if( aSym.EqualsIgnoreCaseAscii( "False" ) )
1072 {
1073 bResult = false;
1074 Next(); // advance past "False", read next token
1075 }
1076 else if( eCurTok == SYMBOL )
1077 {
1078 // Identifier - save before Peek() overwrites aSym
1079 String aName = aSym;
1080 Peek(); // look ahead to check for comparison
1081
1082 if( eCurTok == EQ || eCurTok == NE || eCurTok == LT || eCurTok == GT ||
1083 eCurTok == LE || eCurTok == GE )
1084 {
1085 // Comparison: identifier <op> value
1086 SbiToken eOp = eCurTok;
1087 Next(); // consume operator
1088 Peek(); // read RHS
1089
1090 // Resolve left-hand value
1091 bool bLVal = false;
1092 double nLVal = 0.0;
1093 bool bLIsNum = false;
1094 if( maConstNumMap.count( aName ) > 0 )
1095 {
1096 nLVal = maConstNumMap[ aName ];
1097 bLIsNum = true;
1098 bLVal = (nLVal != 0.0);
1099 }
1100 else if( maConstMap.count( aName ) > 0 )
1101 bLVal = maConstMap[ aName ];
1102 else
1103 isVBAPredefinedConst( aName, bLVal );
1104
1105 bool bRVal = false;
1106 double nRVal = 0.0;
1107 bool bRIsNum = false;
1108
1109 if( eCurTok == SYMBOL )
1110 {
1111 // RHS is True/False/identifier
1112 String aVal = aSym;
1113 if( aVal.EqualsIgnoreCaseAscii( "True" ) )
1114 bRVal = true;
1115 else if( aVal.EqualsIgnoreCaseAscii( "False" ) )
1116 bRVal = false;
1117 else if( maConstNumMap.count( aVal ) > 0 )
1118 {
1119 nRVal = maConstNumMap[ aVal ];
1120 bRIsNum = true;
1121 bRVal = (nRVal != 0.0);
1122 }
1123 else if( maConstMap.count( aVal ) > 0 )
1124 bRVal = maConstMap[ aVal ];
1125 else
1126 isVBAPredefinedConst( aVal, bRVal );
1127 Next(); // consume RHS
1128 }
1129 else if( eCurTok == NUMBER )
1130 {
1131 bRVal = (nVal != 0.0);
1132 nRVal = nVal;
1133 bRIsNum = true;
1134 Next(); // consume number
1135 }
1136 else
1137 {
1138 bResult = false;
1139 }
1140
1141 bool bNumCmp = bLIsNum || bRIsNum;
1142 if( eOp == EQ )
1143 bResult = bNumCmp ? (nLVal == nRVal) : (bLVal == bRVal);
1144 else if( eOp == NE )
1145 bResult = bNumCmp ? (nLVal != nRVal) : (bLVal != bRVal);
1146 else if( eOp == LT )
1147 bResult = bNumCmp ? (nLVal < nRVal) : (!bLVal && bRVal);
1148 else if( eOp == GT )
1149 bResult = bNumCmp ? (nLVal > nRVal) : (bLVal && !bRVal);
1150 else if( eOp == LE )
1151 bResult = bNumCmp ? (nLVal <= nRVal) : !bLVal;
1152 else if( eOp == GE )
1153 bResult = bNumCmp ? (nLVal >= nRVal) : bRVal;
1154
1155 // Read next token (THEN, AND, OR, etc.) for caller
1156 Peek();
1157 }
1158 else if( eCurTok == THEN )
1159 {
1160 // Just a constant: #If DEBUG Then
1161 if( maConstMap.count( aName ) > 0 )
1162 bResult = maConstMap[ aName ];
1163 else
1164 isVBAPredefinedConst( aName, bResult );
1165 // eCurTok is THEN (from Peek) - leave for caller
1166 }
1167 else
1168 {
1169 // End of expression or other token - use the constant directly
1170 if( maConstMap.count( aName ) > 0 )
1171 bResult = maConstMap[ aName ];
1172 else
1173 isVBAPredefinedConst( aName, bResult );
1174 // eCurTok is the peeked token - leave for caller
1175 }
1176 }
1177 else if( eCurTok == NUMBER )
1178 {
1179 bResult = (nVal != 0.0);
1180 Next(); // advance past number, read next token
1181 }
1182
1183 if( bNegate )
1184 bResult = !bResult;
1185
1186 return bResult;
1187 }
1188
1189 // Helper: Evaluate a conditional compilation expression.
1190 // Supports: True, False, identifier, Not, And, Or, = comparisons, numbers
evaluatePPCondition()1191 bool SbiParser::evaluatePPCondition()
1192 {
1193 bool bResult = evaluatePPTerm();
1194
1195 // Handle And/Or operators for compound expressions
1196 while( eCurTok == AND || eCurTok == OR )
1197 {
1198 bool bIsAnd = (eCurTok == AND);
1199 Next(); // consume And/Or
1200 bool bTerm = evaluatePPTerm();
1201 if( bIsAnd )
1202 bResult = bResult && bTerm;
1203 else
1204 bResult = bResult || bTerm;
1205 }
1206
1207 return bResult;
1208 }
1209 // Helper: Skip statements without generating code
1210 // Tracks nested #If blocks so inactive branches are fully skipped.
1211 // Continues past EOLN/EOS to skip to the next line (unlike a single
1212 // statement terminator check).
skipStatement()1213 void SbiParser::skipStatement()
1214 {
1215 sal_uInt16 nNesting = 0;
1216 while( !bAbort && !IsEof() )
1217 {
1218 if( eCurTok == PP_IF )
1219 {
1220 nNesting++;
1221 Next();
1222 }
1223 else if( eCurTok == PP_ENDIF )
1224 {
1225 if( nNesting > 0 )
1226 {
1227 nNesting--;
1228 Next();
1229 }
1230 else
1231 break;
1232 }
1233 else if( nNesting == 0 && ( eCurTok == PP_ELSEIF || eCurTok == PP_ELSE ) )
1234 {
1235 break;
1236 }
1237 else
1238 Next();
1239 }
1240 }
1241 // MS Excel VBA: #Const conditional compilation constant declaration.
1242 // Status: implemented - stores name=value in the conditional compilation map
PPConst()1243 void SbiParser::PPConst()
1244 {
1245 // #Const name = value
1246 // Parse: name
1247 Peek();
1248 if( eCurTok != SYMBOL )
1249 {
1250 Error( SbERR_SYMBOL_EXPECTED );
1251 Next();
1252 TestToken( ASSIGN );
1253 return;
1254 }
1255
1256 String aConstName = aSym;
1257 Next();
1258 TestToken( EQ );
1259 Next();
1260
1261 // Parse the value - expect True, False, or a number
1262 // eCurTok has the value token already (from Next above)
1263 if( aSym.EqualsIgnoreCaseAscii( "True" ) )
1264 {
1265 maConstMap[ aConstName ] = true;
1266 }
1267 else if( aSym.EqualsIgnoreCaseAscii( "False" ) )
1268 {
1269 maConstMap[ aConstName ] = false;
1270 }
1271 else if( eCurTok == NUMBER )
1272 {
1273 maConstMap[ aConstName ] = (nVal != 0.0);
1274 maConstNumMap[ aConstName ] = nVal;
1275 }
1276 else
1277 {
1278 // Unknown constant value - default to false
1279 maConstMap[ aConstName ] = false;
1280 SbiExpression aExpr( this ); // consume the expression
1281 }
1282
1283 // Expect end of statement
1284 TestEoln();
1285 }
1286
1287 // Helper: Skip all statements until matching #End If
skipUntilEndIf()1288 void SbiParser::skipUntilEndIf()
1289 {
1290 sal_uInt16 nNesting = 0;
1291 SbiToken eTok = Peek();
1292 while( !bAbort && !IsEof() )
1293 {
1294 if( eTok == PP_IF )
1295 {
1296 nNesting++;
1297 Next();
1298 }
1299 else if( eTok == PP_ENDIF )
1300 {
1301 if( nNesting == 0 )
1302 {
1303 Next();
1304 return;
1305 }
1306 nNesting--;
1307 Next();
1308 }
1309 else
1310 {
1311 Next();
1312 }
1313 eTok = Peek();
1314 }
1315 }
1316