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_SC_INC_COMPILER_HXX
21 : #define INCLUDED_SC_INC_COMPILER_HXX
22 :
23 : #include <string.h>
24 :
25 : #include <tools/mempool.hxx>
26 : #include "scdllapi.h"
27 : #include "global.hxx"
28 : #include "refdata.hxx"
29 : #include "token.hxx"
30 : #include <formula/token.hxx>
31 : #include <formula/grammar.hxx>
32 : #include <unotools/charclass.hxx>
33 : #include <rtl/ustrbuf.hxx>
34 : #include <com/sun/star/sheet/ExternalLinkInfo.hpp>
35 : #include <vector>
36 :
37 : #include <formula/FormulaCompiler.hxx>
38 :
39 : // constants and data types also for external modules (ScInterpreter et al)
40 :
41 : #define MAXSTRLEN 1024 /* maximum length of input string of one symbol */
42 :
43 : // flag values of CharTable
44 : #define SC_COMPILER_C_ILLEGAL 0x00000000
45 : #define SC_COMPILER_C_CHAR 0x00000001
46 : #define SC_COMPILER_C_CHAR_BOOL 0x00000002
47 : #define SC_COMPILER_C_CHAR_WORD 0x00000004
48 : #define SC_COMPILER_C_CHAR_VALUE 0x00000008
49 : #define SC_COMPILER_C_CHAR_STRING 0x00000010
50 : #define SC_COMPILER_C_CHAR_DONTCARE 0x00000020
51 : #define SC_COMPILER_C_BOOL 0x00000040
52 : #define SC_COMPILER_C_WORD 0x00000080
53 : #define SC_COMPILER_C_WORD_SEP 0x00000100
54 : #define SC_COMPILER_C_VALUE 0x00000200
55 : #define SC_COMPILER_C_VALUE_SEP 0x00000400
56 : #define SC_COMPILER_C_VALUE_EXP 0x00000800
57 : #define SC_COMPILER_C_VALUE_SIGN 0x00001000
58 : #define SC_COMPILER_C_VALUE_VALUE 0x00002000
59 : #define SC_COMPILER_C_STRING_SEP 0x00004000
60 : #define SC_COMPILER_C_NAME_SEP 0x00008000 // there can be only one! '\''
61 : #define SC_COMPILER_C_CHAR_IDENT 0x00010000 // identifier (built-in function) or reference start
62 : #define SC_COMPILER_C_IDENT 0x00020000 // identifier or reference continuation
63 : #define SC_COMPILER_C_ODF_LBRACKET 0x00040000 // ODF '[' reference bracket
64 : #define SC_COMPILER_C_ODF_RBRACKET 0x00080000 // ODF ']' reference bracket
65 : #define SC_COMPILER_C_ODF_LABEL_OP 0x00100000 // ODF '!!' automatic intersection of labels
66 : #define SC_COMPILER_C_ODF_NAME_MARKER 0x00200000 // ODF '$$' marker that starts a defined (range) name
67 : #define SC_COMPILER_C_CHAR_NAME 0x00400000 // start character of a defined name
68 : #define SC_COMPILER_C_NAME 0x00800000 // continuation character of a defined name
69 : #define SC_COMPILER_C_CHAR_ERRCONST 0x01000000 // start character of an error constant ('#')
70 :
71 : #define SC_COMPILER_FILE_TAB_SEP '#' // 'Doc'#Tab
72 :
73 : class ScDocument;
74 : class ScMatrix;
75 : class ScRangeData;
76 : class ScExternalRefManager;
77 : class ScTokenArray;
78 :
79 : namespace sc {
80 :
81 : class CompileFormulaContext;
82 :
83 : }
84 :
85 : // constants and data types internal to compiler
86 :
87 : /*
88 : OpCode eOp; // OpCode
89 : formula::StackVar eType; // type of data
90 : */
91 :
92 66070 : struct ScRawTokenBase
93 : {
94 : protected:
95 : OpCode eOp;
96 : formula::StackVar eType;
97 : };
98 :
99 : struct ScDoubleRawToken: private ScRawTokenBase
100 : {
101 : public:
102 : union
103 : { // union only to assure alignment identical to ScRawToken
104 : double nValue;
105 : struct {
106 : sal_uInt8 cByte;
107 : bool bHasForceArray;
108 : } sbyte;
109 : };
110 : DECL_FIXEDMEMPOOL_NEWDEL( ScDoubleRawToken );
111 : };
112 :
113 : struct ScRawToken: private ScRawTokenBase
114 : {
115 : friend class ScCompiler;
116 : // Friends that use a temporary ScRawToken on the stack (and therefore need
117 : // the private dtor) and know what they're doing..
118 : friend class ScTokenArray;
119 : public:
120 : union {
121 : double nValue;
122 : struct {
123 : sal_uInt8 cByte;
124 : bool bHasForceArray;
125 : } sbyte;
126 : ScComplexRefData aRef;
127 : struct {
128 : sal_uInt16 nFileId;
129 : sal_Unicode cTabName[MAXSTRLEN+1];
130 : ScComplexRefData aRef;
131 : } extref;
132 : struct {
133 : sal_uInt16 nFileId;
134 : sal_Unicode cName[MAXSTRLEN+1];
135 : } extname;
136 : struct {
137 : bool bGlobal;
138 : sal_uInt16 nIndex;
139 : } name;
140 : struct {
141 : sal_uInt16 nIndex;
142 : ScTableRefToken::Item eItem;
143 : } table;
144 : struct {
145 : rtl_uString* mpData;
146 : rtl_uString* mpDataIgnoreCase;
147 : } sharedstring;
148 : ScMatrix* pMat;
149 : sal_uInt16 nError;
150 : sal_Unicode cStr[ MAXSTRLEN+1 ]; // string (up to 255 characters + 0)
151 : short nJump[ FORMULA_MAXJUMPCOUNT + 1 ]; // If/Chose token
152 : };
153 :
154 : //! members not initialized
155 66070 : ScRawToken() {}
156 : private:
157 66070 : ~ScRawToken() {} //! only delete via Delete()
158 : public:
159 : DECL_FIXEDMEMPOOL_NEWDEL( ScRawToken );
160 45197 : formula::StackVar GetType() const { return eType; }
161 60677 : OpCode GetOpCode() const { return eOp; }
162 169 : void NewOpCode( OpCode e ) { eOp = e; }
163 :
164 : // Use these methods only on tokens that are not part of a token array,
165 : // since the reference count is cleared!
166 : void SetOpCode( OpCode eCode );
167 : void SetString( rtl_uString* pData, rtl_uString* pDataIgoreCase );
168 : void SetSingleReference( const ScSingleRefData& rRef );
169 : void SetDoubleReference( const ScComplexRefData& rRef );
170 : void SetDouble( double fVal );
171 : void SetErrorConstant( sal_uInt16 nErr );
172 :
173 : // These methods are ok to use, reference count not cleared.
174 : void SetName(bool bGlobal, sal_uInt16 nIndex);
175 : void SetExternalSingleRef( sal_uInt16 nFileId, const OUString& rTabName, const ScSingleRefData& rRef );
176 : void SetExternalDoubleRef( sal_uInt16 nFileId, const OUString& rTabName, const ScComplexRefData& rRef );
177 : void SetExternalName( sal_uInt16 nFileId, const OUString& rName );
178 : void SetMatrix( ScMatrix* p );
179 : void SetExternal(const sal_Unicode* pStr);
180 :
181 : /** If the token is a non-external reference, determine if the reference is
182 : valid. If the token is an external reference, return true. Else return
183 : false. Used only in ScCompiler::NextNewToken() to preserve non-existing
184 : sheet names in otherwise valid references.
185 : */
186 : bool IsValidReference() const;
187 :
188 : formula::FormulaToken* CreateToken() const; // create typified token
189 : void Load( SvStream&, sal_uInt16 nVer );
190 :
191 : static sal_Int32 GetStrLen( const sal_Unicode* pStr ); // as long as a "string" is an array
192 101 : static size_t GetStrLenBytes( sal_Int32 nLen )
193 101 : { return nLen * sizeof(sal_Unicode); }
194 : static size_t GetStrLenBytes( const sal_Unicode* pStr )
195 : { return GetStrLenBytes( GetStrLen( pStr ) ); }
196 : };
197 :
198 : class SC_DLLPUBLIC ScCompiler : public formula::FormulaCompiler
199 : {
200 : public:
201 :
202 : enum ExtendedErrorDetection
203 : {
204 : EXTENDED_ERROR_DETECTION_NONE = 0, // no error on unknown symbols, default (interpreter handles it)
205 : EXTENDED_ERROR_DETECTION_NAME_BREAK, // name error on unknown symbols and break, pCode incomplete
206 : EXTENDED_ERROR_DETECTION_NAME_NO_BREAK // name error on unknown symbols, don't break, continue
207 : };
208 :
209 : struct Convention
210 : {
211 : const formula::FormulaGrammar::AddressConvention meConv;
212 :
213 : Convention( formula::FormulaGrammar::AddressConvention eConvP );
214 : virtual ~Convention();
215 :
216 : virtual void makeRefStr(
217 : OUStringBuffer& rBuffer,
218 : formula::FormulaGrammar::Grammar eGram,
219 : const ScAddress& rPos,
220 : const OUString& rErrRef, const std::vector<OUString>& rTabNames,
221 : const ScComplexRefData& rRef, bool bSingleRef ) const = 0;
222 :
223 : virtual ::com::sun::star::i18n::ParseResult
224 : parseAnyToken( const OUString& rFormula,
225 : sal_Int32 nSrcPos,
226 : const CharClass* pCharClass) const = 0;
227 :
228 : /**
229 : * Parse the symbol string and pick up the file name and the external
230 : * range name.
231 : *
232 : * @return true on successful parse, or false otherwise.
233 : */
234 : virtual bool parseExternalName( const OUString& rSymbol, OUString& rFile, OUString& rName,
235 : const ScDocument* pDoc,
236 : const ::com::sun::star::uno::Sequence<
237 : com::sun::star::sheet::ExternalLinkInfo>* pExternalLinks ) const = 0;
238 :
239 : virtual OUString makeExternalNameStr( sal_uInt16 nFileId, const OUString& rFile,
240 : const OUString& rName ) const = 0;
241 :
242 : virtual void makeExternalRefStr(
243 : OUStringBuffer& rBuffer, const ScAddress& rPos, sal_uInt16 nFileId, const OUString& rFileName,
244 : const OUString& rTabName, const ScSingleRefData& rRef ) const = 0;
245 :
246 : virtual void makeExternalRefStr(
247 : OUStringBuffer& rBuffer, const ScAddress& rPos,
248 : sal_uInt16 nFileId, const OUString& rFileName, const std::vector<OUString>& rTabNames,
249 : const OUString& rTabName, const ScComplexRefData& rRef ) const = 0;
250 :
251 : enum SpecialSymbolType
252 : {
253 : /**
254 : * Character between sheet name and address. In OOO A1 this is
255 : * '.', while XL A1 and XL R1C1 this is '!'.
256 : */
257 : SHEET_SEPARATOR,
258 :
259 : /**
260 : * In OOO A1, a sheet name may be prefixed with '$' to indicate an
261 : * absolute sheet position.
262 : */
263 : ABS_SHEET_PREFIX
264 : };
265 : virtual sal_Unicode getSpecialSymbol( SpecialSymbolType eSymType ) const = 0;
266 :
267 : virtual sal_uLong getCharTableFlags( sal_Unicode c, sal_Unicode cLast ) const = 0;
268 :
269 : protected:
270 : const sal_uLong* mpCharTable;
271 : };
272 : friend struct Convention;
273 :
274 : private:
275 :
276 : static CharClass *pCharClassEnglish; // character classification for en_US locale
277 : static const Convention *pConventions[ formula::FormulaGrammar::CONV_LAST ];
278 :
279 : static struct AddInMap
280 : {
281 : const char* pODFF;
282 : const char* pEnglish;
283 : const char* pOriginal; // programmatical name
284 : const char* pUpper; // upper case programmatical name
285 : } maAddInMap[];
286 : static const AddInMap* GetAddInMap();
287 : static size_t GetAddInMapCount();
288 :
289 : ScDocument* pDoc;
290 : ScAddress aPos;
291 :
292 : SvNumberFormatter* mpFormatter;
293 :
294 : // For CONV_XL_OOX, may be set via API by MOOXML filter.
295 : com::sun::star::uno::Sequence<com::sun::star::sheet::ExternalLinkInfo> maExternalLinks;
296 :
297 : sal_Unicode cSymbol[MAXSTRLEN]; // current Symbol
298 : OUString aFormula; // formula source code
299 : sal_Int32 nSrcPos; // tokenizer position (source code)
300 : mutable ScRawToken maRawToken;
301 :
302 : const CharClass* pCharClass; // which character classification is used for parseAnyToken
303 : sal_uInt16 mnPredetectedReference; // reference when reading ODF, 0 (none), 1 (single) or 2 (double)
304 : SCsTAB nMaxTab; // last sheet in document
305 : sal_Int32 mnRangeOpPosInSymbol; // if and where a range operator is in symbol
306 : const Convention *pConv;
307 : ExtendedErrorDetection meExtendedErrorDetection;
308 : bool mbCloseBrackets; // whether to close open brackets automatically, default TRUE
309 : bool mbRewind; // whether symbol is to be rewound to some step during lexical analysis
310 : std::vector<sal_uInt16> maExternalFiles;
311 :
312 : std::vector<OUString> maTabNames; /// sheet names mangled for the current grammar for output
313 : std::vector<OUString> &GetSetupTabNames() const; /// get or setup tab names for the current grammar
314 :
315 0 : struct TableRefEntry
316 : {
317 : ScTokenRef mxToken;
318 : sal_uInt16 mnLevel;
319 0 : TableRefEntry( formula::FormulaToken* p ) : mxToken(p), mnLevel(0) {}
320 : };
321 : std::vector<TableRefEntry> maTableRefs; /// "stack" of currently active ocTableRef tokens
322 :
323 : bool NextNewToken(bool bInArray = false);
324 :
325 : virtual void SetError(sal_uInt16 nError) SAL_OVERRIDE;
326 : sal_Int32 NextSymbol(bool bInArray);
327 : bool IsValue( const OUString& );
328 : bool IsOpCode( const OUString&, bool bInArray );
329 : bool IsOpCode2( const OUString& );
330 : bool IsString();
331 : bool IsReference( const OUString& );
332 : bool IsSingleReference( const OUString& );
333 : bool IsPredetectedReference(const OUString&);
334 : bool IsDoubleReference( const OUString& );
335 : bool IsMacro( const OUString& );
336 : bool IsNamedRange( const OUString& );
337 : bool IsExternalNamedRange( const OUString& rSymbol, bool& rbInvalidExternalNameRange );
338 : bool IsDBRange( const OUString& );
339 : bool IsColRowName( const OUString& );
340 : bool IsBoolean( const OUString& );
341 : void AutoCorrectParsedSymbol();
342 :
343 : void SetRelNameReference();
344 :
345 : /** Obtain range data for ocName token, global or sheet local.
346 :
347 : Prerequisite: rToken is a FormulaIndexToken so IsGlobal() and
348 : GetIndex() can be called on it. We don't check with RTTI.
349 : */
350 : ScRangeData* GetRangeData( const formula::FormulaToken& pToken ) const;
351 :
352 : static void InitCharClassEnglish();
353 :
354 : public:
355 : ScCompiler( sc::CompileFormulaContext& rCxt, const ScAddress& rPos );
356 :
357 : ScCompiler( ScDocument* pDocument, const ScAddress&);
358 :
359 : ScCompiler( sc::CompileFormulaContext& rCxt, const ScAddress& rPos, ScTokenArray& rArr );
360 :
361 : ScCompiler( ScDocument* pDocument, const ScAddress&,ScTokenArray& rArr);
362 :
363 : virtual ~ScCompiler();
364 :
365 : public:
366 : static void DeInit(); /// all
367 :
368 : // for ScAddress::Format()
369 : static void CheckTabQuotes( OUString& aTabName,
370 : const formula::FormulaGrammar::AddressConvention eConv = formula::FormulaGrammar::CONV_OOO );
371 :
372 : /** Analyzes a string for a 'Doc'#Tab construct, or 'Do''c'#Tab etc..
373 :
374 : @returns the position of the unquoted # hash mark in 'Doc'#Tab, or
375 : -1 if none. */
376 : static sal_Int32 GetDocTabPos( const OUString& rString );
377 :
378 : static bool EnQuote( OUString& rStr );
379 : sal_Unicode GetNativeAddressSymbol( Convention::SpecialSymbolType eType ) const;
380 :
381 : // Check if it is a valid english function name
382 : bool IsEnglishSymbol( const OUString& rName );
383 :
384 : bool IsErrorConstant( const OUString& ) const;
385 : bool IsTableRefItem( const OUString& ) const;
386 : bool IsTableRefColumn( const OUString& ) const;
387 :
388 : /** Calls GetToken() if PeekNextNoSpaces() is of given OpCode. */
389 : bool GetTokenIfOpCode( OpCode eOp );
390 :
391 : /**
392 : * When auto correction is set, the jump command reorder must be enabled.
393 : */
394 : void SetAutoCorrection( bool bVal );
395 0 : void SetCloseBrackets( bool bVal ) { mbCloseBrackets = bVal; }
396 : void SetRefConvention( const Convention *pConvP );
397 : void SetRefConvention( const formula::FormulaGrammar::AddressConvention eConv );
398 :
399 : static const Convention* GetRefConvention( formula::FormulaGrammar::AddressConvention eConv );
400 :
401 : /// Set symbol map if not empty.
402 : void SetFormulaLanguage( const OpCodeMapPtr & xMap );
403 :
404 : void SetGrammar( const formula::FormulaGrammar::Grammar eGrammar );
405 :
406 : void SetNumberFormatter( SvNumberFormatter* pFormatter );
407 :
408 : private:
409 : /** Set grammar and reference convention from within SetFormulaLanguage()
410 : or SetGrammar().
411 :
412 : @param eNewGrammar
413 : The new grammar to be set and the associated reference convention.
414 :
415 : @param eOldGrammar
416 : The previous grammar that was active before SetFormulaLanguage().
417 : */
418 : void SetGrammarAndRefConvention(
419 : const formula::FormulaGrammar::Grammar eNewGrammar,
420 : const formula::FormulaGrammar::Grammar eOldGrammar );
421 : public:
422 :
423 : /// Set external link info for ScAddress::CONV_XL_OOX.
424 1115 : void SetExternalLinks(
425 : const ::com::sun::star::uno::Sequence<
426 : com::sun::star::sheet::ExternalLinkInfo>& rLinks )
427 : {
428 1115 : maExternalLinks = rLinks;
429 1115 : }
430 :
431 : void CreateStringFromXMLTokenArray( OUString& rFormula, OUString& rFormulaNmsp );
432 :
433 76 : void SetExtendedErrorDetection( ExtendedErrorDetection eVal ) { meExtendedErrorDetection = eVal; }
434 :
435 0 : bool IsCorrected() { return bCorrected; }
436 0 : const OUString& GetCorrectedFormula() { return aCorrectedFormula; }
437 :
438 : /**
439 : * Tokenize formula expression string into an array of tokens.
440 : *
441 : * @param rFormula formula expression to tokenize.
442 : *
443 : * @return heap allocated token array object. The caller <i>must</i>
444 : * manage the life cycle of this object.
445 : */
446 : ScTokenArray* CompileString( const OUString& rFormula );
447 : ScTokenArray* CompileString( const OUString& rFormula, const OUString& rFormulaNmsp );
448 : const ScDocument* GetDoc() const { return pDoc; }
449 56 : const ScAddress& GetPos() const { return aPos; }
450 :
451 : void MoveRelWrap( SCCOL nMaxCol, SCROW nMaxRow );
452 : static void MoveRelWrap( ScTokenArray& rArr, ScDocument* pDoc, const ScAddress& rPos,
453 : SCCOL nMaxCol, SCROW nMaxRow );
454 :
455 : /** If the character is allowed as tested by nFlags (SC_COMPILER_C_...
456 : bits) for all known address conventions. If more than one bit is given
457 : in nFlags, all bits must match. If bTestLetterNumeric is false and
458 : char>=128, no LetterNumeric test is done and false is returned. */
459 : static bool IsCharFlagAllConventions(
460 : OUString const & rStr, sal_Int32 nPos, sal_uLong nFlags, bool bTestLetterNumeric = true );
461 :
462 : private:
463 : // FormulaCompiler
464 : virtual OUString FindAddInFunction( const OUString& rUpperName, bool bLocalFirst ) const SAL_OVERRIDE;
465 : virtual void fillFromAddInCollectionUpperName( NonConstOpCodeMapPtr xMap ) const SAL_OVERRIDE;
466 : virtual void fillFromAddInCollectionEnglishName( NonConstOpCodeMapPtr xMap ) const SAL_OVERRIDE;
467 : virtual void fillFromAddInMap( NonConstOpCodeMapPtr xMap, formula::FormulaGrammar::Grammar _eGrammar ) const SAL_OVERRIDE;
468 : virtual void fillAddInToken(::std::vector< ::com::sun::star::sheet::FormulaOpCodeMapEntry >& _rVec,bool _bIsEnglish) const SAL_OVERRIDE;
469 :
470 : virtual bool HandleExternalReference(const formula::FormulaToken& _aToken) SAL_OVERRIDE;
471 : virtual bool HandleRange() SAL_OVERRIDE;
472 : virtual bool HandleColRowName() SAL_OVERRIDE;
473 : virtual bool HandleDbData() SAL_OVERRIDE;
474 : virtual bool HandleTableRef() SAL_OVERRIDE;
475 :
476 : virtual formula::FormulaTokenRef ExtendRangeReference( formula::FormulaToken & rTok1, formula::FormulaToken & rTok2, bool bReuseDoubleRef ) SAL_OVERRIDE;
477 : virtual void CreateStringFromExternal( OUStringBuffer& rBuffer, const formula::FormulaToken* pToken ) const SAL_OVERRIDE;
478 : virtual void CreateStringFromSingleRef( OUStringBuffer& rBuffer, const formula::FormulaToken* pToken ) const SAL_OVERRIDE;
479 : virtual void CreateStringFromDoubleRef( OUStringBuffer& rBuffer, const formula::FormulaToken* pToken ) const SAL_OVERRIDE;
480 : virtual void CreateStringFromMatrix( OUStringBuffer& rBuffer, const formula::FormulaToken* pToken ) const SAL_OVERRIDE;
481 : virtual void CreateStringFromIndex( OUStringBuffer& rBuffer, const formula::FormulaToken* pToken ) const SAL_OVERRIDE;
482 : virtual void LocalizeString( OUString& rName ) const SAL_OVERRIDE; // modify rName - input: exact name
483 :
484 : /// Access the CharTable flags
485 125709 : inline sal_uLong GetCharTableFlags( sal_Unicode c, sal_Unicode cLast )
486 125709 : { return c < 128 ? pConv->getCharTableFlags(c, cLast) : 0; }
487 : };
488 :
489 : #endif
490 :
491 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|