Starmath now allows multiple parsers third part

Allows SmDocShell to choose parser

Change-Id: I36e7563943690ffe3bf031438061b302b7439258
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111684
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/starmath/inc/document.hxx b/starmath/inc/document.hxx
index 17f0713..c7885c0 100644
--- a/starmath/inc/document.hxx
+++ b/starmath/inc/document.hxx
@@ -77,7 +77,6 @@ class SM_DLLPUBLIC SmDocShell : public SfxObjectShell, public SfxListener

    OUString            maText;
    SmFormat            maFormat;
    SmParser            maParser;
    OUString            maAccText;
    SvtLinguOptions     maLinguOptions;
    std::unique_ptr<SmTableNode> mpTree;
@@ -88,6 +87,7 @@ class SM_DLLPUBLIC SmDocShell : public SfxObjectShell, public SfxListener
    sal_uInt16          mnModifyCount;
    bool                mbFormulaArranged;
    sal_uInt16          mnSmSyntaxVersion;
    std::unique_ptr<AbstractSmParser> maParser;
    std::unique_ptr<SmCursor> mpCursor;
    std::set< OUString >    maUsedSymbols;   // to export used symbols only when saving

@@ -166,12 +166,11 @@ public:
    const SmFormat&  GetFormat() const { return maFormat; }

    void            Parse();
    SmParser &      GetParser() { return maParser; }
    AbstractSmParser* GetParser() { return maParser.get(); }
    const SmTableNode *GetFormulaTree() const  { return mpTree.get(); }
    void            SetFormulaTree(SmTableNode *pTree) { mpTree.reset(pTree); }
    sal_uInt16      GetSmSyntaxVersion() const { return mnSmSyntaxVersion; }
    void            SetSmSyntaxVersion(sal_uInt16 nSmSyntaxVersion)
    { mnSmSyntaxVersion = nSmSyntaxVersion; }
    void            SetSmSyntaxVersion(sal_uInt16 nSmSyntaxVersion);

    const std::set< OUString > &    GetUsedSymbols() const  { return maUsedSymbols; }

diff --git a/starmath/qa/cppunit/test_starmath.cxx b/starmath/qa/cppunit/test_starmath.cxx
index 480796a..37bc234 100644
--- a/starmath/qa/cppunit/test_starmath.cxx
+++ b/starmath/qa/cppunit/test_starmath.cxx
@@ -234,28 +234,28 @@ void Test::editFailure()
{
    m_xDocShRef->SetText("color a b over {a/}");

    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();

    CPPUNIT_ASSERT_MESSAGE("Should be a SmParseError::ColorExpected",
        pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Should be a SmParseError::ColorExpected",
        SmParseError::ColorExpected, pErrorDesc->m_eType);

    pErrorDesc = m_xDocShRef->GetParser().PrevError();
    pErrorDesc = m_xDocShRef->GetParser()->PrevError();

    CPPUNIT_ASSERT_MESSAGE("Should be a SmParseError::UnexpectedChar",
        pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Should be a SmParseError::UnexpectedChar",
        SmParseError::UnexpectedChar, pErrorDesc->m_eType);

    pErrorDesc = m_xDocShRef->GetParser().PrevError();
    pErrorDesc = m_xDocShRef->GetParser()->PrevError();

    CPPUNIT_ASSERT_MESSAGE("Should be a SmParseError::RgroupExpected",
        pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Should be a SmParseError::RgroupExpected",
        SmParseError::RgroupExpected, pErrorDesc->m_eType);

    const SmErrorDesc *pLastErrorDesc = m_xDocShRef->GetParser().PrevError();
    const SmErrorDesc *pLastErrorDesc = m_xDocShRef->GetParser()->PrevError();

    CPPUNIT_ASSERT_MESSAGE("Should be three syntax errors",
        pLastErrorDesc);
@@ -266,7 +266,7 @@ void Test::editFailure()
void Test::ParseErrorUnexpectedToken()
{
    m_xDocShRef->SetText("\\foo");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::UnexpectedToken expected",
                           SmParseError::UnexpectedToken, pErrorDesc->m_eType);
@@ -275,7 +275,7 @@ void Test::ParseErrorUnexpectedToken()
void Test::ParseErrorPoundExpected()
{
    m_xDocShRef->SetText("matrix {1#2##a##b#c}");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::PoundExpected expected",
                           SmParseError::PoundExpected, pErrorDesc->m_eType);
@@ -284,7 +284,7 @@ void Test::ParseErrorPoundExpected()
void Test::ParseErrorColorExpected()
{
    m_xDocShRef->SetText("color 42 x");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::ColorExpected expected",
                           SmParseError::ColorExpected, pErrorDesc->m_eType);
@@ -293,7 +293,7 @@ void Test::ParseErrorColorExpected()
void Test::ParseErrorLgroupExpected()
{
    m_xDocShRef->SetText("stack 42");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::LgroupExpected expected",
                           SmParseError::LgroupExpected, pErrorDesc->m_eType);
@@ -302,7 +302,7 @@ void Test::ParseErrorLgroupExpected()
void Test::ParseErrorRgroupExpected()
{
    m_xDocShRef->SetText("stack {a#b#c)");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::RgroupExpected expected",
                           SmParseError::RgroupExpected, pErrorDesc->m_eType);
@@ -311,7 +311,7 @@ void Test::ParseErrorRgroupExpected()
void Test::ParseErrorLbraceExpected()
{
    m_xDocShRef->SetText("left 42");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::LbraceExpected expected",
                           SmParseError::LbraceExpected, pErrorDesc->m_eType);
@@ -320,7 +320,7 @@ void Test::ParseErrorLbraceExpected()
void Test::ParseErrorRbraceExpected()
{
    m_xDocShRef->SetText("left ( foo right x");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::RbraceExpected expected",
                           SmParseError::RbraceExpected, pErrorDesc->m_eType);
@@ -329,7 +329,7 @@ void Test::ParseErrorRbraceExpected()
void Test::ParseErrorParentMismatch()
{
    m_xDocShRef->SetText("lbrace foo rceil");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::ParentMismatch expected",
                           SmParseError::ParentMismatch, pErrorDesc->m_eType);
@@ -338,7 +338,7 @@ void Test::ParseErrorParentMismatch()
void Test::ParseErrorRightExpected()
{
    m_xDocShRef->SetText("left ( x mline y )");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::RightExpected expected",
                           SmParseError::RightExpected, pErrorDesc->m_eType);
@@ -347,7 +347,7 @@ void Test::ParseErrorRightExpected()
void Test::ParseErrorFontExpected()
{
    m_xDocShRef->SetText("font small bar");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::FontExpected expected",
                           SmParseError::FontExpected, pErrorDesc->m_eType);
@@ -356,7 +356,7 @@ void Test::ParseErrorFontExpected()
void Test::ParseErrorSizeExpected()
{
    m_xDocShRef->SetText("size small baz");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::SizeExpected expected",
                           SmParseError::SizeExpected, pErrorDesc->m_eType);
@@ -365,7 +365,7 @@ void Test::ParseErrorSizeExpected()
void Test::ParseErrorDoubleAlign()
{
    m_xDocShRef->SetText("alignl alignc x");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::DoubleAlign expected",
                           SmParseError::DoubleAlign, pErrorDesc->m_eType);
@@ -374,7 +374,7 @@ void Test::ParseErrorDoubleAlign()
void Test::ParseErrorDoubleSubsupscript()
{
    m_xDocShRef->SetText("x_y_z");
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
    const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser()->NextError();
    CPPUNIT_ASSERT(pErrorDesc);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("SmParseError::DoubleSubsupscript expected",
                           SmParseError::DoubleSubsupscript, pErrorDesc->m_eType);
diff --git a/starmath/source/document.cxx b/starmath/source/document.cxx
index 59dcb21..6c0e566 100644
--- a/starmath/source/document.cxx
+++ b/starmath/source/document.cxx
@@ -99,6 +99,12 @@ void SmDocShell::InitInterface_Impl()
    GetStaticInterface()->RegisterPopupMenu("view");
}

void SmDocShell::SetSmSyntaxVersion(sal_uInt16 nSmSyntaxVersion)
{
    mnSmSyntaxVersion = nSmSyntaxVersion;
    maParser.reset(starmathdatabase::GetVersionSmParser(mnSmSyntaxVersion));
}

SFX_IMPL_OBJECTFACTORY(SmDocShell, SvGlobalName(SO3_SM_CLASSID), "smath" )

void SmDocShell::Notify(SfxBroadcaster&, const SfxHint& rHint)
@@ -219,11 +225,11 @@ void SmDocShell::Parse()
{
    mpTree.reset();
    ReplaceBadChars();
    mpTree = maParser.Parse(maText);
    mpTree = maParser->Parse(maText);
    mnModifyCount++;     //! see comment for SID_GAPHIC_SM in SmDocShell::GetState
    SetFormulaArranged( false );
    InvalidateCursor();
    maUsedSymbols = maParser.GetUsedSymbols();
    maUsedSymbols = maParser->GetUsedSymbols();
}


@@ -627,6 +633,7 @@ SmDocShell::SmDocShell( SfxModelFlags i_nSfxCreationFlags )

    SmModule *pp = SM_MOD();
    maFormat = pp->GetConfig()->GetStandardFormat();
    maParser.reset(starmathdatabase::GetVersionSmParser(mnSmSyntaxVersion));

    StartListening(maFormat);
    StartListening(*pp->GetConfig());
diff --git a/starmath/source/mathml/mathmlexport.cxx b/starmath/source/mathml/mathmlexport.cxx
index 8414bc7..f1952b5 100644
--- a/starmath/source/mathml/mathmlexport.cxx
+++ b/starmath/source/mathml/mathmlexport.cxx
@@ -469,13 +469,13 @@ void SmXMLExport::ExportContent_()
    if (pDocShell)
    {
        nSmSyntaxVersion = pDocShell->GetSmSyntaxVersion();
        SmParser& rParser = pDocShell->GetParser();
        bool bVal = rParser.IsExportSymbolNames();
        rParser.SetExportSymbolNames(true);
        auto pTmpTree = rParser.Parse(aText);
        aText = rParser.GetText();
        AbstractSmParser* rParser = pDocShell->GetParser();
        bool bVal = rParser->IsExportSymbolNames();
        rParser->SetExportSymbolNames(true);
        auto pTmpTree = rParser->Parse(aText);
        aText = rParser->GetText();
        pTmpTree.reset();
        rParser.SetExportSymbolNames(bVal);
        rParser->SetExportSymbolNames(bVal);
    }

    OUStringBuffer sStrBuf(12);
diff --git a/starmath/source/mathml/mathmlimport.cxx b/starmath/source/mathml/mathmlimport.cxx
index f986243..f224f14 100644
--- a/starmath/source/mathml/mathmlimport.cxx
+++ b/starmath/source/mathml/mathmlimport.cxx
@@ -471,13 +471,13 @@ void SmXMLImport::endDocument()
            }

            // Convert symbol names
            SmParser& rParser = pDocShell->GetParser();
            bool bVal = rParser.IsImportSymbolNames();
            rParser.SetImportSymbolNames(true);
            auto pTmpTree = rParser.Parse(aText);
            aText = rParser.GetText();
            AbstractSmParser* rParser = pDocShell->GetParser();
            bool bVal = rParser->IsImportSymbolNames();
            rParser->SetImportSymbolNames(true);
            auto pTmpTree = rParser->Parse(aText);
            aText = rParser->GetText();
            pTmpTree.reset();
            rParser.SetImportSymbolNames(bVal);
            rParser->SetImportSymbolNames(bVal);

            pDocShell->SetText(aText);
            pDocShell->SetSmSyntaxVersion(mnSmSyntaxVersion);
diff --git a/starmath/source/view.cxx b/starmath/source/view.cxx
index 0539f52..ee72fb5 100644
--- a/starmath/source/view.cxx
+++ b/starmath/source/view.cxx
@@ -1285,7 +1285,7 @@ void SmViewShell::SetStatusText(const OUString& rText)
void SmViewShell::ShowError(const SmErrorDesc* pErrorDesc)
{
    assert(GetDoc());
    if (pErrorDesc || nullptr != (pErrorDesc = GetDoc()->GetParser().GetError()) )
    if (pErrorDesc || nullptr != (pErrorDesc = GetDoc()->GetParser()->GetError()) )
    {
        SetStatusText( pErrorDesc->m_aText );
        GetEditWindow()->MarkError( Point( pErrorDesc->m_pNode->GetColumn(),
@@ -1296,7 +1296,7 @@ void SmViewShell::ShowError(const SmErrorDesc* pErrorDesc)
void SmViewShell::NextError()
{
    assert(GetDoc());
    const SmErrorDesc   *pErrorDesc = GetDoc()->GetParser().NextError();
    const SmErrorDesc   *pErrorDesc = GetDoc()->GetParser()->NextError();

    if (pErrorDesc)
        ShowError( pErrorDesc );
@@ -1305,7 +1305,7 @@ void SmViewShell::NextError()
void SmViewShell::PrevError()
{
    assert(GetDoc());
    const SmErrorDesc   *pErrorDesc = GetDoc()->GetParser().PrevError();
    const SmErrorDesc   *pErrorDesc = GetDoc()->GetParser()->PrevError();

    if (pErrorDesc)
        ShowError( pErrorDesc );