summaryrefslogtreecommitdiff
path: root/starmath/inc/parse.hxx
blob: cf4758fe8c13b3eeee97316351b9131b6e3267cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: parse.hxx,v $
 * $Revision: 1.10 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef PARSE_HXX
#define PARSE_HXX


#include <vcl/svapp.hxx>
#include <tools/stack.hxx>
#include <tools/list.hxx>
#include <tools/string.hxx>

#include "types.hxx"

class SmNode;
class SmDocShell;


// TokenGroups
#define TGOPER			0x00000001
#define TGRELATION		0x00000002
#define TGSUM			0x00000004
#define TGPRODUCT		0x00000008
#define TGUNOPER		0x00000010
#define TGPOWER 		0x00000020
#define TGATTRIBUT		0x00000040
#define TGALIGN 		0x00000080
#define TGFUNCTION		0x00000100
#define TGBLANK 		0x00000200
#define TGLBRACES		0x00000400
#define TGRBRACES		0x00000800
#define TGCOLOR 		0x00001000
#define TGFONT			0x00002000
#define TGSTANDALONE	0x00004000
#define TGDISCARDED		0x00008000
#define TGLIMIT			0x00010000
#define TGFONTATTR		0x00020000


enum SmTokenType
{
    TEND,			TLGROUP,		TRGROUP,		TLPARENT,		TRPARENT,
    TLBRACKET,		TRBRACKET,		TPLUS,			TMINUS,			TMULTIPLY,
    TDIVIDEBY,		TASSIGN,		TPOUND,			TSPECIAL,		TSLASH,
    TBACKSLASH,		TBLANK,			TSBLANK,		TRSUB,			TRSUP,
    TCSUB,			TCSUP,			TLSUB,			TLSUP,			TGT,
    TLT,			TAND,			TOR,			TINTERSECT,		TUNION,
    TNEWLINE,		TBINOM,			TFROM,			TTO,			TINT,
    TSUM,			TOPER,			TABS,			TSQRT,			TFACT,
    TNROOT,			TOVER,			TTIMES,			TGE,			TLE,
    TGG,			TLL,			TDOTSAXIS,		TDOTSLOW,		TDOTSVERT,
    TDOTSDIAG,		TDOTSUP,		TDOTSDOWN,		TACUTE,			TBAR,
    TBREVE,			TCHECK,			TCIRCLE,		TDOT,			TDDOT,
    TDDDOT,			TGRAVE,			THAT,			TTILDE,			TVEC,
    TUNDERLINE,		TOVERLINE,		TOVERSTRIKE,	TITALIC,		TNITALIC,
    TBOLD,			TNBOLD,			TPHANTOM,		TFONT,			TSIZE,
    TCOLOR,			TALIGNL,		TALIGNC,		TALIGNR,		TLEFT,
    TRIGHT,			TLANGLE,		TLBRACE,		TLLINE,			TLDLINE,
    TLCEIL,			TLFLOOR,		TNONE,			TMLINE,			TRANGLE,
    TRBRACE,		TRLINE,			TRDLINE,		TRCEIL,			TRFLOOR,
    TSIN,			TCOS,			TTAN,			TCOT,			TFUNC,
    TSTACK,			TMATRIX,		TMATFORM,		TDPOUND,		TPLACE,
    TTEXT,			TNUMBER,		TCHARACTER,		TIDENT,			TNEQ,
    TEQUIV,			TDEF,			TPROP,			TSIM,			TSIMEQ,
    TAPPROX,		TPARALLEL,		TORTHO,			TIN,			TNOTIN,
    TSUBSET,		TSUBSETEQ,		TSUPSET,		TSUPSETEQ,		TPLUSMINUS,
    TMINUSPLUS,		TOPLUS,			TOMINUS,		TDIV,			TOTIMES,
    TODIVIDE,		TTRANSL,		TTRANSR,		TIINT,			TIIINT,
    TLINT,			TLLINT,			TLLLINT,		TPROD,			TCOPROD,
    TFORALL,		TEXISTS,		TLIM,			TNABLA,			TTOWARD,
    TSINH,			TCOSH,			TTANH,			TCOTH,			TASIN,
    TACOS,			TATAN,			TLN,			TLOG,			TUOPER,
    TBOPER,			TBLACK,			TWHITE,			TRED,			TGREEN,
    TBLUE,			TCYAN,			TMAGENTA,		TYELLOW,		TFIXED,
    TSANS,			TSERIF,			TPOINT,			TASINH,			TACOSH,
    TATANH,			TACOTH,			TACOT,			TEXP,			TCDOT,
    TODOT,			TLESLANT,		TGESLANT,		TNSUBSET,		TNSUPSET,
    TNSUBSETEQ,		TNSUPSETEQ,		TPARTIAL,		TNEG,			TNI,
    TBACKEPSILON,	TALEPH,			TIM,			TRE,			TWP,
    TEMPTYSET,		TINFINITY,		TESCAPE,		TLIMSUP,		TLIMINF,
    TNDIVIDES,		TDRARROW,		TDLARROW,		TDLRARROW,		TUNDERBRACE,
    TOVERBRACE,		TCIRC,			TTOP,			THBAR,			TLAMBDABAR,
    TLEFTARROW,		TRIGHTARROW,	TUPARROW,		TDOWNARROW,		TDIVIDES,
    TNDIBVIDES,		TSETN,			TSETZ,			TSETQ,			TSETR,
    TSETC,			TWIDEVEC,		TWIDETILDE,		TWIDEHAT,		TWIDESLASH,
    TWIDEBACKSLASH, TLDBRACKET,		TRDBRACKET,
    TUNKNOWN,		TDEBUG
};


struct SmToken
{
    // token text
    String	aText;
    // token info
    SmTokenType eType;
    sal_Unicode		cMathChar;
    // parse-help info
    ULONG		nGroup;
    USHORT		nLevel;
    // token position
    USHORT      nRow;
    xub_StrLen  nCol;

    SmToken();
};


enum SmParseError
{
    PE_NONE,					PE_UNEXPECTED_END_OF_INPUT,
    PE_UNEXPECTED_CHAR,			PE_UNEXPECTED_TOKEN,
    PE_FUNC_EXPECTED,			PE_UNOPER_EXPECTED,
    PE_BINOPER_EXPECTED,		PE_SYMBOL_EXPECTED,
    PE_IDENTIFIER_EXPECTED,		PE_POUND_EXPECTED,
    PE_COLOR_EXPECTED,			PE_LGROUP_EXPECTED,
    PE_RGROUP_EXPECTED,			PE_LBRACE_EXPECTED,
    PE_RBRACE_EXPECTED,			PE_PARENT_MISMATCH,
    PE_RIGHT_EXPECTED,			PE_FONT_EXPECTED,
    PE_SIZE_EXPECTED,			PE_DOUBLE_ALIGN,
    PE_DOUBLE_SUBSUPSCRIPT
};


struct SmErrorDesc
{
    SmParseError  Type;
    SmNode		 *pNode;
    String		  Text;
};

DECLARE_STACK(SmNodeStack,  SmNode *)
DECLARE_LIST(SmErrDescList, SmErrorDesc *)

/**************************************************************************/

// defines possible conversions of the formula text from the format of
// one release to the one of another.
enum SmConvert
{
    CONVERT_NONE,
    CONVERT_40_TO_50,
    CONVERT_50_TO_60,
    CONVERT_60_TO_50
};


class SmParser
{
    String          BufferString;
    SmToken         CurToken;
    SmNodeStack     NodeStack;
    SmErrDescList   ErrDescList;
    int             CurError;
    LanguageType    nLang;
    xub_StrLen      BufferIndex,
                    nTokenIndex;
    USHORT          Row,
                    ColOff;
    SmConvert       eConversion;
    BOOL            bImportSymNames,
                    bExportSymNames;

    // declare copy-constructor and assignment-operator private
    SmParser(const SmParser &);
    SmParser & operator = (const SmParser &);

protected:
#if OSL_DEBUG_LEVEL
    BOOL			IsDelimiter( const String &rTxt, xub_StrLen nPos );
#endif
    void			NextToken();
    xub_StrLen		GetTokenIndex() const	{ return nTokenIndex; }
    void 			Insert(const String &rText, USHORT nPos);
    void            Replace( USHORT nPos, USHORT nLen, const String &rText );

    inline BOOL		TokenInGroup(ULONG nGroup);

    // grammar
    void	Table();
    void	Line();
    void	Expression();
    void	Relation();
    void	Sum();
    void	Product();
    void	SubSup(ULONG nActiveGroup);
    void	OpSubSup();
    void 	Power();
    void	Blank();
    void	Term();
    void	Escape();
    void	Operator();
    void	Oper();
    void	UnOper();
    void	Align();
    void	FontAttribut();
    void	Attribut();
    void	Font();
    void	FontSize();
    void	Color();
    void	Brace();
    void	Bracebody(BOOL bIsLeftRight);
    void	Function();
    void	Binom();
    void	Stack();
    void	Matrix();
    void	Special();
    void	GlyphSpecial();
    // end of grammar
    
    LanguageType    GetLanguage() const { return nLang; }
    void            SetLanguage( LanguageType nNewLang ) { nLang = nNewLang; }

    void	Error(SmParseError Error);

public:
                 SmParser();

    SmNode		*Parse(const String &rBuffer);

    const String & GetText() const { return BufferString; };

    SmConvert    GetConversion() const              { return eConversion; }
    void         SetConversion(SmConvert eConv)     { eConversion = eConv; }

    BOOL         IsImportSymbolNames() const        { return bImportSymNames; }
    void         SetImportSymbolNames(BOOL bVal)    { bImportSymNames = bVal; }
    BOOL         IsExportSymbolNames() const        { return bExportSymNames; }
    void         SetExportSymbolNames(BOOL bVal)    { bExportSymNames = bVal; }

    USHORT		 AddError(SmParseError Type, SmNode *pNode);

    const SmErrorDesc *	NextError();
    const SmErrorDesc *	PrevError();
    const SmErrorDesc *	GetError(USHORT i = 0xFFFF);
};


inline BOOL SmParser::TokenInGroup(ULONG nGroup)
{
    return (CurToken.nGroup & nGroup) ? TRUE : FALSE;
}


#endif