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

Generated by: LCOV version 1.10