Seamly2D
Code documentation
qmuformulabase.cpp
Go to the documentation of this file.
1 /***************************************************************************************************
2  **
3  ** Copyright (C) 2015 Roman Telezhynskyi
4  **
5  ** Permission is hereby granted, free of charge, to any person obtaining a copy of this
6  ** software and associated documentation files (the "Software"), to deal in the Software
7  ** without restriction, including without limitation the rights to use, copy, modify,
8  ** merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9  ** permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  **
11  ** The above copyright notice and this permission notice shall be included in all copies or
12  ** substantial portions of the Software.
13  **
14  ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15  ** NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17  ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  **
20  ******************************************************************************************************/
21 
22 #include "qmuformulabase.h"
23 
24 #include <QChar>
25 #include <QList>
26 #include <QLocale>
27 #include <QMap>
28 #include <QStaticStringData>
29 #include <QStringData>
30 #include <QStringDataPtr>
31 #include <QStringList>
32 
33 namespace qmu
34 {
35 
36 //---------------------------------------------------------------------------------------------------------------------
38  :QmuParser()
39 {
40 }
41 
42 //---------------------------------------------------------------------------------------------------------------------
44 {
45 }
46 
47 //---------------------------------------------------------------------------------------------------------------------
48 /**
49  * @brief InitCharSets init character set for parser.
50  *
51  * QMuParser require setting character set for legal characters. Because we try make our expresion language independent
52  * we set all posible unique characters from all alphabets.
53  *
54  */
56 {
57  //String with all unique symbols for supported alphabets.
58  //See script alphabets.py for generation and more information.
59  //Note. MSVC doesn't support normal string concatenation for long string. That's why we use QStringList in this
60  //case.
61  const QStringList symbols = QStringList()
62  << "ցЀĆЈVӧĎАғΕĖӅИқΝĞơРңњΥҔĦШҫ̆جگĮаҳѕεشԶиһνԾрÃυلՆӝшËՎїPÓՖXӛӟŞãզhëծpóӞնxßվāŁЃֆĉЋ"
63  << "CŬđҐГΒęҘЛΚŘġҠУGاհЫدԱҰгβطԹõлκKՁÀуςهՉÈыvیՑÐSOřӘћաőcӐթèkàѓżűðsķչøӥӔĀփӣІĈЎ"
64  << "ґĐΗЖҙĘȚΟОҡĠآΧЦتЮұİزηжԸغẽοоÁՀقχцÉՈيюÑՐђӋіәťӆўáŠĺѐfөըnñŰӤӨӹոľЁրăЉŭċБӸēłΔҖ"
65  << "ЙŤěΜӜDСձģΤӰЩīņحҮбưԳصδHйԻŇμӲӴсՃمτƠщՋєLQŹՓŕÖYśÞaգĽæiŽիӓîqճöyջþĂօЄӦĊЌΑĒДҗј"
66  << "ΙȘĚМΡéĵĢФūӚΩبĪЬүќαذԲдҷιظԺмρՂфÇωوՊьÏՒTŚĻJբdçժlïӪղtպӫAւąЇčŃЏĕӯЗΖEțŮĝПΞأĥ"
67  << "ĹЧΦثÆӳЯIسŲԵзζԽпξكՅÄчφNMՍӌяӢՕÔWÎŝÜџёźեägխoӒյôwĶBžսüЂĄև̈ЊČƏљΓВҕĔӮΛКĜΣТҥĤک"
68  << "ЪƯخγвŅԴŪضλкԼĴσтÅՄنъÍՌRӕՔZÝŜbåդﻩjíլļrӵմzýռپêЅքćچЍďӱҒЕůėژșΘØҚНğńءΠFҢХħΨҪ"
69  << "ЭųįҶرҲеԷňعθҺнԿفπÂхՇψÊэšՏÒUəÚѝŻşҤӑâeէŐımկòuշÕúտŔ";
70 
72  Q_UNUSED(expUpper)
73  Q_UNUSED(expLower)
74  Q_UNUSED(decimalPoint)
75  Q_UNUSED(groupSeparator)
76 
77  // Defining identifier character sets
78  const QString nameChars = QString() + sign0 + sign1 + sign2 + sign3 + sign4 + sign5 + sign6 + sign7 + sign8 +
79  sign9 + QLatin1String("_@#'") + symbols.join("");
80  DefineNameChars(nameChars);
81 
82  const QString oprtChars = symbols.join("") + positiveSign + negativeSign + QLatin1String("*^/?<>=!$%&|~'_");
83  DefineOprtChars(oprtChars);
84 
85  const QString infixOprtChars = QString() + positiveSign + negativeSign + QLatin1String("*^/?<>=!$%&|~'_");
86  DefineInfixOprtChars(infixOprtChars);
87 }
88 
89 //---------------------------------------------------------------------------------------------------------------------
90 // Factory function for creating new parser variables
91 // This could as well be a function performing database queries.
92 qreal *QmuFormulaBase::AddVariable(const QString &a_szName, void *a_pUserData)
93 {
94  Q_UNUSED(a_szName)
95  Q_UNUSED(a_pUserData)
96 
97  static qreal value = 0;
98  return &value;
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
102 /**
103  * @brief SetSepForTr set separators for translation expression.
104  * @param fromUser true if expression come from user (from dialog).
105  */
106 void QmuFormulaBase::SetSepForTr(bool osSeparator, bool fromUser)
107 {
108  if (fromUser)
109  {
110  const QLocale loc = QLocale();
111  setLocale(loc);
112  SetArgSep(';');
113  if (osSeparator)
114  {
115  setDecimalPoint(loc.decimalPoint());
116  setThousandsSeparator(loc.groupSeparator());
117  return;
118  }
119  }
120 
121  SetSepForEval();//Same separators (internal) as for eval.
122 }
123 
124 //---------------------------------------------------------------------------------------------------------------------
125 /**
126  * @brief SetSepForEval set separators for eval. Each expression eval in internal (C) locale.
127  */
129 {
130  SetArgSep(';');
132  setDecimalPoint('.');
133 }
134 
135 //---------------------------------------------------------------------------------------------------------------------
136 /**
137  * @brief RemoveAll remove token from token list.
138  *
139  * Standard Qt class QMap doesn't have method RemoveAll.
140  * Example: remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
141  *
142  * @param map map with tokens
143  * @param val token that need delete
144  */
145 void QmuFormulaBase::RemoveAll(QMap<int, QString> &map, const QString &val)
146 {
147  const QList<int> listKeys = map.keys(val);//Take all keys that contain token.
148  if (listKeys.size() > 0)
149  {
150  for (int i = 0; i < listKeys.size(); ++i)
151  {
152  map.remove(listKeys.at(i));
153  }
154  }
155 }
156 
157 }// namespace qmu
virtual void InitCharSets() Q_DECL_OVERRIDE
InitCharSets init character set for parser.
virtual ~QmuFormulaBase() Q_DECL_OVERRIDE
void SetSepForTr(bool osSeparator, bool fromUser)
SetSepForTr set separators for translation expression.
void SetSepForEval()
SetSepForEval set separators for eval. Each expression eval in internal (C) locale.
static void RemoveAll(QMap< int, QString > &map, const QString &val)
RemoveAll remove token from token list.
static qreal * AddVariable(const QString &a_szName, void *a_pUserData)
void setDecimalPoint(const QChar &c)
void SetArgSep(char_type cArgSep)
Set argument separator.
void DefineOprtChars(const QString &a_szCharset)
Define the set of valid characters to be used in names of binary operators and postfix operators.
void DefineInfixOprtChars(const QString &a_szCharset)
Define the set of valid characters to be used in names of infix operators.
QLocale m_locale
The locale used by the parser.
void setThousandsSeparator(const QChar &c)
void setLocale(const QLocale &value)
void DefineNameChars(const QString &a_szCharset)
Define the set of valid characters to be used in names of functions, variables, constants.
Mathematical expressions parser.
Definition: qmuparser.h:50
Namespace for mathematical applications.
#define INIT_LOCALE_VARIABLES(locale)
Definition: qmudef.h:38