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