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_TOKENARRAY_HXX
21 : #define INCLUDED_FORMULA_TOKENARRAY_HXX
22 :
23 : #include <com/sun/star/sheet/FormulaToken.hpp>
24 : #include <formula/token.hxx>
25 : #include <formula/ExternalReferenceHelper.hxx>
26 : #include <limits.h>
27 :
28 : #include <boost/unordered_set.hpp>
29 :
30 : namespace formula
31 : {
32 :
33 : // RecalcMode access only via TokenArray SetRecalcMode / IsRecalcMode...
34 :
35 : typedef sal_uInt8 ScRecalcMode;
36 : // Only one of the exclusive bits can be set,
37 : // handled by TokenArray SetRecalcMode... methods
38 : #define RECALCMODE_NORMAL 0x01 // exclusive
39 : #define RECALCMODE_ALWAYS 0x02 // exclusive, always
40 : #define RECALCMODE_ONLOAD 0x04 // exclusive, always after load
41 : #define RECALCMODE_ONLOAD_ONCE 0x08 // exclusive, once after load
42 : #define RECALCMODE_FORCED 0x10 // combined, also if cell isn't visible
43 : #define RECALCMODE_ONREFMOVE 0x20 // combined, if reference was moved
44 : #define RECALCMODE_EMASK 0x0F // mask of exclusive bits
45 : // If new bits are to be defined, AddRecalcMode has to be adjusted!
46 :
47 : class FormulaMissingContext;
48 :
49 : class FORMULA_DLLPUBLIC MissingConvention
50 : {
51 : bool mbODFF; /// TRUE: ODFF, FALSE: PODF
52 : public:
53 0 : explicit MissingConvention( bool bODFF ) : mbODFF(bODFF) {}
54 : // Implementation and usage only in token.cxx
55 : inline bool isRewriteNeeded( OpCode eOp ) const;
56 0 : inline bool isODFF() const { return mbODFF; }
57 : };
58 :
59 : class FORMULA_DLLPUBLIC FormulaTokenArray
60 : {
61 : friend class FormulaCompiler;
62 : friend class FormulaTokenIterator;
63 : friend class FormulaMissingContext;
64 :
65 : protected:
66 : FormulaToken** pCode; // Token code array
67 : FormulaToken** pRPN; // RPN array
68 : sal_uInt16 nLen; // Length of token array
69 : sal_uInt16 nRPN; // Length of RPN array
70 : sal_uInt16 nIndex; // Current step index
71 : sal_uInt16 nError; // Error code
72 : short nRefs; // Count of cell references
73 : ScRecalcMode nMode; // Flags to indicate when to recalc this code
74 : bool bHyperLink; // If HYPERLINK() occurs in the formula.
75 :
76 : protected:
77 : void Assign( const FormulaTokenArray& );
78 :
79 : /// Also used by the compiler. The token MUST had been allocated with new!
80 : FormulaToken* Add( FormulaToken* );
81 0 : inline void SetCombinedBitsRecalcMode( ScRecalcMode nBits )
82 0 : { nMode |= (nBits & ~RECALCMODE_EMASK); }
83 0 : inline ScRecalcMode GetCombinedBitsRecalcMode() const
84 0 : { return nMode & ~RECALCMODE_EMASK; }
85 : /** Exclusive bits already set in nMode are
86 : zero'ed, nBits may contain combined bits, but
87 : only one exclusive bit may be set! */
88 0 : inline void SetMaskedRecalcMode( ScRecalcMode nBits )
89 0 : { nMode = GetCombinedBitsRecalcMode() | nBits; }
90 :
91 : public:
92 : FormulaTokenArray();
93 : /// Assignment with references to FormulaToken entries (not copied!)
94 : FormulaTokenArray( const FormulaTokenArray& );
95 : virtual ~FormulaTokenArray();
96 : FormulaTokenArray* Clone() const; /// True copy!
97 :
98 : void Clear();
99 : void DelRPN();
100 0 : FormulaToken* First() { nIndex = 0; return Next(); }
101 : FormulaToken* Next();
102 : FormulaToken* FirstNoSpaces() { nIndex = 0; return NextNoSpaces(); }
103 : FormulaToken* NextNoSpaces();
104 : FormulaToken* GetNextName();
105 : FormulaToken* GetNextReference();
106 : FormulaToken* GetNextReferenceRPN();
107 : FormulaToken* GetNextReferenceOrName();
108 : FormulaToken* GetNextColRowName();
109 : FormulaToken* GetNextOpCodeRPN( OpCode );
110 : /// Peek at nIdx-1 if not out of bounds, decrements nIdx if successful. Returns NULL if not.
111 : FormulaToken* PeekPrev( sal_uInt16 & nIdx );
112 : FormulaToken* PeekNext();
113 : FormulaToken* PeekPrevNoSpaces(); /// Only after Reset/First/Next/Last/Prev!
114 : FormulaToken* PeekNextNoSpaces(); /// Only after Reset/First/Next/Last/Prev!
115 0 : FormulaToken* FirstRPN() { nIndex = 0; return NextRPN(); }
116 : FormulaToken* NextRPN();
117 0 : FormulaToken* LastRPN() { nIndex = nRPN; return PrevRPN(); }
118 : FormulaToken* PrevRPN();
119 :
120 : bool HasReferences() const;
121 :
122 : bool HasExternalRef() const;
123 : bool HasOpCode( OpCode ) const;
124 : bool HasOpCodeRPN( OpCode ) const;
125 : /// Token of type svIndex or opcode ocColRowName
126 : bool HasNameOrColRowName() const;
127 :
128 : /**
129 : * Check if the token array contains any of specified opcode tokens.
130 : *
131 : * @param rOpCodes collection of opcodes to check against.
132 : *
133 : * @return true if the token array contains at least one of the specified
134 : * opcode tokens, false otherwise.
135 : */
136 : bool HasOpCodes( const boost::unordered_set<OpCode>& rOpCodes ) const;
137 :
138 0 : FormulaToken** GetArray() const { return pCode; }
139 0 : FormulaToken** GetCode() const { return pRPN; }
140 0 : sal_uInt16 GetLen() const { return nLen; }
141 0 : sal_uInt16 GetCodeLen() const { return nRPN; }
142 0 : void Reset() { nIndex = 0; }
143 0 : sal_uInt16 GetCodeError() const { return nError; }
144 0 : void SetCodeError( sal_uInt16 n ) { nError = n; }
145 : short GetRefs() const { return nRefs; }
146 0 : void IncrementRefs() { ++nRefs; }
147 0 : void SetHyperLink( bool bVal ) { bHyperLink = bVal; }
148 0 : bool IsHyperLink() const { return bHyperLink; }
149 :
150 0 : inline ScRecalcMode GetRecalcMode() const { return nMode; }
151 : /** Bits aren't set directly but validated and
152 : maybe handled according to priority if more
153 : than one exclusive bit was set. */
154 : void AddRecalcMode( ScRecalcMode nBits );
155 :
156 0 : inline void ClearRecalcMode() { nMode = RECALCMODE_NORMAL; }
157 0 : inline void SetExclusiveRecalcModeNormal()
158 0 : { SetMaskedRecalcMode( RECALCMODE_NORMAL ); }
159 0 : inline void SetExclusiveRecalcModeAlways()
160 0 : { SetMaskedRecalcMode( RECALCMODE_ALWAYS ); }
161 0 : inline void SetExclusiveRecalcModeOnLoad()
162 0 : { SetMaskedRecalcMode( RECALCMODE_ONLOAD ); }
163 0 : inline void SetExclusiveRecalcModeOnLoadOnce()
164 0 : { SetMaskedRecalcMode( RECALCMODE_ONLOAD_ONCE ); }
165 0 : inline void SetRecalcModeForced()
166 0 : { nMode |= RECALCMODE_FORCED; }
167 : inline void ClearRecalcModeForced()
168 : { nMode &= ~RECALCMODE_FORCED; }
169 0 : inline void SetRecalcModeOnRefMove()
170 0 : { nMode |= RECALCMODE_ONREFMOVE; }
171 : inline void ClearRecalcModeOnRefMove()
172 : { nMode &= ~RECALCMODE_ONREFMOVE; }
173 0 : inline bool IsRecalcModeNormal() const
174 0 : { return (nMode & RECALCMODE_NORMAL) != 0; }
175 0 : inline bool IsRecalcModeAlways() const
176 0 : { return (nMode & RECALCMODE_ALWAYS) != 0; }
177 0 : inline bool IsRecalcModeOnLoad() const
178 0 : { return (nMode & RECALCMODE_ONLOAD) != 0; }
179 0 : inline bool IsRecalcModeOnLoadOnce() const
180 0 : { return (nMode & RECALCMODE_ONLOAD_ONCE) != 0; }
181 0 : inline bool IsRecalcModeForced() const
182 0 : { return (nMode & RECALCMODE_FORCED) != 0; }
183 0 : inline bool IsRecalcModeOnRefMove() const
184 0 : { return (nMode & RECALCMODE_ONREFMOVE) != 0; }
185 :
186 : /** Get OpCode of the most outer function */
187 : inline OpCode GetOuterFuncOpCode();
188 :
189 : /** Operators +,-,*,/,^,&,=,<>,<,>,<=,>=
190 : with DoubleRef in Formula? */
191 : bool HasMatrixDoubleRefOps();
192 :
193 : virtual FormulaToken* AddOpCode(OpCode e);
194 :
195 : /** Adds the single token to array.
196 : Derived classes must overload it when they want to support derived classes from FormulaToken.
197 : @return true when an error occurs
198 : */
199 : virtual bool AddFormulaToken(const com::sun::star::sheet::FormulaToken& _aToken, ExternalReferenceHelper* _pRef = NULL);
200 :
201 : /** fill the array with the tokens from the sequence.
202 : It calls AddFormulaToken for each token in the list.
203 : @param _aSequence the token to add
204 : @return true when an error occurs
205 : */
206 : bool Fill(const com::sun::star::uno::Sequence< com::sun::star::sheet::FormulaToken >& _aSequence, ExternalReferenceHelper* _pRef = NULL);
207 :
208 : /**
209 : * Do some checking based on the individual tokens. For now, we use this
210 : * only to check whether we can vectorize the token array.
211 : */
212 : virtual void CheckToken( const FormulaToken& t );
213 :
214 : FormulaToken* AddToken( const FormulaToken& );
215 : FormulaToken* AddString( const OUString& rStr );
216 : FormulaToken* AddDouble( double fVal );
217 : FormulaToken* AddExternal( const sal_Unicode* pStr );
218 : /** Xcl import may play dirty tricks with OpCode!=ocExternal.
219 : Others don't use! */
220 : FormulaToken* AddExternal( const OUString& rStr, OpCode eOp = ocExternal );
221 : FormulaToken* AddBad( const OUString& rStr ); /// ocBad with OUString
222 : FormulaToken* AddStringXML( const OUString& rStr ); /// ocStringXML with OUString, temporary during import
223 :
224 : virtual FormulaToken* MergeArray( );
225 :
226 : /// Assignment with references to FormulaToken entries (not copied!)
227 : FormulaTokenArray& operator=( const FormulaTokenArray& );
228 :
229 : /** Determines if this formula needs any changes to convert it to something
230 : previous versions of OOo could consume (Plain Old Formula). */
231 : bool NeedsPofRewrite(const MissingConvention & rConv);
232 :
233 : /** Rewrites to Plain Old Formula, substituting missing parameters. The
234 : FormulaTokenArray* returned is new'ed. */
235 : FormulaTokenArray* RewriteMissingToPof(const MissingConvention & rConv);
236 :
237 : /** Determines if this formula may be followed by a reference. */
238 : bool MayReferenceFollow();
239 : };
240 :
241 0 : inline OpCode FormulaTokenArray::GetOuterFuncOpCode()
242 : {
243 0 : if ( pRPN && nRPN )
244 0 : return pRPN[nRPN-1]->GetOpCode();
245 0 : return ocNone;
246 : }
247 :
248 : struct ImpTokenIterator
249 : {
250 : ImpTokenIterator* pNext;
251 : const FormulaTokenArray* pArr;
252 : short nPC;
253 : short nStop;
254 :
255 0 : DECL_FIXEDMEMPOOL_NEWDEL( ImpTokenIterator );
256 : };
257 :
258 : class FORMULA_DLLPUBLIC FormulaTokenIterator
259 : {
260 : ImpTokenIterator* pCur;
261 :
262 : public:
263 : FormulaTokenIterator( const FormulaTokenArray& );
264 : ~FormulaTokenIterator();
265 : void Reset();
266 : const FormulaToken* Next();
267 : const FormulaToken* PeekNextOperator();
268 : bool IsEndOfPath() const; /// if a jump or subroutine path is done
269 0 : bool HasStacked() const { return pCur->pNext != 0; }
270 0 : short GetPC() const { return pCur->nPC; }
271 :
272 : /** Jump or subroutine call.
273 : Program counter values will be incremented before code is executed =>
274 : positions are to be passed with -1 offset.
275 : @param nStart
276 : Start on code at position nStart+1 (yes, pass with offset -1)
277 : @param nNext
278 : After subroutine continue with instruction at position nNext+1
279 : @param nStop
280 : Stop before reaching code at position nStop. If not specified the
281 : default is to either run the entire code, or to stop if an ocSep or
282 : ocClose is encountered, which are only present in ocIf or ocChose
283 : jumps.
284 : */
285 : void Jump( short nStart, short nNext, short nStop = SHRT_MAX );
286 : void Push( const FormulaTokenArray* );
287 : void Pop();
288 :
289 : private:
290 : const FormulaToken* GetNonEndOfPathToken( short nIdx ) const;
291 : };
292 : } // formula
293 :
294 : #endif // INCLUDED_FORMULA_TOKENARRAY_HXX
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|