LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/formula - grammar.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 30 32 93.8 %
Date: 2013-07-09 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          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             :         /// English with Excel OOXML reference style.
     133             :         GRAM_ENGLISH_XL_OOX  = ::com::sun::star::sheet::FormulaLanguage::XL_ENGLISH  |
     134             :                                 ((CONV_XL_OOX        +
     135             :                                   kConventionOffset) << kConventionShift)            |
     136             :                                 kEnglishBit,
     137             :         /// Central definition of the default grammar to be used.
     138             :         GRAM_DEFAULT        = GRAM_NATIVE_UI,
     139             : 
     140             :         /// Central definition of the default storage grammar to be used.
     141             :         GRAM_STORAGE_DEFAULT = GRAM_ODFF,
     142             : 
     143             :         /** OpCodeMap set by external filter and merged with reference
     144             :             convention plus English bit on top. Plain value acts as
     145             :             FormulaLanguage. */
     146             :         GRAM_EXTERNAL       = (1 << (kConventionShift - 1))
     147             :     };
     148             : 
     149             :     /// If English parsing/formatting is associated with a grammar.
     150       39752 :     static inline bool isEnglish( const Grammar eGrammar )
     151             :     {
     152       39752 :         return (eGrammar & kEnglishBit) != 0;
     153             :     }
     154             : 
     155             :     /** Compatibility helper for old "bCompileEnglish, bCompileXML" API calls
     156             :         to obtain the new grammar. */
     157         186 :     static Grammar mapAPItoGrammar( const bool bEnglish, const bool bXML )
     158             :     {
     159             :         Grammar eGrammar;
     160         186 :         if (bEnglish && bXML)
     161           0 :             eGrammar = GRAM_PODF;
     162         186 :         else if (bEnglish && !bXML)
     163         142 :             eGrammar = GRAM_PODF_A1;
     164          44 :         else if (!bEnglish && bXML)
     165           0 :             eGrammar = GRAM_NATIVE_ODF;
     166             :         else // (!bEnglish && !bXML)
     167          44 :             eGrammar = GRAM_NATIVE;
     168         186 :         return eGrammar;
     169             :     }
     170             : 
     171             :     static bool isSupported( const Grammar eGrammar )
     172             :     {
     173             :         switch (eGrammar)
     174             :         {
     175             :             case GRAM_ODFF           :
     176             :             case GRAM_PODF           :
     177             :             case GRAM_ENGLISH        :
     178             :             case GRAM_NATIVE         :
     179             :             case GRAM_ODFF_UI        :
     180             :             case GRAM_ODFF_A1        :
     181             :             case GRAM_PODF_UI        :
     182             :             case GRAM_PODF_A1        :
     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:
     189             :                 return true;
     190             :             default:
     191             :                 return extractFormulaLanguage( eGrammar) == GRAM_EXTERNAL;
     192             :         }
     193             :     }
     194             : 
     195       80146 :     static inline sal_Int32 extractFormulaLanguage( const Grammar eGrammar )
     196             :     {
     197       80146 :         return eGrammar & kFlagMask;
     198             :     }
     199             : 
     200       94245 :     static inline AddressConvention extractRefConvention( const Grammar eGrammar )
     201             :     {
     202             :         return static_cast<AddressConvention>(
     203       94245 :                 ((eGrammar & ~kEnglishBit) >> kConventionShift) -
     204       94245 :                 kConventionOffset);
     205             :     }
     206             : 
     207       39210 :     static inline Grammar setEnglishBit( const Grammar eGrammar, const bool bEnglish )
     208             :     {
     209       39210 :         if (bEnglish)
     210        4010 :             return static_cast<Grammar>( eGrammar | kEnglishBit);
     211             :         else
     212       35200 :             return static_cast<Grammar>( eGrammar & ~kEnglishBit);
     213             :     }
     214             : 
     215       39183 :     static inline Grammar mergeToGrammar( const Grammar eGrammar, const AddressConvention eConv )
     216             :     {
     217       39183 :         bool bEnglish = isEnglish( eGrammar);
     218             :         Grammar eGram = static_cast<Grammar>(
     219       39183 :                 extractFormulaLanguage( eGrammar) |
     220       39183 :                 ((eConv + kConventionOffset) << kConventionShift));
     221       39183 :         eGram = setEnglishBit( eGram, bEnglish);
     222             :         DBG_ASSERT( isSupported( eGram), "CompilerGrammarMap::mergeToGrammar: unsupported grammar");
     223       39183 :         return eGram;
     224             :     }
     225             : 
     226             :     /// If grammar is of ODF 1.1
     227        3763 :     static inline bool isPODF( const Grammar eGrammar )
     228             :     {
     229        3763 :         return extractFormulaLanguage( eGrammar) ==
     230        3763 :             ::com::sun::star::sheet::FormulaLanguage::ODF_11;
     231             :     }
     232             : 
     233             :     /// If grammar is of ODFF
     234        4363 :     static inline bool isODFF( const Grammar eGrammar )
     235             :     {
     236        4363 :         return extractFormulaLanguage( eGrammar) ==
     237        4363 :             ::com::sun::star::sheet::FormulaLanguage::ODFF;
     238             :     }
     239             : 
     240             : };
     241             : // =============================================================================
     242             : } // formula
     243             : // =============================================================================
     244             : 
     245             : #endif // FORMULA_GRAMMAR_HXX
     246             : 
     247             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10