LCOV - code coverage report
Current view: top level - include/formula - FormulaCompiler.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 48 51 94.1 %
Date: 2014-11-03 Functions: 23 23 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_FORMULA_FORMULACOMPILER_HXX
      21             : #define INCLUDED_FORMULA_FORMULACOMPILER_HXX
      22             : 
      23             : #include <formula/formuladllapi.h>
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <rtl/ustring.hxx>
      26             : #include <tools/debug.hxx>
      27             : 
      28             : #include <boost/shared_ptr.hpp>
      29             : #include <boost/unordered_map.hpp>
      30             : #include <boost/noncopyable.hpp>
      31             : 
      32             : #include <com/sun/star/uno/Sequence.hxx>
      33             : 
      34             : #include <formula/opcode.hxx>
      35             : #include <formula/grammar.hxx>
      36             : #include <formula/token.hxx>
      37             : #include <formula/ExternalReferenceHelper.hxx>
      38             : 
      39             : #define FORMULA_MAXJUMPCOUNT    32  /* maximum number of jumps (ocChose) */
      40             : #define FORMULA_MAXTOKENS     8192  /* maximum number of tokens in formula */
      41             : 
      42             : 
      43             : namespace com { namespace sun { namespace star {
      44             :     namespace sheet {
      45             :         struct FormulaOpCodeMapEntry;
      46             :         struct FormulaToken;
      47             :     }
      48             : }}}
      49             : 
      50             : 
      51             : namespace formula
      52             : {
      53             :     class FormulaTokenArray;
      54             : 
      55             : struct FormulaArrayStack
      56             : {
      57             :     FormulaArrayStack*  pNext;
      58             :     FormulaTokenArray*  pArr;
      59             :     bool bTemp;
      60             : };
      61             : 
      62             : 
      63             : typedef ::boost::unordered_map< OUString, OpCode, OUStringHash, ::std::equal_to< OUString > > OpCodeHashMap;
      64             : typedef ::boost::unordered_map< OUString, OUString, OUStringHash, ::std::equal_to< OUString > > ExternalHashMap;
      65             : 
      66             : class FORMULA_DLLPUBLIC FormulaCompiler : boost::noncopyable
      67             : {
      68             : public:
      69             :     FormulaCompiler();
      70             :     FormulaCompiler(FormulaTokenArray& _rArr);
      71             :     virtual ~FormulaCompiler();
      72             : 
      73             :     // SUNWS8 needs a forward declared friend, otherwise members of the outer
      74             :     // class are not accessible.
      75             :     class OpCodeMap;
      76             :     friend class FormulaCompiler::OpCodeMap;
      77             : 
      78             :     /** Mappings from strings to OpCodes and vice versa. */
      79             :     class FORMULA_DLLPUBLIC OpCodeMap
      80             :     {
      81             :         OpCodeHashMap         * mpHashMap;                 /// Hash map of symbols, OUString -> OpCode
      82             :         OUString              * mpTable;                   /// Array of symbols, OpCode -> OUString, offset==OpCode
      83             :         ExternalHashMap       * mpExternalHashMap;         /// Hash map of ocExternal, Filter String -> AddIn String
      84             :         ExternalHashMap       * mpReverseExternalHashMap;  /// Hash map of ocExternal, AddIn String -> Filter String
      85             :         FormulaGrammar::Grammar meGrammar;                  /// Grammar, language and reference convention
      86             :         sal_uInt16              mnSymbols;                  /// Count of OpCode symbols
      87             :         bool                    mbCore      : 1;            /// If mapping was setup by core, not filters
      88             :         bool                    mbEnglish   : 1;            /// If English symbols and external names
      89             : 
      90             :         OpCodeMap();                              // prevent usage
      91             :         OpCodeMap( const OpCodeMap& );            // prevent usage
      92             :         OpCodeMap& operator=( const OpCodeMap& ); // prevent usage
      93             : 
      94             :     public:
      95             : 
      96         580 :         OpCodeMap(sal_uInt16 nSymbols, bool bCore, FormulaGrammar::Grammar eGrammar ) :
      97         580 :             mpHashMap( new OpCodeHashMap( nSymbols)),
      98        1160 :             mpTable( new OUString[ nSymbols ]),
      99         580 :             mpExternalHashMap( new ExternalHashMap),
     100         580 :             mpReverseExternalHashMap( new ExternalHashMap),
     101             :             meGrammar( eGrammar),
     102             :             mnSymbols( nSymbols),
     103        3480 :             mbCore( bCore)
     104             :         {
     105         580 :             mbEnglish = FormulaGrammar::isEnglish( meGrammar);
     106         580 :         }
     107             :         virtual ~OpCodeMap();
     108             : 
     109             :         /** Copy mappings from r into this map, effectively replacing this map.
     110             : 
     111             :             @param  bOverrideKnownBad
     112             :                     If TRUE, override known legacy bad function names with
     113             :                     correct ones if the conditions can be derived from the
     114             :                     current maps.
     115             :          */
     116             :         void copyFrom( const OpCodeMap& r, bool bOverrideKnownBad );
     117             : 
     118             :         /// Get the symbol String -> OpCode hash map for finds.
     119       51200 :         inline const OpCodeHashMap* getHashMap() const { return mpHashMap; }
     120             : 
     121             :         /// Get the symbol String -> AddIn String hash map for finds.
     122        2444 :         inline const ExternalHashMap* getExternalHashMap() const { return mpExternalHashMap; }
     123             : 
     124             :         /// Get the AddIn String -> symbol String hash map for finds.
     125         304 :         inline const ExternalHashMap* getReverseExternalHashMap() const { return mpReverseExternalHashMap; }
     126             : 
     127             :         /// Get the symbol string matching an OpCode.
     128      350455 :         inline const OUString& getSymbol( const OpCode eOp ) const
     129             :         {
     130             :             DBG_ASSERT( sal_uInt16(eOp) < mnSymbols, "OpCodeMap::getSymbol: OpCode out of range");
     131      350455 :             if (sal_uInt16(eOp) < mnSymbols)
     132      350455 :                 return mpTable[ eOp ];
     133           0 :             static OUString s_sEmpty;
     134           0 :             return s_sEmpty;
     135             :         }
     136             : 
     137             :         /// Get the first character of the symbol string matching an OpCode.
     138      142236 :         inline sal_Unicode getSymbolChar( const OpCode eOp ) const {  return getSymbol(eOp)[0]; };
     139             : 
     140             :         /// Get the grammar.
     141       82536 :         inline FormulaGrammar::Grammar getGrammar() const { return meGrammar; }
     142             : 
     143             :         /// Get the symbol count.
     144       91250 :         inline sal_uInt16 getSymbolCount() const { return mnSymbols; }
     145             : 
     146             :         /** Are these English symbols, as opposed to native language (which may
     147             :             be English as well)? */
     148      161228 :         inline bool isEnglish() const { return mbEnglish; }
     149             : 
     150             :         /// Is it an internal core mapping, or setup by filters?
     151             :         inline bool isCore() const { return mbCore; }
     152             : 
     153             :         /// Is it an ODF 1.1 compatibility mapping?
     154         154 :         inline bool isPODF() const { return FormulaGrammar::isPODF( meGrammar); }
     155             : 
     156             :         /// Is it an ODFF / ODF 1.2 mapping?
     157        7232 :         inline bool isODFF() const { return FormulaGrammar::isODFF( meGrammar); }
     158             : 
     159             :         /// Is it an OOXML mapping?
     160             :         inline bool isOOXML() const { return FormulaGrammar::isOOXML( meGrammar); }
     161             : 
     162             :         /// Does it have external symbol/name mappings?
     163        7274 :         inline bool hasExternals() const { return !mpExternalHashMap->empty(); }
     164             : 
     165             :         /// Put entry of symbol String and OpCode pair.
     166             :         void putOpCode( const OUString & rStr, const OpCode eOp );
     167             : 
     168             :         /// Put entry of symbol String and AddIn international String pair.
     169             :         void putExternal( const OUString & rSymbol, const OUString & rAddIn );
     170             : 
     171             :         /** Put entry of symbol String and AddIn international String pair,
     172             :             failing silently if rAddIn name already exists. */
     173             :         void putExternalSoftly( const OUString & rSymbol, const OUString & rAddIn );
     174             : 
     175             :         /// Core implementation of XFormulaOpCodeMapper::getMappings()
     176             :         ::com::sun::star::uno::Sequence< ::com::sun::star::sheet::FormulaToken >
     177             :             createSequenceOfFormulaTokens(const FormulaCompiler& _rCompiler,
     178             :                     const ::com::sun::star::uno::Sequence< OUString >& rNames ) const;
     179             : 
     180             :         /// Core implementation of XFormulaOpCodeMapper::getAvailableMappings()
     181             :         ::com::sun::star::uno::Sequence<
     182             :             ::com::sun::star::sheet::FormulaOpCodeMapEntry >
     183             :             createSequenceOfAvailableMappings( const FormulaCompiler& _rCompiler,const sal_Int32 nGroup ) const;
     184             : 
     185             :         /** The value used in createSequenceOfAvailableMappings() and thus in
     186             :             XFormulaOpCodeMapper::getMappings() for an unknown symbol. */
     187             :         static sal_Int32 getOpCodeUnknown();
     188             : 
     189             :     private:
     190             : 
     191             :         /** Conditionally put a mapping in copyFrom() context.
     192             : 
     193             :             Does NOT check eOp range!
     194             :          */
     195             :         void putCopyOpCode( const OUString& rSymbol, OpCode eOp );
     196             :     };
     197             : 
     198             : public:
     199             :     typedef ::boost::shared_ptr< const OpCodeMap >  OpCodeMapPtr;
     200             :     typedef ::boost::shared_ptr< OpCodeMap >        NonConstOpCodeMapPtr;
     201             : 
     202             :     /** Get OpCodeMap for formula language.
     203             :         @param nLanguage
     204             :             One of ::com::sun::star::sheet::FormulaLanguage constants.
     205             :         @return Map for nLanguage. If nLanguage is unknown, a NULL map is returned.
     206             :      */
     207             :     OpCodeMapPtr GetOpCodeMap( const sal_Int32 nLanguage ) const;
     208             : 
     209             :     /** Create an internal symbol map from API mapping.
     210             :         @param bEnglish
     211             :             Use English number parser / formatter instead of native.
     212             :      */
     213             :     OpCodeMapPtr CreateOpCodeMap(
     214             :             const ::com::sun::star::uno::Sequence<
     215             :             const ::com::sun::star::sheet::FormulaOpCodeMapEntry > & rMapping,
     216             :             bool bEnglish );
     217             : 
     218             :     /** Get current OpCodeMap in effect. */
     219       55216 :     inline OpCodeMapPtr GetCurrentOpCodeMap() const { return mxSymbols; }
     220             : 
     221             :     /** Get OpCode for English symbol.
     222             :         Used in XFunctionAccess to create token array.
     223             :         @param rName
     224             :             Symbol to lookup. MUST be upper case.
     225             :      */
     226             :     OpCode GetEnglishOpCode( const OUString& rName ) const;
     227             : 
     228             :     sal_uInt16 GetErrorConstant( const OUString& rName ) const;
     229             : 
     230             :     void EnableJumpCommandReorder( bool bEnable );
     231             :     void EnableStopOnError( bool bEnable );
     232             : 
     233             :     static bool IsOpCodeVolatile( OpCode eOp );
     234             :     static bool IsOpCodeJumpCommand( OpCode eOp );
     235             : 
     236             :     static bool DeQuote( OUString& rStr );
     237             : 
     238             : 
     239             :     static const OUString&  GetNativeSymbol( OpCode eOp );
     240             :     static sal_Unicode      GetNativeSymbolChar( OpCode eOp );
     241             :     static  bool            IsMatrixFunction(OpCode _eOpCode);   // if a function _always_ returns a Matrix
     242             : 
     243       15272 :     short GetNumFormatType() const { return nNumFmt; }
     244             :     bool  CompileTokenArray();
     245             : 
     246             :     void CreateStringFromTokenArray( OUString& rFormula );
     247             :     void CreateStringFromTokenArray( OUStringBuffer& rBuffer );
     248             :     FormulaToken* CreateStringFromToken( OUString& rFormula, FormulaToken* pToken,
     249             :                                     bool bAllowArrAdvance = false );
     250             :     FormulaToken* CreateStringFromToken( OUStringBuffer& rBuffer, FormulaToken* pToken,
     251             :                                     bool bAllowArrAdvance = false );
     252             : 
     253             :     void AppendBoolean( OUStringBuffer& rBuffer, bool bVal ) const;
     254             :     void AppendDouble( OUStringBuffer& rBuffer, double fVal ) const;
     255             :     void AppendString( OUStringBuffer& rBuffer, const OUString & rStr ) const;
     256             : 
     257             :     /** Set symbol map corresponding to one of predefined formula::FormulaGrammar::Grammar,
     258             :         including an address reference convention. */
     259      333256 :     inline  FormulaGrammar::Grammar   GetGrammar() const { return meGrammar; }
     260             : 
     261             :     static void UpdateSeparatorsNative( const OUString& rSep, const OUString& rArrayColSep, const OUString& rArrayRowSep );
     262             :     static void ResetNativeSymbols();
     263             :     static void SetNativeSymbols( const OpCodeMapPtr& xMap );
     264             : 
     265             :     /** Separators mapped when loading opcodes from the resource, values other
     266             :         than RESOURCE_BASE may override the resource strings. Used by OpCodeList
     267             :         implementation via loadSymbols().
     268             :      */
     269             :     enum SeparatorType
     270             :     {
     271             :         RESOURCE_BASE,
     272             :         SEMICOLON_BASE,
     273             :         COMMA_BASE
     274             :     };
     275             : 
     276             : protected:
     277             :     virtual OUString FindAddInFunction( const OUString& rUpperName, bool bLocalFirst ) const;
     278             :     virtual void fillFromAddInCollectionUpperName( NonConstOpCodeMapPtr xMap ) const;
     279             :     virtual void fillFromAddInMap( NonConstOpCodeMapPtr xMap, FormulaGrammar::Grammar _eGrammar ) const;
     280             :     virtual void fillFromAddInCollectionEnglishName( NonConstOpCodeMapPtr xMap ) const;
     281             :     virtual void fillAddInToken(::std::vector< ::com::sun::star::sheet::FormulaOpCodeMapEntry >& _rVec,bool _bIsEnglish) const;
     282             : 
     283             :     virtual void SetError(sal_uInt16 nError);
     284             :     virtual FormulaTokenRef ExtendRangeReference( FormulaToken & rTok1, FormulaToken & rTok2, bool bReuseDoubleRef );
     285             :     virtual bool HandleExternalReference(const FormulaToken& _aToken);
     286             :     virtual bool HandleRange();
     287             :     virtual bool HandleSingleRef();
     288             :     virtual bool HandleDbData();
     289             : 
     290             :     virtual void CreateStringFromExternal(OUStringBuffer& rBuffer, FormulaToken* pTokenP) const;
     291             :     virtual void CreateStringFromSingleRef(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
     292             :     virtual void CreateStringFromDoubleRef(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
     293             :     virtual void CreateStringFromMatrix(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
     294             :     virtual void CreateStringFromIndex(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
     295             :     virtual void LocalizeString( OUString& rName ) const;   // modify rName - input: exact name
     296             : 
     297             :     void AppendErrorConstant( OUStringBuffer& rBuffer, sal_uInt16 nError ) const;
     298             : 
     299             :     bool   GetToken();
     300             :     OpCode NextToken();
     301             :     void PutCode( FormulaTokenRef& );
     302             :     void Factor();
     303             :     void RangeLine();
     304             :     void UnionLine();
     305             :     void IntersectionLine();
     306             :     void UnaryLine();
     307             :     void PostOpLine();
     308             :     void PowLine();
     309             :     void MulDivLine();
     310             :     void AddSubLine();
     311             :     void ConcatLine();
     312             :     void CompareLine();
     313             :     void NotLine();
     314             :     OpCode Expression();
     315             :     void PopTokenArray();
     316             :     void PushTokenArray( FormulaTokenArray*, bool = false );
     317             : 
     318             :     bool MergeRangeReference( FormulaToken * * const pCode1, FormulaToken * const * const pCode2 );
     319             : 
     320             :     OUString            aCorrectedFormula;      // autocorrected Formula
     321             :     OUString            aCorrectedSymbol;       // autocorrected Symbol
     322             : 
     323             :     OpCodeMapPtr        mxSymbols;              // which symbols are used
     324             : 
     325             :     FormulaTokenRef     mpToken;                // current token
     326             :     FormulaTokenRef     pCurrentFactorToken;    // current factor token (of Factor() method)
     327             :     FormulaTokenArray*  pArr;
     328             : 
     329             :     FormulaToken**      pCode;
     330             :     FormulaArrayStack*  pStack;
     331             : 
     332             :     OpCode              eLastOp;
     333             :     short               nRecursion;             // GetToken() recursions
     334             :     short               nNumFmt;                // set during CompileTokenArray()
     335             :     sal_uInt16          pc;                     // program counter
     336             : 
     337             :     FormulaGrammar::Grammar meGrammar;          // The grammar used, language plus convention.
     338             : 
     339             :     bool                bAutoCorrect;           // whether to apply AutoCorrection
     340             :     bool                bCorrected;             // AutoCorrection was applied
     341             :     bool                glSubTotal;             // if code contains one or more subtotal functions
     342             : 
     343             :     bool mbJumpCommandReorder; /// Whether or not to reorder RPN for jump commands.
     344             :     bool mbStopOnError;        /// Whether to stop compilation on first encountered error.
     345             : 
     346             : private:
     347             :     void InitSymbolsNative() const;    /// only SymbolsNative, on first document creation
     348             :     void InitSymbolsEnglish() const;   /// only SymbolsEnglish, maybe later
     349             :     void InitSymbolsPODF() const;      /// only SymbolsPODF, on demand
     350             :     void InitSymbolsODFF() const;      /// only SymbolsODFF, on demand
     351             :     void InitSymbolsEnglishXL() const; /// only SymbolsEnglishXL, on demand
     352             :     void InitSymbolsOOXML() const;     /// only SymbolsOOXML, on demand
     353             : 
     354             :     void loadSymbols( sal_uInt16 nSymbols, FormulaGrammar::Grammar eGrammar, NonConstOpCodeMapPtr& rxMap,
     355             :             SeparatorType eSepType = SEMICOLON_BASE ) const;
     356             : 
     357       78278 :     static inline void ForceArrayOperator( FormulaTokenRef& rCurr, const FormulaTokenRef& rPrev )
     358             :         {
     359      193956 :             if ( rPrev && rPrev->HasForceArray() && rCurr->GetOpCode() != ocPush &&
     360       78378 :                     (rCurr->GetType() == svByte || rCurr->GetType() == svJump) &&
     361          50 :                     !rCurr->HasForceArray() )
     362           0 :                 rCurr->SetForceArray( true);
     363       78278 :         }
     364             : 
     365             :     // SUNWS7 needs a forward declared friend, otherwise members of the outer
     366             :     // class are not accessible.
     367             :     class CurrentFactor;
     368             :     friend class FormulaCompiler::CurrentFactor;
     369             :     class CurrentFactor
     370             :     {
     371             :         FormulaTokenRef  pPrevFac;
     372             :         FormulaCompiler* pCompiler;
     373             :         // not implemented
     374             :         CurrentFactor( const CurrentFactor& );
     375             :         CurrentFactor& operator=( const CurrentFactor& );
     376             :     public:
     377       56800 :         explicit CurrentFactor( FormulaCompiler* pComp )
     378             :             : pPrevFac( pComp->pCurrentFactorToken )
     379       56800 :             , pCompiler( pComp )
     380       56800 :             {}
     381       56800 :         ~CurrentFactor()
     382       56800 :             { pCompiler->pCurrentFactorToken = pPrevFac; }
     383             :         // yes, this operator= may modify the RValue
     384       11938 :         void operator=( FormulaTokenRef& r )
     385             :             {
     386       11938 :                 ForceArrayOperator( r, pPrevFac);
     387       11938 :                 pCompiler->pCurrentFactorToken = r;
     388       11938 :             }
     389           2 :         void operator=( FormulaToken* p )
     390             :             {
     391           2 :                 FormulaTokenRef xTemp( p );
     392           2 :                 *this = xTemp;
     393           2 :             }
     394       11936 :         operator FormulaTokenRef&()
     395       11936 :             { return pCompiler->pCurrentFactorToken; }
     396       12230 :         FormulaToken* operator->()
     397       12230 :             { return pCompiler->pCurrentFactorToken.operator->(); }
     398           2 :         operator FormulaToken*()
     399           2 :             { return operator->(); }
     400             :     };
     401             : 
     402             : 
     403             :     mutable NonConstOpCodeMapPtr  mxSymbolsODFF;      // ODFF symbols
     404             :     mutable NonConstOpCodeMapPtr  mxSymbolsPODF;      // ODF 1.1 symbols
     405             :     mutable NonConstOpCodeMapPtr  mxSymbolsNative;    // native symbols
     406             :     mutable NonConstOpCodeMapPtr  mxSymbolsEnglish;   // English symbols
     407             :     mutable NonConstOpCodeMapPtr  mxSymbolsEnglishXL; // English Excel symbols (for VBA formula parsing)
     408             :     mutable NonConstOpCodeMapPtr  mxSymbolsOOXML;     // Excel OOXML symbols
     409             : };
     410             : 
     411             : } // formula
     412             : 
     413             : 
     414             : #endif // INCLUDED_FORMULA_FORMULACOMPILER_HXX
     415             : 
     416             : 
     417             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10