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