summaryrefslogtreecommitdiff
path: root/starmath
diff options
context:
space:
mode:
authorTakeshi Abe <tabe@fixedpoint.jp>2017-03-25 17:52:46 +0900
committerTakeshi Abe <tabe@fixedpoint.jp>2017-04-04 01:24:31 +0000
commit1a1d1a86e9129ec3885610b641179b30f9bf5e79 (patch)
tree850c593a0777617d4f744c202a3a9a087b82c0fc /starmath
parent646e52adf4a84ca5beab7bae1e1fae4e793c4896 (diff)
starmath: Simplify code parsing nospace
This also comes with its unit test. Change-Id: I1478bf48c5522691978e3534c9c8a9c0ddfc1e59 Reviewed-on: https://gerrit.libreoffice.org/35985 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Takeshi Abe <tabe@fixedpoint.jp>
Diffstat (limited to 'starmath')
-rw-r--r--starmath/inc/parse.hxx4
-rw-r--r--starmath/qa/cppunit/test_parse.cxx31
-rw-r--r--starmath/source/parse.cxx46
3 files changed, 48 insertions, 33 deletions
diff --git a/starmath/inc/parse.hxx b/starmath/inc/parse.hxx
index 43b84bab221c..4911e8516a81 100644
--- a/starmath/inc/parse.hxx
+++ b/starmath/inc/parse.hxx
@@ -63,7 +63,7 @@ class SmParser
// grammar
SmTableNode *DoTable();
void DoLine();
- SmNode *DoExpression();
+ SmNode *DoExpression(bool bUseExtraSpaces = true);
SmNode *DoRelation();
SmNode *DoSum();
SmNode *DoProduct();
@@ -76,7 +76,7 @@ class SmParser
SmOperNode *DoOperator();
SmNode *DoOper();
SmStructureNode *DoUnOper();
- SmNode *DoAlign();
+ SmNode *DoAlign(bool bUseExtraSpaces = true);
SmStructureNode *DoFontAttribut();
SmAttributNode *DoAttribut();
SmStructureNode *DoFont();
diff --git a/starmath/qa/cppunit/test_parse.cxx b/starmath/qa/cppunit/test_parse.cxx
index 80da700be4cc..4e7198e5db0e 100644
--- a/starmath/qa/cppunit/test_parse.cxx
+++ b/starmath/qa/cppunit/test_parse.cxx
@@ -33,9 +33,11 @@ public:
private:
void testMinus();
+ void testNospace();
CPPUNIT_TEST_SUITE(ParseTest);
CPPUNIT_TEST(testMinus);
+ CPPUNIT_TEST(testNospace);
CPPUNIT_TEST_SUITE_END();
SmDocShellRef mxDocShell;
@@ -90,6 +92,35 @@ void ParseTest::testMinus()
static_cast<const SmTextNode *>(pNode001)->GetToken().aText);
}
+/*
+ * This shows that "nospace" turns off the expression's IsUseExtraSpaces(),
+ * but leaves its decendants' flag on.
+ */
+void ParseTest::testNospace()
+{
+ std::unique_ptr<SmTableNode> pNode(SmParser().Parse("nospace{ nitalic d {F(x) G(x)} }"));
+ CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), pNode->GetNumSubNodes());
+ const SmNode *pNode0 = pNode->GetSubNode(0);
+ CPPUNIT_ASSERT(pNode0);
+ CPPUNIT_ASSERT_EQUAL(NLINE, pNode0->GetType());
+ CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), pNode0->GetNumSubNodes());
+ const SmNode *pNode00 = pNode0->GetSubNode(0);
+ CPPUNIT_ASSERT(pNode00);
+ CPPUNIT_ASSERT_EQUAL(NEXPRESSION, pNode00->GetType());
+ CPPUNIT_ASSERT(!static_cast<const SmExpressionNode *>(pNode00)->IsUseExtraSpaces());
+ CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), pNode00->GetNumSubNodes());
+ const SmNode *pNode000 = pNode00->GetSubNode(0);
+ CPPUNIT_ASSERT(pNode000);
+ CPPUNIT_ASSERT_EQUAL(NFONT, pNode000->GetType());
+ CPPUNIT_ASSERT_EQUAL(OUString("nitalic"),
+ static_cast<const SmFontNode *>(pNode000)->GetToken().aText);
+ const SmNode *pNode001 = pNode00->GetSubNode(1);
+ CPPUNIT_ASSERT(pNode001);
+ CPPUNIT_ASSERT_EQUAL(NEXPRESSION, pNode001->GetType());
+ CPPUNIT_ASSERT(static_cast<const SmExpressionNode *>(pNode001)->IsUseExtraSpaces());
+ CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), pNode00->GetNumSubNodes());
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION(ParseTest);
}
diff --git a/starmath/source/parse.cxx b/starmath/source/parse.cxx
index 9e423e1a7f17..1a2418314178 100644
--- a/starmath/source/parse.cxx
+++ b/starmath/source/parse.cxx
@@ -963,7 +963,7 @@ SmTableNode *SmParser::DoTable()
return pSNode.release();
}
-SmNode *SmParser::DoAlign()
+SmNode *SmParser::DoAlign(bool bUseExtraSpaces)
// parse alignment info (if any), then go on with rest of expression
{
std::unique_ptr<SmStructureNode> pSNode;
@@ -979,7 +979,7 @@ SmNode *SmParser::DoAlign()
return DoError(SmParseError::DoubleAlign);
}
- std::unique_ptr<SmNode> pNode(DoExpression());
+ std::unique_ptr<SmNode> pNode(DoExpression(bUseExtraSpaces));
if (pSNode)
{
@@ -1017,18 +1017,8 @@ void SmParser::DoLine()
m_aNodeStack.push_front(std::move(pSNode));
}
-SmNode *SmParser::DoExpression()
+SmNode *SmParser::DoExpression(bool bUseExtraSpaces)
{
- bool bUseExtraSpaces = true;
- if (!m_aNodeStack.empty())
- {
- if (m_aNodeStack.front()->GetToken().eType == TNOSPACE)
- {
- m_aNodeStack.pop_front();
- bUseExtraSpaces = false;
- }
- }
-
SmNodeArray RelationArray;
RelationArray.push_back(DoRelation());
while (m_aCurToken.nLevel >= 4)
@@ -1273,37 +1263,31 @@ SmNode *SmParser::DoTerm(bool bGroupNumberIdent)
case TLGROUP :
{
bool bNoSpace = m_aCurToken.eType == TNOSPACE;
- if (bNoSpace) // push 'no space' node and continue to parse expression
- {
- m_aNodeStack.push_front(o3tl::make_unique<SmExpressionNode>(m_aCurToken));
+ if (bNoSpace)
NextToken();
- }
if (m_aCurToken.eType != TLGROUP)
- {
- m_aNodeStack.pop_front(); // get rid of the 'no space' node pushed above
- return DoTerm(false);
- }
+ return DoTerm(false); // nospace is no loger concerned
+
NextToken();
// allow for empty group
if (m_aCurToken.eType == TRGROUP)
{
- if (bNoSpace) // get rid of the 'no space' node pushed above
- m_aNodeStack.pop_front();
std::unique_ptr<SmStructureNode> pSNode(new SmExpressionNode(m_aCurToken));
pSNode->SetSubNodes(nullptr, nullptr);
NextToken();
return pSNode.release();
}
- // go as usual
- m_aNodeStack.emplace_front(DoAlign());
- if (m_aCurToken.eType != TRGROUP)
- return DoError(SmParseError::RgroupExpected);
- NextToken();
- auto pNode = std::move(m_aNodeStack.front());
- m_aNodeStack.pop_front();
- return pNode.release();
+
+ std::unique_ptr<SmNode> pNode(DoAlign(!bNoSpace));
+ if (m_aCurToken.eType == TRGROUP) {
+ NextToken();
+ return pNode.release();
+ }
+ auto pSNode = o3tl::make_unique<SmExpressionNode>(m_aCurToken);
+ pSNode->SetSubNodes(pNode.release(), DoError(SmParseError::RgroupExpected));
+ return pSNode.release();
}
case TLEFT :