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 0 : OpCodeMap(sal_uInt16 nSymbols, bool bCore, FormulaGrammar::Grammar eGrammar ) :
97 0 : mpHashMap( new OpCodeHashMap( nSymbols)),
98 0 : mpTable( new OUString[ nSymbols ]),
99 0 : mpExternalHashMap( new ExternalHashMap),
100 0 : mpReverseExternalHashMap( new ExternalHashMap),
101 : meGrammar( eGrammar),
102 : mnSymbols( nSymbols),
103 0 : mbCore( bCore)
104 : {
105 0 : mbEnglish = FormulaGrammar::isEnglish( meGrammar);
106 0 : }
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 0 : inline const OpCodeHashMap* getHashMap() const { return mpHashMap; }
120 :
121 : /// Get the symbol String -> AddIn String hash map for finds.
122 0 : inline const ExternalHashMap* getExternalHashMap() const { return mpExternalHashMap; }
123 :
124 : /// Get the AddIn String -> symbol String hash map for finds.
125 0 : inline const ExternalHashMap* getReverseExternalHashMap() const { return mpReverseExternalHashMap; }
126 :
127 : /// Get the symbol string matching an OpCode.
128 0 : inline const OUString& getSymbol( const OpCode eOp ) const
129 : {
130 : DBG_ASSERT( sal_uInt16(eOp) < mnSymbols, "OpCodeMap::getSymbol: OpCode out of range");
131 0 : if (sal_uInt16(eOp) < mnSymbols)
132 0 : 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 0 : inline sal_Unicode getSymbolChar( const OpCode eOp ) const { return getSymbol(eOp)[0]; };
139 :
140 : /// Get the grammar.
141 0 : inline FormulaGrammar::Grammar getGrammar() const { return meGrammar; }
142 :
143 : /// Get the symbol count.
144 0 : 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 0 : 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 0 : inline bool isPODF() const { return FormulaGrammar::isPODF( meGrammar); }
155 :
156 : /// Is it an ODFF / ODF 1.2 mapping?
157 0 : 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 0 : 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 0 : 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 0 : void SetCompileForFAP( bool bVal )
231 0 : { bCompileForFAP = bVal; bIgnoreErrors = bVal; }
232 :
233 : static bool IsOpCodeVolatile( OpCode eOp );
234 :
235 : static bool DeQuote( OUString& rStr );
236 :
237 :
238 : static const OUString& GetNativeSymbol( OpCode eOp );
239 : static sal_Unicode GetNativeSymbolChar( OpCode eOp );
240 : static bool IsMatrixFunction(OpCode _eOpCode); // if a function _always_ returns a Matrix
241 :
242 0 : short GetNumFormatType() const { return nNumFmt; }
243 : bool CompileTokenArray();
244 :
245 : void CreateStringFromTokenArray( OUString& rFormula );
246 : void CreateStringFromTokenArray( OUStringBuffer& rBuffer );
247 : FormulaToken* CreateStringFromToken( OUString& rFormula, FormulaToken* pToken,
248 : bool bAllowArrAdvance = false );
249 : FormulaToken* CreateStringFromToken( OUStringBuffer& rBuffer, FormulaToken* pToken,
250 : bool bAllowArrAdvance = false );
251 :
252 : void AppendBoolean( OUStringBuffer& rBuffer, bool bVal ) const;
253 : void AppendDouble( OUStringBuffer& rBuffer, double fVal ) const;
254 : void AppendString( OUStringBuffer& rBuffer, const OUString & rStr ) const;
255 :
256 : /** Set symbol map corresponding to one of predefined formula::FormulaGrammar::Grammar,
257 : including an address reference convention. */
258 0 : inline FormulaGrammar::Grammar GetGrammar() const { return meGrammar; }
259 :
260 : static void UpdateSeparatorsNative( const OUString& rSep, const OUString& rArrayColSep, const OUString& rArrayRowSep );
261 : static void ResetNativeSymbols();
262 : static void SetNativeSymbols( const OpCodeMapPtr& xMap );
263 :
264 : /** Separators mapped when loading opcodes from the resource, values other
265 : than RESOURCE_BASE may override the resource strings. Used by OpCodeList
266 : implementation via loadSymbols().
267 : */
268 : enum SeparatorType
269 : {
270 : RESOURCE_BASE,
271 : SEMICOLON_BASE,
272 : COMMA_BASE
273 : };
274 :
275 : protected:
276 : virtual OUString FindAddInFunction( const OUString& rUpperName, bool bLocalFirst ) const;
277 : virtual void fillFromAddInCollectionUpperName( NonConstOpCodeMapPtr xMap ) const;
278 : virtual void fillFromAddInMap( NonConstOpCodeMapPtr xMap, FormulaGrammar::Grammar _eGrammar ) const;
279 : virtual void fillFromAddInCollectionEnglishName( NonConstOpCodeMapPtr xMap ) const;
280 : virtual void fillAddInToken(::std::vector< ::com::sun::star::sheet::FormulaOpCodeMapEntry >& _rVec,bool _bIsEnglish) const;
281 :
282 : virtual void SetError(sal_uInt16 nError);
283 : virtual FormulaTokenRef ExtendRangeReference( FormulaToken & rTok1, FormulaToken & rTok2, bool bReuseDoubleRef );
284 : virtual bool HandleExternalReference(const FormulaToken& _aToken);
285 : virtual bool HandleRange();
286 : virtual bool HandleSingleRef();
287 : virtual bool HandleDbData();
288 :
289 : virtual void CreateStringFromExternal(OUStringBuffer& rBuffer, FormulaToken* pTokenP) const;
290 : virtual void CreateStringFromSingleRef(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
291 : virtual void CreateStringFromDoubleRef(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
292 : virtual void CreateStringFromMatrix(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
293 : virtual void CreateStringFromIndex(OUStringBuffer& rBuffer,FormulaToken* pTokenP) const;
294 : virtual void LocalizeString( OUString& rName ) const; // modify rName - input: exact name
295 :
296 : void AppendErrorConstant( OUStringBuffer& rBuffer, sal_uInt16 nError ) const;
297 :
298 : bool GetToken();
299 : OpCode NextToken();
300 : void PutCode( FormulaTokenRef& );
301 : void Factor();
302 : void RangeLine();
303 : void UnionLine();
304 : void IntersectionLine();
305 : void UnaryLine();
306 : void PostOpLine();
307 : void PowLine();
308 : void MulDivLine();
309 : void AddSubLine();
310 : void ConcatLine();
311 : void CompareLine();
312 : void NotLine();
313 : OpCode Expression();
314 : void PopTokenArray();
315 : void PushTokenArray( FormulaTokenArray*, bool = false );
316 :
317 : bool MergeRangeReference( FormulaToken * * const pCode1, FormulaToken * const * const pCode2 );
318 :
319 : OUString aCorrectedFormula; // autocorrected Formula
320 : OUString aCorrectedSymbol; // autocorrected Symbol
321 :
322 : OpCodeMapPtr mxSymbols; // which symbols are used
323 :
324 : FormulaTokenRef mpToken; // current token
325 : FormulaTokenRef pCurrentFactorToken; // current factor token (of Factor() method)
326 : FormulaTokenArray* pArr;
327 :
328 : FormulaToken** pCode;
329 : FormulaArrayStack* pStack;
330 :
331 : OpCode eLastOp;
332 : short nRecursion; // GetToken() recursions
333 : short nNumFmt; // set during CompileTokenArray()
334 : sal_uInt16 pc; // program counter
335 :
336 : FormulaGrammar::Grammar meGrammar; // The grammar used, language plus convention.
337 :
338 : bool bAutoCorrect; // whether to apply AutoCorrection
339 : bool bCorrected; // AutoCorrection was applied
340 : bool bCompileForFAP; //! not real RPN but names, for FunctionAutoPilot
341 : // will not be resolved
342 : bool bIgnoreErrors; // on AutoCorrect and CompileForFAP
343 : // ignore errors and create RPN nevertheless
344 : bool glSubTotal; // if code contains one or more subtotal functions
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 0 : static inline void ForceArrayOperator( FormulaTokenRef& rCurr, const FormulaTokenRef& rPrev )
358 : {
359 0 : if ( rPrev && rPrev->HasForceArray() &&
360 0 : rCurr->GetType() == svByte && rCurr->GetOpCode() != ocPush
361 0 : && !rCurr->HasForceArray() )
362 0 : rCurr->SetForceArray( true);
363 0 : }
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 0 : explicit CurrentFactor( FormulaCompiler* pComp )
378 : : pPrevFac( pComp->pCurrentFactorToken )
379 0 : , pCompiler( pComp )
380 0 : {}
381 0 : ~CurrentFactor()
382 0 : { pCompiler->pCurrentFactorToken = pPrevFac; }
383 : // yes, this operator= may modify the RValue
384 0 : void operator=( FormulaTokenRef& r )
385 : {
386 0 : ForceArrayOperator( r, pPrevFac);
387 0 : pCompiler->pCurrentFactorToken = r;
388 0 : }
389 0 : void operator=( FormulaToken* p )
390 : {
391 0 : FormulaTokenRef xTemp( p );
392 0 : *this = xTemp;
393 0 : }
394 0 : operator FormulaTokenRef&()
395 0 : { return pCompiler->pCurrentFactorToken; }
396 0 : FormulaToken* operator->()
397 0 : { return pCompiler->pCurrentFactorToken.operator->(); }
398 0 : operator FormulaToken*()
399 0 : { 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: */
|