LibreOffice Module basic (master) 1
exprtree.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include <memory>
22#include <parser.hxx>
23#include <basic/sberrors.hxx>
24#include <basic/sbmod.hxx>
26#include <expr.hxx>
27
29 SbiExprMode eMode, const KeywordSymbolInfo* pKeywordSymbolInfo ) :
30 pParser(p),
31 eCurExpr(t),
32 m_eMode(eMode)
33{
34 pExpr = (t != SbSTDEXPR ) ? Term( pKeywordSymbolInfo ) : Boolean();
35 if( t != SbSYMBOL )
36 {
37 pExpr->Optimize(pParser);
38 }
39 if( t == SbLVALUE && !pExpr->IsLvalue() )
40 {
42 }
43 if( t == SbOPERAND && !IsVariable() )
44 {
46 }
47}
48
50 pParser(p),
51 eCurExpr(SbOPERAND),
52 m_eMode(EXPRMODE_STANDARD)
53{
54 pExpr = std::make_unique<SbiExprNode>( n, t );
55 pExpr->Optimize(pParser);
56}
57
59 pParser(p),
60 eCurExpr(SbOPERAND),
61 m_eMode(EXPRMODE_STANDARD)
62{
63 pExpr = std::make_unique<SbiExprNode>( r, SbxVARIANT, std::move(pPar) );
64}
65
67
68// reading in a complete identifier
69// an identifier has the following form:
70// name[(Parameter)][.Name[(parameter)]]...
71// structure elements are coupled via the element pNext,
72// so that they're not in the tree.
73
74// Are there parameters without brackets following? This may be a number,
75// a string, a symbol or also a comma (if the 1st parameter is missing)
76
77static bool DoParametersFollow( const SbiParser* p, SbiExprType eCurExpr, SbiToken eTok )
78{
79 if( eTok == LPAREN )
80 {
81 return true;
82 }
83 // but only if similar to CALL!
84 if( !p->WhiteSpace() || eCurExpr != SbSYMBOL )
85 {
86 return false;
87 }
88 if ( eTok == NUMBER || eTok == MINUS || eTok == FIXSTRING ||
89 eTok == SYMBOL || eTok == COMMA || eTok == DOT || eTok == NOT || eTok == BYVAL )
90 {
91 return true;
92 }
93 else // check for default params with reserved names ( e.g. names of tokens )
94 {
95 SbiTokenizer tokens( *static_cast<const SbiTokenizer*>(p) );
96 // Urk the Next() / Peek() semantics are... weird
97 tokens.Next();
98 if ( tokens.Peek() == ASSIGN )
99 {
100 return true;
101 }
102 }
103 return false;
104}
105
106// definition of a new symbol
107
108static SbiSymDef* AddSym ( SbiToken eTok, SbiSymPool& rPool, SbiExprType eCurExpr,
109 const OUString& rName, SbxDataType eType, const SbiExprList* pPar )
110{
111 SbiSymDef* pDef;
112 // A= is not a procedure
113 bool bHasType = ( eTok == EQ || eTok == DOT );
114 if( ( !bHasType && eCurExpr == SbSYMBOL ) || pPar )
115 {
116 // so this is a procedure
117 // the correct pool should be found out, as
118 // procs must always get into a public pool
119 SbiSymPool* pPool = &rPool;
120 if( pPool->GetScope() != SbPUBLIC )
121 {
122 pPool = &rPool.GetParser()->aPublics;
123 }
124 SbiProcDef* pProc = pPool->AddProc( rName );
125
126 // special treatment for Colls like Documents(1)
127 if( eCurExpr == SbSTDEXPR )
128 {
129 bHasType = true;
130 }
131 pDef = pProc;
132 pDef->SetType( bHasType ? eType : SbxEMPTY );
133 if( pPar )
134 {
135 // generate dummy parameters
136 for( sal_Int32 n = 1; n <= pPar->GetSize(); n++ )
137 {
138 OUString aPar = "PAR" + OUString::number( n );
139 pProc->GetParams().AddSym( aPar );
140 }
141 }
142 }
143 else
144 {
145 // or a normal symbol
146 pDef = rPool.AddSym( rName );
147 pDef->SetType( eType );
148 }
149 return pDef;
150}
151
152// currently even keywords are allowed (because of Dflt properties of the same name)
153
154std::unique_ptr<SbiExprNode> SbiExpression::Term( const KeywordSymbolInfo* pKeywordSymbolInfo )
155{
156 if( pParser->Peek() == DOT )
157 {
158 SbiExprNode* pWithVar = pParser->GetWithVar();
159 // #26608: get to the node-chain's end to pass the correct object
160 SbiSymDef* pDef = pWithVar ? pWithVar->GetRealVar() : nullptr;
161 std::unique_ptr<SbiExprNode> pNd;
162 if( !pDef )
163 {
164 pParser->Next();
165 }
166 else
167 {
168 pNd = ObjTerm( *pDef );
169 if( pNd )
170 {
171 pNd->SetWithParent( pWithVar );
172 }
173 }
174 if( !pNd )
175 {
177 pNd = std::make_unique<SbiExprNode>( 1.0, SbxDOUBLE );
178 }
179 return pNd;
180 }
181
182 SbiToken eTok = (pKeywordSymbolInfo == nullptr) ? pParser->Next() : SYMBOL;
183 // memorize the parsing's begin
185 OUString aSym( (pKeywordSymbolInfo == nullptr) ? pParser->GetSym() : pKeywordSymbolInfo->m_aKeywordSymbol );
186 SbxDataType eType = (pKeywordSymbolInfo == nullptr) ? pParser->GetType() : pKeywordSymbolInfo->m_eSbxDataType;
187 SbiExprListPtr pPar;
188 std::unique_ptr<SbiExprListVector> pvMoreParLcl;
189 // are there parameters following?
190 SbiToken eNextTok = pParser->Peek();
191 // is it a known parameter?
192 // create a string constant then, which will be recognized
193 // in the SbiParameters-ctor and is continued to be handled
194 if( eNextTok == ASSIGN )
195 {
197 return std::make_unique<SbiExprNode>( aSym );
198 }
199 // no keywords allowed from here on!
200 if( SbiTokenizer::IsKwd( eTok )
201 && (!pParser->IsCompatible() || eTok != INPUT) )
202 {
204 bError = true;
205 }
206
207 eTok = eNextTok;
208 if( DoParametersFollow( pParser, eCurExpr, eTok ) )
209 {
210 bool bStandaloneExpression = (m_eMode == EXPRMODE_STANDALONE);
211 pPar = SbiExprList::ParseParameters( pParser, bStandaloneExpression );
212 bError = bError || !pPar->IsValid();
213 if( !bError )
214 bBracket = pPar->IsBracket();
215 eTok = pParser->Peek();
216
217 // i75443 check for additional sets of parameters
218 while( eTok == LPAREN )
219 {
220 if( pvMoreParLcl == nullptr )
221 {
222 pvMoreParLcl.reset(new SbiExprListVector);
223 }
225 bError = bError || !pAddPar->IsValid();
226 pvMoreParLcl->push_back( std::move(pAddPar) );
227 eTok = pParser->Peek();
228 }
229 }
230 // It might be an object part, if . or ! is following.
231 // In case of . the variable must already be defined;
232 // it's an object, if pDef is NULL after the search.
233 bool bObj = ( ( eTok == DOT || eTok == EXCLAM )
234 && !pParser->WhiteSpace() );
235 if( bObj )
236 {
237 bBracket = false; // Now the bracket for the first term is obsolete
238 if( eType == SbxVARIANT )
239 {
241 }
242 else
243 {
244 // Name%. really does not work!
246 bError = true;
247 }
248 }
249 // Search:
250 SbiSymDef* pDef = pParser->pPool->Find( aSym );
251 if( !pDef )
252 {
253 // Part of the Runtime-Library?
254 // from 31.3.1996: swapped out to parser-method
255 // (is also needed in SbiParser::DefVar() in DIM.CXX)
256 pDef = pParser->CheckRTLForSym( aSym, eType );
257
258 // #i109184: Check if symbol is or later will be defined inside module
259 SbModule& rMod = pParser->aGen.GetModule();
260 if( rMod.FindMethod( aSym, SbxClassType::DontCare ) )
261 {
262 pDef = nullptr;
263 }
264 }
265 if( !pDef )
266 {
267 if( bObj )
268 {
270 }
271 pDef = AddSym( eTok, *pParser->pPool, eCurExpr, aSym, eType, pPar.get() );
272 // Looks like this is a local ( but undefined variable )
273 // if it is in a static procedure then make this Symbol
274 // static
275 if ( !bObj && pParser->pProc && pParser->pProc->IsStatic() )
276 {
277 pDef->SetStatic();
278 }
279 }
280 else
281 {
282
283 SbiConstDef* pConst = pDef->GetConstDef();
284 if( pConst )
285 {
286 pPar = nullptr;
287 pvMoreParLcl.reset();
288 if( pConst->GetType() == SbxSTRING )
289 {
290 return std::make_unique<SbiExprNode>( pConst->GetString() );
291 }
292 else
293 {
294 return std::make_unique<SbiExprNode>( pConst->GetValue(), pConst->GetType() );
295 }
296 }
297
298 // 0 parameters come up to ()
299 if( pDef->GetDims() )
300 {
301 if( pPar && pPar->GetSize() && pPar->GetSize() != pDef->GetDims() )
302 {
304 }
305 }
306 if( pDef->IsDefinedAs() )
307 {
308 SbxDataType eDefType = pDef->GetType();
309 // #119187 Only error if types conflict
310 if( eType >= SbxINTEGER && eType <= SbxSTRING && eType != eDefType )
311 {
312 // How? Define with AS first and take a Suffix then?
314 bError = true;
315 }
316 else if ( eType == SbxVARIANT )
317 {
318 // if there's nothing named, take the type of the entry,
319 // but only if the var hasn't been defined with AS XXX
320 // so that we catch n% = 5 : print n
321 eType = eDefType;
322 }
323 }
324 // checking type of variables:
325 // is there named anything different in the scanner?
326 // That's OK for methods!
327 if( eType != SbxVARIANT && // Variant takes everything
328 eType != pDef->GetType() &&
329 !pDef->GetProcDef() )
330 {
331 // maybe pDef describes an object that so far has only been
332 // recognized as SbxVARIANT - then change type of pDef
333 // from 16.12.95 (similar cases possible perhaps?!?)
334 if( eType == SbxOBJECT && pDef->GetType() == SbxVARIANT )
335 {
336 pDef->SetType( SbxOBJECT );
337 }
338 else
339 {
341 bError = true;
342 }
343 }
344 }
345 auto pNd = std::make_unique<SbiExprNode>( *pDef, eType );
346 if( !pPar )
347 {
348 pPar = SbiExprList::ParseParameters( pParser,false,false );
349 }
350 pNd->aVar.pPar = pPar.release();
351 pNd->aVar.pvMorePar = pvMoreParLcl.release();
352 if( bObj )
353 {
354 // from 8.1.95: Object may also be of the type SbxVARIANT
355 if( pDef->GetType() == SbxVARIANT )
356 pDef->SetType( SbxOBJECT );
357 // if we scan something with point,
358 // the type must be SbxOBJECT
359 if( pDef->GetType() != SbxOBJECT && pDef->GetType() != SbxVARIANT )
360 {
361 // defer error until runtime if in vba mode
362 if ( !pParser->IsVBASupportOn() )
363 {
365 bError = true;
366 }
367 }
368 if( !bError )
369 {
370 pNd->aVar.pNext = ObjTerm( *pDef ).release();
371 }
372 }
373
375 return pNd;
376}
377
378// construction of an object term. A term of this kind is part
379// of an expression that begins with an object variable.
380
381std::unique_ptr<SbiExprNode> SbiExpression::ObjTerm( SbiSymDef& rObj )
382{
383 pParser->Next();
384 SbiToken eTok = pParser->Next();
385 if( eTok != SYMBOL && !SbiTokenizer::IsKwd( eTok ) && !SbiTokenizer::IsExtra( eTok ) )
386 {
387 // #66745 Some operators can also be allowed
388 // as identifiers, important for StarOne
389 if( eTok != MOD && eTok != NOT && eTok != AND && eTok != OR &&
390 eTok != XOR && eTok != EQV && eTok != IMP && eTok != IS )
391 {
393 bError = true;
394 }
395 }
396
397 if( bError )
398 {
399 return nullptr;
400 }
401 OUString aSym( pParser->GetSym() );
403 SbiExprListPtr pPar;
404 SbiExprListVector* pvMoreParLcl = nullptr;
405 eTok = pParser->Peek();
406
407 if( DoParametersFollow( pParser, eCurExpr, eTok ) )
408 {
409 pPar = SbiExprList::ParseParameters( pParser, false/*bStandaloneExpression*/ );
410 bError = bError || !pPar->IsValid();
411 eTok = pParser->Peek();
412
413 // i109624 check for additional sets of parameters
414 while( eTok == LPAREN )
415 {
416 if( pvMoreParLcl == nullptr )
417 {
418 pvMoreParLcl = new SbiExprListVector;
419 }
421 bError = bError || !pPar->IsValid();
422 pvMoreParLcl->push_back( std::move(pAddPar) );
423 eTok = pParser->Peek();
424 }
425 }
426 bool bObj = ( ( eTok == DOT || eTok == EXCLAM ) && !pParser->WhiteSpace() );
427 if( bObj )
428 {
429 if( eType == SbxVARIANT )
430 {
432 }
433 else
434 {
435 // Name%. does really not work!
437 bError = true;
438 }
439 }
440
441 // an object's symbol pool is always PUBLIC
442 SbiSymPool& rPool = rObj.GetPool();
443 rPool.SetScope( SbPUBLIC );
444 SbiSymDef* pDef = rPool.Find( aSym );
445 if( !pDef )
446 {
447 pDef = AddSym( eTok, rPool, eCurExpr, aSym, eType, pPar.get() );
448 pDef->SetType( eType );
449 }
450
451 auto pNd = std::make_unique<SbiExprNode>( *pDef, eType );
452 pNd->aVar.pPar = pPar.release();
453 pNd->aVar.pvMorePar = pvMoreParLcl;
454 if( bObj )
455 {
456 if( pDef->GetType() == SbxVARIANT )
457 {
458 pDef->SetType( SbxOBJECT );
459 }
460 if( pDef->GetType() != SbxOBJECT )
461 {
463 bError = true;
464 }
465 if( !bError )
466 {
467 pNd->aVar.pNext = ObjTerm( *pDef ).release();
468 pNd->eType = eType;
469 }
470 }
471 return pNd;
472}
473
474// an operand can be:
475// constant
476// scalar variable
477// structure elements
478// array elements
479// functions
480// bracketed expressions
481
482std::unique_ptr<SbiExprNode> SbiExpression::Operand( bool bUsedForTypeOf )
483{
484 std::unique_ptr<SbiExprNode> pRes;
485
486 // test operand:
487 switch( SbiToken eTok = pParser->Peek() )
488 {
489 case SYMBOL:
490 pRes = Term();
491 // process something like "IF Not r Is Nothing Then .."
492 if( !bUsedForTypeOf && pParser->IsVBASupportOn() && pParser->Peek() == IS )
493 {
494 eTok = pParser->Next();
495 pRes = std::make_unique<SbiExprNode>( std::move(pRes), eTok, Like() );
496 }
497 break;
498 case DOT: // .with
499 pRes = Term(); break;
500 case NOT:
501 pRes = VBA_Not();
502 break;
503 case NUMBER:
504 pParser->Next();
505 pRes = std::make_unique<SbiExprNode>( pParser->GetDbl(), pParser->GetType() );
506 break;
507 case FIXSTRING:
508 pParser->Next();
509 pRes = std::make_unique<SbiExprNode>( pParser->GetSym() ); break;
510 case LPAREN:
511 pParser->Next();
513 {
515 pRes = std::make_unique<SbiExprNode>(); // Dummy node
516 pParser->Next();
517 break;
518 }
519 nParenLevel++;
520 pRes = Boolean();
521 if( pParser->Peek() != RPAREN )
522 {
523 // If there was a LPARAM, it does not belong to the expression
525 {
527 }
528 else
529 {
531 }
532 }
533 else
534 {
535 pParser->Next();
537 {
538 SbiToken eTokAfterRParen = pParser->Peek();
539 if( eTokAfterRParen == EQ || eTokAfterRParen == LPAREN || eTokAfterRParen == DOT )
540 {
542 }
543 else
544 {
546 }
547 }
548 }
549 nParenLevel--;
550 break;
551 default:
552 // keywords here are OK at the moment!
553 if( SbiTokenizer::IsKwd( eTok ) )
554 {
555 pRes = Term();
556 }
557 else
558 {
559 pParser->Next();
560 pRes = std::make_unique<SbiExprNode>( 1.0, SbxDOUBLE );
562 }
563 break;
564 }
565 return pRes;
566}
567
568std::unique_ptr<SbiExprNode> SbiExpression::Unary()
569{
570 std::unique_ptr<SbiExprNode> pNd;
571 SbiToken eTok = pParser->Peek();
572 switch( eTok )
573 {
574 case MINUS:
575 eTok = NEG;
576 pParser->Next();
577 pNd = std::make_unique<SbiExprNode>( Unary(), eTok, nullptr );
578 break;
579 case NOT:
580 if( pParser->IsVBASupportOn() )
581 {
582 pNd = Operand();
583 }
584 else
585 {
586 pParser->Next();
587 pNd = std::make_unique<SbiExprNode>( Unary(), eTok, nullptr );
588 }
589 break;
590 case PLUS:
591 pParser->Next();
592 pNd = Unary();
593 break;
594 case TYPEOF:
595 {
596 pParser->Next();
597 std::unique_ptr<SbiExprNode> pObjNode = Operand( true/*bUsedForTypeOf*/ );
598 pParser->TestToken( IS );
599 SbiSymDef* pTypeDef = new SbiSymDef( OUString() );
600 pParser->TypeDecl( *pTypeDef, true );
601 pNd = std::make_unique<SbiExprNode>( std::move(pObjNode), pTypeDef->GetTypeId() );
602 break;
603 }
604 case NEW:
605 {
606 pParser->Next();
607 SbiSymDef* pTypeDef = new SbiSymDef( OUString() );
608 pParser->TypeDecl( *pTypeDef, true );
609 pNd = std::make_unique<SbiExprNode>( pTypeDef->GetTypeId() );
610 break;
611 }
612 default:
613 pNd = Operand();
614 }
615 return pNd;
616}
617
618std::unique_ptr<SbiExprNode> SbiExpression::Exp()
619{
620 std::unique_ptr<SbiExprNode> pNd = Unary();
622 {
623 while( pParser->Peek() == EXPON )
624 {
625 SbiToken eTok = pParser->Next();
626 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Unary() );
627 }
628 }
629 return pNd;
630}
631
632std::unique_ptr<SbiExprNode> SbiExpression::MulDiv()
633{
634 std::unique_ptr<SbiExprNode> pNd = Exp();
636 {
637 for( ;; )
638 {
639 SbiToken eTok = pParser->Peek();
640 if( eTok != MUL && eTok != DIV )
641 {
642 break;
643 }
644 eTok = pParser->Next();
645 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Exp() );
646 }
647 }
648 return pNd;
649}
650
651std::unique_ptr<SbiExprNode> SbiExpression::IntDiv()
652{
653 std::unique_ptr<SbiExprNode> pNd = MulDiv();
655 {
656 while( pParser->Peek() == IDIV )
657 {
658 SbiToken eTok = pParser->Next();
659 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, MulDiv() );
660 }
661 }
662 return pNd;
663}
664
665std::unique_ptr<SbiExprNode> SbiExpression::Mod()
666{
667 std::unique_ptr<SbiExprNode> pNd = IntDiv();
669 {
670 while( pParser->Peek() == MOD )
671 {
672 SbiToken eTok = pParser->Next();
673 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, IntDiv() );
674 }
675 }
676 return pNd;
677}
678
679std::unique_ptr<SbiExprNode> SbiExpression::AddSub()
680{
681 std::unique_ptr<SbiExprNode> pNd = Mod();
683 {
684 for( ;; )
685 {
686 SbiToken eTok = pParser->Peek();
687 if( eTok != PLUS && eTok != MINUS )
688 {
689 break;
690 }
691 eTok = pParser->Next();
692 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Mod() );
693 }
694 }
695 return pNd;
696}
697
698std::unique_ptr<SbiExprNode> SbiExpression::Cat()
699{
700 std::unique_ptr<SbiExprNode> pNd = AddSub();
702 {
703 for( ;; )
704 {
705 SbiToken eTok = pParser->Peek();
706 if( eTok != CAT )
707 {
708 break;
709 }
710 eTok = pParser->Next();
711 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, AddSub() );
712 }
713 }
714 return pNd;
715}
716
717std::unique_ptr<SbiExprNode> SbiExpression::Comp()
718{
719 std::unique_ptr<SbiExprNode> pNd = Cat();
721 {
722 for( ;; )
723 {
724 SbiToken eTok = pParser->Peek();
726 {
727 break;
728 }
729 if( eTok != EQ && eTok != NE && eTok != LT &&
730 eTok != GT && eTok != LE && eTok != GE )
731 {
732 break;
733 }
734 eTok = pParser->Next();
735 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Cat() );
736 }
737 }
738 return pNd;
739}
740
741
742std::unique_ptr<SbiExprNode> SbiExpression::VBA_Not()
743{
744 std::unique_ptr<SbiExprNode> pNd;
745
746 SbiToken eTok = pParser->Peek();
747 if( eTok == NOT )
748 {
749 pParser->Next();
750 pNd = std::make_unique<SbiExprNode>( VBA_Not(), eTok, nullptr );
751 }
752 else
753 {
754 pNd = Comp();
755 }
756 return pNd;
757}
758
759std::unique_ptr<SbiExprNode> SbiExpression::Like()
760{
761 std::unique_ptr<SbiExprNode> pNd = pParser->IsVBASupportOn() ? VBA_Not() : Comp();
763 {
764 short nCount = 0;
765 while( pParser->Peek() == LIKE )
766 {
767 SbiToken eTok = pParser->Next();
768 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Comp() );
769 nCount++;
770 }
771 // multiple operands in a row does not work
772 if( nCount > 1 && !pParser->IsVBASupportOn() )
773 {
775 bError = true;
776 }
777 }
778 return pNd;
779}
780
781std::unique_ptr<SbiExprNode> SbiExpression::Boolean()
782{
783 std::unique_ptr<SbiExprNode> pNd = Like();
785 {
786 for( ;; )
787 {
788 SbiToken eTok = pParser->Peek();
789 if( (eTok != AND) && (eTok != OR) &&
790 (eTok != XOR) && (eTok != EQV) &&
791 (eTok != IMP) && (eTok != IS) )
792 {
793 break;
794 }
795 eTok = pParser->Next();
796 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Like() );
797 }
798 }
799 return pNd;
800}
801
803{
804 if( pExpr->IsConstant() )
805 {
806 eType = pExpr->GetType();
807 if( pExpr->IsNumber() )
808 {
809 nVal = pExpr->nVal;
810 }
811 else
812 {
813 nVal = 0;
814 aVal = pExpr->aStrVal;
815 }
816 }
817 else
818 {
819 // #40204 special treatment for sal_Bool-constants
820 bool bIsBool = false;
821 if( pExpr->eNodeType == SbxVARVAL )
822 {
823 SbiSymDef* pVarDef = pExpr->GetVar();
824
825 bool bBoolVal = false;
826 if( pVarDef->GetName().equalsIgnoreAsciiCase( "true" ) )
827 {
828 bIsBool = true;
829 bBoolVal = true;
830 }
831 else if( pVarDef->GetName().equalsIgnoreAsciiCase( "false" ) )
832 //else if( pVarDef->GetName().ICompare( "false" ) == COMPARE_EQUAL )
833 {
834 bIsBool = true;
835 bBoolVal = false;
836 }
837
838 if( bIsBool )
839 {
840 pExpr = std::make_unique<SbiExprNode>( (bBoolVal ? SbxTRUE : SbxFALSE), SbxINTEGER );
841 eType = pExpr->GetType();
842 nVal = pExpr->nVal;
843 }
844 }
845
846 if( !bIsBool )
847 {
850 nVal = 0;
851 }
852 }
853}
854
856{
857 if( eType == SbxSTRING )
858 {
859 SbxVariableRef refConv = new SbxVariable;
860 refConv->PutString( aVal );
861 return refConv->GetInteger();
862 }
863 else
864 {
865 double n = nVal;
866 if( n > 0 )
867 {
868 n += .5;
869 }
870 else
871 {
872 n -= .5;
873 }
874 if( n > SbxMAXINT )
875 {
876 n = SbxMAXINT;
878 }
879 else if( n < SbxMININT )
880 {
881 n = SbxMININT;
883 }
884
885 return static_cast<short>(n);
886 }
887}
888
889
891{
892 nDim = 0;
893 bError = false;
894 bBracket = false;
895}
896
898
900{
901 return aData[n].get();
902}
903
904void SbiExprList::addExpression( std::unique_ptr<SbiExpression>&& pExpr )
905{
906 aData.push_back(std::move(pExpr));
907}
908
909// the parameter list is completely parsed
910// "procedurename()" is OK
911// it's a function without parameters then
912// i. e. you give an array as procedure parameter
913
914// #i79918/#i80532: bConst has never been set to true
915// -> reused as bStandaloneExpression
916//SbiParameters::SbiParameters( SbiParser* p, sal_Bool bConst, sal_Bool bPar) :
917SbiExprListPtr SbiExprList::ParseParameters( SbiParser* pParser, bool bStandaloneExpression, bool bPar)
918{
919 auto pExprList = std::make_unique<SbiExprList>();
920 if( !bPar )
921 {
922 return pExprList;
923 }
924
925 SbiToken eTok = pParser->Peek();
926
927 bool bAssumeExprLParenMode = false;
928 bool bAssumeArrayMode = false;
929 if( eTok == LPAREN )
930 {
931 if( bStandaloneExpression )
932 {
933 bAssumeExprLParenMode = true;
934 }
935 else
936 {
937 pExprList->bBracket = true;
938 pParser->Next();
939 eTok = pParser->Peek();
940 }
941 }
942
943
944 if( ( pExprList->bBracket && eTok == RPAREN ) || SbiTokenizer::IsEoln( eTok ) )
945 {
946 if( eTok == RPAREN )
947 {
948 pParser->Next();
949 }
950 return pExprList;
951 }
952 // read in parameter table and lay down in correct order!
953 while( !pExprList->bError )
954 {
955 std::unique_ptr<SbiExpression> pExpr;
956 // missing argument
957 if( eTok == COMMA )
958 {
959 pExpr = std::make_unique<SbiExpression>( pParser, 0, SbxEMPTY );
960 }
961 // named arguments: either .name= or name:=
962 else
963 {
964 bool bByVal = false;
965 if( eTok == BYVAL )
966 {
967 bByVal = true;
968 pParser->Next();
969 eTok = pParser->Peek();
970 }
971
972 if( bAssumeExprLParenMode )
973 {
974 pExpr = std::make_unique<SbiExpression>( pParser, SbSTDEXPR, EXPRMODE_LPAREN_PENDING );
975 bAssumeExprLParenMode = false;
976
977 SbiExprMode eModeAfter = pExpr->m_eMode;
978 if( eModeAfter == EXPRMODE_LPAREN_NOT_NEEDED )
979 {
980 pExprList->bBracket = true;
981 }
982 else if( eModeAfter == EXPRMODE_ARRAY_OR_OBJECT )
983 {
984 // Expression "looks" like an array assignment
985 // a(...)[(...)] = ? or a(...).b(...)
986 // RPAREN is already parsed
987 pExprList->bBracket = true;
988 bAssumeArrayMode = true;
989 eTok = NIL;
990 }
991 else if( eModeAfter == EXPRMODE_EMPTY_PAREN )
992 {
993 pExprList->bBracket = true;
994 return pExprList;
995 }
996 }
997 else
998 {
999 pExpr = std::make_unique<SbiExpression>( pParser );
1000 }
1001 if( bByVal && pExpr->IsLvalue() )
1002 {
1003 pExpr->SetByVal();
1004 }
1005 if( !bAssumeArrayMode )
1006 {
1007 OUString aName;
1008 if( pParser->Peek() == ASSIGN )
1009 {
1010 // VBA mode: name:=
1011 // SbiExpression::Term() has made as string out of it
1012 aName = pExpr->GetString();
1013 pParser->Next();
1014 pExpr = std::make_unique<SbiExpression>( pParser );
1015 }
1016 pExpr->GetName() = aName;
1017 }
1018 }
1019 pExprList->bError = pExprList->bError || !pExpr->IsValid();
1020 pExprList->aData.push_back(std::move(pExpr));
1021 if( bAssumeArrayMode )
1022 {
1023 break;
1024 }
1025 // next element?
1026 eTok = pParser->Peek();
1027 if( eTok != COMMA )
1028 {
1029 if( ( pExprList->bBracket && eTok == RPAREN ) || SbiTokenizer::IsEoln( eTok ) )
1030 {
1031 // tdf#80731
1032 if (SbiTokenizer::IsEoln(eTok) && pExprList->bBracket)
1033 {
1034 // tdf#106529: only fail here in strict mode (i.e. when compiled from IDE), and
1035 // allow legacy code with missing closing parenthesis when started e.g. from
1036 // extensions and event handlers
1037 if (comphelper::IsContextFlagActive("BasicStrict"))
1038 {
1040 pExprList->bError = true;
1041 }
1042 }
1043 break;
1044 }
1045 pParser->Error( pExprList->bBracket ? ERRCODE_BASIC_BAD_BRACKETS : ERRCODE_BASIC_EXPECTED, COMMA );
1046 pExprList->bError = true;
1047 }
1048 else
1049 {
1050 pParser->Next();
1051 eTok = pParser->Peek();
1052 if( ( pExprList->bBracket && eTok == RPAREN ) || SbiTokenizer::IsEoln( eTok ) )
1053 {
1054 break;
1055 }
1056 }
1057 }
1058 // closing bracket
1059 if( eTok == RPAREN )
1060 {
1061 pParser->Next();
1062 pParser->Peek();
1063 if( !pExprList->bBracket )
1064 {
1066 pExprList->bError = true;
1067 }
1068 }
1069 pExprList->nDim = pExprList->GetSize();
1070 return pExprList;
1071}
1072
1073// A list of array dimensions is parsed.
1074
1076{
1077 auto pExprList = std::make_unique<SbiExprList>();
1078
1079 if( pParser->Next() != LPAREN )
1080 {
1081 pParser->Error( ERRCODE_BASIC_EXPECTED, LPAREN );
1082 pExprList->bError = true; return pExprList;
1083 }
1084
1085 if( pParser->Peek() != RPAREN )
1086 {
1087 SbiToken eTok;
1088 for( ;; )
1089 {
1090 auto pExpr1 = std::make_unique<SbiExpression>( pParser );
1091 eTok = pParser->Next();
1092 if( eTok == TO )
1093 {
1094 auto pExpr2 = std::make_unique<SbiExpression>( pParser );
1095 pExpr1->ConvertToIntConstIfPossible();
1096 pExpr2->ConvertToIntConstIfPossible();
1097 eTok = pParser->Next();
1098 pExprList->bError = pExprList->bError || !pExpr1->IsValid() || !pExpr2->IsValid();
1099 pExprList->aData.push_back(std::move(pExpr1));
1100 pExprList->aData.push_back(std::move(pExpr2));
1101 }
1102 else
1103 {
1104 pExpr1->SetBased();
1105 pExpr1->ConvertToIntConstIfPossible();
1106 pExprList->bError = pExprList->bError || !pExpr1->IsValid();
1107 pExprList->aData.push_back(std::move(pExpr1));
1108 }
1109 pExprList->nDim++;
1110 if( eTok == RPAREN ) break;
1111 if( eTok != COMMA )
1112 {
1114 pParser->Next();
1115 break;
1116 }
1117 }
1118 }
1119 else pParser->Next();
1120 return pExprList;
1121}
1122
1123/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
SbMethod * FindMethod(const OUString &, SbxClassType)
Definition: sbxmod.cxx:524
SbModule & GetModule()
Definition: codegen.hxx:39
double GetValue() const
Definition: symtbl.hxx:214
const OUString & GetString() const
Definition: symtbl.hxx:215
OUString aVal
Definition: expr.hxx:217
SbiConstExpression(SbiParser *)
Definition: exprtree.cxx:802
SbxDataType eType
Definition: expr.hxx:218
short GetShortValue()
Definition: exprtree.cxx:855
short GetSize() const
Definition: expr.hxx:100
bool bBracket
Definition: expr.hxx:92
void addExpression(std::unique_ptr< SbiExpression > &&pExpr)
Definition: exprtree.cxx:904
bool bError
Definition: expr.hxx:91
static SbiExprListPtr ParseParameters(SbiParser *, bool bStandaloneExpression=false, bool bPar=true)
Definition: exprtree.cxx:917
short nDim
Definition: expr.hxx:90
static SbiExprListPtr ParseDimList(SbiParser *)
Definition: exprtree.cxx:1075
SbiExpression * Get(size_t)
Definition: exprtree.cxx:899
std::vector< std::unique_ptr< SbiExpression > > aData
Definition: expr.hxx:89
SbiSymDef * GetRealVar()
Definition: exprnode.cxx:127
std::unique_ptr< SbiExprNode > VBA_Not()
Definition: exprtree.cxx:742
SbiExprMode m_eMode
Definition: expr.hxx:173
std::unique_ptr< SbiExprNode > Comp()
Definition: exprtree.cxx:717
bool bError
Definition: expr.hxx:175
std::unique_ptr< SbiExprNode > Boolean()
Definition: exprtree.cxx:781
std::unique_ptr< SbiExprNode > Like()
Definition: exprtree.cxx:759
std::unique_ptr< SbiExprNode > Unary()
Definition: exprtree.cxx:568
std::unique_ptr< SbiExprNode > Cat()
Definition: exprtree.cxx:698
SbiExprType eCurExpr
Definition: expr.hxx:172
std::unique_ptr< SbiExprNode > Operand(bool bUsedForTypeOf=false)
Definition: exprtree.cxx:482
std::unique_ptr< SbiExprNode > IntDiv()
Definition: exprtree.cxx:651
std::unique_ptr< SbiExprNode > pExpr
Definition: expr.hxx:171
std::unique_ptr< SbiExprNode > Exp()
Definition: exprtree.cxx:618
std::unique_ptr< SbiExprNode > AddSub()
Definition: exprtree.cxx:679
bool IsVariable() const
Definition: expr.hxx:205
bool bBracket
Definition: expr.hxx:177
sal_uInt16 nParenLevel
Definition: expr.hxx:178
std::unique_ptr< SbiExprNode > MulDiv()
Definition: exprtree.cxx:632
std::unique_ptr< SbiExprNode > Term(const KeywordSymbolInfo *pKeywordSymbolInfo=nullptr)
Definition: exprtree.cxx:154
SbiParser * pParser
Definition: expr.hxx:170
std::unique_ptr< SbiExprNode > ObjTerm(SbiSymDef &)
Definition: exprtree.cxx:381
SbiExpression(SbiParser *, SbiExprType=SbSTDEXPR, SbiExprMode eMode=EXPRMODE_STANDARD, const KeywordSymbolInfo *pKeywordSymbolInfo=nullptr)
Definition: exprtree.cxx:28
std::unique_ptr< SbiExprNode > Mod()
Definition: exprtree.cxx:665
SbiSymPool * pPool
Definition: parser.hxx:69
bool TestToken(SbiToken)
Definition: parser.cxx:261
SbiExprNode * GetWithVar()
Definition: parser.cxx:472
SbiSymPool aPublics
Definition: parser.hxx:66
void TypeDecl(SbiSymDef &, bool bAsNewAlreadyParsed=false)
Definition: dim.cxx:79
SbiSymDef * CheckRTLForSym(const OUString &rSym, SbxDataType eType)
Definition: parser.cxx:157
SbiProcDef * pProc
Definition: parser.hxx:36
SbiCodeGen aGen
Definition: parser.hxx:68
SbiSymPool & GetParams()
Definition: symtbl.hxx:177
void UnlockColumn()
Definition: scanner.cxx:69
double GetDbl() const
Definition: scanner.hxx:93
bool IsVBASupportOn() const
Definition: scanner.hxx:77
SbxDataType GetType() const
Definition: scanner.hxx:92
bool WhiteSpace() const
Definition: scanner.hxx:78
const OUString & GetSym() const
Definition: scanner.hxx:91
bool IsCompatible() const
Definition: scanner.hxx:75
void LockColumn()
Definition: scanner.cxx:63
virtual SbiProcDef * GetProcDef()
Definition: symtbl.cxx:310
virtual SbiConstDef * GetConstDef()
Definition: symtbl.cxx:315
short GetDims() const
Definition: symtbl.hxx:127
sal_uInt16 GetTypeId() const
Definition: symtbl.hxx:121
SbiSymPool & GetPool()
Definition: symtbl.cxx:385
SbxDataType GetType() const
Definition: symtbl.hxx:115
bool IsStatic() const
Definition: symtbl.hxx:145
const OUString & GetName()
Definition: symtbl.cxx:321
void SetStatic(bool bAsStatic=true)
Definition: symtbl.hxx:134
virtual void SetType(SbxDataType)
Definition: symtbl.cxx:331
bool IsDefinedAs() const
Definition: symtbl.hxx:147
SbiSymScope GetScope() const
Definition: symtbl.hxx:66
SbiSymDef * Find(const OUString &, bool bSearchInParents=true)
Definition: symtbl.cxx:202
SbiProcDef * AddProc(const OUString &)
Definition: symtbl.cxx:150
void SetScope(SbiSymScope s)
Definition: symtbl.hxx:67
SbiParser * GetParser()
Definition: symtbl.hxx:68
SbiSymDef * AddSym(const OUString &)
Definition: symtbl.cxx:139
static bool IsExtra(SbiToken t)
Definition: token.hxx:131
static bool IsKwd(SbiToken t)
Definition: token.hxx:129
SbiToken Next()
Definition: token.cxx:309
static bool IsEoln(SbiToken t)
Definition: token.hxx:127
void Error(ErrCode c)
Definition: token.hxx:123
SbiToken Peek()
Definition: token.cxx:248
int nCount
std::unique_ptr< SbiExprList > SbiExprListPtr
Definition: expr.hxx:34
std::vector< SbiExprListPtr > SbiExprListVector
Definition: expr.hxx:38
SbiExprType
Definition: expr.hxx:53
@ SbSTDEXPR
Definition: expr.hxx:54
@ SbOPERAND
Definition: expr.hxx:57
@ SbLVALUE
Definition: expr.hxx:55
@ SbSYMBOL
Definition: expr.hxx:56
@ SbxVARVAL
Definition: expr.hxx:74
SbiExprMode
Definition: expr.hxx:60
@ EXPRMODE_LPAREN_PENDING
Definition: expr.hxx:63
@ EXPRMODE_EMPTY_PAREN
Definition: expr.hxx:68
@ EXPRMODE_LPAREN_NOT_NEEDED
Definition: expr.hxx:64
@ EXPRMODE_ARRAY_OR_OBJECT
Definition: expr.hxx:65
@ EXPRMODE_STANDARD
Definition: expr.hxx:61
@ EXPRMODE_STANDALONE
Definition: expr.hxx:62
static bool DoParametersFollow(const SbiParser *p, SbiExprType eCurExpr, SbiToken eTok)
Definition: exprtree.cxx:77
static SbiSymDef * AddSym(SbiToken eTok, SbiSymPool &rPool, SbiExprType eCurExpr, const OUString &rName, SbxDataType eType, const SbiExprList *pPar)
Definition: exprtree.cxx:108
DocumentType eType
OUString aName
Mode eMode
void * p
sal_Int64 n
bool IsContextFlagActive(const OUString &sName)
dictionary tokens
#define ERRCODE_BASIC_EXPECTED
Definition: sberrors.hxx:125
#define ERRCODE_BASIC_BAD_DECLARATION
Definition: sberrors.hxx:141
#define ERRCODE_BASIC_VAR_EXPECTED
Definition: sberrors.hxx:127
#define ERRCODE_BASIC_UNEXPECTED
Definition: sberrors.hxx:124
#define ERRCODE_BASIC_LVALUE_EXPECTED
Definition: sberrors.hxx:129
#define ERRCODE_BASIC_OUT_OF_RANGE
Definition: sberrors.hxx:28
#define ERRCODE_BASIC_SYNTAX
Definition: sberrors.hxx:25
#define ERRCODE_BASIC_BAD_BRACKETS
Definition: sberrors.hxx:140
#define ERRCODE_BASIC_WRONG_DIMS
Definition: sberrors.hxx:148
@ SbxFALSE
Definition: sbxdef.hxx:215
@ SbxTRUE
Definition: sbxdef.hxx:215
constexpr auto SbxMAXINT
Definition: sbxdef.hxx:187
SbxDataType
Definition: sbxdef.hxx:37
@ SbxOBJECT
Definition: sbxdef.hxx:47
@ SbxEMPTY
Definition: sbxdef.hxx:38
@ SbxVARIANT
Definition: sbxdef.hxx:51
@ SbxSTRING
Definition: sbxdef.hxx:46
@ SbxINTEGER
Definition: sbxdef.hxx:40
@ SbxDOUBLE
Definition: sbxdef.hxx:43
constexpr auto SbxMININT
Definition: sbxdef.hxx:188
OUString m_aKeywordSymbol
Definition: expr.hxx:49
SbxDataType m_eSbxDataType
Definition: expr.hxx:50
@ SbPUBLIC
Definition: symtbl.hxx:32
SbiToken
Definition: token.hxx:30
@ NOT
Definition: token.hxx:74
@ MUL
Definition: token.hxx:71
@ NUMBER
Definition: token.hxx:78
@ GT
Definition: token.hxx:73
@ MOD
Definition: token.hxx:72
@ CAT
Definition: token.hxx:75
@ RPAREN
Definition: token.hxx:33
@ FIXSTRING
Definition: token.hxx:78
@ LPAREN
Definition: token.hxx:33
@ LT
Definition: token.hxx:73
@ NE
Definition: token.hxx:73
@ ASSIGN
Definition: token.hxx:38
@ GE
Definition: token.hxx:73
@ TYPEOF
Definition: token.hxx:75
@ LE
Definition: token.hxx:73
@ NIL
Definition: token.hxx:31
@ IS
Definition: token.hxx:75
@ EQV
Definition: token.hxx:74
@ TO
Definition: token.hxx:62
@ IMP
Definition: token.hxx:75
@ DIV
Definition: token.hxx:72
@ AND
Definition: token.hxx:74
@ DOT
Definition: token.hxx:33
@ PLUS
Definition: token.hxx:72
@ EXCLAM
Definition: token.hxx:33
@ OR
Definition: token.hxx:74
@ EQ
Definition: token.hxx:73
@ LIKE
Definition: token.hxx:75
@ BYVAL
Definition: token.hxx:78
@ XOR
Definition: token.hxx:74
@ EXPON
Definition: token.hxx:71
@ INPUT
Definition: token.hxx:55
@ SYMBOL
Definition: token.hxx:78
@ NEW
Definition: token.hxx:57
@ MINUS
Definition: token.hxx:72
@ NEG
Definition: token.hxx:71
@ COMMA
Definition: token.hxx:33
@ IDIV
Definition: token.hxx:72