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 "types.hxx"
32 #include <rtl/ustrbuf.hxx>
33 #include <tools/color.hxx>
34 #include <o3tl/typed_flags_set.hxx>
35 
36 // std imports
37 #include <cassert>
38 #include <memory>
39 #include <vector>
40 #include <stack>
41 #include <tuple>
42 #include <list>
43 #include <set>
44 
45 // TokenGroups
46 enum class TG
47 {
48     NONE = 0x000000,
49     Oper = 0x000001,
50     Relation = 0x000002,
51     Sum = 0x000004,
52     Product = 0x000008,
53     UnOper = 0x000010,
54     Power = 0x000020,
55     Attribute = 0x000040,
56     Align = 0x000080,
57     Function = 0x000100,
58     Blank = 0x000200,
59     LBrace = 0x000400,
60     RBrace = 0x000800,
61     Color = 0x001000,
62     Font = 0x002000,
63     Standalone = 0x004000,
64     Limit = 0x010000,
65     FontAttr = 0x020000
66 };
67 
68 namespace o3tl
69 {
70 template <> struct typed_flags<TG> : is_typed_flags<TG, 0x037fff>
71 {
72 };
73 }
74 
75 // Tokens identifiers. Allow to know what kind of information the node contains.
76 enum SmTokenType
77 {
78     // clang-format off
79     // Uncategorized
80     TEND,           TSPECIAL,       TNONE,          TESCAPE,        TUNKNOWN,
81     TBLANK,         TSBLANK,        TPLACE,         TNOSPACE,       TDOTSDOWN,
82     TNEWLINE,       TDOTSAXIS,      TDOTSLOW,       TDOTSVERT,      TBACKEPSILON,
83     TDOTSDIAG,      TDOTSUP,        TERROR,
84     // Basic
85     TPLUS,          TMINUS,         TMULTIPLY,      TDIVIDEBY,      // +-*/
86     TGT,            TLT,            TGE,            TLE,            // > < >= <=
87     TASSIGN,        TNEQ,           TGG,            TLL,            // = != >>> <<<
88     TPARALLEL,      TORTHO,         TEQUIV,                         // Geometry
89     TOPER,          TSUM,           TPROD,          TCOPROD,        // Operators
90     TIM,            TRE,            THBAR,          TLAMBDABAR,     // Complex and constants
91     TPLUSMINUS,     TMINUSPLUS,     TSIM,           TSIMEQ,         // +- -+ ~ ~=
92     TLIM,           TLIMSUP,        TLIMINF,        TTOWARD,        // Limits
93     TOVER,          TTIMES,         TCDOT,          TDIV,           // Product type
94     TSLASH,         TBACKSLASH,     TWIDESLASH,     TWIDEBACKSLASH, //Slash
95     TFRAC,          TIT,                                            // mathml related
96     // Structure
97     TMATRIX,         TPOUND,        TDPOUND,        TSTACK,         TBINOM,
98     // Logic
99     TAND,           TOR,            TNEG,                           // && || !
100     TPRECEDES,      TSUCCEEDS,      TNOTPRECEDES,   TNOTSUCCEEDS,   // Order
101     TPRECEDESEQUAL, TSUCCEEDSEQUAL, TPRECEDESEQUIV, TSUCCEEDSEQUIV, // Order eq
102     TLEFTARROW,     TRIGHTARROW,    TUPARROW,       TDOWNARROW,     // Arrows
103     TDRARROW,       TDLARROW,       TDLRARROW,      TDEF,           // Double arrows, definition
104     TPROP,          TNDIVIDES,      TDIVIDES,       TAPPROX,        // Proportions, approximation
105     TLESLANT,       TGESLANT,       TTRANSL,        TTRANSR,        // <= >= corresponds
106     // Tensors
107     TOPLUS,         TOMINUS,        TOTIMES,        TODIVIDE,       TODOT,
108     TCIRC,
109     // Positions
110     TRSUB,          TRSUP,          TCSUB,          TCSUP,          TLSUB,
111     TLSUP,          TFROM,          TTO,            TUOPER,         TBOPER,
112     // Set theory
113     TSETN,          TSETZ,          TSETQ,          TSETR,          TSETC,
114     TIN,            TNOTIN,         TNI,            TEMPTYSET,      // Insideout
115     TSUBSET,        TSUBSETEQ,      TSUPSET,        TSUPSETEQ,      // Subsupset
116     TNSUBSET,       TNSUPSET,       TNSUBSETEQ,     TNSUPSETEQ,     // Not subsupset
117     TINTERSECT,     TUNION,         TSETMINUS,      TSETQUOTIENT,   // +-/
118     TALEPH,         TWP,            TINFINITY,                      // Abstract sets
119     TFORALL,        TEXISTS,        TNOTEXISTS,                     // Existential
120     // Font
121     TFONT,          TSIZE,          TCOLOR,         TPHANTOM,       // Basic
122     TITALIC,        TNITALIC,       TBOLD,          TNBOLD,         // Bold ital
123     TALIGNL,        TALIGNC,        TALIGNR,                        // Align
124     TUNDERLINE,     TOVERLINE,      TOVERSTRIKE,    TBAR,           // Lines
125     TFIXED,         TSANS,          TSERIF,                         // Types
126     TACUTE,         TGRAVE,         THAT,           TBREVE,         // Accents
127     TWIDEVEC,       TWIDEHARPOON,   TWIDETILDE,     TWIDEHAT,       // Wide math
128     TVEC,           THARPOON,       TTILDE,         TCIRCLE,        // math
129     TCHECK,
130     TTEXT,          TNUMBER,        TCHARACTER,     TIDENT,         // Content type
131     // Brackets
132     TLEFT,          TRIGHT,         TUNDERBRACE,    TOVERBRACE,     // Scalable, upsidedown
133     TLGROUP,        TRGROUP,        TLPARENT,       TRPARENT,       // Structural
134     TLBRACKET,      TRBRACKET,      TLDBRACKET,     TRDBRACKET,     // Bracket x1 & x2
135     TLCEIL,         TRCEIL,         TLFLOOR,        TRFLOOR,        // Reals -> Wholes
136     TLANGLE,        TRANGLE,        TLBRACE,        TRBRACE,        // <x> {x}
137     TLLINE,         TRLINE,         TLDLINE,        TRDLINE,        // Lines x1 x2
138     TMLINE,         TEVALUATE,      TLRLINE,        TLRDLINE,       // Custom
139     // Differential calculus
140     TNABLA,         TPARTIAL,       TFOURIER,       TLAPLACE,       // Derivative, Transformation
141     TINTD,          TINT,           TIINT,          TIIINT,         // Integral
142     TLINT,          TLLINT,         TLLLINT,                        // Circuit integral
143     TDOT,           TDDOT,          TDDDOT,                         // Derivative dots
144     // Function
145     TFUNC,          TLN,            TLOG,           TEXP,           // Exp - Log
146     TSIN,           TCOS,           TTAN,           TCOT,           // Trigo
147     TSINH,          TCOSH,          TTANH,          TCOTH,          // Trigo hyperbolic
148     TASIN,          TACOS,          TATAN,          TACOT,          // Arctrigo
149     TASINH,         TACOSH,         TATANH,         TACOTH,         // Arctrigo hyperbolic
150     TSQRT,          TNROOT,         TFACT,          TABS,           // roots, n! |z|
151     // Color
152     TRGB,           TRGBA,          THEX,           THTMLCOL,       TDVIPSNAMESCOL,
153     TICONICCOL,     TMATHMLCOL
154     // clang-format on
155 };
156 
157 struct SmTokenTableEntry
158 {
159     std::u16string_view pIdent;
160     SmTokenType eType;
161     sal_Unicode cMathChar;
162     TG nGroup;
163     sal_uInt16 nLevel;
164 };
165 
166 struct SmColorTokenTableEntry
167 {
168     const char* pIdent;
169     SmTokenType eType;
170     Color cColor;
171 
SmColorTokenTableEntrySmColorTokenTableEntry172     SmColorTokenTableEntry()
173         : pIdent("")
174         , eType(TERROR)
175         , cColor()
176     {
177     }
178 
SmColorTokenTableEntrySmColorTokenTableEntry179     SmColorTokenTableEntry(const SmColorTokenTableEntry* amColorTokenTableEntry)
180         : pIdent(amColorTokenTableEntry->pIdent)
181         , eType(amColorTokenTableEntry->eType)
182         , cColor(amColorTokenTableEntry->cColor)
183     {
184     }
185 
SmColorTokenTableEntrySmColorTokenTableEntry186     SmColorTokenTableEntry(const std::unique_ptr<SmColorTokenTableEntry> amColorTokenTableEntry)
187         : pIdent(amColorTokenTableEntry->pIdent)
188         , eType(amColorTokenTableEntry->eType)
189         , cColor(amColorTokenTableEntry->cColor)
190     {
191     }
192 
SmColorTokenTableEntrySmColorTokenTableEntry193     SmColorTokenTableEntry(const char* name, SmTokenType ctype, Color ncolor)
194         : pIdent(name)
195         , eType(ctype)
196         , cColor(ncolor)
197     {
198     }
199 
SmColorTokenTableEntrySmColorTokenTableEntry200     SmColorTokenTableEntry(const char* name, SmTokenType ctype, sal_uInt32 ncolor)
201         : pIdent(name)
202         , eType(ctype)
203         , cColor(ColorTransparency, ncolor)
204     {
205     }
206 
equalsSmColorTokenTableEntry207     bool equals(const OUString& colorname) const
208     {
209         return colorname.compareToIgnoreAsciiCaseAscii(pIdent) == 0;
210     }
211 
equalsSmColorTokenTableEntry212     bool equals(sal_uInt32 colorcode) const { return colorcode == static_cast<sal_uInt32>(cColor); }
213 
equalsSmColorTokenTableEntry214     bool equals(Color colorcode) const { return colorcode == cColor; }
215 };
216 
217 struct SmToken
218 {
219     OUString aText; // token text
220     SmTokenType eType; // token info
221     OUString cMathChar;
222 
223     // parse-help info
224     TG nGroup;
225     sal_uInt16 nLevel;
226 
SmTokenSmToken227     SmToken()
228         : eType(TUNKNOWN)
229         , cMathChar('\0')
230         , nGroup(TG::NONE)
231         , nLevel(0)
232     {
233     }
234 
SmTokenSmToken235     SmToken(SmTokenType eTokenType, sal_Unicode cMath, const char* pText, TG nTokenGroup = TG::NONE,
236             sal_uInt16 nTokenLevel = 0)
237         : aText(OUString::createFromAscii(pText))
238         , eType(eTokenType)
239         , cMathChar(cMath)
240         , nGroup(nTokenGroup)
241         , nLevel(nTokenLevel)
242     {
243     }
244 
operator =SmToken245     void operator=(const SmTokenTableEntry& aTokenTableEntry)
246     {
247         aText = aTokenTableEntry.pIdent;
248         eType = aTokenTableEntry.eType;
249         cMathChar = OUString(&aTokenTableEntry.cMathChar, 1);
250         nGroup = aTokenTableEntry.nGroup;
251         nLevel = aTokenTableEntry.nLevel;
252     }
253 
operator =SmToken254     void operator=(const SmTokenTableEntry* aTokenTableEntry)
255     {
256         aText = aTokenTableEntry->pIdent;
257         eType = aTokenTableEntry->eType;
258         cMathChar = OUString(&aTokenTableEntry->cMathChar, 1);
259         nGroup = aTokenTableEntry->nGroup;
260         nLevel = aTokenTableEntry->nLevel;
261     }
262 
operator =SmToken263     void operator=(const SmColorTokenTableEntry& aTokenTableEntry)
264     {
265         aText = u"";
266         eType = aTokenTableEntry.eType;
267         cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry.cColor), 16);
268         nGroup = TG::Color;
269         nLevel = 0;
270     }
271 
operator =SmToken272     void operator=(const SmColorTokenTableEntry* aTokenTableEntry)
273     {
274         aText = u"";
275         eType = aTokenTableEntry->eType;
276         cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16);
277         nGroup = TG::Color;
278         nLevel = 0;
279     }
280 
operator =SmToken281     void operator=(const std::unique_ptr<SmColorTokenTableEntry>& aTokenTableEntry)
282     {
283         aText = u"";
284         eType = aTokenTableEntry->eType;
285         cMathChar = OUString::number(static_cast<sal_uInt32>(aTokenTableEntry->cColor), 16);
286         nGroup = TG::Color;
287         nLevel = 0;
288     }
289 
setCharSmToken290     void setChar(sal_Unicode cChar) { cMathChar = OUString(&cChar, 1); }
291 };
292 
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
294