/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
// tdf#149402 - check if line ends with a whitespace
bLineEndsWithWhitespace = (n > nEnd);
aLine = aBuf.copy(nBufPos, nEnd - nBufPos);
// Fast-forward past the line ending if(n + 1 < nLen && aBuf[n] == '\r' && aBuf[n + 1] == '\n')
n += 2; elseif(n < nLen)
++n;
nBufPos = n;
nLineIdx = 0;
++nLine;
nCol = nCol1 = nCol2 = 0;
nColLock = 0;
returntrue;
}
// Function to check if a string is a valid compiler directive staticbool isValidCompilerDirective(std::u16string_view directive)
{ static constexpr std::string_view validDirectives[]
= { "if", "elseif", "else", "end", "const" };
do
{
nLineTempIdx++;
} while (nLineTempIdx < aLine.getLength() && !BasicCharClass::isWhitespace(aLine[nLineTempIdx])
&& aLine[nLineTempIdx] != '#' && aLine[nLineTempIdx] != ','); // leave it if it is a date literal - it will be handled later if (nLineTempIdx >= aLine.getLength() || aLine[nLineTempIdx] != '#')
{
++nLineIdx;
++nCol; //handle compiler directives (# is first non-space character) if (nOldCol2 == 0)
{ if (isValidCompilerDirective(candidate))
{ // Skip the whole line if starts with a hash and is a valid compiler directive
nCol = 0; goto eoln;
} else
{
GenError(ERRCODE_BASIC_SYNTAX);
}
} else
bHash = true;
}
}
// copy character if symbol if(nCol < aLine.getLength() && (BasicCharClass::isAlpha(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
{ // if there's nothing behind '_' , it's the end of a line! if(nCol + 1 == aLine.getLength() && aLine[nCol] == '_')
{ // Note that nCol is not incremented here...
++nLineIdx; goto eoln;
}
bSymbol = true;
scanAlphanumeric();
// Special handling for "go to" if(nCol < aLine.getLength() && bCompatible && aSym.equalsIgnoreAsciiCase("go"))
scanGoto();
// tdf#125637 - check for closing underscore if (nCol == aLine.getLength() && aLine[nCol - 1] == '_')
{
bClosingUnderscore = true;
} // type recognition? // don't test the exclamation mark // if there's a symbol behind it elseif((nCol >= aLine.getLength() || aLine[nCol] != '!') ||
(nCol + 1 >= aLine.getLength() || !BasicCharClass::isAlpha(aLine[nCol + 1], bCompatible)))
{ if(nCol < aLine.getLength())
{
SbxDataType t(GetSuffixType(aLine[nCol])); if( t != SbxVARIANT )
{
eScanType = t;
++nLineIdx;
++nCol;
}
}
}
}
// read in and convert if number elseif((nCol < aLine.getLength() && rtl::isAsciiDigit(aLine[nCol])) ||
(nCol + 1 < aLine.getLength() && aLine[nCol] == '.' && rtl::isAsciiDigit(aLine[nCol + 1])))
{ short exp = 0; short dec = 0;
eScanType = SbxDOUBLE; bool bScanError = false; bool bBufOverflow = false; // All this because of 'D' or 'd' floating point type, sigh... while(!bScanError && nCol < aLine.getLength() && strchr("0123456789.DEde", aLine[nCol]))
{ // from 4.1.1996: buffer full? -> go on scanning empty if( (p-buf) == (BUF_SIZE-1) )
{
bBufOverflow = true;
++nLineIdx;
++nCol; continue;
} // point or exponent? if(aLine[nCol] == '.')
{ if( ++dec > 1 )
bScanError = true; else
*p++ = '.';
} elseif(strchr("DdEe", aLine[nCol]))
{ if (++exp > 1)
bScanError = true; else
{
*p++ = 'E'; if (nCol + 1 < aLine.getLength() && (aLine[nCol+1] == '+' || aLine[nCol+1] == '-'))
{
++nLineIdx;
++nCol; if( (p-buf) == (BUF_SIZE-1) )
{
bBufOverflow = true; continue;
}
*p++ = aLine[nCol];
}
}
} else
{
*p++ = aLine[nCol];
}
++nLineIdx;
++nCol;
}
*p = 0;
aSym = p; bNumber = true;
// For bad characters, scan and parse errors generate only one error.
ErrCode nError = ERRCODE_NONE; if (bScanError)
{
--nLineIdx;
--nCol;
aError = OUString( aLine[nCol]);
nError = ERRCODE_BASIC_BAD_CHAR_IN_NUMBER;
}
rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; const sal_Unicode* pParseEnd = buf;
nVal = rtl_math_uStringToDouble( buf, buf+(p-buf), '.', ',', &eStatus, &pParseEnd ); if (pParseEnd != buf+(p-buf))
{ // e.g. "12e" or "12e+", or with bScanError "12d"+"E".
sal_Int32 nChars = buf+(p-buf) - pParseEnd;
nLineIdx -= nChars;
nCol -= nChars; // For bScanError, nLineIdx and nCol were already decremented, just // add that character to the parse end. if (bScanError)
++nChars; // Copy error position from original string, not the buffer // replacement where "12dE" => "12EE".
aError = aLine.copy( nCol, nChars);
nError = ERRCODE_BASIC_BAD_CHAR_IN_NUMBER;
} elseif (eStatus != rtl_math_ConversionStatus_Ok)
{ // Keep the scan error and character at position, if any. if (!nError)
nError = ERRCODE_BASIC_MATH_OVERFLOW;
}
// type recognition? if( nCol < aLine.getLength() )
{
SbxDataType t(GetSuffixType(aLine[nCol])); if( t != SbxVARIANT )
{
eScanType = t;
++nLineIdx;
++nCol;
} // tdf#130476 - don't allow String trailing data type character with numbers if ( t == SbxSTRING )
{
GenError( ERRCODE_BASIC_SYNTAX );
}
}
}
// Hex/octal number? Read in and convert: elseif(aLine.getLength() - nCol > 1 && aLine[nCol] == '&')
{
++nLineIdx; ++nCol;
sal_Unicode base = 16;
sal_Unicode xch = aLine[nCol];
++nLineIdx; ++nCol; switch( rtl::toAsciiUpperCase( xch ) )
{ case'O':
base = 8; break; case'H': break; default : // treated as an operator
--nLineIdx; --nCol; nCol1 = nCol-1;
aSym = "&"; returntrue;
}
bNumber = true; // Hex literals are signed Integers ( as defined by basic // e.g. -2,147,483,648 through 2,147,483,647 (signed)
sal_uInt64 lu = 0; bool bOverflow = false; while(nCol < aLine.getLength() && BasicCharClass::isAlphaNumeric(aLine[nCol], false))
{
sal_Unicode ch = rtl::toAsciiUpperCase(aLine[nCol]);
++nLineIdx; ++nCol; if( ((base == 16 ) && rtl::isAsciiHexDigit( ch ) ) ||
((base == 8) && rtl::isAsciiOctalDigit( ch )))
{ int i = ch - '0'; if( i > 9 ) i -= 7;
lu = ( lu * base ) + i; if( lu > SAL_MAX_UINT32 )
{
bOverflow = true;
}
} else
{
aError = OUString(ch);
GenError( ERRCODE_BASIC_BAD_CHAR_IN_NUMBER );
}
}
// tdf#130476 - take into account trailing data type characters if( nCol < aLine.getLength() )
{
SbxDataType t(GetSuffixType(aLine[nCol])); if( t != SbxVARIANT )
{
eScanType = t;
++nLineIdx;
++nCol;
} // tdf#130476 - don't allow String trailing data type character with numbers if ( t == SbxSTRING )
{
GenError( ERRCODE_BASIC_SYNTAX );
}
}
// tdf#130476 - take into account trailing data type characters switch ( eScanType )
{ case SbxINTEGER:
nVal = static_cast<double>( static_cast<sal_Int16>(lu) ); if ( lu > SbxMAXUINT )
{
bOverflow = true;
} break; case SbxLONG: nVal = static_cast<double>( static_cast<sal_Int32>(lu) ); break; case SbxVARIANT:
{ // tdf#62326 - If the value of the hex string without explicit type character lies within // the range of 0x8000 (SbxMAXINT + 1) and 0xFFFF (SbxMAXUINT) inclusive, cast the value // to 16 bit in order to get signed integers, e.g., SbxMININT through SbxMAXINT
sal_Int32 ls = (lu > SbxMAXINT && lu <= SbxMAXUINT) ? static_cast<sal_Int16>(lu) : static_cast<sal_Int32>(lu);
eScanType = ( ls >= SbxMININT && ls <= SbxMAXINT ) ? SbxINTEGER : SbxLONG;
nVal = static_cast<double>(ls); break;
} default:
nVal = static_cast<double>(lu); break;
} if( bOverflow )
GenError( ERRCODE_BASIC_MATH_OVERFLOW );
}
// Strings: elseif (nLineIdx < aLine.getLength() && (aLine[nLineIdx] == '"' || aLine[nLineIdx] == '['))
{
sal_Unicode cSep = aLine[nLineIdx]; if( cSep == '[' )
{
bSymbol = true;
cSep = ']';
}
sal_Int32 n = nCol + 1; while (nLineIdx < aLine.getLength())
{ do
{
nLineIdx++;
nCol++;
} while (nLineIdx < aLine.getLength() && (aLine[nLineIdx] != cSep)); if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == cSep)
{
nLineIdx++; nCol++; if (nLineIdx >= aLine.getLength() || aLine[nLineIdx] != cSep || cSep == ']')
{ // If VBA Interop then doesn't eat the [] chars if ( cSep == ']' && bVBASupportOn )
aSym = aLine.copy( n - 1, nCol - n + 1); else
aSym = aLine.copy( n, nCol - n - 1 ); // get out duplicate string delimiters
OUStringBuffer aSymBuf(aSym.getLength()); for ( sal_Int32 i = 0, len = aSym.getLength(); i < len; ++i )
{
aSymBuf.append( aSym[i] ); if ( aSym[i] == cSep && ( i+1 < len ) && aSym[i+1] == cSep )
++i;
}
aSym = aSymBuf.makeStringAndClear(); if( cSep != ']' )
eScanType = SbxSTRING; break;
}
} else
{
aError = OUString(cSep);
GenError( ERRCODE_BASIC_EXPECTED );
}
}
}
// Date: elseif (nLineIdx < aLine.getLength() && aLine[nLineIdx] == '#')
{
sal_Int32 n = nCol + 1; do
{
nLineIdx++;
nCol++;
} while (nLineIdx < aLine.getLength() && (aLine[nLineIdx] != '#')); if (nLineIdx < aLine.getLength() && aLine[nLineIdx] == '#')
{
nLineIdx++; nCol++;
aSym = aLine.copy( n, nCol - n - 1 );
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.