00001
00002
00003
00004
00005
00006 #ifndef FITYK_EPARSER_H_
00007 #define FITYK_EPARSER_H_
00008
00009 #include <vector>
00010 #include <string>
00011
00012 #include "vm.h"
00013
00014 struct Token;
00015 class Lexer;
00016
00017
00018 class AggregFunc
00019 {
00020 public:
00021 AggregFunc() : counter_(0), v_(0.) {}
00022 virtual ~AggregFunc() {}
00023
00024 void put(double x, int n) { ++counter_; op(x, n); }
00025 virtual double value() const { return v_; }
00026
00027 protected:
00028 int counter_;
00029 double v_;
00030
00031 virtual void op(double x, int n) = 0;
00032 };
00033
00034
00035
00036
00037
00038 class ExpressionParser : public ExprCalculator
00039 {
00040 public:
00041 enum ExpectedType
00042 {
00043 kOperator,
00044 kValue,
00045 kIndex,
00046 };
00047
00048 enum ParseMode
00049 {
00050 kNormalMode,
00051 kAstMode,
00052 kDatasetTrMode,
00053 };
00054
00055
00056 ExpressionParser(const Ftk* F) : ExprCalculator(F), expected_(kValue),
00057 finished_(false) {}
00058
00059
00060 void clear_vm() { vm_.clear_data(); }
00061
00062
00063 void parse_expr(Lexer& lex, int default_ds,
00064 const std::vector<std::string> *custom_vars=NULL,
00065 std::vector<std::string> *new_vars=NULL,
00066 ParseMode mode=kNormalMode);
00067
00068
00069 bool parse_full(Lexer& lex, int default_ds,
00070 const std::vector<std::string> *custom_vars=NULL);
00071
00072
00073 void push_assign_lhs(const Token& t);
00074
00075 private:
00076
00077 std::vector<int> opstack_;
00078
00079
00080
00081
00082
00083 ExpectedType expected_;
00084
00085
00086
00087
00088 bool finished_;
00089
00090 void put_number(double value);
00091 void put_unary_op(Op op);
00092 void put_binary_op(Op op);
00093 void put_function(Op op);
00094 void put_ag_function(Lexer& lex, int ds, AggregFunc& ag);
00095 void put_value_from_curly(Lexer& lex, int ds);
00096 void put_array_var(bool has_index, Op op);
00097 void put_name(Lexer& lex, const std::string& word,
00098 const std::vector<std::string>* custom_vars,
00099 std::vector<std::string>* new_vars,
00100 bool ast_mode);
00101 void put_variable_sth(Lexer& lex, const std::string& name, bool ast_mode);
00102 void put_func_sth(Lexer& lex, const std::string& name, bool ast_mode);
00103 void put_fz_sth(Lexer& lex, char fz, int ds, bool ast_mode);
00104
00105 void pop_onto_que();
00106 void pop_until_bracket();
00107 };
00108
00109 #endif // FITYK_EPARSER_H_
00110