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_FORMULA_GRAMMAR_HXX
21 #define INCLUDED_FORMULA_GRAMMAR_HXX
22 
23 #include <com/sun/star/sheet/FormulaLanguage.hpp>
24 #include <formula/formuladllapi.h>
25 #include <sal/types.h>
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         // not a real address convention, a special case for INDIRECT function interpretation
50         // only -> try using CONV_OOO, failing that CONV_XL_A1
51         CONV_A1_XL_A1
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       = (1 << (kConventionShift + 8));
60     // Mask off all non-language bits.
61     static const int kFlagMask         = ~((~unsigned(0)) << 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           = css::sheet::FormulaLanguage::ODFF                 |
73                                 ((CONV_ODF           +
74                                   kConventionOffset) << kConventionShift)       |
75                                 kEnglishBit,
76         /// ODF 1.1 with default ODF A1 bracketed references.
77         GRAM_PODF           = css::sheet::FormulaLanguage::ODF_11               |
78                                 ((CONV_ODF           +
79                                   kConventionOffset) << kConventionShift)       |
80                                 kEnglishBit,
81         /// English with default A1 reference style.
82         GRAM_ENGLISH        = css::sheet::FormulaLanguage::ENGLISH              |
83                                 ((CONV_OOO           +
84                                   kConventionOffset) << kConventionShift)       |
85                                 kEnglishBit,
86         /// Native with default A1 reference style.
87         GRAM_NATIVE         = css::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        = css::sheet::FormulaLanguage::ODFF                 |
92                                 ((CONV_UNSPECIFIED   +
93                                   kConventionOffset) << kConventionShift)       |
94                                 kEnglishBit,
95         /// ODFF with A1 reference style, unbracketed.
96         GRAM_ODFF_A1        = css::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        = css::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        = css::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      = css::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     = css::sheet::FormulaLanguage::NATIVE               |
116                                 ((CONV_ODF           +
117                                   kConventionOffset) << kConventionShift),
118         /// Native with Excel A1 reference style.
119         GRAM_NATIVE_XL_A1   = css::sheet::FormulaLanguage::NATIVE               |
120                                 ((CONV_XL_A1         +
121                                   kConventionOffset) << kConventionShift),
122         /// Native with Excel R1C1 reference style.
123         GRAM_NATIVE_XL_R1C1 = css::sheet::FormulaLanguage::NATIVE               |
124                                 ((CONV_XL_R1C1       +
125                                   kConventionOffset) << kConventionShift),
126         /// English with Excel A1 reference style.
127         GRAM_ENGLISH_XL_A1   = css::sheet::FormulaLanguage::XL_ENGLISH               |
128                                 ((CONV_XL_A1         +
129                                   kConventionOffset) << kConventionShift)            |
130                                 kEnglishBit,
131         /// English with Excel R1C1 reference style.
132         GRAM_ENGLISH_XL_R1C1 = css::sheet::FormulaLanguage::XL_ENGLISH               |
133                                 ((CONV_XL_R1C1       +
134                                   kConventionOffset) << kConventionShift)            |
135                                 kEnglishBit,
136         /// English with Excel OOXML reference style.
137         GRAM_ENGLISH_XL_OOX  = css::sheet::FormulaLanguage::XL_ENGLISH               |
138                                 ((CONV_XL_OOX        +
139                                   kConventionOffset) << kConventionShift)            |
140                                 kEnglishBit,
141         /// Excel OOXML with Excel OOXML reference style.
142         GRAM_OOXML           = css::sheet::FormulaLanguage::OOXML               |
143                                 ((CONV_XL_OOX        +
144                                   kConventionOffset) << kConventionShift)       |
145                                 kEnglishBit,
146         /// API English with A1 reference style, unbracketed.
147         GRAM_API            = css::sheet::FormulaLanguage::API                  |
148                                 ((CONV_OOO           +
149                                   kConventionOffset) << kConventionShift)       |
150                                 kEnglishBit,
151         /// Central definition of the default grammar to be used.
152         GRAM_DEFAULT        = GRAM_NATIVE_UI,
153 
154         /// Central definition of the default storage grammar to be used.
155         GRAM_STORAGE_DEFAULT = GRAM_ODFF,
156 
157         /** OpCodeMap set by external filter and merged with reference
158             convention plus English bit on top. Plain value acts as
159             FormulaLanguage. */
160         GRAM_EXTERNAL       = (1 << (kConventionShift - 1))
161     };
162 
163     /// If English parsing/formatting is associated with a grammar.
isEnglish(const Grammar eGrammar)164     static bool isEnglish( const Grammar eGrammar )
165     {
166         return (eGrammar & kEnglishBit) != 0;
167     }
168 
169     /** Compatibility helper for old "bCompileEnglish, bCompileXML" API calls
170         to obtain the new grammar. */
171     static Grammar mapAPItoGrammar( const bool bEnglish, const bool bXML );
172 
173     static bool isSupported( const Grammar eGrammar );
174 
extractFormulaLanguage(const Grammar eGrammar)175     static sal_Int32 extractFormulaLanguage( const Grammar eGrammar )
176     {
177         return eGrammar & kFlagMask;
178     }
179 
extractRefConvention(const Grammar eGrammar)180     static AddressConvention extractRefConvention( const Grammar eGrammar )
181     {
182         return static_cast<AddressConvention>(
183                 ((eGrammar & ~kEnglishBit) >> kConventionShift) -
184                 kConventionOffset);
185     }
186 
187     static Grammar setEnglishBit( const Grammar eGrammar, const bool bEnglish );
188 
189     static Grammar mergeToGrammar( const Grammar eGrammar, const AddressConvention eConv );
190 
191     /// If grammar is of ODF 1.1
isPODF(const Grammar eGrammar)192     static bool isPODF( const Grammar eGrammar )
193     {
194         return extractFormulaLanguage( eGrammar) ==
195             css::sheet::FormulaLanguage::ODF_11;
196     }
197 
198     /// If grammar is of ODFF
isODFF(const Grammar eGrammar)199     static bool isODFF( const Grammar eGrammar )
200     {
201         return extractFormulaLanguage( eGrammar) ==
202             css::sheet::FormulaLanguage::ODFF;
203     }
204 
205     /// If grammar is of OOXML
isOOXML(const Grammar eGrammar)206     static bool isOOXML( const Grammar eGrammar )
207     {
208         return extractFormulaLanguage( eGrammar) ==
209             css::sheet::FormulaLanguage::OOXML;
210     }
211 
212     /// If grammar has an Excel syntax, determined by address convention.
isExcelSyntax(const Grammar eGrammar)213     static bool isExcelSyntax( const Grammar eGrammar )
214     {
215         AddressConvention eConv = extractRefConvention( eGrammar );
216         switch (eConv)
217         {
218             case FormulaGrammar::AddressConvention::CONV_XL_A1:
219             case FormulaGrammar::AddressConvention::CONV_XL_R1C1:
220             case FormulaGrammar::AddressConvention::CONV_XL_OOX:
221                 return true;
222             default:
223                 return false;
224         }
225     }
226 
227 };
228 
229 } // formula
230 
231 
232 #endif // INCLUDED_FORMULA_GRAMMAR_HXX
233 
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
235