1*f6a9d5caSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*f6a9d5caSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*f6a9d5caSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*f6a9d5caSAndrew Rist * distributed with this work for additional information 6*f6a9d5caSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*f6a9d5caSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*f6a9d5caSAndrew Rist * "License"); you may not use this file except in compliance 9*f6a9d5caSAndrew Rist * with the License. You may obtain a copy of the License at 10*f6a9d5caSAndrew Rist * 11*f6a9d5caSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*f6a9d5caSAndrew Rist * 13*f6a9d5caSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*f6a9d5caSAndrew Rist * software distributed under the License is distributed on an 15*f6a9d5caSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*f6a9d5caSAndrew Rist * KIND, either express or implied. See the License for the 17*f6a9d5caSAndrew Rist * specific language governing permissions and limitations 18*f6a9d5caSAndrew Rist * under the License. 19*f6a9d5caSAndrew Rist * 20*f6a9d5caSAndrew Rist *************************************************************/ 21*f6a9d5caSAndrew Rist 22*f6a9d5caSAndrew Rist 23cdf0e10cSrcweir #ifndef PARSE_HXX 24cdf0e10cSrcweir #define PARSE_HXX 25cdf0e10cSrcweir 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <vcl/svapp.hxx> 28cdf0e10cSrcweir #include <tools/stack.hxx> 29cdf0e10cSrcweir #include <tools/list.hxx> 30cdf0e10cSrcweir #include <tools/string.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <set> 33cdf0e10cSrcweir #include <stack> 34cdf0e10cSrcweir #include <list> 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include "types.hxx" 37cdf0e10cSrcweir 38cdf0e10cSrcweir class SmNode; 39cdf0e10cSrcweir class SmDocShell; 40cdf0e10cSrcweir 41cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////// 42cdf0e10cSrcweir 43cdf0e10cSrcweir // TokenGroups 44cdf0e10cSrcweir #define TGOPER 0x00000001 45cdf0e10cSrcweir #define TGRELATION 0x00000002 46cdf0e10cSrcweir #define TGSUM 0x00000004 47cdf0e10cSrcweir #define TGPRODUCT 0x00000008 48cdf0e10cSrcweir #define TGUNOPER 0x00000010 49cdf0e10cSrcweir #define TGPOWER 0x00000020 50cdf0e10cSrcweir #define TGATTRIBUT 0x00000040 51cdf0e10cSrcweir #define TGALIGN 0x00000080 52cdf0e10cSrcweir #define TGFUNCTION 0x00000100 53cdf0e10cSrcweir #define TGBLANK 0x00000200 54cdf0e10cSrcweir #define TGLBRACES 0x00000400 55cdf0e10cSrcweir #define TGRBRACES 0x00000800 56cdf0e10cSrcweir #define TGCOLOR 0x00001000 57cdf0e10cSrcweir #define TGFONT 0x00002000 58cdf0e10cSrcweir #define TGSTANDALONE 0x00004000 59cdf0e10cSrcweir #define TGDISCARDED 0x00008000 60cdf0e10cSrcweir #define TGLIMIT 0x00010000 61cdf0e10cSrcweir #define TGFONTATTR 0x00020000 62cdf0e10cSrcweir 63cdf0e10cSrcweir 64cdf0e10cSrcweir enum SmTokenType 65cdf0e10cSrcweir { 66cdf0e10cSrcweir TEND, TLGROUP, TRGROUP, TLPARENT, TRPARENT, 67cdf0e10cSrcweir TLBRACKET, TRBRACKET, TPLUS, TMINUS, TMULTIPLY, 68cdf0e10cSrcweir TDIVIDEBY, TASSIGN, TPOUND, TSPECIAL, TSLASH, 69cdf0e10cSrcweir TBACKSLASH, TBLANK, TSBLANK, TRSUB, TRSUP, 70cdf0e10cSrcweir TCSUB, TCSUP, TLSUB, TLSUP, TGT, 71cdf0e10cSrcweir TLT, TAND, TOR, TINTERSECT, TUNION, 72cdf0e10cSrcweir TNEWLINE, TBINOM, TFROM, TTO, TINT, 73cdf0e10cSrcweir TSUM, TOPER, TABS, TSQRT, TFACT, 74cdf0e10cSrcweir TNROOT, TOVER, TTIMES, TGE, TLE, 75cdf0e10cSrcweir TGG, TLL, TDOTSAXIS, TDOTSLOW, TDOTSVERT, 76cdf0e10cSrcweir TDOTSDIAG, TDOTSUP, TDOTSDOWN, TACUTE, TBAR, 77cdf0e10cSrcweir TBREVE, TCHECK, TCIRCLE, TDOT, TDDOT, 78cdf0e10cSrcweir TDDDOT, TGRAVE, THAT, TTILDE, TVEC, 79cdf0e10cSrcweir TUNDERLINE, TOVERLINE, TOVERSTRIKE, TITALIC, TNITALIC, 80cdf0e10cSrcweir TBOLD, TNBOLD, TPHANTOM, TFONT, TSIZE, 81cdf0e10cSrcweir TCOLOR, TALIGNL, TALIGNC, TALIGNR, TLEFT, 82cdf0e10cSrcweir TRIGHT, TLANGLE, TLBRACE, TLLINE, TLDLINE, 83cdf0e10cSrcweir TLCEIL, TLFLOOR, TNONE, TMLINE, TRANGLE, 84cdf0e10cSrcweir TRBRACE, TRLINE, TRDLINE, TRCEIL, TRFLOOR, 85cdf0e10cSrcweir TSIN, TCOS, TTAN, TCOT, TFUNC, 86cdf0e10cSrcweir TSTACK, TMATRIX, TMATFORM, TDPOUND, TPLACE, 87cdf0e10cSrcweir TTEXT, TNUMBER, TCHARACTER, TIDENT, TNEQ, 88cdf0e10cSrcweir TEQUIV, TDEF, TPROP, TSIM, TSIMEQ, 89cdf0e10cSrcweir TAPPROX, TPARALLEL, TORTHO, TIN, TNOTIN, 90cdf0e10cSrcweir TSUBSET, TSUBSETEQ, TSUPSET, TSUPSETEQ, TPLUSMINUS, 91cdf0e10cSrcweir TMINUSPLUS, TOPLUS, TOMINUS, TDIV, TOTIMES, 92cdf0e10cSrcweir TODIVIDE, TTRANSL, TTRANSR, TIINT, TIIINT, 93cdf0e10cSrcweir TLINT, TLLINT, TLLLINT, TPROD, TCOPROD, 94cdf0e10cSrcweir TFORALL, TEXISTS, TLIM, TNABLA, TTOWARD, 95cdf0e10cSrcweir TSINH, TCOSH, TTANH, TCOTH, TASIN, 96cdf0e10cSrcweir TACOS, TATAN, TLN, TLOG, TUOPER, 97cdf0e10cSrcweir TBOPER, TBLACK, TWHITE, TRED, TGREEN, 98cdf0e10cSrcweir TBLUE, TCYAN, TMAGENTA, TYELLOW, TFIXED, 99cdf0e10cSrcweir TSANS, TSERIF, TPOINT, TASINH, TACOSH, 100cdf0e10cSrcweir TATANH, TACOTH, TACOT, TEXP, TCDOT, 101cdf0e10cSrcweir TODOT, TLESLANT, TGESLANT, TNSUBSET, TNSUPSET, 102cdf0e10cSrcweir TNSUBSETEQ, TNSUPSETEQ, TPARTIAL, TNEG, TNI, 103cdf0e10cSrcweir TBACKEPSILON, TALEPH, TIM, TRE, TWP, 104cdf0e10cSrcweir TEMPTYSET, TINFINITY, TESCAPE, TLIMSUP, TLIMINF, 105cdf0e10cSrcweir TNDIVIDES, TDRARROW, TDLARROW, TDLRARROW, TUNDERBRACE, 106cdf0e10cSrcweir TOVERBRACE, TCIRC, TTOP, THBAR, TLAMBDABAR, 107cdf0e10cSrcweir TLEFTARROW, TRIGHTARROW, TUPARROW, TDOWNARROW, TDIVIDES, 108cdf0e10cSrcweir TNDIBVIDES, TSETN, TSETZ, TSETQ, TSETR, 109cdf0e10cSrcweir TSETC, TWIDEVEC, TWIDETILDE, TWIDEHAT, TWIDESLASH, 110cdf0e10cSrcweir TWIDEBACKSLASH, TLDBRACKET, TRDBRACKET, TNOSPACE, 111cdf0e10cSrcweir TUNKNOWN, TDEBUG 112cdf0e10cSrcweir }; 113cdf0e10cSrcweir 114cdf0e10cSrcweir 115cdf0e10cSrcweir struct SmToken 116cdf0e10cSrcweir { 117cdf0e10cSrcweir 118cdf0e10cSrcweir String aText; // token text 119cdf0e10cSrcweir SmTokenType eType; // token info 120cdf0e10cSrcweir sal_Unicode cMathChar; 121cdf0e10cSrcweir 122cdf0e10cSrcweir // parse-help info 123cdf0e10cSrcweir sal_uLong nGroup; 124cdf0e10cSrcweir sal_uInt16 nLevel; 125cdf0e10cSrcweir 126cdf0e10cSrcweir // token position 127cdf0e10cSrcweir sal_uInt16 nRow; 128cdf0e10cSrcweir xub_StrLen nCol; 129cdf0e10cSrcweir 130cdf0e10cSrcweir SmToken(); 131cdf0e10cSrcweir }; 132cdf0e10cSrcweir 133cdf0e10cSrcweir 134cdf0e10cSrcweir enum SmParseError 135cdf0e10cSrcweir { 136cdf0e10cSrcweir PE_NONE, PE_UNEXPECTED_END_OF_INPUT, 137cdf0e10cSrcweir PE_UNEXPECTED_CHAR, PE_UNEXPECTED_TOKEN, 138cdf0e10cSrcweir PE_FUNC_EXPECTED, PE_UNOPER_EXPECTED, 139cdf0e10cSrcweir PE_BINOPER_EXPECTED, PE_SYMBOL_EXPECTED, 140cdf0e10cSrcweir PE_IDENTIFIER_EXPECTED, PE_POUND_EXPECTED, 141cdf0e10cSrcweir PE_COLOR_EXPECTED, PE_LGROUP_EXPECTED, 142cdf0e10cSrcweir PE_RGROUP_EXPECTED, PE_LBRACE_EXPECTED, 143cdf0e10cSrcweir PE_RBRACE_EXPECTED, PE_PARENT_MISMATCH, 144cdf0e10cSrcweir PE_RIGHT_EXPECTED, PE_FONT_EXPECTED, 145cdf0e10cSrcweir PE_SIZE_EXPECTED, PE_DOUBLE_ALIGN, 146cdf0e10cSrcweir PE_DOUBLE_SUBSUPSCRIPT 147cdf0e10cSrcweir }; 148cdf0e10cSrcweir 149cdf0e10cSrcweir 150cdf0e10cSrcweir struct SmErrorDesc 151cdf0e10cSrcweir { 152cdf0e10cSrcweir SmParseError Type; 153cdf0e10cSrcweir SmNode *pNode; 154cdf0e10cSrcweir String Text; 155cdf0e10cSrcweir }; 156cdf0e10cSrcweir 157cdf0e10cSrcweir 158cdf0e10cSrcweir DECLARE_STACK(SmNodeStack, SmNode *) 159cdf0e10cSrcweir DECLARE_LIST(SmErrDescList, SmErrorDesc *) 160cdf0e10cSrcweir 161cdf0e10cSrcweir /**************************************************************************/ 162cdf0e10cSrcweir 163cdf0e10cSrcweir // defines possible conversions of the formula text from the format of 164cdf0e10cSrcweir // one release to the one of another. 165cdf0e10cSrcweir enum SmConvert 166cdf0e10cSrcweir { 167cdf0e10cSrcweir CONVERT_NONE, 168cdf0e10cSrcweir CONVERT_40_TO_50, 169cdf0e10cSrcweir CONVERT_50_TO_60, 170cdf0e10cSrcweir CONVERT_60_TO_50 171cdf0e10cSrcweir }; 172cdf0e10cSrcweir 173cdf0e10cSrcweir 174cdf0e10cSrcweir class SmParser 175cdf0e10cSrcweir { 176cdf0e10cSrcweir String m_aBufferString; 177cdf0e10cSrcweir SmToken m_aCurToken; 178cdf0e10cSrcweir SmNodeStack m_aNodeStack; 179cdf0e10cSrcweir SmErrDescList m_aErrDescList; 180cdf0e10cSrcweir int m_nCurError; 181cdf0e10cSrcweir LanguageType m_nLang; 182cdf0e10cSrcweir xub_StrLen m_nBufferIndex, 183cdf0e10cSrcweir m_nTokenIndex; 184cdf0e10cSrcweir sal_uInt16 m_Row, 185cdf0e10cSrcweir m_nColOff; 186cdf0e10cSrcweir SmConvert m_eConversion; 187cdf0e10cSrcweir sal_Bool m_bImportSymNames, 188cdf0e10cSrcweir m_bExportSymNames; 189cdf0e10cSrcweir 190cdf0e10cSrcweir // map of used symbols (used to reduce file size by exporting only actually used symbols) 191cdf0e10cSrcweir std::set< rtl::OUString > m_aUsedSymbols; 192cdf0e10cSrcweir 193cdf0e10cSrcweir // declare copy-constructor and assignment-operator private 194cdf0e10cSrcweir SmParser(const SmParser &); 195cdf0e10cSrcweir SmParser & operator = (const SmParser &); 196cdf0e10cSrcweir 197cdf0e10cSrcweir protected: 198cdf0e10cSrcweir #if OSL_DEBUG_LEVEL 199cdf0e10cSrcweir sal_Bool IsDelimiter( const String &rTxt, xub_StrLen nPos ); 200cdf0e10cSrcweir #endif 201cdf0e10cSrcweir void NextToken(); 202cdf0e10cSrcweir xub_StrLen GetTokenIndex() const { return m_nTokenIndex; } 203cdf0e10cSrcweir void Insert(const String &rText, sal_uInt16 nPos); 204cdf0e10cSrcweir void Replace( sal_uInt16 nPos, sal_uInt16 nLen, const String &rText ); 205cdf0e10cSrcweir 206cdf0e10cSrcweir inline sal_Bool TokenInGroup(sal_uLong nGroup); 207cdf0e10cSrcweir 208cdf0e10cSrcweir // grammar 209cdf0e10cSrcweir void Table(); 210cdf0e10cSrcweir void Line(); 211cdf0e10cSrcweir void Expression(); 212cdf0e10cSrcweir void Relation(); 213cdf0e10cSrcweir void Sum(); 214cdf0e10cSrcweir void Product(); 215cdf0e10cSrcweir void SubSup(sal_uLong nActiveGroup); 216cdf0e10cSrcweir void OpSubSup(); 217cdf0e10cSrcweir void Power(); 218cdf0e10cSrcweir void Blank(); 219cdf0e10cSrcweir void Term(); 220cdf0e10cSrcweir void Escape(); 221cdf0e10cSrcweir void Operator(); 222cdf0e10cSrcweir void Oper(); 223cdf0e10cSrcweir void UnOper(); 224cdf0e10cSrcweir void Align(); 225cdf0e10cSrcweir void FontAttribut(); 226cdf0e10cSrcweir void Attribut(); 227cdf0e10cSrcweir void Font(); 228cdf0e10cSrcweir void FontSize(); 229cdf0e10cSrcweir void Color(); 230cdf0e10cSrcweir void Brace(); 231cdf0e10cSrcweir void Bracebody(sal_Bool bIsLeftRight); 232cdf0e10cSrcweir void Function(); 233cdf0e10cSrcweir void Binom(); 234cdf0e10cSrcweir void Stack(); 235cdf0e10cSrcweir void Matrix(); 236cdf0e10cSrcweir void Special(); 237cdf0e10cSrcweir void GlyphSpecial(); 238cdf0e10cSrcweir // end of grammar 239cdf0e10cSrcweir 240cdf0e10cSrcweir LanguageType GetLanguage() const { return m_nLang; } 241cdf0e10cSrcweir void SetLanguage( LanguageType nNewLang ) { m_nLang = nNewLang; } 242cdf0e10cSrcweir 243cdf0e10cSrcweir void Error(SmParseError Error); 244cdf0e10cSrcweir 245cdf0e10cSrcweir void ClearUsedSymbols() { m_aUsedSymbols.clear(); } 246cdf0e10cSrcweir void AddToUsedSymbols( const String &rSymbolName ) { m_aUsedSymbols.insert( rSymbolName ); } 247cdf0e10cSrcweir 248cdf0e10cSrcweir public: 249cdf0e10cSrcweir SmParser(); 250cdf0e10cSrcweir 251cdf0e10cSrcweir SmNode *Parse(const String &rBuffer); 252cdf0e10cSrcweir 253cdf0e10cSrcweir const String & GetText() const { return m_aBufferString; }; 254cdf0e10cSrcweir 255cdf0e10cSrcweir SmConvert GetConversion() const { return m_eConversion; } 256cdf0e10cSrcweir void SetConversion(SmConvert eConv) { m_eConversion = eConv; } 257cdf0e10cSrcweir 258cdf0e10cSrcweir sal_Bool IsImportSymbolNames() const { return m_bImportSymNames; } 259cdf0e10cSrcweir void SetImportSymbolNames(sal_Bool bVal) { m_bImportSymNames = bVal; } 260cdf0e10cSrcweir sal_Bool IsExportSymbolNames() const { return m_bExportSymNames; } 261cdf0e10cSrcweir void SetExportSymbolNames(sal_Bool bVal) { m_bExportSymNames = bVal; } 262cdf0e10cSrcweir 263cdf0e10cSrcweir sal_uInt16 AddError(SmParseError Type, SmNode *pNode); 264cdf0e10cSrcweir 265cdf0e10cSrcweir const SmErrorDesc * NextError(); 266cdf0e10cSrcweir const SmErrorDesc * PrevError(); 267cdf0e10cSrcweir const SmErrorDesc * GetError(sal_uInt16 i = 0xFFFF); 268cdf0e10cSrcweir 269cdf0e10cSrcweir bool IsUsedSymbol( const String &rSymbolName ) const { return m_aUsedSymbols.find( rSymbolName ) != m_aUsedSymbols.end(); } 270cdf0e10cSrcweir std::set< rtl::OUString > GetUsedSymbols() const { return m_aUsedSymbols; } 271cdf0e10cSrcweir }; 272cdf0e10cSrcweir 273cdf0e10cSrcweir 274cdf0e10cSrcweir inline sal_Bool SmParser::TokenInGroup(sal_uLong nGroup) 275cdf0e10cSrcweir { 276cdf0e10cSrcweir return (m_aCurToken.nGroup & nGroup) ? sal_True : sal_False; 277cdf0e10cSrcweir } 278cdf0e10cSrcweir 279cdf0e10cSrcweir 280cdf0e10cSrcweir #endif 281cdf0e10cSrcweir 282