xref: /trunk/main/formula/inc/formula/FormulaCompiler.hxx (revision 195598447694ec8542914a9b51ae9942e8ebacd9) !
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef FORMULA_COMPILER_HXX_INCLUDED
25 #define FORMULA_COMPILER_HXX_INCLUDED
26 
27 #include "formula/formuladllapi.h"
28 #include <tools/string.hxx>
29 #include <tools/debug.hxx>
30 #include <rtl/ustrbuf.hxx>
31 
32 #include <boost/shared_ptr.hpp>
33 #include <unordered_map>
34 
35 #include <com/sun/star/uno/Sequence.hxx>
36 
37 #include "formula/opcode.hxx"
38 #include "formula/grammar.hxx"
39 #include "formula/token.hxx"
40 #include "formula/ExternalReferenceHelper.hxx"
41 
42 
43 #define MAXJUMPCOUNT 32     /* maximum number of jumps (ocChose) */
44 #define MAXCODE      2048   /* maximum number of tokens in formula */
45 
46 
47 namespace com { namespace sun { namespace star {
48     namespace sheet {
49         struct FormulaOpCodeMapEntry;
50         struct FormulaToken;
51     }
52 }}}
53 
54 
55 namespace formula
56 {
57     class FormulaTokenArray;
58 
59 struct FormulaArrayStack
60 {
61     FormulaArrayStack*  pNext;
62     FormulaTokenArray*  pArr;
63     sal_Bool bTemp;
64 };
65 
66 
67 struct FORMULA_DLLPUBLIC StringHashCode
68 {
operator ()formula::StringHashCode69     size_t operator()( const String& rStr ) const
70     {
71         return rtl_ustr_hashCode_WithLength( rStr.GetBuffer(), rStr.Len() );
72     }
73 };
74 
75 typedef ::std::unordered_map< String, OpCode, StringHashCode, ::std::equal_to< String > > OpCodeHashMap;
76 typedef ::std::unordered_map< String, String, StringHashCode, ::std::equal_to< String > > ExternalHashMap;
77 
78 class FORMULA_DLLPUBLIC FormulaCompiler
79 {
80 public:
81     FormulaCompiler();
82     FormulaCompiler(FormulaTokenArray& _rArr);
83     virtual ~FormulaCompiler();
84 
85     // SUNWS8 needs a forward declared friend, otherwise members of the outer
86     // class are not accessible.
87     class OpCodeMap;
88     friend class FormulaCompiler::OpCodeMap;
89 
90     /** Mappings from strings to OpCodes and vice versa. */
91     class FORMULA_DLLPUBLIC OpCodeMap
92     {
93         OpCodeHashMap         * mpHashMap;                 /// Hash map of symbols, String -> OpCode
94         String              *   mpTable;                   /// Array of symbols, OpCode -> String, offset==OpCode
95         ExternalHashMap       * mpExternalHashMap;         /// Hash map of ocExternal, Filter String -> AddIn String
96         ExternalHashMap       * mpReverseExternalHashMap;  /// Hash map of ocExternal, AddIn String -> Filter String
97         FormulaGrammar::Grammar meGrammar;                  /// Grammar, language and reference convention
98         sal_uInt16                  mnSymbols;                  /// Count of OpCode symbols
99         bool                    mbCore      : 1;            /// If mapping was setup by core, not filters
100         bool                    mbEnglish   : 1;            /// If English symbols and external names
101 
102         OpCodeMap();                              // prevent usage
103         OpCodeMap( const OpCodeMap& );            // prevent usage
104         OpCodeMap& operator=( const OpCodeMap& ); // prevent usage
105 
106     public:
107 
OpCodeMap(sal_uInt16 nSymbols,bool bCore,FormulaGrammar::Grammar eGrammar)108         OpCodeMap(sal_uInt16 nSymbols, bool bCore, FormulaGrammar::Grammar eGrammar ) :
109             mpHashMap( new OpCodeHashMap( nSymbols)),
110             mpTable( new String[ nSymbols ]),
111             mpExternalHashMap( new ExternalHashMap),
112             mpReverseExternalHashMap( new ExternalHashMap),
113             meGrammar( eGrammar),
114             mnSymbols( nSymbols),
115             mbCore( bCore)
116         {
117             mbEnglish = FormulaGrammar::isEnglish( meGrammar);
118         }
119         virtual ~OpCodeMap();
120 
121 
122         /// Get the symbol String -> OpCode hash map for finds.
getHashMap() const123         inline const OpCodeHashMap* getHashMap() const { return mpHashMap; }
124 
125         /// Get the symbol String -> AddIn String hash map for finds.
getExternalHashMap() const126         inline const ExternalHashMap* getExternalHashMap() const { return mpExternalHashMap; }
127 
128         /// Get the AddIn String -> symbol String hash map for finds.
getReverseExternalHashMap() const129         inline const ExternalHashMap* getReverseExternalHashMap() const { return mpReverseExternalHashMap; }
130 
131         /// Get the symbol string matching an OpCode.
getSymbol(const OpCode eOp) const132         inline const String& getSymbol( const OpCode eOp ) const
133         {
134             DBG_ASSERT( sal_uInt16(eOp) < mnSymbols, "OpCodeMap::getSymbol: OpCode out of range");
135             if (sal_uInt16(eOp) < mnSymbols)
136                 return mpTable[ eOp ];
137             static String s_sEmpty;
138             return s_sEmpty;
139         }
140 
141         /// Get the grammar.
getGrammar() const142         inline FormulaGrammar::Grammar getGrammar() const { return meGrammar; }
143 
144         /// Get the symbol count.
getSymbolCount() const145         inline sal_uInt16 getSymbolCount() const { return mnSymbols; }
146 
147         /** Are these English symbols, as opposed to native language (which may
148             be English as well)? */
isEnglish() const149         inline bool isEnglish() const { return mbEnglish; }
150 
151         /// Is it an internal core mapping, or setup by filters?
isCore() const152         inline bool isCore() const { return mbCore; }
153 
154         /// Is it an ODF 1.1 compatibility mapping?
isPODF() const155         inline bool isPODF() const { return FormulaGrammar::isPODF( meGrammar); }
156 
157         /// Is it an ODFF / ODF 1.2 mapping?
isODFF() const158         inline bool isODFF() const { return FormulaGrammar::isODFF( meGrammar); }
159 
160         /// Does it have external symbol/name mappings?
hasExternals() const161         inline bool hasExternals() const { return !mpExternalHashMap->empty(); }
162 
163         /// Put entry of symbol String and OpCode pair.
164         void putOpCode( const String & rStr, const OpCode eOp );
165 
166         /// Put an additional symbol for eOp that is accepted on input only.
167         void putOpCodeAlias( const String & rStr, const OpCode eOp );
168 
169         /// Put entry of symbol String and AddIn international String pair.
170         void putExternal( const String & rSymbol, const String & rAddIn );
171 
172         /** Put entry of symbol String and AddIn international String pair,
173             failing silently if rAddIn name already exists. */
174         void putExternalSoftly( const String & rSymbol, const String & rAddIn );
175 
176         /// Core implementation of XFormulaOpCodeMapper::getMappings()
177         ::com::sun::star::uno::Sequence< ::com::sun::star::sheet::FormulaToken >
178             createSequenceOfFormulaTokens(const FormulaCompiler& _rCompiler,
179                     const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rNames ) const;
180 
181         /// Core implementation of XFormulaOpCodeMapper::getAvailableMappings()
182         ::com::sun::star::uno::Sequence<
183             ::com::sun::star::sheet::FormulaOpCodeMapEntry >
184             createSequenceOfAvailableMappings( const FormulaCompiler& _rCompiler,const sal_Int32 nGroup ) const;
185 
186         /** The value used in createSequenceOfAvailableMappings() and thus in
187             XFormulaOpCodeMapper::getMappings() for an unknown symbol. */
188         static sal_Int32 getOpCodeUnknown();
189     };
190 
191 public:
192     typedef ::boost::shared_ptr< const OpCodeMap >  OpCodeMapPtr;
193     typedef ::boost::shared_ptr< OpCodeMap >        NonConstOpCodeMapPtr;
194 
195     /** Get OpCodeMap for formula language.
196         @param nLanguage
197             One of ::com::sun::star::sheet::FormulaLanguage constants.
198         @return Map for nLanguage. If nLanguage is unknown, a NULL map is returned.
199      */
200     OpCodeMapPtr GetOpCodeMap( const sal_Int32 nLanguage ) const;
201 
202     /** Create an internal symbol map from API mapping.
203         @param bEnglish
204             Use English number parser / formatter instead of native.
205      */
206     OpCodeMapPtr CreateOpCodeMap(
207             const ::com::sun::star::uno::Sequence<
208             const ::com::sun::star::sheet::FormulaOpCodeMapEntry > & rMapping,
209             bool bEnglish );
210 
211     /** Get OpCode for English symbol.
212         Used in XFunctionAccess to create token array.
213         @param rName
214             Symbol to lookup. MUST be upper case.
215      */
216     OpCode GetEnglishOpCode( const String& rName ) const;
217 
SetCompileForFAP(sal_Bool bVal)218     void            SetCompileForFAP( sal_Bool bVal )
219                         { bCompileForFAP = bVal; bIgnoreErrors = bVal; }
220 
221     static sal_Bool DeQuote( String& rStr );
222 
223     static const String&    GetNativeSymbol( OpCode eOp );
224     static  sal_Bool            IsMatrixFunction(OpCode _eOpCode);   // if a function _always_ returns a Matrix
225 
GetNumFormatType() const226     short GetNumFormatType() const { return nNumFmt; }
227     sal_Bool  CompileTokenArray();
228 
229     void CreateStringFromTokenArray( String& rFormula );
230     void CreateStringFromTokenArray( rtl::OUStringBuffer& rBuffer );
231     FormulaToken* CreateStringFromToken( String& rFormula, FormulaToken* pToken,
232                                     sal_Bool bAllowArrAdvance = sal_False );
233     FormulaToken* CreateStringFromToken( rtl::OUStringBuffer& rBuffer, FormulaToken* pToken,
234                                     sal_Bool bAllowArrAdvance = sal_False );
235 
236     void AppendBoolean( rtl::OUStringBuffer& rBuffer, bool bVal );
237     void AppendDouble( rtl::OUStringBuffer& rBuffer, double fVal );
238     void AppendString( rtl::OUStringBuffer& rBuffer, const String & rStr );
239 
240     /** Set symbol map corresponding to one of predefined formula::FormulaGrammar::Grammar,
241         including an address reference convention. */
GetGrammar() const242     inline  FormulaGrammar::Grammar   GetGrammar() const { return meGrammar; }
243 
244 protected:
245     virtual String FindAddInFunction( const String& rUpperName, sal_Bool bLocalFirst ) const;
246     virtual void fillFromAddInCollectionUpperName( NonConstOpCodeMapPtr xMap ) const;
247     virtual void fillFromAddInMap( NonConstOpCodeMapPtr xMap, FormulaGrammar::Grammar _eGrammar ) const;
248     virtual void fillFromAddInCollectionEnglishName( NonConstOpCodeMapPtr xMap ) const;
249     virtual void fillAddInToken(::std::vector< ::com::sun::star::sheet::FormulaOpCodeMapEntry >& _rVec,bool _bIsEnglish) const;
250 
251     virtual void SetError(sal_uInt16 nError);
252     virtual FormulaTokenRef ExtendRangeReference( FormulaToken & rTok1, FormulaToken & rTok2, bool bReuseDoubleRef );
253     virtual sal_Bool HandleExternalReference(const FormulaToken& _aToken);
254     virtual sal_Bool HandleRange();
255     virtual sal_Bool HandleSingleRef();
256     virtual sal_Bool HandleDbData();
257 
258     virtual void CreateStringFromExternal(rtl::OUStringBuffer& rBuffer, FormulaToken* pTokenP);
259     virtual void CreateStringFromSingleRef(rtl::OUStringBuffer& rBuffer,FormulaToken* pTokenP);
260     virtual void CreateStringFromDoubleRef(rtl::OUStringBuffer& rBuffer,FormulaToken* pTokenP);
261     virtual void CreateStringFromMatrix(rtl::OUStringBuffer& rBuffer,FormulaToken* pTokenP);
262     virtual void CreateStringFromIndex(rtl::OUStringBuffer& rBuffer,FormulaToken* pTokenP);
263     virtual void LocalizeString( String& rName );   // modify rName - input: exact name
264     virtual sal_Bool IsImportingXML() const;
265 
266     sal_Bool   GetToken();
267     OpCode NextToken();
268     void PutCode( FormulaTokenRef& );
269     void Factor();
270     void RangeLine();
271     void UnionLine();
272     void IntersectionLine();
273     void UnaryLine();
274     void PostOpLine();
275     void PowLine();
276     void MulDivLine();
277     void AddSubLine();
278     void ConcatLine();
279     void CompareLine();
280     void NotLine();
281     OpCode Expression();
282     void PopTokenArray();
283     void PushTokenArray( FormulaTokenArray*, sal_Bool = sal_False );
284 
285     bool MergeRangeReference( FormulaToken * * const pCode1, FormulaToken * const * const pCode2 );
286 
287     String              aCorrectedFormula;          // autocorrected Formula
288     String              aCorrectedSymbol;           // autocorrected Symbol
289 
290     OpCodeMapPtr        mxSymbols;                  // which symbols are used
291 
292     FormulaTokenRef     pToken;                     // current token
293     FormulaTokenRef     pCurrentFactorToken;        // current factor token (of Factor() method)
294     FormulaTokenArray*  pArr;
295     ExternalReferenceHelper* pExternalRef;
296 
297     FormulaToken**      pCode;
298     FormulaArrayStack*  pStack;
299 
300     OpCode              eLastOp;
301     short               nRecursion;                 // GetToken() recursions
302     short               nNumFmt;                    // set during CompileTokenArray()
303     sal_uInt16              pc;
304 
305     FormulaGrammar::Grammar
306                         meGrammar;          // The grammar used, language plus convention.
307 
308     sal_Bool                bAutoCorrect;               // whether to apply AutoCorrection
309     sal_Bool                bCorrected;                 // AutoCorrection was applied
310     sal_Bool                bCompileForFAP;             //! not real RPN but names, for FunctionAutoPilot,
311                                                     // will not be resolved
312     sal_Bool                bIgnoreErrors;              // on AutoCorrect and CompileForFAP
313                                                     // ignore errors and create RPN nevertheless
314     sal_Bool                glSubTotal;                 // if code contains one or more subtotal functions
315 private:
316     void InitSymbolsNative() const;    /// only SymbolsNative, on first document creation
317     void InitSymbolsEnglish() const;   /// only SymbolsEnglish, maybe later
318     void InitSymbolsPODF() const;      /// only SymbolsPODF, on demand
319     void InitSymbolsODFF() const;      /// only SymbolsODFF, on demand
320 
321     void loadSymbols(sal_uInt16 _nSymbols,FormulaGrammar::Grammar _eGrammar,NonConstOpCodeMapPtr& _xMap) const;
322 
ForceArrayOperator(FormulaTokenRef & rCurr,const FormulaTokenRef & rPrev)323     static inline void ForceArrayOperator( FormulaTokenRef& rCurr, const FormulaTokenRef& rPrev )
324         {
325             // ODF 1.3: the lookup and regression functions take array-mode arguments, but only the tokens nested in them
326             // are marked, not the function itself. A bare reference in a scalar parameter keeps the implicit intersection;
327             // an operator or function nested in that parameter is array-evaluated too.
328             if ( rPrev.Is() && ( rPrev->HasForceArray() || ( rPrev.get() != rCurr.get() && ( rPrev->GetOpCode() == ocHLookup ||
329                         rPrev->GetOpCode() == ocVLookup || rPrev->GetOpCode() == ocMatch || rPrev->GetOpCode() == ocRGP ||
330                         rPrev->GetOpCode() == ocRKP ) ) ) &&
331                     rCurr->GetType() == svByte && rCurr->GetOpCode() != ocPush
332                     && !rCurr->HasForceArray() )
333                 rCurr->SetForceArray( true);
334         }
335 
336     // SUNWS7 needs a forward declared friend, otherwise members of the outer
337     // class are not accessible.
338     class CurrentFactor;
339     friend class FormulaCompiler::CurrentFactor;
340     class CurrentFactor
341     {
342         FormulaTokenRef  pPrevFac;
343         FormulaCompiler* pCompiler;
344         // not implemented
345         CurrentFactor( const CurrentFactor& );
346         CurrentFactor& operator=( const CurrentFactor& );
347     public:
CurrentFactor(FormulaCompiler * pComp)348         explicit CurrentFactor( FormulaCompiler* pComp )
349             : pPrevFac( pComp->pCurrentFactorToken )
350             , pCompiler( pComp )
351             {}
~CurrentFactor()352         ~CurrentFactor()
353             { pCompiler->pCurrentFactorToken = pPrevFac; }
354         // yes, this operator= may modify the RValue
operator =(FormulaTokenRef & r)355         void operator=( FormulaTokenRef& r )
356             {
357                 ForceArrayOperator( r, pPrevFac);
358                 pCompiler->pCurrentFactorToken = r;
359             }
operator =(FormulaToken * p)360         void operator=( FormulaToken* p )
361             {
362                 FormulaTokenRef xTemp( p );
363                 *this = xTemp;
364             }
operator FormulaTokenRef&()365         operator FormulaTokenRef&()
366             { return pCompiler->pCurrentFactorToken; }
operator ->()367         FormulaToken* operator->()
368             { return pCompiler->pCurrentFactorToken.operator->(); }
operator FormulaToken*()369         operator FormulaToken*()
370             { return operator->(); }
371     };
372 
373 
374     mutable NonConstOpCodeMapPtr  mxSymbolsODFF;                          // ODFF symbols
375     mutable NonConstOpCodeMapPtr  mxSymbolsPODF;                          // ODF 1.1 symbols
376     mutable NonConstOpCodeMapPtr  mxSymbolsNative;                        // native symbols
377     mutable NonConstOpCodeMapPtr  mxSymbolsEnglish;                       // English symbols
378 };
379 // =============================================================================
380 } // formula
381 // =============================================================================
382 
383 #endif // FORMULA_COMPILER_HXX_INCLUDED
384