summaryrefslogtreecommitdiff
path: root/src/libs/modelinglib/qstringparser
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/modelinglib/qstringparser')
-rw-r--r--src/libs/modelinglib/qstringparser/qstringparser.cpp212
-rw-r--r--src/libs/modelinglib/qstringparser/qstringparser.h147
-rw-r--r--src/libs/modelinglib/qstringparser/qstringparser.pri7
3 files changed, 366 insertions, 0 deletions
diff --git a/src/libs/modelinglib/qstringparser/qstringparser.cpp b/src/libs/modelinglib/qstringparser/qstringparser.cpp
new file mode 100644
index 0000000000..dbacef18fc
--- /dev/null
+++ b/src/libs/modelinglib/qstringparser/qstringparser.cpp
@@ -0,0 +1,212 @@
+/***************************************************************************
+**
+** Copyright (C) 2015 Jochen Becher
+** Contact: http://www.qt.io/licensing
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms and
+** conditions see http://www.qt.io/terms-conditions. For further information
+** use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "qstringparser.h"
+
+// Possible Improvements
+// %w: skip optional whitespaces in m_source
+// more types: different int types, float, char, word, some character delimited string
+// some Q Types: QColor (named, hex rgb)
+// introduce public type ParseState which can be got from Parser and gives some info like error state, error index etc
+
+QStringParser::QStringParser(const QString &source)
+ : m_source(source)
+{
+}
+
+QStringParser::~QStringParser()
+{
+}
+
+QStringParser::Parser QStringParser::parse(const QString &pattern)
+{
+ return Parser(m_source, pattern);
+}
+
+QStringParser::Parser::Parser(const QString &source, const QString &pattern)
+ : m_source(source),
+ m_pattern(pattern)
+{
+}
+
+QStringParser::Parser::~Parser()
+{
+ evaluate();
+ qDeleteAll(m_nodes);
+}
+
+bool QStringParser::Parser::failed()
+{
+ evaluate();
+ return m_evaluationFailed;
+}
+
+bool QStringParser::Parser::scan(int *i, int *index)
+{
+ *i = 0;
+ int sign = 1;
+ while (*index < m_source.length() && m_source.at(*index).isSpace())
+ ++(*index);
+ if (*index >= m_source.length())
+ return false;
+ if (m_source.at(*index) == QLatin1Char('+')) {
+ ++(*index);
+ } else if (m_source.at(*index) == QLatin1Char('-')) {
+ sign = -1;
+ ++(*index);
+ }
+ if (*index >= m_source.length() || !m_source.at(*index).isDigit())
+ return false;
+ while (*index < m_source.length() && m_source.at(*index).isDigit()) {
+ *i = *i * 10 + m_source.at(*index).digitValue();
+ ++(*index);
+ }
+ *i *= sign;
+ return true;
+}
+
+bool QStringParser::Parser::scan(double *d, int *index)
+{
+ int startIndex = *index;
+ // skip whitespaces
+ while (*index < m_source.length() && m_source.at(*index).isSpace())
+ ++(*index);
+ if (*index >= m_source.length())
+ return false;
+ // sign
+ if (m_source.at(*index) == QLatin1Char('+'))
+ ++(*index);
+ else if (m_source.at(*index) == QLatin1Char('-'))
+ ++(*index);
+ // int
+ while (*index < m_source.length() && m_source.at(*index).isDigit())
+ ++(*index);
+ // point
+ if (*index < m_source.length() && m_source.at(*index) == QLatin1Char('.'))
+ ++(*index);
+ // int
+ while (*index < m_source.length() && m_source.at(*index).isDigit())
+ ++(*index);
+ // exponent
+ if (*index < m_source.length() && m_source.at(*index).toLower() == QLatin1Char('e')) {
+ ++(*index);
+ if (*index >= m_source.length())
+ return false;
+ // sign
+ if (m_source.at(*index) == QLatin1Char('+'))
+ ++(*index);
+ else if (m_source.at(*index) == QLatin1Char('-'))
+ ++(*index);
+ // int
+ while (*index < m_source.length() && m_source.at(*index).isDigit())
+ ++(*index);
+ }
+ bool ok = false;
+ *d = m_source.mid(startIndex, *index - startIndex).toDouble(&ok);
+ return ok;
+}
+
+void QStringParser::Parser::evaluate()
+{
+ if (!m_isEvaluated) {
+ m_isEvaluated = true;
+ m_evaluationFailed = false;
+
+ int p = 0;
+ int i = 0;
+ while (p < m_pattern.length()) {
+ if (m_pattern.at(p) == QLatin1Char('%')) {
+ ++p;
+ // a % must be followed by a another char.
+ if (p >= m_pattern.length()) {
+ // syntax error in pattern
+ m_evaluationFailed = true;
+ return;
+ }
+ if (m_pattern.at(p) == QLatin1Char('%')) {
+ // two %% are handled like a simple % in m_source
+ ++p;
+ if (i >= m_source.length() || m_source.at(i) != QLatin1Char('%')) {
+ m_evaluationFailed = true;
+ return;
+ }
+ ++i;
+ } else if (m_pattern.at(p).isDigit()) {
+ // now extract a value matching the Nth node type
+ int N = 0;
+ while (p < m_pattern.length() && m_pattern.at(p).isDigit()) {
+ N = N * 10 + m_pattern.at(p).digitValue();
+ ++p;
+ }
+ if (N < 1 || N > m_nodes.length()) {
+ // argument out of bounds in pattern
+ m_evaluationFailed = true;
+ return;
+ }
+ if (!m_nodes.at(N-1)->accept(*this, &i)) {
+ m_evaluationFailed = true;
+ return;
+ }
+ } else {
+ // any other % syntax is an error
+ m_evaluationFailed = true;
+ return;
+ }
+ } else {
+ if (m_pattern.at(p).isSpace()) {
+ ++p;
+ // m_source must end or have at least one space
+ if (i < m_source.length() && !m_source.at(i).isSpace()) {
+ m_evaluationFailed = true;
+ return;
+ }
+ // skip spaces in m_pattern
+ while (p < m_pattern.length() && m_pattern.at(p).isSpace())
+ ++p;
+
+ // skip spaces in m_source
+ while (i < m_source.length() && m_source.at(i).isSpace())
+ ++i;
+ } else if (i >= m_source.length() || m_source.at(i) != m_pattern.at(p)) {
+ m_evaluationFailed = true;
+ return;
+ } else {
+ ++p;
+ ++i;
+ }
+ }
+ }
+ // m_source and m_pattern must both be scanned completely
+ if (i < m_source.length() || p < m_pattern.length()) {
+ m_evaluationFailed = true;
+ return;
+ }
+ }
+}
diff --git a/src/libs/modelinglib/qstringparser/qstringparser.h b/src/libs/modelinglib/qstringparser/qstringparser.h
new file mode 100644
index 0000000000..e8a5ccbd56
--- /dev/null
+++ b/src/libs/modelinglib/qstringparser/qstringparser.h
@@ -0,0 +1,147 @@
+/***************************************************************************
+**
+** Copyright (C) 2015 Jochen Becher
+** Contact: http://www.qt.io/licensing
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms and
+** conditions see http://www.qt.io/terms-conditions. For further information
+** use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef QSTRINGPARSER_H
+#define QSTRINGPARSER_H
+
+#include <QString>
+#include <QList>
+
+class QStringParser
+{
+private:
+ class Parser
+ {
+ private:
+ class Node
+ {
+ public:
+ virtual ~Node() { }
+ virtual bool accept(Parser &visitor, int *index) = 0;
+ };
+
+ template<typename V>
+ class RefNode : public Node
+ {
+ public:
+ explicit RefNode(V &v) : m_v(v) { }
+ bool accept(Parser &visitor, int *index) override { return visitor.visit(this, index); }
+ V &ref() const { return m_v; }
+ private:
+ V &m_v;
+ };
+
+ template<class U, typename V>
+ class SetterNode : public Node
+ {
+ public:
+ SetterNode(U &u, void (U::*setter)(V)) : m_object(u), m_setter(setter) { }
+ bool accept(Parser &visitor, int *index) override{ return visitor.visit(this, index); }
+ U &object() const { return m_object; }
+ void (U::*setter() const)(V) { return m_setter; }
+ private:
+ U &m_object;
+ void (U::*m_setter)(V) = 0;
+ };
+
+ public:
+ Parser(const QString &source, const QString &pattern);
+ ~Parser();
+
+ template<typename V>
+ Parser &arg(V &v)
+ {
+ m_nodes.append(new RefNode<V>(v));
+ return *this;
+ }
+
+ template<class U, typename V>
+ Parser &arg(U &u, void (U::*setter)(V))
+ {
+ m_nodes.append(new SetterNode<U, V>(u, setter));
+ return *this;
+ }
+
+ bool failed();
+
+ private:
+ bool scan(int *i, int *index);
+ bool scan(double *d, int *index);
+
+ template<typename V>
+ bool visit(RefNode<V> *node, int *index)
+ {
+ V v = 0;
+ if (!scan(&v, index))
+ return false;
+ node->ref() = v;
+ return true;
+ }
+
+ template<class U, typename V>
+ bool visit(SetterNode<U, V> *node, int *index)
+ {
+ V v = 0;
+ if (!scan(&v, index))
+ return false;
+ (node->object().*(node->setter()))(v);
+ return true;
+ }
+
+ template<class U, typename V>
+ bool visit(SetterNode<U, const V &> *node, int *index)
+ {
+ V v = 0;
+ if (!scan(&v, index))
+ return false;
+ (node->object().*(node->setter()))(v);
+ return true;
+ }
+
+ void evaluate();
+
+ const QString m_source;
+ const QString m_pattern;
+ bool m_isEvaluated = false;
+ bool m_evaluationFailed = false;
+ QList<Node *> m_nodes;
+ };
+
+public:
+ explicit QStringParser(const QString &source);
+ ~QStringParser();
+
+ Parser parse(const QString &pattern);
+
+private:
+ const QString m_source;
+};
+
+#endif // QSTRINGPARSER_H
diff --git a/src/libs/modelinglib/qstringparser/qstringparser.pri b/src/libs/modelinglib/qstringparser/qstringparser.pri
new file mode 100644
index 0000000000..4220a09895
--- /dev/null
+++ b/src/libs/modelinglib/qstringparser/qstringparser.pri
@@ -0,0 +1,7 @@
+
+HEADERS += \
+ $$PWD/qstringparser.h
+
+SOURCES += \
+ $$PWD/qstringparser.cpp
+