diff options
author | dante <dante19031999@gmail.com> | 2021-02-06 11:19:36 +0100 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-02-06 18:39:31 +0100 |
commit | 7c959884d9a4a3f187a79cbb01407cd20d92a2f8 (patch) | |
tree | b82cf1081b55b320a0495130c01775fd6582a9e7 | |
parent | 956c09ca7d690471f62e8e2e14ad04fefcebf7e7 (diff) |
Modifications on starmath smparse error handle
Made modifications for:
- Being able to highlight syntax errors
- Displaying error description in visual editor
- Easier constructor
- Rely on starmathdatabase instead of long switch in code.
Change-Id: Ic6c7f920463090176d80668f9b660ccf3e9b5708
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110497
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | starmath/inc/error.hxx | 56 | ||||
-rw-r--r-- | starmath/inc/parse.hxx | 46 | ||||
-rw-r--r-- | starmath/inc/strings.hrc | 1 | ||||
-rw-r--r-- | starmath/source/parse.cxx | 78 |
4 files changed, 83 insertions, 98 deletions
diff --git a/starmath/inc/error.hxx b/starmath/inc/error.hxx deleted file mode 100644 index dc798f6906e3..000000000000 --- a/starmath/inc/error.hxx +++ /dev/null @@ -1,56 +0,0 @@ -/* -*- 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 . - */ -#ifndef INCLUDED_STARMATH_INC_ERROR_HXX -#define INCLUDED_STARMATH_INC_ERROR_HXX - -#include <rtl/ustring.hxx> - -class SmNode; - -// Those are the errors that the parser (parser.hxx/parser.cxx) may encounter. -enum class SmParseError -{ - None, - UnexpectedChar, - UnexpectedToken, - PoundExpected, - ColorExpected, - LgroupExpected, - RgroupExpected, - LbraceExpected, - RbraceExpected, - ParentMismatch, - RightExpected, - FontExpected, - SizeExpected, - DoubleAlign, - DoubleSubsupscript, - NumberExpected -}; - -struct SmErrorDesc -{ - SmParseError m_eType; - SmNode* m_pNode; - OUString m_aText; -}; - -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/starmath/inc/parse.hxx b/starmath/inc/parse.hxx index b010db941832..6eb83500b3d3 100644 --- a/starmath/inc/parse.hxx +++ b/starmath/inc/parse.hxx @@ -30,7 +30,6 @@ #include <vector> #include "token.hxx" -#include "error.hxx" class SmBlankNode; class SmBinVerNode; @@ -46,6 +45,50 @@ class SmTextNode; #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) + {} + +}; + +namespace starmathdatabase{ + +// Must be in sync with SmParseError list +extern const char* SmParseErrorDesc[16]; + +OUString getParseErrorDesc(SmParseError err); + +} + class SmParser { OUString m_aBufferString; @@ -148,7 +191,6 @@ public: 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(); diff --git a/starmath/inc/strings.hrc b/starmath/inc/strings.hrc index 01dc7b864ee4..daa90f02545d 100644 --- a/starmath/inc/strings.hrc +++ b/starmath/inc/strings.hrc @@ -374,6 +374,7 @@ #define STR_STATSTR_WRITING NC_("STR_STATSTR_WRITING", "Saving document..." ) #define STR_MATH_DOCUMENT_FULLTYPE_CURRENT NC_("STR_MATH_DOCUMENT_FULLTYPE_CURRENT", "%PRODUCTNAME %PRODUCTVERSION Formula") #define RID_ERR_IDENT NC_("RID_ERR_IDENT", "ERROR : " ) +#define RID_ERR_NONE NC_("RID_ERR_NONE", "no error" ) #define RID_ERR_UNEXPECTEDCHARACTER NC_("RID_ERR_UNEXPECTEDCHARACTER", "Unexpected character" ) #define RID_ERR_UNEXPECTEDTOKEN NC_("RID_ERR_UNEXPECTEDTOKEN", "Unexpected token" ) #define RID_ERR_LGROUPEXPECTED NC_("RID_ERR_LGROUPEXPECTED", "'{' expected" ) diff --git a/starmath/source/parse.cxx b/starmath/source/parse.cxx index 8bd06070c268..fbf27478d2c7 100644 --- a/starmath/source/parse.cxx +++ b/starmath/source/parse.cxx @@ -39,6 +39,31 @@ using namespace ::com::sun::star::i18n; +const char* starmathdatabase::SmParseErrorDesc[] = { + // clang-format off + RID_ERR_NONE, + RID_ERR_UNEXPECTEDCHARACTER, + RID_ERR_UNEXPECTEDTOKEN, + RID_ERR_POUNDEXPECTED, + RID_ERR_COLOREXPECTED, + RID_ERR_LGROUPEXPECTED, + RID_ERR_RGROUPEXPECTED, + RID_ERR_LBRACEEXPECTED, + RID_ERR_RBRACEEXPECTED, + RID_ERR_PARENTMISMATCH, + RID_ERR_RIGHTEXPECTED, + RID_ERR_FONTEXPECTED, + RID_ERR_SIZEEXPECTED, + RID_ERR_DOUBLEALIGN, + RID_ERR_DOUBLESUBSUPSCRIPT, + RID_ERR_NUMBEREXPECTED + // clang-format on +}; + +OUString starmathdatabase::getParseErrorDesc(SmParseError err){ + return SmResId(starmathdatabase::SmParseErrorDesc[static_cast<uint_fast8_t>(err)]); +} + //Definition of math keywords const SmTokenTableEntry aTokenTable[] = { @@ -2607,11 +2632,21 @@ std::unique_ptr<SmExpressionNode> SmParser::DoError(SmParseError eError) if (aDepthGuard.TooDeep()) throw std::range_error("parser depth limit"); + // Identify error message + OUStringBuffer sStrBuf(128); + sStrBuf.append(SmResId(RID_ERR_IDENT)); + sStrBuf.append(starmathdatabase::getParseErrorDesc(eError)); + + // Generate error node + m_aCurToken.eType = TERROR; + m_aCurToken.cMathChar = sStrBuf.makeStringAndClear(); auto xSNode = std::make_unique<SmExpressionNode>(m_aCurToken); - std::unique_ptr<SmErrorNode> pErr(new SmErrorNode(m_aCurToken)); - xSNode->SetSubNodes(std::move(pErr), nullptr); + SmErrorNode* pErr(new SmErrorNode(m_aCurToken)); + xSNode->SetSubNode(0, pErr); - AddError(eError, xSNode.get()); + // Append error to the error list + SmErrorDesc* pErrDesc = new SmErrorDesc(eError, xSNode.get(), m_aCurToken.cMathChar); + m_aErrDescList.push_back(std::unique_ptr<SmErrorDesc>(pErrDesc)); NextToken(); @@ -2667,43 +2702,6 @@ std::unique_ptr<SmNode> SmParser::ParseExpression(const OUString &rBuffer) return DoExpression(); } - -void SmParser::AddError(SmParseError Type, SmNode *pNode) -{ - std::unique_ptr<SmErrorDesc> pErrDesc(new SmErrorDesc); - - pErrDesc->m_eType = Type; - pErrDesc->m_pNode = pNode; - pErrDesc->m_aText = SmResId(RID_ERR_IDENT); - - const char* pRID; - switch (Type) - { - case SmParseError::UnexpectedChar: pRID = RID_ERR_UNEXPECTEDCHARACTER; break; - case SmParseError::UnexpectedToken: pRID = RID_ERR_UNEXPECTEDTOKEN; break; - case SmParseError::PoundExpected: pRID = RID_ERR_POUNDEXPECTED; break; - case SmParseError::ColorExpected: pRID = RID_ERR_COLOREXPECTED; break; - case SmParseError::LgroupExpected: pRID = RID_ERR_LGROUPEXPECTED; break; - case SmParseError::RgroupExpected: pRID = RID_ERR_RGROUPEXPECTED; break; - case SmParseError::LbraceExpected: pRID = RID_ERR_LBRACEEXPECTED; break; - case SmParseError::RbraceExpected: pRID = RID_ERR_RBRACEEXPECTED; break; - case SmParseError::ParentMismatch: pRID = RID_ERR_PARENTMISMATCH; break; - case SmParseError::RightExpected: pRID = RID_ERR_RIGHTEXPECTED; break; - case SmParseError::FontExpected: pRID = RID_ERR_FONTEXPECTED; break; - case SmParseError::SizeExpected: pRID = RID_ERR_SIZEEXPECTED; break; - case SmParseError::DoubleAlign: pRID = RID_ERR_DOUBLEALIGN; break; - case SmParseError::DoubleSubsupscript: pRID = RID_ERR_DOUBLESUBSUPSCRIPT; break; - case SmParseError::NumberExpected: pRID = RID_ERR_NUMBEREXPECTED; break; - default: - assert(false); - return; - } - pErrDesc->m_aText += SmResId(pRID); - - m_aErrDescList.push_back(std::move(pErrDesc)); -} - - const SmErrorDesc *SmParser::NextError() { if ( !m_aErrDescList.empty() ) |