xref: /trunk/main/starmath/inc/parse.hxx (revision 8f2cf668)
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,        TSILVER,
99     TGRAY,          TMAROON,        TPURPLE,        TLIME,          TOLIVE,
100     TNAVY,          TTEAL,          TAQUA,          TFUCHSIA,		TFIXED,
101 	TSANS,			TSERIF,			TPOINT,			TASINH,			TACOSH,
102 	TATANH,			TACOTH,			TACOT,			TEXP,			TCDOT,
103 	TODOT,			TLESLANT,		TGESLANT,		TNSUBSET,		TNSUPSET,
104 	TNSUBSETEQ,		TNSUPSETEQ,		TPARTIAL,		TNEG,			TNI,
105 	TBACKEPSILON,	TALEPH,			TIM,			TRE,			TWP,
106 	TEMPTYSET,		TINFINITY,		TESCAPE,		TLIMSUP,		TLIMINF,
107 	TNDIVIDES,		TDRARROW,		TDLARROW,		TDLRARROW,		TUNDERBRACE,
108 	TOVERBRACE,		TCIRC,			TTOP,			THBAR,			TLAMBDABAR,
109 	TLEFTARROW,		TRIGHTARROW,	TUPARROW,		TDOWNARROW,		TDIVIDES,
110 	TNDIBVIDES,		TSETN,			TSETZ,			TSETQ,			TSETR,
111 	TSETC,			TWIDEVEC,		TWIDETILDE,		TWIDEHAT,		TWIDESLASH,
112 	TWIDEBACKSLASH, TLDBRACKET,		TRDBRACKET,     TNOSPACE,
113 	TUNKNOWN,		TDEBUG
114 };
115 
116 
117 struct SmToken
118 {
119 
120     String          aText;      // token text
121     SmTokenType     eType;      // token info
122     sal_Unicode		cMathChar;
123 
124     // parse-help info
125     sal_uLong       nGroup;
126     sal_uInt16      nLevel;
127 
128     // token position
129     sal_uInt16      nRow;
130     xub_StrLen      nCol;
131 
132 	SmToken();
133 };
134 
135 
136 enum SmParseError
137 {
138 	PE_NONE,					PE_UNEXPECTED_END_OF_INPUT,
139 	PE_UNEXPECTED_CHAR,			PE_UNEXPECTED_TOKEN,
140 	PE_FUNC_EXPECTED,			PE_UNOPER_EXPECTED,
141 	PE_BINOPER_EXPECTED,		PE_SYMBOL_EXPECTED,
142 	PE_IDENTIFIER_EXPECTED,		PE_POUND_EXPECTED,
143 	PE_COLOR_EXPECTED,			PE_LGROUP_EXPECTED,
144 	PE_RGROUP_EXPECTED,			PE_LBRACE_EXPECTED,
145 	PE_RBRACE_EXPECTED,			PE_PARENT_MISMATCH,
146 	PE_RIGHT_EXPECTED,			PE_FONT_EXPECTED,
147 	PE_SIZE_EXPECTED,			PE_DOUBLE_ALIGN,
148 	PE_DOUBLE_SUBSUPSCRIPT
149 };
150 
151 
152 struct SmErrorDesc
153 {
154 	SmParseError  Type;
155 	SmNode		 *pNode;
156 	String		  Text;
157 };
158 
159 
160 DECLARE_STACK(SmNodeStack,  SmNode *)
161 DECLARE_LIST(SmErrDescList, SmErrorDesc *)
162 
163 /**************************************************************************/
164 
165 // defines possible conversions of the formula text from the format of
166 // one release to the one of another.
167 enum SmConvert
168 {
169     CONVERT_NONE,
170     CONVERT_40_TO_50,
171     CONVERT_50_TO_60,
172     CONVERT_60_TO_50
173 };
174 
175 
176 class SmParser
177 {
178     String          m_aBufferString;
179     SmToken         m_aCurToken;
180     SmNodeStack     m_aNodeStack;
181     SmErrDescList   m_aErrDescList;
182     int             m_nCurError;
183     LanguageType    m_nLang;
184     xub_StrLen      m_nBufferIndex,
185                     m_nTokenIndex;
186     sal_uInt16          m_Row,
187                     m_nColOff;
188     SmConvert       m_eConversion;
189     sal_Bool        m_bImportSymNames,
190                     m_bExportSymNames;
191 
192     // map of used symbols (used to reduce file size by exporting only actually used symbols)
193     std::set< rtl::OUString >   m_aUsedSymbols;
194 
195 	// declare copy-constructor and assignment-operator private
196 	SmParser(const SmParser &);
197 	SmParser & operator = (const SmParser &);
198 
199 protected:
200 #if OSL_DEBUG_LEVEL
201 	sal_Bool			IsDelimiter( const String &rTxt, xub_StrLen nPos );
202 #endif
203 	void			NextToken();
GetTokenIndex() const204     xub_StrLen      GetTokenIndex() const   { return m_nTokenIndex; }
205 	void 			Insert(const String &rText, sal_uInt16 nPos);
206     void            Replace( sal_uInt16 nPos, sal_uInt16 nLen, const String &rText );
207 
208 	inline sal_Bool		TokenInGroup(sal_uLong nGroup);
209 
210 	// grammar
211 	void	Table();
212 	void	Line();
213 	void	Expression();
214 	void	Relation();
215 	void	Sum();
216 	void	Product();
217 	void	SubSup(sal_uLong nActiveGroup);
218 	void	OpSubSup();
219 	void 	Power();
220 	void	Blank();
221 	void	Term();
222 	void	Escape();
223 	void	Operator();
224 	void	Oper();
225 	void	UnOper();
226 	void	Align();
227 	void	FontAttribut();
228 	void	Attribut();
229 	void	Font();
230 	void	FontSize();
231 	void	Color();
232 	void	Brace();
233 	void	Bracebody(sal_Bool bIsLeftRight);
234 	void	Function();
235 	void	Binom();
236 	void	Stack();
237 	void	Matrix();
238 	void	Special();
239 	void	GlyphSpecial();
240 	// end of grammar
241 
GetLanguage() const242     LanguageType    GetLanguage() const { return m_nLang; }
SetLanguage(LanguageType nNewLang)243     void            SetLanguage( LanguageType nNewLang ) { m_nLang = nNewLang; }
244 
245 	void	Error(SmParseError Error);
246 
ClearUsedSymbols()247     void    ClearUsedSymbols()                              { m_aUsedSymbols.clear(); }
AddToUsedSymbols(const String & rSymbolName)248     void    AddToUsedSymbols( const String &rSymbolName )   { m_aUsedSymbols.insert( rSymbolName ); }
249 
250 public:
251                  SmParser();
252 
253 	SmNode		*Parse(const String &rBuffer);
254 
GetText() const255     const String & GetText() const { return m_aBufferString; };
256 
GetConversion() const257     SmConvert    GetConversion() const              { return m_eConversion; }
SetConversion(SmConvert eConv)258     void         SetConversion(SmConvert eConv)     { m_eConversion = eConv; }
259 
IsImportSymbolNames() const260     sal_Bool     IsImportSymbolNames() const        { return m_bImportSymNames; }
SetImportSymbolNames(sal_Bool bVal)261     void         SetImportSymbolNames(sal_Bool bVal)    { m_bImportSymNames = bVal; }
IsExportSymbolNames() const262     sal_Bool     IsExportSymbolNames() const        { return m_bExportSymNames; }
SetExportSymbolNames(sal_Bool bVal)263     void         SetExportSymbolNames(sal_Bool bVal)    { m_bExportSymNames = bVal; }
264 
265 	sal_uInt16		 AddError(SmParseError Type, SmNode *pNode);
266 
267 	const SmErrorDesc *	NextError();
268 	const SmErrorDesc *	PrevError();
269     const SmErrorDesc * GetError(sal_uInt16 i = 0xFFFF);
270 
IsUsedSymbol(const String & rSymbolName) const271     bool    IsUsedSymbol( const String &rSymbolName ) const { return m_aUsedSymbols.find( rSymbolName ) != m_aUsedSymbols.end(); }
GetUsedSymbols() const272     std::set< rtl::OUString >   GetUsedSymbols() const      { return m_aUsedSymbols; }
273 };
274 
275 
TokenInGroup(sal_uLong nGroup)276 inline sal_Bool SmParser::TokenInGroup(sal_uLong nGroup)
277 {
278     return (m_aCurToken.nGroup & nGroup) ? sal_True : sal_False;
279 }
280 
281 
282 #endif
283 
284