+ wxPluralFormsToken::Number number = m_plural->evaluate(n);
+ if (number < 0 || number > m_nplurals)
+ {
+ return 0;
+ }
+ return number;
+}
+
+
+class wxPluralFormsParser
+{
+public:
+ wxPluralFormsParser(wxPluralFormsScanner& scanner) : m_scanner(scanner) {}
+ bool parse(wxPluralFormsCalculator& rCalculator);
+
+private:
+ wxPluralFormsNode* parsePlural();
+ // stops at T_SEMICOLON, returns 0 if error
+ wxPluralFormsScanner& m_scanner;
+ const wxPluralFormsToken& token() const;
+ bool nextToken();
+
+ wxPluralFormsNode* expression();
+ wxPluralFormsNode* logicalOrExpression();
+ wxPluralFormsNode* logicalAndExpression();
+ wxPluralFormsNode* equalityExpression();
+ wxPluralFormsNode* multiplicativeExpression();
+ wxPluralFormsNode* relationalExpression();
+ wxPluralFormsNode* pmExpression();
+};
+
+bool wxPluralFormsParser::parse(wxPluralFormsCalculator& rCalculator)
+{
+ if (token().type() != wxPluralFormsToken::T_NPLURALS)
+ return false;
+ if (!nextToken())
+ return false;
+ if (token().type() != wxPluralFormsToken::T_ASSIGN)
+ return false;
+ if (!nextToken())
+ return false;
+ if (token().type() != wxPluralFormsToken::T_NUMBER)
+ return false;
+ wxPluralFormsToken::Number nplurals = token().number();
+ if (!nextToken())
+ return false;
+ if (token().type() != wxPluralFormsToken::T_SEMICOLON)
+ return false;
+ if (!nextToken())
+ return false;
+ if (token().type() != wxPluralFormsToken::T_PLURAL)
+ return false;
+ if (!nextToken())
+ return false;
+ if (token().type() != wxPluralFormsToken::T_ASSIGN)
+ return false;
+ if (!nextToken())
+ return false;
+ wxPluralFormsNode* plural = parsePlural();
+ if (plural == 0)
+ return false;
+ if (token().type() != wxPluralFormsToken::T_SEMICOLON)
+ return false;
+ if (!nextToken())
+ return false;
+ if (token().type() != wxPluralFormsToken::T_EOF)
+ return false;
+ rCalculator.init(nplurals, plural);
+ return true;
+}
+
+wxPluralFormsNode* wxPluralFormsParser::parsePlural()
+{
+ wxPluralFormsNode* p = expression();
+ if (p == NULL)
+ {
+ return NULL;
+ }
+ wxPluralFormsNodePtr n(p);
+ if (token().type() != wxPluralFormsToken::T_SEMICOLON)
+ {
+ return NULL;
+ }
+ return n.release();
+}
+
+const wxPluralFormsToken& wxPluralFormsParser::token() const
+{
+ return m_scanner.token();
+}
+
+bool wxPluralFormsParser::nextToken()
+{
+ if (!m_scanner.nextToken())
+ return false;
+ return true;
+}
+
+wxPluralFormsNode* wxPluralFormsParser::expression()
+{
+ wxPluralFormsNode* p = logicalOrExpression();
+ if (p == NULL)
+ return NULL;
+ wxPluralFormsNodePtr n(p);
+ if (token().type() == wxPluralFormsToken::T_QUESTION)
+ {
+ wxPluralFormsNodePtr qn(new wxPluralFormsNode(token()));
+ if (!nextToken())
+ {
+ return 0;
+ }
+ p = expression();
+ if (p == 0)
+ {
+ return 0;
+ }
+ qn->setNode(1, p);
+ if (token().type() != wxPluralFormsToken::T_COLON)
+ {
+ return 0;
+ }
+ if (!nextToken())
+ {
+ return 0;
+ }
+ p = expression();
+ if (p == 0)
+ {
+ return 0;
+ }
+ qn->setNode(2, p);
+ qn->setNode(0, n.release());
+ return qn.release();
+ }
+ return n.release();
+}
+
+wxPluralFormsNode*wxPluralFormsParser::logicalOrExpression()
+{
+ wxPluralFormsNode* p = logicalAndExpression();
+ if (p == NULL)
+ return NULL;
+ wxPluralFormsNodePtr ln(p);
+ if (token().type() == wxPluralFormsToken::T_LOGICAL_OR)
+ {
+ wxPluralFormsNodePtr un(new wxPluralFormsNode(token()));
+ if (!nextToken())
+ {
+ return 0;
+ }
+ p = logicalOrExpression();
+ if (p == 0)
+ {
+ return 0;
+ }
+ wxPluralFormsNodePtr rn(p); // right
+ if (rn->token().type() == wxPluralFormsToken::T_LOGICAL_OR)
+ {
+ // see logicalAndExpression comment
+ un->setNode(0, ln.release());
+ un->setNode(1, rn->releaseNode(0));
+ rn->setNode(0, un.release());
+ return rn.release();
+ }
+
+
+ un->setNode(0, ln.release());
+ un->setNode(1, rn.release());
+ return un.release();
+ }
+ return ln.release();
+}
+
+wxPluralFormsNode* wxPluralFormsParser::logicalAndExpression()
+{
+ wxPluralFormsNode* p = equalityExpression();
+ if (p == NULL)
+ return NULL;
+ wxPluralFormsNodePtr ln(p); // left
+ if (token().type() == wxPluralFormsToken::T_LOGICAL_AND)
+ {
+ wxPluralFormsNodePtr un(new wxPluralFormsNode(token())); // up
+ if (!nextToken())
+ {
+ return NULL;
+ }
+ p = logicalAndExpression();
+ if (p == 0)
+ {
+ return NULL;
+ }
+ wxPluralFormsNodePtr rn(p); // right
+ if (rn->token().type() == wxPluralFormsToken::T_LOGICAL_AND)
+ {
+// transform 1 && (2 && 3) -> (1 && 2) && 3
+// u r
+// l r -> u 3
+// 2 3 l 2
+ un->setNode(0, ln.release());
+ un->setNode(1, rn->releaseNode(0));
+ rn->setNode(0, un.release());
+ return rn.release();
+ }
+
+ un->setNode(0, ln.release());
+ un->setNode(1, rn.release());
+ return un.release();
+ }
+ return ln.release();
+}
+
+wxPluralFormsNode* wxPluralFormsParser::equalityExpression()
+{
+ wxPluralFormsNode* p = relationalExpression();
+ if (p == NULL)
+ return NULL;
+ wxPluralFormsNodePtr n(p);
+ if (token().type() == wxPluralFormsToken::T_EQUAL
+ || token().type() == wxPluralFormsToken::T_NOT_EQUAL)
+ {
+ wxPluralFormsNodePtr qn(new wxPluralFormsNode(token()));
+ if (!nextToken())
+ {
+ return NULL;
+ }
+ p = relationalExpression();
+ if (p == NULL)
+ {
+ return NULL;
+ }
+ qn->setNode(1, p);
+ qn->setNode(0, n.release());
+ return qn.release();
+ }
+ return n.release();
+}
+
+wxPluralFormsNode* wxPluralFormsParser::relationalExpression()
+{
+ wxPluralFormsNode* p = multiplicativeExpression();
+ if (p == NULL)
+ return NULL;
+ wxPluralFormsNodePtr n(p);
+ if (token().type() == wxPluralFormsToken::T_GREATER
+ || token().type() == wxPluralFormsToken::T_LESS
+ || token().type() == wxPluralFormsToken::T_GREATER_OR_EQUAL
+ || token().type() == wxPluralFormsToken::T_LESS_OR_EQUAL)
+ {
+ wxPluralFormsNodePtr qn(new wxPluralFormsNode(token()));
+ if (!nextToken())
+ {
+ return NULL;
+ }
+ p = multiplicativeExpression();
+ if (p == NULL)
+ {
+ return NULL;
+ }
+ qn->setNode(1, p);
+ qn->setNode(0, n.release());
+ return qn.release();
+ }
+ return n.release();
+}
+
+wxPluralFormsNode* wxPluralFormsParser::multiplicativeExpression()
+{
+ wxPluralFormsNode* p = pmExpression();
+ if (p == NULL)
+ return NULL;
+ wxPluralFormsNodePtr n(p);
+ if (token().type() == wxPluralFormsToken::T_REMINDER)
+ {
+ wxPluralFormsNodePtr qn(new wxPluralFormsNode(token()));
+ if (!nextToken())
+ {
+ return NULL;
+ }
+ p = pmExpression();
+ if (p == NULL)
+ {
+ return NULL;
+ }
+ qn->setNode(1, p);
+ qn->setNode(0, n.release());
+ return qn.release();
+ }
+ return n.release();
+}