tdf#150927: properly handle nesting in tables
[LibreOffice.git] / starmath / inc / token.hxx
blobdf026efbf30e8c23e4cb58109f636212bce156ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 /** The tokens contain the information gathered by the parser.
22 * They contain:
23 * the data type (~ mathematical operation).
24 * The mathematical char.
25 * The corresponding code or information to recreate it.
26 * Location of the token in the starmath code.
29 #pragma once
31 #include <tools/color.hxx>
32 #include <o3tl/string_view.hxx>
33 #include <o3tl/typed_flags_set.hxx>
35 // std imports
36 #include <memory>
37 #include <utility>
39 // TokenGroups
40 enum class TG
42 NONE = 0x000000,
43 Oper = 0x000001,
44 Relation = 0x000002,
45 Sum = 0x000004,
46 Product = 0x000008,
47 UnOper = 0x000010,
48 Power = 0x000020,
49 Attribute = 0x000040,
50 Align = 0x000080,
51 Function = 0x000100,
52 Blank = 0x000200,
53 LBrace = 0x000400,
54 RBrace = 0x000800,
55 Color = 0x001000,
56 Font = 0x002000,
57 Standalone = 0x004000,
58 Limit = 0x010000,
59 FontAttr = 0x020000
62 namespace o3tl
64 template <> struct typed_flags<TG> : is_typed_flags<TG, 0x037fff>
69 // Tokens identifiers. Allow to know what kind of information the node contains.
70 enum SmTokenType
72 // clang-format off
73 // Uncategorized
74 TEND, TSPECIAL, TNONE, TESCAPE, TUNKNOWN,
75 TBLANK, TSBLANK, TPLACE, TNOSPACE, TDOTSDOWN,
76 TNEWLINE, TDOTSAXIS, TDOTSLOW, TDOTSVERT, TBACKEPSILON,
77 TDOTSDIAG, TDOTSUP, TERROR,
78 // Basic
79 TPLUS, TMINUS, TMULTIPLY, TDIVIDEBY, // +-*/
80 TGT, TLT, TGE, TLE, // > < >= <=
81 TASSIGN, TNEQ, TGG, TLL, // = != >>> <<<
82 TPARALLEL, TORTHO, TEQUIV, // Geometry
83 TOPER, TSUM, TPROD, TCOPROD, // Operators
84 TIM, TRE, THBAR, TLAMBDABAR, // Complex and constants
85 TPLUSMINUS, TMINUSPLUS, TSIM, TSIMEQ, // +- -+ ~ ~=
86 TLIM, TLIMSUP, TLIMINF, TTOWARD, // Limits
87 TOVER, TTIMES, TCDOT, TDIV, // Product type
88 TSLASH, TBACKSLASH, TWIDESLASH, TWIDEBACKSLASH, //Slash
89 TFRAC, TIT, // mathml related
90 // Structure
91 TMATRIX, TPOUND, TDPOUND, TSTACK, TBINOM,
92 // Logic
93 TAND, TOR, TNEG, // && || !
94 TPRECEDES, TSUCCEEDS, TNOTPRECEDES, TNOTSUCCEEDS, // Order
95 TPRECEDESEQUAL, TSUCCEEDSEQUAL, TPRECEDESEQUIV, TSUCCEEDSEQUIV, // Order eq
96 TLEFTARROW, TRIGHTARROW, TUPARROW, TDOWNARROW, // Arrows
97 TDRARROW, TDLARROW, TDLRARROW, TDEF, // Double arrows, definition
98 TPROP, TNDIVIDES, TDIVIDES, TAPPROX, // Proportions, approximation
99 TLESLANT, TGESLANT, TTRANSL, TTRANSR, // <= >= corresponds
100 // Tensors
101 TOPLUS, TOMINUS, TOTIMES, TODIVIDE, TODOT,
102 TCIRC,
103 // Positions
104 TRSUB, TRSUP, TCSUB, TCSUP, TLSUB,
105 TLSUP, TFROM, TTO, TUOPER, TBOPER,
106 // Set theory
107 TSETN, TSETZ, TSETQ, TSETR, TSETC,
108 TIN, TNOTIN, TNI, TEMPTYSET, // Insideout
109 TSUBSET, TSUBSETEQ, TSUPSET, TSUPSETEQ, // Subsupset
110 TNSUBSET, TNSUPSET, TNSUBSETEQ, TNSUPSETEQ, // Not subsupset
111 TINTERSECT, TUNION, TSETMINUS, TSETQUOTIENT, // +-/
112 TALEPH, TWP, TINFINITY, // Abstract sets
113 TFORALL, TEXISTS, TNOTEXISTS, // Existential
114 // Font
115 TFONT, TSIZE, TCOLOR, TPHANTOM, // Basic
116 TITALIC, TNITALIC, TBOLD, TNBOLD, // Bold ital
117 TALIGNL, TALIGNC, TALIGNR, // Align
118 TUNDERLINE, TOVERLINE, TOVERSTRIKE, TBAR, // Lines
119 TFIXED, TSANS, TSERIF, // Types
120 TACUTE, TGRAVE, THAT, TBREVE, // Accents
121 TWIDEVEC, TWIDEHARPOON, TWIDETILDE, TWIDEHAT, // Wide math
122 TVEC, THARPOON, TTILDE, TCIRCLE, // math
123 TCHECK,
124 TTEXT, TNUMBER, TCHARACTER, TIDENT, // Content type
125 // Brackets
126 TLEFT, TRIGHT, TUNDERBRACE, TOVERBRACE, // Scalable, upsidedown
127 TLGROUP, TRGROUP, TLPARENT, TRPARENT, // Structural
128 TLBRACKET, TRBRACKET, TLDBRACKET, TRDBRACKET, // Bracket x1 & x2
129 TLCEIL, TRCEIL, TLFLOOR, TRFLOOR, // Reals -> Wholes
130 TLANGLE, TRANGLE, TLBRACE, TRBRACE, // <x> {x}
131 TLLINE, TRLINE, TLDLINE, TRDLINE, // Lines x1 x2
132 TMLINE, TEVALUATE, TLRLINE, TLRDLINE, // Custom
133 // Differential calculus
134 TNABLA, TPARTIAL, TFOURIER, TLAPLACE, // Derivative, Transformation
135 TINTD, TINT, TIINT, TIIINT, // Integral
136 TLINT, TLLINT, TLLLINT, // Circuit integral
137 TDOT, TDDOT, TDDDOT, // Derivative dots
138 // Function
139 TFUNC, TLN, TLOG, TEXP, // Exp - Log
140 TSIN, TCOS, TTAN, TCOT, // Trigo
141 TSINH, TCOSH, TTANH, TCOTH, // Trigo hyperbolic
142 TASIN, TACOS, TATAN, TACOT, // Arctrigo
143 TASINH, TACOSH, TATANH, TACOTH, // Arctrigo hyperbolic
144 TSQRT, TNROOT, TFACT, TABS, // roots, n! |z|
145 // Color
146 TRGB, TRGBA, THEX, THTMLCOL, TDVIPSNAMESCOL,
147 TICONICCOL, TMATHMLCOL
148 // clang-format on
151 struct SmTokenTableEntry
153 OUString aIdent;
154 SmTokenType eType;
155 sal_Unicode cMathChar;
156 TG nGroup;
157 sal_uInt16 nLevel;
160 struct SmColorTokenTableEntry
162 OUString aIdent;
163 SmTokenType eType;
164 Color cColor;
166 SmColorTokenTableEntry()
167 : eType(TERROR)
168 , cColor()
172 SmColorTokenTableEntry(const SmColorTokenTableEntry* amColorTokenTableEntry)
173 : aIdent(amColorTokenTableEntry->aIdent)
174 , eType(amColorTokenTableEntry->eType)
175 , cColor(amColorTokenTableEntry->cColor)
179 SmColorTokenTableEntry(const std::unique_ptr<SmColorTokenTableEntry> amColorTokenTableEntry)
180 : aIdent(amColorTokenTableEntry->aIdent)
181 , eType(amColorTokenTableEntry->eType)
182 , cColor(amColorTokenTableEntry->cColor)
186 SmColorTokenTableEntry(OUString name, SmTokenType ctype, Color ncolor)
187 : aIdent(std::move(name))
188 , eType(ctype)
189 , cColor(ncolor)
193 SmColorTokenTableEntry(OUString name, SmTokenType ctype, sal_uInt32 ncolor)
194 : aIdent(std::move(name))
195 , eType(ctype)
196 , cColor(ColorTransparency, ncolor)
200 bool equals(std::u16string_view colorname) const
202 return o3tl::compareToIgnoreAsciiCase(colorname, aIdent) == 0;
205 bool equals(sal_uInt32 colorcode) const { return colorcode == static_cast<sal_uInt32>(cColor); }
207 bool equals(Color colorcode) const { return colorcode == cColor; }
210 struct SmToken
212 OUString aText; // token text
213 SmTokenType eType; // token info
214 OUString cMathChar;
216 // parse-help info
217 TG nGroup;
218 sal_uInt16 nLevel;
220 SmToken()
221 : eType(TUNKNOWN)
222 , cMathChar('\0')
223 , nGroup(TG::NONE)
224 , nLevel(0)
228 SmToken(SmTokenType eTokenType, sal_Unicode cMath, OUString rText, TG nTokenGroup = TG::NONE,
229 sal_uInt16 nTokenLevel = 0)
230 : aText(std::move(rText))
231 , eType(eTokenType)
232 , cMathChar(cMath)
233 , nGroup(nTokenGroup)
234 , nLevel(nTokenLevel)
238 void operator=(const SmTokenTableEntry& aTokenTableEntry)
240 aText = aTokenTableEntry.aIdent;
241 eType = aTokenTableEntry.eType;
242 cMathChar = OUString(&aTokenTableEntry.cMathChar, 1);
243 nGroup = aTokenTableEntry.nGroup;
244 nLevel = aTokenTableEntry.nLevel;
247 void operator=(const SmTokenTableEntry* aTokenTableEntry)
249 aText = aTokenTableEntry->aIdent;
250 eType = aTokenTableEntry->eType;
251 cMathChar = OUString(&aTokenTableEntry->cMathChar, 1);
252 nGroup = aTokenTableEntry->nGroup;
253 nLevel = aTokenTableEntry->nLevel;
256 void operator=(const SmColorTokenTableEntry& aTokenTableEntry)
258 aText = u"";
259 eType = aTokenTableEntry.eType;
260 cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry.cColor), 16);
261 nGroup = TG::Color;
262 nLevel = 0;
265 void operator=(const SmColorTokenTableEntry* aTokenTableEntry)
267 aText = u"";
268 eType = aTokenTableEntry->eType;
269 cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16);
270 nGroup = TG::Color;
271 nLevel = 0;
274 void operator=(const std::unique_ptr<SmColorTokenTableEntry>& aTokenTableEntry)
276 aText = u"";
277 eType = aTokenTableEntry->eType;
278 cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16);
279 nGroup = TG::Color;
280 nLevel = 0;
283 void setChar(sal_Unicode cChar) { cMathChar = OUString(&cChar, 1); }
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */