libstx-exparser/ExpressionParser.h

Go to the documentation of this file.
00001 // $Id: ExpressionParser.h 59 2007-07-17 14:43:23Z tb $
00002 
00003 /*
00004  * STX Expression Parser C++ Framework v0.7
00005  * Copyright (C) 2007 Timo Bingmann
00006  *
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or (at your
00010  * option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
00015  * for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #ifndef _STX_ExpressionParser_H_
00029 #define _STX_ExpressionParser_H_
00030 
00031 #include <string>
00032 #include <vector>
00033 #include <map>
00034 #include <assert.h>
00035 #include <boost/smart_ptr.hpp>
00036 #include "AnyScalar.h"
00037 
00039 namespace stx {
00040 
00043 class ExpressionParserException : public std::runtime_error
00044 {
00045 public:
00047     inline ExpressionParserException(const std::string &s) throw()
00048         : std::runtime_error(s)
00049     { }
00050 };
00051 
00055 class ConversionException : public ExpressionParserException
00056 {
00057 public:
00059     inline ConversionException(const std::string &s) throw()
00060         : ExpressionParserException(s)
00061     { }
00062 };
00063 
00067 class ArithmeticException : public ExpressionParserException
00068 {
00069 public:
00071     inline ArithmeticException(const std::string &s) throw()
00072         : ExpressionParserException(s)
00073     { }
00074 };
00075 
00079 class BadSyntaxException : public ExpressionParserException
00080 {
00081 public:
00083     inline BadSyntaxException(const std::string &s) throw()
00084         : ExpressionParserException(s)
00085     { }
00086 };
00087 
00091 class UnknownSymbolException : public ExpressionParserException
00092 {
00093 public:
00095     inline UnknownSymbolException(const std::string &s) throw()
00096         : ExpressionParserException(s)
00097     { }
00098 };
00099 
00103 class BadFunctionCallException : public ExpressionParserException
00104 {
00105 public:
00107     inline BadFunctionCallException(const std::string &s) throw()
00108         : ExpressionParserException(s)
00109     { }
00110 };
00111 
00116 class SymbolTable
00117 {
00118 public:
00120     typedef std::vector<AnyScalar>      paramlist_type;
00121 
00123     virtual ~SymbolTable();
00124 
00126     virtual AnyScalar   lookupVariable(const std::string &varname) const = 0;
00127 
00130     virtual AnyScalar   processFunction(const std::string &funcname,
00131                                         const paramlist_type &paramlist) const = 0;
00132 };
00133 
00138 class EmptySymbolTable : public SymbolTable
00139 {
00140 public:
00142     typedef std::vector<AnyScalar>      paramlist_type;
00143 
00145     virtual ~EmptySymbolTable();
00146 
00149     virtual AnyScalar   lookupVariable(const std::string &varname) const;
00150 
00154     virtual AnyScalar   processFunction(const std::string &funcname,
00155                                         const paramlist_type &paramlist) const;
00156 };
00157 
00163 class BasicSymbolTable : public SymbolTable
00164 {
00165 public:
00167     typedef AnyScalar   (*functionptr_type)(const paramlist_type& paramlist);
00168 
00169 protected:
00170 
00172     typedef std::map<std::string, AnyScalar>    variablemap_type;
00173 
00175     struct FunctionInfo
00176     {
00179         int             arguments;
00180 
00182         functionptr_type func;
00183 
00185         FunctionInfo(int _arguments = 0, functionptr_type _func = NULL)
00186             : arguments(_arguments), func(_func)
00187         {
00188         }
00189     };
00190 
00192     typedef std::map<std::string, struct FunctionInfo>  functionmap_type;
00193 
00194 private:
00196     variablemap_type    variablemap;
00197 
00199     functionmap_type    functionmap;
00200 
00201 protected:
00202     // *** Lots of Standard Functions
00203 
00205     static AnyScalar    funcPI(const paramlist_type& paramlist);
00206 
00208     static AnyScalar    funcSIN(const paramlist_type& paramlist);
00209 
00211     static AnyScalar    funcCOS(const paramlist_type& paramlist);
00212 
00214     static AnyScalar    funcTAN(const paramlist_type& paramlist);
00215 
00217     static AnyScalar    funcABS(const paramlist_type& paramlist);
00218 
00220     static AnyScalar    funcEXP(const paramlist_type& paramlist);
00221 
00223     static AnyScalar    funcLOGN(const paramlist_type& paramlist);
00224 
00226     static AnyScalar    funcPOW(const paramlist_type& paramlist);
00227 
00229     static AnyScalar    funcSQRT(const paramlist_type& paramlist);
00230 
00231 public:
00233     BasicSymbolTable();
00234 
00236     virtual ~BasicSymbolTable();
00237 
00240     virtual AnyScalar   lookupVariable(const std::string &varname) const;
00241 
00244     virtual AnyScalar   processFunction(const std::string &funcname,
00245                                         const paramlist_type &paramlist) const;
00246 
00248     void        setVariable(const std::string& varname, const AnyScalar &value);
00249 
00251     void        setFunction(const std::string& funcname, int arguments, functionptr_type funcptr);
00252 
00254     void        clearVariables();
00255 
00257     void        clearFunctions();
00258 
00260     void        addStandardFunctions();
00261 };
00262 
00267 class ParseNode
00268 {
00269 protected:
00271     inline ParseNode()
00272     { }
00273 
00275     ParseNode(const ParseNode &pn);
00276 
00278     ParseNode& operator=(const ParseNode &pn);
00279     
00280 public:
00283     virtual ~ParseNode()
00284     {
00285     }
00286 
00289     virtual AnyScalar evaluate(const class SymbolTable &st = BasicSymbolTable()) const = 0;
00290 
00296     virtual bool evaluate_const(AnyScalar *dest) const = 0;
00297 
00299     virtual std::string toString() const = 0;
00300 };
00301 
00305 class ParseTree
00306 {
00307 protected:
00310     boost::shared_ptr<ParseNode>        rootnode;
00311 
00312 public:
00315     ParseTree()
00316         : rootnode(static_cast<ParseNode*>(NULL))
00317     {
00318     }
00319 
00321     ParseTree(ParseNode* pt)
00322         : rootnode(pt)
00323     {
00324     }
00325 
00327     inline bool isEmpty() const
00328     {
00329         return (rootnode.get() == NULL);
00330     }
00331 
00334     AnyScalar   evaluate(const class SymbolTable &st = BasicSymbolTable()) const
00335     {
00336         assert(rootnode.get() != NULL);
00337         return rootnode->evaluate(st);
00338     }
00339 
00341     std::string toString() const
00342     {
00343         assert(rootnode.get() != NULL);
00344         return rootnode->toString();
00345     }
00346 };
00347 
00350 const ParseTree parseExpression(const std::string &input);
00351 
00354 std::string parseExpressionXML(const std::string &input);
00355 
00359 class ParseTreeList : public std::vector<ParseTree>
00360 {
00361 protected:
00363     typedef std::vector<ParseTree>      parent_type;
00364 
00365 public:
00368     std::vector<AnyScalar>      evaluate(const class SymbolTable &st = BasicSymbolTable()) const;
00369 
00372     std::string toString() const;
00373 };
00374 
00378 ParseTreeList parseExpressionList(const std::string &input);
00379 
00380 } // namespace stx
00381 
00382 #endif // _STX_ExpressionParser_H_

Generated on Tue Jul 17 16:51:58 2007 for STX Expression Parser by  doxygen 1.5.2