Branch data 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 FORMULA_GRAMMAR_HXX
21 : : #define FORMULA_GRAMMAR_HXX
22 : :
23 : : #include "com/sun/star/sheet/FormulaLanguage.hpp"
24 : : #include "formula/formuladllapi.h"
25 : : #include <tools/debug.hxx>
26 : :
27 : : namespace formula
28 : : {
29 : :
30 : : /** Grammars digested by ScCompiler.
31 : : */
32 : : class FORMULA_DLLPUBLIC FormulaGrammar
33 : : {
34 : : public:
35 : : enum AddressConvention{
36 : : CONV_UNSPECIFIED = -1, /* useful when we want method to chose, must be first */
37 : :
38 : : /* elements must be sequential and changes should be reflected in ScCompiler::pCharTables */
39 : : CONV_OOO = 0, /* 'doc'#sheet.A1:sheet2.B2 */
40 : : CONV_ODF, /* ['doc'#sheet.A1:sheet2.B2] */
41 : : CONV_XL_A1, /* [doc]sheet:sheet2!A1:B2 */
42 : : CONV_XL_R1C1, /* [doc]sheet:sheet2!R1C1:R2C2 */
43 : : CONV_XL_OOX, /* [#]sheet:sheet2!A1:B2 */
44 : :
45 : : CONV_LOTUS_A1, /* external? 3d? A1.B2 <placeholder/> */
46 : :
47 : : CONV_LAST /* for loops, must always be last */
48 : : };
49 : :
50 : : //! CONV_UNSPECIFIED is a negative value!
51 : : static const int kConventionOffset = - CONV_UNSPECIFIED + 1;
52 : : // Room for 32k hypothetical languages plus EXTERNAL.
53 : : static const int kConventionShift = 16;
54 : : // Room for 256 reference conventions.
55 : : static const int kEnglishBit = (1 << (kConventionShift + 8));
56 : : // Mask off all non-language bits.
57 : : static const int kFlagMask = ~((~int(0)) << kConventionShift);
58 : :
59 : : /** Values encoding the formula language plus address reference convention
60 : : plus English parsing/formatting
61 : : */
62 : : //! When adding new values adapt isSupported() below as well.
63 : : enum Grammar
64 : : {
65 : : /// Used only in ScCompiler ctor and in some XML import API context.
66 : : GRAM_UNSPECIFIED = -1,
67 : : /// ODFF with default ODF A1 bracketed references.
68 : : GRAM_ODFF = ::com::sun::star::sheet::FormulaLanguage::ODFF |
69 : : ((CONV_ODF +
70 : : kConventionOffset) << kConventionShift) |
71 : : kEnglishBit,
72 : : /// ODF 1.1 with default ODF A1 bracketed references.
73 : : GRAM_PODF = ::com::sun::star::sheet::FormulaLanguage::ODF_11 |
74 : : ((CONV_ODF +
75 : : kConventionOffset) << kConventionShift) |
76 : : kEnglishBit,
77 : : /// English with default A1 reference style.
78 : : GRAM_ENGLISH = ::com::sun::star::sheet::FormulaLanguage::ENGLISH |
79 : : ((CONV_OOO +
80 : : kConventionOffset) << kConventionShift) |
81 : : kEnglishBit,
82 : : /// Native with default A1 reference style.
83 : : GRAM_NATIVE = ::com::sun::star::sheet::FormulaLanguage::NATIVE |
84 : : ((CONV_OOO +
85 : : kConventionOffset) << kConventionShift),
86 : : /// ODFF with reference style as set in UI, may be A1 or R1C1.
87 : : GRAM_ODFF_UI = ::com::sun::star::sheet::FormulaLanguage::ODFF |
88 : : ((CONV_UNSPECIFIED +
89 : : kConventionOffset) << kConventionShift) |
90 : : kEnglishBit,
91 : : /// ODFF with A1 reference style, unbracketed.
92 : : GRAM_ODFF_A1 = ::com::sun::star::sheet::FormulaLanguage::ODFF |
93 : : ((CONV_OOO +
94 : : kConventionOffset) << kConventionShift) |
95 : : kEnglishBit,
96 : : /// ODF 1.1 with reference style as set in UI, may be A1 or R1C1.
97 : : GRAM_PODF_UI = ::com::sun::star::sheet::FormulaLanguage::ODF_11 |
98 : : ((CONV_UNSPECIFIED +
99 : : kConventionOffset) << kConventionShift) |
100 : : kEnglishBit,
101 : : /// ODF 1.1 with A1 reference style, unbracketed.
102 : : GRAM_PODF_A1 = ::com::sun::star::sheet::FormulaLanguage::ODF_11 |
103 : : ((CONV_OOO +
104 : : kConventionOffset) << kConventionShift) |
105 : : kEnglishBit,
106 : : /// Native with reference style as set in UI, may be A1 or R1C1.
107 : : GRAM_NATIVE_UI = ::com::sun::star::sheet::FormulaLanguage::NATIVE |
108 : : ((CONV_UNSPECIFIED +
109 : : kConventionOffset) << kConventionShift),
110 : : /// Native with ODF A1 bracketed references. Not very useful but supported.
111 : : GRAM_NATIVE_ODF = ::com::sun::star::sheet::FormulaLanguage::NATIVE |
112 : : ((CONV_ODF +
113 : : kConventionOffset) << kConventionShift),
114 : : /// Native with Excel A1 reference style.
115 : : GRAM_NATIVE_XL_A1 = ::com::sun::star::sheet::FormulaLanguage::NATIVE |
116 : : ((CONV_XL_A1 +
117 : : kConventionOffset) << kConventionShift),
118 : : /// Native with Excel R1C1 reference style.
119 : : GRAM_NATIVE_XL_R1C1 = ::com::sun::star::sheet::FormulaLanguage::NATIVE |
120 : : ((CONV_XL_R1C1 +
121 : : kConventionOffset) << kConventionShift),
122 : : /// English with Excel A1 reference style.
123 : : GRAM_ENGLISH_XL_A1 = ::com::sun::star::sheet::FormulaLanguage::XL_ENGLISH |
124 : : ((CONV_XL_A1 +
125 : : kConventionOffset) << kConventionShift) |
126 : : kEnglishBit,
127 : : /// English with Excel R1C1 reference style.
128 : : GRAM_ENGLISH_XL_R1C1 = ::com::sun::star::sheet::FormulaLanguage::XL_ENGLISH |
129 : : ((CONV_XL_R1C1 +
130 : : kConventionOffset) << kConventionShift) |
131 : : kEnglishBit,
132 : : /// Central definition of the default grammar to be used.
133 : : GRAM_DEFAULT = GRAM_NATIVE_UI,
134 : :
135 : : /// Central definition of the default storage grammar to be used.
136 : : GRAM_STORAGE_DEFAULT = GRAM_ODFF,
137 : :
138 : : /** OpCodeMap set by external filter and merged with reference
139 : : convention plus English bit on top. Plain value acts as
140 : : FormulaLanguage. */
141 : : GRAM_EXTERNAL = (1 << (kConventionShift - 1))
142 : : };
143 : :
144 : : /// If English parsing/formatting is associated with a grammar.
145 : 39028 : static inline bool isEnglish( const Grammar eGrammar )
146 : : {
147 : 39028 : return (eGrammar & kEnglishBit) != 0;
148 : : }
149 : :
150 : : /** Compatibility helper for old "bCompileEnglish, bCompileXML" API calls
151 : : to obtain the new grammar. */
152 : 147 : static Grammar mapAPItoGrammar( const bool bEnglish, const bool bXML )
153 : : {
154 : : Grammar eGrammar;
155 [ + + ][ - + ]: 147 : if (bEnglish && bXML)
156 : 0 : eGrammar = GRAM_PODF;
157 [ + + ][ + - ]: 147 : else if (bEnglish && !bXML)
158 : 83 : eGrammar = GRAM_PODF_A1;
159 [ + - ][ - + ]: 64 : else if (!bEnglish && bXML)
160 : 0 : eGrammar = GRAM_NATIVE_ODF;
161 : : else // (!bEnglish && !bXML)
162 : 64 : eGrammar = GRAM_NATIVE;
163 : 147 : return eGrammar;
164 : : }
165 : :
166 : : static bool isSupported( const Grammar eGrammar )
167 : : {
168 : : switch (eGrammar)
169 : : {
170 : : case GRAM_ODFF :
171 : : case GRAM_PODF :
172 : : case GRAM_ENGLISH :
173 : : case GRAM_NATIVE :
174 : : case GRAM_ODFF_UI :
175 : : case GRAM_ODFF_A1 :
176 : : case GRAM_PODF_UI :
177 : : case GRAM_PODF_A1 :
178 : : case GRAM_NATIVE_UI :
179 : : case GRAM_NATIVE_ODF :
180 : : case GRAM_NATIVE_XL_A1 :
181 : : case GRAM_NATIVE_XL_R1C1 :
182 : : case GRAM_ENGLISH_XL_A1 :
183 : : case GRAM_ENGLISH_XL_R1C1:
184 : : return true;
185 : : default:
186 : : return extractFormulaLanguage( eGrammar) == GRAM_EXTERNAL;
187 : : }
188 : : }
189 : :
190 : 81780 : static inline sal_Int32 extractFormulaLanguage( const Grammar eGrammar )
191 : : {
192 : 81780 : return eGrammar & kFlagMask;
193 : : }
194 : :
195 : 76425 : static inline AddressConvention extractRefConvention( const Grammar eGrammar )
196 : : {
197 : : return static_cast<AddressConvention>(
198 : : ((eGrammar & ~kEnglishBit) >> kConventionShift) -
199 : 76425 : kConventionOffset);
200 : : }
201 : :
202 : 38350 : static inline Grammar setEnglishBit( const Grammar eGrammar, const bool bEnglish )
203 : : {
204 [ + + ]: 38350 : if (bEnglish)
205 : 5432 : return static_cast<Grammar>( eGrammar | kEnglishBit);
206 : : else
207 : 38350 : return static_cast<Grammar>( eGrammar & ~kEnglishBit);
208 : : }
209 : :
210 : 38326 : static inline Grammar mergeToGrammar( const Grammar eGrammar, const AddressConvention eConv )
211 : : {
212 : 38326 : bool bEnglish = isEnglish( eGrammar);
213 : : Grammar eGram = static_cast<Grammar>(
214 : 38326 : extractFormulaLanguage( eGrammar) |
215 : 38326 : ((eConv + kConventionOffset) << kConventionShift));
216 : 38326 : eGram = setEnglishBit( eGram, bEnglish);
217 : : DBG_ASSERT( isSupported( eGram), "CompilerGrammarMap::mergeToGrammar: unsupported grammar");
218 : 38326 : return eGram;
219 : : }
220 : :
221 : : /// If grammar is of ODF 1.1
222 : 4393 : static inline bool isPODF( const Grammar eGrammar )
223 : : {
224 : 4393 : return extractFormulaLanguage( eGrammar) ==
225 : 4393 : ::com::sun::star::sheet::FormulaLanguage::ODF_11;
226 : : }
227 : :
228 : : /// If grammar is of ODFF
229 : 5700 : static inline bool isODFF( const Grammar eGrammar )
230 : : {
231 : 5700 : return extractFormulaLanguage( eGrammar) ==
232 : 5700 : ::com::sun::star::sheet::FormulaLanguage::ODFF;
233 : : }
234 : :
235 : : };
236 : : // =============================================================================
237 : : } // formula
238 : : // =============================================================================
239 : :
240 : : #endif // FORMULA_GRAMMAR_HXX
241 : :
242 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|