summaryrefslogtreecommitdiff
path: root/starmath/inc/parse.hxx
blob: dde0c3454958a3572abc04372d2caf3146ba041b (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

/** Parses the starmath code and creates the nodes.
  *
  */

#ifndef INCLUDED_STARMATH_INC_PARSE_HXX
#define INCLUDED_STARMATH_INC_PARSE_HXX

#include <unotools/charclass.hxx>
#include <memory>
#include <set>
#include <vector>

#include "token.hxx"
#include "error.hxx"

class SmBlankNode;
class SmBinVerNode;
class SmBracebodyNode;
class SmExpressionNode;
class SmGlyphSpecialNode;
class SmNode;
class SmOperNode;
class SmSpecialNode;
class SmStructureNode;
class SmTableNode;
class SmTextNode;

#define DEPTH_LIMIT 1024

class SmParser
{
    OUString        m_aBufferString;
    SmToken         m_aCurToken;
    std::vector<std::unique_ptr<SmErrorDesc>> m_aErrDescList;
    int             m_nCurError;
    sal_Int32       m_nBufferIndex,
                    m_nTokenIndex;
    sal_Int32       m_nRow,    // 1-based
                    m_nColOff; // 0-based
    bool            m_bImportSymNames,
                    m_bExportSymNames;
    sal_Int32       m_nParseDepth;

    class DepthProtect
    {
    private:
        sal_Int32& m_rParseDepth;
    public:
        DepthProtect(sal_Int32& rParseDepth)
            : m_rParseDepth(rParseDepth)
        {
            ++m_rParseDepth;
        }
        bool TooDeep() const { return m_rParseDepth > DEPTH_LIMIT; }
        ~DepthProtect()
        {
            --m_rParseDepth;
        }
    };

    // map of used symbols (used to reduce file size by exporting only actually used symbols)
    std::set< OUString >   m_aUsedSymbols;

    // CharClass representing a locale for parsing numbers
    CharClass m_aNumCC;
    // pointer to System locale's CharClass, which is alive inside SM_MOD()
    const CharClass* m_pSysCC;

    SmParser(const SmParser&) = delete;
    SmParser& operator=(const SmParser&) = delete;

    // Moves between tokens inside starmath code.
    void            NextToken();
    void            NextTokenColor(bool dvipload);
    void            NextTokenFontSize();
    sal_Int32       GetTokenIndex() const   { return m_nTokenIndex; }
    void            Replace( sal_Int32 nPos, sal_Int32 nLen, const OUString &rText );

    inline bool     TokenInGroup( TG nGroup );

    // grammar
    std::unique_ptr<SmTableNode> DoTable();
    std::unique_ptr<SmNode> DoLine();
    std::unique_ptr<SmNode> DoExpression(bool bUseExtraSpaces = true);
    std::unique_ptr<SmNode> DoRelation();
    std::unique_ptr<SmNode> DoSum();
    std::unique_ptr<SmNode> DoProduct();
    std::unique_ptr<SmNode> DoSubSup(TG nActiveGroup, SmNode *pGivenNode);
    std::unique_ptr<SmNode> DoSubSupEvaluate(SmNode *pGivenNode);
    std::unique_ptr<SmNode> DoOpSubSup();
    std::unique_ptr<SmNode> DoPower();
    std::unique_ptr<SmBlankNode> DoBlank();
    std::unique_ptr<SmNode> DoTerm(bool bGroupNumberIdent);
    std::unique_ptr<SmNode> DoEscape();
    std::unique_ptr<SmOperNode> DoOperator();
    std::unique_ptr<SmNode> DoOper();
    std::unique_ptr<SmStructureNode> DoUnOper();
    std::unique_ptr<SmNode> DoAlign(bool bUseExtraSpaces = true);
    std::unique_ptr<SmStructureNode> DoFontAttribut();
    std::unique_ptr<SmStructureNode> DoAttribut();
    std::unique_ptr<SmStructureNode> DoFont();
    std::unique_ptr<SmStructureNode> DoFontSize();
    std::unique_ptr<SmStructureNode> DoColor();
    std::unique_ptr<SmStructureNode> DoBrace();
    std::unique_ptr<SmBracebodyNode> DoBracebody(bool bIsLeftRight);
    std::unique_ptr<SmNode> DoEvaluate();
    std::unique_ptr<SmTextNode> DoFunction();
    std::unique_ptr<SmTableNode> DoBinom();
    std::unique_ptr<SmBinVerNode> DoFrac();
    std::unique_ptr<SmStructureNode> DoStack();
    std::unique_ptr<SmStructureNode> DoMatrix();
    std::unique_ptr<SmSpecialNode> DoSpecial();
    std::unique_ptr<SmGlyphSpecialNode> DoGlyphSpecial();
    std::unique_ptr<SmExpressionNode> DoError(SmParseError Error);
    // end of grammar

public:
                 SmParser();

    /** Parse rBuffer to formula tree */
    std::unique_ptr<SmTableNode> Parse(const OUString &rBuffer);
    /** Parse rBuffer to formula subtree that constitutes an expression */
    std::unique_ptr<SmNode> ParseExpression(const OUString &rBuffer);

    const OUString & GetText() const { return m_aBufferString; };

    bool        IsImportSymbolNames() const        { return m_bImportSymNames; }
    void        SetImportSymbolNames(bool bVal)    { m_bImportSymNames = bVal; }
    bool        IsExportSymbolNames() const        { return m_bExportSymNames; }
    void        SetExportSymbolNames(bool bVal)    { m_bExportSymNames = bVal; }

    void        AddError(SmParseError Type, SmNode *pNode);
    const SmErrorDesc*  NextError();
    const SmErrorDesc*  PrevError();
    const SmErrorDesc*  GetError();
    const std::set< OUString >&   GetUsedSymbols() const      { return m_aUsedSymbols; }
};


inline bool SmParser::TokenInGroup( TG nGroup)
{
    return bool(m_aCurToken.nGroup & nGroup);
}


#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */