Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef FormulaParserH
00033 #define FormulaParserH
00034
00035 #include <zeusmath/System/Interfaces/IAngle.hpp>
00036 #include <zeusmath/System/Interfaces/IFormulaParserObserver.hpp>
00037 #include <zeusbase/System/ZObject.h>
00038 #include <zeusbase/System/SingleLinkedList.hpp>
00039 #include <zeusbase/System/Interfaces/ISubject.hpp>
00040
00041 BEGIN_NAMESPACE_Zeus
00042
00043 class IFormulaParserObserver;
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00076
00077 enum ETokenType
00078 {
00079 etUnknownToken = 0,
00080 etOpenBracket = 1,
00081 etCloseBracket = 2,
00082 etMul = 3,
00083 etDiff = 4,
00084 etAdd = 5,
00085 etSub = 6,
00086 etExponent = 7,
00087 etListSeperator = 8,
00088 etName = 20,
00089 etNumber = 21
00090 };
00091
00092
00095
00096 zeusmath_class TFormulaParser : public TZObject, public ISubject
00097 {
00098 public:
00099 TFormulaParser(const TString& rFormula = L"");
00100
00101 Retval evalFloatValue(Float& rfValue);
00102
00103 void setFormula(const TString& rValue);
00104 void setAngleSystemProperty(IAngle::ESystem eSystem);
00105
00106
00107 virtual Retval MQUALIFIER attach(IObserver& rObserver);
00108 virtual Retval MQUALIFIER detach(IObserver& rObserver);
00109
00110
00111 MEMORY_MANAGER_DECL
00112
00113 protected:
00114 virtual ~TFormulaParser();
00115
00116 private:
00118 TString m_strFormula;
00120 IAngle::ESystem m_eSystem;
00121
00122
00123
00124
00125
00128
00129 struct TypeToken
00130 {
00132 ETokenType eType;
00134 TString strData;
00135
00136 TypeToken()
00137 {
00138 eType = etUnknownToken;
00139 }
00140
00141 bool operator==(const TypeToken& rInPar) const
00142 {
00143 return (rInPar.eType == eType &&
00144 rInPar.strData == strData);
00145 }
00146
00147 };
00148
00150 TSingleLinkedList<IFormulaParserObserver*> m_lstObservers;
00152 TSingleLinkedList<TypeToken> m_lstTokenList;
00154 Int m_iTokenIndex;
00156 Retval m_retErrorCode;
00157
00158 void notifyObserver(IFormulaParserObserver::EFormulaErrorType eErrorType, const TString& rMessage);
00159 Float parsePrimitive(bool bAllowUnaryOperator,
00160 bool& bError);
00161 Float parseExpression(const Float& rLeftSide,
00162 Int iPrecedence,
00163 bool& bError);
00164 Float parseFunction(const TString& rName, bool& bError);
00165 Float callPow(const Float& rfBasis, const Float& rfExponent, bool& bError);
00166 Int getNextOperatorPrecedence(bool& bError);
00167 bool isExpressionEndReached();
00168
00169 void startScanner();
00170 static bool getNameToken(TypeToken& rToken, const char* pcBuffer, Int& rCounter);
00171 static bool getNumberToken(TypeToken& rToken, const char* pcBuffer, Int& rCounter);
00172
00173
00174 };
00175
00176
00180
00181 inline void TFormulaParser::setAngleSystemProperty(IAngle::ESystem eSystem)
00182 {
00183 m_eSystem = eSystem;
00184 }
00185
00186
00189
00190 inline void TFormulaParser::notifyObserver(IFormulaParserObserver::EFormulaErrorType eErrorType, const TString& rMessage)
00191 {
00192 TIterator<IFormulaParserObserver*> It = m_lstObservers.getIterator();
00193 while(It.hasNextItem())
00194 {
00195 It.getNextItem()->onErrorOccured(eErrorType, rMessage);
00196 }
00197 }
00198
00199 END_NAMESPACE_Zeus
00200
00201 #endif