Files
libreoffice/basic/source/comp/scanner.cxx

561 lines
16 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2000-09-18 15:18:56 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2000-09-18 15:18:56 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 15:18:56 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 15:18:56 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 15:18:56 +00:00
*
************************************************************************/
#include "basiccharclass.hxx"
2000-09-18 15:18:56 +00:00
#include "sbcomp.hxx"
2011-11-17 23:06:47 -05:00
#include <vcl/svapp.hxx>
2000-09-18 15:18:56 +00:00
SbiScanner::SbiScanner( const ::rtl::OUString& rBuf, StarBASIC* p ) : aBuf( rBuf )
2000-09-18 15:18:56 +00:00
{
pBasic = p;
pLine = NULL;
nVal = 0;
eScanType = SbxVARIANT;
nErrors = 0;
nBufPos = 0;
nCurCol1 = 0;
nSavedCol1 = 0;
nColLock = 0;
nLine = 0;
nCol1 = 0;
nCol2 = 0;
2000-09-18 15:18:56 +00:00
nCol = 0;
bError =
bAbort =
bSpaces =
bNumber =
bSymbol =
bCompatible =
bVBASupportOn =
bPrevLineExtentsComment = sal_False;
2000-09-18 15:18:56 +00:00
bHash =
bErrors = sal_True;
2000-09-18 15:18:56 +00:00
}
SbiScanner::~SbiScanner()
{}
void SbiScanner::LockColumn()
{
if( !nColLock++ )
nSavedCol1 = nCol1;
}
void SbiScanner::UnlockColumn()
{
if( nColLock )
nColLock--;
}
void SbiScanner::GenError( SbError code )
{
if( GetSbData()->bBlockCompilerError )
{
2011-11-18 01:53:30 -05:00
bAbort = true;
return;
}
2000-09-18 15:18:56 +00:00
if( !bError && bErrors )
{
2011-11-18 01:53:30 -05:00
bool bRes = true;
// report only one error per statement
2011-11-18 01:53:30 -05:00
bError = true;
2000-09-18 15:18:56 +00:00
if( pBasic )
{
// in case of EXPECTED or UNEXPECTED it always refers
// to the last token, so take the Col1 over
2011-12-05 17:41:28 -05:00
sal_Int32 nc = nColLock ? nSavedCol1 : nCol1;
2000-09-18 15:18:56 +00:00
switch( code )
{
case SbERR_EXPECTED:
case SbERR_UNEXPECTED:
case SbERR_SYMBOL_EXPECTED:
case SbERR_LABEL_EXPECTED:
nc = nCol1;
if( nc > nCol2 ) nCol2 = nc;
break;
}
bRes = pBasic->CError( code, aError, nLine, nc, nCol2 );
}
bAbort |= !bRes |
( code == SbERR_NO_MEMORY || code == SbERR_PROG_TOO_LARGE );
}
if( bErrors )
nErrors++;
}
// used by SbiTokenizer::MayBeLabel() to detect a label
2011-11-18 01:53:30 -05:00
bool SbiScanner::DoesColonFollow()
2000-09-18 15:18:56 +00:00
{
2012-01-07 20:18:35 -05:00
if(nCol < aLine.getLength() && aLine[nCol] == ':')
2000-09-18 15:18:56 +00:00
{
2012-01-09 00:24:54 -05:00
++pLine; ++nCol;
2011-11-18 01:53:30 -05:00
return true;
2000-09-18 15:18:56 +00:00
}
2011-11-18 01:53:30 -05:00
else
return false;
2000-09-18 15:18:56 +00:00
}
// test for legal suffix
2000-09-18 15:18:56 +00:00
static SbxDataType GetSuffixType( sal_Unicode c )
{
2011-11-18 01:20:36 -05:00
switch (c)
2000-09-18 15:18:56 +00:00
{
2011-11-18 01:20:36 -05:00
case '%':
return SbxDataType(SbxINTEGER);
case '&':
return SbxDataType(SbxLONG);
case '!':
return SbxDataType(SbxSINGLE);
case '#':
return SbxDataType(SbxDOUBLE);
case '@':
return SbxDataType(SbxCURRENCY);
case '$':
return SbxDataType(SbxSTRING);
default:
return SbxDataType(SbxVARIANT);
2000-09-18 15:18:56 +00:00
}
}
// reading the next symbol into the variables aSym, nVal and eType
// return value is sal_False at EOF or errors
2000-09-18 15:18:56 +00:00
#define BUF_SIZE 80
2011-12-04 22:13:24 -05:00
void SbiScanner::scanAlphanumeric()
{
2011-12-05 17:41:28 -05:00
sal_Int32 n = nCol;
2012-01-07 20:18:35 -05:00
while(nCol < aLine.getLength() && (theBasicCharClass::get().isAlphaNumeric(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
2011-12-04 22:13:24 -05:00
{
2012-01-09 00:24:54 -05:00
++pLine;
++nCol;
2011-12-04 22:13:24 -05:00
}
aSym = aLine.copy(n, nCol - n);
}
2011-11-18 21:27:44 -05:00
void SbiScanner::scanGoto()
{
2012-01-07 18:32:47 -05:00
sal_Int32 n = nCol;
while(n < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[n]))
++n;
2011-11-18 21:27:44 -05:00
2012-01-07 18:32:47 -05:00
if(n + 1 < aLine.getLength())
2011-11-18 21:27:44 -05:00
{
2012-01-07 18:32:47 -05:00
::rtl::OUString aTemp = aLine.copy(n, 2);
if(aTemp.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("to")))
2011-11-18 21:27:44 -05:00
{
2011-11-18 21:35:01 -05:00
aSym = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("goto"));
2012-01-07 18:32:47 -05:00
pLine += n + 2 - nCol;
nCol = n + 2;
2011-11-18 21:27:44 -05:00
}
}
}
2012-01-07 18:23:45 -05:00
bool SbiScanner::readLine()
{
if(nBufPos >= aBuf.getLength())
return false;
sal_Int32 n = nBufPos;
sal_Int32 nLen = aBuf.getLength();
while(n < nLen && aBuf[n] != '\r' && aBuf[n] != '\n')
++n;
// Trim trailing whitespace
sal_Int32 nEnd = n;
while(nBufPos < nEnd && theBasicCharClass::get().isWhitespace(aBuf[nEnd - 1]))
--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;
else if(n < nLen)
2012-01-09 00:24:54 -05:00
++n;
2012-01-07 18:23:45 -05:00
nBufPos = n;
pLine = aLine.getStr();
++nLine;
nCol = nCol1 = nCol2 = 0;
nColLock = 0;
return true;
}
2011-11-18 01:53:30 -05:00
bool SbiScanner::NextSym()
2000-09-18 15:18:56 +00:00
{
// memorize for the EOLN-case
2011-12-05 17:41:28 -05:00
sal_Int32 nOldLine = nLine;
sal_Int32 nOldCol1 = nCol1;
sal_Int32 nOldCol2 = nCol2;
2000-09-18 15:18:56 +00:00
sal_Unicode buf[ BUF_SIZE ], *p = buf;
eScanType = SbxVARIANT;
aSym = ::rtl::OUString();
2012-01-09 00:27:16 -05:00
bHash = bSymbol = bNumber = bSpaces = false;
2000-09-18 15:18:56 +00:00
// read in line?
2000-09-18 15:18:56 +00:00
if( !pLine )
{
2012-01-07 18:23:45 -05:00
if(!readLine())
2011-11-18 01:53:30 -05:00
return false;
2000-09-18 15:18:56 +00:00
2012-01-07 18:23:45 -05:00
nOldLine = nLine;
nOldCol1 = nOldCol2 = 0;
}
2012-01-09 00:33:45 -05:00
if(nCol < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[nCol]))
{
bSpaces = true;
while(nCol < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[nCol]))
++pLine, ++nCol;
}
2000-09-18 15:18:56 +00:00
nCol1 = nCol;
// only blank line?
2012-01-07 20:18:35 -05:00
if(nCol >= aLine.getLength())
goto eoln;
2000-09-18 15:18:56 +00:00
if( bPrevLineExtentsComment )
goto PrevLineCommentLbl;
2012-01-07 20:18:35 -05:00
if(nCol < aLine.getLength() && aLine[nCol] == '#')
{
2012-01-09 00:24:54 -05:00
++pLine;
++nCol;
2011-11-18 01:53:30 -05:00
bHash = true;
}
2000-09-18 15:18:56 +00:00
// copy character if symbol
2012-01-09 00:27:16 -05:00
if(nCol < aLine.getLength() && (theBasicCharClass::get().isAlpha(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
2000-09-18 15:18:56 +00:00
{
// if there's nothing behind '_' , it's the end of a line!
2012-01-09 00:33:45 -05:00
if(nCol + 1 == aLine.getLength() && aLine[nCol] == '_')
{
// Note that nCol is not incremented here...
++pLine;
goto eoln;
}
2011-11-18 01:53:30 -05:00
bSymbol = true;
2011-12-04 22:13:24 -05:00
scanAlphanumeric();
// Special handling for "go to"
2012-01-09 00:33:45 -05:00
if(nCol < aLine.getLength() && bCompatible && aSym.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("go")))
2011-11-18 21:27:44 -05:00
scanGoto();
// replace closing '_' by space when end of line is following
// (wrong line continuation otherwise)
2012-01-09 00:33:45 -05:00
if(nCol == aLine.getLength() && aLine[nCol - 1] == '_' )
{
// We are going to modify a potentially shared string, so force
// a copy, so that aSym is not modified by the following operation
::rtl::OUString aSymCopy( aSym.getStr(), aSym.getLength() );
aSym = aSymCopy;
// HACK: modifying a potentially shared string here!
*((sal_Unicode*)(pLine-1)) = ' ';
}
2012-01-09 00:33:45 -05:00
// type recognition?
// don't test the exclamation mark
// if there's a symbol behind it
2012-01-09 00:33:45 -05:00
else if((nCol >= aLine.getLength() || aLine[nCol] != '!') ||
(nCol + 1 >= aLine.getLength() || !theBasicCharClass::get().isAlpha(aLine[nCol + 1], bCompatible)))
2000-09-18 15:18:56 +00:00
{
2012-01-09 00:33:45 -05:00
if(nCol < aLine.getLength())
2000-09-18 15:18:56 +00:00
{
2012-01-14 15:18:39 -05:00
SbxDataType t(GetSuffixType(aLine[nCol]));
2012-01-09 00:33:45 -05:00
if( t != SbxVARIANT )
{
eScanType = t;
++pLine;
++nCol;
}
2000-09-18 15:18:56 +00:00
}
}
}
// read in and convert if number
2012-01-14 17:47:43 -05:00
else if((nCol < aLine.getLength() && theBasicCharClass::get().isDigit(aLine[nCol] & 0xFF)) ||
(nCol + 1 < aLine.getLength() && aLine[nCol] == '.' && theBasicCharClass::get().isDigit(aLine[nCol + 1] & 0xFF)))
2000-09-18 15:18:56 +00:00
{
short exp = 0;
short comma = 0;
short ndig = 0;
short ncdig = 0;
eScanType = SbxDOUBLE;
2011-11-18 01:53:30 -05:00
bool bBufOverflow = false;
2012-01-14 18:39:17 -05:00
while(nCol < aLine.getLength() && strchr("0123456789.DEde", aLine[nCol]))
2000-09-18 15:18:56 +00:00
{
// from 4.1.1996: buffer full? -> go on scanning empty
2000-09-18 15:18:56 +00:00
if( (p-buf) == (BUF_SIZE-1) )
{
2011-11-18 01:53:30 -05:00
bBufOverflow = true;
2012-01-09 00:24:54 -05:00
++pLine, ++nCol;
2000-09-18 15:18:56 +00:00
continue;
}
// point or exponent?
2012-01-14 18:39:17 -05:00
if(aLine[nCol] == '.')
2000-09-18 15:18:56 +00:00
{
if( ++comma > 1 )
{
2012-01-09 00:24:54 -05:00
++pLine; ++nCol; continue;
2000-09-18 15:18:56 +00:00
}
2012-01-14 18:39:17 -05:00
else
{
*p = '.';
++p, ++pLine, ++nCol;
}
2000-09-18 15:18:56 +00:00
}
2012-01-14 18:39:17 -05:00
else if(strchr("DdEe", aLine[nCol]))
2000-09-18 15:18:56 +00:00
{
if (++exp > 1)
{
2012-01-09 00:24:54 -05:00
++pLine; ++nCol; continue;
2000-09-18 15:18:56 +00:00
}
2012-01-14 18:39:17 -05:00
*p = 'E';
++p, ++pLine, ++nCol;
if(aLine[nCol] == '+')
2012-01-09 00:24:54 -05:00
++pLine, ++nCol;
2012-01-14 18:39:17 -05:00
else if(aLine[nCol] == '-')
{
*p = '-';
++p, ++pLine, ++nCol;
}
2000-09-18 15:18:56 +00:00
}
else
{
2012-01-14 18:39:17 -05:00
*p = aLine[nCol];
++p, ++pLine, ++nCol;
2012-01-09 00:24:54 -05:00
if( comma && !exp ) ++ncdig;
2000-09-18 15:18:56 +00:00
}
2012-01-09 00:24:54 -05:00
if (!exp) ++ndig;
2000-09-18 15:18:56 +00:00
}
*p = 0;
2011-11-18 01:53:30 -05:00
aSym = p; bNumber = true;
2000-09-18 15:18:56 +00:00
if( comma > 1 || exp > 1 )
{ aError = ::rtl::OUString('.');
2000-09-18 15:18:56 +00:00
GenError( SbERR_BAD_CHAR_IN_NUMBER ); }
// #57844 use localized function
nVal = rtl_math_uStringToDouble( buf, buf+(p-buf), '.', ',', NULL, NULL );
2000-09-18 15:18:56 +00:00
ndig = ndig - comma;
2000-09-18 15:18:56 +00:00
if( !comma && !exp )
{
if( nVal >= SbxMININT && nVal <= SbxMAXINT )
eScanType = SbxINTEGER;
else
if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG )
eScanType = SbxLONG;
}
if( bBufOverflow )
GenError( SbERR_MATH_OVERFLOW );
// type recognition?
2012-01-14 18:39:17 -05:00
SbxDataType t(GetSuffixType(aLine[nCol]));
2000-09-18 15:18:56 +00:00
if( t != SbxVARIANT )
{
eScanType = t;
2012-01-09 00:24:54 -05:00
++pLine;
++nCol;
2000-09-18 15:18:56 +00:00
}
}
// Hex/octal number? Read in and convert:
2012-01-15 02:15:54 -05:00
else if(nCol < aLine.getLength() && aLine[nCol] == '&')
2000-09-18 15:18:56 +00:00
{
2012-01-09 00:24:54 -05:00
++pLine; ++nCol;
sal_Unicode cmp1[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', 0 };
sal_Unicode cmp2[] = { '0', '1', '2', '3', '4', '5', '6', '7', 0 };
2000-09-18 15:18:56 +00:00
sal_Unicode *cmp = cmp1;
sal_Unicode base = 16;
sal_Unicode ndig = 8;
2012-01-15 02:15:54 -05:00
sal_Unicode xch = aLine[nCol] & 0xFF;
++pLine; ++nCol;
2000-09-18 15:18:56 +00:00
switch( toupper( xch ) )
{
case 'O':
cmp = cmp2; base = 8; ndig = 11; break;
case 'H':
break;
default :
// treated as an operator
2012-01-09 00:24:54 -05:00
--pLine; --nCol; nCol1 = nCol-1;
aSym = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("&"));
return SYMBOL;
2000-09-18 15:18:56 +00:00
}
2011-11-18 01:53:30 -05:00
bNumber = true;
2000-09-18 15:18:56 +00:00
long l = 0;
int i;
2011-11-18 01:53:30 -05:00
bool bBufOverflow = false;
2012-01-15 02:15:54 -05:00
while(nCol < aLine.getLength() && theBasicCharClass::get().isAlphaNumeric(aLine[nCol] & 0xFF, bCompatible))
2000-09-18 15:18:56 +00:00
{
sal_Unicode ch = sal::static_int_cast< sal_Unicode >(
2012-01-15 02:15:54 -05:00
toupper(aLine[nCol] & 0xFF));
2012-01-09 00:24:54 -05:00
++pLine; ++nCol;
// from 4.1.1996: buffer full, go on scanning empty
2000-09-18 15:18:56 +00:00
if( (p-buf) == (BUF_SIZE-1) )
2011-11-18 01:53:30 -05:00
bBufOverflow = true;
2000-09-18 15:18:56 +00:00
else if( String( cmp ).Search( ch ) != STRING_NOTFOUND )
//else if( strchr( cmp, ch ) )
*p++ = ch;
else
{
aError = ::rtl::OUString(ch);
2000-09-18 15:18:56 +00:00
GenError( SbERR_BAD_CHAR_IN_NUMBER );
}
}
*p = 0;
2012-01-09 00:24:54 -05:00
for( p = buf; *p; ++p )
2000-09-18 15:18:56 +00:00
{
i = (*p & 0xFF) - '0';
if( i > 9 ) i -= 7;
l = ( l * base ) + i;
if( !ndig-- )
{
GenError( SbERR_MATH_OVERFLOW ); break;
}
}
2012-01-15 02:15:54 -05:00
if(nCol < aLine.getLength() && aLine[nCol] == '&') ++pLine, ++nCol;
2000-09-18 15:18:56 +00:00
nVal = (double) l;
eScanType = ( l >= SbxMININT && l <= SbxMAXINT ) ? SbxINTEGER : SbxLONG;
if( bBufOverflow )
GenError( SbERR_MATH_OVERFLOW );
}
// Strings:
else if( *pLine == '"' || *pLine == '[' )
{
sal_Unicode cSep = *pLine;
if( cSep == '[' )
2011-11-18 01:53:30 -05:00
bSymbol = true, cSep = ']';
2011-12-05 17:41:28 -05:00
sal_Int32 n = nCol + 1;
2000-09-18 15:18:56 +00:00
while( *pLine )
{
do pLine++, nCol++;
while( *pLine && ( *pLine != cSep ) );
if( *pLine == cSep )
{
pLine++; nCol++;
if( *pLine != cSep || cSep == ']' ) break;
} else aError = ::rtl::OUString(cSep), GenError( SbERR_EXPECTED );
2000-09-18 15:18:56 +00:00
}
// If VBA Interop then doen'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
::rtl::OUStringBuffer aSymBuf;
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();
2000-09-18 15:18:56 +00:00
if( cSep != ']' )
eScanType = ( cSep == '#' ) ? SbxDATE : SbxSTRING;
}
// invalid characters:
2000-09-18 15:18:56 +00:00
else if( ( *pLine & 0xFF ) >= 0x7F )
{
GenError( SbERR_SYNTAX ); pLine++; nCol++;
}
// other groups:
2000-09-18 15:18:56 +00:00
else
{
2011-12-05 17:41:28 -05:00
sal_Int32 n = 1;
2000-09-18 15:18:56 +00:00
switch( *pLine++ )
{
case '<': if( *pLine == '>' || *pLine == '=' ) n = 2; break;
case '>': if( *pLine == '=' ) n = 2; break;
case ':': if( *pLine == '=' ) n = 2; break;
}
aSym = aLine.copy( nCol, n );
pLine += n-1; nCol = nCol + n;
2000-09-18 15:18:56 +00:00
}
nCol2 = nCol-1;
PrevLineCommentLbl:
if( bPrevLineExtentsComment || (eScanType != SbxSTRING &&
( aSym[0] == '\'' || aSym.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM("REM") ) ) ) )
2000-09-18 15:18:56 +00:00
{
2011-11-18 01:53:30 -05:00
bPrevLineExtentsComment = false;
aSym = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("REM"));
2011-12-05 17:41:28 -05:00
sal_Int32 nLen = String( pLine ).Len();
if( bCompatible && pLine[ nLen - 1 ] == '_' && pLine[ nLen - 2 ] == ' ' )
2011-11-18 01:53:30 -05:00
bPrevLineExtentsComment = true;
nCol2 = nCol2 + nLen;
2000-09-18 15:18:56 +00:00
pLine = NULL;
}
2011-11-18 01:53:30 -05:00
return true;
2000-09-18 15:18:56 +00:00
2000-09-18 15:18:56 +00:00
eoln:
if( nCol && *--pLine == '_' )
{
pLine = NULL;
bool bRes = NextSym();
if( bVBASupportOn && aSym[0] == '.' )
{
// object _
// .Method
// ^^^ <- spaces is legal in MSO VBA
OSL_TRACE("*** resetting bSpaces***");
2011-11-18 01:53:30 -05:00
bSpaces = false;
}
return bRes;
2000-09-18 15:18:56 +00:00
}
else
{
pLine = NULL;
nLine = nOldLine;
nCol1 = nOldCol1;
nCol2 = nOldCol2;
aSym = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("\n"));
2000-09-18 15:18:56 +00:00
nColLock = 0;
2011-11-18 01:53:30 -05:00
return true;
2000-09-18 15:18:56 +00:00
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */