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 : #ifndef INCLUDED_STARMATH_INC_PARSE_HXX
20 : #define INCLUDED_STARMATH_INC_PARSE_HXX
21 :
22 : #include <vcl/svapp.hxx>
23 :
24 : #include <set>
25 : #include <stack>
26 : #include <list>
27 :
28 : #include "types.hxx"
29 :
30 : #include <vector>
31 :
32 : class SmNode;
33 :
34 :
35 :
36 : // TokenGroups
37 : #define TGOPER 0x00000001
38 : #define TGRELATION 0x00000002
39 : #define TGSUM 0x00000004
40 : #define TGPRODUCT 0x00000008
41 : #define TGUNOPER 0x00000010
42 : #define TGPOWER 0x00000020
43 : #define TGATTRIBUT 0x00000040
44 : #define TGALIGN 0x00000080
45 : #define TGFUNCTION 0x00000100
46 : #define TGBLANK 0x00000200
47 : #define TGLBRACES 0x00000400
48 : #define TGRBRACES 0x00000800
49 : #define TGCOLOR 0x00001000
50 : #define TGFONT 0x00002000
51 : #define TGSTANDALONE 0x00004000
52 : #define TGDISCARDED 0x00008000
53 : #define TGLIMIT 0x00010000
54 : #define TGFONTATTR 0x00020000
55 :
56 :
57 : enum SmTokenType
58 : {
59 : /* 0*/ TEND, TLGROUP, TRGROUP, TLPARENT, TRPARENT,
60 : /* 5*/ TLBRACKET, TRBRACKET, TPLUS, TMINUS, TMULTIPLY,
61 : /* 10*/ TDIVIDEBY, TASSIGN, TPOUND, TSPECIAL, TSLASH,
62 : /* 15*/ TBACKSLASH, TBLANK, TSBLANK, TRSUB, TRSUP,
63 : /* 20*/ TCSUB, TCSUP, TLSUB, TLSUP, TGT,
64 : /* 25*/ TLT, TAND, TOR, TINTERSECT, TUNION,
65 : /* 30*/ TNEWLINE, TBINOM, TFROM, TTO, TINT,
66 : /* 35*/ TSUM, TOPER, TABS, TSQRT, TFACT,
67 : /* 40*/ TNROOT, TOVER, TTIMES, TGE, TLE,
68 : /* 45*/ TGG, TLL, TDOTSAXIS, TDOTSLOW, TDOTSVERT,
69 : /* 50*/ TDOTSDIAG, TDOTSUP, TDOTSDOWN, TACUTE, TBAR,
70 : /* 55*/ TBREVE, TCHECK, TCIRCLE, TDOT, TDDOT,
71 : /* 60*/ TDDDOT, TGRAVE, THAT, TTILDE, TVEC,
72 : /* 65*/ TUNDERLINE, TOVERLINE, TOVERSTRIKE, TITALIC, TNITALIC,
73 : /* 70*/ TBOLD, TNBOLD, TPHANTOM, TFONT, TSIZE,
74 : /* 75*/ TCOLOR, TALIGNL, TALIGNC, TALIGNR, TLEFT,
75 : /* 80*/ TRIGHT, TLANGLE, TLBRACE, TLLINE, TLDLINE,
76 : /* 85*/ TLCEIL, TLFLOOR, TNONE, TMLINE, TRANGLE,
77 : /* 90*/ TRBRACE, TRLINE, TRDLINE, TRCEIL, TRFLOOR,
78 : /* 95*/ TSIN, TCOS, TTAN, TCOT, TFUNC,
79 : /*100*/ TSTACK, TMATRIX, TMATFORM, TDPOUND, TPLACE,
80 : /*105*/ TTEXT, TNUMBER, TCHARACTER, TIDENT, TNEQ,
81 : /*110*/ TEQUIV, TDEF, TPROP, TSIM, TSIMEQ,
82 : /*115*/ TAPPROX, TPARALLEL, TORTHO, TIN, TNOTIN,
83 : /*120*/ TSUBSET, TSUBSETEQ, TSUPSET, TSUPSETEQ, TPLUSMINUS,
84 : /*125*/ TMINUSPLUS, TOPLUS, TOMINUS, TDIV, TOTIMES,
85 : /*130*/ TODIVIDE, TTRANSL, TTRANSR, TIINT, TIIINT,
86 : /*135*/ TLINT, TLLINT, TLLLINT, TPROD, TCOPROD,
87 : /*140*/ TFORALL, TEXISTS, TNOTEXISTS, TLIM, TNABLA,
88 : /*145*/ TTOWARD, TSINH, TCOSH, TTANH, TCOTH,
89 : /*150*/ TASIN, TACOS, TATAN, TLN, TLOG,
90 : /*155*/ TUOPER, TBOPER, TBLACK, TWHITE, TRED,
91 : /*160*/ TGREEN, TBLUE, TCYAN, TMAGENTA, TYELLOW,
92 : /*165*/ TFIXED, TSANS, TSERIF, TPOINT, TASINH,
93 : /*170*/ TACOSH, TATANH, TACOTH, TACOT, TEXP,
94 : /*175*/ TCDOT, TODOT, TLESLANT, TGESLANT, TNSUBSET,
95 : /*180*/ TNSUPSET, TNSUBSETEQ, TNSUPSETEQ, TPARTIAL, TNEG,
96 : /*185*/ TNI, TBACKEPSILON, TALEPH, TIM, TRE,
97 : /*190*/ TWP, TEMPTYSET, TINFINITY, TESCAPE, TLIMSUP,
98 : /*195*/ TLIMINF, TNDIVIDES, TDRARROW, TDLARROW, TDLRARROW,
99 : /*200*/ TUNDERBRACE, TOVERBRACE, TCIRC, TTOP, THBAR,
100 : /*205*/ TLAMBDABAR, TLEFTARROW, TRIGHTARROW, TUPARROW, TDOWNARROW,
101 : /*210*/ TDIVIDES, TNDIBVIDES, TSETN, TSETZ, TSETQ,
102 : /*215*/ TSETR, TSETC, TWIDEVEC, TWIDETILDE, TWIDEHAT,
103 : /*220*/ TWIDESLASH, TWIDEBACKSLASH, TLDBRACKET, TRDBRACKET, TNOSPACE,
104 : /*225*/ TUNKNOWN, TDEBUG, TPRECEDES, TSUCCEEDS, TPRECEDESEQUAL,
105 : /*230*/ TSUCCEEDSEQUAL, TPRECEDESEQUIV, TSUCCEEDSEQUIV, TNOTPRECEDES, TNOTSUCCEEDS,
106 : /*235*/ TINTD
107 : };
108 :
109 :
110 0 : struct SmToken
111 : {
112 :
113 : OUString aText; // token text
114 : SmTokenType eType; // token info
115 : sal_Unicode cMathChar;
116 :
117 : // parse-help info
118 : sal_uLong nGroup;
119 : sal_uInt16 nLevel;
120 :
121 : // token position
122 : sal_Int32 nRow;
123 : sal_Int32 nCol;
124 :
125 : SmToken();
126 : SmToken(SmTokenType eTokenType,
127 : sal_Unicode cMath,
128 : const sal_Char* pText,
129 : sal_uLong nTokenGroup = 0,
130 : sal_uInt16 nTokenLevel = 0);
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 0 : struct SmErrorDesc
151 : {
152 : SmParseError Type;
153 : SmNode *pNode;
154 : OUString Text;
155 : };
156 :
157 :
158 : typedef ::std::stack< SmNode* > SmNodeStack;
159 : typedef ::std::vector< SmErrorDesc* > SmErrDescList;
160 :
161 : /**************************************************************************/
162 :
163 : struct SmTokenTableEntry
164 : {
165 : const sal_Char* pIdent;
166 : SmTokenType eType;
167 : sal_Unicode cMathChar;
168 : sal_uLong nGroup;
169 : sal_uInt16 nLevel;
170 : };
171 :
172 0 : class SmParser
173 : {
174 : OUString m_aBufferString;
175 : SmToken m_aCurToken;
176 : SmNodeStack m_aNodeStack;
177 : SmErrDescList m_aErrDescList;
178 : int m_nCurError;
179 : LanguageType m_nLang;
180 : sal_Int32 m_nBufferIndex,
181 : m_nTokenIndex;
182 : sal_Int32 m_Row,
183 : m_nColOff;
184 : bool bImportSymNames,
185 : m_bExportSymNames;
186 :
187 : // map of used symbols (used to reduce file size by exporting only actually used symbols)
188 : std::set< OUString > m_aUsedSymbols;
189 :
190 : //! locale where '.' is decimal separator!
191 : ::com::sun::star::lang::Locale m_aDotLoc;
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 > 1
199 : bool IsDelimiter( const OUString &rTxt, sal_Int32 nPos );
200 : #endif
201 : void NextToken();
202 0 : sal_Int32 GetTokenIndex() const { return m_nTokenIndex; }
203 : void Replace( sal_Int32 nPos, sal_Int32 nLen, const OUString &rText );
204 :
205 : inline bool TokenInGroup( sal_uLong nGroup );
206 :
207 : // grammar
208 : void Table();
209 : void Line();
210 : void Expression();
211 : void Relation();
212 : void Sum();
213 : void Product();
214 : void SubSup(sal_uLong nActiveGroup);
215 : void OpSubSup();
216 : void Power();
217 : void Blank();
218 : void Term(bool bGroupNumberIdent);
219 : void Escape();
220 : void Operator();
221 : void Oper();
222 : void UnOper();
223 : void Align();
224 : void FontAttribut();
225 : void Attribut();
226 : void Font();
227 : void FontSize();
228 : void Color();
229 : void Brace();
230 : void Bracebody(bool bIsLeftRight);
231 : void Function();
232 : void Binom();
233 : void Stack();
234 : void Matrix();
235 : void Special();
236 : void GlyphSpecial();
237 : // end of grammar
238 :
239 : LanguageType GetLanguage() const { return m_nLang; }
240 0 : void SetLanguage( LanguageType nNewLang ) { m_nLang = nNewLang; }
241 :
242 : void Error(SmParseError Error);
243 :
244 0 : void ClearUsedSymbols() { m_aUsedSymbols.clear(); }
245 0 : void AddToUsedSymbols( const OUString &rSymbolName ) { m_aUsedSymbols.insert( rSymbolName ); }
246 :
247 : public:
248 : SmParser();
249 :
250 : /** Parse rBuffer to formula tree */
251 : SmNode *Parse(const OUString &rBuffer);
252 : /** Parse rBuffer to formula subtree that constitutes an expression */
253 : SmNode *ParseExpression(const OUString &rBuffer);
254 :
255 0 : const OUString & GetText() const { return m_aBufferString; };
256 :
257 0 : bool IsImportSymbolNames() const { return bImportSymNames; }
258 0 : void SetImportSymbolNames(bool bVal) { bImportSymNames = bVal; }
259 0 : bool IsExportSymbolNames() const { return m_bExportSymNames; }
260 0 : void SetExportSymbolNames(bool bVal) { m_bExportSymNames = bVal; }
261 :
262 : size_t AddError(SmParseError Type, SmNode *pNode);
263 : const SmErrorDesc* NextError();
264 : const SmErrorDesc* PrevError();
265 : const SmErrorDesc* GetError(size_t i = size_t(-1) );
266 : static const SmTokenTableEntry* GetTokenTableEntry( const OUString &rName );
267 : bool IsUsedSymbol( const OUString &rSymbolName ) const { return m_aUsedSymbols.find( rSymbolName ) != m_aUsedSymbols.end(); }
268 0 : std::set< OUString > GetUsedSymbols() const { return m_aUsedSymbols; }
269 : };
270 :
271 :
272 0 : inline bool SmParser::TokenInGroup( sal_uLong nGroup)
273 : {
274 0 : return (m_aCurToken.nGroup & nGroup) ? true : false;
275 : }
276 :
277 :
278 : #endif
279 :
280 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|