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