Scribus
Open source desktop publishing at your fingertips
fparser.hh
1 /***************************************************************************\
2 |* Function Parser for C++ v3.3.2 *|
3 |*-------------------------------------------------------------------------*|
4 |* Copyright: Juha Nieminen *|
5 \***************************************************************************/
6 
7 #ifndef ONCE_FPARSER_H_
8 #define ONCE_FPARSER_H_
9 
10 #include <string>
11 #include <vector>
12 
13 #include "scribusapi.h"
14 
15 #ifdef FUNCTIONPARSER_SUPPORT_DEBUG_OUTPUT
16 #include <iostream>
17 #endif
18 
19 namespace FPoptimizer_CodeTree { class CodeTree; }
20 
21 class SCRIBUS_API FunctionParser
22 {
23 public:
24  enum ParseErrorType
25  {
26  SYNTAX_ERROR=0, MISM_PARENTH, MISSING_PARENTH, EMPTY_PARENTH,
27  EXPECT_OPERATOR, OUT_OF_MEMORY, UNEXPECTED_ERROR, INVALID_VARS,
28  ILL_PARAMS_AMOUNT, PREMATURE_EOS, EXPECT_PARENTH_FUNC,
29  NO_FUNCTION_PARSED_YET,
30  FP_NO_ERROR
31  };
32 
33 
34  int Parse(const char* Function, const std::string& Vars,
35  bool useDegrees = false);
36  int Parse(const std::string& Function, const std::string& Vars,
37  bool useDegrees = false);
38 
39  void setDelimiterChar(char);
40 
41  const char* ErrorMsg() const;
42  inline ParseErrorType GetParseErrorType() const { return parseErrorType; }
43 
44  double Eval(const double* Vars);
45  inline int EvalError() const { return evalErrorType; }
46 
47  bool AddConstant(const std::string& name, double value);
48  bool AddUnit(const std::string& name, double value);
49 
50  typedef double (*FunctionPtr)(const double*);
51 
52  bool AddFunction(const std::string& name,
53  FunctionPtr, unsigned paramsAmount);
54  bool AddFunction(const std::string& name, FunctionParser&);
55 
56  bool RemoveIdentifier(const std::string& name);
57 
58  void Optimize();
59 
60 
61  int ParseAndDeduceVariables(const std::string& function,
62  int* amountOfVariablesFound = 0,
63  bool useDegrees = false);
64  int ParseAndDeduceVariables(const std::string& function,
65  std::string& resultVarString,
66  int* amountOfVariablesFound = 0,
67  bool useDegrees = false);
68  int ParseAndDeduceVariables(const std::string& function,
69  std::vector<std::string>& resultVars,
70  bool useDegrees = false);
71 
72 
74  ~FunctionParser();
75 
76  // Copy constructor and assignment operator (implemented using the
77  // copy-on-write technique for efficiency):
79  FunctionParser& operator=(const FunctionParser&);
80 
81 
82  void ForceDeepCopy();
83 
84 
85 #ifdef FUNCTIONPARSER_SUPPORT_DEBUG_OUTPUT
86  // For debugging purposes only:
87  void PrintByteCode(std::ostream& dest, bool showExpression = true) const;
88 #endif
89 
90 
91 
92 //========================================================================
93 private:
94 //========================================================================
95 
96 // Private data:
97 // ------------
98  char delimiterChar;
99  ParseErrorType parseErrorType;
100  int evalErrorType;
101 
102  friend class FPoptimizer_CodeTree::CodeTree;
103 
104  struct Data;
105  Data* data;
106 
107  bool useDegreeConversion;
108  unsigned evalRecursionLevel;
109  unsigned StackPtr;
110  const char* errorLocation;
111 
112 
113 // Private methods:
114 // ---------------
115  void CopyOnWrite();
116  bool CheckRecursiveLinking(const FunctionParser*) const;
117  bool NameExists(const char*, unsigned);
118  bool ParseVariables(const std::string&);
119  int ParseFunction(const char*, bool);
120  const char* SetErrorType(ParseErrorType, const char*);
121 
122  void AddFunctionOpcode_CheckDegreesConversion(unsigned);
123  void AddFunctionOpcode(unsigned);
124  inline void AddMultiplicationByConst(double value);
125  template<typename Operation>
126  inline void AddBinaryOperationByConst();
127  inline void incStackPtr();
128  bool CompilePowi(int);
129 
130  const char* CompileIf(const char*);
131  const char* CompileFunctionParams(const char*, unsigned);
132  const char* CompileElement(const char*);
133  const char* CompilePossibleUnit(const char*);
134  const char* CompilePow(const char*);
135  const char* CompileUnaryMinus(const char*);
136  const char* CompileMult(const char*);
137  const char* CompileAddition(const char*);
138  const char* CompileComparison(const char*);
139  const char* CompileAnd(const char*);
140  const char* CompileExpression(const char*);
141 };
142 
143 #endif
Definition: fparser.hh:19
Definition: fpoptimizer.cc:178
Definition: fparser.hh:21