1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef FORMULA_GRAMMAR_HXX
21 #define FORMULA_GRAMMAR_HXX
23 #include "com/sun/star/sheet/FormulaLanguage.hpp"
24 #include "formula/formuladllapi.h"
25 #include <tools/debug.hxx>
30 /** Grammars digested by ScCompiler.
32 class FORMULA_DLLPUBLIC FormulaGrammar
35 enum AddressConvention
{
36 CONV_UNSPECIFIED
= -1, /* useful when we want method to chose, must be first */
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 */
45 CONV_LOTUS_A1
, /* external? 3d? A1.B2 <placeholder/> */
47 CONV_LAST
/* for loops, must always be last */
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
);
59 /** Values encoding the formula language plus address reference convention
60 plus English parsing/formatting
62 //! When adding new values adapt isSupported() below as well.
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
|
70 kConventionOffset
) << kConventionShift
) |
72 /// ODF 1.1 with default ODF A1 bracketed references.
73 GRAM_PODF
= ::com::sun::star::sheet::FormulaLanguage::ODF_11
|
75 kConventionOffset
) << kConventionShift
) |
77 /// English with default A1 reference style.
78 GRAM_ENGLISH
= ::com::sun::star::sheet::FormulaLanguage::ENGLISH
|
80 kConventionOffset
) << kConventionShift
) |
82 /// Native with default A1 reference style.
83 GRAM_NATIVE
= ::com::sun::star::sheet::FormulaLanguage::NATIVE
|
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
|
89 kConventionOffset
) << kConventionShift
) |
91 /// ODFF with A1 reference style, unbracketed.
92 GRAM_ODFF_A1
= ::com::sun::star::sheet::FormulaLanguage::ODFF
|
94 kConventionOffset
) << kConventionShift
) |
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
|
99 kConventionOffset
) << kConventionShift
) |
101 /// ODF 1.1 with A1 reference style, unbracketed.
102 GRAM_PODF_A1
= ::com::sun::star::sheet::FormulaLanguage::ODF_11
|
104 kConventionOffset
) << kConventionShift
) |
106 /// Native with reference style as set in UI, may be A1 or R1C1.
107 GRAM_NATIVE_UI
= ::com::sun::star::sheet::FormulaLanguage::NATIVE
|
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
|
113 kConventionOffset
) << kConventionShift
),
114 /// Native with Excel A1 reference style.
115 GRAM_NATIVE_XL_A1
= ::com::sun::star::sheet::FormulaLanguage::NATIVE
|
117 kConventionOffset
) << kConventionShift
),
118 /// Native with Excel R1C1 reference style.
119 GRAM_NATIVE_XL_R1C1
= ::com::sun::star::sheet::FormulaLanguage::NATIVE
|
121 kConventionOffset
) << kConventionShift
),
122 /// English with Excel A1 reference style.
123 GRAM_ENGLISH_XL_A1
= ::com::sun::star::sheet::FormulaLanguage::XL_ENGLISH
|
125 kConventionOffset
) << kConventionShift
) |
127 /// English with Excel R1C1 reference style.
128 GRAM_ENGLISH_XL_R1C1
= ::com::sun::star::sheet::FormulaLanguage::XL_ENGLISH
|
130 kConventionOffset
) << kConventionShift
) |
132 /// English with Excel OOXML reference style.
133 GRAM_ENGLISH_XL_OOX
= ::com::sun::star::sheet::FormulaLanguage::XL_ENGLISH
|
135 kConventionOffset
) << kConventionShift
) |
137 /// Central definition of the default grammar to be used.
138 GRAM_DEFAULT
= GRAM_NATIVE_UI
,
140 /// Central definition of the default storage grammar to be used.
141 GRAM_STORAGE_DEFAULT
= GRAM_ODFF
,
143 /** OpCodeMap set by external filter and merged with reference
144 convention plus English bit on top. Plain value acts as
146 GRAM_EXTERNAL
= (1 << (kConventionShift
- 1))
149 /// If English parsing/formatting is associated with a grammar.
150 static inline bool isEnglish( const Grammar eGrammar
)
152 return (eGrammar
& kEnglishBit
) != 0;
155 /** Compatibility helper for old "bCompileEnglish, bCompileXML" API calls
156 to obtain the new grammar. */
157 static Grammar
mapAPItoGrammar( const bool bEnglish
, const bool bXML
)
160 if (bEnglish
&& bXML
)
161 eGrammar
= GRAM_PODF
;
162 else if (bEnglish
&& !bXML
)
163 eGrammar
= GRAM_PODF_A1
;
164 else if (!bEnglish
&& bXML
)
165 eGrammar
= GRAM_NATIVE_ODF
;
166 else // (!bEnglish && !bXML)
167 eGrammar
= GRAM_NATIVE
;
171 static bool isSupported( const Grammar eGrammar
)
183 case GRAM_NATIVE_UI
:
184 case GRAM_NATIVE_ODF
:
185 case GRAM_NATIVE_XL_A1
:
186 case GRAM_NATIVE_XL_R1C1
:
187 case GRAM_ENGLISH_XL_A1
:
188 case GRAM_ENGLISH_XL_R1C1
:
191 return extractFormulaLanguage( eGrammar
) == GRAM_EXTERNAL
;
195 static inline sal_Int32
extractFormulaLanguage( const Grammar eGrammar
)
197 return eGrammar
& kFlagMask
;
200 static inline AddressConvention
extractRefConvention( const Grammar eGrammar
)
202 return static_cast<AddressConvention
>(
203 ((eGrammar
& ~kEnglishBit
) >> kConventionShift
) -
207 static inline Grammar
setEnglishBit( const Grammar eGrammar
, const bool bEnglish
)
210 return static_cast<Grammar
>( eGrammar
| kEnglishBit
);
212 return static_cast<Grammar
>( eGrammar
& ~kEnglishBit
);
215 static inline Grammar
mergeToGrammar( const Grammar eGrammar
, const AddressConvention eConv
)
217 bool bEnglish
= isEnglish( eGrammar
);
218 Grammar eGram
= static_cast<Grammar
>(
219 extractFormulaLanguage( eGrammar
) |
220 ((eConv
+ kConventionOffset
) << kConventionShift
));
221 eGram
= setEnglishBit( eGram
, bEnglish
);
222 DBG_ASSERT( isSupported( eGram
), "CompilerGrammarMap::mergeToGrammar: unsupported grammar");
226 /// If grammar is of ODF 1.1
227 static inline bool isPODF( const Grammar eGrammar
)
229 return extractFormulaLanguage( eGrammar
) ==
230 ::com::sun::star::sheet::FormulaLanguage::ODF_11
;
233 /// If grammar is of ODFF
234 static inline bool isODFF( const Grammar eGrammar
)
236 return extractFormulaLanguage( eGrammar
) ==
237 ::com::sun::star::sheet::FormulaLanguage::ODFF
;
241 // =============================================================================
243 // =============================================================================
245 #endif // FORMULA_GRAMMAR_HXX
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */