Debian Codegraph @

basic/source/comp/exprtree.cxx

raw
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>
25 #include <comphelper/SetFlagContextHelper.hxx>
26 #include <expr.hxx>
27
28 SbiExpression::SbiExpression( SbiParser* p, SbiExprType t,
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 {
41 p->Error( ERRCODE_BASIC_LVALUE_EXPECTED );
42 }
43 if( t == SbOPERAND && !IsVariable() )
44 {
45 p->Error( ERRCODE_BASIC_VAR_EXPECTED );
46 }
47 }
48
49 SbiExpression::SbiExpression( SbiParser* p, double n, SbxDataType t ) :
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
58 SbiExpression::SbiExpression( SbiParser* p, const SbiSymDef& r, SbiExprListPtr pPar ) :
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
66 SbiExpression::~SbiExpression() { }
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
77 static 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
108 static 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
154 std::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 {
176 pParser->Error( ERRCODE_BASIC_UNEXPECTED, DOT );
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
184 pParser->LockColumn();
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 {
196 pParser->UnlockColumn();
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 {
203 pParser->Error( ERRCODE_BASIC_SYNTAX );
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 }
224 SbiExprListPtr pAddPar = SbiExprList::ParseParameters( pParser );
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 {
240 eType = SbxOBJECT;
241 }
242 else
243 {
244 // Name%. really does not work!
245 pParser->Error( ERRCODE_BASIC_BAD_DECLARATION, aSym );
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 {
269 eType = SbxOBJECT;
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 {
303 pParser->Error( ERRCODE_BASIC_WRONG_DIMS );
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?
313 pParser->Error( ERRCODE_BASIC_BAD_DECLARATION, aSym );
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 // Do not modify the type of procedure arguments
337 if (pDef->GetScope() != SbPARAM)
338 pDef->SetType(SbxOBJECT);
339 }
340 else
341 {
342 pParser->Error( ERRCODE_BASIC_BAD_DECLARATION, aSym );
343 bError = true;
344 }
345 }
346 }
347 auto pNd = std::make_unique<SbiExprNode>( *pDef, eType );
348 if( !pPar )
349 {
350 pPar = SbiExprList::ParseParameters( pParser,false,false );
351 }
352 pNd->aVar.pPar = pPar.release();
353 pNd->aVar.pvMorePar = pvMoreParLcl.release();
354 if( bObj )
355 {
356 // from 8.1.95: Object may also be of the type SbxVARIANT
357 // (but do not modify type of procedure arguments)
358 if (pDef->GetType() == SbxVARIANT && pDef->GetScope() != SbPARAM)
359 pDef->SetType( SbxOBJECT );
360 // if we scan something with point,
361 // the type must be SbxOBJECT
362 if( pDef->GetType() != SbxOBJECT && pDef->GetType() != SbxVARIANT )
363 {
364 // defer error until runtime if in vba mode
365 if ( !pParser->IsVBASupportOn() )
366 {
367 pParser->Error( ERRCODE_BASIC_BAD_DECLARATION, aSym );
368 bError = true;
369 }
370 }
371 if( !bError )
372 {
373 pNd->aVar.pNext = ObjTerm( *pDef ).release();
374 }
375 }
376
377 pParser->UnlockColumn();
378 return pNd;
379 }
380
381 // construction of an object term. A term of this kind is part
382 // of an expression that begins with an object variable.
383
384 std::unique_ptr<SbiExprNode> SbiExpression::ObjTerm( SbiSymDef& rObj )
385 {
386 pParser->Next();
387 SbiToken eTok = pParser->Next();
388 if( eTok != SYMBOL && !SbiTokenizer::IsKwd( eTok ) && !SbiTokenizer::IsExtra( eTok ) )
389 {
390 // #66745 Some operators can also be allowed
391 // as identifiers, important for StarOne
392 if( eTok != MOD && eTok != NOT && eTok != AND && eTok != OR &&
393 eTok != XOR && eTok != EQV && eTok != IMP && eTok != IS )
394 {
395 pParser->Error( ERRCODE_BASIC_VAR_EXPECTED );
396 bError = true;
397 }
398 }
399
400 if( bError )
401 {
402 return nullptr;
403 }
404 OUString aSym( pParser->GetSym() );
405 SbxDataType eType = pParser->GetType();
406 SbiExprListPtr pPar;
407 SbiExprListVector* pvMoreParLcl = nullptr;
408 eTok = pParser->Peek();
409
410 if( DoParametersFollow( pParser, eCurExpr, eTok ) )
411 {
412 pPar = SbiExprList::ParseParameters( pParser, false/*bStandaloneExpression*/ );
413 bError = bError || !pPar->IsValid();
414 eTok = pParser->Peek();
415
416 // i109624 check for additional sets of parameters
417 while( eTok == LPAREN )
418 {
419 if( pvMoreParLcl == nullptr )
420 {
421 pvMoreParLcl = new SbiExprListVector;
422 }
423 SbiExprListPtr pAddPar = SbiExprList::ParseParameters( pParser );
424 bError = bError || !pPar->IsValid();
425 pvMoreParLcl->push_back( std::move(pAddPar) );
426 eTok = pParser->Peek();
427 }
428 }
429 bool bObj = ( ( eTok == DOT || eTok == EXCLAM ) && !pParser->WhiteSpace() );
430 if( bObj )
431 {
432 if( eType == SbxVARIANT )
433 {
434 eType = SbxOBJECT;
435 }
436 else
437 {
438 // Name%. does really not work!
439 pParser->Error( ERRCODE_BASIC_BAD_DECLARATION, aSym );
440 bError = true;
441 }
442 }
443
444 // an object's symbol pool is always PUBLIC
445 SbiSymPool& rPool = rObj.GetPool();
446 rPool.SetScope( SbPUBLIC );
447 SbiSymDef* pDef = rPool.Find( aSym );
448 if( !pDef )
449 {
450 pDef = AddSym( eTok, rPool, eCurExpr, aSym, eType, pPar.get() );
451 pDef->SetType( eType );
452 }
453
454 auto pNd = std::make_unique<SbiExprNode>( *pDef, eType );
455 pNd->aVar.pPar = pPar.release();
456 pNd->aVar.pvMorePar = pvMoreParLcl;
457 if( bObj )
458 {
459 if( pDef->GetType() == SbxVARIANT )
460 {
461 pDef->SetType( SbxOBJECT );
462 }
463 if( pDef->GetType() != SbxOBJECT )
464 {
465 pParser->Error( ERRCODE_BASIC_BAD_DECLARATION, aSym );
466 bError = true;
467 }
468 if( !bError )
469 {
470 pNd->aVar.pNext = ObjTerm( *pDef ).release();
471 pNd->eType = eType;
472 }
473 }
474 return pNd;
475 }
476
477 // an operand can be:
478 // constant
479 // scalar variable
480 // structure elements
481 // array elements
482 // functions
483 // bracketed expressions
484
485 std::unique_ptr<SbiExprNode> SbiExpression::Operand( bool bUsedForTypeOf )
486 {
487 std::unique_ptr<SbiExprNode> pRes;
488
489 // test operand:
490 switch( SbiToken eTok = pParser->Peek() )
491 {
492 case SYMBOL:
493 pRes = Term();
494 // process something like "IF Not r Is Nothing Then .."
495 if( !bUsedForTypeOf && pParser->IsVBASupportOn() && pParser->Peek() == IS )
496 {
497 eTok = pParser->Next();
498 pRes = std::make_unique<SbiExprNode>( std::move(pRes), eTok, Like() );
499 }
500 break;
501 case DOT: // .with
502 pRes = Term(); break;
503 case NOT:
504 pRes = VBA_Not();
505 break;
506 case NUMBER:
507 pParser->Next();
508 pRes = std::make_unique<SbiExprNode>( pParser->GetDbl(), pParser->GetType() );
509 break;
510 case FIXSTRING:
511 pParser->Next();
512 pRes = std::make_unique<SbiExprNode>( pParser->GetSym() ); break;
513 case LPAREN:
514 pParser->Next();
515 if( nParenLevel == 0 && m_eMode == EXPRMODE_LPAREN_PENDING && pParser->Peek() == RPAREN )
516 {
517 m_eMode = EXPRMODE_EMPTY_PAREN;
518 pRes = std::make_unique<SbiExprNode>(); // Dummy node
519 pParser->Next();
520 break;
521 }
522 nParenLevel++;
523 pRes = Boolean();
524 if( pParser->Peek() != RPAREN )
525 {
526 // If there was a LPARAM, it does not belong to the expression
527 if( nParenLevel == 1 && m_eMode == EXPRMODE_LPAREN_PENDING )
528 {
529 m_eMode = EXPRMODE_LPAREN_NOT_NEEDED;
530 }
531 else
532 {
533 pParser->Error( ERRCODE_BASIC_BAD_BRACKETS );
534 }
535 }
536 else
537 {
538 pParser->Next();
539 if( nParenLevel == 1 && m_eMode == EXPRMODE_LPAREN_PENDING )
540 {
541 SbiToken eTokAfterRParen = pParser->Peek();
542 if( eTokAfterRParen == EQ || eTokAfterRParen == LPAREN || eTokAfterRParen == DOT )
543 {
544 m_eMode = EXPRMODE_ARRAY_OR_OBJECT;
545 }
546 else
547 {
548 m_eMode = EXPRMODE_STANDARD;
549 }
550 }
551 }
552 nParenLevel--;
553 break;
554 default:
555 // keywords here are OK at the moment!
556 if( SbiTokenizer::IsKwd( eTok ) )
557 {
558 pRes = Term();
559 }
560 else
561 {
562 pParser->Next();
563 pRes = std::make_unique<SbiExprNode>( 1.0, SbxDOUBLE );
564 pParser->Error( ERRCODE_BASIC_UNEXPECTED, eTok );
565 }
566 break;
567 }
568 return pRes;
569 }
570
571 std::unique_ptr<SbiExprNode> SbiExpression::Unary()
572 {
573 std::unique_ptr<SbiExprNode> pNd;
574 SbiToken eTok = pParser->Peek();
575 switch( eTok )
576 {
577 case MINUS:
578 eTok = NEG;
579 pParser->Next();
580 pNd = std::make_unique<SbiExprNode>( Unary(), eTok, nullptr );
581 break;
582 case NOT:
583 if( pParser->IsVBASupportOn() )
584 {
585 pNd = Operand();
586 }
587 else
588 {
589 pParser->Next();
590 pNd = std::make_unique<SbiExprNode>( Unary(), eTok, nullptr );
591 }
592 break;
593 case PLUS:
594 pParser->Next();
595 pNd = Unary();
596 break;
597 case TYPEOF:
598 {
599 pParser->Next();
600 std::unique_ptr<SbiExprNode> pObjNode = Operand( true/*bUsedForTypeOf*/ );
601 pParser->TestToken( IS );
602 SbiSymDef* pTypeDef = new SbiSymDef( OUString() );
603 pParser->TypeDecl( *pTypeDef, true );
604 pNd = std::make_unique<SbiExprNode>( std::move(pObjNode), pTypeDef->GetTypeId() );
605 break;
606 }
607 case NEW:
608 {
609 pParser->Next();
610 SbiSymDef* pTypeDef = new SbiSymDef( OUString() );
611 pParser->TypeDecl( *pTypeDef, true );
612 pNd = std::make_unique<SbiExprNode>( pTypeDef->GetTypeId() );
613 break;
614 }
615 default:
616 pNd = Operand();
617 }
618 return pNd;
619 }
620
621 std::unique_ptr<SbiExprNode> SbiExpression::Exp()
622 {
623 std::unique_ptr<SbiExprNode> pNd = Unary();
624 if( m_eMode != EXPRMODE_EMPTY_PAREN )
625 {
626 while( pParser->Peek() == EXPON )
627 {
628 SbiToken eTok = pParser->Next();
629 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Unary() );
630 }
631 }
632 return pNd;
633 }
634
635 std::unique_ptr<SbiExprNode> SbiExpression::MulDiv()
636 {
637 std::unique_ptr<SbiExprNode> pNd = Exp();
638 if( m_eMode != EXPRMODE_EMPTY_PAREN )
639 {
640 for( ;; )
641 {
642 SbiToken eTok = pParser->Peek();
643 if( eTok != MUL && eTok != DIV )
644 {
645 break;
646 }
647 eTok = pParser->Next();
648 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Exp() );
649 }
650 }
651 return pNd;
652 }
653
654 std::unique_ptr<SbiExprNode> SbiExpression::IntDiv()
655 {
656 std::unique_ptr<SbiExprNode> pNd = MulDiv();
657 if( m_eMode != EXPRMODE_EMPTY_PAREN )
658 {
659 while( pParser->Peek() == IDIV )
660 {
661 SbiToken eTok = pParser->Next();
662 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, MulDiv() );
663 }
664 }
665 return pNd;
666 }
667
668 std::unique_ptr<SbiExprNode> SbiExpression::Mod()
669 {
670 std::unique_ptr<SbiExprNode> pNd = IntDiv();
671 if( m_eMode != EXPRMODE_EMPTY_PAREN )
672 {
673 while( pParser->Peek() == MOD )
674 {
675 SbiToken eTok = pParser->Next();
676 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, IntDiv() );
677 }
678 }
679 return pNd;
680 }
681
682 std::unique_ptr<SbiExprNode> SbiExpression::AddSub()
683 {
684 std::unique_ptr<SbiExprNode> pNd = Mod();
685 if( m_eMode != EXPRMODE_EMPTY_PAREN )
686 {
687 for( ;; )
688 {
689 SbiToken eTok = pParser->Peek();
690 if( eTok != PLUS && eTok != MINUS )
691 {
692 break;
693 }
694 eTok = pParser->Next();
695 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Mod() );
696 }
697 }
698 return pNd;
699 }
700
701 std::unique_ptr<SbiExprNode> SbiExpression::Cat()
702 {
703 std::unique_ptr<SbiExprNode> pNd = AddSub();
704 if( m_eMode != EXPRMODE_EMPTY_PAREN )
705 {
706 for( ;; )
707 {
708 SbiToken eTok = pParser->Peek();
709 if( eTok != CAT )
710 {
711 break;
712 }
713 eTok = pParser->Next();
714 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, AddSub() );
715 }
716 }
717 return pNd;
718 }
719
720 std::unique_ptr<SbiExprNode> SbiExpression::Comp()
721 {
722 std::unique_ptr<SbiExprNode> pNd = Cat();
723 if( m_eMode != EXPRMODE_EMPTY_PAREN )
724 {
725 for( ;; )
726 {
727 SbiToken eTok = pParser->Peek();
728 if( m_eMode == EXPRMODE_ARRAY_OR_OBJECT )
729 {
730 break;
731 }
732 if( eTok != EQ && eTok != NE && eTok != LT &&
733 eTok != GT && eTok != LE && eTok != GE )
734 {
735 break;
736 }
737 eTok = pParser->Next();
738 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Cat() );
739 }
740 }
741 return pNd;
742 }
743
744
745 std::unique_ptr<SbiExprNode> SbiExpression::VBA_Not()
746 {
747 std::unique_ptr<SbiExprNode> pNd;
748
749 SbiToken eTok = pParser->Peek();
750 if( eTok == NOT )
751 {
752 pParser->Next();
753 pNd = std::make_unique<SbiExprNode>( VBA_Not(), eTok, nullptr );
754 }
755 else
756 {
757 pNd = Comp();
758 }
759 return pNd;
760 }
761
762 std::unique_ptr<SbiExprNode> SbiExpression::Like()
763 {
764 std::unique_ptr<SbiExprNode> pNd = pParser->IsVBASupportOn() ? VBA_Not() : Comp();
765 if( m_eMode != EXPRMODE_EMPTY_PAREN )
766 {
767 short nCount = 0;
768 while( pParser->Peek() == LIKE )
769 {
770 SbiToken eTok = pParser->Next();
771 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Comp() );
772 nCount++;
773 }
774 // multiple operands in a row does not work
775 if( nCount > 1 && !pParser->IsVBASupportOn() )
776 {
777 pParser->Error( ERRCODE_BASIC_SYNTAX );
778 bError = true;
779 }
780 }
781 return pNd;
782 }
783
784 std::unique_ptr<SbiExprNode> SbiExpression::Boolean()
785 {
786 std::unique_ptr<SbiExprNode> pNd = Like();
787 if( m_eMode != EXPRMODE_EMPTY_PAREN )
788 {
789 for( ;; )
790 {
791 SbiToken eTok = pParser->Peek();
792 if( (eTok != AND) && (eTok != OR) &&
793 (eTok != XOR) && (eTok != EQV) &&
794 (eTok != IMP) && (eTok != IS) )
795 {
796 break;
797 }
798 eTok = pParser->Next();
799 pNd = std::make_unique<SbiExprNode>( std::move(pNd), eTok, Like() );
800 }
801 }
802 return pNd;
803 }
804
805 SbiConstExpression::SbiConstExpression( SbiParser* p ) : SbiExpression( p )
806 {
807 if( pExpr->IsConstant() )
808 {
809 eType = pExpr->GetType();
810 if( pExpr->IsNumber() )
811 {
812 nVal = pExpr->nVal;
813 }
814 else
815 {
816 nVal = 0;
817 aVal = pExpr->aStrVal;
818 }
819 }
820 else
821 {
822 // #40204 special treatment for sal_Bool-constants
823 bool bIsBool = false;
824 if( pExpr->eNodeType == SbxVARVAL )
825 {
826 SbiSymDef* pVarDef = pExpr->GetVar();
827
828 bool bBoolVal = false;
829 if( pVarDef->GetName().equalsIgnoreAsciiCase( "true" ) )
830 {
831 bIsBool = true;
832 bBoolVal = true;
833 }
834 else if( pVarDef->GetName().equalsIgnoreAsciiCase( "false" ) )
835 //else if( pVarDef->GetName().ICompare( "false" ) == COMPARE_EQUAL )
836 {
837 bIsBool = true;
838 bBoolVal = false;
839 }
840
841 if( bIsBool )
842 {
843 pExpr = std::make_unique<SbiExprNode>( (bBoolVal ? SbxTRUE : SbxFALSE), SbxINTEGER );
844 eType = pExpr->GetType();
845 nVal = pExpr->nVal;
846 }
847 }
848
849 if( !bIsBool )
850 {
851 pParser->Error( ERRCODE_BASIC_SYNTAX );
852 eType = SbxDOUBLE;
853 nVal = 0;
854 }
855 }
856 }
857
858 short SbiConstExpression::GetShortValue()
859 {
860 if( eType == SbxSTRING )
861 {
862 SbxVariableRef refConv = new SbxVariable;
863 refConv->PutString( aVal );
864 return refConv->GetInteger();
865 }
866 else
867 {
868 double n = nVal;
869 if( n > 0 )
870 {
871 n += .5;
872 }
873 else
874 {
875 n -= .5;
876 }
877 if( n > SbxMAXINT )
878 {
879 n = SbxMAXINT;
880 pParser->Error( ERRCODE_BASIC_OUT_OF_RANGE );
881 }
882 else if( n < SbxMININT )
883 {
884 n = SbxMININT;
885 pParser->Error( ERRCODE_BASIC_OUT_OF_RANGE );
886 }
887
888 return static_cast<short>(n);
889 }
890 }
891
892
893 SbiExprList::SbiExprList( )
894 {
895 nDim = 0;
896 bError = false;
897 bBracket = false;
898 }
899
900 SbiExprList::~SbiExprList() {}
901
902 SbiExpression* SbiExprList::Get( size_t n )
903 {
904 return aData[n].get();
905 }
906
907 void SbiExprList::addExpression( std::unique_ptr<SbiExpression>&& pExpr )
908 {
909 aData.push_back(std::move(pExpr));
910 }
911
912 // the parameter list is completely parsed
913 // "procedurename()" is OK
914 // it's a function without parameters then
915 // i. e. you give an array as procedure parameter
916
917 // #i79918/#i80532: bConst has never been set to true
918 // -> reused as bStandaloneExpression
919 //SbiParameters::SbiParameters( SbiParser* p, sal_Bool bConst, sal_Bool bPar) :
920 SbiExprListPtr SbiExprList::ParseParameters( SbiParser* pParser, bool bStandaloneExpression, bool bPar)
921 {
922 auto pExprList = std::make_unique<SbiExprList>();
923 if( !bPar )
924 {
925 return pExprList;
926 }
927
928 SbiToken eTok = pParser->Peek();
929
930 bool bAssumeExprLParenMode = false;
931 bool bAssumeArrayMode = false;
932 if( eTok == LPAREN )
933 {
934 if( bStandaloneExpression )
935 {
936 bAssumeExprLParenMode = true;
937 }
938 else
939 {
940 pExprList->bBracket = true;
941 pParser->Next();
942 eTok = pParser->Peek();
943 }
944 }
945
946
947 if( ( pExprList->bBracket && eTok == RPAREN ) || SbiTokenizer::IsEoln( eTok ) )
948 {
949 if( eTok == RPAREN )
950 {
951 pParser->Next();
952 }
953 return pExprList;
954 }
955 // read in parameter table and lay down in correct order!
956 while( !pExprList->bError )
957 {
958 std::unique_ptr<SbiExpression> pExpr;
959 // missing argument
960 if( eTok == COMMA )
961 {
962 pExpr = std::make_unique<SbiExpression>( pParser, 0, SbxEMPTY );
963 }
964 // named arguments: either .name= or name:=
965 else
966 {
967 bool bByVal = false;
968 if( eTok == BYVAL )
969 {
970 bByVal = true;
971 pParser->Next();
972 eTok = pParser->Peek();
973 }
974
975 if( bAssumeExprLParenMode )
976 {
977 pExpr = std::make_unique<SbiExpression>( pParser, SbSTDEXPR, EXPRMODE_LPAREN_PENDING );
978 bAssumeExprLParenMode = false;
979
980 SbiExprMode eModeAfter = pExpr->m_eMode;
981 if( eModeAfter == EXPRMODE_LPAREN_NOT_NEEDED )
982 {
983 pExprList->bBracket = true;
984 }
985 else if( eModeAfter == EXPRMODE_ARRAY_OR_OBJECT )
986 {
987 // Expression "looks" like an array assignment
988 // a(...)[(...)] = ? or a(...).b(...)
989 // RPAREN is already parsed
990 pExprList->bBracket = true;
991 bAssumeArrayMode = true;
992 eTok = NIL;
993 }
994 else if( eModeAfter == EXPRMODE_EMPTY_PAREN )
995 {
996 pExprList->bBracket = true;
997 return pExprList;
998 }
999 }
1000 else
1001 {
1002 pExpr = std::make_unique<SbiExpression>( pParser );
1003 }
1004 if( bByVal && pExpr->IsLvalue() )
1005 {
1006 pExpr->SetByVal();
1007 }
1008 if( !bAssumeArrayMode )
1009 {
1010 OUString aName;
1011 if( pParser->Peek() == ASSIGN )
1012 {
1013 // VBA mode: name:=
1014 // SbiExpression::Term() has made as string out of it
1015 aName = pExpr->GetString();
1016 pParser->Next();
1017 pExpr = std::make_unique<SbiExpression>( pParser );
1018 }
1019 pExpr->GetName() = aName;
1020 }
1021 }
1022 pExprList->bError = pExprList->bError || !pExpr->IsValid();
1023 pExprList->aData.push_back(std::move(pExpr));
1024 if( bAssumeArrayMode )
1025 {
1026 break;
1027 }
1028 // next element?
1029 eTok = pParser->Peek();
1030 if( eTok != COMMA )
1031 {
1032 if( ( pExprList->bBracket && eTok == RPAREN ) || SbiTokenizer::IsEoln( eTok ) )
1033 {
1034 // tdf#80731
1035 if (SbiTokenizer::IsEoln(eTok) && pExprList->bBracket)
1036 {
1037 // tdf#106529: only fail here in strict mode (i.e. when compiled from IDE), and
1038 // allow legacy code with missing closing parenthesis when started e.g. from
1039 // extensions and event handlers
1040 if (comphelper::IsContextFlagActive(u"BasicStrict"_ustr))
1041 {
1042 pParser->Error(ERRCODE_BASIC_EXPECTED, RPAREN);
1043 pExprList->bError = true;
1044 }
1045 }
1046 break;
1047 }
1048 pParser->Error( pExprList->bBracket ? ERRCODE_BASIC_BAD_BRACKETS : ERRCODE_BASIC_EXPECTED, COMMA );
1049 pExprList->bError = true;
1050 }
1051 else
1052 {
1053 pParser->Next();
1054 eTok = pParser->Peek();
1055 if( ( pExprList->bBracket && eTok == RPAREN ) || SbiTokenizer::IsEoln( eTok ) )
1056 {
1057 break;
1058 }
1059 }
1060 }
1061 // closing bracket
1062 if( eTok == RPAREN )
1063 {
1064 pParser->Next();
1065 pParser->Peek();
1066 if( !pExprList->bBracket )
1067 {
1068 pParser->Error( ERRCODE_BASIC_BAD_BRACKETS );
1069 pExprList->bError = true;
1070 }
1071 }
1072 pExprList->nDim = pExprList->GetSize();
1073 return pExprList;
1074 }
1075
1076 // A list of array dimensions is parsed.
1077
1078 SbiExprListPtr SbiExprList::ParseDimList( SbiParser* pParser )
1079 {
1080 auto pExprList = std::make_unique<SbiExprList>();
1081
1082 if( pParser->Next() != LPAREN )
1083 {
1084 pParser->Error( ERRCODE_BASIC_EXPECTED, LPAREN );
1085 pExprList->bError = true; return pExprList;
1086 }
1087
1088 if( pParser->Peek() != RPAREN )
1089 {
1090 SbiToken eTok;
1091 for( ;; )
1092 {
1093 auto pExpr1 = std::make_unique<SbiExpression>( pParser );
1094 eTok = pParser->Next();
1095 if( eTok == TO )
1096 {
1097 auto pExpr2 = std::make_unique<SbiExpression>( pParser );
1098 pExpr1->ConvertToIntConstIfPossible();
1099 pExpr2->ConvertToIntConstIfPossible();
1100 eTok = pParser->Next();
1101 pExprList->bError = pExprList->bError || !pExpr1->IsValid() || !pExpr2->IsValid();
1102 pExprList->aData.push_back(std::move(pExpr1));
1103 pExprList->aData.push_back(std::move(pExpr2));
1104 }
1105 else
1106 {
1107 pExpr1->SetBased();
1108 pExpr1->ConvertToIntConstIfPossible();
1109 pExprList->bError = pExprList->bError || !pExpr1->IsValid();
1110 pExprList->aData.push_back(std::move(pExpr1));
1111 }
1112 pExprList->nDim++;
1113 if( eTok == RPAREN ) break;
1114 if( eTok != COMMA )
1115 {
1116 pParser->Error( ERRCODE_BASIC_BAD_BRACKETS );
1117 pParser->Next();
1118 break;
1119 }
1120 }
1121 }
1122 else pParser->Next();
1123 return pExprList;
1124 }
1125
1126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */