| 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 |
#include <basic/sberrors.hxx> |
| 21 |
#include <basic/sbxmeth.hxx> |
| 22 |
#include <basic/sbmod.hxx> |
| 23 |
#include <basic/sbstar.hxx> |
| 24 |
#include <basic/sbx.hxx> |
| 25 |
#include <parser.hxx> |
| 26 |
#include <com/sun/star/script/ModuleType.hpp> |
| 27 |
#include <rtl/character.hxx> |
| 28 |
|
| 29 |
struct SbiParseStack { // "Stack" for statement-blocks |
| 30 |
SbiParseStack* pNext; // Chain |
| 31 |
SbiExprNode* pWithVar; |
| 32 |
SbiToken eExitTok; |
| 33 |
sal_uInt32 nChain; // JUMP-Chain |
| 34 |
}; |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
struct SbiStatement { |
| 39 |
SbiToken eTok; |
| 40 |
void( SbiParser::*Func )(); |
| 41 |
bool bMain; // true: OK outside the SUB |
| 42 |
bool bSubr; // true: OK inside the SUB |
| 43 |
}; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
#define Y true |
| 48 |
#define N false |
| 49 |
|
| 50 |
const SbiStatement StmntTable [] = { |
| 51 |
{ ATTRIBUTE, &SbiParser::Attribute, Y, Y, }, // ATTRIBUTE |
| 52 |
{ CALL, &SbiParser::Call, N, Y, }, // CALL |
| 53 |
{ CLOSE, &SbiParser::Close, N, Y, }, // CLOSE |
| 54 |
{ CONST_, &SbiParser::Dim, Y, Y, }, // CONST |
| 55 |
{ DECLARE, &SbiParser::Declare, Y, N, }, // DECLARE |
| 56 |
{ DEFBOOL, &SbiParser::DefXXX, Y, N, }, // DEFBOOL |
| 57 |
{ DEFCUR, &SbiParser::DefXXX, Y, N, }, // DEFCUR |
| 58 |
{ DEFDATE, &SbiParser::DefXXX, Y, N, }, // DEFDATE |
| 59 |
{ DEFDBL, &SbiParser::DefXXX, Y, N, }, // DEFDBL |
| 60 |
{ DEFERR, &SbiParser::DefXXX, Y, N, }, // DEFERR |
| 61 |
{ DEFINT, &SbiParser::DefXXX, Y, N, }, // DEFINT |
| 62 |
{ DEFLNG, &SbiParser::DefXXX, Y, N, }, // DEFLNG |
| 63 |
{ DEFOBJ, &SbiParser::DefXXX, Y, N, }, // DEFOBJ |
| 64 |
{ DEFSNG, &SbiParser::DefXXX, Y, N, }, // DEFSNG |
| 65 |
{ DEFSTR, &SbiParser::DefXXX, Y, N, }, // DEFSTR |
| 66 |
{ DEFVAR, &SbiParser::DefXXX, Y, N, }, // DEFVAR |
| 67 |
{ DIM, &SbiParser::Dim, Y, Y, }, // DIM |
| 68 |
{ DO, &SbiParser::DoLoop, N, Y, }, // DO |
| 69 |
{ ELSE, &SbiParser::NoIf, N, Y, }, // ELSE |
| 70 |
{ ELSEIF, &SbiParser::NoIf, N, Y, }, // ELSEIF |
| 71 |
{ ENDIF, &SbiParser::NoIf, N, Y, }, // ENDIF |
| 72 |
{ END, &SbiParser::Stop, N, Y, }, // END |
| 73 |
{ ENUM, &SbiParser::Enum, Y, N, }, // TYPE |
| 74 |
{ ERASE, &SbiParser::Erase, N, Y, }, // ERASE |
| 75 |
{ ERROR_, &SbiParser::ErrorStmnt, N, Y, }, // ERROR |
| 76 |
{ EXIT, &SbiParser::Exit, N, Y, }, // EXIT |
| 77 |
{ FOR, &SbiParser::For, N, Y, }, // FOR |
| 78 |
{ FUNCTION, &SbiParser::SubFunc, Y, N, }, // FUNCTION |
| 79 |
{ GOSUB, &SbiParser::Goto, N, Y, }, // GOSUB |
| 80 |
{ GLOBAL, &SbiParser::Dim, Y, N, }, // GLOBAL |
| 81 |
{ GOTO, &SbiParser::Goto, N, Y, }, // GOTO |
| 82 |
{ IF, &SbiParser::If, N, Y, }, // IF |
| 83 |
{ IMPLEMENTS, &SbiParser::Implements, Y, N, }, // IMPLEMENTS |
| 84 |
{ INPUT, &SbiParser::Input, N, Y, }, // INPUT |
| 85 |
{ LET, &SbiParser::Assign, N, Y, }, // LET |
| 86 |
{ LINE, &SbiParser::Line, N, Y, }, // LINE, -> LINE INPUT (#i92642) |
| 87 |
{ LINEINPUT,&SbiParser::LineInput, N, Y, }, // LINE INPUT |
| 88 |
{ LSET, &SbiParser::LSet, N, Y, }, // LSET |
| 89 |
{ NAME, &SbiParser::Name, N, Y, }, // NAME |
| 90 |
{ ON, &SbiParser::On, N, Y, }, // ON |
| 91 |
{ OPEN, &SbiParser::Open, N, Y, }, // OPEN |
| 92 |
{ OPTION, &SbiParser::Option, Y, N, }, // OPTION |
| 93 |
{ PRINT, &SbiParser::Print, N, Y, }, // PRINT |
| 94 |
{ PRIVATE, &SbiParser::Dim, Y, N, }, // PRIVATE |
| 95 |
{ PROPERTY, &SbiParser::SubFunc, Y, N, }, // FUNCTION |
| 96 |
{ PUBLIC, &SbiParser::Dim, Y, N, }, // PUBLIC |
| 97 |
{ REDIM, &SbiParser::ReDim, N, Y, }, // DIM |
| 98 |
{ RESUME, &SbiParser::Resume, N, Y, }, // RESUME |
| 99 |
{ RETURN, &SbiParser::Return, N, Y, }, // RETURN |
| 100 |
{ RSET, &SbiParser::RSet, N, Y, }, // RSET |
| 101 |
{ SELECT, &SbiParser::Select, N, Y, }, // SELECT |
| 102 |
{ SET, &SbiParser::Set, N, Y, }, // SET |
| 103 |
{ STATIC, &SbiParser::Static, Y, Y, }, // STATIC |
| 104 |
{ STOP, &SbiParser::Stop, N, Y, }, // STOP |
| 105 |
{ SUB, &SbiParser::SubFunc, Y, N, }, // SUB |
| 106 |
{ TYPE, &SbiParser::Type, Y, N, }, // TYPE |
| 107 |
{ WHILE, &SbiParser::While, N, Y, }, // WHILE |
| 108 |
{ WITH, &SbiParser::With, N, Y, }, // WITH |
| 109 |
{ WRITE, &SbiParser::Write, N, Y, }, // WRITE |
| 110 |
}; |
| 111 |
|
| 112 |
SbiParser::SbiParser( StarBASIC* pb, SbModule* pm ) |
| 113 |
: SbiTokenizer( pm->GetSource(), pb ), |
| 114 |
pStack(nullptr), |
| 115 |
pProc(nullptr), |
| 116 |
pWithVar(nullptr), |
| 117 |
eEndTok(NIL), |
| 118 |
bGblDefs(false), |
| 119 |
bNewGblDefs(false), |
| 120 |
bSingleLineIf(false), |
| 121 |
bCodeCompleting(false), |
| 122 |
aGlobals( aGblStrings, SbGLOBAL, this ), |
| 123 |
aPublics( aGblStrings, SbPUBLIC, this ), |
| 124 |
aRtlSyms( aGblStrings, SbRTL, this ), |
| 125 |
aGen( *pm, this ), |
| 126 |
nBase(0), |
| 127 |
bExplicit(false) |
| 128 |
{ |
| 129 |
bClassModule = ( pm->GetModuleType() == css::script::ModuleType::CLASS ); |
| 130 |
pPool = &aPublics; |
| 131 |
for(SbxDataType & eDefType : eDefTypes) |
| 132 |
eDefType = SbxVARIANT; // no explicit default type |
| 133 |
|
| 134 |
aPublics.SetParent( &aGlobals ); |
| 135 |
aGlobals.SetParent( &aRtlSyms ); |
| 136 |
|
| 137 |
|
| 138 |
nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 ); |
| 139 |
|
| 140 |
rTypeArray = new SbxArray; // array for user defined types |
| 141 |
rEnumArray = new SbxArray; // array for Enum types |
| 142 |
bVBASupportOn = pm->IsVBASupport(); |
| 143 |
if ( bVBASupportOn ) |
| 144 |
EnableCompatibility(); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
SbiParser::~SbiParser() { } |
| 149 |
|
| 150 |
// part of the runtime-library? |
| 151 |
SbiSymDef* SbiParser::CheckRTLForSym(const OUString& rSym, SbxDataType eType) |
| 152 |
{ |
| 153 |
SbxVariable* pVar = GetBasic()->GetRtl()->Find(rSym, SbxClassType::DontCare); |
| 154 |
if (!pVar) |
| 155 |
return nullptr; |
| 156 |
|
| 157 |
if (SbxMethod* pMethod = dynamic_cast<SbxMethod*>(pVar)) |
| 158 |
{ |
| 159 |
SbiProcDef* pProc_ = aRtlSyms.AddProc( rSym ); |
| 160 |
if (pMethod->IsRuntimeFunction()) |
| 161 |
{ |
| 162 |
pProc_->SetType( pMethod->GetRuntimeFunctionReturnType() ); |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
pProc_->SetType( pVar->GetType() ); |
| 167 |
} |
| 168 |
return pProc_; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
SbiSymDef* pDef = aRtlSyms.AddSym(rSym); |
| 173 |
pDef->SetType(eType); |
| 174 |
return pDef; |
| 175 |
} |
| 176 |
|
| 177 |
// close global chain |
| 178 |
|
| 179 |
bool SbiParser::HasGlobalCode() |
| 180 |
{ |
| 181 |
if( bGblDefs && nGblChain ) |
| 182 |
{ |
| 183 |
aGen.BackChain( nGblChain ); |
| 184 |
aGen.Gen( SbiOpcode::LEAVE_ ); |
| 185 |
nGblChain = 0; |
| 186 |
} |
| 187 |
return bGblDefs; |
| 188 |
} |
| 189 |
|
| 190 |
void SbiParser::OpenBlock( SbiToken eTok, SbiExprNode* pVar ) |
| 191 |
{ |
| 192 |
SbiParseStack* p = new SbiParseStack; |
| 193 |
p->eExitTok = (eTok == GET || eTok == LET || eTok == SET) ? PROPERTY : eTok; // #i109051 |
| 194 |
p->nChain = 0; |
| 195 |
p->pWithVar = pWithVar; |
| 196 |
p->pNext = pStack; |
| 197 |
pStack = p; |
| 198 |
pWithVar = pVar; |
| 199 |
|
| 200 |
// #29955 service the for-loop level |
| 201 |
if( eTok == FOR ) |
| 202 |
aGen.IncForLevel(); |
| 203 |
} |
| 204 |
|
| 205 |
void SbiParser::CloseBlock() |
| 206 |
{ |
| 207 |
if( !pStack ) |
| 208 |
return; |
| 209 |
|
| 210 |
SbiParseStack* p = pStack; |
| 211 |
|
| 212 |
// #29955 service the for-loop level |
| 213 |
if( p->eExitTok == FOR ) |
| 214 |
aGen.DecForLevel(); |
| 215 |
|
| 216 |
aGen.BackChain( p->nChain ); |
| 217 |
pStack = p->pNext; |
| 218 |
pWithVar = p->pWithVar; |
| 219 |
delete p; |
| 220 |
} |
| 221 |
|
| 222 |
// EXIT ... |
| 223 |
|
| 224 |
void SbiParser::Exit() |
| 225 |
{ |
| 226 |
SbiToken eTok = Next(); |
| 227 |
for( SbiParseStack* p = pStack; p; p = p->pNext ) |
| 228 |
{ |
| 229 |
if (eTok == p->eExitTok) |
| 230 |
{ |
| 231 |
p->nChain = aGen.Gen( SbiOpcode::JUMP_, p->nChain ); |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
if( pStack ) |
| 236 |
Error( ERRCODE_BASIC_EXPECTED, pStack->eExitTok ); |
| 237 |
else |
| 238 |
Error( ERRCODE_BASIC_BAD_EXIT ); |
| 239 |
} |
| 240 |
|
| 241 |
bool SbiParser::TestSymbol() |
| 242 |
{ |
| 243 |
Peek(); |
| 244 |
if( eCurTok == SYMBOL ) |
| 245 |
{ |
| 246 |
Next(); return true; |
| 247 |
} |
| 248 |
Error( ERRCODE_BASIC_SYMBOL_EXPECTED ); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
bool SbiParser::TestToken( SbiToken t ) |
| 254 |
{ |
| 255 |
if( Peek() == t ) |
| 256 |
{ |
| 257 |
Next(); return true; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
Error( ERRCODE_BASIC_EXPECTED, t ); |
| 262 |
return false; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
bool SbiParser::TestComma() |
| 268 |
{ |
| 269 |
SbiToken eTok = Peek(); |
| 270 |
if( IsEoln( eTok ) ) |
| 271 |
{ |
| 272 |
Next(); |
| 273 |
return false; |
| 274 |
} |
| 275 |
else if( eTok != COMMA ) |
| 276 |
{ |
| 277 |
Error( ERRCODE_BASIC_EXPECTED, COMMA ); |
| 278 |
return false; |
| 279 |
} |
| 280 |
Next(); |
| 281 |
return true; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
void SbiParser::TestEoln() |
| 286 |
{ |
| 287 |
if( !IsEoln( Next() ) ) |
| 288 |
{ |
| 289 |
Error( ERRCODE_BASIC_EXPECTED, EOLN ); |
| 290 |
while( !IsEoln( Next() ) ) {} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
void SbiParser::StmntBlock( SbiToken eEnd ) |
| 296 |
{ |
| 297 |
SbiToken xe = eEndTok; |
| 298 |
eEndTok = eEnd; |
| 299 |
while( !bAbort && Parse() ) {} |
| 300 |
eEndTok = xe; |
| 301 |
if( IsEof() ) |
| 302 |
{ |
| 303 |
Error( ERRCODE_BASIC_BAD_BLOCK, eEnd ); |
| 304 |
bAbort = true; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void SbiParser::SetCodeCompleting( bool b ) |
| 309 |
{ |
| 310 |
bCodeCompleting = b; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
bool SbiParser::Parse() |
| 315 |
{ |
| 316 |
if( bAbort ) return false; |
| 317 |
|
| 318 |
EnableErrors(); |
| 319 |
|
| 320 |
bErrorIsSymbol = false; |
| 321 |
Peek(); |
| 322 |
bErrorIsSymbol = true; |
| 323 |
|
| 324 |
if( IsEof() ) |
| 325 |
{ |
| 326 |
// AB #33133: If no sub has been created before, |
| 327 |
// the global chain must be closed here! |
| 328 |
// AB #40689: Due to the new static-handling there |
| 329 |
// can be another nGblChain, so ask for it before. |
| 330 |
if( bNewGblDefs && nGblChain == 0 ) |
| 331 |
nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 ); |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
if( IsEoln( eCurTok ) ) |
| 337 |
{ |
| 338 |
Next(); return true; |
| 339 |
} |
| 340 |
|
| 341 |
if( !bSingleLineIf && MayBeLabel( true ) ) |
| 342 |
{ |
| 343 |
// is a label |
| 344 |
if( !pProc ) |
| 345 |
Error( ERRCODE_BASIC_NOT_IN_MAIN, aSym ); |
| 346 |
else |
| 347 |
pProc->GetLabels().Define( aSym ); |
| 348 |
Next(); Peek(); |
| 349 |
|
| 350 |
if( IsEoln( eCurTok ) ) |
| 351 |
{ |
| 352 |
Next(); return true; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// end of parsing? |
| 357 |
if( eCurTok == eEndTok || |
| 358 |
( bVBASupportOn && // #i109075 |
| 359 |
eEndTok == ENDPROPERTY && |
| 360 |
(pProc && pProc->getPropertyMode() == PropertyMode::Get) && |
| 361 |
( eCurTok == ENDFUNC || eCurTok == ENDSUB) ) ) |
| 362 |
{ |
| 363 |
Next(); |
| 364 |
if( eCurTok != NIL ) |
| 365 |
aGen.Statement(); |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
// comment? |
| 370 |
if( eCurTok == REM ) |
| 371 |
{ |
| 372 |
Next(); return true; |
| 373 |
} |
| 374 |
|
| 375 |
// In vba it's possible to do Error.foobar ( even if it results in |
| 376 |
// a runtime error |
| 377 |
if ( eCurTok == ERROR_ && IsVBASupportOn() ) // we probably need to define a subset of keywords where this madness applies e.g. if ( IsVBASupportOn() && SymbolCanBeRedined( eCurTok ) ) |
| 378 |
{ |
| 379 |
SbiTokenizer tokens( *this ); |
| 380 |
tokens.Next(); |
| 381 |
if ( tokens.Peek() == DOT ) |
| 382 |
{ |
| 383 |
eCurTok = SYMBOL; |
| 384 |
ePush = eCurTok; |
| 385 |
} |
| 386 |
} |
| 387 |
// if there's a symbol, it's either a variable (LET) |
| 388 |
// or a SUB-procedure (CALL without brackets) |
| 389 |
// DOT for assignments in the WITH-block: .A=5 |
| 390 |
if( eCurTok == SYMBOL || eCurTok == DOT ) |
| 391 |
{ |
| 392 |
if( !pProc ) |
| 393 |
Error( ERRCODE_BASIC_EXPECTED, SUB ); |
| 394 |
else |
| 395 |
{ |
| 396 |
// for correct line and column... |
| 397 |
Next(); |
| 398 |
Push( eCurTok ); |
| 399 |
aGen.Statement(); |
| 400 |
Symbol(nullptr); |
| 401 |
} |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
Next(); |
| 406 |
|
| 407 |
// statement parsers |
| 408 |
|
| 409 |
const auto p = std::find_if(std::begin(StmntTable), std::end(StmntTable), |
| 410 |
[this](auto element) { return element.eTok == eCurTok; }); |
| 411 |
if (p != std::end(StmntTable)) |
| 412 |
{ |
| 413 |
if( !pProc && !p->bMain ) |
| 414 |
Error( ERRCODE_BASIC_NOT_IN_MAIN, eCurTok ); |
| 415 |
else if( pProc && !p->bSubr ) |
| 416 |
Error( ERRCODE_BASIC_NOT_IN_SUBR, eCurTok ); |
| 417 |
else |
| 418 |
{ |
| 419 |
// AB #41606/#40689: Due to the new static-handling there |
| 420 |
// can be another nGblChain, so ask for it before. |
| 421 |
if( bNewGblDefs && nGblChain == 0 && |
| 422 |
( eCurTok == SUB || eCurTok == FUNCTION || eCurTok == PROPERTY ) ) |
| 423 |
{ |
| 424 |
nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 ); |
| 425 |
bNewGblDefs = false; |
| 426 |
} |
| 427 |
// statement-opcode at the beginning of a sub, too, please |
| 428 |
if( ( p->bSubr && (eCurTok != STATIC || Peek() == SUB || Peek() == FUNCTION ) ) || |
| 429 |
eCurTok == SUB || eCurTok == FUNCTION ) |
| 430 |
aGen.Statement(); |
| 431 |
(this->*( p->Func ) )(); |
| 432 |
ErrCode nSbxErr = SbxBase::GetError(); |
| 433 |
if( nSbxErr ) |
| 434 |
{ |
| 435 |
SbxBase::ResetError(); |
| 436 |
Error( nSbxErr ); |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
else |
| 441 |
Error( ERRCODE_BASIC_UNEXPECTED, eCurTok ); |
| 442 |
} |
| 443 |
|
| 444 |
// test for the statement's end - |
| 445 |
// might also be an ELSE, as there must not necessary be a : before the ELSE! |
| 446 |
|
| 447 |
if( !IsEos() ) |
| 448 |
{ |
| 449 |
Peek(); |
| 450 |
if( !IsEos() && eCurTok != ELSE ) |
| 451 |
{ |
| 452 |
// if the parsing has been aborted, jump over to the ":" |
| 453 |
Error( ERRCODE_BASIC_UNEXPECTED, eCurTok ); |
| 454 |
while( !IsEos() ) Next(); |
| 455 |
} |
| 456 |
} |
| 457 |
// The parser aborts at the end, the |
| 458 |
// next token has not been fetched yet! |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
SbiExprNode* SbiParser::GetWithVar() |
| 464 |
{ |
| 465 |
if( pWithVar ) |
| 466 |
return pWithVar; |
| 467 |
|
| 468 |
SbiParseStack* p = pStack; |
| 469 |
while( p ) |
| 470 |
{ |
| 471 |
// LoopVar can at the moment only be for with |
| 472 |
if( p->pWithVar ) |
| 473 |
return p->pWithVar; |
| 474 |
p = p->pNext; |
| 475 |
} |
| 476 |
return nullptr; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
// assignment or subroutine call |
| 481 |
|
| 482 |
void SbiParser::Symbol( const KeywordSymbolInfo* pKeywordSymbolInfo ) |
| 483 |
{ |
| 484 |
SbiExprMode eMode = bVBASupportOn ? EXPRMODE_STANDALONE : EXPRMODE_STANDARD; |
| 485 |
SbiExpression aVar( this, SbSYMBOL, eMode, pKeywordSymbolInfo ); |
| 486 |
|
| 487 |
bool bEQ = ( Peek() == EQ ); |
| 488 |
if( !bEQ && bVBASupportOn && aVar.IsBracket() ) |
| 489 |
Error( ERRCODE_BASIC_EXPECTED, u"="_ustr ); |
| 490 |
|
| 491 |
RecursiveMode eRecMode = ( bEQ ? PREVENT_CALL : FORCE_CALL ); |
| 492 |
bool bSpecialMidHandling = false; |
| 493 |
SbiSymDef* pDef = aVar.GetRealVar(); |
| 494 |
if( bEQ && pDef && pDef->GetScope() == SbRTL ) |
| 495 |
{ |
| 496 |
OUString aRtlName = pDef->GetName(); |
| 497 |
if( aRtlName.equalsIgnoreAsciiCase("Mid") ) |
| 498 |
{ |
| 499 |
SbiExprNode* pExprNode = aVar.GetExprNode(); |
| 500 |
if( pExprNode && pExprNode->GetNodeType() == SbxVARVAL ) |
| 501 |
{ |
| 502 |
SbiExprList* pPar = pExprNode->GetParameters(); |
| 503 |
short nParCount = pPar ? pPar->GetSize() : 0; |
| 504 |
if( nParCount == 2 || nParCount == 3 ) |
| 505 |
{ |
| 506 |
if( nParCount == 2 ) |
| 507 |
pPar->addExpression( std::make_unique<SbiExpression>( this, -1, SbxLONG ) ); |
| 508 |
|
| 509 |
TestToken( EQ ); |
| 510 |
pPar->addExpression( std::make_unique<SbiExpression>( this ) ); |
| 511 |
|
| 512 |
bSpecialMidHandling = true; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
aVar.Gen( eRecMode ); |
| 518 |
if( bSpecialMidHandling ) |
| 519 |
return; |
| 520 |
|
| 521 |
if( !bEQ ) |
| 522 |
{ |
| 523 |
aGen.Gen( SbiOpcode::GET_ ); |
| 524 |
} |
| 525 |
else |
| 526 |
{ |
| 527 |
// so it must be an assignment! |
| 528 |
if( !aVar.IsLvalue() ) |
| 529 |
Error( ERRCODE_BASIC_LVALUE_EXPECTED ); |
| 530 |
TestToken( EQ ); |
| 531 |
SbiExpression aExpr( this ); |
| 532 |
aExpr.Gen(); |
| 533 |
SbiOpcode eOp = SbiOpcode::PUT_; |
| 534 |
if( pDef ) |
| 535 |
{ |
| 536 |
if( pDef->GetConstDef() ) |
| 537 |
Error( ERRCODE_BASIC_DUPLICATE_DEF, pDef->GetName() ); |
| 538 |
if( pDef->GetType() == SbxOBJECT ) |
| 539 |
{ |
| 540 |
eOp = SbiOpcode::SET_; |
| 541 |
if( pDef->GetTypeId() ) |
| 542 |
{ |
| 543 |
aGen.Gen( SbiOpcode::SETCLASS_, pDef->GetTypeId() ); |
| 544 |
return; |
| 545 |
} |
| 546 |
} |
| 547 |
else if (auto nLen = pDef->GetLen()) // Dim s As String * 123 ' 123 -> nLen |
| 548 |
{ |
| 549 |
aGen.Gen(SbiOpcode::PAD_, nLen); |
| 550 |
} |
| 551 |
} |
| 552 |
aGen.Gen( eOp ); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
void SbiParser::Assign() |
| 558 |
{ |
| 559 |
SbiExpression aLvalue( this, SbLVALUE ); |
| 560 |
TestToken( EQ ); |
| 561 |
SbiExpression aExpr( this ); |
| 562 |
aLvalue.Gen(); |
| 563 |
aExpr.Gen(); |
| 564 |
if (SbiSymDef* pDef = aLvalue.GetRealVar()) |
| 565 |
{ |
| 566 |
if( pDef->GetConstDef() ) |
| 567 |
Error( ERRCODE_BASIC_DUPLICATE_DEF, pDef->GetName() ); |
| 568 |
if (auto nLen = pDef->GetLen()) // Dim s As String * 123 ' 123 -> nLen |
| 569 |
{ |
| 570 |
aGen.Gen( SbiOpcode::PAD_, nLen ); |
| 571 |
} |
| 572 |
} |
| 573 |
aGen.Gen( SbiOpcode::PUT_ ); |
| 574 |
} |
| 575 |
|
| 576 |
// assignments of an object-variable |
| 577 |
|
| 578 |
void SbiParser::Set() |
| 579 |
{ |
| 580 |
SbiExpression aLvalue( this, SbLVALUE ); |
| 581 |
SbxDataType eType = aLvalue.GetType(); |
| 582 |
if( eType != SbxOBJECT && eType != SbxEMPTY && eType != SbxVARIANT ) |
| 583 |
Error( ERRCODE_BASIC_INVALID_OBJECT ); |
| 584 |
TestToken( EQ ); |
| 585 |
SbiSymDef* pDef = aLvalue.GetRealVar(); |
| 586 |
if( pDef->GetConstDef() ) |
| 587 |
Error( ERRCODE_BASIC_DUPLICATE_DEF, pDef->GetName() ); |
| 588 |
|
| 589 |
SbiToken eTok = Peek(); |
| 590 |
if( eTok == NEW ) |
| 591 |
{ |
| 592 |
Next(); |
| 593 |
SbiSymDef aTypeDef( u""_ustr ); |
| 594 |
TypeDecl( aTypeDef, true ); |
| 595 |
|
| 596 |
aLvalue.Gen(); |
| 597 |
aGen.Gen( SbiOpcode::CREATE_, pDef->GetId(), aTypeDef.GetTypeId() ); |
| 598 |
aGen.Gen( SbiOpcode::SETCLASS_, pDef->GetTypeId() ); |
| 599 |
} |
| 600 |
else |
| 601 |
{ |
| 602 |
SbiExpression aExpr( this ); |
| 603 |
aLvalue.Gen(); |
| 604 |
aExpr.Gen(); |
| 605 |
// It's a good idea to distinguish between |
| 606 |
// set something = another & |
| 607 |
// something = another |
| 608 |
// ( it's necessary for vba objects where set is object |
| 609 |
// specific and also doesn't involve processing default params ) |
| 610 |
if( pDef->GetTypeId() ) |
| 611 |
{ |
| 612 |
if ( bVBASupportOn ) |
| 613 |
aGen.Gen( SbiOpcode::VBASETCLASS_, pDef->GetTypeId() ); |
| 614 |
else |
| 615 |
aGen.Gen( SbiOpcode::SETCLASS_, pDef->GetTypeId() ); |
| 616 |
} |
| 617 |
else |
| 618 |
{ |
| 619 |
if ( bVBASupportOn ) |
| 620 |
aGen.Gen( SbiOpcode::VBASET_ ); |
| 621 |
else |
| 622 |
aGen.Gen( SbiOpcode::SET_ ); |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
// JSM 07.10.95 |
| 628 |
void SbiParser::LSet() |
| 629 |
{ |
| 630 |
SbiExpression aLvalue( this, SbLVALUE ); |
| 631 |
if( aLvalue.GetType() != SbxSTRING ) |
| 632 |
{ |
| 633 |
Error( ERRCODE_BASIC_INVALID_OBJECT ); |
| 634 |
} |
| 635 |
TestToken( EQ ); |
| 636 |
SbiSymDef* pDef = aLvalue.GetRealVar(); |
| 637 |
if( pDef && pDef->GetConstDef() ) |
| 638 |
{ |
| 639 |
Error( ERRCODE_BASIC_DUPLICATE_DEF, pDef->GetName() ); |
| 640 |
} |
| 641 |
SbiExpression aExpr( this ); |
| 642 |
aLvalue.Gen(); |
| 643 |
aExpr.Gen(); |
| 644 |
aGen.Gen( SbiOpcode::LSET_ ); |
| 645 |
} |
| 646 |
|
| 647 |
// JSM 07.10.95 |
| 648 |
void SbiParser::RSet() |
| 649 |
{ |
| 650 |
SbiExpression aLvalue( this, SbLVALUE ); |
| 651 |
if( aLvalue.GetType() != SbxSTRING ) |
| 652 |
{ |
| 653 |
Error( ERRCODE_BASIC_INVALID_OBJECT ); |
| 654 |
} |
| 655 |
TestToken( EQ ); |
| 656 |
SbiSymDef* pDef = aLvalue.GetRealVar(); |
| 657 |
if( pDef && pDef->GetConstDef() ) |
| 658 |
Error( ERRCODE_BASIC_DUPLICATE_DEF, pDef->GetName() ); |
| 659 |
SbiExpression aExpr( this ); |
| 660 |
aLvalue.Gen(); |
| 661 |
aExpr.Gen(); |
| 662 |
aGen.Gen( SbiOpcode::RSET_ ); |
| 663 |
} |
| 664 |
|
| 665 |
// DEFINT, DEFLNG, DEFSNG, DEFDBL, DEFSTR and so on |
| 666 |
|
| 667 |
void SbiParser::DefXXX() |
| 668 |
{ |
| 669 |
sal_Unicode ch1, ch2; |
| 670 |
SbxDataType t = SbxDataType( eCurTok - DEFINT + SbxINTEGER ); |
| 671 |
|
| 672 |
while( !bAbort ) |
| 673 |
{ |
| 674 |
if( Next() != SYMBOL ) break; |
| 675 |
ch1 = rtl::toAsciiUpperCase(aSym[0]); |
| 676 |
ch2 = 0; |
| 677 |
if( Peek() == MINUS ) |
| 678 |
{ |
| 679 |
Next(); |
| 680 |
if( Next() != SYMBOL ) Error( ERRCODE_BASIC_SYMBOL_EXPECTED ); |
| 681 |
else |
| 682 |
{ |
| 683 |
ch2 = rtl::toAsciiUpperCase(aSym[0]); |
| 684 |
if( ch2 < ch1 ) |
| 685 |
{ |
| 686 |
Error( ERRCODE_BASIC_SYNTAX ); |
| 687 |
ch2 = 0; |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
if (!ch2) ch2 = ch1; |
| 692 |
ch1 -= 'A'; ch2 -= 'A'; |
| 693 |
for (; ch1 <= ch2; ch1++) eDefTypes[ ch1 ] = t; |
| 694 |
if( !TestComma() ) break; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
// STOP/SYSTEM |
| 699 |
|
| 700 |
void SbiParser::Stop() |
| 701 |
{ |
| 702 |
aGen.Gen( SbiOpcode::STOP_ ); |
| 703 |
Peek(); // #35694: only Peek(), so that EOL is recognized in Single-Line-If |
| 704 |
} |
| 705 |
|
| 706 |
// IMPLEMENTS |
| 707 |
|
| 708 |
void SbiParser::Implements() |
| 709 |
{ |
| 710 |
if( !bClassModule ) |
| 711 |
{ |
| 712 |
Error( ERRCODE_BASIC_UNEXPECTED, IMPLEMENTS ); |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
Peek(); |
| 717 |
if( eCurTok != SYMBOL ) |
| 718 |
{ |
| 719 |
Error( ERRCODE_BASIC_SYMBOL_EXPECTED ); |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
OUString aImplementedIface = aSym; |
| 724 |
Next(); |
| 725 |
if( Peek() == DOT ) |
| 726 |
{ |
| 727 |
OUString aDotStr( '.' ); |
| 728 |
while( Peek() == DOT ) |
| 729 |
{ |
| 730 |
aImplementedIface += aDotStr; |
| 731 |
Next(); |
| 732 |
SbiToken ePeekTok = Peek(); |
| 733 |
if( ePeekTok == SYMBOL || IsKwd( ePeekTok ) ) |
| 734 |
{ |
| 735 |
Next(); |
| 736 |
aImplementedIface += aSym; |
| 737 |
} |
| 738 |
else |
| 739 |
{ |
| 740 |
Next(); |
| 741 |
Error( ERRCODE_BASIC_SYMBOL_EXPECTED ); |
| 742 |
break; |
| 743 |
} |
| 744 |
} |
| 745 |
} |
| 746 |
aIfaceVector.push_back( aImplementedIface ); |
| 747 |
} |
| 748 |
|
| 749 |
void SbiParser::EnableCompatibility() |
| 750 |
{ |
| 751 |
if( !bCompatible ) |
| 752 |
AddConstants(); |
| 753 |
bCompatible = true; |
| 754 |
} |
| 755 |
|
| 756 |
// OPTION |
| 757 |
|
| 758 |
void SbiParser::Option() |
| 759 |
{ |
| 760 |
switch( Next() ) |
| 761 |
{ |
| 762 |
case BASIC_EXPLICIT: |
| 763 |
bExplicit = true; break; |
| 764 |
case BASE: |
| 765 |
if( Next() == NUMBER && ( nVal == 0 || nVal == 1 ) ) |
| 766 |
{ |
| 767 |
nBase = static_cast<short>(nVal); |
| 768 |
break; |
| 769 |
} |
| 770 |
Error( ERRCODE_BASIC_EXPECTED, u"0/1"_ustr ); |
| 771 |
break; |
| 772 |
case PRIVATE: |
| 773 |
{ |
| 774 |
OUString aString = SbiTokenizer::Symbol(Next()); |
| 775 |
if( !aString.equalsIgnoreAsciiCase("Module") ) |
| 776 |
{ |
| 777 |
Error( ERRCODE_BASIC_EXPECTED, u"Module"_ustr ); |
| 778 |
} |
| 779 |
break; |
| 780 |
} |
| 781 |
case COMPARE: |
| 782 |
{ |
| 783 |
SbiToken eTok = Next(); |
| 784 |
if( eTok == BINARY ) |
| 785 |
{ |
| 786 |
} |
| 787 |
else if( eTok == SYMBOL && GetSym().equalsIgnoreAsciiCase("text") ) |
| 788 |
{ |
| 789 |
} |
| 790 |
else |
| 791 |
{ |
| 792 |
Error( ERRCODE_BASIC_EXPECTED, u"Text/Binary"_ustr ); |
| 793 |
} |
| 794 |
break; |
| 795 |
} |
| 796 |
case COMPATIBLE: |
| 797 |
EnableCompatibility(); |
| 798 |
break; |
| 799 |
|
| 800 |
case CLASSMODULE: |
| 801 |
bClassModule = true; |
| 802 |
aGen.GetModule().SetModuleType( css::script::ModuleType::CLASS ); |
| 803 |
break; |
| 804 |
case VBASUPPORT: // Option VBASupport used to override the module mode ( in fact this must reset the mode |
| 805 |
if( Next() == NUMBER ) |
| 806 |
{ |
| 807 |
if ( nVal == 1 || nVal == 0 ) |
| 808 |
{ |
| 809 |
bVBASupportOn = ( nVal == 1 ); |
| 810 |
if ( bVBASupportOn ) |
| 811 |
{ |
| 812 |
EnableCompatibility(); |
| 813 |
} |
| 814 |
// if the module setting is different |
| 815 |
// reset it to what the Option tells us |
| 816 |
if ( bVBASupportOn != aGen.GetModule().IsVBASupport() ) |
| 817 |
{ |
| 818 |
aGen.GetModule().SetVBASupport( bVBASupportOn ); |
| 819 |
} |
| 820 |
break; |
| 821 |
} |
| 822 |
} |
| 823 |
Error( ERRCODE_BASIC_EXPECTED, u"0/1"_ustr ); |
| 824 |
break; |
| 825 |
default: |
| 826 |
Error( ERRCODE_BASIC_BAD_OPTION, eCurTok ); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
static void addStringConst( SbiSymPool& rPool, const OUString& pSym, const OUString& rStr ) |
| 831 |
{ |
| 832 |
SbiConstDef* pConst = new SbiConstDef( pSym ); |
| 833 |
pConst->SetType( SbxSTRING ); |
| 834 |
pConst->Set( rStr ); |
| 835 |
rPool.Add( pConst ); |
| 836 |
} |
| 837 |
|
| 838 |
static void addNumericConst(SbiSymPool& rPool, const OUString& pSym, double nVal) |
| 839 |
{ |
| 840 |
SbiConstDef* pConst = new SbiConstDef(pSym); |
| 841 |
pConst->Set(nVal, SbxDOUBLE); |
| 842 |
rPool.Add(pConst); |
| 843 |
} |
| 844 |
|
| 845 |
void SbiParser::AddConstants() |
| 846 |
{ |
| 847 |
// tdf#153543 - shell constants |
| 848 |
// See https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/shell-constants |
| 849 |
addNumericConst(aPublics, u"vbHide"_ustr, 0); |
| 850 |
addNumericConst(aPublics, u"vbNormalFocus"_ustr, 1); |
| 851 |
addNumericConst(aPublics, u"vbMinimizedFocus"_ustr, 2); |
| 852 |
addNumericConst(aPublics, u"vbMaximizedFocus"_ustr, 3); |
| 853 |
addNumericConst(aPublics, u"vbNormalNoFocus"_ustr, 4); |
| 854 |
addNumericConst(aPublics, u"vbMinimizedNoFocus"_ustr, 6); |
| 855 |
|
| 856 |
// tdf#131563 - add vba color constants |
| 857 |
// See https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/color-constants |
| 858 |
addNumericConst(aPublics, u"vbBlack"_ustr, 0x0); |
| 859 |
addNumericConst(aPublics, u"vbRed"_ustr, 0xFF); |
| 860 |
addNumericConst(aPublics, u"vbGreen"_ustr, 0xFF00); |
| 861 |
addNumericConst(aPublics, u"vbYellow"_ustr, 0xFFFF); |
| 862 |
addNumericConst(aPublics, u"vbBlue"_ustr, 0xFF0000); |
| 863 |
addNumericConst(aPublics, u"vbMagenta"_ustr, 0xFF00FF); |
| 864 |
addNumericConst(aPublics, u"vbCyan"_ustr, 0xFFFF00); |
| 865 |
addNumericConst(aPublics, u"vbWhite"_ustr, 0xFFFFFF); |
| 866 |
|
| 867 |
// #113063 Create constant RTL symbols |
| 868 |
addStringConst( aPublics, u"vbCr"_ustr, u"\x0D"_ustr ); |
| 869 |
addStringConst( aPublics, u"vbCrLf"_ustr, u"\x0D\x0A"_ustr ); |
| 870 |
addStringConst( aPublics, u"vbFormFeed"_ustr, u"\x0C"_ustr ); |
| 871 |
addStringConst( aPublics, u"vbLf"_ustr, u"\x0A"_ustr ); |
| 872 |
#ifdef _WIN32 |
| 873 |
addStringConst( aPublics, "vbNewLine", "\x0D\x0A" ); |
| 874 |
#else |
| 875 |
addStringConst( aPublics, u"vbNewLine"_ustr, u"\x0A"_ustr ); |
| 876 |
#endif |
| 877 |
addStringConst( aPublics, u"vbNullString"_ustr, u""_ustr ); |
| 878 |
addStringConst( aPublics, u"vbTab"_ustr, u"\x09"_ustr ); |
| 879 |
addStringConst( aPublics, u"vbVerticalTab"_ustr, u"\x0B"_ustr ); |
| 880 |
|
| 881 |
addStringConst( aPublics, u"vbNullChar"_ustr, OUString(u'\0') ); |
| 882 |
} |
| 883 |
|
| 884 |
// ERROR n |
| 885 |
|
| 886 |
void SbiParser::ErrorStmnt() |
| 887 |
{ |
| 888 |
SbiExpression aPar( this ); |
| 889 |
aPar.Gen(); |
| 890 |
aGen.Gen( SbiOpcode::ERROR_ ); |
| 891 |
} |
| 892 |
|
| 893 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |