xref: /trunk/main/formula/inc/formula/grammar.hxx (revision b296461c)
15116778eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
35116778eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
45116778eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
55116778eSAndrew Rist  * distributed with this work for additional information
65116778eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
75116778eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
85116778eSAndrew Rist  * "License"); you may not use this file except in compliance
95116778eSAndrew Rist  * with the License.  You may obtain a copy of the License at
105116778eSAndrew Rist  *
115116778eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
125116778eSAndrew Rist  *
135116778eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
145116778eSAndrew Rist  * software distributed under the License is distributed on an
155116778eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165116778eSAndrew Rist  * KIND, either express or implied.  See the License for the
175116778eSAndrew Rist  * specific language governing permissions and limitations
185116778eSAndrew Rist  * under the License.
195116778eSAndrew Rist  *
205116778eSAndrew Rist  *************************************************************/
215116778eSAndrew Rist 
225116778eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef FORMULA_GRAMMAR_HXX
25cdf0e10cSrcweir #define FORMULA_GRAMMAR_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "com/sun/star/sheet/FormulaLanguage.hpp"
28cdf0e10cSrcweir #include "formula/formuladllapi.h"
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace formula
32cdf0e10cSrcweir {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /** Grammars digested by ScCompiler.
35cdf0e10cSrcweir  */
36cdf0e10cSrcweir class FORMULA_DLLPUBLIC FormulaGrammar
37cdf0e10cSrcweir {
38cdf0e10cSrcweir public:
39cdf0e10cSrcweir     enum AddressConvention{
40cdf0e10cSrcweir         CONV_UNSPECIFIED = -1,  /* useful when we want method to chose, must be first */
41cdf0e10cSrcweir 
42cdf0e10cSrcweir         /* elements must be sequential and changes should be reflected in ScCompiler::pCharTables */
43cdf0e10cSrcweir         CONV_OOO     =  0,  /* 'doc'#sheet.A1:sheet2.B2 */
44cdf0e10cSrcweir         CONV_ODF,           /* ['doc'#sheet.A1:sheet2.B2] */
45cdf0e10cSrcweir         CONV_XL_A1,         /* [doc]sheet:sheet2!A1:B2 */
46cdf0e10cSrcweir         CONV_XL_R1C1,       /* [doc]sheet:sheet2!R1C1:R2C2 */
47cdf0e10cSrcweir         CONV_XL_OOX,        /* [#]sheet:sheet2!A1:B2 */
48cdf0e10cSrcweir 
49cdf0e10cSrcweir         CONV_LOTUS_A1,      /* external? 3d? A1.B2 <placeholder/> */
50cdf0e10cSrcweir 
51cdf0e10cSrcweir         CONV_LAST   /* for loops, must always be last */
52cdf0e10cSrcweir     };
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     //! CONV_UNSPECIFIED is a negative value!
55cdf0e10cSrcweir     static const int kConventionOffset = - CONV_UNSPECIFIED + 1;
56cdf0e10cSrcweir     // Room for 32k hypothetical languages plus EXTERNAL.
57cdf0e10cSrcweir     static const int kConventionShift  = 16;
58cdf0e10cSrcweir     // Room for 256 reference conventions.
59*b296461cStruckman     static const int kEnglishBit       = (1u << (kConventionShift + 8));
60cdf0e10cSrcweir     // Mask off all non-language bits.
61*b296461cStruckman     static const int kFlagMask         = ~((~0u) << kConventionShift);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /** Values encoding the formula language plus address reference convention
64cdf0e10cSrcweir         plus English parsing/formatting
65cdf0e10cSrcweir      */
66cdf0e10cSrcweir     //! When adding new values adapt isSupported() below as well.
67cdf0e10cSrcweir     enum Grammar
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         /// Used only in ScCompiler ctor and in some XML import API context.
70cdf0e10cSrcweir         GRAM_UNSPECIFIED    = -1,
71cdf0e10cSrcweir         /// ODFF with default ODF A1 bracketed references.
72cdf0e10cSrcweir         GRAM_ODFF           = ::com::sun::star::sheet::FormulaLanguage::ODFF    |
73cdf0e10cSrcweir                                 ((CONV_ODF           +
74cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
75cdf0e10cSrcweir                                 kEnglishBit,
76cdf0e10cSrcweir         /// ODF 1.1 with default ODF A1 bracketed references.
77cdf0e10cSrcweir         GRAM_PODF           = ::com::sun::star::sheet::FormulaLanguage::ODF_11  |
78cdf0e10cSrcweir                                 ((CONV_ODF           +
79cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
80cdf0e10cSrcweir                                 kEnglishBit,
81cdf0e10cSrcweir         /// English with default A1 reference style.
82cdf0e10cSrcweir         GRAM_ENGLISH        = ::com::sun::star::sheet::FormulaLanguage::ENGLISH |
83cdf0e10cSrcweir                                 ((CONV_OOO           +
84cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
85cdf0e10cSrcweir                                 kEnglishBit,
86cdf0e10cSrcweir         /// Native with default A1 reference style.
87cdf0e10cSrcweir         GRAM_NATIVE         = ::com::sun::star::sheet::FormulaLanguage::NATIVE  |
88cdf0e10cSrcweir                                 ((CONV_OOO           +
89cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift),
90cdf0e10cSrcweir         /// ODFF with reference style as set in UI, may be A1 or R1C1.
91cdf0e10cSrcweir         GRAM_ODFF_UI        = ::com::sun::star::sheet::FormulaLanguage::ODFF    |
92cdf0e10cSrcweir                                 ((CONV_UNSPECIFIED   +
93cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
94cdf0e10cSrcweir                                 kEnglishBit,
95cdf0e10cSrcweir         /// ODFF with A1 reference style, unbracketed.
96cdf0e10cSrcweir         GRAM_ODFF_A1        = ::com::sun::star::sheet::FormulaLanguage::ODFF    |
97cdf0e10cSrcweir                                 ((CONV_OOO           +
98cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
99cdf0e10cSrcweir                                 kEnglishBit,
100cdf0e10cSrcweir         /// ODF 1.1 with reference style as set in UI, may be A1 or R1C1.
101cdf0e10cSrcweir         GRAM_PODF_UI        = ::com::sun::star::sheet::FormulaLanguage::ODF_11  |
102cdf0e10cSrcweir                                 ((CONV_UNSPECIFIED   +
103cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
104cdf0e10cSrcweir                                 kEnglishBit,
105cdf0e10cSrcweir         /// ODF 1.1 with A1 reference style, unbracketed.
106cdf0e10cSrcweir         GRAM_PODF_A1        = ::com::sun::star::sheet::FormulaLanguage::ODF_11  |
107cdf0e10cSrcweir                                 ((CONV_OOO           +
108cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift)       |
109cdf0e10cSrcweir                                 kEnglishBit,
110cdf0e10cSrcweir         /// Native with reference style as set in UI, may be A1 or R1C1.
111cdf0e10cSrcweir         GRAM_NATIVE_UI      = ::com::sun::star::sheet::FormulaLanguage::NATIVE  |
112cdf0e10cSrcweir                                 ((CONV_UNSPECIFIED   +
113cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift),
114cdf0e10cSrcweir         /// Native with ODF A1 bracketed references. Not very useful but supported.
115cdf0e10cSrcweir         GRAM_NATIVE_ODF     = ::com::sun::star::sheet::FormulaLanguage::NATIVE  |
116cdf0e10cSrcweir                                 ((CONV_ODF           +
117cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift),
118cdf0e10cSrcweir         /// Native with Excel A1 reference style.
119cdf0e10cSrcweir         GRAM_NATIVE_XL_A1   = ::com::sun::star::sheet::FormulaLanguage::NATIVE  |
120cdf0e10cSrcweir                                 ((CONV_XL_A1         +
121cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift),
122cdf0e10cSrcweir         /// Native with Excel R1C1 reference style.
123cdf0e10cSrcweir         GRAM_NATIVE_XL_R1C1 = ::com::sun::star::sheet::FormulaLanguage::NATIVE  |
124cdf0e10cSrcweir                                 ((CONV_XL_R1C1       +
125cdf0e10cSrcweir                                   kConventionOffset) << kConventionShift),
126cdf0e10cSrcweir         /// Central definition of the default grammar to be used.
127cdf0e10cSrcweir         GRAM_DEFAULT        = GRAM_NATIVE_UI,
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         /// Central definition of the default storage grammar to be used.
130cdf0e10cSrcweir         GRAM_STORAGE_DEFAULT = GRAM_ODFF,
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         /** OpCodeMap set by external filter and merged with reference
133cdf0e10cSrcweir             convention plus English bit on top. Plain value acts as
134cdf0e10cSrcweir             FormulaLanguage. */
135cdf0e10cSrcweir         GRAM_EXTERNAL       = (1 << (kConventionShift - 1))
136cdf0e10cSrcweir     };
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     /// If English parsing/formatting is associated with a grammar.
isEnglish(const Grammar eGrammar)139cdf0e10cSrcweir     static inline bool isEnglish( const Grammar eGrammar )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         return (eGrammar & kEnglishBit) != 0;
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     /** Compatibility helper for old "bCompileEnglish, bCompileXML" API calls
145cdf0e10cSrcweir         to obtain the new grammar. */
mapAPItoGrammar(const bool bEnglish,const bool bXML)146cdf0e10cSrcweir     static Grammar mapAPItoGrammar( const bool bEnglish, const bool bXML )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         Grammar eGrammar;
149cdf0e10cSrcweir         if (bEnglish && bXML)
150cdf0e10cSrcweir             eGrammar = GRAM_PODF;
151cdf0e10cSrcweir         else if (bEnglish && !bXML)
152cdf0e10cSrcweir             eGrammar = GRAM_PODF_A1;
153cdf0e10cSrcweir         else if (!bEnglish && bXML)
154cdf0e10cSrcweir             eGrammar = GRAM_NATIVE_ODF;
155cdf0e10cSrcweir         else // (!bEnglish && !bXML)
156cdf0e10cSrcweir             eGrammar = GRAM_NATIVE;
157cdf0e10cSrcweir         return eGrammar;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
isSupported(const Grammar eGrammar)160cdf0e10cSrcweir     static bool isSupported( const Grammar eGrammar )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         switch (eGrammar)
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             case GRAM_ODFF           :
165cdf0e10cSrcweir             case GRAM_PODF           :
166cdf0e10cSrcweir             case GRAM_ENGLISH        :
167cdf0e10cSrcweir             case GRAM_NATIVE         :
168cdf0e10cSrcweir             case GRAM_ODFF_UI        :
169cdf0e10cSrcweir             case GRAM_ODFF_A1        :
170cdf0e10cSrcweir             case GRAM_PODF_UI        :
171cdf0e10cSrcweir             case GRAM_PODF_A1        :
172cdf0e10cSrcweir             case GRAM_NATIVE_UI      :
173cdf0e10cSrcweir             case GRAM_NATIVE_ODF     :
174cdf0e10cSrcweir             case GRAM_NATIVE_XL_A1   :
175cdf0e10cSrcweir             case GRAM_NATIVE_XL_R1C1 :
176cdf0e10cSrcweir                 return true;
177cdf0e10cSrcweir             default:
178cdf0e10cSrcweir                 return extractFormulaLanguage( eGrammar) == GRAM_EXTERNAL;
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
extractFormulaLanguage(const Grammar eGrammar)182cdf0e10cSrcweir     static inline sal_Int32 extractFormulaLanguage( const Grammar eGrammar )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         return eGrammar & kFlagMask;
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
extractRefConvention(const Grammar eGrammar)187cdf0e10cSrcweir     static inline AddressConvention extractRefConvention( const Grammar eGrammar )
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         return static_cast<AddressConvention>(
190cdf0e10cSrcweir                 ((eGrammar & ~kEnglishBit) >> kConventionShift) -
191cdf0e10cSrcweir                 kConventionOffset);
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
setEnglishBit(const Grammar eGrammar,const bool bEnglish)194cdf0e10cSrcweir     static inline Grammar setEnglishBit( const Grammar eGrammar, const bool bEnglish )
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         if (bEnglish)
197cdf0e10cSrcweir             return static_cast<Grammar>( eGrammar | kEnglishBit);
198cdf0e10cSrcweir         else
199cdf0e10cSrcweir             return static_cast<Grammar>( eGrammar & ~kEnglishBit);
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
mergeToGrammar(const Grammar eGrammar,const AddressConvention eConv)202cdf0e10cSrcweir     static inline Grammar mergeToGrammar( const Grammar eGrammar, const AddressConvention eConv )
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         bool bEnglish = isEnglish( eGrammar);
205cdf0e10cSrcweir         Grammar eGram = static_cast<Grammar>(
206cdf0e10cSrcweir                 extractFormulaLanguage( eGrammar) |
207cdf0e10cSrcweir                 ((eConv + kConventionOffset) << kConventionShift));
208cdf0e10cSrcweir         eGram = setEnglishBit( eGram, bEnglish);
209cdf0e10cSrcweir         DBG_ASSERT( isSupported( eGram), "CompilerGrammarMap::mergeToGrammar: unsupported grammar");
210cdf0e10cSrcweir         return eGram;
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     /// If grammar is of ODF 1.1
isPODF(const Grammar eGrammar)214cdf0e10cSrcweir     static inline bool isPODF( const Grammar eGrammar )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         return extractFormulaLanguage( eGrammar) ==
217cdf0e10cSrcweir             ::com::sun::star::sheet::FormulaLanguage::ODF_11;
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     /// If grammar is of ODFF
isODFF(const Grammar eGrammar)221cdf0e10cSrcweir     static inline bool isODFF( const Grammar eGrammar )
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         return extractFormulaLanguage( eGrammar) ==
224cdf0e10cSrcweir             ::com::sun::star::sheet::FormulaLanguage::ODFF;
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir };
228cdf0e10cSrcweir // =============================================================================
229cdf0e10cSrcweir } // formula
230cdf0e10cSrcweir // =============================================================================
231cdf0e10cSrcweir 
232cdf0e10cSrcweir #endif // FORMULA_GRAMMAR_HXX
233