xref: /aoo41x/main/sc/inc/compiler.hxx (revision dffa72de)
138d50f7bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
338d50f7bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
438d50f7bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
538d50f7bSAndrew Rist  * distributed with this work for additional information
638d50f7bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
738d50f7bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
838d50f7bSAndrew Rist  * "License"); you may not use this file except in compliance
938d50f7bSAndrew Rist  * with the License.  You may obtain a copy of the License at
1038d50f7bSAndrew Rist  *
1138d50f7bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1238d50f7bSAndrew Rist  *
1338d50f7bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1438d50f7bSAndrew Rist  * software distributed under the License is distributed on an
1538d50f7bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1638d50f7bSAndrew Rist  * KIND, either express or implied.  See the License for the
1738d50f7bSAndrew Rist  * specific language governing permissions and limitations
1838d50f7bSAndrew Rist  * under the License.
1938d50f7bSAndrew Rist  *
2038d50f7bSAndrew Rist  *************************************************************/
2138d50f7bSAndrew Rist 
2238d50f7bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SC_COMPILER_HXX
25cdf0e10cSrcweir #define SC_COMPILER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef INCLUDED_STRING_H
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir #define INCLUDED_STRING_H
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir #include <tools/mempool.hxx>
32cdf0e10cSrcweir #include "scdllapi.h"
33cdf0e10cSrcweir #include "global.hxx"
34cdf0e10cSrcweir #include "refdata.hxx"
35cdf0e10cSrcweir #include "formula/token.hxx"
36cdf0e10cSrcweir #include "formula/intruref.hxx"
37cdf0e10cSrcweir #include "formula/grammar.hxx"
38cdf0e10cSrcweir #include <unotools/charclass.hxx>
39cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40cdf0e10cSrcweir #include <com/sun/star/sheet/ExternalLinkInfo.hpp>
41cdf0e10cSrcweir #include <vector>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <formula/FormulaCompiler.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
47cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #ifndef INCLUDED_HASH_MAP
51cdf0e10cSrcweir #include <hash_map>
52cdf0e10cSrcweir #define INCLUDED_HASH_MAP
53cdf0e10cSrcweir #endif
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //-----------------------------------------------
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // constants and data types also for external modules (ScInterpreter et al)
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #define MAXCODE      512    /* maximum number of tokens in formula */
60cdf0e10cSrcweir #define MAXSTRLEN    1024   /* maximum length of input string of one symbol */
61cdf0e10cSrcweir #define MAXJUMPCOUNT 32     /* maximum number of jumps (ocChose) */
62cdf0e10cSrcweir 
63cdf0e10cSrcweir // flag values of CharTable
64cdf0e10cSrcweir #define SC_COMPILER_C_ILLEGAL         0x00000000
65cdf0e10cSrcweir #define SC_COMPILER_C_CHAR            0x00000001
66cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_BOOL       0x00000002
67cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_WORD       0x00000004
68cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_VALUE      0x00000008
69cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_STRING     0x00000010
70cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_DONTCARE   0x00000020
71cdf0e10cSrcweir #define SC_COMPILER_C_BOOL            0x00000040
72cdf0e10cSrcweir #define SC_COMPILER_C_WORD            0x00000080
73cdf0e10cSrcweir #define SC_COMPILER_C_WORD_SEP        0x00000100
74cdf0e10cSrcweir #define SC_COMPILER_C_VALUE           0x00000200
75cdf0e10cSrcweir #define SC_COMPILER_C_VALUE_SEP       0x00000400
76cdf0e10cSrcweir #define SC_COMPILER_C_VALUE_EXP       0x00000800
77cdf0e10cSrcweir #define SC_COMPILER_C_VALUE_SIGN      0x00001000
78cdf0e10cSrcweir #define SC_COMPILER_C_VALUE_VALUE     0x00002000
79cdf0e10cSrcweir #define SC_COMPILER_C_STRING_SEP      0x00004000
80cdf0e10cSrcweir #define SC_COMPILER_C_NAME_SEP        0x00008000  // there can be only one! '\''
81cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_IDENT      0x00010000  // identifier (built-in function) or reference start
82cdf0e10cSrcweir #define SC_COMPILER_C_IDENT           0x00020000  // identifier or reference continuation
83cdf0e10cSrcweir #define SC_COMPILER_C_ODF_LBRACKET    0x00040000  // ODF '[' reference bracket
84cdf0e10cSrcweir #define SC_COMPILER_C_ODF_RBRACKET    0x00080000  // ODF ']' reference bracket
85cdf0e10cSrcweir #define SC_COMPILER_C_ODF_LABEL_OP    0x00100000  // ODF '!!' automatic intersection of labels
86cdf0e10cSrcweir #define SC_COMPILER_C_ODF_NAME_MARKER 0x00200000  // ODF '$$' marker that starts a defined (range) name
87cdf0e10cSrcweir #define SC_COMPILER_C_CHAR_NAME       0x00400000  // start character of a defined name
88cdf0e10cSrcweir #define SC_COMPILER_C_NAME            0x00800000  // continuation character of a defined name
89cdf0e10cSrcweir 
90cdf0e10cSrcweir #define SC_COMPILER_FILE_TAB_SEP      '#'         // 'Doc'#Tab
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
93cdf0e10cSrcweir class ScDocument;
94cdf0e10cSrcweir class ScMatrix;
95cdf0e10cSrcweir class ScRangeData;
96cdf0e10cSrcweir class ScExternalRefManager;
97cdf0e10cSrcweir class ScTokenArray;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir // constants and data types internal to compiler
100cdf0e10cSrcweir 
101cdf0e10cSrcweir #if 0
102cdf0e10cSrcweir /*
103cdf0e10cSrcweir     OpCode   eOp;           // OpCode
104cdf0e10cSrcweir     formula::StackVar eType;         // type of data
105cdf0e10cSrcweir     sal_uInt16   nRefCnt;       // reference count
106cdf0e10cSrcweir     sal_Bool     bRaw;          // not cloned yet and trimmed to real size
107cdf0e10cSrcweir  */
108cdf0e10cSrcweir #endif
109cdf0e10cSrcweir 
110cdf0e10cSrcweir #define SC_TOKEN_FIX_MEMBERS    \
111cdf0e10cSrcweir     OpCode   eOp;               \
112cdf0e10cSrcweir     formula::StackVar eType;    \
113cdf0e10cSrcweir     sal_uInt16   nRefCnt;           \
114cdf0e10cSrcweir     sal_Bool     bRaw;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir struct ScDoubleRawToken
117cdf0e10cSrcweir {
118cdf0e10cSrcweir private:
119cdf0e10cSrcweir     SC_TOKEN_FIX_MEMBERS
120cdf0e10cSrcweir public:
121cdf0e10cSrcweir     union
122cdf0e10cSrcweir     {   // union only to assure alignment identical to ScRawToken
123cdf0e10cSrcweir         double      nValue;
124cdf0e10cSrcweir         struct {
125cdf0e10cSrcweir             sal_uInt8        cByte;
126cdf0e10cSrcweir             bool        bHasForceArray;
127cdf0e10cSrcweir         } sbyte;
128cdf0e10cSrcweir     };
129cdf0e10cSrcweir                 DECL_FIXEDMEMPOOL_NEWDEL( ScDoubleRawToken );
130cdf0e10cSrcweir };
131cdf0e10cSrcweir 
132cdf0e10cSrcweir struct ScRawToken
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     friend class ScCompiler;
135cdf0e10cSrcweir     // Friends that use a temporary ScRawToken on the stack (and therefor need
136cdf0e10cSrcweir     // the private dtor) and know what they're doing..
137cdf0e10cSrcweir     friend class ScTokenArray;
138cdf0e10cSrcweir     friend sal_uInt16 lcl_ScRawTokenOffset();
139cdf0e10cSrcweir private:
140cdf0e10cSrcweir     SC_TOKEN_FIX_MEMBERS
141cdf0e10cSrcweir public:
142cdf0e10cSrcweir     union {
143cdf0e10cSrcweir         double       nValue;
144cdf0e10cSrcweir         struct {
145cdf0e10cSrcweir             sal_uInt8        cByte;
146cdf0e10cSrcweir             bool        bHasForceArray;
147cdf0e10cSrcweir         } sbyte;
148cdf0e10cSrcweir         ScComplexRefData aRef;
149cdf0e10cSrcweir         struct {
150cdf0e10cSrcweir             sal_uInt16      nFileId;
151cdf0e10cSrcweir             sal_Unicode     cTabName[MAXSTRLEN+1];
152cdf0e10cSrcweir             ScComplexRefData    aRef;
153cdf0e10cSrcweir         } extref;
154cdf0e10cSrcweir         struct {
155cdf0e10cSrcweir             sal_uInt16  nFileId;
156cdf0e10cSrcweir             sal_Unicode cName[MAXSTRLEN+1];
157cdf0e10cSrcweir         } extname;
158cdf0e10cSrcweir         ScMatrix*    pMat;
159cdf0e10cSrcweir         sal_uInt16       nIndex;                // index into name collection
160cdf0e10cSrcweir         sal_Unicode  cStr[ MAXSTRLEN+1 ];   // string (up to 255 characters + 0)
161cdf0e10cSrcweir         short        nJump[MAXJUMPCOUNT+1]; // If/Chose token
162cdf0e10cSrcweir     };
163cdf0e10cSrcweir 
164cdf0e10cSrcweir                 //! other members not initialized
ScRawTokenScRawToken165cdf0e10cSrcweir                 ScRawToken() : bRaw( sal_True ) {}
166cdf0e10cSrcweir private:
~ScRawTokenScRawToken167cdf0e10cSrcweir                 ~ScRawToken() {}                //! only delete via Delete()
168cdf0e10cSrcweir public:
169cdf0e10cSrcweir                 DECL_FIXEDMEMPOOL_NEWDEL( ScRawToken );
GetTypeScRawToken170cdf0e10cSrcweir     formula::StackVar    GetType()   const       { return (formula::StackVar) eType; }
GetOpCodeScRawToken171cdf0e10cSrcweir     OpCode      GetOpCode() const       { return (OpCode)   eOp;   }
NewOpCodeScRawToken172cdf0e10cSrcweir     void        NewOpCode( OpCode e )   { eOp = e; }
IncRefScRawToken173cdf0e10cSrcweir     void        IncRef()                { nRefCnt++;       }
DecRefScRawToken174cdf0e10cSrcweir     void        DecRef()                { if( !--nRefCnt ) Delete(); }
GetRefScRawToken175cdf0e10cSrcweir     sal_uInt16      GetRef() const          { return nRefCnt; }
176cdf0e10cSrcweir     SC_DLLPUBLIC void       Delete();
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     // Use these methods only on tokens that are not part of a token array,
179cdf0e10cSrcweir     // since the reference count is cleared!
180cdf0e10cSrcweir     void SetOpCode( OpCode eCode );
181cdf0e10cSrcweir     void SetString( const sal_Unicode* pStr );
182cdf0e10cSrcweir     void SetSingleReference( const ScSingleRefData& rRef );
183cdf0e10cSrcweir     void SetDoubleReference( const ScComplexRefData& rRef );
184cdf0e10cSrcweir     void SetDouble( double fVal );
185cdf0e10cSrcweir //UNUSED2008-05  void SetInt( int nVal );
186cdf0e10cSrcweir //UNUSED2008-05  void SetMatrix( ScMatrix* p );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     // These methods are ok to use, reference count not cleared.
189cdf0e10cSrcweir //UNUSED2008-05  ScComplexRefData& GetReference();
190cdf0e10cSrcweir //UNUSED2008-05  void SetReference( ScComplexRefData& rRef );
191cdf0e10cSrcweir     void SetName( sal_uInt16 n );
192cdf0e10cSrcweir     void SetExternalSingleRef( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& rRef );
193cdf0e10cSrcweir     void SetExternalDoubleRef( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& rRef );
194cdf0e10cSrcweir     void SetExternalName( sal_uInt16 nFileId, const String& rName );
195cdf0e10cSrcweir     void SetMatrix( ScMatrix* p );
196cdf0e10cSrcweir     void SetExternal(const sal_Unicode* pStr);
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     ScRawToken* Clone() const;      // real copy!
199cdf0e10cSrcweir     formula::FormulaToken* CreateToken() const;   // create typified token
200cdf0e10cSrcweir     void Load( SvStream&, sal_uInt16 nVer );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     static xub_StrLen GetStrLen( const sal_Unicode* pStr ); // as long as a "string" is an array
GetStrLenBytesScRawToken203cdf0e10cSrcweir     static size_t GetStrLenBytes( xub_StrLen nLen )
204cdf0e10cSrcweir         { return nLen * sizeof(sal_Unicode); }
GetStrLenBytesScRawToken205cdf0e10cSrcweir     static size_t GetStrLenBytes( const sal_Unicode* pStr )
206cdf0e10cSrcweir         { return GetStrLenBytes( GetStrLen( pStr ) ); }
207cdf0e10cSrcweir };
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 
210cdf0e10cSrcweir typedef formula::SimpleIntrusiveReference< struct ScRawToken > ScRawTokenRef;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir class SC_DLLPUBLIC ScCompiler : public formula::FormulaCompiler
213cdf0e10cSrcweir {
214cdf0e10cSrcweir public:
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     enum EncodeUrlMode
217cdf0e10cSrcweir     {
218cdf0e10cSrcweir         ENCODE_BY_GRAMMAR,
219cdf0e10cSrcweir         ENCODE_ALWAYS,
220cdf0e10cSrcweir         ENCODE_NEVER,
221cdf0e10cSrcweir     };
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     struct Convention
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         const formula::FormulaGrammar::AddressConvention meConv;
226cdf0e10cSrcweir         const sal_uLong*                mpCharTable;
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 
229cdf0e10cSrcweir         Convention( formula::FormulaGrammar::AddressConvention eConvP );
230cdf0e10cSrcweir         virtual ~Convention();
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         virtual void MakeRefStr( rtl::OUStringBuffer&   rBuffer,
233cdf0e10cSrcweir                                  const ScCompiler&      rCompiler,
234cdf0e10cSrcweir                                  const ScComplexRefData&    rRef,
235cdf0e10cSrcweir                                  sal_Bool bSingleRef ) const = 0;
236cdf0e10cSrcweir         virtual ::com::sun::star::i18n::ParseResult
237cdf0e10cSrcweir                     parseAnyToken( const String& rFormula,
238cdf0e10cSrcweir                                    xub_StrLen nSrcPos,
239cdf0e10cSrcweir                                    const CharClass* pCharClass) const = 0;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         /**
242cdf0e10cSrcweir          * Parse the symbol string and pick up the file name and the external
243cdf0e10cSrcweir          * range name.
244cdf0e10cSrcweir          *
245cdf0e10cSrcweir          * @return true on successful parse, or false otherwise.
246cdf0e10cSrcweir          */
247cdf0e10cSrcweir         virtual bool parseExternalName( const String& rSymbol, String& rFile, String& rName,
248cdf0e10cSrcweir                 const ScDocument* pDoc,
249cdf0e10cSrcweir                 const ::com::sun::star::uno::Sequence<
250cdf0e10cSrcweir                     const ::com::sun::star::sheet::ExternalLinkInfo > * pExternalLinks ) const = 0;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         virtual String makeExternalNameStr( const String& rFile, const String& rName ) const = 0;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         virtual void makeExternalRefStr( ::rtl::OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
255cdf0e10cSrcweir                                          sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& rRef,
256cdf0e10cSrcweir                                          ScExternalRefManager* pRefMgr ) const = 0;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         virtual void makeExternalRefStr( ::rtl::OUStringBuffer& rBuffer, const ScCompiler& rCompiler,
259cdf0e10cSrcweir                                          sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& rRef,
260cdf0e10cSrcweir                                          ScExternalRefManager* pRefMgr ) const = 0;
261cdf0e10cSrcweir 
262cdf0e10cSrcweir         enum SpecialSymbolType
263cdf0e10cSrcweir         {
264cdf0e10cSrcweir             /**
265cdf0e10cSrcweir              * Character between sheet name and address.  In OOO A1 this is
266cdf0e10cSrcweir              * '.', while XL A1 and XL R1C1 this is '!'.
267cdf0e10cSrcweir              */
268cdf0e10cSrcweir             SHEET_SEPARATOR,
269cdf0e10cSrcweir 
270cdf0e10cSrcweir             /**
271cdf0e10cSrcweir              * In OOO A1, a sheet name may be prefixed with '$' to indicate an
272cdf0e10cSrcweir              * absolute sheet position.
273cdf0e10cSrcweir              */
274cdf0e10cSrcweir             ABS_SHEET_PREFIX
275cdf0e10cSrcweir         };
276cdf0e10cSrcweir         virtual sal_Unicode getSpecialSymbol( SpecialSymbolType eSymType ) const = 0;
277cdf0e10cSrcweir     };
278cdf0e10cSrcweir     friend struct Convention;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir private:
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 
283cdf0e10cSrcweir     static CharClass            *pCharClassEnglish;                      // character classification for en_US locale
284cdf0e10cSrcweir     static const Convention     *pConventions[ formula::FormulaGrammar::CONV_LAST ];
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     static const Convention * const pConvOOO_A1;
287cdf0e10cSrcweir     static const Convention * const pConvOOO_A1_ODF;
288cdf0e10cSrcweir     static const Convention * const pConvXL_A1;
289cdf0e10cSrcweir     static const Convention * const pConvXL_R1C1;
290cdf0e10cSrcweir     static const Convention * const pConvXL_OOX;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir     static struct AddInMap
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         const char* pODFF;
295cdf0e10cSrcweir         const char* pEnglish;
296cdf0e10cSrcweir         bool        bMapDupToInternal;      // when writing ODFF
297cdf0e10cSrcweir         const char* pOriginal;              // programmatical name
298cdf0e10cSrcweir         const char* pUpper;                 // upper case programmatical name
299cdf0e10cSrcweir     } maAddInMap[];
300cdf0e10cSrcweir     static const AddInMap* GetAddInMap();
301cdf0e10cSrcweir     static size_t GetAddInMapCount();
302cdf0e10cSrcweir 
303cdf0e10cSrcweir     ScDocument* pDoc;
304cdf0e10cSrcweir     ScAddress   aPos;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir     // For CONV_XL_OOX, may be set via API by MOOXML filter.
307cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< const ::com::sun::star::sheet::ExternalLinkInfo > maExternalLinks;
308cdf0e10cSrcweir 
309cdf0e10cSrcweir     sal_Unicode cSymbol[MAXSTRLEN];                 // current Symbol
310cdf0e10cSrcweir     String      aFormula;                           // formula source code
311cdf0e10cSrcweir     xub_StrLen  nSrcPos;                            // tokenizer position (source code)
312cdf0e10cSrcweir     ScRawTokenRef   pRawToken;
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     const CharClass*    pCharClass;         // which character classification is used for parseAnyToken
315cdf0e10cSrcweir     sal_uInt16      mnPredetectedReference;     // reference when reading ODF, 0 (none), 1 (single) or 2 (double)
316cdf0e10cSrcweir     SCsTAB      nMaxTab;                    // last sheet in document
317cdf0e10cSrcweir     sal_Int32   mnRangeOpPosInSymbol;       // if and where a range operator is in symbol
318cdf0e10cSrcweir     const Convention *pConv;
319cdf0e10cSrcweir     EncodeUrlMode   meEncodeUrlMode;
320cdf0e10cSrcweir     bool        mbCloseBrackets;            // whether to close open brackets automatically, default TRUE
321cdf0e10cSrcweir     bool        mbExtendedErrorDetection;
322cdf0e10cSrcweir     bool        mbRewind;                   // whether symbol is to be rewound to some step during lexical analysis
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     sal_Bool   NextNewToken(bool bInArray = false);
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     virtual void SetError(sal_uInt16 nError);
327cdf0e10cSrcweir     xub_StrLen NextSymbol(bool bInArray);
328cdf0e10cSrcweir     sal_Bool IsValue( const String& );
329cdf0e10cSrcweir     sal_Bool IsOpCode( const String&, bool bInArray );
330cdf0e10cSrcweir     sal_Bool IsOpCode2( const String& );
331cdf0e10cSrcweir     sal_Bool IsString();
332cdf0e10cSrcweir     sal_Bool IsReference( const String& );
333cdf0e10cSrcweir     sal_Bool IsSingleReference( const String& );
334cdf0e10cSrcweir     sal_Bool IsPredetectedReference( const String& );
335cdf0e10cSrcweir     sal_Bool IsDoubleReference( const String& );
336cdf0e10cSrcweir     sal_Bool IsMacro( const String& );
337cdf0e10cSrcweir     sal_Bool IsNamedRange( const String& );
338cdf0e10cSrcweir     bool IsExternalNamedRange( const String& rSymbol );
339cdf0e10cSrcweir     sal_Bool IsDBRange( const String& );
340cdf0e10cSrcweir     sal_Bool IsColRowName( const String& );
341cdf0e10cSrcweir     sal_Bool IsBoolean( const String& );
342cdf0e10cSrcweir     void AutoCorrectParsedSymbol();
343cdf0e10cSrcweir 
344cdf0e10cSrcweir     void SetRelNameReference();
345cdf0e10cSrcweir 
346cdf0e10cSrcweir     static void InitCharClassEnglish();
347cdf0e10cSrcweir 
348cdf0e10cSrcweir public:
349cdf0e10cSrcweir     ScCompiler( ScDocument* pDocument, const ScAddress&);
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     ScCompiler( ScDocument* pDocument, const ScAddress&,ScTokenArray& rArr);
352cdf0e10cSrcweir 
353cdf0e10cSrcweir public:
354cdf0e10cSrcweir     static void DeInit();               /// all
355cdf0e10cSrcweir 
356cdf0e10cSrcweir     // for ScAddress::Format()
357cdf0e10cSrcweir     static void CheckTabQuotes( String& aTabName,
358cdf0e10cSrcweir                                 const formula::FormulaGrammar::AddressConvention eConv = formula::FormulaGrammar::CONV_OOO );
359cdf0e10cSrcweir 
360cdf0e10cSrcweir     /** Analyzes a string for a 'Doc'#Tab construct, or 'Do''c'#Tab etc..
361cdf0e10cSrcweir 
362cdf0e10cSrcweir         @returns the position of the unquoted # hash mark in 'Doc'#Tab, or
363cdf0e10cSrcweir                  STRING_NOTFOUND if none. */
364cdf0e10cSrcweir     static xub_StrLen GetDocTabPos( const String& rString );
365cdf0e10cSrcweir 
366cdf0e10cSrcweir     static sal_Bool EnQuote( String& rStr );
367cdf0e10cSrcweir     sal_Unicode GetNativeAddressSymbol( Convention::SpecialSymbolType eType ) const;
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 
370cdf0e10cSrcweir     // Check if it is a valid english function name
371cdf0e10cSrcweir     bool IsEnglishSymbol( const String& rName );
372cdf0e10cSrcweir 
373cdf0e10cSrcweir     //! _either_ CompileForFAP _or_ AutoCorrection, _not_ both
374cdf0e10cSrcweir     // #i101512# SetCompileForFAP is in formula::FormulaCompiler
SetAutoCorrection(sal_Bool bVal)375cdf0e10cSrcweir     void            SetAutoCorrection( sal_Bool bVal )
376cdf0e10cSrcweir                         { bAutoCorrect = bVal; bIgnoreErrors = bVal; }
SetCloseBrackets(bool bVal)377cdf0e10cSrcweir     void            SetCloseBrackets( bool bVal ) { mbCloseBrackets = bVal; }
378cdf0e10cSrcweir     void            SetRefConvention( const Convention *pConvP );
379cdf0e10cSrcweir     void            SetRefConvention( const formula::FormulaGrammar::AddressConvention eConv );
380cdf0e10cSrcweir 
381cdf0e10cSrcweir     /// Set symbol map if not empty.
382cdf0e10cSrcweir     void            SetFormulaLanguage( const OpCodeMapPtr & xMap );
383cdf0e10cSrcweir 
384cdf0e10cSrcweir     void            SetGrammar( const formula::FormulaGrammar::Grammar eGrammar );
385cdf0e10cSrcweir 
386cdf0e10cSrcweir     void            SetEncodeUrlMode( EncodeUrlMode eMode );
387cdf0e10cSrcweir     EncodeUrlMode   GetEncodeUrlMode() const;
388cdf0e10cSrcweir private:
389cdf0e10cSrcweir     /** Set grammar and reference convention from within SetFormulaLanguage()
390cdf0e10cSrcweir         or SetGrammar().
391cdf0e10cSrcweir 
392cdf0e10cSrcweir         @param eNewGrammar
393cdf0e10cSrcweir             The new grammar to be set and the associated reference convention.
394cdf0e10cSrcweir 
395cdf0e10cSrcweir         @param eOldGrammar
396cdf0e10cSrcweir             The previous grammar that was active before SetFormulaLanguage().
397cdf0e10cSrcweir      */
398cdf0e10cSrcweir     void            SetGrammarAndRefConvention(
399cdf0e10cSrcweir                         const formula::FormulaGrammar::Grammar eNewGrammar,
400cdf0e10cSrcweir                         const formula::FormulaGrammar::Grammar eOldGrammar );
401cdf0e10cSrcweir public:
402cdf0e10cSrcweir 
403cdf0e10cSrcweir     /// Set external link info for ScAddress::CONV_XL_OOX.
SetExternalLinks(const::com::sun::star::uno::Sequence<const::com::sun::star::sheet::ExternalLinkInfo> & rLinks)404cdf0e10cSrcweir     inline  void    SetExternalLinks(
405cdf0e10cSrcweir             const ::com::sun::star::uno::Sequence<
406cdf0e10cSrcweir             const ::com::sun::star::sheet::ExternalLinkInfo > & rLinks )
407cdf0e10cSrcweir     {
408cdf0e10cSrcweir         maExternalLinks = rLinks;
409cdf0e10cSrcweir     }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir     void            CreateStringFromXMLTokenArray( String& rFormula, String& rFormulaNmsp );
412cdf0e10cSrcweir 
SetExtendedErrorDetection(bool bVal)413cdf0e10cSrcweir     void            SetExtendedErrorDetection( bool bVal ) { mbExtendedErrorDetection = bVal; }
414cdf0e10cSrcweir 
IsCorrected()415cdf0e10cSrcweir     sal_Bool            IsCorrected() { return bCorrected; }
GetCorrectedFormula()416cdf0e10cSrcweir     const String&   GetCorrectedFormula() { return aCorrectedFormula; }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir     // Use convention from this->aPos by default
419cdf0e10cSrcweir     ScTokenArray* CompileString( const String& rFormula );
420cdf0e10cSrcweir     ScTokenArray* CompileString( const String& rFormula, const String& rFormulaNmsp );
GetDoc() const421cdf0e10cSrcweir     const ScDocument* GetDoc() const { return pDoc; }
GetPos() const422cdf0e10cSrcweir     const ScAddress& GetPos() const { return aPos; }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir     void MoveRelWrap( SCCOL nMaxCol, SCROW nMaxRow );
425cdf0e10cSrcweir     static void MoveRelWrap( ScTokenArray& rArr, ScDocument* pDoc, const ScAddress& rPos,
426cdf0e10cSrcweir                              SCCOL nMaxCol, SCROW nMaxRow );
427cdf0e10cSrcweir 
428cdf0e10cSrcweir     sal_Bool UpdateNameReference( UpdateRefMode eUpdateRefMode,
429cdf0e10cSrcweir                               const ScRange&,
430cdf0e10cSrcweir                               SCsCOL nDx, SCsROW nDy, SCsTAB nDz,
431cdf0e10cSrcweir                               sal_Bool& rChanged, sal_Bool bSharedFormula = sal_False);
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     ScRangeData* UpdateReference( UpdateRefMode eUpdateRefMode,
434cdf0e10cSrcweir                                   const ScAddress& rOldPos, const ScRange&,
435cdf0e10cSrcweir                                   SCsCOL nDx, SCsROW nDy, SCsTAB nDz,
436cdf0e10cSrcweir                                   sal_Bool& rChanged, sal_Bool& rRefSizeChanged );
437cdf0e10cSrcweir 
438cdf0e10cSrcweir     /// Only once for converted shared formulas,
439cdf0e10cSrcweir     /// token array has to be compiled afterwards.
440cdf0e10cSrcweir     void UpdateSharedFormulaReference( UpdateRefMode eUpdateRefMode,
441cdf0e10cSrcweir                                   const ScAddress& rOldPos, const ScRange&,
442cdf0e10cSrcweir                                   SCsCOL nDx, SCsROW nDy, SCsTAB nDz );
443cdf0e10cSrcweir 
444cdf0e10cSrcweir     ScRangeData* UpdateInsertTab(SCTAB nTable, sal_Bool bIsName );
445cdf0e10cSrcweir     ScRangeData* UpdateDeleteTab(SCTAB nTable, sal_Bool bIsMove, sal_Bool bIsName, sal_Bool& bCompile);
446*dffa72deSWang Lei     // the last parameter is true only when copying a sheet, to update the range name's address
447*dffa72deSWang Lei     ScRangeData* UpdateMoveTab(SCTAB nOldPos, SCTAB nNewPos, bool bIsName, bool bOnlyUpdateOwnTab = false );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir     sal_Bool HasModifiedRange();
450cdf0e10cSrcweir 
451cdf0e10cSrcweir     /** If the character is allowed as first character in sheet names or
452cdf0e10cSrcweir         references, includes '$' and '?'. */
IsCharWordChar(String const & rStr,xub_StrLen nPos,const formula::FormulaGrammar::AddressConvention eConv=formula::FormulaGrammar::CONV_OOO)453cdf0e10cSrcweir     static inline sal_Bool IsCharWordChar( String const & rStr,
454cdf0e10cSrcweir                                        xub_StrLen nPos,
455cdf0e10cSrcweir                                        const formula::FormulaGrammar::AddressConvention eConv = formula::FormulaGrammar::CONV_OOO )
456cdf0e10cSrcweir         {
457cdf0e10cSrcweir             sal_Unicode c = rStr.GetChar( nPos );
458cdf0e10cSrcweir             if (c < 128)
459cdf0e10cSrcweir             {
460cdf0e10cSrcweir                 return pConventions[eConv] ? static_cast<sal_Bool>(
461cdf0e10cSrcweir                         (pConventions[eConv]->mpCharTable[ sal_uInt8(c) ] & SC_COMPILER_C_CHAR_WORD) == SC_COMPILER_C_CHAR_WORD) :
462cdf0e10cSrcweir                     sal_False;   // no convention => assume invalid
463cdf0e10cSrcweir             }
464cdf0e10cSrcweir             else
465cdf0e10cSrcweir                 return ScGlobal::pCharClass->isLetterNumeric( rStr, nPos );
466cdf0e10cSrcweir         }
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     /** If the character is allowed in sheet names, thus may be part of a
469cdf0e10cSrcweir         reference, includes '$' and '?' and such. */
IsWordChar(String const & rStr,xub_StrLen nPos,const formula::FormulaGrammar::AddressConvention eConv=formula::FormulaGrammar::CONV_OOO)470cdf0e10cSrcweir     static inline sal_Bool IsWordChar( String const & rStr,
471cdf0e10cSrcweir                                    xub_StrLen nPos,
472cdf0e10cSrcweir                                    const formula::FormulaGrammar::AddressConvention eConv = formula::FormulaGrammar::CONV_OOO )
473cdf0e10cSrcweir         {
474cdf0e10cSrcweir             sal_Unicode c = rStr.GetChar( nPos );
475cdf0e10cSrcweir             if (c < 128)
476cdf0e10cSrcweir             {
477cdf0e10cSrcweir                 return pConventions[eConv] ? static_cast<sal_Bool>(
478cdf0e10cSrcweir                         (pConventions[eConv]->mpCharTable[ sal_uInt8(c) ] & SC_COMPILER_C_WORD) == SC_COMPILER_C_WORD) :
479cdf0e10cSrcweir                     sal_False;   // convention not known => assume invalid
480cdf0e10cSrcweir             }
481cdf0e10cSrcweir             else
482cdf0e10cSrcweir                 return ScGlobal::pCharClass->isLetterNumeric( rStr, nPos );
483cdf0e10cSrcweir         }
484cdf0e10cSrcweir 
485cdf0e10cSrcweir     /** If the character is allowed as tested by nFlags (SC_COMPILER_C_...
486cdf0e10cSrcweir         bits) for all known address conventions. If more than one bit is given
487cdf0e10cSrcweir         in nFlags, all bits must match. If bTestLetterNumeric is sal_False and
488cdf0e10cSrcweir         char>=128, no LetterNumeric test is done and sal_False is returned. */
IsCharFlagAllConventions(String const & rStr,xub_StrLen nPos,sal_uLong nFlags,bool bTestLetterNumeric=true)489cdf0e10cSrcweir     static inline bool IsCharFlagAllConventions( String const & rStr,
490cdf0e10cSrcweir                                                  xub_StrLen nPos,
491cdf0e10cSrcweir                                                  sal_uLong nFlags,
492cdf0e10cSrcweir                                                  bool bTestLetterNumeric = true )
493cdf0e10cSrcweir         {
494cdf0e10cSrcweir             sal_Unicode c = rStr.GetChar( nPos );
495cdf0e10cSrcweir             if (c < 128)
496cdf0e10cSrcweir             {
497cdf0e10cSrcweir                 for ( int nConv = formula::FormulaGrammar::CONV_UNSPECIFIED;
498cdf0e10cSrcweir                         ++nConv < formula::FormulaGrammar::CONV_LAST; )
499cdf0e10cSrcweir                 {
500cdf0e10cSrcweir                     if (pConventions[nConv] &&
501cdf0e10cSrcweir                             ((pConventions[nConv]->mpCharTable[ sal_uInt8(c) ] & nFlags) != nFlags))
502cdf0e10cSrcweir                         return false;
503cdf0e10cSrcweir                     // convention not known => assume valid
504cdf0e10cSrcweir                 }
505cdf0e10cSrcweir                 return true;
506cdf0e10cSrcweir             }
507cdf0e10cSrcweir             else if (bTestLetterNumeric)
508cdf0e10cSrcweir                 return ScGlobal::pCharClass->isLetterNumeric( rStr, nPos );
509cdf0e10cSrcweir             else
510cdf0e10cSrcweir                 return false;
511cdf0e10cSrcweir         }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir private:
514cdf0e10cSrcweir     // FormulaCompiler
515cdf0e10cSrcweir     virtual String FindAddInFunction( const String& rUpperName, sal_Bool bLocalFirst ) const;
516cdf0e10cSrcweir     virtual void fillFromAddInCollectionUpperName( NonConstOpCodeMapPtr xMap ) const;
517cdf0e10cSrcweir     virtual void fillFromAddInCollectionEnglishName( NonConstOpCodeMapPtr xMap ) const;
518cdf0e10cSrcweir     virtual void fillFromAddInMap( NonConstOpCodeMapPtr xMap, formula::FormulaGrammar::Grammar _eGrammar ) const;
519cdf0e10cSrcweir     virtual void fillAddInToken(::std::vector< ::com::sun::star::sheet::FormulaOpCodeMapEntry >& _rVec,bool _bIsEnglish) const;
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     virtual sal_Bool HandleExternalReference(const formula::FormulaToken& _aToken);
522cdf0e10cSrcweir     virtual sal_Bool HandleRange();
523cdf0e10cSrcweir     virtual sal_Bool HandleSingleRef();
524cdf0e10cSrcweir     virtual sal_Bool HandleDbData();
525cdf0e10cSrcweir 
526cdf0e10cSrcweir     virtual formula::FormulaTokenRef ExtendRangeReference( formula::FormulaToken & rTok1, formula::FormulaToken & rTok2, bool bReuseDoubleRef );
527cdf0e10cSrcweir 	virtual void CreateStringFromExternal(rtl::OUStringBuffer& rBuffer, formula::FormulaToken* pTokenP);
528cdf0e10cSrcweir     virtual void CreateStringFromSingleRef(rtl::OUStringBuffer& rBuffer,formula::FormulaToken* _pTokenP);
529cdf0e10cSrcweir     virtual void CreateStringFromDoubleRef(rtl::OUStringBuffer& rBuffer,formula::FormulaToken* _pTokenP);
530cdf0e10cSrcweir     virtual void CreateStringFromMatrix( rtl::OUStringBuffer& rBuffer, formula::FormulaToken* _pTokenP);
531cdf0e10cSrcweir     virtual void CreateStringFromIndex(rtl::OUStringBuffer& rBuffer,formula::FormulaToken* _pTokenP);
532cdf0e10cSrcweir     virtual void LocalizeString( String& rName );	// modify rName - input: exact name
533cdf0e10cSrcweir     virtual sal_Bool IsImportingXML() const;
534cdf0e10cSrcweir 
535cdf0e10cSrcweir     /// Access the CharTable flags
GetCharTableFlags(sal_Unicode c)536cdf0e10cSrcweir     inline sal_uLong GetCharTableFlags( sal_Unicode c )
537cdf0e10cSrcweir         { return c < 128 ? pConv->mpCharTable[ sal_uInt8(c) ] : 0; }
538cdf0e10cSrcweir };
539cdf0e10cSrcweir 
540cdf0e10cSrcweir SC_DLLPUBLIC String GetScCompilerNativeSymbol( OpCode eOp ); //CHINA001
541cdf0e10cSrcweir 
542cdf0e10cSrcweir #endif
543