| 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 <basiccharclass.hxx> |
| 21 |
#include <scanner.hxx> |
| 22 |
#include <sbintern.hxx> |
| 23 |
#include <runtime.hxx> |
| 24 |
|
| 25 |
#include <basic/sberrors.hxx> |
| 26 |
#include <i18nlangtag/lang.h> |
| 27 |
#include <svl/numformat.hxx> |
| 28 |
#include <svl/zforlist.hxx> |
| 29 |
#include <rtl/character.hxx> |
| 30 |
#include <o3tl/string_view.hxx> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
SbiScanner::SbiScanner(OUString _aBuf, StarBASIC* p) |
| 34 |
: aBuf(std::move(_aBuf)) |
| 35 |
, nLineIdx(-1) |
| 36 |
, nSaveLineIdx(-1) |
| 37 |
, pBasic(p) |
| 38 |
, eScanType(SbxVARIANT) |
| 39 |
, nVal(0) |
| 40 |
, nSavedCol1(0) |
| 41 |
, nCol(0) |
| 42 |
, nErrors(0) |
| 43 |
, nColLock(0) |
| 44 |
, nBufPos(0) |
| 45 |
, nLine(0) |
| 46 |
, nCol1(0) |
| 47 |
, nCol2(0) |
| 48 |
, bSymbol(false) |
| 49 |
, bNumber(false) |
| 50 |
, bSpaces(false) |
| 51 |
, bAbort(false) |
| 52 |
, bHash(true) |
| 53 |
, bError(false) |
| 54 |
, bCompatible(false) |
| 55 |
, bVBASupportOn(false) |
| 56 |
, bPrevLineExtentsComment(false) |
| 57 |
, bClosingUnderscore(false) |
| 58 |
, bLineEndsWithWhitespace(false) |
| 59 |
, bInStatement(false) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
void SbiScanner::LockColumn() |
| 64 |
{ |
| 65 |
if( !nColLock++ ) |
| 66 |
nSavedCol1 = nCol1; |
| 67 |
} |
| 68 |
|
| 69 |
void SbiScanner::UnlockColumn() |
| 70 |
{ |
| 71 |
if( nColLock ) |
| 72 |
nColLock--; |
| 73 |
} |
| 74 |
|
| 75 |
void SbiScanner::GenError( ErrCode code ) |
| 76 |
{ |
| 77 |
if( GetSbData()->bBlockCompilerError ) |
| 78 |
{ |
| 79 |
bAbort = true; |
| 80 |
return; |
| 81 |
} |
| 82 |
if( !bError ) |
| 83 |
{ |
| 84 |
bool bRes = true; |
| 85 |
// report only one error per statement |
| 86 |
bError = true; |
| 87 |
if( pBasic ) |
| 88 |
{ |
| 89 |
// in case of EXPECTED or UNEXPECTED it always refers |
| 90 |
// to the last token, so take the Col1 over |
| 91 |
sal_Int32 nc = nColLock ? nSavedCol1 : nCol1; |
| 92 |
if ( code.anyOf( |
| 93 |
ERRCODE_BASIC_EXPECTED, |
| 94 |
ERRCODE_BASIC_UNEXPECTED, |
| 95 |
ERRCODE_BASIC_SYMBOL_EXPECTED, |
| 96 |
ERRCODE_BASIC_LABEL_EXPECTED) ) |
| 97 |
{ |
| 98 |
nc = nCol1; |
| 99 |
if( nc > nCol2 ) nCol2 = nc; |
| 100 |
} |
| 101 |
bRes = pBasic->CError( code, aError, nLine, nc, nCol2 ); |
| 102 |
} |
| 103 |
bAbort = bAbort || !bRes || ( code == ERRCODE_BASIC_NO_MEMORY || code == ERRCODE_BASIC_PROG_TOO_LARGE ); |
| 104 |
} |
| 105 |
nErrors++; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// used by SbiTokenizer::MayBeLabel() to detect a label |
| 110 |
bool SbiScanner::DoesColonFollow() |
| 111 |
{ |
| 112 |
if(nCol < aLine.getLength() && aLine[nCol] == ':') |
| 113 |
{ |
| 114 |
++nLineIdx; ++nCol; |
| 115 |
return true; |
| 116 |
} |
| 117 |
else |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
// test for legal suffix |
| 122 |
static SbxDataType GetSuffixType( sal_Unicode c ) |
| 123 |
{ |
| 124 |
switch (c) |
| 125 |
{ |
| 126 |
case '%': |
| 127 |
return SbxINTEGER; |
| 128 |
case '&': |
| 129 |
return SbxLONG; |
| 130 |
case '!': |
| 131 |
return SbxSINGLE; |
| 132 |
case '#': |
| 133 |
return SbxDOUBLE; |
| 134 |
case '@': |
| 135 |
return SbxCURRENCY; |
| 136 |
case '$': |
| 137 |
return SbxSTRING; |
| 138 |
default: |
| 139 |
return SbxVARIANT; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// reading the next symbol into the variables aSym, nVal and eType |
| 144 |
// return value is sal_False at EOF or errors |
| 145 |
|
| 146 |
void SbiScanner::scanAlphanumeric() |
| 147 |
{ |
| 148 |
sal_Int32 n = nCol; |
| 149 |
while(nCol < aLine.getLength() && (BasicCharClass::isAlphaNumeric(aLine[nCol], bCompatible) || aLine[nCol] == '_')) |
| 150 |
{ |
| 151 |
++nLineIdx; |
| 152 |
++nCol; |
| 153 |
} |
| 154 |
aSym = aLine.copy(n, nCol - n); |
| 155 |
} |
| 156 |
|
| 157 |
void SbiScanner::scanGoto() |
| 158 |
{ |
| 159 |
sal_Int32 n = nCol; |
| 160 |
while(n < aLine.getLength() && BasicCharClass::isWhitespace(aLine[n])) |
| 161 |
++n; |
| 162 |
|
| 163 |
if(n + 1 < aLine.getLength()) |
| 164 |
{ |
| 165 |
std::u16string_view aTemp = aLine.subView(n, 2); |
| 166 |
if(o3tl::equalsIgnoreAsciiCase(aTemp, u"to")) |
| 167 |
{ |
| 168 |
aSym = "goto"; |
| 169 |
nLineIdx += n + 2 - nCol; |
| 170 |
nCol = n + 2; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
bool SbiScanner::readLine() |
| 176 |
{ |
| 177 |
if(nBufPos >= aBuf.getLength()) |
| 178 |
return false; |
| 179 |
|
| 180 |
sal_Int32 n = nBufPos; |
| 181 |
sal_Int32 nLen = aBuf.getLength(); |
| 182 |
|
| 183 |
while(n < nLen && aBuf[n] != '\r' && aBuf[n] != '\n') |
| 184 |
++n; |
| 185 |
|
| 186 |
// Trim trailing whitespace |
| 187 |
sal_Int32 nEnd = n; |
| 188 |
while(nBufPos < nEnd && BasicCharClass::isWhitespace(aBuf[nEnd - 1])) |
| 189 |
--nEnd; |
| 190 |
|
| 191 |
// tdf#149402 - check if line ends with a whitespace |
| 192 |
bLineEndsWithWhitespace = (n > nEnd); |
| 193 |
aLine = aBuf.copy(nBufPos, nEnd - nBufPos); |
| 194 |
|
| 195 |
// Fast-forward past the line ending |
| 196 |
if(n + 1 < nLen && aBuf[n] == '\r' && aBuf[n + 1] == '\n') |
| 197 |
n += 2; |
| 198 |
else if(n < nLen) |
| 199 |
++n; |
| 200 |
|
| 201 |
nBufPos = n; |
| 202 |
nLineIdx = 0; |
| 203 |
|
| 204 |
++nLine; |
| 205 |
nCol = nCol1 = nCol2 = 0; |
| 206 |
nColLock = 0; |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
// Function to check if a string is a valid compiler directive |
| 212 |
static bool isValidCompilerDirective(std::u16string_view directive) |
| 213 |
{ |
| 214 |
static constexpr std::string_view validDirectives[] |
| 215 |
= { "if", "elseif", "else", "end", "const" }; |
| 216 |
|
| 217 |
return std::any_of(std::begin(validDirectives), std::end(validDirectives), |
| 218 |
[&directive](const auto& valid) |
| 219 |
{ return o3tl::matchIgnoreAsciiCase(directive, valid); }); |
| 220 |
} |
| 221 |
|
| 222 |
bool SbiScanner::NextSym() |
| 223 |
{ |
| 224 |
constexpr sal_Int32 BUF_SIZE = 80; |
| 225 |
// memorize for the EOLN-case |
| 226 |
sal_Int32 nOldLine = nLine; |
| 227 |
sal_Int32 nOldCol1 = nCol1; |
| 228 |
sal_Int32 nOldCol2 = nCol2; |
| 229 |
sal_Unicode buf[ BUF_SIZE ], *p = buf; |
| 230 |
|
| 231 |
eScanType = SbxVARIANT; |
| 232 |
aSym.clear(); |
| 233 |
bHash = bSymbol = bNumber = bSpaces = false; |
| 234 |
|
| 235 |
// read in line? |
| 236 |
if (nLineIdx == -1) |
| 237 |
{ |
| 238 |
if(!readLine()) |
| 239 |
return false; |
| 240 |
|
| 241 |
nOldLine = nLine; |
| 242 |
nOldCol1 = nOldCol2 = 0; |
| 243 |
} |
| 244 |
|
| 245 |
const sal_Int32 nLineIdxScanStart = nLineIdx; |
| 246 |
|
| 247 |
if(nCol < aLine.getLength() && BasicCharClass::isWhitespace(aLine[nCol])) |
| 248 |
{ |
| 249 |
bSpaces = true; |
| 250 |
while(nCol < aLine.getLength() && BasicCharClass::isWhitespace(aLine[nCol])) |
| 251 |
{ |
| 252 |
++nLineIdx; |
| 253 |
++nCol; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
nCol1 = nCol; |
| 258 |
|
| 259 |
// only blank line? |
| 260 |
if(nCol >= aLine.getLength()) |
| 261 |
goto eoln; |
| 262 |
|
| 263 |
if( bPrevLineExtentsComment ) |
| 264 |
goto PrevLineCommentLbl; |
| 265 |
|
| 266 |
if(nCol < aLine.getLength() && aLine[nCol] == '#') |
| 267 |
{ |
| 268 |
sal_Int32 nLineTempIdx = nLineIdx; |
| 269 |
std::u16string_view candidate(aLine.subView(nCol + 1)); |
| 270 |
|
| 271 |
do |
| 272 |
{ |
| 273 |
nLineTempIdx++; |
| 274 |
} while (nLineTempIdx < aLine.getLength() && !BasicCharClass::isWhitespace(aLine[nLineTempIdx]) |
| 275 |
&& aLine[nLineTempIdx] != '#' && aLine[nLineTempIdx] != ','); |
| 276 |
// leave it if it is a date literal - it will be handled later |
| 277 |
if (nLineTempIdx >= aLine.getLength() || aLine[nLineTempIdx] != '#') |
| 278 |
{ |
| 279 |
++nLineIdx; |
| 280 |
++nCol; |
| 281 |
//handle compiler directives (# is first non-space character) |
| 282 |
if (nOldCol2 == 0) |
| 283 |
{ |
| 284 |
if (isValidCompilerDirective(candidate)) |
| 285 |
{ |
| 286 |
// Skip the whole line if starts with a hash and is a valid compiler directive |
| 287 |
nCol = 0; |
| 288 |
goto eoln; |
| 289 |
} |
| 290 |
else |
| 291 |
{ |
| 292 |
GenError(ERRCODE_BASIC_SYNTAX); |
| 293 |
} |
| 294 |
} |
| 295 |
else |
| 296 |
bHash = true; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
// copy character if symbol |
| 301 |
if(nCol < aLine.getLength() && (BasicCharClass::isAlpha(aLine[nCol], bCompatible) || aLine[nCol] == '_')) |
| 302 |
{ |
| 303 |
// if there's nothing behind '_' , it's the end of a line! |
| 304 |
if(nCol + 1 == aLine.getLength() && aLine[nCol] == '_') |
| 305 |
{ |
| 306 |
// Note that nCol is not incremented here... |
| 307 |
++nLineIdx; |
| 308 |
goto eoln; |
| 309 |
} |
| 310 |
|
| 311 |
bSymbol = true; |
| 312 |
|
| 313 |
scanAlphanumeric(); |
| 314 |
|
| 315 |
// Special handling for "go to" |
| 316 |
if(nCol < aLine.getLength() && bCompatible && aSym.equalsIgnoreAsciiCase("go")) |
| 317 |
scanGoto(); |
| 318 |
|
| 319 |
// tdf#125637 - check for closing underscore |
| 320 |
if (nCol == aLine.getLength() && aLine[nCol - 1] == '_') |
| 321 |
{ |
| 322 |
bClosingUnderscore = true; |
| 323 |
} |
| 324 |
// type recognition? |
| 325 |
// don't test the exclamation mark |
| 326 |
// if there's a symbol behind it |
| 327 |
else if((nCol >= aLine.getLength() || aLine[nCol] != '!') || |
| 328 |
(nCol + 1 >= aLine.getLength() || !BasicCharClass::isAlpha(aLine[nCol + 1], bCompatible))) |
| 329 |
{ |
| 330 |
if(nCol < aLine.getLength()) |
| 331 |
{ |
| 332 |
SbxDataType t(GetSuffixType(aLine[nCol])); |
| 333 |
if( t != SbxVARIANT ) |
| 334 |
{ |
| 335 |
eScanType = t; |
| 336 |
++nLineIdx; |
| 337 |
++nCol; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
// read in and convert if number |
| 344 |
else if((nCol < aLine.getLength() && rtl::isAsciiDigit(aLine[nCol])) || |
| 345 |
(nCol + 1 < aLine.getLength() && aLine[nCol] == '.' && rtl::isAsciiDigit(aLine[nCol + 1]))) |
| 346 |
{ |
| 347 |
short exp = 0; |
| 348 |
short dec = 0; |
| 349 |
eScanType = SbxDOUBLE; |
| 350 |
bool bScanError = false; |
| 351 |
bool bBufOverflow = false; |
| 352 |
// All this because of 'D' or 'd' floating point type, sigh... |
| 353 |
while(!bScanError && nCol < aLine.getLength() && strchr("0123456789.DEde", aLine[nCol])) |
| 354 |
{ |
| 355 |
// from 4.1.1996: buffer full? -> go on scanning empty |
| 356 |
if( (p-buf) == (BUF_SIZE-1) ) |
| 357 |
{ |
| 358 |
bBufOverflow = true; |
| 359 |
++nLineIdx; |
| 360 |
++nCol; |
| 361 |
continue; |
| 362 |
} |
| 363 |
// point or exponent? |
| 364 |
if(aLine[nCol] == '.') |
| 365 |
{ |
| 366 |
if( ++dec > 1 ) |
| 367 |
bScanError = true; |
| 368 |
else |
| 369 |
*p++ = '.'; |
| 370 |
} |
| 371 |
else if(strchr("DdEe", aLine[nCol])) |
| 372 |
{ |
| 373 |
if (++exp > 1) |
| 374 |
bScanError = true; |
| 375 |
else |
| 376 |
{ |
| 377 |
*p++ = 'E'; |
| 378 |
if (nCol + 1 < aLine.getLength() && (aLine[nCol+1] == '+' || aLine[nCol+1] == '-')) |
| 379 |
{ |
| 380 |
++nLineIdx; |
| 381 |
++nCol; |
| 382 |
if( (p-buf) == (BUF_SIZE-1) ) |
| 383 |
{ |
| 384 |
bBufOverflow = true; |
| 385 |
continue; |
| 386 |
} |
| 387 |
*p++ = aLine[nCol]; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
*p++ = aLine[nCol]; |
| 394 |
} |
| 395 |
++nLineIdx; |
| 396 |
++nCol; |
| 397 |
} |
| 398 |
*p = 0; |
| 399 |
aSym = p; bNumber = true; |
| 400 |
|
| 401 |
// For bad characters, scan and parse errors generate only one error. |
| 402 |
ErrCode nError = ERRCODE_NONE; |
| 403 |
if (bScanError) |
| 404 |
{ |
| 405 |
--nLineIdx; |
| 406 |
--nCol; |
| 407 |
aError = OUString( aLine[nCol]); |
| 408 |
nError = ERRCODE_BASIC_BAD_CHAR_IN_NUMBER; |
| 409 |
} |
| 410 |
|
| 411 |
rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; |
| 412 |
const sal_Unicode* pParseEnd = buf; |
| 413 |
nVal = rtl_math_uStringToDouble( buf, buf+(p-buf), '.', ',', &eStatus, &pParseEnd ); |
| 414 |
if (pParseEnd != buf+(p-buf)) |
| 415 |
{ |
| 416 |
// e.g. "12e" or "12e+", or with bScanError "12d"+"E". |
| 417 |
sal_Int32 nChars = buf+(p-buf) - pParseEnd; |
| 418 |
nLineIdx -= nChars; |
| 419 |
nCol -= nChars; |
| 420 |
// For bScanError, nLineIdx and nCol were already decremented, just |
| 421 |
// add that character to the parse end. |
| 422 |
if (bScanError) |
| 423 |
++nChars; |
| 424 |
// Copy error position from original string, not the buffer |
| 425 |
// replacement where "12dE" => "12EE". |
| 426 |
aError = aLine.copy( nCol, nChars); |
| 427 |
nError = ERRCODE_BASIC_BAD_CHAR_IN_NUMBER; |
| 428 |
} |
| 429 |
else if (eStatus != rtl_math_ConversionStatus_Ok) |
| 430 |
{ |
| 431 |
// Keep the scan error and character at position, if any. |
| 432 |
if (!nError) |
| 433 |
nError = ERRCODE_BASIC_MATH_OVERFLOW; |
| 434 |
} |
| 435 |
|
| 436 |
if (nError) |
| 437 |
GenError( nError ); |
| 438 |
|
| 439 |
if( !dec && !exp ) |
| 440 |
{ |
| 441 |
if( nVal >= SbxMININT && nVal <= SbxMAXINT ) |
| 442 |
eScanType = SbxINTEGER; |
| 443 |
else if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG ) |
| 444 |
eScanType = SbxLONG; |
| 445 |
} |
| 446 |
|
| 447 |
if( bBufOverflow ) |
| 448 |
GenError( ERRCODE_BASIC_MATH_OVERFLOW ); |
| 449 |
|
| 450 |
// type recognition? |
| 451 |
if( nCol < aLine.getLength() ) |
| 452 |
{ |
| 453 |
SbxDataType t(GetSuffixType(aLine[nCol])); |
| 454 |
if( t != SbxVARIANT ) |
| 455 |
{ |
| 456 |
eScanType = t; |
| 457 |
++nLineIdx; |
| 458 |
++nCol; |
| 459 |
} |
| 460 |
// tdf#130476 - don't allow String trailing data type character with numbers |
| 461 |
if ( t == SbxSTRING ) |
| 462 |
{ |
| 463 |
GenError( ERRCODE_BASIC_SYNTAX ); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
// Hex/octal number? Read in and convert: |
| 469 |
else if(aLine.getLength() - nCol > 1 && aLine[nCol] == '&') |
| 470 |
{ |
| 471 |
++nLineIdx; ++nCol; |
| 472 |
sal_Unicode base = 16; |
| 473 |
sal_Unicode xch = aLine[nCol]; |
| 474 |
++nLineIdx; ++nCol; |
| 475 |
switch( rtl::toAsciiUpperCase( xch ) ) |
| 476 |
{ |
| 477 |
case 'O': |
| 478 |
base = 8; |
| 479 |
break; |
| 480 |
case 'H': |
| 481 |
break; |
| 482 |
default : |
| 483 |
// treated as an operator |
| 484 |
--nLineIdx; --nCol; nCol1 = nCol-1; |
| 485 |
aSym = "&"; |
| 486 |
return true; |
| 487 |
} |
| 488 |
bNumber = true; |
| 489 |
// Hex literals are signed Integers ( as defined by basic |
| 490 |
// e.g. -2,147,483,648 through 2,147,483,647 (signed) |
| 491 |
sal_uInt64 lu = 0; |
| 492 |
bool bOverflow = false; |
| 493 |
while(nCol < aLine.getLength() && BasicCharClass::isAlphaNumeric(aLine[nCol], false)) |
| 494 |
{ |
| 495 |
sal_Unicode ch = rtl::toAsciiUpperCase(aLine[nCol]); |
| 496 |
++nLineIdx; ++nCol; |
| 497 |
if( ((base == 16 ) && rtl::isAsciiHexDigit( ch ) ) || |
| 498 |
((base == 8) && rtl::isAsciiOctalDigit( ch ))) |
| 499 |
{ |
| 500 |
int i = ch - '0'; |
| 501 |
if( i > 9 ) i -= 7; |
| 502 |
lu = ( lu * base ) + i; |
| 503 |
if( lu > SAL_MAX_UINT32 ) |
| 504 |
{ |
| 505 |
bOverflow = true; |
| 506 |
} |
| 507 |
} |
| 508 |
else |
| 509 |
{ |
| 510 |
aError = OUString(ch); |
| 511 |
GenError( ERRCODE_BASIC_BAD_CHAR_IN_NUMBER ); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
// tdf#130476 - take into account trailing data type characters |
| 516 |
if( nCol < aLine.getLength() ) |
| 517 |
{ |
| 518 |
SbxDataType t(GetSuffixType(aLine[nCol])); |
| 519 |
if( t != SbxVARIANT ) |
| 520 |
{ |
| 521 |
eScanType = t; |
| 522 |
++nLineIdx; |
| 523 |
++nCol; |
| 524 |
} |
| 525 |
// tdf#130476 - don't allow String trailing data type character with numbers |
| 526 |
if ( t == SbxSTRING ) |
| 527 |
{ |
| 528 |
GenError( ERRCODE_BASIC_SYNTAX ); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
// tdf#130476 - take into account trailing data type characters |
| 533 |
switch ( eScanType ) |
| 534 |
{ |
| 535 |
case SbxINTEGER: |
| 536 |
nVal = static_cast<double>( static_cast<sal_Int16>(lu) ); |
| 537 |
if ( lu > SbxMAXUINT ) |
| 538 |
{ |
| 539 |
bOverflow = true; |
| 540 |
} |
| 541 |
break; |
| 542 |
case SbxLONG: nVal = static_cast<double>( static_cast<sal_Int32>(lu) ); break; |
| 543 |
case SbxVARIANT: |
| 544 |
{ |
| 545 |
// tdf#62326 - If the value of the hex string without explicit type character lies within |
| 546 |
// the range of 0x8000 (SbxMAXINT + 1) and 0xFFFF (SbxMAXUINT) inclusive, cast the value |
| 547 |
// to 16 bit in order to get signed integers, e.g., SbxMININT through SbxMAXINT |
| 548 |
sal_Int32 ls = (lu > SbxMAXINT && lu <= SbxMAXUINT) ? static_cast<sal_Int16>(lu) : static_cast<sal_Int32>(lu); |
| 549 |
eScanType = ( ls >= SbxMININT && ls <= SbxMAXINT ) ? SbxINTEGER : SbxLONG; |
| 550 |
nVal = static_cast<double>(ls); |
| 551 |
break; |
| 552 |
} |
| 553 |
default: |
| 554 |
nVal = static_cast<double>(lu); |
| 555 |
break; |
| 556 |
} |
| 557 |
if( bOverflow ) |
| 558 |
GenError( ERRCODE_BASIC_MATH_OVERFLOW ); |
| 559 |
} |
| 560 |
|
| 561 |
// Strings: |
| 562 |
else if (nLineIdx < aLine.getLength() && (aLine[nLineIdx] == '"' || aLine[nLineIdx] == '[')) |
| 563 |
{ |
| 564 |
sal_Unicode cSep = aLine[nLineIdx]; |
| 565 |
if( cSep == '[' ) |
| 566 |
{ |
| 567 |
bSymbol = true; |
| 568 |
cSep = ']'; |
| 569 |
} |
| 570 |
sal_Int32 n = nCol + 1; |
| 571 |
while (nLineIdx < aLine.getLength()) |
| 572 |
{ |
| 573 |
do |
| 574 |
{ |
| 575 |
nLineIdx++; |
| 576 |
nCol++; |
| 577 |
} |
| 578 |
while (nLineIdx < aLine.getLength() && (aLine[nLineIdx] != cSep)); |
| 579 |
if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == cSep) |
| 580 |
{ |
| 581 |
nLineIdx++; nCol++; |
| 582 |
if (nLineIdx >= aLine.getLength() || aLine[nLineIdx] != cSep || cSep == ']') |
| 583 |
{ |
| 584 |
// If VBA Interop then doesn't eat the [] chars |
| 585 |
if ( cSep == ']' && bVBASupportOn ) |
| 586 |
aSym = aLine.copy( n - 1, nCol - n + 1); |
| 587 |
else |
| 588 |
aSym = aLine.copy( n, nCol - n - 1 ); |
| 589 |
// get out duplicate string delimiters |
| 590 |
OUStringBuffer aSymBuf(aSym.getLength()); |
| 591 |
for ( sal_Int32 i = 0, len = aSym.getLength(); i < len; ++i ) |
| 592 |
{ |
| 593 |
aSymBuf.append( aSym[i] ); |
| 594 |
if ( aSym[i] == cSep && ( i+1 < len ) && aSym[i+1] == cSep ) |
| 595 |
++i; |
| 596 |
} |
| 597 |
aSym = aSymBuf.makeStringAndClear(); |
| 598 |
if( cSep != ']' ) |
| 599 |
eScanType = SbxSTRING; |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
else |
| 604 |
{ |
| 605 |
aError = OUString(cSep); |
| 606 |
GenError( ERRCODE_BASIC_EXPECTED ); |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
// Date: |
| 612 |
else if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == '#') |
| 613 |
{ |
| 614 |
sal_Int32 n = nCol + 1; |
| 615 |
do |
| 616 |
{ |
| 617 |
nLineIdx++; |
| 618 |
nCol++; |
| 619 |
} |
| 620 |
while (nLineIdx < aLine.getLength() && (aLine[nLineIdx] != '#')); |
| 621 |
if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == '#') |
| 622 |
{ |
| 623 |
nLineIdx++; nCol++; |
| 624 |
aSym = aLine.copy( n, nCol - n - 1 ); |
| 625 |
|
| 626 |
// parse date literal |
| 627 |
std::shared_ptr<SvNumberFormatter> pFormatter; |
| 628 |
if (GetSbData()->pInst) |
| 629 |
{ |
| 630 |
pFormatter = GetSbData()->pInst->GetNumberFormatter(); |
| 631 |
} |
| 632 |
else |
| 633 |
{ |
| 634 |
sal_uInt32 nDummy; |
| 635 |
pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy ); |
| 636 |
} |
| 637 |
sal_uInt32 nIndex = pFormatter->GetStandardIndex( LANGUAGE_ENGLISH_US); |
| 638 |
bool bSuccess = pFormatter->IsNumberFormat(aSym, nIndex, nVal); |
| 639 |
if( bSuccess ) |
| 640 |
{ |
| 641 |
SvNumFormatType nType_ = pFormatter->GetType(nIndex); |
| 642 |
if( !(nType_ & SvNumFormatType::DATE) ) |
| 643 |
bSuccess = false; |
| 644 |
} |
| 645 |
|
| 646 |
if (!bSuccess) |
| 647 |
GenError( ERRCODE_BASIC_CONVERSION ); |
| 648 |
|
| 649 |
bNumber = true; |
| 650 |
eScanType = SbxDATE; |
| 651 |
} |
| 652 |
else |
| 653 |
{ |
| 654 |
aError = OUString('#'); |
| 655 |
GenError( ERRCODE_BASIC_EXPECTED ); |
| 656 |
} |
| 657 |
} |
| 658 |
// invalid characters: |
| 659 |
else if (nLineIdx < aLine.getLength() && aLine[nLineIdx] >= 0x7F) |
| 660 |
{ |
| 661 |
GenError( ERRCODE_BASIC_SYNTAX ); nLineIdx++; nCol++; |
| 662 |
} |
| 663 |
// other groups: |
| 664 |
else |
| 665 |
{ |
| 666 |
sal_Int32 n = 1; |
| 667 |
auto nChar = nLineIdx < aLine.getLength() ? aLine[nLineIdx] : 0; |
| 668 |
++nLineIdx; |
| 669 |
if (nLineIdx < aLine.getLength()) |
| 670 |
{ |
| 671 |
switch (nChar) |
| 672 |
{ |
| 673 |
case '<': if( aLine[nLineIdx] == '>' || aLine[nLineIdx] == '=' ) n = 2; break; |
| 674 |
case '>': if( aLine[nLineIdx] == '=' ) n = 2; break; |
| 675 |
case ':': if( aLine[nLineIdx] == '=' ) n = 2; break; |
| 676 |
} |
| 677 |
} |
| 678 |
aSym = aLine.copy(nCol, std::min(n, aLine.getLength() - nCol)); |
| 679 |
nLineIdx += n-1; nCol = nCol + n; |
| 680 |
} |
| 681 |
|
| 682 |
nCol2 = nCol-1; |
| 683 |
|
| 684 |
PrevLineCommentLbl: |
| 685 |
|
| 686 |
if (bPrevLineExtentsComment || |
| 687 |
(eScanType != SbxSTRING && |
| 688 |
(aSym.startsWith("'") || aSym.equalsIgnoreAsciiCase("REM") || aSym.startsWith("#")))) |
| 689 |
{ |
| 690 |
bPrevLineExtentsComment = false; |
| 691 |
aSym = "REM"; |
| 692 |
sal_Int32 nLen = aLine.getLength() - nLineIdx; |
| 693 |
// tdf#149402 - don't extend comment if line ends in a whitespace (BasicCharClass::isWhitespace) |
| 694 |
if (bCompatible && !bLineEndsWithWhitespace && aLine[nLineIdx + nLen - 1] == '_' |
| 695 |
&& aLine[nLineIdx + nLen - 2] == ' ') |
| 696 |
bPrevLineExtentsComment = true; |
| 697 |
nCol2 = nCol2 + nLen; |
| 698 |
nLineIdx = -1; |
| 699 |
} |
| 700 |
|
| 701 |
if (nLineIdx == nLineIdxScanStart) |
| 702 |
{ |
| 703 |
GenError( ERRCODE_BASIC_SYMBOL_EXPECTED ); |
| 704 |
return false; |
| 705 |
} |
| 706 |
|
| 707 |
return true; |
| 708 |
|
| 709 |
|
| 710 |
eoln: |
| 711 |
if (nCol && aLine[--nLineIdx] == '_' && !bClosingUnderscore) |
| 712 |
{ |
| 713 |
nLineIdx = -1; |
| 714 |
bool bRes = NextSym(); |
| 715 |
if( aSym.startsWith(".") ) |
| 716 |
{ |
| 717 |
// object _ |
| 718 |
// .Method |
| 719 |
// ^^^ <- spaces is legal in MSO VBA |
| 720 |
bSpaces = false; |
| 721 |
} |
| 722 |
return bRes; |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
nLineIdx = -1; |
| 727 |
nLine = nOldLine; |
| 728 |
nCol1 = nOldCol1; |
| 729 |
nCol2 = nOldCol2; |
| 730 |
aSym = "\n"; |
| 731 |
nColLock = 0; |
| 732 |
bClosingUnderscore = false; |
| 733 |
// tdf#149157 - break multiline continuation in a comment after a new line |
| 734 |
bPrevLineExtentsComment = false; |
| 735 |
return true; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |