Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/starmath/inc/token.hxx
Line
Count
Source
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,       TWIDELVEC,      TWIDEHARPOON,   TWIDELHARPOON,
123
    TWIDETILDE,     TWIDEHAT,                                       // Wide math
124
    TVEC,           TLVEC,          THARPOON,       TLHARPOON,
125
    TTILDE,         TCIRCLE,                                        // math
126
    TCHECK,
127
    TTEXT,          TNUMBER,        TCHARACTER,     TIDENT,         // Content type
128
    // Brackets
129
    TLEFT,          TRIGHT,         TUNDERBRACE,    TOVERBRACE,     // Scalable, upsidedown
130
    TLGROUP,        TRGROUP,        TLPARENT,       TRPARENT,       // Structural
131
    TLBRACKET,      TRBRACKET,      TLDBRACKET,     TRDBRACKET,     // Bracket x1 & x2
132
    TLCEIL,         TRCEIL,         TLFLOOR,        TRFLOOR,        // Reals -> Wholes
133
    TLANGLE,        TRANGLE,        TLBRACE,        TRBRACE,        // <x> {x}
134
    TLLINE,         TRLINE,         TLDLINE,        TRDLINE,        // Lines x1 x2
135
    TMLINE,         TEVALUATE,      TLRLINE,        TLRDLINE,       // Custom
136
    // Differential calculus
137
    TNABLA,         TPARTIAL,       TFOURIER,       TLAPLACE,       // Derivative, Transformation
138
    TINTD,          TINT,           TIINT,          TIIINT,         // Integral
139
    TLINT,          TLLINT,         TLLLINT,                        // Circuit integral
140
    TDOT,           TDDOT,          TDDDOT,                         // Derivative dots
141
    // Function
142
    TFUNC,          TLN,            TLOG,           TEXP,           // Exp - Log
143
    TSIN,           TCOS,           TTAN,           TCOT,           // Trigo
144
    TSEC,           TCSC,           TSECH,          TCSCH,
145
    TSINH,          TCOSH,          TTANH,          TCOTH,          // Trigo hyperbolic
146
    TASIN,          TACOS,          TATAN,          TACOT,          // Arctrigo
147
    TASINH,         TACOSH,         TATANH,         TACOTH,         // Arctrigo hyperbolic
148
    TSQRT,          TNROOT,         TFACT,          TABS,           // roots, n! |z|
149
    // Color
150
    TRGB,           TRGBA,          THEX,           THTMLCOL,       TDVIPSNAMESCOL,
151
    TICONICCOL,     TMATHMLCOL
152
    // clang-format on
153
};
154
155
struct SmTokenTableEntry
156
{
157
    OUString aIdent;
158
    SmTokenType eType;
159
    sal_uInt32 cMathChar;
160
    TG nGroup;
161
    sal_uInt16 nLevel;
162
};
163
164
struct SmColorTokenTableEntry
165
{
166
    OUString aIdent;
167
    SmTokenType eType;
168
    Color cColor;
169
170
    SmColorTokenTableEntry()
171
128
        : eType(TERROR)
172
128
        , cColor()
173
128
    {
174
128
    }
175
176
    explicit SmColorTokenTableEntry(const SmColorTokenTableEntry* amColorTokenTableEntry)
177
0
        : aIdent(amColorTokenTableEntry->aIdent)
178
0
        , eType(amColorTokenTableEntry->eType)
179
0
        , cColor(amColorTokenTableEntry->cColor)
180
0
    {
181
0
    }
182
183
    explicit SmColorTokenTableEntry(
184
        const std::unique_ptr<SmColorTokenTableEntry> amColorTokenTableEntry)
185
        : aIdent(amColorTokenTableEntry->aIdent)
186
        , eType(amColorTokenTableEntry->eType)
187
        , cColor(amColorTokenTableEntry->cColor)
188
0
    {
189
0
    }
190
191
    SmColorTokenTableEntry(OUString name, SmTokenType ctype, Color ncolor)
192
10.4k
        : aIdent(std::move(name))
193
10.4k
        , eType(ctype)
194
10.4k
        , cColor(ncolor)
195
10.4k
    {
196
10.4k
    }
197
198
    SmColorTokenTableEntry(OUString name, SmTokenType ctype, sal_uInt32 ncolor)
199
0
        : aIdent(std::move(name))
200
0
        , eType(ctype)
201
0
        , cColor(ColorTransparency, ncolor)
202
0
    {
203
0
    }
204
205
    bool equals(std::u16string_view colorname) const
206
0
    {
207
0
        return o3tl::compareToIgnoreAsciiCase(colorname, aIdent) == 0;
208
0
    }
209
210
0
    bool equals(sal_uInt32 colorcode) const { return colorcode == static_cast<sal_uInt32>(cColor); }
211
212
0
    bool equals(Color colorcode) const { return colorcode == cColor; }
213
};
214
215
struct SmToken
216
{
217
    OUString aText; // token text
218
    SmTokenType eType; // token info
219
    OUString cMathChar;
220
221
    // parse-help info
222
    TG nGroup;
223
    sal_uInt16 nLevel;
224
225
    SmToken()
226
208k
        : eType(TUNKNOWN)
227
208k
        , cMathChar('\0')
228
208k
        , nGroup(TG::NONE)
229
208k
        , nLevel(0)
230
208k
    {
231
208k
    }
232
233
    SmToken(SmTokenType eTokenType, sal_uInt32 cMath, OUString rText, TG nTokenGroup = TG::NONE,
234
            sal_uInt16 nTokenLevel = 0)
235
53.4k
        : aText(std::move(rText))
236
53.4k
        , eType(eTokenType)
237
53.4k
        , cMathChar(&cMath, 1)
238
53.4k
        , nGroup(nTokenGroup)
239
53.4k
        , nLevel(nTokenLevel)
240
53.4k
    {
241
53.4k
    }
242
243
    void operator=(const SmTokenTableEntry& aTokenTableEntry)
244
0
    {
245
0
        aText = aTokenTableEntry.aIdent;
246
0
        eType = aTokenTableEntry.eType;
247
0
        cMathChar = OUString(&aTokenTableEntry.cMathChar, 1);
248
0
        nGroup = aTokenTableEntry.nGroup;
249
0
        nLevel = aTokenTableEntry.nLevel;
250
0
    }
251
252
    void operator=(const SmTokenTableEntry* aTokenTableEntry)
253
0
    {
254
0
        aText = aTokenTableEntry->aIdent;
255
0
        eType = aTokenTableEntry->eType;
256
0
        cMathChar = OUString(&aTokenTableEntry->cMathChar, 1);
257
0
        nGroup = aTokenTableEntry->nGroup;
258
0
        nLevel = aTokenTableEntry->nLevel;
259
0
    }
260
261
    void operator=(const SmColorTokenTableEntry& aTokenTableEntry)
262
0
    {
263
0
        aText = u""_ustr;
264
0
        eType = aTokenTableEntry.eType;
265
0
        cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry.cColor), 16);
266
0
        nGroup = TG::Color;
267
0
        nLevel = 0;
268
0
    }
269
270
    void operator=(const SmColorTokenTableEntry* aTokenTableEntry)
271
1.32k
    {
272
1.32k
        aText = u""_ustr;
273
1.32k
        eType = aTokenTableEntry->eType;
274
1.32k
        cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16);
275
1.32k
        nGroup = TG::Color;
276
1.32k
        nLevel = 0;
277
1.32k
    }
278
279
    void operator=(const std::unique_ptr<SmColorTokenTableEntry>& aTokenTableEntry)
280
0
    {
281
0
        aText = u""_ustr;
282
0
        eType = aTokenTableEntry->eType;
283
0
        cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16);
284
0
        nGroup = TG::Color;
285
0
        nLevel = 0;
286
0
    }
287
288
3.84M
    void setChar(sal_uInt32 cChar) { cMathChar = OUString(&cChar, 1); }
289
49.4k
    void setChar(const OUString& rText) { cMathChar = rText; }
290
};
291
292
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */