// // Namespace Expressions // This namespace contains the representations for all OPAL expressions // (scalar and array). It also defines the entire user interface for // the expression parser. All classes derived from these classes are // declared in this same namespace in the module "Expressions". // // The expression parser is a recursive-descent parser. When a parse // routine recognizes an expression, it returns its internal represenation // and skips the corresponding input tokens. In case of error, a // ParseError exception is thrown. // // Template class: Expression::Scalar // This abstract base class defines the interface for OPAL scalar // expressions whose values have type T. // // Template class: Expression::Array // This abstract base class defines the interface for OPAL array // expressions whose components have type T. // // Template class: Expression::PtrToScalar // This class implements a pointer which owns a scalar expression // of type Expression::Scalar. // // Template class: Expression::PtrToArray // This class implements a pointer which owns an array expression // of type Expression::Array. // // Template class: Expression::ArrayOfPtrs // This class implements an array of pointers, each of which owns // an expression of type Expression::Scalar. // // Declared functions: // A collection of functions implementing a recursive descent parser. // // // Copyright (c) 2008-2020 // Paul Scherrer Institut, Villigen PSI, Switzerland // All rights reserved. // // OPAL is licensed under GNU GPL version 3. // #ifndef OPAL_Expressions_HH #define OPAL_Expressions_HH 1 #include "MemoryManagement/OwnPtr.h" #include "Parser/Token.h" #include #include #include #include class PlaceRep; class RangeRep; class SFunction; class Statement; class Table; class TableRowRep; // ======================================================================== /// Representation objects and parsers for attribute expressions. namespace Expressions { // Template class Expression::Scalar. // ---------------------------------------------------------------------- /// A scalar expression. template class Scalar { public: Scalar(); Scalar(const Scalar &); virtual ~Scalar(); /// Copy scalar expression. // Deep copy of the expression. virtual Scalar *clone() const = 0; /// Evaluate. // Recursively evaluate the expression and return the result. // Any non-deferred values are cached. virtual T evaluate() const = 0; /// Test for constant. // Default action assumes non-constant. virtual bool isConstant() const; /// Print expression. // Print the expression on the specified stream. // The [b]precedence[/b] parameter controls printing of parentheses. virtual void print(std::ostream &, int precedence = 99) const = 0; private: // Not implemented. void operator=(const Scalar &); }; // Template class Expression::PtrToScalar. // ---------------------------------------------------------------------- /// A pointer to a scalar expression. template class PtrToScalar: public OwnPtr > { public: /// Constructor from an object just created. PtrToScalar(Scalar *rhs); PtrToScalar(); PtrToScalar(const PtrToScalar &rhs); ~PtrToScalar(); PtrToScalar& operator=(const PtrToScalar&) = default; }; // Template class Expression::ArrayOfPtrs. // ------------------------------------------------------------------------ /// An array of pointers to scalar expressions. template class ArrayOfPtrs: public std::vector > { public: ArrayOfPtrs(); ArrayOfPtrs(int); ArrayOfPtrs(const std::vector > &rhs); ~ArrayOfPtrs(); }; // Template class Expression::Array. // ---------------------------------------------------------------------- /// An array expression. template class OArray { public: OArray(); OArray(const OArray &); virtual ~OArray(); /// Copy expression. // Deep copy. virtual OArray *clone() const = 0; /// Evaluate. // Recursively evaluate the expression and return the result. // Any non-deferred expression is cached. virtual std::vector evaluate() const = 0; /// Test for constant. // Default action assumes non-constant. virtual bool isConstant() const; /// Print expression. // Print the expression on the specified stream. // The [b]precedence[/b] parameter controls printing of parentheses. virtual void print(std::ostream &, int precedence = 99) const = 0; private: // Not implemented. void operator=(const OArray &); }; // Template class Expression::PtrToArray. // ---------------------------------------------------------------------- /// A pointer to an array expression. template class PtrToArray: public OwnPtr > { public: /// Constructor from object just created. PtrToArray(OArray *rhs); PtrToArray(); PtrToArray(const PtrToArray &rhs); ~PtrToArray(); PtrToArray& operator=(const PtrToArray&) = default; }; // ---------------------------------------------------------------------- // SCALAR EXPRESSION PARSERS. /// Parse boolean expression. extern PtrToScalar parseBool(Statement &); /// Parse real expression. extern PtrToScalar parseReal(Statement &); /// Parse real constant. extern double parseRealConst(Statement &); /// Parse string value. // When no string is seen, a ParseError is thrown with the message // given as the second argument. extern std::string parseString(Statement &, const char msg[]); // ARRAY EXPRESSION PARSERS. /// Parse boolean array expression. extern PtrToArray parseBoolArray(Statement &); /// Parse real array expression. extern PtrToArray parseRealArray(Statement &); /// Parse real array constant. extern PtrToArray parseRealConstArray(Statement &); /// Parse string array. extern std::vector parseStringArray(Statement &); // SPECIAL VALUE PARSERS. /// Test for one-character delimiter. extern void parseDelimiter(Statement &stat, char delim); /// Test for two-character delimiter. extern void parseDelimiter(Statement &stat, const char delim[2]); /// Parse place specification. extern PlaceRep parsePlace(Statement &); /// Parse range specification. extern RangeRep parseRange(Statement &); // Forward declaration. template class SRefAttr; /// Parse variable reference. extern SRefAttr *parseReference(Statement &); /// Parse a token list (for macro argument and the like). extern TableRowRep parseTableRow(Statement &); /// Parse a token list (for macro argument and the like). extern std::list parseTokenList(Statement &); /// Parse a token list array (for LIST commands). extern std::vector > parseTokenListArray(Statement &); // SPECIAL PARSER. /// Parse table expression (depends on a table's rows). extern PtrToScalar parseTableExpression(Statement &, const Table *); // Implementation of class Expression::Scalar. // ---------------------------------------------------------------------- template inline Scalar::Scalar() {} template inline Scalar::Scalar(const Scalar &) {} template inline Scalar::~Scalar() {} template inline bool Scalar::isConstant() const { return false; } // Implementation of class Expression::OArray. // ---------------------------------------------------------------------- template inline OArray::OArray() {} template inline OArray::OArray(const OArray &) {} template inline OArray::~OArray() {} template inline bool OArray::isConstant() const { return false; } // Implementation of class Expression::PtrToScalar. // ---------------------------------------------------------------------- template inline PtrToScalar::PtrToScalar(): OwnPtr >() {} template inline PtrToScalar::PtrToScalar(const PtrToScalar &rhs): OwnPtr >(rhs) {} template inline PtrToScalar::PtrToScalar(Scalar *rhs): OwnPtr >(rhs) {} template inline PtrToScalar::~PtrToScalar() {} // Implementation of class Expression::ArrayOfPtrs. // ---------------------------------------------------------------------- template inline ArrayOfPtrs::ArrayOfPtrs(): std::vector >() {} template inline ArrayOfPtrs::ArrayOfPtrs(int n): std::vector >(n, PtrToScalar()) {} template inline ArrayOfPtrs::ArrayOfPtrs(const std::vector > &rhs): std::vector >(rhs) {} template inline ArrayOfPtrs::~ArrayOfPtrs() {} // Implementation of class Expression::PtrToArray. // ---------------------------------------------------------------------- template inline PtrToArray::PtrToArray(): OwnPtr >() {} template inline PtrToArray::PtrToArray(const PtrToArray &rhs): OwnPtr >(rhs) {} template inline PtrToArray::PtrToArray(OArray *rhs): OwnPtr >(rhs) {} template inline PtrToArray::~PtrToArray() {} // End of namespace Expressions. // ====================================================================== }; #endif // OPAL_Expressions_HH // vi: set et ts=4 sw=4 sts=4: // Local Variables: // mode:c // c-basic-offset: 4 // indent-tabs-mode: nil // require-final-newline: nil // End: