LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/oox - formulaparser.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 131 1423 9.2 %
Date: 2012-12-27 Functions: 23 210 11.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "formulaparser.hxx"
      21             : 
      22             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      23             : #include <com/sun/star/sheet/ComplexReference.hpp>
      24             : #include <com/sun/star/sheet/ExternalReference.hpp>
      25             : #include <com/sun/star/sheet/FormulaToken.hpp>
      26             : #include <com/sun/star/sheet/NameToken.hpp>
      27             : #include <com/sun/star/sheet/ReferenceFlags.hpp>
      28             : #include <com/sun/star/sheet/SingleReference.hpp>
      29             : #include "oox/core/filterbase.hxx"
      30             : #include "oox/token/properties.hxx"
      31             : #include "addressconverter.hxx"
      32             : #include "biffinputstream.hxx"
      33             : #include "defnamesbuffer.hxx"
      34             : #include "externallinkbuffer.hxx"
      35             : #include "tablebuffer.hxx"
      36             : #include "worksheethelper.hxx"
      37             : 
      38             : namespace oox {
      39             : namespace xls {
      40             : 
      41             : // ============================================================================
      42             : 
      43             : using namespace ::com::sun::star::sheet;
      44             : using namespace ::com::sun::star::sheet::ReferenceFlags;
      45             : using namespace ::com::sun::star::table;
      46             : using namespace ::com::sun::star::uno;
      47             : 
      48             : using ::rtl::OUString;
      49             : 
      50             : // ============================================================================
      51             : 
      52             : namespace {
      53             : 
      54           0 : sal_uInt16 lclReadFmlaSize( BiffInputStream& rStrm, BiffType eBiff, const sal_uInt16* pnFmlaSize )
      55             : {
      56           0 :     return pnFmlaSize ? *pnFmlaSize : ((eBiff == BIFF2) ? rStrm.readuInt8() : rStrm.readuInt16());
      57             : }
      58             : 
      59             : } // namespace
      60             : 
      61             : // formula finalizer ==========================================================
      62             : 
      63          11 : FormulaFinalizer::FormulaFinalizer( const OpCodeProvider& rOpCodeProv ) :
      64             :     OpCodeProvider( rOpCodeProv ),
      65          11 :     ApiOpCodes( getOpCodes() )
      66             : {
      67          11 :     maTokens.reserve( 0x2000 );
      68          11 : }
      69             : 
      70          58 : ApiTokenSequence FormulaFinalizer::finalizeTokenArray( const ApiTokenSequence& rTokens )
      71             : {
      72          58 :     maTokens.clear();
      73          58 :     if( rTokens.hasElements() )
      74             :     {
      75          58 :         const ApiToken* pToken = rTokens.getConstArray();
      76          58 :         processTokens( pToken, pToken + rTokens.getLength() );
      77             :     }
      78          58 :     return ContainerHelper::vectorToSequence( maTokens );
      79             : }
      80             : 
      81           0 : const FunctionInfo* FormulaFinalizer::resolveBadFuncName( const OUString& ) const
      82             : {
      83           0 :     return 0;
      84             : }
      85             : 
      86           0 : OUString FormulaFinalizer::resolveDefinedName( sal_Int32 ) const
      87             : {
      88           0 :     return OUString();
      89             : }
      90             : 
      91          90 : const FunctionInfo* FormulaFinalizer::getFunctionInfo( ApiToken& orFuncToken )
      92             : {
      93             :     // first, try to find a regular function info from token op-code
      94          90 :     if( const FunctionInfo* pRegFuncInfo = getFuncInfoFromApiToken( orFuncToken ) )
      95          10 :         return pRegFuncInfo;
      96             : 
      97             :     // try to recognize a function from an external library
      98          80 :     if( (orFuncToken.OpCode == OPCODE_BAD) && orFuncToken.Data.has< OUString >() )
      99             :     {
     100             :         // virtual call to resolveBadFuncName()
     101           2 :         if( const FunctionInfo* pLibFuncInfo = resolveBadFuncName( orFuncToken.Data.get< OUString >() ) )
     102             :         {
     103             :             // write function op-code to the OPCODE_BAD token
     104           0 :             orFuncToken.OpCode = pLibFuncInfo->mnApiOpCode;
     105             :             // if it is an external function, insert programmatic function name
     106           0 :             if( (orFuncToken.OpCode == OPCODE_EXTERNAL) && (!pLibFuncInfo->maExtProgName.isEmpty()) )
     107           0 :                 orFuncToken.Data <<= pLibFuncInfo->maExtProgName;
     108             :             else
     109           0 :                 orFuncToken.Data.clear();   // clear string from OPCODE_BAD
     110           0 :             return pLibFuncInfo;
     111             :         }
     112             :     }
     113             : 
     114             :     // no success - return null
     115          80 :     return 0;
     116             : }
     117             : 
     118           0 : const FunctionInfo* FormulaFinalizer::getExternCallInfo( ApiToken& orFuncToken, const ApiToken& rECToken )
     119             : {
     120             :     // try to resolve the passed token to a supported sheet function
     121           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromApiToken( rECToken ) )
     122             :     {
     123           0 :         orFuncToken.OpCode = pFuncInfo->mnApiOpCode;
     124             :         // programmatic add-in function name
     125           0 :         if( (pFuncInfo->mnApiOpCode == OPCODE_EXTERNAL) && !pFuncInfo->maExtProgName.isEmpty() )
     126           0 :             orFuncToken.Data <<= pFuncInfo->maExtProgName;
     127             :         // name of unsupported function, convert to OPCODE_BAD to preserve the name
     128           0 :         else if( (pFuncInfo->mnApiOpCode == OPCODE_BAD) && !pFuncInfo->maOoxFuncName.isEmpty() )
     129           0 :             orFuncToken.Data <<= pFuncInfo->maOoxFuncName;
     130           0 :         return pFuncInfo;
     131             :     }
     132             : 
     133             :     // macro call or unknown function name, move data to function token
     134           0 :     if( (rECToken.OpCode == OPCODE_MACRO) || (rECToken.OpCode == OPCODE_BAD) )
     135           0 :         orFuncToken = rECToken;
     136             : 
     137             :     // defined name used as function call, convert to OPCODE_BAD to preserve the name
     138           0 :     if( (rECToken.OpCode == OPCODE_NAME) && rECToken.Data.has< sal_Int32 >() )
     139             :     {
     140           0 :         OUString aDefName = resolveDefinedName( rECToken.Data.get< sal_Int32 >() );
     141           0 :         if( !aDefName.isEmpty() )
     142             :         {
     143           0 :             orFuncToken.OpCode = OPCODE_BAD;
     144           0 :             orFuncToken.Data <<= aDefName;
     145           0 :         }
     146             :     }
     147             : 
     148           0 :     return 0;
     149             : }
     150             : 
     151          76 : void FormulaFinalizer::processTokens( const ApiToken* pToken, const ApiToken* pTokenEnd )
     152             : {
     153         242 :     while( pToken < pTokenEnd )
     154             :     {
     155             :         // push the current token into the vector
     156          90 :         bool bValid = appendFinalToken( *pToken );
     157             :         // try to process a function
     158          90 :         if( const FunctionInfo* pFuncInfo = bValid ? getFunctionInfo( maTokens.back() ) : 0 )
     159          10 :             pToken = processParameters( *pFuncInfo, pToken + 1, pTokenEnd );
     160             :         // otherwise, go to next token
     161             :         else
     162          80 :             ++pToken;
     163             :     }
     164          76 : }
     165             : 
     166          10 : const ApiToken* FormulaFinalizer::processParameters(
     167             :         const FunctionInfo& rFuncInfo, const ApiToken* pToken, const ApiToken* pTokenEnd )
     168             : {
     169             :     // remember position of the token containing the function op-code
     170          10 :     size_t nFuncNameIdx = maTokens.size() - 1;
     171             : 
     172             :     // process a function, if an OPCODE_OPEN token is following
     173             :     OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "FormulaFinalizer::processParameters - OPCODE_OPEN expected" );
     174          10 :     if( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN) )
     175             :     {
     176             :         // append the OPCODE_OPEN token to the vector
     177          10 :         maTokens.append( OPCODE_OPEN );
     178             : 
     179             :         // store positions of OPCODE_OPEN, parameter separators, and OPCODE_CLOSE
     180          10 :         ParameterPosVector aParams;
     181          10 :         pToken = findParameters( aParams, pToken, pTokenEnd );
     182             :         OSL_ENSURE( aParams.size() >= 2, "FormulaFinalizer::processParameters - missing tokens" );
     183          10 :         size_t nParamCount = aParams.size() - 1;
     184             : 
     185          10 :         if( (nParamCount == 1) && isEmptyParameter( aParams[ 0 ] + 1, aParams[ 1 ] ) )
     186             :         {
     187             :             /*  Empty pair of parentheses -> function call without parameters,
     188             :                 process parameter, there might be spaces between parentheses. */
     189           2 :             processTokens( aParams[ 0 ] + 1, aParams[ 1 ] );
     190             :         }
     191             :         else
     192             :         {
     193           8 :             const FunctionInfo* pRealFuncInfo = &rFuncInfo;
     194           8 :             ParameterPosVector::const_iterator aPosIt = aParams.begin();
     195             : 
     196             :             /*  Preprocess EXTERN.CALL functions. The actual function name is
     197             :                 contained as reference to a defined name in the first (hidden)
     198             :                 parameter. */
     199           8 :             if( rFuncInfo.mnBiffFuncId == BIFF_FUNC_EXTERNCALL )
     200             :             {
     201           0 :                 ApiToken& rFuncToken = maTokens[ nFuncNameIdx ];
     202           0 :                 rFuncToken.OpCode = OPCODE_NONAME;
     203             : 
     204             :                 // try to initialize function token from first parameter
     205           0 :                 if( const ApiToken* pECToken = getSingleToken( *aPosIt + 1, *(aPosIt + 1) ) )
     206           0 :                     if( const FunctionInfo* pECFuncInfo = getExternCallInfo( rFuncToken, *pECToken ) )
     207           0 :                         pRealFuncInfo = pECFuncInfo;
     208             : 
     209             :                 /*  On success (something has been inserted into rFuncToken),
     210             :                     skip the first parameter. */
     211           0 :                 if( rFuncToken.OpCode != OPCODE_NONAME )
     212             :                 {
     213           0 :                     --nParamCount;
     214           0 :                     ++aPosIt;
     215             :                 }
     216             :             }
     217             : 
     218             :             // process all parameters
     219           8 :             FunctionParamInfoIterator aParamInfoIt( *pRealFuncInfo );
     220           8 :             size_t nLastValidSize = maTokens.size();
     221           8 :             size_t nLastValidCount = 0;
     222          24 :             for( size_t nParam = 0; nParam < nParamCount; ++nParam, ++aPosIt, ++aParamInfoIt )
     223             :             {
     224             :                 // add embedded Calc-only parameters
     225          16 :                 if( aParamInfoIt.isCalcOnlyParam() )
     226             :                 {
     227           0 :                     appendCalcOnlyParameter( *pRealFuncInfo, nParam );
     228           0 :                     while( aParamInfoIt.isCalcOnlyParam() ) ++aParamInfoIt;
     229             :                 }
     230             : 
     231          16 :                 const ApiToken* pParamBegin = *aPosIt + 1;
     232          16 :                 const ApiToken* pParamEnd = *(aPosIt + 1);
     233          16 :                 bool bIsEmpty = isEmptyParameter( pParamBegin, pParamEnd );
     234             : 
     235          16 :                 if( !aParamInfoIt.isExcelOnlyParam() )
     236             :                 {
     237             :                     // handle empty parameters
     238          16 :                     if( bIsEmpty )
     239             :                     {
     240             :                         // append leading space tokens from original token array
     241           0 :                         while( (pParamBegin < pParamEnd) && (pParamBegin->OpCode == OPCODE_SPACES) )
     242           0 :                             maTokens.push_back( *pParamBegin++ );
     243             :                         // add default values for some empty parameters, or the OPCODE_MISSING token
     244           0 :                         appendEmptyParameter( *pRealFuncInfo, nParam );
     245             :                         // reset bIsEmpty flag, if something has been appended in appendEmptyParameter()
     246           0 :                         bIsEmpty = maTokens.back().OpCode == OPCODE_MISSING;
     247             :                         // skip OPCODE_MISSING token in the original token array
     248             :                         OSL_ENSURE( (pParamBegin == pParamEnd) || (pParamBegin->OpCode == OPCODE_MISSING), "FormulaFinalizer::processParameters - OPCODE_MISSING expected" );
     249           0 :                         if( pParamBegin < pParamEnd ) ++pParamBegin;
     250             :                         // append trailing space tokens from original token array
     251           0 :                         while( (pParamBegin < pParamEnd) && (pParamBegin->OpCode == OPCODE_SPACES) )
     252           0 :                             maTokens.push_back( *pParamBegin++ );
     253             :                     }
     254             :                     else
     255             :                     {
     256             :                         // if parameter is not empty, process all tokens of the parameter
     257          16 :                         processTokens( pParamBegin, pParamEnd );
     258             :                     }
     259             : 
     260             :                     // append parameter separator token
     261          16 :                     maTokens.append( OPCODE_SEP );
     262             :                 }
     263             : 
     264             :                 /*  #84453# Update size of new token sequence with valid parameters
     265             :                     to be able to remove trailing optional empty parameters. */
     266          16 :                 if( !bIsEmpty || (nParam < pRealFuncInfo->mnMinParamCount) )
     267             :                 {
     268          16 :                     nLastValidSize = maTokens.size();
     269          16 :                     nLastValidCount = nParam + 1;
     270             :                 }
     271             :             }
     272             : 
     273             :             // #84453# remove trailing optional empty parameters
     274           8 :             maTokens.resize( nLastValidSize );
     275             : 
     276             :             // add trailing Calc-only parameters
     277           8 :             if( aParamInfoIt.isCalcOnlyParam() )
     278           0 :                 appendCalcOnlyParameter( *pRealFuncInfo, nLastValidCount );
     279             : 
     280             :             // add optional parameters that are required in Calc
     281           8 :             appendRequiredParameters( *pRealFuncInfo, nLastValidCount );
     282             : 
     283             :             // remove last parameter separator token
     284           8 :             if( maTokens.back().OpCode == OPCODE_SEP )
     285           8 :                 maTokens.pop_back();
     286             :         }
     287             : 
     288             :         /*  Append the OPCODE_CLOSE token to the vector, but only if there is
     289             :             no OPCODE_BAD token at the end, this token already contains the
     290             :             trailing closing parentheses. */
     291          10 :         if( (pTokenEnd - 1)->OpCode != OPCODE_BAD )
     292          10 :             maTokens.append( OPCODE_CLOSE );
     293             :     }
     294             : 
     295             :     /*  Replace OPCODE_EXTERNAL with OPCODE_NONAME to get #NAME! error in cell,
     296             :         if no matching add-in function was found. */
     297          10 :     ApiToken& rFuncNameToken = maTokens[ nFuncNameIdx ];
     298          10 :     if( (rFuncNameToken.OpCode == OPCODE_EXTERNAL) && !rFuncNameToken.Data.hasValue() )
     299           0 :         rFuncNameToken.OpCode = OPCODE_NONAME;
     300             : 
     301          10 :     return pToken;
     302             : }
     303             : 
     304          22 : bool FormulaFinalizer::isEmptyParameter( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     305             : {
     306          22 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     307          22 :     if( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_MISSING) ) ++pToken;
     308          22 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     309          22 :     return pToken == pTokenEnd;
     310             : }
     311             : 
     312           0 : const ApiToken* FormulaFinalizer::getSingleToken( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     313             : {
     314           0 :     const ApiToken* pSingleToken = 0;
     315             :     // skip leading whitespace tokens
     316           0 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     317             :     // remember first non-whitespace token
     318           0 :     if( pToken < pTokenEnd ) pSingleToken = pToken++;
     319             :     // skip trailing whitespace tokens
     320           0 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     321             :     // return null, if other non-whitespace tokens follow
     322           0 :     return (pToken == pTokenEnd) ? pSingleToken : 0;
     323             : }
     324             : 
     325           0 : const ApiToken* FormulaFinalizer::skipParentheses( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     326             : {
     327             :     // skip tokens between OPCODE_OPEN and OPCODE_CLOSE
     328             :     OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "skipParentheses - OPCODE_OPEN expected" );
     329           0 :     ++pToken;
     330           0 :     while( (pToken < pTokenEnd) && (pToken->OpCode != OPCODE_CLOSE) )
     331             :     {
     332           0 :         if( pToken->OpCode == OPCODE_OPEN )
     333           0 :             pToken = skipParentheses( pToken, pTokenEnd );
     334             :         else
     335           0 :             ++pToken;
     336             :     }
     337             :     // skip the OPCODE_CLOSE token
     338             :     OSL_ENSURE( ((pToken < pTokenEnd) && (pToken->OpCode == OPCODE_CLOSE)) || ((pTokenEnd - 1)->OpCode == OPCODE_BAD), "skipParentheses - OPCODE_CLOSE expected" );
     339           0 :     return (pToken < pTokenEnd) ? (pToken + 1) : pTokenEnd;
     340             : }
     341             : 
     342          10 : const ApiToken* FormulaFinalizer::findParameters( ParameterPosVector& rParams,
     343             :         const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     344             : {
     345             :     // push position of OPCODE_OPEN
     346             :     OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "FormulaFinalizer::findParameters - OPCODE_OPEN expected" );
     347          10 :     rParams.push_back( pToken++ );
     348             : 
     349             :     // find positions of parameter separators
     350          46 :     while( (pToken < pTokenEnd) && (pToken->OpCode != OPCODE_CLOSE) )
     351             :     {
     352          26 :         if( pToken->OpCode == OPCODE_OPEN )
     353           0 :             pToken = skipParentheses( pToken, pTokenEnd );
     354          26 :         else if( pToken->OpCode == OPCODE_SEP )
     355           8 :             rParams.push_back( pToken++ );
     356             :         else
     357          18 :             ++pToken;
     358             :     }
     359             : 
     360             :     // push position of OPCODE_CLOSE
     361             :     OSL_ENSURE( ((pToken < pTokenEnd) && (pToken->OpCode == OPCODE_CLOSE)) || ((pTokenEnd - 1)->OpCode == OPCODE_BAD), "FormulaFinalizer::findParameters - OPCODE_CLOSE expected" );
     362          10 :     rParams.push_back( pToken );
     363          10 :     return (pToken < pTokenEnd) ? (pToken + 1) : pTokenEnd;
     364             : }
     365             : 
     366           0 : void FormulaFinalizer::appendEmptyParameter( const FunctionInfo& rFuncInfo, size_t nParam )
     367             : {
     368             :     // remember old size of the token array
     369           0 :     size_t nTokenArraySize = maTokens.size();
     370             : 
     371           0 :     switch( rFuncInfo.mnBiff12FuncId )
     372             :     {
     373             :         case BIFF_FUNC_IF:
     374           0 :             if( (nParam == 1) || (nParam == 2) )
     375           0 :                 maTokens.append< double >( OPCODE_PUSH, 0.0 );
     376           0 :         break;
     377             :         default:;
     378             :     }
     379             : 
     380             :     // if no token has been added, append a OPCODE_MISSING token
     381           0 :     if( nTokenArraySize == maTokens.size() )
     382           0 :         maTokens.append( OPCODE_MISSING );
     383           0 : }
     384             : 
     385           0 : void FormulaFinalizer::appendCalcOnlyParameter( const FunctionInfo& rFuncInfo, size_t nParam )
     386             : {
     387             :     (void)nParam;   // prevent 'unused' warning
     388           0 :     switch( rFuncInfo.mnBiff12FuncId )
     389             :     {
     390             :         case BIFF_FUNC_FLOOR:
     391             :         case BIFF_FUNC_CEILING:
     392             :             OSL_ENSURE( nParam == 2, "FormulaFinalizer::appendCalcOnlyParameter - unexpected parameter index" );
     393           0 :             maTokens.append< double >( OPCODE_PUSH, 1.0 );
     394           0 :             maTokens.append( OPCODE_SEP );
     395           0 :         break;
     396             :     }
     397           0 : }
     398             : 
     399           8 : void FormulaFinalizer::appendRequiredParameters( const FunctionInfo& rFuncInfo, size_t nParamCount )
     400             : {
     401           8 :     switch( rFuncInfo.mnBiff12FuncId )
     402             :     {
     403             :         case BIFF_FUNC_WEEKNUM:
     404           0 :             if( nParamCount == 1 )
     405             :             {
     406           0 :                 maTokens.append< double >( OPCODE_PUSH, 1.0 );
     407           0 :                 maTokens.append( OPCODE_SEP );
     408             :             }
     409           0 :         break;
     410             :     }
     411           8 : }
     412             : 
     413          90 : bool FormulaFinalizer::appendFinalToken( const ApiToken& rToken )
     414             : {
     415             :     // replace OPCODE_MACRO without macro name with #NAME? error code
     416          90 :     bool bValid = (rToken.OpCode != OPCODE_MACRO) || rToken.Data.hasValue();
     417          90 :     if( bValid )
     418             :     {
     419          90 :         maTokens.push_back( rToken );
     420             :     }
     421             :     else
     422             :     {
     423           0 :         maTokens.append( OPCODE_ARRAY_OPEN );
     424           0 :         maTokens.append( OPCODE_PUSH, BiffHelper::calcDoubleFromError( BIFF_ERR_NAME ) );
     425           0 :         maTokens.append( OPCODE_ARRAY_CLOSE );
     426             :     }
     427          90 :     return bValid;
     428             : }
     429             : 
     430             : // parser implementation base =================================================
     431             : 
     432          11 : class FormulaParserImpl : public FormulaFinalizer, public WorkbookHelper
     433             : {
     434             : public:
     435             :     explicit            FormulaParserImpl( const FormulaParser& rParent );
     436             : 
     437             :     /** Converts an OOXML formula string. */
     438             :     virtual ApiTokenSequence importOoxFormula(
     439             :                             const CellAddress& rBaseAddress,
     440             :                             const OUString& rFormulaString );
     441             : 
     442             :     /** Imports and converts a BIFF12 token array from the passed stream. */
     443             :     virtual ApiTokenSequence importBiff12Formula(
     444             :                             const CellAddress& rBaseAddress,
     445             :                             FormulaType eType,
     446             :                             SequenceInputStream& rStrm );
     447             : 
     448             :     /** Imports and converts a BIFF2-BIFF8 token array from the passed stream. */
     449             :     virtual ApiTokenSequence importBiffFormula(
     450             :                             const CellAddress& rBaseAddress,
     451             :                             FormulaType eType,
     452             :                             BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize );
     453             : 
     454             :     /** Tries to resolve the passed ref-id to an OLE target URL. */
     455             :     OUString            resolveOleTarget( sal_Int32 nRefId, bool bUseRefSheets ) const;
     456             : 
     457             : protected:
     458             :     typedef ::std::pair< sal_Int32, bool >  WhiteSpace;
     459             :     typedef ::std::vector< WhiteSpace >     WhiteSpaceVec;
     460             : 
     461             :     /** Initializes the formula parser before importing a formula. */
     462             :     void                initializeImport( const CellAddress& rBaseAddress, FormulaType eType );
     463             :     /** Finalizes the internal token storage after import. */
     464             :     ApiTokenSequence    finalizeImport();
     465             : 
     466             :     // token array ------------------------------------------------------------
     467             : 
     468             :     bool                resetSpaces();
     469             :     static void         appendSpaces( WhiteSpaceVec& orSpaces, sal_Int32 nCount, bool bLineFeed );
     470             :     void                appendLeadingSpaces( sal_Int32 nCount, bool bLineFeed );
     471             :     void                appendOpeningSpaces( sal_Int32 nCount, bool bLineFeed );
     472             :     void                appendClosingSpaces( sal_Int32 nCount, bool bLineFeed );
     473             : 
     474             :     size_t              getFormulaSize() const;
     475             :     Any&                appendRawToken( sal_Int32 nOpCode );
     476             :     Any&                insertRawToken( sal_Int32 nOpCode, size_t nIndexFromEnd );
     477             :     size_t              appendWhiteSpaceTokens( const WhiteSpaceVec* pSpaces );
     478             :     size_t              insertWhiteSpaceTokens( const WhiteSpaceVec* pSpaces, size_t nIndexFromEnd );
     479             : 
     480             :     size_t              getOperandSize( size_t nOpCountFromEnd, size_t nOpIndex ) const;
     481             :     void                pushOperandSize( size_t nSize );
     482             :     size_t              popOperandSize();
     483             : 
     484             :     ApiToken&           getOperandToken( size_t nOpCountFromEnd, size_t nOpIndex, size_t nTokenIndex );
     485             :     void                removeOperand( size_t nOpCountFromEnd, size_t nOpIndex );
     486             : 
     487             :     bool                pushOperandToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     488             :     bool                pushAnyOperandToken( const Any& rAny, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     489             :     template< typename Type >
     490             :     bool                pushValueOperandToken( const Type& rValue, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     491             :     template< typename Type >
     492           0 :     inline bool         pushValueOperandToken( const Type& rValue, const WhiteSpaceVec* pSpaces = 0 )
     493           0 :                             { return pushValueOperandToken( rValue, OPCODE_PUSH, pSpaces ); }
     494             :     bool                pushParenthesesOperandToken( const WhiteSpaceVec* pOpeningSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     495             :     bool                pushUnaryPreOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     496             :     bool                pushUnaryPostOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     497             :     bool                pushBinaryOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     498             :     bool                pushParenthesesOperatorToken( const WhiteSpaceVec* pOpeningSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     499             :     bool                pushFunctionOperatorToken( sal_Int32 nOpCode, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     500             :     bool                pushFunctionOperatorToken( const FunctionInfo& rFuncInfo, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     501             : 
     502             :     bool                pushOperand( sal_Int32 nOpCode );
     503             :     bool                pushAnyOperand( const Any& rAny, sal_Int32 nOpCode );
     504             :     template< typename Type >
     505             :     bool                pushValueOperand( const Type& rValue, sal_Int32 nOpCode );
     506             :     template< typename Type >
     507           0 :     inline bool         pushValueOperand( const Type& rValue )
     508           0 :                             { return pushValueOperand( rValue, OPCODE_PUSH ); }
     509             :     bool                pushBoolOperand( bool bValue );
     510             :     bool                pushErrorOperand( double fEncodedError );
     511             :     bool                pushBiffBoolOperand( sal_uInt8 nValue );
     512             :     bool                pushBiffErrorOperand( sal_uInt8 nErrorCode );
     513             :     bool                pushReferenceOperand( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     514             :     bool                pushReferenceOperand( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     515             :     template< typename Type >
     516             :     bool                pushReferenceOperand( const LinkSheetRange& rSheetRange, const Type& rApiRef );
     517             :     bool                pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     518             :     bool                pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     519             :     bool                pushNlrOperand( const BinSingleRef2d& rRef );
     520             :     bool                pushEmbeddedRefOperand( const DefinedNameBase& rName, bool bPushBadToken );
     521             :     bool                pushDefinedNameOperand( const DefinedNameRef& rxDefName );
     522             :     bool                pushExternalFuncOperand( const FunctionInfo& rFuncInfo );
     523             :     bool                pushDdeLinkOperand( const OUString& rDdeServer, const OUString& rDdeTopic, const OUString& rDdeItem );
     524             :     bool                pushExternalNameOperand( const ExternalNameRef& rxExtName, const ExternalLink& rExtLink );
     525             :     bool                pushSpecialTokenOperand( const BinAddress& rBaseAddr, bool bTable );
     526             : 
     527             :     bool                pushUnaryPreOperator( sal_Int32 nOpCode );
     528             :     bool                pushUnaryPostOperator( sal_Int32 nOpCode );
     529             :     bool                pushBinaryOperator( sal_Int32 nOpCode );
     530             :     bool                pushParenthesesOperator();
     531             :     bool                pushFunctionOperator( sal_Int32 nOpCode, size_t nParamCount );
     532             :     bool                pushFunctionOperator( const FunctionInfo& rFuncInfo, size_t nParamCount );
     533             : 
     534             : private:
     535             :     // reference conversion ---------------------------------------------------
     536             : 
     537             :     void                initReference2d( SingleReference& orApiRef ) const;
     538             :     void                initReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet ) const;
     539             :     void                convertColRow( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bRelativeAsOffset ) const;
     540             :     void                convertReference( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
     541             :     void                convertReference( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
     542             :     void                convertReference2d( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
     543             :     void                convertReference2d( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
     544             :     void                convertReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
     545             :     void                convertReference3d( ComplexReference& orApiRef, const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
     546             : 
     547             : private:
     548             :     // finalize token sequence ------------------------------------------------
     549             : 
     550             :     virtual const FunctionInfo* resolveBadFuncName( const OUString& rTokenData ) const;
     551             :     virtual ::rtl::OUString resolveDefinedName( sal_Int32 nTokenIndex ) const;
     552             : 
     553             : protected:
     554             :     const sal_Int32     mnMaxApiCol;                /// Maximum column index in own document.
     555             :     const sal_Int32     mnMaxApiRow;                /// Maximum row index in own document.
     556             :     const sal_Int32     mnMaxXlsCol;                /// Maximum column index in imported document.
     557             :     const sal_Int32     mnMaxXlsRow;                /// Maximum row index in imported document.
     558             : 
     559             :     CellAddress         maBaseAddr;                 /// Base address for relative references.
     560             :     bool                mbRelativeAsOffset;         /// True = relative row/column index is (signed) offset, false = explicit index.
     561             :     bool                mb2dRefsAs3dRefs;           /// True = convert all 2D references to 3D references in sheet specified by base address.
     562             :     bool                mbSpecialTokens;            /// True = special handling for tExp and tTbl tokens, false = exit with error.
     563             :     bool                mbAllowNulChars;            /// True = keep NUL characters in string tokens.
     564             : 
     565             : private:
     566             :     typedef ::std::vector< size_t > SizeTypeVector;
     567             : 
     568             :     ApiTokenVector      maTokenStorage;             /// Raw unordered token storage.
     569             :     SizeTypeVector      maTokenIndexes;             /// Indexes into maTokenStorage.
     570             :     SizeTypeVector      maOperandSizeStack;         /// Stack with token sizes per operand.
     571             :     WhiteSpaceVec       maLeadingSpaces;            /// List of whitespaces before next token.
     572             :     WhiteSpaceVec       maOpeningSpaces;            /// List of whitespaces before opening parenthesis.
     573             :     WhiteSpaceVec       maClosingSpaces;            /// List of whitespaces before closing parenthesis.
     574             : };
     575             : 
     576             : // ----------------------------------------------------------------------------
     577             : 
     578          11 : FormulaParserImpl::FormulaParserImpl( const FormulaParser& rParent ) :
     579             :     FormulaFinalizer( rParent ),
     580             :     WorkbookHelper( rParent ),
     581          11 :     mnMaxApiCol( rParent.getAddressConverter().getMaxApiAddress().Column ),
     582          11 :     mnMaxApiRow( rParent.getAddressConverter().getMaxApiAddress().Row ),
     583          11 :     mnMaxXlsCol( rParent.getAddressConverter().getMaxXlsAddress().Column ),
     584          11 :     mnMaxXlsRow( rParent.getAddressConverter().getMaxXlsAddress().Row ),
     585             :     mbRelativeAsOffset( false ),
     586             :     mb2dRefsAs3dRefs( false ),
     587             :     mbSpecialTokens( false ),
     588          55 :     mbAllowNulChars( false )
     589             : {
     590             :     // reserve enough space to make resize(), push_back() etc. cheap
     591          11 :     maTokenStorage.reserve( 0x2000 );
     592          11 :     maTokenIndexes.reserve( 0x2000 );
     593          11 :     maOperandSizeStack.reserve( 256 );
     594          11 :     maLeadingSpaces.reserve( 256 );
     595          11 :     maOpeningSpaces.reserve( 256 );
     596          11 :     maClosingSpaces.reserve( 256 );
     597          11 : }
     598             : 
     599           0 : ApiTokenSequence FormulaParserImpl::importOoxFormula( const CellAddress&, const OUString& )
     600             : {
     601             :     OSL_FAIL( "FormulaParserImpl::importOoxFormula - not implemented" );
     602           0 :     return ApiTokenSequence();
     603             : }
     604             : 
     605           0 : ApiTokenSequence FormulaParserImpl::importBiff12Formula( const CellAddress&, FormulaType, SequenceInputStream& )
     606             : {
     607             :     OSL_FAIL( "FormulaParserImpl::importBiff12Formula - not implemented" );
     608           0 :     return ApiTokenSequence();
     609             : }
     610             : 
     611           0 : ApiTokenSequence FormulaParserImpl::importBiffFormula( const CellAddress&, FormulaType, BiffInputStream&, const sal_uInt16* )
     612             : {
     613             :     OSL_FAIL( "FormulaParserImpl::importBiffFormula - not implemented" );
     614           0 :     return ApiTokenSequence();
     615             : }
     616             : 
     617           0 : OUString FormulaParserImpl::resolveOleTarget( sal_Int32 nRefId, bool bUseRefSheets ) const
     618             : {
     619           0 :     const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId, bUseRefSheets ).get();
     620             :     OSL_ENSURE( pExtLink && (pExtLink->getLinkType() == LINKTYPE_OLE), "FormulaParserImpl::resolveOleTarget - missing or wrong link" );
     621           0 :     if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_OLE) )
     622           0 :          return getBaseFilter().getAbsoluteUrl( pExtLink->getTargetUrl() );
     623           0 :     return OUString();
     624             : }
     625             : 
     626           0 : void FormulaParserImpl::initializeImport( const CellAddress& rBaseAddr, FormulaType eType )
     627             : {
     628           0 :     maBaseAddr = rBaseAddr;
     629           0 :     mbRelativeAsOffset = mb2dRefsAs3dRefs = mbSpecialTokens = mbAllowNulChars = false;
     630           0 :     switch( eType )
     631             :     {
     632             :         case FORMULATYPE_CELL:
     633           0 :             mbSpecialTokens = true;
     634           0 :         break;
     635             :         case FORMULATYPE_ARRAY:
     636           0 :         break;
     637             :         case FORMULATYPE_SHAREDFORMULA:
     638           0 :             mbRelativeAsOffset = true;
     639           0 :         break;
     640             :         case FORMULATYPE_CONDFORMAT:
     641           0 :             mbRelativeAsOffset = true;
     642           0 :         break;
     643             :         case FORMULATYPE_VALIDATION:
     644           0 :             mbRelativeAsOffset = true;
     645             :             // enable NUL characters in BIFF import, string list is single tStr token with NUL separators
     646           0 :             mbAllowNulChars = getFilterType() == FILTER_BIFF;
     647           0 :         break;
     648             :         case FORMULATYPE_DEFINEDNAME:
     649           0 :             mbRelativeAsOffset = true;
     650             :             // BIFF2-BIFF4: convert 2D referebces to absolute 3D references
     651           0 :             mb2dRefsAs3dRefs = (getFilterType() == FILTER_BIFF) && (getBiff() <= BIFF4);
     652           0 :         break;
     653             :     }
     654             : 
     655           0 :     maTokenStorage.clear();
     656           0 :     maTokenIndexes.clear();
     657           0 :     maOperandSizeStack.clear();
     658           0 : }
     659             : 
     660           0 : ApiTokenSequence FormulaParserImpl::finalizeImport()
     661             : {
     662           0 :     ApiTokenSequence aTokens( static_cast< sal_Int32 >( maTokenIndexes.size() ) );
     663           0 :     if( aTokens.hasElements() )
     664             :     {
     665           0 :         ApiToken* pToken = aTokens.getArray();
     666           0 :         for( SizeTypeVector::const_iterator aIt = maTokenIndexes.begin(), aEnd = maTokenIndexes.end(); aIt != aEnd; ++aIt, ++pToken )
     667           0 :             *pToken = maTokenStorage[ *aIt ];
     668             :     }
     669           0 :     return finalizeTokenArray( aTokens );
     670             : }
     671             : 
     672             : // token array ----------------------------------------------------------------
     673             : 
     674           0 : bool FormulaParserImpl::resetSpaces()
     675             : {
     676           0 :     maLeadingSpaces.clear();
     677           0 :     maOpeningSpaces.clear();
     678           0 :     maClosingSpaces.clear();
     679           0 :     return true;
     680             : }
     681             : 
     682           0 : void FormulaParserImpl::appendSpaces( WhiteSpaceVec& orSpaces, sal_Int32 nCount, bool bLineFeed )
     683             : {
     684             :     OSL_ENSURE( nCount >= 0, "FormulaParserImpl::appendSpaces - negative count" );
     685           0 :     if( nCount > 0 )
     686           0 :         orSpaces.push_back( WhiteSpace( nCount, bLineFeed ) );
     687           0 : }
     688             : 
     689           0 : void FormulaParserImpl::appendLeadingSpaces( sal_Int32 nCount, bool bLineFeed )
     690             : {
     691           0 :     appendSpaces( maLeadingSpaces, nCount, bLineFeed );
     692           0 : }
     693             : 
     694           0 : void FormulaParserImpl::appendOpeningSpaces( sal_Int32 nCount, bool bLineFeed )
     695             : {
     696           0 :     appendSpaces( maOpeningSpaces, nCount, bLineFeed );
     697           0 : }
     698             : 
     699           0 : void FormulaParserImpl::appendClosingSpaces( sal_Int32 nCount, bool bLineFeed )
     700             : {
     701           0 :     appendSpaces( maClosingSpaces, nCount, bLineFeed );
     702           0 : }
     703             : 
     704           0 : size_t FormulaParserImpl::getFormulaSize() const
     705             : {
     706           0 :     return maTokenIndexes.size();
     707             : }
     708             : 
     709           0 : Any& FormulaParserImpl::appendRawToken( sal_Int32 nOpCode )
     710             : {
     711           0 :     maTokenIndexes.push_back( maTokenStorage.size() );
     712           0 :     return maTokenStorage.append( nOpCode );
     713             : }
     714             : 
     715           0 : Any& FormulaParserImpl::insertRawToken( sal_Int32 nOpCode, size_t nIndexFromEnd )
     716             : {
     717           0 :     maTokenIndexes.insert( maTokenIndexes.end() - nIndexFromEnd, maTokenStorage.size() );
     718           0 :     return maTokenStorage.append( nOpCode );
     719             : }
     720             : 
     721           0 : size_t FormulaParserImpl::appendWhiteSpaceTokens( const WhiteSpaceVec* pSpaces )
     722             : {
     723           0 :     if( pSpaces && !pSpaces->empty() )
     724           0 :         for( WhiteSpaceVec::const_iterator aIt = pSpaces->begin(), aEnd = pSpaces->end(); aIt != aEnd; ++aIt )
     725           0 :             appendRawToken( OPCODE_SPACES ) <<= aIt->first;
     726           0 :     return pSpaces ? pSpaces->size() : 0;
     727             : }
     728             : 
     729           0 : size_t FormulaParserImpl::insertWhiteSpaceTokens( const WhiteSpaceVec* pSpaces, size_t nIndexFromEnd )
     730             : {
     731           0 :     if( pSpaces && !pSpaces->empty() )
     732           0 :         for( WhiteSpaceVec::const_iterator aIt = pSpaces->begin(), aEnd = pSpaces->end(); aIt != aEnd; ++aIt )
     733           0 :             insertRawToken( OPCODE_SPACES, nIndexFromEnd ) <<= aIt->first;
     734           0 :     return pSpaces ? pSpaces->size() : 0;
     735             : }
     736             : 
     737           0 : size_t FormulaParserImpl::getOperandSize( size_t nOpCountFromEnd, size_t nOpIndex ) const
     738             : {
     739             :     OSL_ENSURE( (nOpIndex < nOpCountFromEnd) && (nOpCountFromEnd <= maOperandSizeStack.size()),
     740             :         "FormulaParserImpl::getOperandSize - invalid parameters" );
     741           0 :     return maOperandSizeStack[ maOperandSizeStack.size() - nOpCountFromEnd + nOpIndex ];
     742             : }
     743             : 
     744           0 : void FormulaParserImpl::pushOperandSize( size_t nSize )
     745             : {
     746           0 :     maOperandSizeStack.push_back( nSize );
     747           0 : }
     748             : 
     749           0 : size_t FormulaParserImpl::popOperandSize()
     750             : {
     751             :     OSL_ENSURE( !maOperandSizeStack.empty(), "FormulaParserImpl::popOperandSize - invalid call" );
     752           0 :     size_t nOpSize = maOperandSizeStack.back();
     753           0 :     maOperandSizeStack.pop_back();
     754           0 :     return nOpSize;
     755             : }
     756             : 
     757           0 : ApiToken& FormulaParserImpl::getOperandToken( size_t nOpCountFromEnd, size_t nOpIndex, size_t nTokenIndex )
     758             : {
     759             :     OSL_ENSURE( getOperandSize( nOpCountFromEnd, nOpIndex ) > nTokenIndex,
     760             :         "FormulaParserImpl::getOperandToken - invalid parameters" );
     761           0 :     SizeTypeVector::const_iterator aIndexIt = maTokenIndexes.end();
     762           0 :     for( SizeTypeVector::const_iterator aEnd = maOperandSizeStack.end(), aIt = aEnd - nOpCountFromEnd + nOpIndex; aIt != aEnd; ++aIt )
     763           0 :         aIndexIt -= *aIt;
     764           0 :     return maTokenStorage[ *(aIndexIt + nTokenIndex) ];
     765             : }
     766             : 
     767           0 : bool FormulaParserImpl::pushOperandToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     768             : {
     769           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     770           0 :     appendRawToken( nOpCode );
     771           0 :     pushOperandSize( nSpacesSize + 1 );
     772           0 :     return true;
     773             : }
     774             : 
     775           0 : bool FormulaParserImpl::pushAnyOperandToken( const Any& rAny, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     776             : {
     777           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     778           0 :     appendRawToken( nOpCode ) = rAny;
     779           0 :     pushOperandSize( nSpacesSize + 1 );
     780           0 :     return true;
     781             : }
     782             : 
     783             : template< typename Type >
     784           0 : bool FormulaParserImpl::pushValueOperandToken( const Type& rValue, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     785             : {
     786           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     787           0 :     appendRawToken( nOpCode ) <<= rValue;
     788           0 :     pushOperandSize( nSpacesSize + 1 );
     789           0 :     return true;
     790             : }
     791             : 
     792           0 : bool FormulaParserImpl::pushParenthesesOperandToken( const WhiteSpaceVec* pOpeningSpaces, const WhiteSpaceVec* pClosingSpaces )
     793             : {
     794           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pOpeningSpaces );
     795           0 :     appendRawToken( OPCODE_OPEN );
     796           0 :     nSpacesSize += appendWhiteSpaceTokens( pClosingSpaces );
     797           0 :     appendRawToken( OPCODE_CLOSE );
     798           0 :     pushOperandSize( nSpacesSize + 2 );
     799           0 :     return true;
     800             : }
     801             : 
     802           0 : bool FormulaParserImpl::pushUnaryPreOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     803             : {
     804           0 :     bool bOk = maOperandSizeStack.size() >= 1;
     805           0 :     if( bOk )
     806             :     {
     807           0 :         size_t nOpSize = popOperandSize();
     808           0 :         size_t nSpacesSize = insertWhiteSpaceTokens( pSpaces, nOpSize );
     809           0 :         insertRawToken( nOpCode, nOpSize );
     810           0 :         pushOperandSize( nOpSize + nSpacesSize + 1 );
     811             :     }
     812           0 :     return bOk;
     813             : }
     814             : 
     815           0 : bool FormulaParserImpl::pushUnaryPostOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     816             : {
     817           0 :     bool bOk = maOperandSizeStack.size() >= 1;
     818           0 :     if( bOk )
     819             :     {
     820           0 :         size_t nOpSize = popOperandSize();
     821           0 :         size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     822           0 :         appendRawToken( nOpCode );
     823           0 :         pushOperandSize( nOpSize + nSpacesSize + 1 );
     824             :     }
     825           0 :     return bOk;
     826             : }
     827             : 
     828           0 : bool FormulaParserImpl::pushBinaryOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     829             : {
     830           0 :     bool bOk = maOperandSizeStack.size() >= 2;
     831           0 :     if( bOk )
     832             :     {
     833           0 :         size_t nOp2Size = popOperandSize();
     834           0 :         size_t nOp1Size = popOperandSize();
     835           0 :         size_t nSpacesSize = insertWhiteSpaceTokens( pSpaces, nOp2Size );
     836           0 :         insertRawToken( nOpCode, nOp2Size );
     837           0 :         pushOperandSize( nOp1Size + nSpacesSize + 1 + nOp2Size );
     838             :     }
     839           0 :     return bOk;
     840             : }
     841             : 
     842           0 : bool FormulaParserImpl::pushParenthesesOperatorToken( const WhiteSpaceVec* pOpeningSpaces, const WhiteSpaceVec* pClosingSpaces )
     843             : {
     844           0 :     bool bOk = maOperandSizeStack.size() >= 1;
     845           0 :     if( bOk )
     846             :     {
     847           0 :         size_t nOpSize = popOperandSize();
     848           0 :         size_t nSpacesSize = insertWhiteSpaceTokens( pOpeningSpaces, nOpSize );
     849           0 :         insertRawToken( OPCODE_OPEN, nOpSize );
     850           0 :         nSpacesSize += appendWhiteSpaceTokens( pClosingSpaces );
     851           0 :         appendRawToken( OPCODE_CLOSE );
     852           0 :         pushOperandSize( nOpSize + nSpacesSize + 2 );
     853             :     }
     854           0 :     return bOk;
     855             : }
     856             : 
     857           0 : bool FormulaParserImpl::pushFunctionOperatorToken( sal_Int32 nOpCode, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces, const WhiteSpaceVec* pClosingSpaces )
     858             : {
     859             :     /*  #i70925# if there are not enough tokens available on token stack, do
     860             :         not exit with error, but reduce parameter count. */
     861           0 :     nParamCount = ::std::min( maOperandSizeStack.size(), nParamCount );
     862             : 
     863             :     // convert all parameters on stack to a single operand separated with OPCODE_SEP
     864           0 :     bool bOk = true;
     865           0 :     for( size_t nParam = 1; bOk && (nParam < nParamCount); ++nParam )
     866           0 :         bOk = pushBinaryOperatorToken( OPCODE_SEP );
     867             : 
     868             :     // add function parentheses and function name
     869             :     return bOk &&
     870           0 :         ((nParamCount > 0) ? pushParenthesesOperatorToken( 0, pClosingSpaces ) : pushParenthesesOperandToken( 0, pClosingSpaces )) &&
     871           0 :         pushUnaryPreOperatorToken( nOpCode, pLeadingSpaces );
     872             : }
     873             : 
     874           0 : bool FormulaParserImpl::pushFunctionOperatorToken( const FunctionInfo& rFuncInfo, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces, const WhiteSpaceVec* pClosingSpaces )
     875             : {
     876           0 :     bool bOk = pushFunctionOperatorToken( rFuncInfo.mnApiOpCode, nParamCount, pLeadingSpaces, pClosingSpaces );
     877           0 :     if( bOk )
     878             :     {
     879             :        // create an external add-in call for the passed built-in function
     880           0 :         if( (rFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) && !rFuncInfo.maExtProgName.isEmpty() )
     881           0 :             getOperandToken( 1, 0, 0 ).Data <<= rFuncInfo.maExtProgName;
     882             :         // create a bad token with unsupported function name
     883           0 :         else if( (rFuncInfo.mnApiOpCode == OPCODE_BAD) && !rFuncInfo.maOoxFuncName.isEmpty() )
     884           0 :             getOperandToken( 1, 0, 0 ).Data <<= rFuncInfo.maOoxFuncName;
     885             :     }
     886           0 :     return bOk;
     887             : }
     888             : 
     889           0 : bool FormulaParserImpl::pushOperand( sal_Int32 nOpCode )
     890             : {
     891           0 :     return pushOperandToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
     892             : }
     893             : 
     894           0 : bool FormulaParserImpl::pushAnyOperand( const Any& rAny, sal_Int32 nOpCode )
     895             : {
     896           0 :     return pushAnyOperandToken( rAny, nOpCode, &maLeadingSpaces ) && resetSpaces();
     897             : }
     898             : 
     899             : template< typename Type >
     900           0 : bool FormulaParserImpl::pushValueOperand( const Type& rValue, sal_Int32 nOpCode )
     901             : {
     902           0 :     return pushValueOperandToken( rValue, nOpCode, &maLeadingSpaces ) && resetSpaces();
     903             : }
     904             : 
     905           0 : bool FormulaParserImpl::pushBoolOperand( bool bValue )
     906             : {
     907           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( bValue ? BIFF_FUNC_TRUE : BIFF_FUNC_FALSE ) )
     908           0 :         return pushFunctionOperator( pFuncInfo->mnApiOpCode, 0 );
     909           0 :     return pushValueOperand< double >( bValue ? 1.0 : 0.0 );
     910             : }
     911             : 
     912           0 : bool FormulaParserImpl::pushErrorOperand( double fEncodedError )
     913             : {
     914             :     // HACK: enclose all error codes into an 1x1 matrix
     915             :     // start token array with opening brace and leading spaces
     916           0 :     pushOperand( OPCODE_ARRAY_OPEN );
     917           0 :     size_t nOpSize = popOperandSize();
     918           0 :     size_t nOldArraySize = maTokenIndexes.size();
     919             :     // push a double containing the Calc error code
     920           0 :     appendRawToken( OPCODE_PUSH ) <<= fEncodedError;
     921             :     // close token array and set resulting operand size
     922           0 :     appendRawToken( OPCODE_ARRAY_CLOSE );
     923           0 :     pushOperandSize( nOpSize + maTokenIndexes.size() - nOldArraySize );
     924           0 :     return true;
     925             : }
     926             : 
     927           0 : bool FormulaParserImpl::pushBiffBoolOperand( sal_uInt8 nValue )
     928             : {
     929           0 :     return pushBoolOperand( nValue != BIFF_TOK_BOOL_FALSE );
     930             : }
     931             : 
     932           0 : bool FormulaParserImpl::pushBiffErrorOperand( sal_uInt8 nErrorCode )
     933             : {
     934           0 :     return pushErrorOperand( BiffHelper::calcDoubleFromError( nErrorCode ) );
     935             : }
     936             : 
     937           0 : bool FormulaParserImpl::pushReferenceOperand( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     938             : {
     939           0 :     SingleReference aApiRef;
     940           0 :     convertReference2d( aApiRef, rRef, bDeleted, bRelativeAsOffset );
     941           0 :     return pushValueOperand( aApiRef );
     942             : }
     943             : 
     944           0 : bool FormulaParserImpl::pushReferenceOperand( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     945             : {
     946           0 :     ComplexReference aApiRef;
     947           0 :     convertReference2d( aApiRef, rRef.maRef1, rRef.maRef2, bDeleted, bRelativeAsOffset );
     948           0 :     return pushValueOperand( aApiRef );
     949             : }
     950             : 
     951             : template< typename Type >
     952           0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const Type& rApiRef )
     953             : {
     954           0 :     if( rSheetRange.isExternal() )
     955             :     {
     956           0 :         ExternalReference aApiExtRef;
     957           0 :         aApiExtRef.Index = rSheetRange.getDocLinkIndex();
     958           0 :         aApiExtRef.Reference <<= rApiRef;
     959           0 :         return pushValueOperand( aApiExtRef );
     960             :     }
     961           0 :     return pushValueOperand( rApiRef );
     962             : }
     963             : 
     964           0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     965             : {
     966           0 :     if( rSheetRange.is3dRange() )
     967             :     {
     968             :         // single-cell-range over several sheets, needs to create a ComplexReference
     969           0 :         ComplexReference aApiRef;
     970           0 :         convertReference3d( aApiRef, rSheetRange, rRef, rRef, bDeleted, bRelativeAsOffset );
     971           0 :         return pushReferenceOperand( rSheetRange, aApiRef );
     972             :     }
     973           0 :     SingleReference aApiRef;
     974           0 :     convertReference3d( aApiRef, rSheetRange.getFirstSheet(), rSheetRange.isSameSheet(), rRef, bDeleted, bRelativeAsOffset );
     975           0 :     return pushReferenceOperand( rSheetRange, aApiRef );
     976             : }
     977             : 
     978           0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     979             : {
     980           0 :     ComplexReference aApiRef;
     981           0 :     convertReference3d( aApiRef, rSheetRange, rRef.maRef1, rRef.maRef2, bDeleted, bRelativeAsOffset );
     982           0 :     return pushReferenceOperand( rSheetRange, aApiRef );
     983             : }
     984             : 
     985           0 : bool FormulaParserImpl::pushNlrOperand( const BinSingleRef2d& rRef )
     986             : {
     987           0 :     SingleReference aApiRef;
     988           0 :     convertReference2d( aApiRef, rRef, false, false );
     989           0 :     return pushValueOperand( aApiRef, OPCODE_NLR );
     990             : }
     991             : 
     992           0 : bool FormulaParserImpl::pushEmbeddedRefOperand( const DefinedNameBase& rName, bool bPushBadToken )
     993             : {
     994           0 :     Any aRefAny = rName.getReference( maBaseAddr );
     995           0 :     if( aRefAny.hasValue() )
     996           0 :         return pushAnyOperand( aRefAny, OPCODE_PUSH );
     997           0 :     if( bPushBadToken && !rName.getModelName().isEmpty() && (rName.getModelName()[ 0 ] >= ' ') )
     998           0 :         return pushValueOperand( rName.getModelName(), OPCODE_BAD );
     999           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    1000             : }
    1001             : 
    1002           0 : bool FormulaParserImpl::pushDefinedNameOperand( const DefinedNameRef& rxDefName )
    1003             : {
    1004           0 :     if( !rxDefName || rxDefName->getModelName().isEmpty() )
    1005           0 :         return pushBiffErrorOperand( BIFF_ERR_NAME );
    1006           0 :     if( rxDefName->isMacroFunction() )
    1007           0 :         return pushValueOperand( rxDefName->getModelName(), OPCODE_MACRO );
    1008           0 :     if( rxDefName->getTokenIndex() >= 0 )
    1009           0 :         return pushValueOperand( rxDefName->getTokenIndex(), OPCODE_NAME );
    1010           0 :     return pushEmbeddedRefOperand( *rxDefName, true );
    1011             : }
    1012             : 
    1013           0 : bool FormulaParserImpl::pushExternalFuncOperand( const FunctionInfo& rFuncInfo )
    1014             : {
    1015             :     return (rFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) ?
    1016           0 :         pushValueOperand( rFuncInfo.maExtProgName, OPCODE_EXTERNAL ) :
    1017           0 :         pushOperand( rFuncInfo.mnApiOpCode );
    1018             : }
    1019             : 
    1020           0 : bool FormulaParserImpl::pushDdeLinkOperand( const OUString& rDdeServer, const OUString& rDdeTopic, const OUString& rDdeItem )
    1021             : {
    1022             :     // create the function call DDE("server";"topic";"item")
    1023             :     return
    1024           0 :         pushValueOperandToken( rDdeServer ) &&
    1025           0 :         pushValueOperandToken( rDdeTopic ) &&
    1026           0 :         pushValueOperandToken( rDdeItem ) &&
    1027           0 :         pushFunctionOperator( OPCODE_DDE, 3 );
    1028             : }
    1029             : 
    1030           0 : bool FormulaParserImpl::pushExternalNameOperand( const ExternalNameRef& rxExtName, const ExternalLink& rExtLink )
    1031             : {
    1032           0 :     if( rxExtName.get() ) switch( rExtLink.getLinkType() )
    1033             :     {
    1034             :         case LINKTYPE_INTERNAL:
    1035             :         case LINKTYPE_EXTERNAL:
    1036           0 :             return pushEmbeddedRefOperand( *rxExtName, false );
    1037             : 
    1038             :         case LINKTYPE_ANALYSIS:
    1039             :             // TODO: need support for localized addin function names
    1040           0 :             if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( rxExtName->getUpcaseModelName() ) )
    1041           0 :                 return pushExternalFuncOperand( *pFuncInfo );
    1042           0 :         break;
    1043             : 
    1044             :         case LINKTYPE_LIBRARY:
    1045           0 :             if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( rxExtName->getUpcaseModelName() ) )
    1046           0 :                 if( (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) && (pFuncInfo->meFuncLibType == rExtLink.getFuncLibraryType()) )
    1047           0 :                     return pushExternalFuncOperand( *pFuncInfo );
    1048           0 :         break;
    1049             : 
    1050             :         case LINKTYPE_DDE:
    1051             :         {
    1052           0 :             OUString aDdeServer, aDdeTopic, aDdeItem;
    1053           0 :             if( rxExtName->getDdeLinkData( aDdeServer, aDdeTopic, aDdeItem ) )
    1054           0 :                 return pushDdeLinkOperand( aDdeServer, aDdeTopic, aDdeItem );
    1055             :         }
    1056           0 :         break;
    1057             : 
    1058             :         default:
    1059             :             OSL_ENSURE( rExtLink.getLinkType() != LINKTYPE_SELF, "FormulaParserImpl::pushExternalNameOperand - invalid call" );
    1060             :     }
    1061           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    1062             : }
    1063             : 
    1064           0 : bool FormulaParserImpl::pushSpecialTokenOperand( const BinAddress& rBaseAddr, bool bTable )
    1065             : {
    1066           0 :     CellAddress aBaseAddr( maBaseAddr.Sheet, rBaseAddr.mnCol, rBaseAddr.mnRow );
    1067           0 :     ApiSpecialTokenInfo aTokenInfo( aBaseAddr, bTable );
    1068           0 :     return mbSpecialTokens && (getFormulaSize() == 0) && pushValueOperand( aTokenInfo, OPCODE_BAD );
    1069             : }
    1070             : 
    1071           0 : bool FormulaParserImpl::pushUnaryPreOperator( sal_Int32 nOpCode )
    1072             : {
    1073           0 :     return pushUnaryPreOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
    1074             : }
    1075             : 
    1076           0 : bool FormulaParserImpl::pushUnaryPostOperator( sal_Int32 nOpCode )
    1077             : {
    1078           0 :     return pushUnaryPostOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
    1079             : }
    1080             : 
    1081           0 : bool FormulaParserImpl::pushBinaryOperator( sal_Int32 nOpCode )
    1082             : {
    1083           0 :     return pushBinaryOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
    1084             : }
    1085             : 
    1086           0 : bool FormulaParserImpl::pushParenthesesOperator()
    1087             : {
    1088           0 :     return pushParenthesesOperatorToken( &maOpeningSpaces, &maClosingSpaces ) && resetSpaces();
    1089             : }
    1090             : 
    1091           0 : bool FormulaParserImpl::pushFunctionOperator( sal_Int32 nOpCode, size_t nParamCount )
    1092             : {
    1093           0 :     return pushFunctionOperatorToken( nOpCode, nParamCount, &maLeadingSpaces, &maClosingSpaces ) && resetSpaces();
    1094             : }
    1095             : 
    1096           0 : bool FormulaParserImpl::pushFunctionOperator( const FunctionInfo& rFuncInfo, size_t nParamCount )
    1097             : {
    1098           0 :     return pushFunctionOperatorToken( rFuncInfo, nParamCount, &maLeadingSpaces, &maClosingSpaces ) && resetSpaces();
    1099             : }
    1100             : 
    1101             : // reference conversion -------------------------------------------------------
    1102             : 
    1103           0 : void FormulaParserImpl::initReference2d( SingleReference& orApiRef ) const
    1104             : {
    1105           0 :     if( mb2dRefsAs3dRefs )
    1106             :     {
    1107           0 :         initReference3d( orApiRef, maBaseAddr.Sheet, false );
    1108             :     }
    1109             :     else
    1110             :     {
    1111           0 :         orApiRef.Flags = SHEET_RELATIVE;
    1112             :         // #i10184# absolute sheet index needed for relative references in shared formulas
    1113           0 :         orApiRef.Sheet = maBaseAddr.Sheet;
    1114           0 :         orApiRef.RelativeSheet = 0;
    1115             :     }
    1116           0 : }
    1117             : 
    1118           0 : void FormulaParserImpl::initReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet ) const
    1119             : {
    1120           0 :     orApiRef.Flags = SHEET_3D;
    1121           0 :     if( nSheet < 0 )
    1122             :     {
    1123           0 :         orApiRef.Sheet = 0;
    1124           0 :         orApiRef.Flags |= SHEET_DELETED;
    1125             :     }
    1126           0 :     else if( bSameSheet )
    1127             :     {
    1128             :         OSL_ENSURE( nSheet == 0, "FormulaParserImpl::initReference3d - invalid sheet index" );
    1129           0 :         orApiRef.Flags |= SHEET_RELATIVE;
    1130           0 :         orApiRef.RelativeSheet = 0;
    1131             :     }
    1132             :     else
    1133             :     {
    1134           0 :         orApiRef.Sheet = nSheet;
    1135             :     }
    1136           0 : }
    1137             : 
    1138           0 : void FormulaParserImpl::convertReference( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
    1139             : {
    1140           0 :     if( bDeleted )
    1141             :     {
    1142           0 :         orApiRef.Column = 0;
    1143           0 :         orApiRef.Row = 0;
    1144             :         // no explicit information about whether row or column is deleted
    1145           0 :         orApiRef.Flags |= COLUMN_DELETED | ROW_DELETED;
    1146             :     }
    1147             :     else
    1148             :     {
    1149             :         // column/row indexes and flags
    1150           0 :         setFlag( orApiRef.Flags, COLUMN_RELATIVE, rRef.mbColRel );
    1151           0 :         setFlag( orApiRef.Flags, ROW_RELATIVE, rRef.mbRowRel );
    1152           0 :         (rRef.mbColRel ? orApiRef.RelativeColumn : orApiRef.Column) = rRef.mnCol;
    1153           0 :         (rRef.mbRowRel ? orApiRef.RelativeRow : orApiRef.Row) = rRef.mnRow;
    1154             :         // convert absolute indexes to relative offsets used in API
    1155           0 :         if( !bRelativeAsOffset )
    1156             :         {
    1157           0 :             if( rRef.mbColRel )
    1158           0 :                 orApiRef.RelativeColumn -= maBaseAddr.Column;
    1159           0 :             if( rRef.mbRowRel )
    1160           0 :                 orApiRef.RelativeRow -= maBaseAddr.Row;
    1161             :         }
    1162             :     }
    1163           0 : }
    1164             : 
    1165           0 : void FormulaParserImpl::convertReference( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
    1166             : {
    1167           0 :     convertReference( orApiRef.Reference1, rRef1, bDeleted, bRelativeAsOffset );
    1168           0 :     convertReference( orApiRef.Reference2, rRef2, bDeleted, bRelativeAsOffset );
    1169             :     /*  Handle references to complete rows or columns (e.g. $1:$2 or C:D),
    1170             :         need to expand or shrink to limits of own document. */
    1171           0 :     if( !bDeleted && !rRef1.mbColRel && !rRef2.mbColRel && (orApiRef.Reference1.Column == 0) && (orApiRef.Reference2.Column == mnMaxXlsCol) )
    1172           0 :         orApiRef.Reference2.Column = mnMaxApiCol;
    1173           0 :     if( !bDeleted && !rRef1.mbRowRel && !rRef2.mbRowRel && (orApiRef.Reference1.Row == 0) && (orApiRef.Reference2.Row == mnMaxXlsRow) )
    1174           0 :         orApiRef.Reference2.Row = mnMaxApiRow;
    1175           0 : }
    1176             : 
    1177           0 : void FormulaParserImpl::convertReference2d( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
    1178             : {
    1179           0 :     initReference2d( orApiRef );
    1180           0 :     convertReference( orApiRef, rRef, bDeleted, bRelativeAsOffset );
    1181           0 : }
    1182             : 
    1183           0 : void FormulaParserImpl::convertReference2d( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
    1184             : {
    1185           0 :     initReference2d( orApiRef.Reference1 );
    1186           0 :     initReference2d( orApiRef.Reference2 );
    1187           0 :     convertReference( orApiRef, rRef1, rRef2, bDeleted, bRelativeAsOffset );
    1188             :     // remove sheet name from second part of reference
    1189           0 :     setFlag( orApiRef.Reference2.Flags, SHEET_3D, false );
    1190           0 : }
    1191             : 
    1192           0 : void FormulaParserImpl::convertReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
    1193             : {
    1194           0 :     initReference3d( orApiRef, nSheet, bSameSheet );
    1195           0 :     convertReference( orApiRef, rRef, bDeleted, bRelativeAsOffset );
    1196           0 : }
    1197             : 
    1198           0 : void FormulaParserImpl::convertReference3d( ComplexReference& orApiRef, const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
    1199             : {
    1200           0 :     bool bSameSheet = rSheetRange.isSameSheet();
    1201           0 :     initReference3d( orApiRef.Reference1, rSheetRange.getFirstSheet(), bSameSheet );
    1202           0 :     initReference3d( orApiRef.Reference2, rSheetRange.getLastSheet(), bSameSheet );
    1203           0 :     convertReference( orApiRef, rRef1, rRef2, bDeleted, bRelativeAsOffset );
    1204             :     // remove sheet name from second part of reference
    1205           0 :     setFlag( orApiRef.Reference2.Flags, SHEET_3D, rSheetRange.is3dRange() );
    1206           0 : }
    1207             : 
    1208             : // finalize token sequence ----------------------------------------------------
    1209             : 
    1210           2 : const FunctionInfo* FormulaParserImpl::resolveBadFuncName( const OUString& rTokenData ) const
    1211             : {
    1212             :     /*  Try to parse calls to library functions. The format of such a function
    1213             :         call is "[n]!funcname", n>0 being the link identifier of the function
    1214             :         library spreadsheet file. */
    1215           2 :     sal_Int32 nBracketOpen = rTokenData.indexOf( '[' );
    1216           2 :     sal_Int32 nBracketClose = rTokenData.indexOf( ']' );
    1217           2 :     sal_Int32 nExclamation = rTokenData.indexOf( '!' );
    1218           2 :     if( (0 == nBracketOpen) && (nBracketOpen + 1 < nBracketClose) && (nBracketClose + 1 == nExclamation) && (nExclamation + 1 < rTokenData.getLength()) )
    1219             :     {
    1220           0 :         sal_Int32 nRefId = rTokenData.copy( nBracketOpen + 1, nBracketClose - nBracketOpen - 1 ).toInt32();
    1221           0 :         const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get();
    1222           0 :         if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_LIBRARY) )
    1223             :         {
    1224           0 :             OUString aFuncName = rTokenData.copy( nExclamation + 1 ).toAsciiUpperCase();
    1225           0 :             if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( aFuncName ) )
    1226           0 :                 if( (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) && (pFuncInfo->meFuncLibType == pExtLink->getFuncLibraryType()) )
    1227           0 :                     return pFuncInfo;
    1228             :         }
    1229             :     }
    1230           2 :     return 0;
    1231             : }
    1232             : 
    1233           0 : OUString FormulaParserImpl::resolveDefinedName( sal_Int32 nTokenIndex ) const
    1234             : {
    1235           0 :     if( const DefinedName* pDefName = getDefinedNames().getByTokenIndex( nTokenIndex ).get() )
    1236           0 :         return pDefName->getCalcName();
    1237           0 :     return OUString();
    1238             : }
    1239             : 
    1240             : // OOXML/BIFF12 parser implementation =========================================
    1241             : 
    1242          22 : class OoxFormulaParserImpl : public FormulaParserImpl
    1243             : {
    1244             : public:
    1245             :     explicit            OoxFormulaParserImpl( const FormulaParser& rParent );
    1246             : 
    1247             :     virtual ApiTokenSequence importOoxFormula(
    1248             :                             const CellAddress& rBaseAddr,
    1249             :                             const OUString& rFormulaString );
    1250             : 
    1251             :     virtual ApiTokenSequence importBiff12Formula(
    1252             :                             const CellAddress& rBaseAddr,
    1253             :                             FormulaType eType,
    1254             :                             SequenceInputStream& rStrm );
    1255             : 
    1256             : private:
    1257             :     // import token contents and create API formula token ---------------------
    1258             : 
    1259             :     bool                importAttrToken( SequenceInputStream& rStrm );
    1260             :     bool                importSpaceToken( SequenceInputStream& rStrm );
    1261             :     bool                importTableToken( SequenceInputStream& rStrm );
    1262             :     bool                importArrayToken( SequenceInputStream& rStrm );
    1263             :     bool                importRefToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1264             :     bool                importAreaToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1265             :     bool                importRef3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1266             :     bool                importArea3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1267             :     bool                importMemAreaToken( SequenceInputStream& rStrm, bool bAddData );
    1268             :     bool                importMemFuncToken( SequenceInputStream& rStrm );
    1269             :     bool                importNameToken( SequenceInputStream& rStrm );
    1270             :     bool                importNameXToken( SequenceInputStream& rStrm );
    1271             :     bool                importFuncToken( SequenceInputStream& rStrm );
    1272             :     bool                importFuncVarToken( SequenceInputStream& rStrm );
    1273             :     bool                importExpToken( SequenceInputStream& rStrm );
    1274             : 
    1275             :     LinkSheetRange      readSheetRange( SequenceInputStream& rStrm );
    1276             : 
    1277             :     void                swapStreamPosition( SequenceInputStream& rStrm );
    1278             :     void                skipMemAreaAddData( SequenceInputStream& rStrm );
    1279             : 
    1280             :     // convert BIN token and push API operand or operator ---------------------
    1281             : 
    1282             :     bool                pushBiff12Name( sal_Int32 nNameId );
    1283             :     bool                pushBiff12ExtName( sal_Int32 nRefId, sal_Int32 nNameId );
    1284             :     bool                pushBiff12Function( sal_uInt16 nFuncId );
    1285             :     bool                pushBiff12Function( sal_uInt16 nFuncId, sal_uInt8 nParamCount );
    1286             : 
    1287             : private:
    1288             :     ApiParserWrapper    maApiParser;        /// Wrapper for the API formula parser object.
    1289             :     sal_Int64           mnAddDataPos;       /// Current stream position for additional data (tExp, tArray, tMemArea).
    1290             :     bool                mbNeedExtRefs;      /// True = parser needs initialization of external reference info.
    1291             : };
    1292             : 
    1293             : // ----------------------------------------------------------------------------
    1294             : 
    1295          11 : OoxFormulaParserImpl::OoxFormulaParserImpl( const FormulaParser& rParent ) :
    1296             :     FormulaParserImpl( rParent ),
    1297          11 :     maApiParser( rParent.getBaseFilter().getModelFactory(), rParent ),
    1298             :     mnAddDataPos( 0 ),
    1299          22 :     mbNeedExtRefs( true )
    1300             : {
    1301          11 : }
    1302             : 
    1303          58 : ApiTokenSequence OoxFormulaParserImpl::importOoxFormula( const CellAddress& rBaseAddr, const OUString& rFormulaString )
    1304             : {
    1305          58 :     if( mbNeedExtRefs )
    1306             :     {
    1307           7 :         maApiParser.getParserProperties().setProperty( PROP_ExternalLinks, getExternalLinks().getLinkInfos() );
    1308           7 :         mbNeedExtRefs = false;
    1309             :     }
    1310          58 :     return finalizeTokenArray( maApiParser.parseFormula( rFormulaString, rBaseAddr ) );
    1311             : }
    1312             : 
    1313           0 : ApiTokenSequence OoxFormulaParserImpl::importBiff12Formula( const CellAddress& rBaseAddr, FormulaType eType, SequenceInputStream& rStrm )
    1314             : {
    1315           0 :     initializeImport( rBaseAddr, eType );
    1316             : 
    1317           0 :     sal_Int32 nFmlaSize = rStrm.readInt32();
    1318           0 :     sal_Int64 nFmlaPos = rStrm.tell();
    1319           0 :     sal_Int64 nFmlaEndPos = nFmlaPos + nFmlaSize;
    1320             : 
    1321           0 :     rStrm.seek( nFmlaEndPos );
    1322           0 :     sal_Int32 nAddDataSize = rStrm.readInt32();
    1323           0 :     mnAddDataPos = rStrm.tell();
    1324           0 :     sal_Int64 nAddDataEndPos = mnAddDataPos + nAddDataSize;
    1325           0 :     rStrm.seek( nFmlaPos );
    1326             : 
    1327           0 :     bool bOk = (nFmlaSize >= 0) && (nAddDataSize >= 0);
    1328           0 :     bool bRelativeAsOffset = mbRelativeAsOffset;
    1329             : 
    1330           0 :     while( bOk && !rStrm.isEof() && (rStrm.tell() < nFmlaEndPos) )
    1331             :     {
    1332             :         sal_uInt8 nTokenId;
    1333           0 :         rStrm >> nTokenId;
    1334           0 :         sal_uInt8 nTokenClass = nTokenId & BIFF_TOKCLASS_MASK;
    1335           0 :         sal_uInt8 nBaseId = nTokenId & BIFF_TOKID_MASK;
    1336             : 
    1337           0 :         if( nTokenClass == BIFF_TOKCLASS_NONE )
    1338             :         {
    1339             :             // base tokens
    1340           0 :             switch( nBaseId )
    1341             :             {
    1342           0 :                 case BIFF_TOKID_EXP:        bOk = importExpToken( rStrm );                                      break;
    1343           0 :                 case BIFF_TOKID_ADD:        bOk = pushBinaryOperator( OPCODE_ADD );                             break;
    1344           0 :                 case BIFF_TOKID_SUB:        bOk = pushBinaryOperator( OPCODE_SUB );                             break;
    1345           0 :                 case BIFF_TOKID_MUL:        bOk = pushBinaryOperator( OPCODE_MULT );                            break;
    1346           0 :                 case BIFF_TOKID_DIV:        bOk = pushBinaryOperator( OPCODE_DIV );                             break;
    1347           0 :                 case BIFF_TOKID_POWER:      bOk = pushBinaryOperator( OPCODE_POWER );                           break;
    1348           0 :                 case BIFF_TOKID_CONCAT:     bOk = pushBinaryOperator( OPCODE_CONCAT );                          break;
    1349           0 :                 case BIFF_TOKID_LT:         bOk = pushBinaryOperator( OPCODE_LESS );                            break;
    1350           0 :                 case BIFF_TOKID_LE:         bOk = pushBinaryOperator( OPCODE_LESS_EQUAL );                      break;
    1351           0 :                 case BIFF_TOKID_EQ:         bOk = pushBinaryOperator( OPCODE_EQUAL );                           break;
    1352           0 :                 case BIFF_TOKID_GE:         bOk = pushBinaryOperator( OPCODE_GREATER_EQUAL );                   break;
    1353           0 :                 case BIFF_TOKID_GT:         bOk = pushBinaryOperator( OPCODE_GREATER );                         break;
    1354           0 :                 case BIFF_TOKID_NE:         bOk = pushBinaryOperator( OPCODE_NOT_EQUAL );                       break;
    1355           0 :                 case BIFF_TOKID_ISECT:      bOk = pushBinaryOperator( OPCODE_INTERSECT );                       break;
    1356           0 :                 case BIFF_TOKID_LIST:       bOk = pushBinaryOperator( OPCODE_LIST );                            break;
    1357           0 :                 case BIFF_TOKID_RANGE:      bOk = pushBinaryOperator( OPCODE_RANGE );                           break;
    1358           0 :                 case BIFF_TOKID_UPLUS:      bOk = pushUnaryPreOperator( OPCODE_PLUS_SIGN );                     break;
    1359           0 :                 case BIFF_TOKID_UMINUS:     bOk = pushUnaryPreOperator( OPCODE_MINUS_SIGN );                    break;
    1360           0 :                 case BIFF_TOKID_PERCENT:    bOk = pushUnaryPostOperator( OPCODE_PERCENT );                      break;
    1361           0 :                 case BIFF_TOKID_PAREN:      bOk = pushParenthesesOperator();                                    break;
    1362           0 :                 case BIFF_TOKID_MISSARG:    bOk = pushOperand( OPCODE_MISSING );                                break;
    1363           0 :                 case BIFF_TOKID_STR:        bOk = pushValueOperand( BiffHelper::readString( rStrm, false ) );   break;
    1364           0 :                 case BIFF_TOKID_NLR:        bOk = importTableToken( rStrm );                                    break;
    1365           0 :                 case BIFF_TOKID_ATTR:       bOk = importAttrToken( rStrm );                                     break;
    1366           0 :                 case BIFF_TOKID_ERR:        bOk = pushBiffErrorOperand( rStrm.readuInt8() );                    break;
    1367           0 :                 case BIFF_TOKID_BOOL:       bOk = pushBiffBoolOperand( rStrm.readuInt8() );                     break;
    1368           0 :                 case BIFF_TOKID_INT:        bOk = pushValueOperand< double >( rStrm.readuInt16() );             break;
    1369           0 :                 case BIFF_TOKID_NUM:        bOk = pushValueOperand( rStrm.readDouble() );                       break;
    1370           0 :                 default:                    bOk = false;
    1371             :             }
    1372             :         }
    1373             :         else
    1374             :         {
    1375             :             // classified tokens
    1376           0 :             switch( nBaseId )
    1377             :             {
    1378           0 :                 case BIFF_TOKID_ARRAY:      bOk = importArrayToken( rStrm );                            break;
    1379           0 :                 case BIFF_TOKID_FUNC:       bOk = importFuncToken( rStrm );                             break;
    1380           0 :                 case BIFF_TOKID_FUNCVAR:    bOk = importFuncVarToken( rStrm );                          break;
    1381           0 :                 case BIFF_TOKID_NAME:       bOk = importNameToken( rStrm );                             break;
    1382           0 :                 case BIFF_TOKID_REF:        bOk = importRefToken( rStrm, false, false );                break;
    1383           0 :                 case BIFF_TOKID_AREA:       bOk = importAreaToken( rStrm, false, false );               break;
    1384           0 :                 case BIFF_TOKID_MEMAREA:    bOk = importMemAreaToken( rStrm, true );                    break;
    1385           0 :                 case BIFF_TOKID_MEMERR:     bOk = importMemAreaToken( rStrm, false );                   break;
    1386           0 :                 case BIFF_TOKID_MEMNOMEM:   bOk = importMemAreaToken( rStrm, false );                   break;
    1387           0 :                 case BIFF_TOKID_MEMFUNC:    bOk = importMemFuncToken( rStrm );                          break;
    1388           0 :                 case BIFF_TOKID_REFERR:     bOk = importRefToken( rStrm, true, false );                 break;
    1389           0 :                 case BIFF_TOKID_AREAERR:    bOk = importAreaToken( rStrm, true, false );                break;
    1390           0 :                 case BIFF_TOKID_REFN:       bOk = importRefToken( rStrm, false, true );                 break;
    1391           0 :                 case BIFF_TOKID_AREAN:      bOk = importAreaToken( rStrm, false, true );                break;
    1392           0 :                 case BIFF_TOKID_MEMAREAN:   bOk = importMemFuncToken( rStrm );                          break;
    1393           0 :                 case BIFF_TOKID_MEMNOMEMN:  bOk = importMemFuncToken( rStrm );                          break;
    1394           0 :                 case BIFF_TOKID_NAMEX:      bOk = importNameXToken( rStrm );                            break;
    1395           0 :                 case BIFF_TOKID_REF3D:      bOk = importRef3dToken( rStrm, false, bRelativeAsOffset );  break;
    1396           0 :                 case BIFF_TOKID_AREA3D:     bOk = importArea3dToken( rStrm, false, bRelativeAsOffset ); break;
    1397           0 :                 case BIFF_TOKID_REFERR3D:   bOk = importRef3dToken( rStrm, true, bRelativeAsOffset );   break;
    1398           0 :                 case BIFF_TOKID_AREAERR3D:  bOk = importArea3dToken( rStrm, true, bRelativeAsOffset );  break;
    1399           0 :                 default:                    bOk = false;
    1400             :             }
    1401             :         }
    1402             :     }
    1403             : 
    1404             :     // build and finalize the token sequence
    1405           0 :     ApiTokenSequence aFinalTokens;
    1406           0 :     if( bOk && (rStrm.tell() == nFmlaEndPos) && (mnAddDataPos == nAddDataEndPos) )
    1407           0 :         aFinalTokens = finalizeImport();
    1408             : 
    1409             :     // seek behind token array
    1410           0 :     if( (nFmlaSize >= 0) && (nAddDataSize >= 0) )
    1411           0 :         rStrm.seek( nAddDataEndPos );
    1412             : 
    1413             :     // return the final token sequence
    1414           0 :     return aFinalTokens;
    1415             : }
    1416             : 
    1417             : // import token contents and create API formula token -------------------------
    1418             : 
    1419           0 : bool OoxFormulaParserImpl::importAttrToken( SequenceInputStream& rStrm )
    1420             : {
    1421           0 :     bool bOk = true;
    1422             :     sal_uInt8 nType;
    1423           0 :     rStrm >> nType;
    1424             :     // equal flags in all BIFFs
    1425           0 :     switch( nType )
    1426             :     {
    1427             :         case 0:     // sometimes, tAttrSkip tokens miss the type flag
    1428             :         case BIFF_TOK_ATTR_VOLATILE:
    1429             :         case BIFF_TOK_ATTR_IF:
    1430             :         case BIFF_TOK_ATTR_SKIP:
    1431             :         case BIFF_TOK_ATTR_ASSIGN:
    1432             :         case BIFF_TOK_ATTR_IFERROR:
    1433           0 :             rStrm.skip( 2 );
    1434           0 :         break;
    1435             :         case BIFF_TOK_ATTR_CHOOSE:
    1436           0 :             rStrm.skip( 2 * rStrm.readuInt16() + 2 );
    1437           0 :         break;
    1438             :         case BIFF_TOK_ATTR_SUM:
    1439           0 :             rStrm.skip( 2 );
    1440           0 :             bOk = pushBiff12Function( BIFF_FUNC_SUM, 1 );
    1441           0 :         break;
    1442             :         case BIFF_TOK_ATTR_SPACE:
    1443             :         case BIFF_TOK_ATTR_SPACE_VOLATILE:
    1444           0 :             bOk = importSpaceToken( rStrm );
    1445           0 :         break;
    1446             :         default:
    1447           0 :             bOk = false;
    1448             :     }
    1449           0 :     return bOk;
    1450             : }
    1451             : 
    1452           0 : bool OoxFormulaParserImpl::importSpaceToken( SequenceInputStream& rStrm )
    1453             : {
    1454             :     // equal constants in BIFF and OOX
    1455             :     sal_uInt8 nType, nCount;
    1456           0 :     rStrm >> nType >> nCount;
    1457           0 :     switch( nType )
    1458             :     {
    1459             :         case BIFF_TOK_ATTR_SPACE_SP:
    1460           0 :             appendLeadingSpaces( nCount, false );
    1461           0 :         break;
    1462             :         case BIFF_TOK_ATTR_SPACE_BR:
    1463           0 :             appendLeadingSpaces( nCount, true );
    1464           0 :         break;
    1465             :         case BIFF_TOK_ATTR_SPACE_SP_OPEN:
    1466           0 :             appendOpeningSpaces( nCount, false );
    1467           0 :         break;
    1468             :         case BIFF_TOK_ATTR_SPACE_BR_OPEN:
    1469           0 :             appendOpeningSpaces( nCount, true );
    1470           0 :         break;
    1471             :         case BIFF_TOK_ATTR_SPACE_SP_CLOSE:
    1472           0 :             appendClosingSpaces( nCount, false );
    1473           0 :         break;
    1474             :         case BIFF_TOK_ATTR_SPACE_BR_CLOSE:
    1475           0 :             appendClosingSpaces( nCount, true );
    1476           0 :         break;
    1477             :     }
    1478           0 :     return true;
    1479             : }
    1480             : 
    1481           0 : bool OoxFormulaParserImpl::importTableToken( SequenceInputStream& rStrm )
    1482             : {
    1483             :     sal_uInt16 nFlags, nTableId, nCol1, nCol2;
    1484           0 :     rStrm.skip( 3 );
    1485           0 :     rStrm >> nFlags >> nTableId;
    1486           0 :     rStrm.skip( 2 );
    1487           0 :     rStrm >> nCol1 >> nCol2;
    1488           0 :     TableRef xTable = getTables().getTable( nTableId );
    1489           0 :     sal_Int32 nTokenIndex = xTable.get() ? xTable->getTokenIndex() : -1;
    1490           0 :     if( nTokenIndex >= 0 )
    1491             :     {
    1492           0 :         sal_Int32 nWidth = xTable->getWidth();
    1493           0 :         sal_Int32 nHeight = xTable->getHeight();
    1494           0 :         sal_Int32 nStartCol = 0;
    1495           0 :         sal_Int32 nEndCol = nWidth - 1;
    1496           0 :         sal_Int32 nStartRow = 0;
    1497           0 :         sal_Int32 nEndRow = nHeight - 1;
    1498           0 :         bool bFixedStartRow = true;
    1499           0 :         bool bFixedHeight = false;
    1500             : 
    1501           0 :         bool bSingleCol = getFlag( nFlags, BIFF12_TOK_TABLE_COLUMN );
    1502           0 :         bool bColRange = getFlag( nFlags, BIFF12_TOK_TABLE_COLRANGE );
    1503           0 :         bool bValidRef = !bSingleCol || !bColRange;
    1504             :         OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - illegal combination of single column and column range" );
    1505           0 :         if( bValidRef )
    1506             :         {
    1507           0 :             if( bSingleCol )
    1508           0 :                 nStartCol = nEndCol = nCol1;
    1509           0 :             else if( bColRange )
    1510           0 :                 { nStartCol = nCol1; nEndCol = nCol2; }
    1511           0 :             bValidRef = (nStartCol <= nEndCol) && (nEndCol < nWidth);
    1512             :             OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - invalid column range" );
    1513             :         }
    1514             : 
    1515           0 :         if( bValidRef )
    1516             :         {
    1517           0 :             bool bAllRows    = getFlag( nFlags, BIFF12_TOK_TABLE_ALL );
    1518           0 :             bool bHeaderRows = getFlag( nFlags, BIFF12_TOK_TABLE_HEADERS );
    1519           0 :             bool bDataRows   = getFlag( nFlags, BIFF12_TOK_TABLE_DATA );
    1520           0 :             bool bTotalsRows = getFlag( nFlags, BIFF12_TOK_TABLE_TOTALS );
    1521           0 :             bool bThisRow    = getFlag( nFlags, BIFF12_TOK_TABLE_THISROW );
    1522             : 
    1523           0 :             sal_Int32 nStartDataRow = xTable->getHeaderRows();
    1524           0 :             sal_Int32 nEndDataRow = nEndRow - xTable->getTotalsRows();
    1525           0 :             bValidRef = (nStartRow <= nStartDataRow) && (nStartDataRow <= nEndDataRow) && (nEndDataRow <= nEndRow);
    1526             :             OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - invalid data row range" );
    1527           0 :             if( bValidRef )
    1528             :             {
    1529           0 :                 if( bAllRows )
    1530             :                 {
    1531           0 :                     bValidRef = !bHeaderRows && !bDataRows && !bTotalsRows && !bThisRow;
    1532             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#All] table token" );
    1533             :                 }
    1534           0 :                 else if( bHeaderRows )
    1535             :                 {
    1536           0 :                     bValidRef = !bTotalsRows && !bThisRow;
    1537             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Headers] table token" );
    1538           0 :                     nEndRow = bDataRows ? nEndDataRow : (nStartDataRow - 1);
    1539           0 :                     bFixedHeight = !bDataRows;
    1540             :                 }
    1541           0 :                 else if( bDataRows )
    1542             :                 {
    1543           0 :                     bValidRef = !bThisRow;
    1544             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Data] table token" );
    1545           0 :                     nStartRow = nStartDataRow;
    1546           0 :                     if( !bTotalsRows ) nEndRow = nEndDataRow;
    1547             :                 }
    1548           0 :                 else if( bTotalsRows )
    1549             :                 {
    1550           0 :                     bValidRef = !bThisRow;
    1551             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Totals] table token" );
    1552           0 :                     nStartRow = nEndDataRow + 1;
    1553           0 :                     bFixedStartRow = false;
    1554           0 :                     bFixedHeight = !bDataRows;
    1555             :                 }
    1556           0 :                 else if( bThisRow )
    1557             :                 {
    1558           0 :                     nStartRow = nEndRow = maBaseAddr.Row - xTable->getRange().StartRow;
    1559           0 :                     bFixedHeight = true;
    1560             :                 }
    1561             :                 else
    1562             :                 {
    1563             :                     // nothing is the same as [#Data]
    1564           0 :                     nStartRow = nStartDataRow;
    1565           0 :                     nEndRow = nEndDataRow;
    1566             :                 }
    1567             :             }
    1568           0 :             if( bValidRef )
    1569           0 :                 bValidRef = (0 <= nStartRow) && (nStartRow <= nEndRow) && (nEndRow < nHeight);
    1570             :         }
    1571           0 :         if( bValidRef )
    1572             :         {
    1573             :             // push single database area token, if table token refers to entire table
    1574           0 :             if( (nStartCol == 0) && (nEndCol + 1 == nWidth) && (nStartRow == 0) && (nEndRow + 1 == nHeight) )
    1575           0 :                 return pushValueOperand( nTokenIndex, OPCODE_DBAREA );
    1576             :             // create an OFFSET function call to refer to a subrange of the table
    1577           0 :             const FunctionInfo* pRowsInfo = getFuncInfoFromBiff12FuncId( BIFF_FUNC_ROWS );
    1578           0 :             const FunctionInfo* pColumnsInfo = getFuncInfoFromBiff12FuncId( BIFF_FUNC_COLUMNS );
    1579             :             return
    1580             :                 pRowsInfo && pColumnsInfo &&
    1581           0 :                 pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1582             :                 (bFixedStartRow ?
    1583           0 :                     pushValueOperandToken< double >( nStartRow ) :
    1584           0 :                     (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1585           0 :                      pushFunctionOperatorToken( *pRowsInfo, 1 ) &&
    1586           0 :                      pushValueOperandToken< double >( nHeight - nStartRow ) &&
    1587           0 :                      pushBinaryOperatorToken( OPCODE_SUB ))) &&
    1588           0 :                 pushValueOperandToken< double >( nStartCol ) &&
    1589             :                 (bFixedHeight ?
    1590           0 :                     pushValueOperandToken< double >( nEndRow - nStartRow + 1 ) :
    1591           0 :                     (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1592           0 :                      pushFunctionOperatorToken( *pRowsInfo, 1 ) &&
    1593             :                      (((nStartRow == 0) && (nEndRow + 1 == nHeight)) ||
    1594           0 :                       (pushValueOperandToken< double >( nHeight - (nEndRow - nStartRow + 1) ) &&
    1595           0 :                        pushBinaryOperatorToken( OPCODE_SUB ))))) &&
    1596             :                 (((nStartCol == 0) && (nEndCol + 1 == nWidth)) ?
    1597           0 :                     (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1598           0 :                      pushFunctionOperatorToken( *pColumnsInfo, 1 )) :
    1599           0 :                     pushValueOperandToken< double >( nEndCol - nStartCol + 1 )) &&
    1600           0 :                 pushBiff12Function( BIFF_FUNC_OFFSET, 5 );
    1601             :         }
    1602             :     }
    1603           0 :     return pushBiffErrorOperand( BIFF_ERR_REF );
    1604             : }
    1605             : 
    1606           0 : bool OoxFormulaParserImpl::importArrayToken( SequenceInputStream& rStrm )
    1607             : {
    1608           0 :     rStrm.skip( 14 );
    1609             : 
    1610             :     // start token array with opening brace and leading spaces
    1611           0 :     pushOperand( OPCODE_ARRAY_OPEN );
    1612           0 :     size_t nOpSize = popOperandSize();
    1613           0 :     size_t nOldArraySize = getFormulaSize();
    1614             : 
    1615             :     // read array size
    1616           0 :     swapStreamPosition( rStrm );
    1617           0 :     sal_Int32 nRows = rStrm.readInt32();
    1618           0 :     sal_Int32 nCols = rStrm.readInt32();
    1619             :     OSL_ENSURE( (nCols > 0) && (nRows > 0), "OoxFormulaParserImpl::importArrayToken - empty array" );
    1620             : 
    1621             :     // read array values and build token array
    1622           0 :     for( sal_Int32 nRow = 0; !rStrm.isEof() && (nRow < nRows); ++nRow )
    1623             :     {
    1624           0 :         if( nRow > 0 )
    1625           0 :             appendRawToken( OPCODE_ARRAY_ROWSEP );
    1626           0 :         for( sal_Int32 nCol = 0; !rStrm.isEof() && (nCol < nCols); ++nCol )
    1627             :         {
    1628           0 :             if( nCol > 0 )
    1629           0 :                 appendRawToken( OPCODE_ARRAY_COLSEP );
    1630           0 :             switch( rStrm.readuInt8() )
    1631             :             {
    1632             :                 case BIFF_TOK_ARRAY_DOUBLE:
    1633           0 :                     appendRawToken( OPCODE_PUSH ) <<= rStrm.readDouble();
    1634           0 :                 break;
    1635             :                 case BIFF_TOK_ARRAY_STRING:
    1636           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::readString( rStrm, false );
    1637           0 :                 break;
    1638             :                 case BIFF_TOK_ARRAY_BOOL:
    1639           0 :                     appendRawToken( OPCODE_PUSH ) <<= (static_cast< double >( (rStrm.readuInt8() == BIFF_TOK_BOOL_FALSE) ? 0.0 : 1.0 ));
    1640           0 :                 break;
    1641             :                 case BIFF_TOK_ARRAY_ERROR:
    1642           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( rStrm.readuInt8() );
    1643           0 :                     rStrm.skip( 3 );
    1644           0 :                 break;
    1645             :                 default:
    1646             :                     OSL_FAIL( "OoxFormulaParserImpl::importArrayToken - unknown data type" );
    1647           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( BIFF_ERR_NA );
    1648             :             }
    1649             :         }
    1650             :     }
    1651           0 :     swapStreamPosition( rStrm );
    1652             : 
    1653             :     // close token array and set resulting operand size
    1654           0 :     appendRawToken( OPCODE_ARRAY_CLOSE );
    1655           0 :     pushOperandSize( nOpSize + getFormulaSize() - nOldArraySize );
    1656           0 :     return true;
    1657             : }
    1658             : 
    1659           0 : bool OoxFormulaParserImpl::importRefToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1660             : {
    1661           0 :     BinSingleRef2d aRef;
    1662           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1663           0 :     return pushReferenceOperand( aRef, bDeleted, bRelativeAsOffset );
    1664             : }
    1665             : 
    1666           0 : bool OoxFormulaParserImpl::importAreaToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1667             : {
    1668           0 :     BinComplexRef2d aRef;
    1669           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1670           0 :     return pushReferenceOperand( aRef, bDeleted, bRelativeAsOffset );
    1671             : }
    1672             : 
    1673           0 : bool OoxFormulaParserImpl::importRef3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1674             : {
    1675           0 :     LinkSheetRange aSheetRange = readSheetRange( rStrm );
    1676           0 :     BinSingleRef2d aRef;
    1677           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1678           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    1679             : }
    1680             : 
    1681           0 : bool OoxFormulaParserImpl::importArea3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1682             : {
    1683           0 :     LinkSheetRange aSheetRange = readSheetRange( rStrm );
    1684           0 :     BinComplexRef2d aRef;
    1685           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1686           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    1687             : }
    1688             : 
    1689           0 : bool OoxFormulaParserImpl::importMemAreaToken( SequenceInputStream& rStrm, bool bAddData )
    1690             : {
    1691           0 :     rStrm.skip( 6 );
    1692           0 :     if( bAddData )
    1693           0 :         skipMemAreaAddData( rStrm );
    1694           0 :     return true;
    1695             : }
    1696             : 
    1697           0 : bool OoxFormulaParserImpl::importMemFuncToken( SequenceInputStream& rStrm )
    1698             : {
    1699           0 :     rStrm.skip( 2 );
    1700           0 :     return true;
    1701             : }
    1702             : 
    1703           0 : bool OoxFormulaParserImpl::importNameToken( SequenceInputStream& rStrm )
    1704             : {
    1705           0 :     return pushBiff12Name( rStrm.readInt32() );
    1706             : }
    1707             : 
    1708           0 : bool OoxFormulaParserImpl::importNameXToken( SequenceInputStream& rStrm )
    1709             : {
    1710           0 :     sal_Int32 nRefId = rStrm.readInt16();
    1711           0 :     sal_Int32 nNameId = rStrm.readInt32();
    1712           0 :     return pushBiff12ExtName( nRefId, nNameId );
    1713             : }
    1714             : 
    1715           0 : bool OoxFormulaParserImpl::importFuncToken( SequenceInputStream& rStrm )
    1716             : {
    1717             :     sal_uInt16 nFuncId;
    1718           0 :     rStrm >> nFuncId;
    1719           0 :     return pushBiff12Function( nFuncId );
    1720             : }
    1721             : 
    1722           0 : bool OoxFormulaParserImpl::importFuncVarToken( SequenceInputStream& rStrm )
    1723             : {
    1724             :     sal_uInt8 nParamCount;
    1725             :     sal_uInt16 nFuncId;
    1726           0 :     rStrm >> nParamCount >> nFuncId;
    1727           0 :     return pushBiff12Function( nFuncId, nParamCount );
    1728             : }
    1729             : 
    1730           0 : bool OoxFormulaParserImpl::importExpToken( SequenceInputStream& rStrm )
    1731             : {
    1732           0 :     BinAddress aBaseAddr;
    1733           0 :     rStrm >> aBaseAddr.mnRow;
    1734           0 :     swapStreamPosition( rStrm );
    1735           0 :     rStrm >> aBaseAddr.mnCol;
    1736           0 :     swapStreamPosition( rStrm );
    1737           0 :     return pushSpecialTokenOperand( aBaseAddr, false );
    1738             : }
    1739             : 
    1740           0 : LinkSheetRange OoxFormulaParserImpl::readSheetRange( SequenceInputStream& rStrm )
    1741             : {
    1742           0 :     return getExternalLinks().getSheetRange( rStrm.readInt16() );
    1743             : }
    1744             : 
    1745           0 : void OoxFormulaParserImpl::swapStreamPosition( SequenceInputStream& rStrm )
    1746             : {
    1747           0 :     sal_Int64 nRecPos = rStrm.tell();
    1748           0 :     rStrm.seek( mnAddDataPos );
    1749           0 :     mnAddDataPos = nRecPos;
    1750           0 : }
    1751             : 
    1752           0 : void OoxFormulaParserImpl::skipMemAreaAddData( SequenceInputStream& rStrm )
    1753             : {
    1754           0 :     swapStreamPosition( rStrm );
    1755           0 :     rStrm.skip( 16 * rStrm.readInt32() );
    1756           0 :     swapStreamPosition( rStrm );
    1757           0 : }
    1758             : 
    1759             : // convert BIN token and push API operand or operator -------------------------
    1760             : 
    1761           0 : bool OoxFormulaParserImpl::pushBiff12Name( sal_Int32 nNameId )
    1762             : {
    1763             :     // one-based in BIFF12 formulas
    1764           0 :     return pushDefinedNameOperand( getDefinedNames().getByIndex( nNameId - 1 ) );
    1765             : }
    1766             : 
    1767           0 : bool OoxFormulaParserImpl::pushBiff12ExtName( sal_Int32 nRefId, sal_Int32 nNameId )
    1768             : {
    1769           0 :     if( const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get() )
    1770             :     {
    1771           0 :         if( pExtLink->getLinkType() == LINKTYPE_SELF )
    1772           0 :             return pushBiff12Name( nNameId );
    1773             :         // external name indexes are one-based in BIFF12
    1774           0 :         ExternalNameRef xExtName = pExtLink->getNameByIndex( nNameId - 1 );
    1775           0 :         return pushExternalNameOperand( xExtName, *pExtLink );
    1776             :     }
    1777           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    1778             : }
    1779             : 
    1780           0 : bool OoxFormulaParserImpl::pushBiff12Function( sal_uInt16 nFuncId )
    1781             : {
    1782           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( nFuncId ) )
    1783           0 :         if( pFuncInfo->mnMinParamCount == pFuncInfo->mnMaxParamCount )
    1784           0 :             return pushFunctionOperator( *pFuncInfo, pFuncInfo->mnMinParamCount );
    1785           0 :     return pushFunctionOperator( OPCODE_NONAME, 0 );
    1786             : }
    1787             : 
    1788           0 : bool OoxFormulaParserImpl::pushBiff12Function( sal_uInt16 nFuncId, sal_uInt8 nParamCount )
    1789             : {
    1790           0 :     if( getFlag( nFuncId, BIFF_TOK_FUNCVAR_CMD ) )
    1791           0 :         nParamCount &= BIFF_TOK_FUNCVAR_COUNTMASK;
    1792           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( nFuncId ) )
    1793           0 :         return pushFunctionOperator( *pFuncInfo, nParamCount );
    1794           0 :     return pushFunctionOperator( OPCODE_NONAME, nParamCount );
    1795             : }
    1796             : 
    1797             : // BIFF parser implementation =================================================
    1798             : 
    1799             : namespace {
    1800             : 
    1801             : /** A natural language reference struct with relative flag. */
    1802             : struct BiffNlr
    1803             : {
    1804             :     sal_Int32           mnCol;              /// Column index.
    1805             :     sal_Int32           mnRow;              /// Row index.
    1806             :     bool                mbRel;              /// True = relative column/row reference.
    1807             : 
    1808             :     explicit            BiffNlr();
    1809             : 
    1810             :     void                readBiff8Data( BiffInputStream& rStrm );
    1811             : };
    1812             : 
    1813           0 : BiffNlr::BiffNlr() :
    1814             :     mnCol( 0 ),
    1815             :     mnRow( 0 ),
    1816           0 :     mbRel( false )
    1817             : {
    1818           0 : }
    1819             : 
    1820           0 : void BiffNlr::readBiff8Data( BiffInputStream& rStrm )
    1821             : {
    1822             :     sal_uInt16 nRow, nCol;
    1823           0 :     rStrm >> nRow >> nCol;
    1824           0 :     mnCol = nCol & BIFF_TOK_NLR_MASK;
    1825           0 :     mnRow = nRow;
    1826           0 :     mbRel = getFlag( nCol, BIFF_TOK_NLR_REL );
    1827           0 : }
    1828             : 
    1829           0 : bool lclIsValidNlrStack( const BinAddress& rAddr1, const BinAddress& rAddr2, bool bRow )
    1830             : {
    1831             :     return bRow ?
    1832             :         ((rAddr1.mnRow == rAddr2.mnRow) && (rAddr1.mnCol + 1 == rAddr2.mnCol)) :
    1833           0 :         ((rAddr1.mnCol == rAddr2.mnCol) && (rAddr1.mnRow + 1 == rAddr2.mnRow));
    1834             : }
    1835             : 
    1836           0 : bool lclIsValidNlrRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow )
    1837             : {
    1838             :     return bRow ?
    1839             :         ((rNlr.mnRow == rRange.maFirst.mnRow) && (rNlr.mnCol + 1 == rRange.maFirst.mnCol) && (rRange.maFirst.mnRow == rRange.maLast.mnRow)) :
    1840           0 :         ((rNlr.mnCol == rRange.maFirst.mnCol) && (rNlr.mnRow + 1 == rRange.maFirst.mnRow) && (rRange.maFirst.mnCol == rRange.maLast.mnCol));
    1841             : }
    1842             : 
    1843             : } // namespace
    1844             : 
    1845             : // ----------------------------------------------------------------------------
    1846             : 
    1847           0 : class BiffFormulaParserImpl : public FormulaParserImpl
    1848             : {
    1849             : public:
    1850             :     explicit            BiffFormulaParserImpl( const FormulaParser& rParent );
    1851             : 
    1852             :     virtual ApiTokenSequence importBiffFormula(
    1853             :                             const CellAddress& rBaseAddr,
    1854             :                             FormulaType eType,
    1855             :                             BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize );
    1856             : 
    1857             : private:
    1858             :     // import token contents and create API formula token ---------------------
    1859             : 
    1860             :     bool                importTokenNotAvailable( BiffInputStream& rStrm );
    1861             :     bool                importRefTokenNotAvailable( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1862             :     bool                importStrToken2( BiffInputStream& rStrm );
    1863             :     bool                importStrToken8( BiffInputStream& rStrm );
    1864             :     bool                importAttrToken( BiffInputStream& rStrm );
    1865             :     bool                importSpaceToken3( BiffInputStream& rStrm );
    1866             :     bool                importSpaceToken4( BiffInputStream& rStrm );
    1867             :     bool                importSheetToken2( BiffInputStream& rStrm );
    1868             :     bool                importSheetToken3( BiffInputStream& rStrm );
    1869             :     bool                importEndSheetToken2( BiffInputStream& rStrm );
    1870             :     bool                importEndSheetToken3( BiffInputStream& rStrm );
    1871             :     bool                importNlrToken( BiffInputStream& rStrm );
    1872             :     bool                importArrayToken( BiffInputStream& rStrm );
    1873             :     bool                importRefToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1874             :     bool                importRefToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1875             :     bool                importAreaToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1876             :     bool                importAreaToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1877             :     bool                importRef3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1878             :     bool                importRef3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1879             :     bool                importArea3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1880             :     bool                importArea3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1881             :     bool                importMemAreaToken( BiffInputStream& rStrm, bool bAddData );
    1882             :     bool                importMemFuncToken( BiffInputStream& rStrm );
    1883             :     bool                importNameToken( BiffInputStream& rStrm );
    1884             :     bool                importNameXToken( BiffInputStream& rStrm );
    1885             :     bool                importFuncToken2( BiffInputStream& rStrm );
    1886             :     bool                importFuncToken4( BiffInputStream& rStrm );
    1887             :     bool                importFuncVarToken2( BiffInputStream& rStrm );
    1888             :     bool                importFuncVarToken4( BiffInputStream& rStrm );
    1889             :     bool                importFuncCEToken( BiffInputStream& rStrm );
    1890             :     bool                importExpToken( BiffInputStream& rStrm );
    1891             :     bool                importTblToken( BiffInputStream& rStrm );
    1892             : 
    1893             :     bool                importNlrAddrToken( BiffInputStream& rStrm, bool bRow );
    1894             :     bool                importNlrRangeToken( BiffInputStream& rStrm );
    1895             :     bool                importNlrSAddrToken( BiffInputStream& rStrm, bool bRow );
    1896             :     bool                importNlrSRangeToken( BiffInputStream& rStrm );
    1897             :     bool                importNlrErrToken( BiffInputStream& rStrm, sal_uInt16 nSkip );
    1898             : 
    1899             :     sal_Int32           readRefId( BiffInputStream& rStrm );
    1900             :     sal_uInt16          readNameId( BiffInputStream& rStrm );
    1901             :     LinkSheetRange      readSheetRange5( BiffInputStream& rStrm );
    1902             :     LinkSheetRange      readSheetRange8( BiffInputStream& rStrm );
    1903             : 
    1904             :     void                swapStreamPosition( BiffInputStream& rStrm );
    1905             :     void                skipMemAreaAddData( BiffInputStream& rStrm );
    1906             :     bool                readNlrSAddrAddData( BiffNlr& orNlr, BiffInputStream& rStrm, bool bRow );
    1907             :     bool                readNlrSRangeAddData( BiffNlr& orNlr, bool& orbIsRow, BiffInputStream& rStrm );
    1908             : 
    1909             :     // convert BIFF token and push API operand or operator --------------------
    1910             : 
    1911             :     bool                pushBiffReference( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
    1912             :     bool                pushBiffReference( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
    1913             :     bool                pushBiffNlrAddr( const BiffNlr& rNlr, bool bRow );
    1914             :     bool                pushBiffNlrRange( const BiffNlr& rNlr, const BinRange& rRange );
    1915             :     bool                pushBiffNlrSAddr( const BiffNlr& rNlr, bool bRow );
    1916             :     bool                pushBiffNlrSRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow );
    1917             :     bool                pushBiffName( sal_uInt16 nNameId );
    1918             :     bool                pushBiffExtName( sal_Int32 nRefId, sal_uInt16 nNameId );
    1919             :     bool                pushBiffFunction( sal_uInt16 nFuncId );
    1920             :     bool                pushBiffFunction( sal_uInt16 nFuncId, sal_uInt8 nParamCount );
    1921             : 
    1922             :     // ------------------------------------------------------------------------
    1923             : private:
    1924             :     typedef bool (BiffFormulaParserImpl::*ImportTokenFunc)( BiffInputStream& );
    1925             :     typedef bool (BiffFormulaParserImpl::*ImportRefTokenFunc)( BiffInputStream&, bool, bool );
    1926             : 
    1927             :     ImportTokenFunc     mpImportStrToken;           /// Pointer to tStr import function (string constant).
    1928             :     ImportTokenFunc     mpImportSpaceToken;         /// Pointer to tAttrSpace import function (spaces/line breaks).
    1929             :     ImportTokenFunc     mpImportSheetToken;         /// Pointer to tSheet import function (external reference).
    1930             :     ImportTokenFunc     mpImportEndSheetToken;      /// Pointer to tEndSheet import function (end of external reference).
    1931             :     ImportTokenFunc     mpImportNlrToken;           /// Pointer to tNlr import function (natural language reference).
    1932             :     ImportRefTokenFunc  mpImportRefToken;           /// Pointer to tRef import function (2d cell reference).
    1933             :     ImportRefTokenFunc  mpImportAreaToken;          /// Pointer to tArea import function (2d area reference).
    1934             :     ImportRefTokenFunc  mpImportRef3dToken;         /// Pointer to tRef3d import function (3d cell reference).
    1935             :     ImportRefTokenFunc  mpImportArea3dToken;        /// Pointer to tArea3d import function (3d area reference).
    1936             :     ImportTokenFunc     mpImportNameXToken;         /// Pointer to tNameX import function (external name).
    1937             :     ImportTokenFunc     mpImportFuncToken;          /// Pointer to tFunc import function (function with fixed parameter count).
    1938             :     ImportTokenFunc     mpImportFuncVarToken;       /// Pointer to tFuncVar import function (function with variable parameter count).
    1939             :     ImportTokenFunc     mpImportFuncCEToken;        /// Pointer to tFuncCE import function (command macro call).
    1940             :     sal_Int64           mnAddDataPos;               /// Current stream position for additional data (tArray, tMemArea, tNlr).
    1941             :     sal_Int32           mnCurrRefId;                /// Current ref-id from tSheet token (BIFF2-BIFF4 only).
    1942             :     sal_uInt16          mnAttrDataSize;             /// Size of one tAttr data element.
    1943             :     sal_uInt16          mnArraySize;                /// Size of tArray data.
    1944             :     sal_uInt16          mnNameSize;                 /// Size of tName data.
    1945             :     sal_uInt16          mnMemAreaSize;              /// Size of tMemArea data.
    1946             :     sal_uInt16          mnMemFuncSize;              /// Size of tMemFunc data.
    1947             :     sal_uInt16          mnRefIdSize;                /// Size of unused data following a reference identifier.
    1948             : };
    1949             : 
    1950             : // ----------------------------------------------------------------------------
    1951             : 
    1952           0 : BiffFormulaParserImpl::BiffFormulaParserImpl( const FormulaParser& rParent ) :
    1953             :     FormulaParserImpl( rParent ),
    1954             :     mnAddDataPos( 0 ),
    1955           0 :     mnCurrRefId( 0 )
    1956             : {
    1957           0 :     switch( getBiff() )
    1958             :     {
    1959             :         case BIFF2:
    1960           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    1961           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1962           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken2;
    1963           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken2;
    1964           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1965           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    1966           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    1967           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1968           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1969           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1970           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken2;
    1971           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken2;
    1972           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importFuncCEToken;
    1973           0 :             mnAttrDataSize = 1;
    1974           0 :             mnArraySize = 6;
    1975           0 :             mnNameSize = 5;
    1976           0 :             mnMemAreaSize = 4;
    1977           0 :             mnMemFuncSize = 1;
    1978           0 :             mnRefIdSize = 1;
    1979           0 :         break;
    1980             :         case BIFF3:
    1981           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    1982           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken3;
    1983           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken3;
    1984           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken3;
    1985           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1986           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    1987           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    1988           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1989           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1990           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1991           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken2;
    1992           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken2;
    1993           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importFuncCEToken;
    1994           0 :             mnAttrDataSize = 2;
    1995           0 :             mnArraySize = 7;
    1996           0 :             mnNameSize = 8;
    1997           0 :             mnMemAreaSize = 6;
    1998           0 :             mnMemFuncSize = 2;
    1999           0 :             mnRefIdSize = 2;
    2000           0 :         break;
    2001             :         case BIFF4:
    2002           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    2003           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
    2004           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken3;
    2005           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken3;
    2006           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2007           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    2008           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    2009           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    2010           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    2011           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2012           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
    2013           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
    2014           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2015           0 :             mnAttrDataSize = 2;
    2016           0 :             mnArraySize = 7;
    2017           0 :             mnNameSize = 8;
    2018           0 :             mnMemAreaSize = 6;
    2019           0 :             mnMemFuncSize = 2;
    2020           0 :             mnRefIdSize = 2;
    2021           0 :         break;
    2022             :         case BIFF5:
    2023           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    2024           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
    2025           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2026           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2027           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2028           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    2029           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    2030           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRef3dToken5;
    2031           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importArea3dToken5;
    2032           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importNameXToken;
    2033           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
    2034           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
    2035           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2036           0 :             mnAttrDataSize = 2;
    2037           0 :             mnArraySize = 7;
    2038           0 :             mnNameSize = 12;
    2039           0 :             mnMemAreaSize = 6;
    2040           0 :             mnMemFuncSize = 2;
    2041           0 :             mnRefIdSize = 8;
    2042           0 :         break;
    2043             :         case BIFF8:
    2044           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken8;
    2045           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
    2046           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2047           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2048           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importNlrToken;
    2049           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken8;
    2050           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken8;
    2051           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRef3dToken8;
    2052           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importArea3dToken8;
    2053           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importNameXToken;
    2054           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
    2055           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
    2056           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2057           0 :             mnAttrDataSize = 2;
    2058           0 :             mnArraySize = 7;
    2059           0 :             mnNameSize = 2;
    2060           0 :             mnMemAreaSize = 6;
    2061           0 :             mnMemFuncSize = 2;
    2062           0 :             mnRefIdSize = 0;
    2063           0 :         break;
    2064           0 :         case BIFF_UNKNOWN: break;
    2065             :     }
    2066           0 : }
    2067             : 
    2068           0 : ApiTokenSequence BiffFormulaParserImpl::importBiffFormula( const CellAddress& rBaseAddr,
    2069             :         FormulaType eType, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize )
    2070             : {
    2071           0 :     initializeImport( rBaseAddr, eType );
    2072           0 :     mnCurrRefId = 0;
    2073             : 
    2074           0 :     sal_uInt16 nFmlaSize = lclReadFmlaSize( rStrm, getBiff(), pnFmlaSize );
    2075           0 :     sal_Int64 nEndPos = mnAddDataPos = rStrm.tell() + nFmlaSize;
    2076             : 
    2077           0 :     bool bOk = true;
    2078           0 :     while( bOk && !rStrm.isEof() && (rStrm.tell() < nEndPos) )
    2079             :     {
    2080             :         sal_uInt8 nTokenId;
    2081           0 :         rStrm >> nTokenId;
    2082           0 :         sal_uInt8 nTokenClass = nTokenId & BIFF_TOKCLASS_MASK;
    2083           0 :         sal_uInt8 nBaseId = nTokenId & BIFF_TOKID_MASK;
    2084             : 
    2085           0 :         bOk = !getFlag( nTokenId, BIFF_TOKFLAG_INVALID );
    2086           0 :         if( bOk )
    2087             :         {
    2088           0 :             if( nTokenClass == BIFF_TOKCLASS_NONE )
    2089             :             {
    2090             :                 // base tokens
    2091           0 :                 switch( nBaseId )
    2092             :                 {
    2093           0 :                     case BIFF_TOKID_EXP:        bOk = importExpToken( rStrm );                          break;
    2094           0 :                     case BIFF_TOKID_TBL:        bOk = importTblToken( rStrm );                          break;
    2095           0 :                     case BIFF_TOKID_ADD:        bOk = pushBinaryOperator( OPCODE_ADD );                 break;
    2096           0 :                     case BIFF_TOKID_SUB:        bOk = pushBinaryOperator( OPCODE_SUB );                 break;
    2097           0 :                     case BIFF_TOKID_MUL:        bOk = pushBinaryOperator( OPCODE_MULT );                break;
    2098           0 :                     case BIFF_TOKID_DIV:        bOk = pushBinaryOperator( OPCODE_DIV );                 break;
    2099           0 :                     case BIFF_TOKID_POWER:      bOk = pushBinaryOperator( OPCODE_POWER );               break;
    2100           0 :                     case BIFF_TOKID_CONCAT:     bOk = pushBinaryOperator( OPCODE_CONCAT );              break;
    2101           0 :                     case BIFF_TOKID_LT:         bOk = pushBinaryOperator( OPCODE_LESS );                break;
    2102           0 :                     case BIFF_TOKID_LE:         bOk = pushBinaryOperator( OPCODE_LESS_EQUAL );          break;
    2103           0 :                     case BIFF_TOKID_EQ:         bOk = pushBinaryOperator( OPCODE_EQUAL );               break;
    2104           0 :                     case BIFF_TOKID_GE:         bOk = pushBinaryOperator( OPCODE_GREATER_EQUAL );       break;
    2105           0 :                     case BIFF_TOKID_GT:         bOk = pushBinaryOperator( OPCODE_GREATER );             break;
    2106           0 :                     case BIFF_TOKID_NE:         bOk = pushBinaryOperator( OPCODE_NOT_EQUAL );           break;
    2107           0 :                     case BIFF_TOKID_ISECT:      bOk = pushBinaryOperator( OPCODE_INTERSECT );           break;
    2108           0 :                     case BIFF_TOKID_LIST:       bOk = pushBinaryOperator( OPCODE_LIST );                break;
    2109           0 :                     case BIFF_TOKID_RANGE:      bOk = pushBinaryOperator( OPCODE_RANGE );               break;
    2110           0 :                     case BIFF_TOKID_UPLUS:      bOk = pushUnaryPreOperator( OPCODE_PLUS_SIGN );         break;
    2111           0 :                     case BIFF_TOKID_UMINUS:     bOk = pushUnaryPreOperator( OPCODE_MINUS_SIGN );        break;
    2112           0 :                     case BIFF_TOKID_PERCENT:    bOk = pushUnaryPostOperator( OPCODE_PERCENT );          break;
    2113           0 :                     case BIFF_TOKID_PAREN:      bOk = pushParenthesesOperator();                        break;
    2114           0 :                     case BIFF_TOKID_MISSARG:    bOk = pushOperand( OPCODE_MISSING );                    break;
    2115           0 :                     case BIFF_TOKID_STR:        bOk = (this->*mpImportStrToken)( rStrm );               break;
    2116           0 :                     case BIFF_TOKID_NLR:        bOk = (this->*mpImportNlrToken)( rStrm );               break;
    2117           0 :                     case BIFF_TOKID_ATTR:       bOk = importAttrToken( rStrm );                         break;
    2118           0 :                     case BIFF_TOKID_SHEET:      bOk = (this->*mpImportSheetToken)( rStrm );             break;
    2119           0 :                     case BIFF_TOKID_ENDSHEET:   bOk = (this->*mpImportEndSheetToken)( rStrm );          break;
    2120           0 :                     case BIFF_TOKID_ERR:        bOk = pushBiffErrorOperand( rStrm.readuInt8() );        break;
    2121           0 :                     case BIFF_TOKID_BOOL:       bOk = pushBiffBoolOperand( rStrm.readuInt8() );         break;
    2122           0 :                     case BIFF_TOKID_INT:        bOk = pushValueOperand< double >( rStrm.readuInt16() ); break;
    2123           0 :                     case BIFF_TOKID_NUM:        bOk = pushValueOperand( rStrm.readDouble() );           break;
    2124           0 :                     default:                    bOk = false;
    2125             :                 }
    2126             :             }
    2127             :             else
    2128             :             {
    2129             :                 // classified tokens
    2130           0 :                 switch( nBaseId )
    2131             :                 {
    2132           0 :                     case BIFF_TOKID_ARRAY:      bOk = importArrayToken( rStrm );                                        break;
    2133           0 :                     case BIFF_TOKID_FUNC:       bOk = (this->*mpImportFuncToken)( rStrm );                              break;
    2134           0 :                     case BIFF_TOKID_FUNCVAR:    bOk = (this->*mpImportFuncVarToken)( rStrm );                           break;
    2135           0 :                     case BIFF_TOKID_NAME:       bOk = importNameToken( rStrm );                                         break;
    2136           0 :                     case BIFF_TOKID_REF:        bOk = (this->*mpImportRefToken)( rStrm, false, false );                 break;
    2137           0 :                     case BIFF_TOKID_AREA:       bOk = (this->*mpImportAreaToken)( rStrm, false, false );                break;
    2138           0 :                     case BIFF_TOKID_MEMAREA:    bOk = importMemAreaToken( rStrm, true );                                break;
    2139           0 :                     case BIFF_TOKID_MEMERR:     bOk = importMemAreaToken( rStrm, false );                               break;
    2140           0 :                     case BIFF_TOKID_MEMNOMEM:   bOk = importMemAreaToken( rStrm, false );                               break;
    2141           0 :                     case BIFF_TOKID_MEMFUNC:    bOk = importMemFuncToken( rStrm );                                      break;
    2142           0 :                     case BIFF_TOKID_REFERR:     bOk = (this->*mpImportRefToken)( rStrm, true, false );                  break;
    2143           0 :                     case BIFF_TOKID_AREAERR:    bOk = (this->*mpImportAreaToken)( rStrm, true, false );                 break;
    2144           0 :                     case BIFF_TOKID_REFN:       bOk = (this->*mpImportRefToken)( rStrm, false, true );                  break;
    2145           0 :                     case BIFF_TOKID_AREAN:      bOk = (this->*mpImportAreaToken)( rStrm, false, true );                 break;
    2146           0 :                     case BIFF_TOKID_MEMAREAN:   bOk = importMemFuncToken( rStrm );                                      break;
    2147           0 :                     case BIFF_TOKID_MEMNOMEMN:  bOk = importMemFuncToken( rStrm );                                      break;
    2148           0 :                     case BIFF_TOKID_FUNCCE:     bOk = (this->*mpImportFuncCEToken)( rStrm );                            break;
    2149           0 :                     case BIFF_TOKID_NAMEX:      bOk = (this->*mpImportNameXToken)( rStrm );                             break;
    2150           0 :                     case BIFF_TOKID_REF3D:      bOk = (this->*mpImportRef3dToken)( rStrm, false, mbRelativeAsOffset );  break;
    2151           0 :                     case BIFF_TOKID_AREA3D:     bOk = (this->*mpImportArea3dToken)( rStrm, false, mbRelativeAsOffset ); break;
    2152           0 :                     case BIFF_TOKID_REFERR3D:   bOk = (this->*mpImportRef3dToken)( rStrm, true, mbRelativeAsOffset );   break;
    2153           0 :                     case BIFF_TOKID_AREAERR3D:  bOk = (this->*mpImportArea3dToken)( rStrm, true, mbRelativeAsOffset );  break;
    2154           0 :                     default:                    bOk = false;
    2155             :                 }
    2156             :             }
    2157             :         }
    2158             :     }
    2159             : 
    2160             :     // build and finalize the token sequence
    2161           0 :     ApiTokenSequence aFinalTokens;
    2162           0 :     if( bOk && (rStrm.tell() == nEndPos) )
    2163           0 :         aFinalTokens = finalizeImport();
    2164             : 
    2165             :     // seek behind additional token data of tArray, tMemArea, tNlr tokens
    2166           0 :     rStrm.seek( mnAddDataPos );
    2167             : 
    2168             :     // return the final token sequence
    2169           0 :     return aFinalTokens;
    2170             : }
    2171             : 
    2172             : // import token contents and create API formula token -------------------------
    2173             : 
    2174           0 : bool BiffFormulaParserImpl::importTokenNotAvailable( BiffInputStream& )
    2175             : {
    2176             :     // dummy function for pointer-to-member-function
    2177           0 :     return false;
    2178             : }
    2179             : 
    2180           0 : bool BiffFormulaParserImpl::importRefTokenNotAvailable( BiffInputStream&, bool, bool )
    2181             : {
    2182             :     // dummy function for pointer-to-member-function
    2183           0 :     return false;
    2184             : }
    2185             : 
    2186           0 : bool BiffFormulaParserImpl::importStrToken2( BiffInputStream& rStrm )
    2187             : {
    2188           0 :     return pushValueOperand( rStrm.readByteStringUC( false, getTextEncoding(), mbAllowNulChars ) );
    2189             : }
    2190             : 
    2191           0 : bool BiffFormulaParserImpl::importStrToken8( BiffInputStream& rStrm )
    2192             : {
    2193             :     // read flags field for empty strings also
    2194           0 :     return pushValueOperand( rStrm.readUniStringBody( rStrm.readuInt8(), mbAllowNulChars ) );
    2195             : }
    2196             : 
    2197           0 : bool BiffFormulaParserImpl::importAttrToken( BiffInputStream& rStrm )
    2198             : {
    2199           0 :     bool bOk = true;
    2200             :     sal_uInt8 nType;
    2201           0 :     rStrm >> nType;
    2202           0 :     switch( nType )
    2203             :     {
    2204             :         case 0:     // sometimes, tAttrSkip tokens miss the type flag
    2205             :         case BIFF_TOK_ATTR_VOLATILE:
    2206             :         case BIFF_TOK_ATTR_IF:
    2207             :         case BIFF_TOK_ATTR_SKIP:
    2208             :         case BIFF_TOK_ATTR_ASSIGN:
    2209           0 :             rStrm.skip( mnAttrDataSize );
    2210           0 :         break;
    2211             :         case BIFF_TOK_ATTR_CHOOSE:
    2212           0 :             rStrm.skip( mnAttrDataSize * (1 + ((getBiff() == BIFF2) ? rStrm.readuInt8() : rStrm.readuInt16())) );
    2213           0 :         break;
    2214             :         case BIFF_TOK_ATTR_SUM:
    2215           0 :             rStrm.skip( mnAttrDataSize );
    2216           0 :             bOk = pushBiffFunction( BIFF_FUNC_SUM, 1 );
    2217           0 :         break;
    2218             :         case BIFF_TOK_ATTR_SPACE:
    2219             :         case BIFF_TOK_ATTR_SPACE_VOLATILE:
    2220           0 :             bOk = (this->*mpImportSpaceToken)( rStrm );
    2221           0 :         break;
    2222             :         default:
    2223           0 :             bOk = false;
    2224             :     }
    2225           0 :     return bOk;
    2226             : }
    2227             : 
    2228           0 : bool BiffFormulaParserImpl::importSpaceToken3( BiffInputStream& rStrm )
    2229             : {
    2230           0 :     rStrm.skip( 2 );
    2231           0 :     return true;
    2232             : }
    2233             : 
    2234           0 : bool BiffFormulaParserImpl::importSpaceToken4( BiffInputStream& rStrm )
    2235             : {
    2236             :     sal_uInt8 nType, nCount;
    2237           0 :     rStrm >> nType >> nCount;
    2238           0 :     switch( nType )
    2239             :     {
    2240             :         case BIFF_TOK_ATTR_SPACE_SP:
    2241           0 :             appendLeadingSpaces( nCount, false );
    2242           0 :         break;
    2243             :         case BIFF_TOK_ATTR_SPACE_BR:
    2244           0 :             appendLeadingSpaces( nCount, true );
    2245           0 :         break;
    2246             :         case BIFF_TOK_ATTR_SPACE_SP_OPEN:
    2247           0 :             appendOpeningSpaces( nCount, false );
    2248           0 :         break;
    2249             :         case BIFF_TOK_ATTR_SPACE_BR_OPEN:
    2250           0 :             appendOpeningSpaces( nCount, true );
    2251           0 :         break;
    2252             :         case BIFF_TOK_ATTR_SPACE_SP_CLOSE:
    2253           0 :             appendClosingSpaces( nCount, false );
    2254           0 :         break;
    2255             :         case BIFF_TOK_ATTR_SPACE_BR_CLOSE:
    2256           0 :             appendClosingSpaces( nCount, true );
    2257           0 :         break;
    2258             :     }
    2259           0 :     return true;
    2260             : }
    2261             : 
    2262           0 : bool BiffFormulaParserImpl::importSheetToken2( BiffInputStream& rStrm )
    2263             : {
    2264           0 :     rStrm.skip( 4 );
    2265           0 :     mnCurrRefId = readRefId( rStrm );
    2266           0 :     return true;
    2267             : }
    2268             : 
    2269           0 : bool BiffFormulaParserImpl::importSheetToken3( BiffInputStream& rStrm )
    2270             : {
    2271           0 :     rStrm.skip( 6 );
    2272           0 :     mnCurrRefId = readRefId( rStrm );
    2273           0 :     return true;
    2274             : }
    2275             : 
    2276           0 : bool BiffFormulaParserImpl::importEndSheetToken2( BiffInputStream& rStrm )
    2277             : {
    2278           0 :     rStrm.skip( 3 );
    2279           0 :     mnCurrRefId = 0;
    2280           0 :     return true;
    2281             : }
    2282             : 
    2283           0 : bool BiffFormulaParserImpl::importEndSheetToken3( BiffInputStream& rStrm )
    2284             : {
    2285           0 :     rStrm.skip( 4 );
    2286           0 :     mnCurrRefId = 0;
    2287           0 :     return true;
    2288             : }
    2289             : 
    2290           0 : bool BiffFormulaParserImpl::importNlrToken( BiffInputStream& rStrm )
    2291             : {
    2292           0 :     bool bOk = true;
    2293             :     sal_uInt8 nNlrType;
    2294           0 :     rStrm >> nNlrType;
    2295           0 :     switch( nNlrType )
    2296             :     {
    2297           0 :         case BIFF_TOK_NLR_ERR:      bOk = importNlrErrToken( rStrm, 4 );        break;
    2298           0 :         case BIFF_TOK_NLR_ROWR:     bOk = importNlrAddrToken( rStrm, true );    break;
    2299           0 :         case BIFF_TOK_NLR_COLR:     bOk = importNlrAddrToken( rStrm, false );   break;
    2300           0 :         case BIFF_TOK_NLR_ROWV:     bOk = importNlrAddrToken( rStrm, true );    break;
    2301           0 :         case BIFF_TOK_NLR_COLV:     bOk = importNlrAddrToken( rStrm, false );   break;
    2302           0 :         case BIFF_TOK_NLR_RANGE:    bOk = importNlrRangeToken( rStrm );         break;
    2303           0 :         case BIFF_TOK_NLR_SRANGE:   bOk = importNlrSRangeToken( rStrm );        break;
    2304           0 :         case BIFF_TOK_NLR_SROWR:    bOk = importNlrSAddrToken( rStrm, true );   break;
    2305           0 :         case BIFF_TOK_NLR_SCOLR:    bOk = importNlrSAddrToken( rStrm, false );  break;
    2306           0 :         case BIFF_TOK_NLR_SROWV:    bOk = importNlrSAddrToken( rStrm, true );   break;
    2307           0 :         case BIFF_TOK_NLR_SCOLV:    bOk = importNlrSAddrToken( rStrm, false );  break;
    2308           0 :         case BIFF_TOK_NLR_RANGEERR: bOk = importNlrErrToken( rStrm, 13 );       break;
    2309           0 :         case BIFF_TOK_NLR_SXNAME:   bOk = importNlrErrToken( rStrm, 4 );        break;
    2310           0 :         default:                    bOk = false;
    2311             :     }
    2312           0 :     return bOk;
    2313             : }
    2314             : 
    2315           0 : bool BiffFormulaParserImpl::importArrayToken( BiffInputStream& rStrm )
    2316             : {
    2317           0 :     rStrm.skip( mnArraySize );
    2318             : 
    2319             :     // start token array with opening brace and leading spaces
    2320           0 :     pushOperand( OPCODE_ARRAY_OPEN );
    2321           0 :     size_t nOpSize = popOperandSize();
    2322           0 :     size_t nOldArraySize = getFormulaSize();
    2323           0 :     bool bBiff8 = getBiff() == BIFF8;
    2324             : 
    2325             :     // read array size
    2326           0 :     swapStreamPosition( rStrm );
    2327           0 :     sal_uInt16 nCols = rStrm.readuInt8();
    2328           0 :     sal_uInt16 nRows = rStrm.readuInt16();
    2329           0 :     if( bBiff8 ) { ++nCols; ++nRows; } else if( nCols == 0 ) nCols = 256;
    2330             :     OSL_ENSURE( (nCols > 0) && (nRows > 0), "BiffFormulaParserImpl::importArrayToken - empty array" );
    2331             : 
    2332             :     // read array values and build token array
    2333           0 :     for( sal_uInt16 nRow = 0; !rStrm.isEof() && (nRow < nRows); ++nRow )
    2334             :     {
    2335           0 :         if( nRow > 0 )
    2336           0 :             appendRawToken( OPCODE_ARRAY_ROWSEP );
    2337           0 :         for( sal_uInt16 nCol = 0; !rStrm.isEof() && (nCol < nCols); ++nCol )
    2338             :         {
    2339           0 :             if( nCol > 0 )
    2340           0 :                 appendRawToken( OPCODE_ARRAY_COLSEP );
    2341           0 :             switch( rStrm.readuInt8() )
    2342             :             {
    2343             :                 case BIFF_DATATYPE_EMPTY:
    2344           0 :                     appendRawToken( OPCODE_PUSH ) <<= OUString();
    2345           0 :                     rStrm.skip( 8 );
    2346           0 :                 break;
    2347             :                 case BIFF_DATATYPE_DOUBLE:
    2348           0 :                     appendRawToken( OPCODE_PUSH ) <<= rStrm.readDouble();
    2349           0 :                 break;
    2350             :                 case BIFF_DATATYPE_STRING:
    2351           0 :                     appendRawToken( OPCODE_PUSH ) <<= bBiff8 ?
    2352             :                         rStrm.readUniString( mbAllowNulChars ) :
    2353           0 :                         rStrm.readByteStringUC( false, getTextEncoding(), mbAllowNulChars );
    2354           0 :                 break;
    2355             :                 case BIFF_DATATYPE_BOOL:
    2356           0 :                     appendRawToken( OPCODE_PUSH ) <<= (static_cast< double >( (rStrm.readuInt8() == BIFF_TOK_BOOL_FALSE) ? 0.0 : 1.0 ));
    2357           0 :                     rStrm.skip( 7 );
    2358           0 :                 break;
    2359             :                 case BIFF_DATATYPE_ERROR:
    2360           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( rStrm.readuInt8() );
    2361           0 :                     rStrm.skip( 7 );
    2362           0 :                 break;
    2363             :                 default:
    2364             :                     OSL_FAIL( "BiffFormulaParserImpl::importArrayToken - unknown data type" );
    2365           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( BIFF_ERR_NA );
    2366             :             }
    2367             :         }
    2368             :     }
    2369           0 :     swapStreamPosition( rStrm );
    2370             : 
    2371             :     // close token array and set resulting operand size
    2372           0 :     appendRawToken( OPCODE_ARRAY_CLOSE );
    2373           0 :     pushOperandSize( nOpSize + getFormulaSize() - nOldArraySize );
    2374           0 :     return true;
    2375             : }
    2376             : 
    2377           0 : bool BiffFormulaParserImpl::importRefToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2378             : {
    2379           0 :     BinSingleRef2d aRef;
    2380           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2381           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2382             : }
    2383             : 
    2384           0 : bool BiffFormulaParserImpl::importRefToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2385             : {
    2386           0 :     BinSingleRef2d aRef;
    2387           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2388           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2389             : }
    2390             : 
    2391           0 : bool BiffFormulaParserImpl::importAreaToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2392             : {
    2393           0 :     BinComplexRef2d aRef;
    2394           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2395           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2396             : }
    2397             : 
    2398           0 : bool BiffFormulaParserImpl::importAreaToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2399             : {
    2400           0 :     BinComplexRef2d aRef;
    2401           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2402           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2403             : }
    2404             : 
    2405           0 : bool BiffFormulaParserImpl::importRef3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2406             : {
    2407           0 :     LinkSheetRange aSheetRange = readSheetRange5( rStrm );
    2408           0 :     BinSingleRef2d aRef;
    2409           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2410           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2411             : }
    2412             : 
    2413           0 : bool BiffFormulaParserImpl::importRef3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2414             : {
    2415           0 :     LinkSheetRange aSheetRange = readSheetRange8( rStrm );
    2416           0 :     BinSingleRef2d aRef;
    2417           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2418           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2419             : }
    2420             : 
    2421           0 : bool BiffFormulaParserImpl::importArea3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2422             : {
    2423           0 :     LinkSheetRange aSheetRange = readSheetRange5( rStrm );
    2424           0 :     BinComplexRef2d aRef;
    2425           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2426           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2427             : }
    2428             : 
    2429           0 : bool BiffFormulaParserImpl::importArea3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2430             : {
    2431           0 :     LinkSheetRange aSheetRange = readSheetRange8( rStrm );
    2432           0 :     BinComplexRef2d aRef;
    2433           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2434           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2435             : }
    2436             : 
    2437           0 : bool BiffFormulaParserImpl::importMemAreaToken( BiffInputStream& rStrm, bool bAddData )
    2438             : {
    2439           0 :     rStrm.skip( mnMemAreaSize );
    2440           0 :     if( bAddData )
    2441           0 :         skipMemAreaAddData( rStrm );
    2442           0 :     return true;
    2443             : }
    2444             : 
    2445           0 : bool BiffFormulaParserImpl::importMemFuncToken( BiffInputStream& rStrm )
    2446             : {
    2447           0 :     rStrm.skip( mnMemFuncSize );
    2448           0 :     return true;
    2449             : }
    2450             : 
    2451           0 : bool BiffFormulaParserImpl::importNameToken( BiffInputStream& rStrm )
    2452             : {
    2453           0 :     sal_uInt16 nNameId = readNameId( rStrm );
    2454           0 :     return (mnCurrRefId > 0) ? pushBiffExtName( mnCurrRefId, nNameId ) : pushBiffName( nNameId );
    2455             : }
    2456             : 
    2457           0 : bool BiffFormulaParserImpl::importNameXToken( BiffInputStream& rStrm )
    2458             : {
    2459           0 :     sal_Int32 nRefId = readRefId( rStrm );
    2460           0 :     sal_uInt16 nNameId = readNameId( rStrm );
    2461           0 :     return pushBiffExtName( nRefId, nNameId );
    2462             : }
    2463             : 
    2464           0 : bool BiffFormulaParserImpl::importFuncToken2( BiffInputStream& rStrm )
    2465             : {
    2466             :     sal_uInt8 nFuncId;
    2467           0 :     rStrm >> nFuncId;
    2468           0 :     return pushBiffFunction( nFuncId );
    2469             : }
    2470             : 
    2471           0 : bool BiffFormulaParserImpl::importFuncToken4( BiffInputStream& rStrm )
    2472             : {
    2473             :     sal_uInt16 nFuncId;
    2474           0 :     rStrm >> nFuncId;
    2475           0 :     return pushBiffFunction( nFuncId );
    2476             : }
    2477             : 
    2478           0 : bool BiffFormulaParserImpl::importFuncVarToken2( BiffInputStream& rStrm )
    2479             : {
    2480             :     sal_uInt8 nParamCount, nFuncId;
    2481           0 :     rStrm >> nParamCount >> nFuncId;
    2482           0 :     return pushBiffFunction( nFuncId, nParamCount );
    2483             : }
    2484             : 
    2485           0 : bool BiffFormulaParserImpl::importFuncVarToken4( BiffInputStream& rStrm )
    2486             : {
    2487             :     sal_uInt8 nParamCount;
    2488             :     sal_uInt16 nFuncId;
    2489           0 :     rStrm >> nParamCount >> nFuncId;
    2490           0 :     return pushBiffFunction( nFuncId, nParamCount & BIFF_TOK_FUNCVAR_COUNTMASK );
    2491             : }
    2492             : 
    2493           0 : bool BiffFormulaParserImpl::importFuncCEToken( BiffInputStream& rStrm )
    2494             : {
    2495             :     sal_uInt8 nParamCount, nFuncId;
    2496           0 :     rStrm >> nParamCount >> nFuncId;
    2497           0 :     sal_uInt16 nCmdId = nFuncId;
    2498           0 :     setFlag( nCmdId, BIFF_TOK_FUNCVAR_CMD );
    2499           0 :     return pushBiffFunction( nCmdId, nParamCount );
    2500             : }
    2501             : 
    2502           0 : bool BiffFormulaParserImpl::importExpToken( BiffInputStream& rStrm )
    2503             : {
    2504           0 :     BinAddress aBaseAddr;
    2505           0 :     aBaseAddr.read( rStrm );
    2506           0 :     return pushSpecialTokenOperand( aBaseAddr, false );
    2507             : }
    2508             : 
    2509           0 : bool BiffFormulaParserImpl::importTblToken( BiffInputStream& rStrm )
    2510             : {
    2511           0 :     BinAddress aBaseAddr;
    2512           0 :     aBaseAddr.read( rStrm );
    2513           0 :     return pushSpecialTokenOperand( aBaseAddr, true );
    2514             : }
    2515             : 
    2516           0 : bool BiffFormulaParserImpl::importNlrAddrToken( BiffInputStream& rStrm, bool bRow )
    2517             : {
    2518           0 :     BiffNlr aNlr;
    2519           0 :     aNlr.readBiff8Data( rStrm );
    2520           0 :     return pushBiffNlrAddr( aNlr, bRow );
    2521             : }
    2522             : 
    2523           0 : bool BiffFormulaParserImpl::importNlrRangeToken( BiffInputStream& rStrm )
    2524             : {
    2525           0 :     BiffNlr aNlr;
    2526           0 :     aNlr.readBiff8Data( rStrm );
    2527           0 :     rStrm.skip( 1 );
    2528           0 :     BinRange aRange;
    2529           0 :     rStrm >> aRange;
    2530           0 :     return pushBiffNlrRange( aNlr, aRange );
    2531             : }
    2532             : 
    2533           0 : bool BiffFormulaParserImpl::importNlrSAddrToken( BiffInputStream& rStrm, bool bRow )
    2534             : {
    2535           0 :     rStrm.skip( 4 );
    2536           0 :     BiffNlr aNlr;
    2537           0 :     return readNlrSAddrAddData( aNlr, rStrm, bRow ) ? pushBiffNlrSAddr( aNlr, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
    2538             : }
    2539             : 
    2540           0 : bool BiffFormulaParserImpl::importNlrSRangeToken( BiffInputStream& rStrm )
    2541             : {
    2542           0 :     rStrm.skip( 5 );
    2543           0 :     BinRange aRange;
    2544           0 :     rStrm >> aRange;
    2545           0 :     BiffNlr aNlr;
    2546             :     bool bRow;
    2547           0 :     return readNlrSRangeAddData( aNlr, bRow, rStrm ) ? pushBiffNlrSRange( aNlr, aRange, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
    2548             : }
    2549             : 
    2550           0 : bool BiffFormulaParserImpl::importNlrErrToken( BiffInputStream& rStrm, sal_uInt16 nIgnore )
    2551             : {
    2552           0 :     rStrm.skip( nIgnore );
    2553           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    2554             : }
    2555             : 
    2556           0 : sal_Int32 BiffFormulaParserImpl::readRefId( BiffInputStream& rStrm )
    2557             : {
    2558             :     sal_Int16 nRefId;
    2559           0 :     rStrm >> nRefId;
    2560           0 :     rStrm.skip( mnRefIdSize );
    2561           0 :     return nRefId;
    2562             : }
    2563             : 
    2564           0 : sal_uInt16 BiffFormulaParserImpl::readNameId( BiffInputStream& rStrm )
    2565             : {
    2566             :     sal_uInt16 nNameId;
    2567           0 :     rStrm >> nNameId;
    2568           0 :     rStrm.skip( mnNameSize );
    2569           0 :     return nNameId;
    2570             : }
    2571             : 
    2572           0 : LinkSheetRange BiffFormulaParserImpl::readSheetRange5( BiffInputStream& rStrm )
    2573             : {
    2574           0 :     sal_Int32 nRefId = readRefId( rStrm );
    2575             :     sal_Int16 nTab1, nTab2;
    2576           0 :     rStrm >> nTab1 >> nTab2;
    2577           0 :     return getExternalLinks().getSheetRange( nRefId, nTab1, nTab2 );
    2578             : }
    2579             : 
    2580           0 : LinkSheetRange BiffFormulaParserImpl::readSheetRange8( BiffInputStream& rStrm )
    2581             : {
    2582           0 :     return getExternalLinks().getSheetRange( readRefId( rStrm ) );
    2583             : }
    2584             : 
    2585           0 : void BiffFormulaParserImpl::swapStreamPosition( BiffInputStream& rStrm )
    2586             : {
    2587           0 :     sal_Int64 nRecPos = rStrm.tell();
    2588           0 :     rStrm.seek( mnAddDataPos );
    2589           0 :     mnAddDataPos = nRecPos;
    2590           0 : }
    2591             : 
    2592           0 : void BiffFormulaParserImpl::skipMemAreaAddData( BiffInputStream& rStrm )
    2593             : {
    2594           0 :     swapStreamPosition( rStrm );
    2595           0 :     sal_Int32 nCount = rStrm.readuInt16();
    2596           0 :     rStrm.skip( ((getBiff() == BIFF8) ? 8 : 6) * nCount );
    2597           0 :     swapStreamPosition( rStrm );
    2598           0 : }
    2599             : 
    2600           0 : bool BiffFormulaParserImpl::readNlrSAddrAddData( BiffNlr& orNlr, BiffInputStream& rStrm, bool bRow )
    2601             : {
    2602           0 :     bool bIsRow = false;
    2603           0 :     return readNlrSRangeAddData( orNlr, bIsRow, rStrm ) && (bIsRow == bRow);
    2604             : }
    2605             : 
    2606           0 : bool BiffFormulaParserImpl::readNlrSRangeAddData( BiffNlr& orNlr, bool& orbIsRow, BiffInputStream& rStrm )
    2607             : {
    2608           0 :     orbIsRow = false;
    2609           0 :     swapStreamPosition( rStrm );
    2610             :     // read number of cell addresses and relative flag
    2611             :     sal_uInt32 nCount;
    2612           0 :     rStrm >> nCount;
    2613           0 :     bool bRel = getFlag( nCount, BIFF_TOK_NLR_ADDREL );
    2614           0 :     nCount &= BIFF_TOK_NLR_ADDMASK;
    2615           0 :     sal_Int64 nEndPos = rStrm.tell() + 4 * nCount;
    2616             :     // read list of cell addresses
    2617           0 :     bool bValid = false;
    2618           0 :     if( nCount >= 2 )
    2619             :     {
    2620             :         // detect column/row orientation
    2621           0 :         BinAddress aAddr1, aAddr2;
    2622           0 :         rStrm >> aAddr1 >> aAddr2;
    2623           0 :         orbIsRow = aAddr1.mnRow == aAddr2.mnRow;
    2624           0 :         bValid = lclIsValidNlrStack( aAddr1, aAddr2, orbIsRow );
    2625             :         // read and verify additional cell positions
    2626           0 :         for( sal_uInt32 nIndex = 2; bValid && (nIndex < nCount); ++nIndex )
    2627             :         {
    2628           0 :             aAddr1 = aAddr2;
    2629           0 :             rStrm >> aAddr2;
    2630           0 :             bValid = !rStrm.isEof() && lclIsValidNlrStack( aAddr1, aAddr2, orbIsRow );
    2631             :         }
    2632             :         // check that last imported position (aAddr2) is not at the end of the sheet
    2633           0 :         bValid = bValid && (orbIsRow ? (aAddr2.mnCol < mnMaxApiCol) : (aAddr2.mnRow < mnMaxApiRow));
    2634             :         // fill the NLR struct with the last imported position
    2635           0 :         if( bValid )
    2636             :         {
    2637           0 :             orNlr.mnCol = aAddr2.mnCol;
    2638           0 :             orNlr.mnRow = aAddr2.mnRow;
    2639           0 :             orNlr.mbRel = bRel;
    2640             :         }
    2641             :     }
    2642             :     // seek to end of additional data for this token
    2643           0 :     rStrm.seek( nEndPos );
    2644           0 :     swapStreamPosition( rStrm );
    2645             : 
    2646           0 :     return bValid;
    2647             : }
    2648             : 
    2649             : // convert BIFF token and push API operand or operator ------------------------
    2650             : 
    2651           0 : bool BiffFormulaParserImpl::pushBiffReference( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
    2652             : {
    2653             :     return (mnCurrRefId > 0) ?
    2654           0 :         pushReferenceOperand( getExternalLinks().getSheetRange( mnCurrRefId, 0, 0 ), rRef, bDeleted, bRelativeAsOffset ) :
    2655           0 :         pushReferenceOperand( rRef, bDeleted, bRelativeAsOffset );
    2656             : }
    2657             : 
    2658           0 : bool BiffFormulaParserImpl::pushBiffReference( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
    2659             : {
    2660             :     return (mnCurrRefId > 0) ?
    2661           0 :         pushReferenceOperand( getExternalLinks().getSheetRange( mnCurrRefId, 0, 0 ), rRef, bDeleted, bRelativeAsOffset ) :
    2662           0 :         pushReferenceOperand( rRef, bDeleted, bRelativeAsOffset );
    2663             : }
    2664             : 
    2665           0 : bool BiffFormulaParserImpl::pushBiffNlrAddr( const BiffNlr& rNlr, bool bRow )
    2666             : {
    2667           0 :     BinSingleRef2d aRef;
    2668           0 :     aRef.mnCol = rNlr.mnCol;
    2669           0 :     aRef.mnRow = rNlr.mnRow;
    2670           0 :     aRef.mbColRel = !bRow;
    2671           0 :     aRef.mbRowRel = bRow;
    2672           0 :     return pushNlrOperand( aRef );
    2673             : }
    2674             : 
    2675           0 : bool BiffFormulaParserImpl::pushBiffNlrRange( const BiffNlr& rNlr, const BinRange& rRange )
    2676             : {
    2677           0 :     bool bRow = rNlr.mnRow == rRange.maFirst.mnRow;
    2678           0 :     return lclIsValidNlrRange( rNlr, rRange, bRow ) ?
    2679           0 :         pushBiffNlrAddr( rNlr, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
    2680             : }
    2681             : 
    2682           0 : bool BiffFormulaParserImpl::pushBiffNlrSAddr( const BiffNlr& rNlr, bool bRow )
    2683             : {
    2684           0 :     BinRange aRange;
    2685           0 :     aRange.maFirst.mnCol = rNlr.mnCol + (bRow ? 1 : 0);
    2686           0 :     aRange.maFirst.mnRow = rNlr.mnRow + (bRow ? 0 : 1);
    2687           0 :     aRange.maLast.mnCol = bRow ? mnMaxApiCol : rNlr.mnCol;
    2688           0 :     aRange.maLast.mnRow = bRow ? rNlr.mnRow : mnMaxApiRow;
    2689           0 :     return pushBiffNlrSRange( rNlr, aRange, bRow );
    2690             : }
    2691             : 
    2692           0 : bool BiffFormulaParserImpl::pushBiffNlrSRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow )
    2693             : {
    2694           0 :     if( lclIsValidNlrRange( rNlr, rRange, bRow ) )
    2695             :     {
    2696           0 :         BinComplexRef2d aRef;
    2697           0 :         aRef.maRef1.mnCol = rRange.maFirst.mnCol;
    2698           0 :         aRef.maRef1.mnRow = rRange.maFirst.mnRow;
    2699           0 :         aRef.maRef2.mnCol = rRange.maLast.mnCol;
    2700           0 :         aRef.maRef2.mnRow = rRange.maLast.mnRow;
    2701           0 :         aRef.maRef1.mbColRel = aRef.maRef2.mbColRel = !bRow && rNlr.mbRel;
    2702           0 :         aRef.maRef1.mbRowRel = aRef.maRef2.mbRowRel = bRow && rNlr.mbRel;
    2703           0 :         return pushReferenceOperand( aRef, false, false );
    2704             :     }
    2705           0 :     return pushBiffErrorOperand( BIFF_ERR_REF );
    2706             : }
    2707             : 
    2708           0 : bool BiffFormulaParserImpl::pushBiffName( sal_uInt16 nNameId )
    2709             : {
    2710             :     // one-based in BIFF formulas
    2711           0 :     return pushDefinedNameOperand( getDefinedNames().getByIndex( static_cast< sal_Int32 >( nNameId ) - 1 ) );
    2712             : }
    2713             : 
    2714           0 : bool BiffFormulaParserImpl::pushBiffExtName( sal_Int32 nRefId, sal_uInt16 nNameId )
    2715             : {
    2716           0 :     if( const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get() )
    2717             :     {
    2718           0 :         if( pExtLink->getLinkType() == LINKTYPE_SELF )
    2719           0 :             return pushBiffName( nNameId );
    2720             :         // external name indexes are one-based in BIFF
    2721           0 :         ExternalNameRef xExtName = pExtLink->getNameByIndex( static_cast< sal_Int32 >( nNameId ) - 1 );
    2722           0 :         return pushExternalNameOperand( xExtName, *pExtLink );
    2723             :     }
    2724           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    2725             : }
    2726             : 
    2727           0 : bool BiffFormulaParserImpl::pushBiffFunction( sal_uInt16 nFuncId )
    2728             : {
    2729           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( nFuncId ) )
    2730           0 :         if( pFuncInfo->mnMinParamCount == pFuncInfo->mnMaxParamCount )
    2731           0 :             return pushFunctionOperator( *pFuncInfo, pFuncInfo->mnMinParamCount );
    2732           0 :     return pushFunctionOperator( OPCODE_NONAME, 0 );
    2733             : }
    2734             : 
    2735           0 : bool BiffFormulaParserImpl::pushBiffFunction( sal_uInt16 nFuncId, sal_uInt8 nParamCount )
    2736             : {
    2737           0 :     if( getFlag( nFuncId, BIFF_TOK_FUNCVAR_CMD ) )
    2738           0 :         nParamCount &= BIFF_TOK_FUNCVAR_COUNTMASK;
    2739           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( nFuncId ) )
    2740           0 :         return pushFunctionOperator( *pFuncInfo, nParamCount );
    2741           0 :     return pushFunctionOperator( OPCODE_NONAME, nParamCount );
    2742             : }
    2743             : 
    2744             : // ============================================================================
    2745             : 
    2746             : namespace {
    2747             : 
    2748             : /** Extracts the reference identifier and the remaining data from a formula in
    2749             :     the format '[RefID]Remaining'. */
    2750           0 : bool lclExtractRefId( sal_Int32& rnRefId, OUString& rRemainder, const OUString& rFormulaString )
    2751             : {
    2752           0 :     if( (rFormulaString.getLength() >= 4) && (rFormulaString[ 0 ] == '[') )
    2753             :     {
    2754           0 :         sal_Int32 nBracketClose = rFormulaString.indexOf( ']', 1 );
    2755           0 :         if( nBracketClose >= 2 )
    2756             :         {
    2757           0 :             rnRefId = rFormulaString.copy( 1, nBracketClose - 1 ).toInt32();
    2758           0 :             rRemainder = rFormulaString.copy( nBracketClose + 1 );
    2759           0 :             return !rRemainder.isEmpty();
    2760             :         }
    2761             :     }
    2762           0 :     return false;
    2763             : }
    2764             : 
    2765             : }
    2766             : 
    2767             : // ----------------------------------------------------------------------------
    2768             : 
    2769          11 : FormulaParser::FormulaParser( const WorkbookHelper& rHelper ) :
    2770          11 :     FormulaProcessorBase( rHelper )
    2771             : {
    2772          11 :     switch( getFilterType() )
    2773             :     {
    2774          11 :         case FILTER_OOXML:  mxImpl.reset( new OoxFormulaParserImpl( *this ) );  break;
    2775           0 :         case FILTER_BIFF:   mxImpl.reset( new BiffFormulaParserImpl( *this ) ); break;
    2776           0 :         case FILTER_UNKNOWN: break;
    2777             :     }
    2778          11 : }
    2779             : 
    2780          22 : FormulaParser::~FormulaParser()
    2781             : {
    2782          22 : }
    2783             : 
    2784          58 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, const OUString& rFormulaString ) const
    2785             : {
    2786          58 :     return mxImpl->importOoxFormula( rBaseAddress, rFormulaString );
    2787             : }
    2788             : 
    2789           0 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, FormulaType eType, SequenceInputStream& rStrm ) const
    2790             : {
    2791           0 :     return mxImpl->importBiff12Formula( rBaseAddress, eType, rStrm );
    2792             : }
    2793             : 
    2794           0 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, FormulaType eType, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize ) const
    2795             : {
    2796           0 :     return mxImpl->importBiffFormula( rBaseAddress, eType, rStrm, pnFmlaSize );
    2797             : }
    2798             : 
    2799           0 : ApiTokenSequence FormulaParser::convertBoolToFormula( bool bValue ) const
    2800             : {
    2801           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( bValue ? BIFF_FUNC_TRUE : BIFF_FUNC_FALSE ) )
    2802             :     {
    2803           0 :         ApiTokenSequence aTokens( 3 );
    2804           0 :         aTokens[ 0 ].OpCode = pFuncInfo->mnApiOpCode;
    2805           0 :         aTokens[ 1 ].OpCode = OPCODE_OPEN;
    2806           0 :         aTokens[ 2 ].OpCode = OPCODE_CLOSE;
    2807           0 :         return aTokens;
    2808             :     }
    2809           0 :     return ApiTokenSequence();
    2810             : }
    2811             : 
    2812           0 : ApiTokenSequence FormulaParser::convertErrorToFormula( sal_uInt8 nErrorCode ) const
    2813             : {
    2814           0 :     ApiTokenSequence aTokens( 3 );
    2815             :     // HACK: enclose all error codes into an 1x1 matrix
    2816           0 :     aTokens[ 0 ].OpCode = OPCODE_ARRAY_OPEN;
    2817           0 :     aTokens[ 1 ].OpCode = OPCODE_PUSH;
    2818           0 :     aTokens[ 1 ].Data <<= BiffHelper::calcDoubleFromError( nErrorCode );
    2819           0 :     aTokens[ 2 ].OpCode = OPCODE_ARRAY_CLOSE;
    2820           0 :     return aTokens;
    2821             : }
    2822             : 
    2823           7 : ApiTokenSequence FormulaParser::convertNameToFormula( sal_Int32 nTokenIndex ) const
    2824             : {
    2825           7 :     if( nTokenIndex < 0 )
    2826           0 :         return convertErrorToFormula( BIFF_ERR_REF );
    2827             : 
    2828           7 :     ApiTokenSequence aTokens( 1 );
    2829           7 :     aTokens[ 0 ].OpCode = OPCODE_NAME;
    2830           7 :     NameToken aNameTokenData;
    2831           7 :     aNameTokenData.Global = sal_True;
    2832           7 :     aNameTokenData.Index = nTokenIndex;
    2833           7 :     aTokens[ 0 ].Data <<= aNameTokenData;
    2834           7 :     return aTokens;
    2835             : }
    2836             : 
    2837           0 : OUString FormulaParser::importOleTargetLink( const OUString& rFormulaString )
    2838             : {
    2839           0 :     sal_Int32 nRefId = -1;
    2840           0 :     OUString aRemainder;
    2841           0 :     if( lclExtractRefId( nRefId, aRemainder, rFormulaString ) && (aRemainder.getLength() >= 3) &&
    2842           0 :             (aRemainder[ 0 ] == '!') && (aRemainder[ 1 ] == '\'') && (aRemainder[ aRemainder.getLength() - 1 ] == '\'') )
    2843           0 :         return mxImpl->resolveOleTarget( nRefId, false );
    2844           0 :     return OUString();
    2845             : }
    2846             : 
    2847           0 : OUString FormulaParser::importOleTargetLink( SequenceInputStream& rStrm )
    2848             : {
    2849           0 :     OUString aTargetLink;
    2850           0 :     sal_Int32 nFmlaSize = rStrm.readInt32();
    2851           0 :     sal_Int64 nFmlaEndPos = rStrm.tell() + ::std::max< sal_Int32 >( nFmlaSize, 0 );
    2852           0 :     if( (nFmlaSize == 7) && (rStrm.getRemaining() >= 7) )
    2853             :     {
    2854             :         sal_uInt8 nToken;
    2855             :         sal_Int16 nRefId;
    2856             :         sal_Int32 nNameId;
    2857           0 :         rStrm >> nToken >> nRefId >> nNameId;
    2858           0 :         if( nToken == (BIFF_TOKCLASS_VAL|BIFF_TOKID_NAMEX) )
    2859           0 :             aTargetLink = mxImpl->resolveOleTarget( nRefId, true );
    2860             :     }
    2861           0 :     rStrm.seek( nFmlaEndPos );
    2862           0 :     return aTargetLink;
    2863             : }
    2864             : 
    2865           0 : OUString FormulaParser::importMacroName( const OUString& rFormulaString )
    2866             : {
    2867             :     /*  Valid macros are either sheet macros or VBA macros. OOXML and all BIFF
    2868             :         documents store defined names for sheet macros, but OOXML documents do
    2869             :         not store any defined name for VBA macros (while BIFF documents do).
    2870             :         Sheet macros may be defined locally to a sheet, or globally to the
    2871             :         document. As a result, all of the following macro specifiers are valid:
    2872             : 
    2873             :         1) Macros located in the own document:
    2874             :             [0]!MySheetMacro    (global sheet macro 'MySheetMacro')
    2875             :             Macro1!MyMacro      (sheet-local sheet macro 'MyMacro')
    2876             :             [0]!MyVBAProc       (VBA macro 'MyVBAProc')
    2877             :             [0]!Mod1.MyVBAProc  (VBA macro 'MyVBAProc' from code module 'Mod1')
    2878             : 
    2879             :         2) Macros from an external document:
    2880             :             [2]!MySheetMacro    (global external sheet macro 'MySheetMacro')
    2881             :             [2]Macro1!MyMacro   (sheet-local external sheet macro 'MyMacro')
    2882             :             [2]!MyVBAProc       (external VBA macro 'MyVBAProc')
    2883             :             [2]!Mod1.MyVBAProc  (external VBA macro from code module 'Mod1')
    2884             : 
    2885             :         This implementation is only interested in VBA macros from the own
    2886             :         document, ignoring the valid syntax 'Macro1!MyMacro' for sheet-local
    2887             :         sheet macros.
    2888             :      */
    2889           0 :     sal_Int32 nRefId = -1;
    2890           0 :     OUString aRemainder;
    2891           0 :     if( lclExtractRefId( nRefId, aRemainder, rFormulaString ) && (aRemainder.getLength() > 1) && (aRemainder[ 0 ] == '!') )
    2892             :     {
    2893             :         /*  In BIFF12 documents, the reference identifier is always the
    2894             :             one-based index of the external link as it is in OOXML documents
    2895             :             (it is not an index into the list of reference sheets as used in
    2896             :             cell formulas). Index 0 is an implicit placeholder for the own
    2897             :             document. In BIFF12 documents, the reference to the own document is
    2898             :             stored explicitly, mostly at the top of the list, so index 1 may
    2899             :             resolve to the own document too.
    2900             :             Passing 'false' to getExternalLink() specifies to ignore the
    2901             :             reference sheets list (if existing) and to access the list of
    2902             :             external links directly. */
    2903           0 :         const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId, false ).get();
    2904             :         OSL_ENSURE( pExtLink, "FormulaParser::importMacroName - missing link" );
    2905             :         // do not accept macros in external documents (not supported)
    2906           0 :         if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_SELF) )
    2907             :         {
    2908             :             // ignore sheet macros (defined name for VBA macros may not exist, see above)
    2909           0 :             OUString aMacroName = aRemainder.copy( 1 );
    2910           0 :             const DefinedName* pDefName = getDefinedNames().getByModelName( aMacroName ).get();
    2911           0 :             if( !pDefName || pDefName->isVBName() )
    2912           0 :                 return aMacroName;
    2913             :         }
    2914             :     }
    2915           0 :     return OUString();
    2916             : }
    2917             : 
    2918             : // ============================================================================
    2919             : 
    2920             : } // namespace xls
    2921           9 : } // namespace oox
    2922             : 
    2923             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10