Back to index

nordugrid-arc-nox  1.1.0~rc6
RSLParser.h
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifndef __ARC_RSLPARSER_H__
00004 #define __ARC_RSLPARSER_H__
00005 
00006 #include <list>
00007 #include <map>
00008 #include <string>
00009 #include <iostream>
00010 
00011 namespace Arc {
00012 
00013   class Logger;
00014 
00015   enum RSLBoolOp {
00016     RSLBoolError,
00017     RSLMulti,
00018     RSLAnd,
00019     RSLOr
00020   };
00021 
00022   enum RSLRelOp {
00023     RSLRelError,
00024     RSLEqual,
00025     RSLNotEqual,
00026     RSLLess,
00027     RSLGreater,
00028     RSLLessOrEqual,
00029     RSLGreaterOrEqual
00030   };
00031 
00032   class RSLValue {
00033   public:
00034     RSLValue();
00035     virtual ~RSLValue();
00036     RSLValue* Evaluate(std::map<std::string, std::string>& vars) const;
00037     virtual void Print(std::ostream& os = std::cout) const = 0;
00038   private:
00039     static Logger logger;
00040   };
00041 
00042   class RSLLiteral
00043     : public RSLValue {
00044   public:
00045     RSLLiteral(const std::string& str);
00046     ~RSLLiteral();
00047     void Print(std::ostream& os = std::cout) const;
00048     const std::string& Value() const {
00049       return str;
00050     }
00051   private:
00052     std::string str;
00053   };
00054 
00055   class RSLVariable
00056     : public RSLValue {
00057   public:
00058     RSLVariable(const std::string& var);
00059     ~RSLVariable();
00060     void Print(std::ostream& os = std::cout) const;
00061     const std::string& Var() const {
00062       return var;
00063     }
00064   private:
00065     std::string var;
00066   };
00067 
00068   class RSLConcat
00069     : public RSLValue {
00070   public:
00071     RSLConcat(RSLValue *left, RSLValue *right);
00072     ~RSLConcat();
00073     void Print(std::ostream& os = std::cout) const;
00074     const RSLValue* Left() const {
00075       return left;
00076     }
00077     const RSLValue* Right() const {
00078       return right;
00079     }
00080   private:
00081     RSLValue *left;
00082     RSLValue *right;
00083   };
00084 
00085   class RSLList
00086     : public RSLValue {
00087   public:
00088     RSLList();
00089     ~RSLList();
00090     void Add(RSLValue *value);
00091     void Print(std::ostream& os = std::cout) const;
00092     std::list<RSLValue*>::iterator begin() {
00093       return values.begin();
00094     }
00095     std::list<RSLValue*>::iterator end() {
00096       return values.end();
00097     }
00098     std::list<RSLValue*>::const_iterator begin() const {
00099       return values.begin();
00100     }
00101     std::list<RSLValue*>::const_iterator end() const {
00102       return values.end();
00103     }
00104     std::list<RSLValue*>::size_type size() const {
00105       return values.size();
00106     }
00107   private:
00108     std::list<RSLValue*> values;
00109   };
00110 
00111   class RSLSequence
00112     : public RSLValue {
00113   public:
00114     RSLSequence(RSLList *seq);
00115     ~RSLSequence();
00116     void Print(std::ostream& os = std::cout) const;
00117     std::list<RSLValue*>::iterator begin() {
00118       return seq->begin();
00119     }
00120     std::list<RSLValue*>::iterator end() {
00121       return seq->end();
00122     }
00123     std::list<RSLValue*>::const_iterator begin() const {
00124       return seq->begin();
00125     }
00126     std::list<RSLValue*>::const_iterator end() const {
00127       return seq->end();
00128     }
00129     std::list<RSLValue*>::size_type size() const {
00130       return seq->size();
00131     }
00132   private:
00133     RSLList *seq;
00134   };
00135 
00136   class RSL {
00137   public:
00138     RSL();
00139     virtual ~RSL();
00140     RSL* Evaluate() const;
00141     virtual void Print(std::ostream& os = std::cout) const = 0;
00142   private:
00143     RSL* Evaluate(std::map<std::string, std::string>& vars) const;
00144     static Logger logger;
00145   };
00146 
00147   class RSLBoolean
00148     : public RSL {
00149   public:
00150     RSLBoolean(RSLBoolOp op);
00151     ~RSLBoolean();
00152     void Add(RSL *condition);
00153     void Print(std::ostream& os = std::cout) const;
00154     RSLBoolOp Op() const {
00155       return op;
00156     }
00157     std::list<RSL*>::iterator begin() {
00158       return conditions.begin();
00159     }
00160     std::list<RSL*>::iterator end() {
00161       return conditions.end();
00162     }
00163     std::list<RSL*>::const_iterator begin() const {
00164       return conditions.begin();
00165     }
00166     std::list<RSL*>::const_iterator end() const {
00167       return conditions.end();
00168     }
00169     std::list<RSL*>::size_type size() const {
00170       return conditions.size();
00171     }
00172   private:
00173     RSLBoolOp op;
00174     std::list<RSL*> conditions;
00175   };
00176 
00177   class RSLCondition
00178     : public RSL {
00179   public:
00180     RSLCondition(const std::string& attr, RSLRelOp op, RSLList *values);
00181     ~RSLCondition();
00182     void Print(std::ostream& os = std::cout) const;
00183     const std::string& Attr() const {
00184       return attr;
00185     }
00186     const RSLRelOp Op() const {
00187       return op;
00188     }
00189     std::list<RSLValue*>::iterator begin() {
00190       return values->begin();
00191     }
00192     std::list<RSLValue*>::iterator end() {
00193       return values->end();
00194     }
00195     std::list<RSLValue*>::const_iterator begin() const {
00196       return values->begin();
00197     }
00198     std::list<RSLValue*>::const_iterator end() const {
00199       return values->end();
00200     }
00201     std::list<RSLValue*>::size_type size() const {
00202       return values->size();
00203     }
00204   private:
00205     std::string attr;
00206     RSLRelOp op;
00207     RSLList *values;
00208   };
00209 
00210   class RSLParser {
00211   public:
00212     RSLParser(const std::string s);
00213     ~RSLParser();
00214     // The Parse method returns a pointer to an RSL object containing a
00215     // parsed representation of the rsl string given to the constructor.
00216     // The pointer is owned by the RSLParser object and should not be
00217     // deleted by the caller. The pointer is valid until the RSLParser
00218     // object goes out of scope.
00219     // If the evaluate flag is true the returned RSL object has been
00220     // evaluated to resolve RSL substitutions and evaluate concatenation
00221     // operations.
00222     // The parsing and evaluation is done on the first call to the Parse
00223     // method. Subsequent calls will simply return stored results.
00224     // If the rsl string can not be parsed or evaluated a NULL pointer
00225     // is returned.
00226     // It is possible that an rsl string can be parsed but not evaluated.
00227     const RSL* Parse(bool evaluate = true);
00228   private:
00229     void SkipWS();
00230     RSLBoolOp ParseBoolOp();
00231     RSLRelOp ParseRelOp();
00232     std::string ParseString(int& status);
00233     RSLList* ParseList();
00234     RSL* ParseRSL();
00235     std::string s;
00236     std::string::size_type n;
00237     RSL *parsed;
00238     RSL *evaluated;
00239     static Logger logger;
00240   };
00241 
00242   std::ostream& operator<<(std::ostream& os, const RSLBoolOp op);
00243   std::ostream& operator<<(std::ostream& os, const RSLRelOp op);
00244   std::ostream& operator<<(std::ostream& os, const RSLValue& value);
00245   std::ostream& operator<<(std::ostream& os, const RSL& rsl);
00246 
00247 } // namespace Arc
00248 
00249 #endif // __ARC_RSLPARSER_H__