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