/src/libreoffice/starmath/inc/token.hxx
Line | Count | Source (jump to first uncovered line) |
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 | | |
20 | | /** The tokens contain the information gathered by the parser. |
21 | | * |
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. |
27 | | */ |
28 | | |
29 | | #pragma once |
30 | | |
31 | | #include <tools/color.hxx> |
32 | | #include <o3tl/string_view.hxx> |
33 | | #include <o3tl/typed_flags_set.hxx> |
34 | | |
35 | | // std imports |
36 | | #include <memory> |
37 | | #include <utility> |
38 | | |
39 | | // TokenGroups |
40 | | enum class TG |
41 | | { |
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 |
60 | | }; |
61 | | |
62 | | namespace o3tl |
63 | | { |
64 | | template <> struct typed_flags<TG> : is_typed_flags<TG, 0x037fff> |
65 | | { |
66 | | }; |
67 | | } |
68 | | |
69 | | // Tokens identifiers. Allow to know what kind of information the node contains. |
70 | | enum SmTokenType |
71 | | { |
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 | | THADD, TNAHA, |
88 | | TOVER, TTIMES, TCDOT, TDIV, // Product type |
89 | | TSLASH, TBACKSLASH, TWIDESLASH, TWIDEBACKSLASH, //Slash |
90 | | TFRAC, TIT, // mathml related |
91 | | // Structure |
92 | | TMATRIX, TPOUND, TDPOUND, TSTACK, TBINOM, |
93 | | // Logic |
94 | | TAND, TOR, TNEG, // && || ! |
95 | | TPRECEDES, TSUCCEEDS, TNOTPRECEDES, TNOTSUCCEEDS, // Order |
96 | | TPRECEDESEQUAL, TSUCCEEDSEQUAL, TPRECEDESEQUIV, TSUCCEEDSEQUIV, // Order eq |
97 | | TLEFTARROW, TRIGHTARROW, TUPARROW, TDOWNARROW, // Arrows |
98 | | TDRARROW, TDLARROW, TDLRARROW, TDEF, // Double arrows, definition |
99 | | TPROP, TNDIVIDES, TDIVIDES, TAPPROX, // Proportions, approximation |
100 | | TLESLANT, TGESLANT, TTRANSL, TTRANSR, // <= >= corresponds |
101 | | // Tensors |
102 | | TOPLUS, TOMINUS, TOTIMES, TODIVIDE, TODOT, |
103 | | TCIRC, |
104 | | // Positions |
105 | | TRSUB, TRSUP, TCSUB, TCSUP, TLSUB, |
106 | | TLSUP, TFROM, TTO, TUOPER, TBOPER, |
107 | | // Set theory |
108 | | TSETN, TSETZ, TSETQ, TSETR, TSETC, |
109 | | TIN, TNOTIN, TNI, TEMPTYSET, // Insideout |
110 | | TSUBSET, TSUBSETEQ, TSUPSET, TSUPSETEQ, // Subsupset |
111 | | TNSUBSET, TNSUPSET, TNSUBSETEQ, TNSUPSETEQ, // Not subsupset |
112 | | TINTERSECT, TUNION, TSETMINUS, TSETQUOTIENT, // +-/ |
113 | | TALEPH, TWP, TINFINITY, // Abstract sets |
114 | | TFORALL, TEXISTS, TNOTEXISTS, // Existential |
115 | | // Font |
116 | | TFONT, TSIZE, TCOLOR, TPHANTOM, // Basic |
117 | | TITALIC, TNITALIC, TBOLD, TNBOLD, // Bold ital |
118 | | TALIGNL, TALIGNC, TALIGNR, // Align |
119 | | TUNDERLINE, TOVERLINE, TOVERSTRIKE, TBAR, // Lines |
120 | | TFIXED, TSANS, TSERIF, // Types |
121 | | TACUTE, TGRAVE, THAT, TBREVE, // Accents |
122 | | TWIDEVEC, TWIDEHARPOON, TWIDETILDE, TWIDEHAT, // Wide math |
123 | | TVEC, THARPOON, TTILDE, TCIRCLE, // math |
124 | | TCHECK, |
125 | | TTEXT, TNUMBER, TCHARACTER, TIDENT, // Content type |
126 | | // Brackets |
127 | | TLEFT, TRIGHT, TUNDERBRACE, TOVERBRACE, // Scalable, upsidedown |
128 | | TLGROUP, TRGROUP, TLPARENT, TRPARENT, // Structural |
129 | | TLBRACKET, TRBRACKET, TLDBRACKET, TRDBRACKET, // Bracket x1 & x2 |
130 | | TLCEIL, TRCEIL, TLFLOOR, TRFLOOR, // Reals -> Wholes |
131 | | TLANGLE, TRANGLE, TLBRACE, TRBRACE, // <x> {x} |
132 | | TLLINE, TRLINE, TLDLINE, TRDLINE, // Lines x1 x2 |
133 | | TMLINE, TEVALUATE, TLRLINE, TLRDLINE, // Custom |
134 | | // Differential calculus |
135 | | TNABLA, TPARTIAL, TFOURIER, TLAPLACE, // Derivative, Transformation |
136 | | TINTD, TINT, TIINT, TIIINT, // Integral |
137 | | TLINT, TLLINT, TLLLINT, // Circuit integral |
138 | | TDOT, TDDOT, TDDDOT, // Derivative dots |
139 | | // Function |
140 | | TFUNC, TLN, TLOG, TEXP, // Exp - Log |
141 | | TSIN, TCOS, TTAN, TCOT, // Trigo |
142 | | TSEC, TCSC, TSECH, TCSCH, |
143 | | TSINH, TCOSH, TTANH, TCOTH, // Trigo hyperbolic |
144 | | TASIN, TACOS, TATAN, TACOT, // Arctrigo |
145 | | TASINH, TACOSH, TATANH, TACOTH, // Arctrigo hyperbolic |
146 | | TSQRT, TNROOT, TFACT, TABS, // roots, n! |z| |
147 | | // Color |
148 | | TRGB, TRGBA, THEX, THTMLCOL, TDVIPSNAMESCOL, |
149 | | TICONICCOL, TMATHMLCOL |
150 | | // clang-format on |
151 | | }; |
152 | | |
153 | | struct SmTokenTableEntry |
154 | | { |
155 | | OUString aIdent; |
156 | | SmTokenType eType; |
157 | | sal_uInt32 cMathChar; |
158 | | TG nGroup; |
159 | | sal_uInt16 nLevel; |
160 | | }; |
161 | | |
162 | | struct SmColorTokenTableEntry |
163 | | { |
164 | | OUString aIdent; |
165 | | SmTokenType eType; |
166 | | Color cColor; |
167 | | |
168 | | SmColorTokenTableEntry() |
169 | 120 | : eType(TERROR) |
170 | 120 | , cColor() |
171 | 120 | { |
172 | 120 | } |
173 | | |
174 | | explicit SmColorTokenTableEntry(const SmColorTokenTableEntry* amColorTokenTableEntry) |
175 | 0 | : aIdent(amColorTokenTableEntry->aIdent) |
176 | 0 | , eType(amColorTokenTableEntry->eType) |
177 | 0 | , cColor(amColorTokenTableEntry->cColor) |
178 | 0 | { |
179 | 0 | } |
180 | | |
181 | | explicit SmColorTokenTableEntry( |
182 | | const std::unique_ptr<SmColorTokenTableEntry> amColorTokenTableEntry) |
183 | | : aIdent(amColorTokenTableEntry->aIdent) |
184 | | , eType(amColorTokenTableEntry->eType) |
185 | | , cColor(amColorTokenTableEntry->cColor) |
186 | 0 | { |
187 | 0 | } |
188 | | |
189 | | SmColorTokenTableEntry(OUString name, SmTokenType ctype, Color ncolor) |
190 | 9.75k | : aIdent(std::move(name)) |
191 | 9.75k | , eType(ctype) |
192 | 9.75k | , cColor(ncolor) |
193 | 9.75k | { |
194 | 9.75k | } |
195 | | |
196 | | SmColorTokenTableEntry(OUString name, SmTokenType ctype, sal_uInt32 ncolor) |
197 | 0 | : aIdent(std::move(name)) |
198 | 0 | , eType(ctype) |
199 | 0 | , cColor(ColorTransparency, ncolor) |
200 | 0 | { |
201 | 0 | } |
202 | | |
203 | | bool equals(std::u16string_view colorname) const |
204 | 0 | { |
205 | 0 | return o3tl::compareToIgnoreAsciiCase(colorname, aIdent) == 0; |
206 | 0 | } |
207 | | |
208 | 0 | bool equals(sal_uInt32 colorcode) const { return colorcode == static_cast<sal_uInt32>(cColor); } |
209 | | |
210 | 0 | bool equals(Color colorcode) const { return colorcode == cColor; } |
211 | | }; |
212 | | |
213 | | struct SmToken |
214 | | { |
215 | | OUString aText; // token text |
216 | | SmTokenType eType; // token info |
217 | | OUString cMathChar; |
218 | | |
219 | | // parse-help info |
220 | | TG nGroup; |
221 | | sal_uInt16 nLevel; |
222 | | |
223 | | SmToken() |
224 | 227k | : eType(TUNKNOWN) |
225 | 227k | , cMathChar('\0') |
226 | 227k | , nGroup(TG::NONE) |
227 | 227k | , nLevel(0) |
228 | 227k | { |
229 | 227k | } |
230 | | |
231 | | SmToken(SmTokenType eTokenType, sal_uInt32 cMath, OUString rText, TG nTokenGroup = TG::NONE, |
232 | | sal_uInt16 nTokenLevel = 0) |
233 | 47.1k | : aText(std::move(rText)) |
234 | 47.1k | , eType(eTokenType) |
235 | 47.1k | , cMathChar(&cMath, 1) |
236 | 47.1k | , nGroup(nTokenGroup) |
237 | 47.1k | , nLevel(nTokenLevel) |
238 | 47.1k | { |
239 | 47.1k | } |
240 | | |
241 | | void operator=(const SmTokenTableEntry& aTokenTableEntry) |
242 | 0 | { |
243 | 0 | aText = aTokenTableEntry.aIdent; |
244 | 0 | eType = aTokenTableEntry.eType; |
245 | 0 | cMathChar = OUString(&aTokenTableEntry.cMathChar, 1); |
246 | 0 | nGroup = aTokenTableEntry.nGroup; |
247 | 0 | nLevel = aTokenTableEntry.nLevel; |
248 | 0 | } |
249 | | |
250 | | void operator=(const SmTokenTableEntry* aTokenTableEntry) |
251 | 0 | { |
252 | 0 | aText = aTokenTableEntry->aIdent; |
253 | 0 | eType = aTokenTableEntry->eType; |
254 | 0 | cMathChar = OUString(&aTokenTableEntry->cMathChar, 1); |
255 | 0 | nGroup = aTokenTableEntry->nGroup; |
256 | 0 | nLevel = aTokenTableEntry->nLevel; |
257 | 0 | } |
258 | | |
259 | | void operator=(const SmColorTokenTableEntry& aTokenTableEntry) |
260 | 0 | { |
261 | 0 | aText = u""_ustr; |
262 | 0 | eType = aTokenTableEntry.eType; |
263 | 0 | cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry.cColor), 16); |
264 | 0 | nGroup = TG::Color; |
265 | 0 | nLevel = 0; |
266 | 0 | } |
267 | | |
268 | | void operator=(const SmColorTokenTableEntry* aTokenTableEntry) |
269 | 2.00k | { |
270 | 2.00k | aText = u""_ustr; |
271 | 2.00k | eType = aTokenTableEntry->eType; |
272 | 2.00k | cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16); |
273 | 2.00k | nGroup = TG::Color; |
274 | 2.00k | nLevel = 0; |
275 | 2.00k | } |
276 | | |
277 | | void operator=(const std::unique_ptr<SmColorTokenTableEntry>& aTokenTableEntry) |
278 | 0 | { |
279 | 0 | aText = u""_ustr; |
280 | 0 | eType = aTokenTableEntry->eType; |
281 | 0 | cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16); |
282 | 0 | nGroup = TG::Color; |
283 | 0 | nLevel = 0; |
284 | 0 | } |
285 | | |
286 | 4.17M | void setChar(sal_uInt32 cChar) { cMathChar = OUString(&cChar, 1); } |
287 | 44.9k | void setChar(const OUString& rText) { cMathChar = rText; } |
288 | | }; |
289 | | |
290 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |