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

Generated by: LCOV version 1.10