diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /starmath/inc/parsebase.hxx | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | starmath/inc/parsebase.hxx | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/starmath/inc/parsebase.hxx b/starmath/inc/parsebase.hxx new file mode 100644 index 000000000..ffa9b538a --- /dev/null +++ b/starmath/inc/parsebase.hxx @@ -0,0 +1,116 @@ +/* -*- 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> + +#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(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: */ |