summaryrefslogtreecommitdiff
path: root/starmath/inc/parsebase.hxx
blob: dd53ce631c11890191dbc7a9b47425611f853eb2 (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
/* -*- 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.
  *
  */

#pragma once

#include <unotools/resmgr.hxx>

#include "node.hxx"

#include <set>
#include <utility>

#define DEPTH_LIMIT 1024

// Those are the errors that the parser may encounter.
enum class SmParseError : uint_fast8_t
{
    None = 0,
    UnexpectedChar = 1,
    UnexpectedToken = 2,
    PoundExpected = 3,
    ColorExpected = 4,
    LgroupExpected = 5,
    RgroupExpected = 6,
    LbraceExpected = 7,
    RbraceExpected = 8,
    ParentMismatch = 9,
    RightExpected = 10,
    FontExpected = 11,
    SizeExpected = 12,
    DoubleAlign = 13,
    DoubleSubsupscript = 14,
    NumberExpected = 15
};

struct SmErrorDesc
{
    SmParseError m_eType;
    SmNode* m_pNode;
    OUString m_aText;

    SmErrorDesc(SmParseError eType, SmNode* pNode, OUString aText)
        : m_eType(eType)
        , m_pNode(pNode)
        , m_aText(std::move(aText))
    {
    }
};

class DepthProtect
{
private:
    sal_Int32& m_rParseDepth;

public:
    DepthProtect(sal_Int32& rParseDepth)
        : m_rParseDepth(rParseDepth)
    {
        ++m_rParseDepth;
        if (m_rParseDepth > DEPTH_LIMIT)
            throw std::range_error("parser depth limit");
    }
    ~DepthProtect() { --m_rParseDepth; }
};

namespace starmathdatabase
{
// Must be in sync with SmParseError list
extern const TranslateId SmParseErrorDesc[16];

OUString getParseErrorDesc(SmParseError err);
}

class AbstractSmParser
{
public:
    virtual ~AbstractSmParser() {}

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

    virtual const OUString& GetText() const = 0;

    virtual bool IsImportSymbolNames() const = 0;
    virtual void SetImportSymbolNames(bool bVal) = 0;
    virtual bool IsExportSymbolNames() const = 0;
    virtual void SetExportSymbolNames(bool bVal) = 0;

    virtual const SmErrorDesc* NextError() = 0;
    virtual const SmErrorDesc* PrevError() = 0;
    virtual const SmErrorDesc* GetError() const = 0;
    virtual const std::set<OUString>& GetUsedSymbols() const = 0;
};

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