2010-10-12 15:53:47 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-07-17 12:30:48 +01:00
/*
* 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 .
*/
2000-09-18 15:18:56 +00:00
2007-06-27 13:20:42 +00:00
# include <basic/sbx.hxx>
2000-09-18 15:18:56 +00:00
# include "sbcomp.hxx"
2014-11-14 22:52:35 +01:00
# include <osl/diagnose.h>
2010-04-16 17:28:52 +02:00
# include <com/sun/star/script/ModuleType.hpp>
GSOC work week 2, getting infromation from variables and print on terminal
This is an early version. I use the BASIC parser to parse the source,
then the infromation is extracted from the symbol table built by parser.
Error reporting is suppressed, beacuse it is not needed fro code completition.
I placed my function inside SbModule, and created a struct called CodeCompletitionData, which holds the object's name, it's parent, and it's type name.
This function, SbMethod::GetCodeCompleteDataFromParse() is called from Basic IDE's Notify function, which updates a cache(actually, reassigns a viariable :) ).
Later, in the EditorWindow::KeyInput function there is a check wheteher dot key is pressed. After that, the actual variable (or word) is being looked up in the vector that holds code completition data. And finally, if it is found, it's methods are printed on the terminal.
Change-Id: Idaf19baa8f720b8b117a76dc3cc2f90dd04fd155
2013-06-21 14:10:31 +02:00
# include <svtools/miscopt.hxx>
2000-09-18 15:18:56 +00:00
2011-08-27 21:37:14 +02:00
struct SbiParseStack { // "Stack" for statement-blocks
2000-09-18 15:18:56 +00:00
SbiParseStack * pNext ; // Chain
2011-08-27 21:37:14 +02:00
SbiExprNode * pWithVar ;
SbiToken eExitTok ;
2011-01-10 14:40:57 +01:00
sal_uInt32 nChain ; // JUMP-Chain
2000-09-18 15:18:56 +00:00
} ;
struct SbiStatement {
SbiToken eTok ;
2011-08-27 21:37:14 +02:00
void ( SbiParser : : * Func ) ( ) ;
2013-10-01 23:16:43 +09:00
bool bMain ; // true: OK outside the SUB
bool bSubr ; // true: OK inside the SUB
2000-09-18 15:18:56 +00:00
} ;
2013-10-01 23:16:43 +09:00
# define Y true
# define N false
2000-09-18 15:18:56 +00:00
2013-08-01 04:32:32 +09:00
static const SbiStatement StmntTable [ ] = {
2010-10-06 10:16:27 +01:00
{ ATTRIBUTE , & SbiParser : : Attribute , Y , Y , } , // ATTRIBUTE
2001-05-17 08:43:07 +00:00
{ CALL , & SbiParser : : Call , N , Y , } , // CALL
{ CLOSE , & SbiParser : : Close , N , Y , } , // CLOSE
{ _CONST_ , & SbiParser : : Dim , Y , Y , } , // CONST
{ DECLARE , & SbiParser : : Declare , Y , N , } , // DECLARE
{ DEFBOOL , & SbiParser : : DefXXX , Y , N , } , // DEFBOOL
{ DEFCUR , & SbiParser : : DefXXX , Y , N , } , // DEFCUR
{ DEFDATE , & SbiParser : : DefXXX , Y , N , } , // DEFDATE
{ DEFDBL , & SbiParser : : DefXXX , Y , N , } , // DEFDBL
{ DEFERR , & SbiParser : : DefXXX , Y , N , } , // DEFERR
{ DEFINT , & SbiParser : : DefXXX , Y , N , } , // DEFINT
{ DEFLNG , & SbiParser : : DefXXX , Y , N , } , // DEFLNG
{ DEFOBJ , & SbiParser : : DefXXX , Y , N , } , // DEFOBJ
{ DEFSNG , & SbiParser : : DefXXX , Y , N , } , // DEFSNG
{ DEFSTR , & SbiParser : : DefXXX , Y , N , } , // DEFSTR
{ DEFVAR , & SbiParser : : DefXXX , Y , N , } , // DEFVAR
{ DIM , & SbiParser : : Dim , Y , Y , } , // DIM
{ DO , & SbiParser : : DoLoop , N , Y , } , // DO
{ ELSE , & SbiParser : : NoIf , N , Y , } , // ELSE
{ ELSEIF , & SbiParser : : NoIf , N , Y , } , // ELSEIF
{ ENDIF , & SbiParser : : NoIf , N , Y , } , // ENDIF
{ END , & SbiParser : : Stop , N , Y , } , // END
2004-11-15 15:34:56 +00:00
{ ENUM , & SbiParser : : Enum , Y , N , } , // TYPE
2001-05-17 08:43:07 +00:00
{ ERASE , & SbiParser : : Erase , N , Y , } , // ERASE
{ _ERROR_ , & SbiParser : : ErrorStmnt , N , Y , } , // ERROR
{ EXIT , & SbiParser : : Exit , N , Y , } , // EXIT
{ FOR , & SbiParser : : For , N , Y , } , // FOR
{ FUNCTION , & SbiParser : : SubFunc , Y , N , } , // FUNCTION
{ GOSUB , & SbiParser : : Goto , N , Y , } , // GOSUB
{ GLOBAL , & SbiParser : : Dim , Y , N , } , // GLOBAL
{ GOTO , & SbiParser : : Goto , N , Y , } , // GOTO
{ IF , & SbiParser : : If , N , Y , } , // IF
2005-03-29 10:49:55 +00:00
{ IMPLEMENTS , & SbiParser : : Implements , Y , N , } , // IMPLEMENTS
2001-05-17 08:43:07 +00:00
{ INPUT , & SbiParser : : Input , N , Y , } , // INPUT
{ LET , & SbiParser : : Assign , N , Y , } , // LET
2010-06-15 20:02:53 +02:00
{ LINE , & SbiParser : : Line , N , Y , } , // LINE, -> LINE INPUT (#i92642)
2001-05-17 08:43:07 +00:00
{ LINEINPUT , & SbiParser : : LineInput , N , Y , } , // LINE INPUT
{ LOOP , & SbiParser : : BadBlock , N , Y , } , // LOOP
{ LSET , & SbiParser : : LSet , N , Y , } , // LSET
{ NAME , & SbiParser : : Name , N , Y , } , // NAME
{ NEXT , & SbiParser : : BadBlock , N , Y , } , // NEXT
{ ON , & SbiParser : : On , N , Y , } , // ON
{ OPEN , & SbiParser : : Open , N , Y , } , // OPEN
{ OPTION , & SbiParser : : Option , Y , N , } , // OPTION
{ PRINT , & SbiParser : : Print , N , Y , } , // PRINT
{ PRIVATE , & SbiParser : : Dim , Y , N , } , // PRIVATE
2004-11-02 10:54:14 +00:00
{ PROPERTY , & SbiParser : : SubFunc , Y , N , } , // FUNCTION
2001-05-17 08:43:07 +00:00
{ PUBLIC , & SbiParser : : Dim , Y , N , } , // PUBLIC
{ REDIM , & SbiParser : : ReDim , N , Y , } , // DIM
{ RESUME , & SbiParser : : Resume , N , Y , } , // RESUME
{ RETURN , & SbiParser : : Return , N , Y , } , // RETURN
{ RSET , & SbiParser : : RSet , N , Y , } , // RSET
{ SELECT , & SbiParser : : Select , N , Y , } , // SELECT
{ SET , & SbiParser : : Set , N , Y , } , // SET
{ STATIC , & SbiParser : : Static , Y , Y , } , // STATIC
{ STOP , & SbiParser : : Stop , N , Y , } , // STOP
{ SUB , & SbiParser : : SubFunc , Y , N , } , // SUB
{ TYPE , & SbiParser : : Type , Y , N , } , // TYPE
{ UNTIL , & SbiParser : : BadBlock , N , Y , } , // UNTIL
{ WHILE , & SbiParser : : While , N , Y , } , // WHILE
{ WEND , & SbiParser : : BadBlock , N , Y , } , // WEND
{ WITH , & SbiParser : : With , N , Y , } , // WITH
{ WRITE , & SbiParser : : Write , N , Y , } , // WRITE
2000-09-18 15:18:56 +00:00
2006-06-19 16:42:42 +00:00
{ NIL , NULL , N , N }
2000-09-18 15:18:56 +00:00
} ;
SbiParser : : SbiParser ( StarBASIC * pb , SbModule * pm )
2003-04-23 15:56:49 +00:00
: SbiTokenizer ( pm - > GetSource32 ( ) , pb ) ,
2000-09-18 15:18:56 +00:00
aGblStrings ( this ) ,
aLclStrings ( this ) ,
aGlobals ( aGblStrings , SbGLOBAL ) ,
2006-06-19 16:42:42 +00:00
aPublics ( aGblStrings , SbPUBLIC ) ,
2000-09-18 15:18:56 +00:00
aRtlSyms ( aGblStrings , SbRTL ) ,
aGen ( * pm , this , 1024 )
{
pBasic = pb ;
eCurExpr = SbSYMBOL ;
eEndTok = NIL ;
pProc = NULL ;
pStack = NULL ;
pWithVar = NULL ;
nBase = 0 ;
bText =
bGblDefs =
bNewGblDefs =
bSingleLineIf =
2013-07-07 19:43:05 +02:00
bCodeCompleting =
2012-08-19 17:49:02 +09:00
bExplicit = false ;
2010-04-16 17:28:52 +02:00
bClassModule = ( pm - > GetModuleType ( ) = = com : : sun : : star : : script : : ModuleType : : CLASS ) ;
2012-12-30 13:07:28 -02:00
OSL_TRACE ( " Parser - %s, bClassModule %d " , OUStringToOString ( pm - > GetName ( ) , RTL_TEXTENCODING_UTF8 ) . getStr ( ) , bClassModule ) ;
2000-09-18 15:18:56 +00:00
pPool = & aPublics ;
for ( short i = 0 ; i < 26 ; i + + )
2011-08-27 21:37:14 +02:00
eDefTypes [ i ] = SbxVARIANT ; // no explicit default type
2000-09-18 15:18:56 +00:00
aPublics . SetParent ( & aGlobals ) ;
aGlobals . SetParent ( & aRtlSyms ) ;
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
nGblChain = aGen . Gen ( _JUMP , 0 ) ;
2011-08-27 21:37:14 +02:00
rTypeArray = new SbxArray ; // array for user defined types
rEnumArray = new SbxArray ; // array for Enum types
2010-03-02 12:39:31 +00:00
bVBASupportOn = pm - > IsVBACompat ( ) ;
if ( bVBASupportOn )
EnableCompatibility ( ) ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
// part of the runtime-library?
2012-11-06 23:34:23 -06:00
SbiSymDef * SbiParser : : CheckRTLForSym ( const OUString & rSym , SbxDataType eType )
2000-09-18 15:18:56 +00:00
{
SbxVariable * pVar = GetBasic ( ) - > GetRtl ( ) - > Find ( rSym , SbxCLASS_DONTCARE ) ;
SbiSymDef * pDef = NULL ;
if ( pVar )
{
if ( pVar - > IsA ( TYPE ( SbxMethod ) ) )
{
2006-06-19 16:42:42 +00:00
SbiProcDef * pProc_ = aRtlSyms . AddProc ( rSym ) ;
2015-01-09 10:54:45 +01:00
SbxMethod * pMethod = static_cast < SbxMethod * > ( pVar ) ;
2015-01-08 16:28:11 +00:00
if ( pMethod & & pMethod - > IsRuntimeFunction ( ) )
{
pProc_ - > SetType ( pMethod - > GetRuntimeFunctionReturnType ( ) ) ;
}
else
{
pProc_ - > SetType ( pVar - > GetType ( ) ) ;
}
2006-06-19 16:42:42 +00:00
pDef = pProc_ ;
2000-09-18 15:18:56 +00:00
}
else
{
pDef = aRtlSyms . AddSym ( rSym ) ;
pDef - > SetType ( eType ) ;
}
}
return pDef ;
}
2011-08-27 21:37:14 +02:00
// close global chain
2000-09-18 15:18:56 +00:00
2012-08-19 17:49:02 +09:00
bool SbiParser : : HasGlobalCode ( )
2000-09-18 15:18:56 +00:00
{
if ( bGblDefs & & nGblChain )
{
aGen . BackChain ( nGblChain ) ;
aGen . Gen ( _LEAVE ) ;
nGblChain = 0 ;
}
return bGblDefs ;
}
void SbiParser : : OpenBlock ( SbiToken eTok , SbiExprNode * pVar )
{
SbiParseStack * p = new SbiParseStack ;
p - > eExitTok = eTok ;
p - > nChain = 0 ;
p - > pWithVar = pWithVar ;
p - > pNext = pStack ;
pStack = p ;
pWithVar = pVar ;
2011-08-27 21:37:14 +02:00
// #29955 service the for-loop level
2000-09-18 15:18:56 +00:00
if ( eTok = = FOR )
aGen . IncForLevel ( ) ;
}
void SbiParser : : CloseBlock ( )
{
if ( pStack )
{
SbiParseStack * p = pStack ;
2011-08-27 21:37:14 +02:00
// #29955 service the for-loop level
2000-09-18 15:18:56 +00:00
if ( p - > eExitTok = = FOR )
aGen . DecForLevel ( ) ;
aGen . BackChain ( p - > nChain ) ;
pStack = p - > pNext ;
pWithVar = p - > pWithVar ;
delete p ;
}
}
// EXIT ...
void SbiParser : : Exit ( )
{
SbiToken eTok = Next ( ) ;
for ( SbiParseStack * p = pStack ; p ; p = p - > pNext )
{
2010-06-15 20:02:53 +02:00
SbiToken eExitTok = p - > eExitTok ;
if ( eTok = = eExitTok | |
( eTok = = PROPERTY & & ( eExitTok = = GET | | eExitTok = = LET ) ) ) // #i109051
2000-09-18 15:18:56 +00:00
{
p - > nChain = aGen . Gen ( _JUMP , p - > nChain ) ;
return ;
}
}
if ( pStack )
Error ( SbERR_EXPECTED , pStack - > eExitTok ) ;
else
Error ( SbERR_BAD_EXIT ) ;
}
2012-08-20 09:44:32 +09:00
bool SbiParser : : TestSymbol ( bool bKwdOk )
2000-09-18 15:18:56 +00:00
{
Peek ( ) ;
if ( eCurTok = = SYMBOL | | ( bKwdOk & & IsKwd ( eCurTok ) ) )
{
2012-08-20 09:44:32 +09:00
Next ( ) ; return true ;
2000-09-18 15:18:56 +00:00
}
Error ( SbERR_SYMBOL_EXPECTED ) ;
2012-08-20 09:44:32 +09:00
return false ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
2012-08-20 09:44:32 +09:00
bool SbiParser : : TestToken ( SbiToken t )
2000-09-18 15:18:56 +00:00
{
if ( Peek ( ) = = t )
{
2012-08-20 09:44:32 +09:00
Next ( ) ; return true ;
2000-09-18 15:18:56 +00:00
}
else
{
Error ( SbERR_EXPECTED , t ) ;
2012-08-20 09:44:32 +09:00
return false ;
2000-09-18 15:18:56 +00:00
}
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
2012-08-20 09:44:32 +09:00
bool SbiParser : : TestComma ( )
2000-09-18 15:18:56 +00:00
{
SbiToken eTok = Peek ( ) ;
if ( IsEoln ( eTok ) )
{
Next ( ) ;
2012-08-20 09:44:32 +09:00
return false ;
2000-09-18 15:18:56 +00:00
}
else if ( eTok ! = COMMA )
{
Error ( SbERR_EXPECTED , COMMA ) ;
2012-08-20 09:44:32 +09:00
return false ;
2000-09-18 15:18:56 +00:00
}
Next ( ) ;
2012-08-20 09:44:32 +09:00
return true ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
void SbiParser : : TestEoln ( )
{
if ( ! IsEoln ( Next ( ) ) )
{
Error ( SbERR_EXPECTED , EOLN ) ;
while ( ! IsEoln ( Next ( ) ) ) { }
}
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
void SbiParser : : StmntBlock ( SbiToken eEnd )
{
SbiToken xe = eEndTok ;
eEndTok = eEnd ;
while ( ! bAbort & & Parse ( ) ) { }
eEndTok = xe ;
if ( IsEof ( ) )
{
Error ( SbERR_BAD_BLOCK , eEnd ) ;
2014-01-28 20:03:03 +01:00
bAbort = true ;
2000-09-18 15:18:56 +00:00
}
}
2013-07-07 19:43:05 +02:00
void SbiParser : : SetCodeCompleting ( const bool & b )
{
bCodeCompleting = b ;
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
2012-08-20 09:44:32 +09:00
bool SbiParser : : Parse ( )
2000-09-18 15:18:56 +00:00
{
2012-08-20 09:44:32 +09:00
if ( bAbort ) return false ;
2000-09-18 15:18:56 +00:00
EnableErrors ( ) ;
2006-05-05 07:48:18 +00:00
bErrorIsSymbol = false ;
2000-09-18 15:18:56 +00:00
Peek ( ) ;
2006-05-05 07:48:18 +00:00
bErrorIsSymbol = true ;
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
if ( IsEof ( ) )
{
2011-08-27 21:37:14 +02:00
// AB #33133: If no sub has been created before,
// the global chain must be closed here!
// AB #40689: Due to the new static-handling there
// can be another nGblChain, so ask for it before.
2000-09-18 15:18:56 +00:00
if ( bNewGblDefs & & nGblChain = = 0 )
nGblChain = aGen . Gen ( _JUMP , 0 ) ;
2012-08-20 09:44:32 +09:00
return false ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
if ( IsEoln ( eCurTok ) )
{
2012-08-20 09:44:32 +09:00
Next ( ) ; return true ;
2000-09-18 15:18:56 +00:00
}
2012-08-22 19:20:19 +09:00
if ( ! bSingleLineIf & & MayBeLabel ( true ) )
2000-09-18 15:18:56 +00:00
{
2011-08-27 21:37:14 +02:00
// is a label
2000-09-18 15:18:56 +00:00
if ( ! pProc )
Error ( SbERR_NOT_IN_MAIN , aSym ) ;
else
pProc - > GetLabels ( ) . Define ( aSym ) ;
Next ( ) ; Peek ( ) ;
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
if ( IsEoln ( eCurTok ) )
{
2012-08-20 09:44:32 +09:00
Next ( ) ; return true ;
2000-09-18 15:18:56 +00:00
}
}
2011-08-27 21:37:14 +02:00
// end of parsing?
2010-06-15 20:02:53 +02:00
if ( eCurTok = = eEndTok | |
( bVBASupportOn & & // #i109075
( eCurTok = = ENDFUNC | | eCurTok = = ENDPROPERTY | | eCurTok = = ENDSUB ) & &
( eEndTok = = ENDFUNC | | eEndTok = = ENDPROPERTY | | eEndTok = = ENDSUB ) ) )
2000-09-18 15:18:56 +00:00
{
Next ( ) ;
if ( eCurTok ! = NIL )
aGen . Statement ( ) ;
2012-08-20 09:44:32 +09:00
return false ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
// comment?
2000-09-18 15:18:56 +00:00
if ( eCurTok = = REM )
{
2012-08-20 09:44:32 +09:00
Next ( ) ; return true ;
2000-09-18 15:18:56 +00:00
}
2010-10-06 10:16:27 +01:00
// In vba it's possible to do Error.foobar ( even if it results in
// a runtime error
if ( eCurTok = = _ERROR_ & & IsVBASupportOn ( ) ) // we probably need to define a subset of keywords where this madness applies e.g. if ( IsVBASupportOn() && SymbolCanBeRedined( eCurTok ) )
{
SbiTokenizer tokens ( * ( SbiTokenizer * ) this ) ;
tokens . Next ( ) ;
if ( tokens . Peek ( ) = = DOT )
{
eCurTok = SYMBOL ;
ePush = eCurTok ;
}
}
2011-08-27 21:37:14 +02:00
// if there's a symbol, it's either a variable (LET)
// or a SUB-procedure (CALL without brackets)
// DOT for assignments in the WITH-block: .A=5
2000-09-18 15:18:56 +00:00
if ( eCurTok = = SYMBOL | | eCurTok = = DOT )
{
if ( ! pProc )
Error ( SbERR_EXPECTED , SUB ) ;
else
{
2011-08-27 21:37:14 +02:00
// for correct line and column...
2000-09-18 15:18:56 +00:00
Next ( ) ;
Push ( eCurTok ) ;
aGen . Statement ( ) ;
2008-06-24 15:02:11 +00:00
Symbol ( ) ;
2000-09-18 15:18:56 +00:00
}
}
else
{
Next ( ) ;
2011-08-27 21:37:14 +02:00
// statement parsers
2000-09-18 15:18:56 +00:00
2013-08-01 04:32:32 +09:00
const SbiStatement * p ;
2000-09-18 15:18:56 +00:00
for ( p = StmntTable ; p - > eTok ! = NIL ; p + + )
if ( p - > eTok = = eCurTok )
break ;
if ( p - > eTok ! = NIL )
{
if ( ! pProc & & ! p - > bMain )
Error ( SbERR_NOT_IN_MAIN , eCurTok ) ;
2001-05-17 08:43:07 +00:00
else if ( pProc & & ! p - > bSubr )
2000-09-18 15:18:56 +00:00
Error ( SbERR_NOT_IN_SUBR , eCurTok ) ;
else
{
2011-08-27 21:37:14 +02:00
// AB #41606/#40689: Due to the new static-handling there
// can be another nGblChain, so ask for it before.
2004-11-02 10:54:14 +00:00
if ( bNewGblDefs & & nGblChain = = 0 & &
( eCurTok = = SUB | | eCurTok = = FUNCTION | | eCurTok = = PROPERTY ) )
2000-09-18 15:18:56 +00:00
{
nGblChain = aGen . Gen ( _JUMP , 0 ) ;
2012-08-19 17:49:02 +09:00
bNewGblDefs = false ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
// statement-opcode at the beginning of a sub, too, please
2000-09-18 15:18:56 +00:00
if ( ( p - > bSubr & & ( eCurTok ! = STATIC | | Peek ( ) = = SUB | | Peek ( ) = = FUNCTION ) ) | |
eCurTok = = SUB | | eCurTok = = FUNCTION )
aGen . Statement ( ) ;
( this - > * ( p - > Func ) ) ( ) ;
SbxError nSbxErr = SbxBase : : GetError ( ) ;
if ( nSbxErr )
SbxBase : : ResetError ( ) , Error ( ( SbError ) nSbxErr ) ;
}
}
else
Error ( SbERR_UNEXPECTED , eCurTok ) ;
}
2011-08-27 21:37:14 +02:00
// test for the statement's end -
2013-03-03 17:11:39 +01:00
// might also be an ELSE, as there must not necessary be a : before the ELSE!
2000-09-18 15:18:56 +00:00
if ( ! IsEos ( ) )
{
Peek ( ) ;
if ( ! IsEos ( ) & & eCurTok ! = ELSE )
{
2011-08-27 21:37:14 +02:00
// if the parsing has been aborted, jump over to the ":"
2000-09-18 15:18:56 +00:00
Error ( SbERR_UNEXPECTED , eCurTok ) ;
while ( ! IsEos ( ) ) Next ( ) ;
}
}
2011-08-27 21:37:14 +02:00
// The parser aborts at the end, the
// next token has not been fetched yet!
2012-08-20 09:44:32 +09:00
return true ;
2000-09-18 15:18:56 +00:00
}
2011-08-27 21:37:14 +02:00
2000-09-18 15:18:56 +00:00
SbiExprNode * SbiParser : : GetWithVar ( )
{
if ( pWithVar )
return pWithVar ;
SbiParseStack * p = pStack ;
while ( p )
{
2011-08-27 21:37:14 +02:00
// LoopVar can at the moment only be for with
2000-09-18 15:18:56 +00:00
if ( p - > pWithVar )
return p - > pWithVar ;
p = p - > pNext ;
}
return NULL ;
}
2011-08-27 21:37:14 +02:00
// assignment or subroutine call
2000-09-18 15:18:56 +00:00
2010-06-15 20:02:53 +02:00
void SbiParser : : Symbol ( const KeywordSymbolInfo * pKeywordSymbolInfo )
2000-09-18 15:18:56 +00:00
{
2008-06-24 15:02:11 +00:00
SbiExprMode eMode = bVBASupportOn ? EXPRMODE_STANDALONE : EXPRMODE_STANDARD ;
2010-06-15 20:02:53 +02:00
SbiExpression aVar ( this , SbSYMBOL , eMode , pKeywordSymbolInfo ) ;
2003-05-22 07:53:47 +00:00
2003-04-23 15:56:49 +00:00
bool bEQ = ( Peek ( ) = = EQ ) ;
2008-06-24 15:02:11 +00:00
if ( ! bEQ & & bVBASupportOn & & aVar . IsBracket ( ) )
Error ( SbERR_EXPECTED , " = " ) ;
2003-05-22 07:53:47 +00:00
RecursiveMode eRecMode = ( bEQ ? PREVENT_CALL : FORCE_CALL ) ;
2006-05-05 07:48:18 +00:00
bool bSpecialMidHandling = false ;
SbiSymDef * pDef = aVar . GetRealVar ( ) ;
if ( bEQ & & pDef & & pDef - > GetScope ( ) = = SbRTL )
2000-09-18 15:18:56 +00:00
{
2012-11-06 23:34:23 -06:00
OUString aRtlName = pDef - > GetName ( ) ;
if ( aRtlName . equalsIgnoreAsciiCase ( " Mid " ) )
2006-05-05 07:48:18 +00:00
{
SbiExprNode * pExprNode = aVar . GetExprNode ( ) ;
if ( pExprNode & & pExprNode - > GetNodeType ( ) = = SbxVARVAL )
{
SbiExprList * pPar = pExprNode - > GetParameters ( ) ;
short nParCount = pPar ? pPar - > GetSize ( ) : 0 ;
if ( nParCount = = 2 | | nParCount = = 3 )
{
if ( nParCount = = 2 )
pPar - > addExpression ( new SbiExpression ( this , - 1 , SbxLONG ) ) ;
TestToken ( EQ ) ;
pPar - > addExpression ( new SbiExpression ( this ) ) ;
bSpecialMidHandling = true ;
}
}
}
2000-09-18 15:18:56 +00:00
}
2006-05-05 07:48:18 +00:00
aVar . Gen ( eRecMode ) ;
if ( ! bSpecialMidHandling )
2000-09-18 15:18:56 +00:00
{
2006-05-05 07:48:18 +00:00
if ( ! bEQ )
{
aGen . Gen ( _GET ) ;
}
else
2000-09-18 15:18:56 +00:00
{
2011-08-27 21:37:14 +02:00
// so it must be an assignment!
2006-05-05 07:48:18 +00:00
if ( ! aVar . IsLvalue ( ) )
Error ( SbERR_LVALUE_EXPECTED ) ;
TestToken ( EQ ) ;
SbiExpression aExpr ( this ) ;
aExpr . Gen ( ) ;
SbiOpcode eOp = _PUT ;
if ( pDef )
2000-09-18 15:18:56 +00:00
{
2006-05-05 07:48:18 +00:00
if ( pDef - > GetConstDef ( ) )
Error ( SbERR_DUPLICATE_DEF , pDef - > GetName ( ) ) ;
if ( pDef - > GetType ( ) = = SbxOBJECT )
2005-03-29 10:49:55 +00:00
{
2006-05-05 07:48:18 +00:00
eOp = _SET ;
if ( pDef - > GetTypeId ( ) )
{
aGen . Gen ( _SETCLASS , pDef - > GetTypeId ( ) ) ;
return ;
}
2005-03-29 10:49:55 +00:00
}
2000-09-18 15:18:56 +00:00
}
2006-05-05 07:48:18 +00:00
aGen . Gen ( eOp ) ;
2000-09-18 15:18:56 +00:00
}
}
}
void SbiParser : : Assign ( )
{
SbiExpression aLvalue ( this , SbLVALUE ) ;
TestToken ( EQ ) ;
SbiExpression aExpr ( this ) ;
aLvalue . Gen ( ) ;
aExpr . Gen ( ) ;
2011-01-10 14:40:57 +01:00
sal_uInt16 nLen = 0 ;
2000-09-18 15:18:56 +00:00
SbiSymDef * pDef = aLvalue . GetRealVar ( ) ;
{
if ( pDef - > GetConstDef ( ) )
Error ( SbERR_DUPLICATE_DEF , pDef - > GetName ( ) ) ;
nLen = aLvalue . GetRealVar ( ) - > GetLen ( ) ;
}
if ( nLen )
aGen . Gen ( _PAD , nLen ) ;
aGen . Gen ( _PUT ) ;
}
2011-08-27 21:37:14 +02:00
// assignments of an object-variable
2000-09-18 15:18:56 +00:00
void SbiParser : : Set ( )
{
SbiExpression aLvalue ( this , SbLVALUE ) ;
2004-03-17 12:33:25 +00:00
SbxDataType eType = aLvalue . GetType ( ) ;
if ( eType ! = SbxOBJECT & & eType ! = SbxEMPTY & & eType ! = SbxVARIANT )
2000-09-18 15:18:56 +00:00
Error ( SbERR_INVALID_OBJECT ) ;
TestToken ( EQ ) ;
SbiSymDef * pDef = aLvalue . GetRealVar ( ) ;
2014-01-27 14:22:30 +00:00
if ( pDef - > GetConstDef ( ) )
2000-09-18 15:18:56 +00:00
Error ( SbERR_DUPLICATE_DEF , pDef - > GetName ( ) ) ;
2004-03-17 12:33:25 +00:00
SbiToken eTok = Peek ( ) ;
if ( eTok = = NEW )
{
Next ( ) ;
2012-11-06 23:34:23 -06:00
OUString aStr ;
2005-03-29 10:49:55 +00:00
SbiSymDef * pTypeDef = new SbiSymDef ( aStr ) ;
2014-01-28 20:03:03 +01:00
TypeDecl ( * pTypeDef , true ) ;
2004-03-17 12:33:25 +00:00
aLvalue . Gen ( ) ;
2005-03-29 10:49:55 +00:00
aGen . Gen ( _CREATE , pDef - > GetId ( ) , pTypeDef - > GetTypeId ( ) ) ;
aGen . Gen ( _SETCLASS , pDef - > GetTypeId ( ) ) ;
2004-03-17 12:33:25 +00:00
}
else
{
SbiExpression aExpr ( this ) ;
aLvalue . Gen ( ) ;
aExpr . Gen ( ) ;
2006-11-02 15:32:13 +00:00
// Its a good idea to distinguish between
2013-03-03 17:11:39 +01:00
// set something = another &
// something = another
2006-11-02 15:32:13 +00:00
// ( its necessary for vba objects where set is object
// specific and also doesn't involve processing default params )
2004-03-17 12:33:25 +00:00
if ( pDef - > GetTypeId ( ) )
2007-08-30 09:00:07 +00:00
{
if ( bVBASupportOn )
aGen . Gen ( _VBASETCLASS , pDef - > GetTypeId ( ) ) ;
else
aGen . Gen ( _SETCLASS , pDef - > GetTypeId ( ) ) ;
}
2005-03-29 10:49:55 +00:00
else
2006-11-02 15:32:13 +00:00
{
if ( bVBASupportOn )
aGen . Gen ( _VBASET ) ;
else
aGen . Gen ( _SET ) ;
}
2004-03-17 12:33:25 +00:00
}
2000-09-18 15:18:56 +00:00
}
// JSM 07.10.95
void SbiParser : : LSet ( )
{
SbiExpression aLvalue ( this , SbLVALUE ) ;
if ( aLvalue . GetType ( ) ! = SbxSTRING )
2012-11-03 09:07:25 -05:00
{
2000-09-18 15:18:56 +00:00
Error ( SbERR_INVALID_OBJECT ) ;
2012-11-03 09:07:25 -05:00
}
2000-09-18 15:18:56 +00:00
TestToken ( EQ ) ;
SbiSymDef * pDef = aLvalue . GetRealVar ( ) ;
if ( pDef & & pDef - > GetConstDef ( ) )
2012-11-03 09:07:25 -05:00
{
2000-09-18 15:18:56 +00:00
Error ( SbERR_DUPLICATE_DEF , pDef - > GetName ( ) ) ;
2012-11-03 09:07:25 -05:00
}
2000-09-18 15:18:56 +00:00
SbiExpression aExpr ( this ) ;
aLvalue . Gen ( ) ;
aExpr . Gen ( ) ;
aGen . Gen ( _LSET ) ;
}
// JSM 07.10.95
void SbiParser : : RSet ( )
{
SbiExpression aLvalue ( this , SbLVALUE ) ;
if ( aLvalue . GetType ( ) ! = SbxSTRING )
2012-11-03 09:07:25 -05:00
{
2000-09-18 15:18:56 +00:00
Error ( SbERR_INVALID_OBJECT ) ;
2012-11-03 09:07:25 -05:00
}
2000-09-18 15:18:56 +00:00
TestToken ( EQ ) ;
SbiSymDef * pDef = aLvalue . GetRealVar ( ) ;
if ( pDef & & pDef - > GetConstDef ( ) )
Error ( SbERR_DUPLICATE_DEF , pDef - > GetName ( ) ) ;
SbiExpression aExpr ( this ) ;
aLvalue . Gen ( ) ;
aExpr . Gen ( ) ;
aGen . Gen ( _RSET ) ;
}
2011-08-27 21:37:14 +02:00
// DEFINT, DEFLNG, DEFSNG, DEFDBL, DEFSTR and so on
2000-09-18 15:18:56 +00:00
void SbiParser : : DefXXX ( )
{
sal_Unicode ch1 , ch2 ;
SbxDataType t = SbxDataType ( eCurTok - DEFINT + SbxINTEGER ) ;
while ( ! bAbort )
{
if ( Next ( ) ! = SYMBOL ) break ;
2011-10-18 14:50:21 -04:00
ch1 = aSym . toAsciiUpperCase ( ) [ 0 ] ;
2000-09-18 15:18:56 +00:00
ch2 = 0 ;
if ( Peek ( ) = = MINUS )
{
Next ( ) ;
if ( Next ( ) ! = SYMBOL ) Error ( SbERR_SYMBOL_EXPECTED ) ;
else
{
2011-10-18 14:50:21 -04:00
ch2 = aSym . toAsciiUpperCase ( ) [ 0 ] ;
2000-09-18 15:18:56 +00:00
if ( ch2 < ch1 ) Error ( SbERR_SYNTAX ) , ch2 = 0 ;
}
}
if ( ! ch2 ) ch2 = ch1 ;
ch1 - = ' A ' ; ch2 - = ' A ' ;
for ( ; ch1 < = ch2 ; ch1 + + ) eDefTypes [ ch1 ] = t ;
if ( ! TestComma ( ) ) break ;
}
}
// STOP/SYSTEM
void SbiParser : : Stop ( )
{
aGen . Gen ( _STOP ) ;
2011-08-27 21:37:14 +02:00
Peek ( ) ; // #35694: only Peek(), so that EOL is recognized in Single-Line-If
2000-09-18 15:18:56 +00:00
}
2005-03-29 10:49:55 +00:00
// IMPLEMENTS
void SbiParser : : Implements ( )
{
if ( ! bClassModule )
{
Error ( SbERR_UNEXPECTED , IMPLEMENTS ) ;
return ;
}
2010-06-15 20:02:53 +02:00
Peek ( ) ;
if ( eCurTok ! = SYMBOL )
{
Error ( SbERR_SYMBOL_EXPECTED ) ;
return ;
}
2012-11-06 23:34:23 -06:00
OUString aImplementedIface = aSym ;
2010-06-15 20:02:53 +02:00
Next ( ) ;
if ( Peek ( ) = = DOT )
2005-03-29 10:49:55 +00:00
{
2012-11-06 23:34:23 -06:00
OUString aDotStr ( ' . ' ) ;
2010-06-15 20:02:53 +02:00
while ( Peek ( ) = = DOT )
{
aImplementedIface + = aDotStr ;
Next ( ) ;
SbiToken ePeekTok = Peek ( ) ;
if ( ePeekTok = = SYMBOL | | IsKwd ( ePeekTok ) )
{
Next ( ) ;
aImplementedIface + = aSym ;
}
else
{
Next ( ) ;
Error ( SbERR_SYMBOL_EXPECTED ) ;
break ;
}
}
2005-03-29 10:49:55 +00:00
}
2010-06-15 20:02:53 +02:00
aIfaceVector . push_back ( aImplementedIface ) ;
2005-03-29 10:49:55 +00:00
}
2006-05-05 09:11:46 +00:00
void SbiParser : : EnableCompatibility ( )
{
if ( ! bCompatible )
AddConstants ( ) ;
2014-01-28 20:03:03 +01:00
bCompatible = true ;
2006-05-05 09:11:46 +00:00
}
2000-09-18 15:18:56 +00:00
// OPTION
void SbiParser : : Option ( )
{
switch ( Next ( ) )
{
2014-01-06 14:10:06 +00:00
case BASIC_EXPLICIT :
2012-08-19 17:49:02 +09:00
bExplicit = true ; break ;
2000-09-18 15:18:56 +00:00
case BASE :
if ( Next ( ) = = NUMBER )
{
if ( nVal = = 0 | | nVal = = 1 )
{
nBase = ( short ) nVal ;
break ;
}
}
Error ( SbERR_EXPECTED , " 0/1 " ) ;
break ;
case PRIVATE :
{
2012-11-06 23:34:23 -06:00
OUString aString = SbiTokenizer : : Symbol ( Next ( ) ) ;
if ( ! aString . equalsIgnoreAsciiCase ( " Module " ) )
{
2000-09-18 15:18:56 +00:00
Error ( SbERR_EXPECTED , " Module " ) ;
2012-11-06 23:34:23 -06:00
}
2000-09-18 15:18:56 +00:00
break ;
}
case COMPARE :
2010-06-15 20:02:53 +02:00
{
SbiToken eTok = Next ( ) ;
if ( eTok = = BINARY )
2012-11-03 09:07:25 -05:00
{
2012-08-20 09:44:32 +09:00
bText = false ;
2012-11-03 09:07:25 -05:00
}
2012-12-30 13:07:28 -02:00
else if ( eTok = = SYMBOL & & GetSym ( ) . equalsIgnoreAsciiCase ( " text " ) )
2012-11-03 09:07:25 -05:00
{
2012-08-20 09:44:32 +09:00
bText = true ;
2012-11-03 09:07:25 -05:00
}
2010-06-15 20:02:53 +02:00
else
2012-11-03 09:07:25 -05:00
{
2010-06-15 20:02:53 +02:00
Error ( SbERR_EXPECTED , " Text/Binary " ) ;
2012-11-03 09:07:25 -05:00
}
2010-06-15 20:02:53 +02:00
break ;
}
2004-03-17 12:33:25 +00:00
case COMPATIBLE :
2006-05-05 09:11:46 +00:00
EnableCompatibility ( ) ;
2004-03-17 12:33:25 +00:00
break ;
2004-11-02 10:54:14 +00:00
case CLASSMODULE :
2012-08-20 09:44:32 +09:00
bClassModule = true ;
2010-04-16 17:28:52 +02:00
aGen . GetModule ( ) . SetModuleType ( com : : sun : : star : : script : : ModuleType : : CLASS ) ;
2004-11-02 10:54:14 +00:00
break ;
2010-10-06 10:16:27 +01:00
case VBASUPPORT : // Option VBASupport used to override the module mode ( in fact this must reset the mode
2006-05-05 09:11:46 +00:00
if ( Next ( ) = = NUMBER )
{
if ( nVal = = 1 | | nVal = = 0 )
{
bVBASupportOn = ( nVal = = 1 ) ;
if ( bVBASupportOn )
2012-11-03 09:07:25 -05:00
{
2006-05-05 09:11:46 +00:00
EnableCompatibility ( ) ;
2012-11-03 09:07:25 -05:00
}
2010-03-02 12:39:31 +00:00
// if the module setting is different
// reset it to what the Option tells us
if ( bVBASupportOn ! = aGen . GetModule ( ) . IsVBACompat ( ) )
2012-11-03 09:07:25 -05:00
{
2010-03-02 12:39:31 +00:00
aGen . GetModule ( ) . SetVBACompat ( bVBASupportOn ) ;
2012-11-03 09:07:25 -05:00
}
2006-05-05 09:11:46 +00:00
break ;
}
}
Error ( SbERR_EXPECTED , " 0/1 " ) ;
break ;
2000-09-18 15:18:56 +00:00
default :
Error ( SbERR_BAD_OPTION , eCurTok ) ;
}
}
2012-11-06 23:34:23 -06:00
void addStringConst ( SbiSymPool & rPool , const char * pSym , const OUString & rStr )
2004-03-17 12:33:25 +00:00
{
2012-11-06 23:34:23 -06:00
SbiConstDef * pConst = new SbiConstDef ( OUString : : createFromAscii ( pSym ) ) ;
2004-03-17 12:33:25 +00:00
pConst - > SetType ( SbxSTRING ) ;
2005-03-29 10:49:55 +00:00
pConst - > Set ( rStr ) ;
2004-03-17 12:33:25 +00:00
rPool . Add ( pConst ) ;
}
2005-03-29 10:49:55 +00:00
inline void addStringConst ( SbiSymPool & rPool , const char * pSym , const char * pStr )
{
2012-12-30 13:07:28 -02:00
addStringConst ( rPool , pSym , OUString : : createFromAscii ( pStr ) ) ;
2005-03-29 10:49:55 +00:00
}
2004-03-17 12:33:25 +00:00
void SbiParser : : AddConstants ( void )
{
// #113063 Create constant RTL symbols
addStringConst ( aPublics , " vbCr " , " \x0D " ) ;
addStringConst ( aPublics , " vbCrLf " , " \x0D \x0A " ) ;
addStringConst ( aPublics , " vbFormFeed " , " \x0C " ) ;
addStringConst ( aPublics , " vbLf " , " \x0A " ) ;
2013-10-12 09:55:48 +03:00
# ifndef WNT
2004-03-17 12:33:25 +00:00
addStringConst ( aPublics , " vbNewLine " , " \x0D \x0A " ) ;
2013-10-12 09:55:48 +03:00
# else
addStringConst ( aPublics , " vbNewLine " , " \x0A " ) ;
2004-03-17 12:33:25 +00:00
# endif
2005-03-29 10:49:55 +00:00
addStringConst ( aPublics , " vbNullString " , " " ) ;
2004-03-17 12:33:25 +00:00
addStringConst ( aPublics , " vbTab " , " \x09 " ) ;
addStringConst ( aPublics , " vbVerticalTab " , " \x0B " ) ;
2005-03-29 10:49:55 +00:00
// Force length 1 and make char 0 afterwards
2012-11-06 23:34:23 -06:00
OUString aNullCharStr ( ( sal_Unicode ) 0 ) ;
2005-03-29 10:49:55 +00:00
addStringConst ( aPublics , " vbNullChar " , aNullCharStr ) ;
2004-03-17 12:33:25 +00:00
}
2000-09-18 15:18:56 +00:00
// ERROR n
void SbiParser : : ErrorStmnt ( )
{
SbiExpression aPar ( this ) ;
aPar . Gen ( ) ;
aGen . Gen ( _ERROR ) ;
}
2010-10-12 15:53:47 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */