SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
InfixExpressionGrammar.h
Go to the documentation of this file.
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_common_InfixExpressionGrammar_h
11 #define smtk_common_InfixExpressionGrammar_h
12 
14 #include <functional>
15 #include <map>
16 #include <string>
17 
18 #include "smtk/CoreExports.h"
19 
22 
23 namespace smtk
24 {
25 namespace common
26 {
27 
28 class SMTKCORE_EXPORT InfixExpressionGrammar
29 {
30 public:
32  ~InfixExpressionGrammar() = default;
33 
34  InfixExpressionGrammar(const InfixExpressionGrammar& other) = default;
35 
36  void addFunction(const std::string& name, const std::function<double(double)>& f);
37  const std::map<std::string, InfixFunction>& functions() const;
38 
39  void setSubsymbolVisitor(const SubsymbolVisitor& f);
40  SubsymbolVisitor subsymbolVisitor() const;
41 
42  // Parses and evaluates |expression|, sets |err| to the status code of the evaluation.
43  // Calls testExpressionSyntax(), so any code from there can be set, in addition to
44  // ERROR_MATH_ERROR.
45  double evaluate(const std::string& expression, InfixExpressionError& err) const;
46 
47  // Tests |expression| for possible errors, without computing its result.
48  // Returns:
49  // ERROR_NONE if successful.
50  // ERROR_INVALID_SYNTAX if |expression| is not a valid infix expression.
51  // ERROR_INVALID_FUNCTION if |expression| uses a function not in |m_functions|.
52  // ERROR_SUBEVALUATION_FAILED if |m_subexpressionFunctor| fails.
53  InfixExpressionError testExpressionSyntax(const std::string& expression) const;
54 
55 private:
56  InfixExpressionError testExpressionSyntax(
57  const std::string& expression,
58  InfixOperators& ops,
59  EvaluationStacks& stacks) const;
60 
61  InfixFunctions m_functions;
62  SubsymbolVisitor m_subsymbolVisitor;
63 };
64 
65 } // namespace common
66 } // namespace smtk
67 
68 #endif // smtk_common_InfixExpressionGrammar_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::common::InfixOperators
Definition: InfixExpressionEvaluation.h:143
InfixExpressionEvaluation.h
smtk::common::InfixExpressionGrammar
Definition: InfixExpressionGrammar.h:28
InfixExpressionError.h
smtk::common::InfixFunctions
Definition: InfixExpressionEvaluation.h:169
smtk::common::EvaluationStacks
Definition: InfixExpressionEvaluation.h:98