nss: upgrade to release 3.73
[LibreOffice.git] / basic / source / comp / scanner.cxx
blob0647a72edfffc8e71bf6d61fd84a1998341a243a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basiccharclass.hxx>
21 #include <scanner.hxx>
22 #include <sbintern.hxx>
23 #include <runtime.hxx>
25 #include <basic/sberrors.hxx>
26 #include <i18nlangtag/lang.h>
27 #include <svl/zforlist.hxx>
28 #include <rtl/character.hxx>
30 SbiScanner::SbiScanner( const OUString& rBuf, StarBASIC* p ) : aBuf( rBuf )
32 pBasic = p;
33 nLineIdx = -1;
34 nVal = 0;
35 eScanType = SbxVARIANT;
36 nErrors = 0;
37 nBufPos = 0;
38 nSavedCol1 = 0;
39 nColLock = 0;
40 nLine = 0;
41 nCol1 = 0;
42 nCol2 = 0;
43 nCol = 0;
44 bError =
45 bAbort =
46 bSpaces =
47 bNumber =
48 bSymbol =
49 bCompatible =
50 bVBASupportOn =
51 bInStatement =
52 bPrevLineExtentsComment = false;
53 bHash = true;
54 nSaveLineIdx = -1;
57 void SbiScanner::LockColumn()
59 if( !nColLock++ )
60 nSavedCol1 = nCol1;
63 void SbiScanner::UnlockColumn()
65 if( nColLock )
66 nColLock--;
69 void SbiScanner::GenError( ErrCode code )
71 if( GetSbData()->bBlockCompilerError )
73 bAbort = true;
74 return;
76 if( !bError )
78 bool bRes = true;
79 // report only one error per statement
80 bError = true;
81 if( pBasic )
83 // in case of EXPECTED or UNEXPECTED it always refers
84 // to the last token, so take the Col1 over
85 sal_Int32 nc = nColLock ? nSavedCol1 : nCol1;
86 if ( code.anyOf(
87 ERRCODE_BASIC_EXPECTED,
88 ERRCODE_BASIC_UNEXPECTED,
89 ERRCODE_BASIC_SYMBOL_EXPECTED,
90 ERRCODE_BASIC_LABEL_EXPECTED) )
92 nc = nCol1;
93 if( nc > nCol2 ) nCol2 = nc;
95 bRes = pBasic->CError( code, aError, nLine, nc, nCol2 );
97 bAbort = bAbort || !bRes || ( code == ERRCODE_BASIC_NO_MEMORY || code == ERRCODE_BASIC_PROG_TOO_LARGE );
99 nErrors++;
103 // used by SbiTokenizer::MayBeLabel() to detect a label
104 bool SbiScanner::DoesColonFollow()
106 if(nCol < aLine.getLength() && aLine[nCol] == ':')
108 ++nLineIdx; ++nCol;
109 return true;
111 else
112 return false;
115 // test for legal suffix
116 static SbxDataType GetSuffixType( sal_Unicode c )
118 switch (c)
120 case '%':
121 return SbxINTEGER;
122 case '&':
123 return SbxLONG;
124 case '!':
125 return SbxSINGLE;
126 case '#':
127 return SbxDOUBLE;
128 case '@':
129 return SbxCURRENCY;
130 case '$':
131 return SbxSTRING;
132 default:
133 return SbxVARIANT;
137 // reading the next symbol into the variables aSym, nVal and eType
138 // return value is sal_False at EOF or errors
139 #define BUF_SIZE 80
141 void SbiScanner::scanAlphanumeric()
143 sal_Int32 n = nCol;
144 while(nCol < aLine.getLength() && (BasicCharClass::isAlphaNumeric(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
146 ++nLineIdx;
147 ++nCol;
149 aSym = aLine.copy(n, nCol - n);
152 void SbiScanner::scanGoto()
154 sal_Int32 n = nCol;
155 while(n < aLine.getLength() && BasicCharClass::isWhitespace(aLine[n]))
156 ++n;
158 if(n + 1 < aLine.getLength())
160 OUString aTemp = aLine.copy(n, 2);
161 if(aTemp.equalsIgnoreAsciiCase("to"))
163 aSym = "goto";
164 nLineIdx += n + 2 - nCol;
165 nCol = n + 2;
170 bool SbiScanner::readLine()
172 if(nBufPos >= aBuf.getLength())
173 return false;
175 sal_Int32 n = nBufPos;
176 sal_Int32 nLen = aBuf.getLength();
178 while(n < nLen && aBuf[n] != '\r' && aBuf[n] != '\n')
179 ++n;
181 // Trim trailing whitespace
182 sal_Int32 nEnd = n;
183 while(nBufPos < nEnd && BasicCharClass::isWhitespace(aBuf[nEnd - 1]))
184 --nEnd;
186 aLine = aBuf.copy(nBufPos, nEnd - nBufPos);
188 // Fast-forward past the line ending
189 if(n + 1 < nLen && aBuf[n] == '\r' && aBuf[n + 1] == '\n')
190 n += 2;
191 else if(n < nLen)
192 ++n;
194 nBufPos = n;
195 nLineIdx = 0;
197 ++nLine;
198 nCol = nCol1 = nCol2 = 0;
199 nColLock = 0;
201 return true;
204 bool SbiScanner::NextSym()
206 // memorize for the EOLN-case
207 sal_Int32 nOldLine = nLine;
208 sal_Int32 nOldCol1 = nCol1;
209 sal_Int32 nOldCol2 = nCol2;
210 sal_Unicode buf[ BUF_SIZE ], *p = buf;
212 eScanType = SbxVARIANT;
213 aSym.clear();
214 bHash = bSymbol = bNumber = bSpaces = false;
215 bool bCompilerDirective = false;
217 // read in line?
218 if (nLineIdx == -1)
220 if(!readLine())
221 return false;
223 nOldLine = nLine;
224 nOldCol1 = nOldCol2 = 0;
227 const sal_Int32 nLineIdxScanStart = nLineIdx;
229 if(nCol < aLine.getLength() && BasicCharClass::isWhitespace(aLine[nCol]))
231 bSpaces = true;
232 while(nCol < aLine.getLength() && BasicCharClass::isWhitespace(aLine[nCol]))
234 ++nLineIdx;
235 ++nCol;
239 nCol1 = nCol;
241 // only blank line?
242 if(nCol >= aLine.getLength())
243 goto eoln;
245 if( bPrevLineExtentsComment )
246 goto PrevLineCommentLbl;
248 if(nCol < aLine.getLength() && aLine[nCol] == '#')
250 sal_Int32 nLineTempIdx = nLineIdx;
253 nLineTempIdx++;
254 } while (nLineTempIdx < aLine.getLength() && !BasicCharClass::isWhitespace(aLine[nLineTempIdx])
255 && aLine[nLineTempIdx] != '#' && aLine[nLineTempIdx] != ',');
256 // leave it if it is a date literal - it will be handled later
257 if (nLineTempIdx >= aLine.getLength() || aLine[nLineTempIdx] != '#')
259 ++nLineIdx;
260 ++nCol;
261 //ignore compiler directives (# is first non-space character)
262 if (nOldCol2 == 0)
263 bCompilerDirective = true;
264 else
265 bHash = true;
269 // copy character if symbol
270 if(nCol < aLine.getLength() && (BasicCharClass::isAlpha(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
272 // if there's nothing behind '_' , it's the end of a line!
273 if(nCol + 1 == aLine.getLength() && aLine[nCol] == '_')
275 // Note that nCol is not incremented here...
276 ++nLineIdx;
277 goto eoln;
280 bSymbol = true;
282 scanAlphanumeric();
284 // Special handling for "go to"
285 if(nCol < aLine.getLength() && bCompatible && aSym.equalsIgnoreAsciiCase("go"))
286 scanGoto();
288 // replace closing '_' by space when end of line is following
289 // (wrong line continuation otherwise)
290 if (nCol == aLine.getLength() && aLine[nCol - 1] == '_')
292 // We are going to modify a potentially shared string, so force
293 // a copy, so that aSym is not modified by the following operation
294 OUString aSymCopy( aSym.getStr(), aSym.getLength() );
295 aSym = aSymCopy;
297 // HACK: modifying a potentially shared string here!
298 const_cast<sal_Unicode*>(aLine.getStr())[nLineIdx - 1] = ' ';
301 // type recognition?
302 // don't test the exclamation mark
303 // if there's a symbol behind it
304 else if((nCol >= aLine.getLength() || aLine[nCol] != '!') ||
305 (nCol + 1 >= aLine.getLength() || !BasicCharClass::isAlpha(aLine[nCol + 1], bCompatible)))
307 if(nCol < aLine.getLength())
309 SbxDataType t(GetSuffixType(aLine[nCol]));
310 if( t != SbxVARIANT )
312 eScanType = t;
313 ++nLineIdx;
314 ++nCol;
320 // read in and convert if number
321 else if((nCol < aLine.getLength() && rtl::isAsciiDigit(aLine[nCol])) ||
322 (nCol + 1 < aLine.getLength() && aLine[nCol] == '.' && rtl::isAsciiDigit(aLine[nCol + 1])))
324 short exp = 0;
325 short dec = 0;
326 eScanType = SbxDOUBLE;
327 bool bScanError = false;
328 bool bBufOverflow = false;
329 // All this because of 'D' or 'd' floating point type, sigh...
330 while(!bScanError && nCol < aLine.getLength() && strchr("0123456789.DEde", aLine[nCol]))
332 // from 4.1.1996: buffer full? -> go on scanning empty
333 if( (p-buf) == (BUF_SIZE-1) )
335 bBufOverflow = true;
336 ++nLineIdx;
337 ++nCol;
338 continue;
340 // point or exponent?
341 if(aLine[nCol] == '.')
343 if( ++dec > 1 )
344 bScanError = true;
345 else
346 *p++ = '.';
348 else if(strchr("DdEe", aLine[nCol]))
350 if (++exp > 1)
351 bScanError = true;
352 else
354 *p++ = 'E';
355 if (nCol + 1 < aLine.getLength() && (aLine[nCol+1] == '+' || aLine[nCol+1] == '-'))
357 ++nLineIdx;
358 ++nCol;
359 if( (p-buf) == (BUF_SIZE-1) )
361 bBufOverflow = true;
362 continue;
364 *p++ = aLine[nCol];
368 else
370 *p++ = aLine[nCol];
372 ++nLineIdx;
373 ++nCol;
375 *p = 0;
376 aSym = p; bNumber = true;
378 // For bad characters, scan and parse errors generate only one error.
379 ErrCode nError = ERRCODE_NONE;
380 if (bScanError)
382 --nLineIdx;
383 --nCol;
384 aError = OUString( aLine[nCol]);
385 nError = ERRCODE_BASIC_BAD_CHAR_IN_NUMBER;
388 rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
389 const sal_Unicode* pParseEnd = buf;
390 nVal = rtl_math_uStringToDouble( buf, buf+(p-buf), '.', ',', &eStatus, &pParseEnd );
391 if (pParseEnd != buf+(p-buf))
393 // e.g. "12e" or "12e+", or with bScanError "12d"+"E".
394 sal_Int32 nChars = buf+(p-buf) - pParseEnd;
395 nLineIdx -= nChars;
396 nCol -= nChars;
397 // For bScanError, nLineIdx and nCol were already decremented, just
398 // add that character to the parse end.
399 if (bScanError)
400 ++nChars;
401 // Copy error position from original string, not the buffer
402 // replacement where "12dE" => "12EE".
403 aError = aLine.copy( nCol, nChars);
404 nError = ERRCODE_BASIC_BAD_CHAR_IN_NUMBER;
406 else if (eStatus != rtl_math_ConversionStatus_Ok)
408 // Keep the scan error and character at position, if any.
409 if (!nError)
410 nError = ERRCODE_BASIC_MATH_OVERFLOW;
413 if (nError)
414 GenError( nError );
416 if( !dec && !exp )
418 if( nVal >= SbxMININT && nVal <= SbxMAXINT )
419 eScanType = SbxINTEGER;
420 else if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG )
421 eScanType = SbxLONG;
424 if( bBufOverflow )
425 GenError( ERRCODE_BASIC_MATH_OVERFLOW );
427 // type recognition?
428 if( nCol < aLine.getLength() )
430 SbxDataType t(GetSuffixType(aLine[nCol]));
431 if( t != SbxVARIANT )
433 eScanType = t;
434 ++nLineIdx;
435 ++nCol;
437 // tdf#130476 - don't allow String trailing data type character with numbers
438 if ( t == SbxSTRING )
440 GenError( ERRCODE_BASIC_SYNTAX );
445 // Hex/octal number? Read in and convert:
446 else if(aLine.getLength() - nCol > 1 && aLine[nCol] == '&')
448 ++nLineIdx; ++nCol;
449 sal_Unicode base = 16;
450 sal_Unicode xch = aLine[nCol];
451 ++nLineIdx; ++nCol;
452 switch( rtl::toAsciiUpperCase( xch ) )
454 case 'O':
455 base = 8;
456 break;
457 case 'H':
458 break;
459 default :
460 // treated as an operator
461 --nLineIdx; --nCol; nCol1 = nCol-1;
462 aSym = "&";
463 return true;
465 bNumber = true;
466 // Hex literals are signed Integers ( as defined by basic
467 // e.g. -2,147,483,648 through 2,147,483,647 (signed)
468 sal_uInt64 lu = 0;
469 bool bOverflow = false;
470 while(nCol < aLine.getLength() && BasicCharClass::isAlphaNumeric(aLine[nCol], false))
472 sal_Unicode ch = rtl::toAsciiUpperCase(aLine[nCol]);
473 ++nLineIdx; ++nCol;
474 if( ((base == 16 ) && rtl::isAsciiHexDigit( ch ) ) ||
475 ((base == 8) && rtl::isAsciiOctalDigit( ch )))
477 int i = ch - '0';
478 if( i > 9 ) i -= 7;
479 lu = ( lu * base ) + i;
480 if( lu > SAL_MAX_UINT32 )
482 bOverflow = true;
485 else
487 aError = OUString(ch);
488 GenError( ERRCODE_BASIC_BAD_CHAR_IN_NUMBER );
492 // tdf#130476 - take into account trailing data type characters
493 if( nCol < aLine.getLength() )
495 SbxDataType t(GetSuffixType(aLine[nCol]));
496 if( t != SbxVARIANT )
498 eScanType = t;
499 ++nLineIdx;
500 ++nCol;
502 // tdf#130476 - don't allow String trailing data type character with numbers
503 if ( t == SbxSTRING )
505 GenError( ERRCODE_BASIC_SYNTAX );
509 // tdf#130476 - take into account trailing data type characters
510 switch ( eScanType )
512 case SbxINTEGER:
513 nVal = static_cast<double>( static_cast<sal_Int16>(lu) );
514 if ( lu > SbxMAXUINT )
516 bOverflow = true;
518 break;
519 case SbxLONG: nVal = static_cast<double>( static_cast<sal_Int32>(lu) ); break;
520 case SbxVARIANT:
522 // tdf#62326 - If the value of the hex string without explicit type character lies within
523 // the range of 0x8000 (SbxMAXINT + 1) and 0xFFFF (SbxMAXUINT) inclusive, cast the value
524 // to 16 bit in order to get signed integers, e.g., SbxMININT through SbxMAXINT
525 sal_Int32 ls = (lu > SbxMAXINT && lu <= SbxMAXUINT) ? static_cast<sal_Int16>(lu) : static_cast<sal_Int32>(lu);
526 eScanType = ( ls >= SbxMININT && ls <= SbxMAXINT ) ? SbxINTEGER : SbxLONG;
527 nVal = static_cast<double>(ls);
528 break;
530 default:
531 nVal = static_cast<double>(lu);
532 break;
534 if( bOverflow )
535 GenError( ERRCODE_BASIC_MATH_OVERFLOW );
538 // Strings:
539 else if (nLineIdx < aLine.getLength() && (aLine[nLineIdx] == '"' || aLine[nLineIdx] == '['))
541 sal_Unicode cSep = aLine[nLineIdx];
542 if( cSep == '[' )
544 bSymbol = true;
545 cSep = ']';
547 sal_Int32 n = nCol + 1;
548 while (nLineIdx < aLine.getLength())
552 nLineIdx++;
553 nCol++;
555 while (nLineIdx < aLine.getLength() && (aLine[nLineIdx] != cSep));
556 if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == cSep)
558 nLineIdx++; nCol++;
559 if (nLineIdx >= aLine.getLength() || aLine[nLineIdx] != cSep || cSep == ']')
561 // If VBA Interop then doesn't eat the [] chars
562 if ( cSep == ']' && bVBASupportOn )
563 aSym = aLine.copy( n - 1, nCol - n + 1);
564 else
565 aSym = aLine.copy( n, nCol - n - 1 );
566 // get out duplicate string delimiters
567 OUStringBuffer aSymBuf(aSym.getLength());
568 for ( sal_Int32 i = 0, len = aSym.getLength(); i < len; ++i )
570 aSymBuf.append( aSym[i] );
571 if ( aSym[i] == cSep && ( i+1 < len ) && aSym[i+1] == cSep )
572 ++i;
574 aSym = aSymBuf.makeStringAndClear();
575 if( cSep != ']' )
576 eScanType = SbxSTRING;
577 break;
580 else
582 aError = OUString(cSep);
583 GenError( ERRCODE_BASIC_EXPECTED );
588 // Date:
589 else if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == '#')
591 sal_Int32 n = nCol + 1;
594 nLineIdx++;
595 nCol++;
597 while (nLineIdx < aLine.getLength() && (aLine[nLineIdx] != '#'));
598 if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == '#')
600 nLineIdx++; nCol++;
601 aSym = aLine.copy( n, nCol - n - 1 );
603 // parse date literal
604 std::shared_ptr<SvNumberFormatter> pFormatter;
605 if (GetSbData()->pInst)
607 pFormatter = GetSbData()->pInst->GetNumberFormatter();
609 else
611 sal_uInt32 nDummy;
612 pFormatter = SbiInstance::PrepareNumberFormatter( nDummy, nDummy, nDummy );
614 sal_uInt32 nIndex = pFormatter->GetStandardIndex( LANGUAGE_ENGLISH_US);
615 bool bSuccess = pFormatter->IsNumberFormat(aSym, nIndex, nVal);
616 if( bSuccess )
618 SvNumFormatType nType_ = pFormatter->GetType(nIndex);
619 if( !(nType_ & SvNumFormatType::DATE) )
620 bSuccess = false;
623 if (!bSuccess)
624 GenError( ERRCODE_BASIC_CONVERSION );
626 bNumber = true;
627 eScanType = SbxDOUBLE;
629 else
631 aError = OUString('#');
632 GenError( ERRCODE_BASIC_EXPECTED );
635 // invalid characters:
636 else if (nLineIdx < aLine.getLength() && aLine[nLineIdx] >= 0x7F)
638 GenError( ERRCODE_BASIC_SYNTAX ); nLineIdx++; nCol++;
640 // other groups:
641 else
643 sal_Int32 n = 1;
644 auto nChar = nLineIdx < aLine.getLength() ? aLine[nLineIdx] : 0;
645 ++nLineIdx;
646 if (nLineIdx < aLine.getLength())
648 switch (nChar)
650 case '<': if( aLine[nLineIdx] == '>' || aLine[nLineIdx] == '=' ) n = 2; break;
651 case '>': if( aLine[nLineIdx] == '=' ) n = 2; break;
652 case ':': if( aLine[nLineIdx] == '=' ) n = 2; break;
655 aSym = aLine.copy(nCol, std::min(n, aLine.getLength() - nCol));
656 nLineIdx += n-1; nCol = nCol + n;
659 nCol2 = nCol-1;
661 PrevLineCommentLbl:
663 if( bPrevLineExtentsComment || (eScanType != SbxSTRING &&
664 ( bCompilerDirective ||
665 aSym.startsWith("'") ||
666 aSym.equalsIgnoreAsciiCase( "REM" ) ) ) )
668 bPrevLineExtentsComment = false;
669 aSym = "REM";
670 sal_Int32 nLen = aLine.getLength() - nLineIdx;
671 if( bCompatible && aLine[nLineIdx + nLen - 1] == '_' && aLine[nLineIdx + nLen - 2] == ' ' )
672 bPrevLineExtentsComment = true;
673 nCol2 = nCol2 + nLen;
674 nLineIdx = -1;
677 if (nLineIdx == nLineIdxScanStart)
679 GenError( ERRCODE_BASIC_SYMBOL_EXPECTED );
680 return false;
683 return true;
686 eoln:
687 if( nCol && aLine[--nLineIdx] == '_' )
689 nLineIdx = -1;
690 bool bRes = NextSym();
691 if( aSym.startsWith(".") )
693 // object _
694 // .Method
695 // ^^^ <- spaces is legal in MSO VBA
696 bSpaces = false;
698 return bRes;
700 else
702 nLineIdx = -1;
703 nLine = nOldLine;
704 nCol1 = nOldCol1;
705 nCol2 = nOldCol2;
706 aSym = "\n";
707 nColLock = 0;
708 return true;
712 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */