xref: /trunk/main/starmath/inc/parse.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1f6a9d5caSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f6a9d5caSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f6a9d5caSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f6a9d5caSAndrew Rist  * distributed with this work for additional information
6f6a9d5caSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f6a9d5caSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f6a9d5caSAndrew Rist  * "License"); you may not use this file except in compliance
9f6a9d5caSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11f6a9d5caSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13f6a9d5caSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f6a9d5caSAndrew Rist  * software distributed under the License is distributed on an
15f6a9d5caSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f6a9d5caSAndrew Rist  * KIND, either express or implied.  See the License for the
17f6a9d5caSAndrew Rist  * specific language governing permissions and limitations
18f6a9d5caSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20f6a9d5caSAndrew Rist  *************************************************************/
21f6a9d5caSAndrew Rist 
22f6a9d5caSAndrew 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,
98*8f2cf668SRegina Henschel     TBLUE,          TCYAN,          TMAGENTA,       TYELLOW,        TSILVER,
99*8f2cf668SRegina Henschel     TGRAY,          TMAROON,        TPURPLE,        TLIME,          TOLIVE,
100*8f2cf668SRegina Henschel     TNAVY,          TTEAL,          TAQUA,          TFUCHSIA,       TFIXED,
101cdf0e10cSrcweir     TSANS,          TSERIF,         TPOINT,         TASINH,         TACOSH,
102cdf0e10cSrcweir     TATANH,         TACOTH,         TACOT,          TEXP,           TCDOT,
103cdf0e10cSrcweir     TODOT,          TLESLANT,       TGESLANT,       TNSUBSET,       TNSUPSET,
104cdf0e10cSrcweir     TNSUBSETEQ,     TNSUPSETEQ,     TPARTIAL,       TNEG,           TNI,
105cdf0e10cSrcweir     TBACKEPSILON,   TALEPH,         TIM,            TRE,            TWP,
106cdf0e10cSrcweir     TEMPTYSET,      TINFINITY,      TESCAPE,        TLIMSUP,        TLIMINF,
107cdf0e10cSrcweir     TNDIVIDES,      TDRARROW,       TDLARROW,       TDLRARROW,      TUNDERBRACE,
108cdf0e10cSrcweir     TOVERBRACE,     TCIRC,          TTOP,           THBAR,          TLAMBDABAR,
109cdf0e10cSrcweir     TLEFTARROW,     TRIGHTARROW,    TUPARROW,       TDOWNARROW,     TDIVIDES,
110cdf0e10cSrcweir     TNDIBVIDES,     TSETN,          TSETZ,          TSETQ,          TSETR,
111cdf0e10cSrcweir     TSETC,          TWIDEVEC,       TWIDETILDE,     TWIDEHAT,       TWIDESLASH,
112cdf0e10cSrcweir     TWIDEBACKSLASH, TLDBRACKET,     TRDBRACKET,     TNOSPACE,
113cdf0e10cSrcweir     TUNKNOWN,       TDEBUG
114cdf0e10cSrcweir };
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir struct SmToken
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     String          aText;      // token text
121cdf0e10cSrcweir     SmTokenType     eType;      // token info
122cdf0e10cSrcweir     sal_Unicode     cMathChar;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     // parse-help info
125cdf0e10cSrcweir     sal_uLong       nGroup;
126cdf0e10cSrcweir     sal_uInt16      nLevel;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     // token position
129cdf0e10cSrcweir     sal_uInt16      nRow;
130cdf0e10cSrcweir     xub_StrLen      nCol;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     SmToken();
133cdf0e10cSrcweir };
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 
136cdf0e10cSrcweir enum SmParseError
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     PE_NONE,                    PE_UNEXPECTED_END_OF_INPUT,
139cdf0e10cSrcweir     PE_UNEXPECTED_CHAR,         PE_UNEXPECTED_TOKEN,
140cdf0e10cSrcweir     PE_FUNC_EXPECTED,           PE_UNOPER_EXPECTED,
141cdf0e10cSrcweir     PE_BINOPER_EXPECTED,        PE_SYMBOL_EXPECTED,
142cdf0e10cSrcweir     PE_IDENTIFIER_EXPECTED,     PE_POUND_EXPECTED,
143cdf0e10cSrcweir     PE_COLOR_EXPECTED,          PE_LGROUP_EXPECTED,
144cdf0e10cSrcweir     PE_RGROUP_EXPECTED,         PE_LBRACE_EXPECTED,
145cdf0e10cSrcweir     PE_RBRACE_EXPECTED,         PE_PARENT_MISMATCH,
146cdf0e10cSrcweir     PE_RIGHT_EXPECTED,          PE_FONT_EXPECTED,
147cdf0e10cSrcweir     PE_SIZE_EXPECTED,           PE_DOUBLE_ALIGN,
148cdf0e10cSrcweir     PE_DOUBLE_SUBSUPSCRIPT
149cdf0e10cSrcweir };
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 
152cdf0e10cSrcweir struct SmErrorDesc
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     SmParseError  Type;
155cdf0e10cSrcweir     SmNode       *pNode;
156cdf0e10cSrcweir     String        Text;
157cdf0e10cSrcweir };
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 
160cdf0e10cSrcweir DECLARE_STACK(SmNodeStack,  SmNode *)
161cdf0e10cSrcweir DECLARE_LIST(SmErrDescList, SmErrorDesc *)
162cdf0e10cSrcweir 
163cdf0e10cSrcweir /**************************************************************************/
164cdf0e10cSrcweir 
165cdf0e10cSrcweir // defines possible conversions of the formula text from the format of
166cdf0e10cSrcweir // one release to the one of another.
167cdf0e10cSrcweir enum SmConvert
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     CONVERT_NONE,
170cdf0e10cSrcweir     CONVERT_40_TO_50,
171cdf0e10cSrcweir     CONVERT_50_TO_60,
172cdf0e10cSrcweir     CONVERT_60_TO_50
173cdf0e10cSrcweir };
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir class SmParser
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     String          m_aBufferString;
179cdf0e10cSrcweir     SmToken         m_aCurToken;
180cdf0e10cSrcweir     SmNodeStack     m_aNodeStack;
181cdf0e10cSrcweir     SmErrDescList   m_aErrDescList;
182cdf0e10cSrcweir     int             m_nCurError;
183cdf0e10cSrcweir     LanguageType    m_nLang;
184cdf0e10cSrcweir     xub_StrLen      m_nBufferIndex,
185cdf0e10cSrcweir                     m_nTokenIndex;
186cdf0e10cSrcweir     sal_uInt16          m_Row,
187cdf0e10cSrcweir                     m_nColOff;
188cdf0e10cSrcweir     SmConvert       m_eConversion;
189cdf0e10cSrcweir     sal_Bool        m_bImportSymNames,
190cdf0e10cSrcweir                     m_bExportSymNames;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     // map of used symbols (used to reduce file size by exporting only actually used symbols)
193cdf0e10cSrcweir     std::set< rtl::OUString >   m_aUsedSymbols;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     // declare copy-constructor and assignment-operator private
196cdf0e10cSrcweir     SmParser(const SmParser &);
197cdf0e10cSrcweir     SmParser & operator = (const SmParser &);
198cdf0e10cSrcweir 
199cdf0e10cSrcweir protected:
200cdf0e10cSrcweir #if OSL_DEBUG_LEVEL
201cdf0e10cSrcweir     sal_Bool            IsDelimiter( const String &rTxt, xub_StrLen nPos );
202cdf0e10cSrcweir #endif
203cdf0e10cSrcweir     void            NextToken();
GetTokenIndex() const204cdf0e10cSrcweir     xub_StrLen      GetTokenIndex() const   { return m_nTokenIndex; }
205cdf0e10cSrcweir     void            Insert(const String &rText, sal_uInt16 nPos);
206cdf0e10cSrcweir     void            Replace( sal_uInt16 nPos, sal_uInt16 nLen, const String &rText );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     inline sal_Bool     TokenInGroup(sal_uLong nGroup);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     // grammar
211cdf0e10cSrcweir     void    Table();
212cdf0e10cSrcweir     void    Line();
213cdf0e10cSrcweir     void    Expression();
214cdf0e10cSrcweir     void    Relation();
215cdf0e10cSrcweir     void    Sum();
216cdf0e10cSrcweir     void    Product();
217cdf0e10cSrcweir     void    SubSup(sal_uLong nActiveGroup);
218cdf0e10cSrcweir     void    OpSubSup();
219cdf0e10cSrcweir     void    Power();
220cdf0e10cSrcweir     void    Blank();
221cdf0e10cSrcweir     void    Term();
222cdf0e10cSrcweir     void    Escape();
223cdf0e10cSrcweir     void    Operator();
224cdf0e10cSrcweir     void    Oper();
225cdf0e10cSrcweir     void    UnOper();
226cdf0e10cSrcweir     void    Align();
227cdf0e10cSrcweir     void    FontAttribut();
228cdf0e10cSrcweir     void    Attribut();
229cdf0e10cSrcweir     void    Font();
230cdf0e10cSrcweir     void    FontSize();
231cdf0e10cSrcweir     void    Color();
232cdf0e10cSrcweir     void    Brace();
233cdf0e10cSrcweir     void    Bracebody(sal_Bool bIsLeftRight);
234cdf0e10cSrcweir     void    Function();
235cdf0e10cSrcweir     void    Binom();
236cdf0e10cSrcweir     void    Stack();
237cdf0e10cSrcweir     void    Matrix();
238cdf0e10cSrcweir     void    Special();
239cdf0e10cSrcweir     void    GlyphSpecial();
240cdf0e10cSrcweir     // end of grammar
241cdf0e10cSrcweir 
GetLanguage() const242cdf0e10cSrcweir     LanguageType    GetLanguage() const { return m_nLang; }
SetLanguage(LanguageType nNewLang)243cdf0e10cSrcweir     void            SetLanguage( LanguageType nNewLang ) { m_nLang = nNewLang; }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     void    Error(SmParseError Error);
246cdf0e10cSrcweir 
ClearUsedSymbols()247cdf0e10cSrcweir     void    ClearUsedSymbols()                              { m_aUsedSymbols.clear(); }
AddToUsedSymbols(const String & rSymbolName)248cdf0e10cSrcweir     void    AddToUsedSymbols( const String &rSymbolName )   { m_aUsedSymbols.insert( rSymbolName ); }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir public:
251cdf0e10cSrcweir                  SmParser();
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     SmNode      *Parse(const String &rBuffer);
254cdf0e10cSrcweir 
GetText() const255cdf0e10cSrcweir     const String & GetText() const { return m_aBufferString; };
256cdf0e10cSrcweir 
GetConversion() const257cdf0e10cSrcweir     SmConvert    GetConversion() const              { return m_eConversion; }
SetConversion(SmConvert eConv)258cdf0e10cSrcweir     void         SetConversion(SmConvert eConv)     { m_eConversion = eConv; }
259cdf0e10cSrcweir 
IsImportSymbolNames() const260cdf0e10cSrcweir     sal_Bool     IsImportSymbolNames() const        { return m_bImportSymNames; }
SetImportSymbolNames(sal_Bool bVal)261cdf0e10cSrcweir     void         SetImportSymbolNames(sal_Bool bVal)    { m_bImportSymNames = bVal; }
IsExportSymbolNames() const262cdf0e10cSrcweir     sal_Bool     IsExportSymbolNames() const        { return m_bExportSymNames; }
SetExportSymbolNames(sal_Bool bVal)263cdf0e10cSrcweir     void         SetExportSymbolNames(sal_Bool bVal)    { m_bExportSymNames = bVal; }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     sal_uInt16       AddError(SmParseError Type, SmNode *pNode);
266cdf0e10cSrcweir 
267cdf0e10cSrcweir     const SmErrorDesc * NextError();
268cdf0e10cSrcweir     const SmErrorDesc * PrevError();
269cdf0e10cSrcweir     const SmErrorDesc * GetError(sal_uInt16 i = 0xFFFF);
270cdf0e10cSrcweir 
IsUsedSymbol(const String & rSymbolName) const271cdf0e10cSrcweir     bool    IsUsedSymbol( const String &rSymbolName ) const { return m_aUsedSymbols.find( rSymbolName ) != m_aUsedSymbols.end(); }
GetUsedSymbols() const272cdf0e10cSrcweir     std::set< rtl::OUString >   GetUsedSymbols() const      { return m_aUsedSymbols; }
273cdf0e10cSrcweir };
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 
TokenInGroup(sal_uLong nGroup)276cdf0e10cSrcweir inline sal_Bool SmParser::TokenInGroup(sal_uLong nGroup)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir     return (m_aCurToken.nGroup & nGroup) ? sal_True : sal_False;
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 
282cdf0e10cSrcweir #endif
283