Expressions.h 10.4 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
//
// 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<T>
//   This abstract base class defines the interface for OPAL scalar
//   expressions whose values have type T.
//
// Template class: Expression::Array<T>
//   This abstract base class defines the interface for OPAL array
//   expressions whose components have type T.
//
// Template class: Expression::PtrToScalar<T>
//   This class implements a pointer which owns a scalar expression
//   of type Expression::Scalar<T>.
//
// Template class: Expression::PtrToArray<T>
//   This class implements a pointer which owns an array expression
//   of type Expression::Array<T>.
//
// Template class: Expression::ArrayOfPtrs<T>
//   This class implements an array of pointers, each of which owns
//   an expression of type Expression::Scalar<T>.
//
// Declared functions:
//   A collection of functions implementing a recursive descent parser.
//
//
37 38 39 40 41
// Copyright (c) 2008-2020
// Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved.
//
// OPAL is licensed under GNU GPL version 3.
gsell's avatar
gsell committed
42
//
43 44 45

#ifndef OPAL_Expressions_HH
#define OPAL_Expressions_HH 1
gsell's avatar
gsell committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

#include "MemoryManagement/OwnPtr.h"
#include "Parser/Token.h"
#include <list>
#include <iosfwd>
#include <string>
#include <vector>

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<T>.
    // ----------------------------------------------------------------------
    /// A scalar expression.

    template <class T> class Scalar {

    public:

        Scalar();
        Scalar(const Scalar &);
        virtual ~Scalar();

        /// Copy scalar expression.
        //  Deep copy of the expression.
        virtual Scalar<T> *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<T>.
    // ----------------------------------------------------------------------
    /// A pointer to a scalar expression.

    template <class T> class PtrToScalar:
        public OwnPtr<Scalar<T> > {

    public:

        /// Constructor from an object just created.
        PtrToScalar(Scalar<T> *rhs);

        PtrToScalar();
        PtrToScalar(const PtrToScalar &rhs);
        ~PtrToScalar();
119
        PtrToScalar& operator=(const PtrToScalar&) = default;
gsell's avatar
gsell committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    };


    // Template class Expression::ArrayOfPtrs<T>.
    // ------------------------------------------------------------------------
    /// An array of pointers to scalar expressions.

    template <class T> class ArrayOfPtrs: public std::vector<PtrToScalar<T> > {

    public:

        ArrayOfPtrs();
        ArrayOfPtrs(int);
        ArrayOfPtrs(const std::vector<PtrToScalar<T> > &rhs);
        ~ArrayOfPtrs();
    };


    // Template class Expression::Array<T>.
    // ----------------------------------------------------------------------
    /// An array expression.

142
    template <class T> class OArray {
gsell's avatar
gsell committed
143 144 145

    public:

146 147 148
        OArray();
        OArray(const OArray &);
        virtual ~OArray();
gsell's avatar
gsell committed
149 150 151

        /// Copy expression.
        //  Deep copy.
152
        virtual OArray<T> *clone() const = 0;
gsell's avatar
gsell committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

        /// Evaluate.
        //  Recursively evaluate the expression and return the result.
        //  Any non-deferred expression is cached.
        virtual std::vector<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.
171
        void operator=(const OArray &);
gsell's avatar
gsell committed
172 173 174 175 176 177 178 179
    };


    // Template class Expression::PtrToArray<T>.
    // ----------------------------------------------------------------------
    /// A pointer to an array expression.

    template <class T> class PtrToArray:
180
        public OwnPtr<OArray<T> > {
gsell's avatar
gsell committed
181 182 183 184

    public:

        /// Constructor from object just created.
185
        PtrToArray(OArray<T> *rhs);
gsell's avatar
gsell committed
186 187 188 189

        PtrToArray();
        PtrToArray(const PtrToArray &rhs);
        ~PtrToArray();
190
        PtrToArray& operator=(const PtrToArray<T>&) = default;
gsell's avatar
gsell committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    };


    // ----------------------------------------------------------------------
    // SCALAR EXPRESSION PARSERS.

    /// Parse boolean expression.
    extern PtrToScalar<bool> parseBool(Statement &);

    /// Parse real expression.
    extern PtrToScalar<double> 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.
Steve Russell's avatar
Steve Russell committed
209
    extern std::string parseString(Statement &, const char msg[]);
gsell's avatar
gsell committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223


    // ARRAY EXPRESSION PARSERS.

    /// Parse boolean array expression.
    extern PtrToArray<bool> parseBoolArray(Statement &);

    /// Parse real array expression.
    extern PtrToArray<double> parseRealArray(Statement &);

    /// Parse real array constant.
    extern PtrToArray<double> parseRealConstArray(Statement &);

    /// Parse string array.
Steve Russell's avatar
Steve Russell committed
224
    extern std::vector<std::string> parseStringArray(Statement &);
gsell's avatar
gsell committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286


    // 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 T> class SRefAttr;

    /// Parse variable reference.
    extern SRefAttr<double> *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<Token> parseTokenList(Statement &);

    /// Parse a token list array (for LIST commands).
    extern std::vector<std::list<Token> > parseTokenListArray(Statement &);


    // SPECIAL PARSER.

    /// Parse table expression (depends on a table's rows).
    extern PtrToScalar<double> parseTableExpression(Statement &, const Table *);


    // Implementation of class Expression::Scalar<T>.
    // ----------------------------------------------------------------------

    template <class T> inline
    Scalar<T>::Scalar()
    {}


    template <class T> inline
    Scalar<T>::Scalar(const Scalar<T> &)
    {}


    template <class T> inline
    Scalar<T>::~Scalar()
    {}


    template <class T> inline
    bool Scalar<T>::isConstant() const {
        return false;
    }


287
    // Implementation of class Expression::OArray<T>.
gsell's avatar
gsell committed
288 289 290
    // ----------------------------------------------------------------------

    template <class T> inline
291
    OArray<T>::OArray()
gsell's avatar
gsell committed
292 293 294 295
    {}


    template <class T> inline
296
    OArray<T>::OArray(const OArray<T> &)
gsell's avatar
gsell committed
297 298 299 300
    {}


    template <class T> inline
301
    OArray<T>::~OArray()
gsell's avatar
gsell committed
302 303 304 305
    {}


    template <class T> inline
306
    bool OArray<T>::isConstant() const {
gsell's avatar
gsell committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        return false;
    }


    // Implementation of class Expression::PtrToScalar<T>.
    // ----------------------------------------------------------------------


    template <class T> inline
    PtrToScalar<T>::PtrToScalar():
        OwnPtr<Scalar<T> >()
    {}


    template <class T> inline
    PtrToScalar<T>::PtrToScalar(const PtrToScalar &rhs):
        OwnPtr<Scalar<T> >(rhs)
    {}


    template <class T> inline
    PtrToScalar<T>::PtrToScalar(Scalar<T> *rhs):
        OwnPtr<Scalar<T> >(rhs)
    {}


    template <class T> inline
    PtrToScalar<T>::~PtrToScalar()
    {}


    // Implementation of class Expression::ArrayOfPtrs<T>.
    // ----------------------------------------------------------------------

    template <class T> inline
    ArrayOfPtrs<T>::ArrayOfPtrs():
        std::vector<PtrToScalar<T> >()
    {}


    template <class T> inline
    ArrayOfPtrs<T>::ArrayOfPtrs(int n):
        std::vector<PtrToScalar<T> >(n, PtrToScalar<T>())
    {}


    template <class T> inline
    ArrayOfPtrs<T>::ArrayOfPtrs(const std::vector<PtrToScalar<T> > &rhs):
        std::vector<PtrToScalar<T> >(rhs)
    {}


    template <class T> inline
    ArrayOfPtrs<T>::~ArrayOfPtrs()
    {}


    // Implementation of class Expression::PtrToArray<T>.
    // ----------------------------------------------------------------------


    template <class T> inline
    PtrToArray<T>::PtrToArray():
370
        OwnPtr<OArray<T> >()
gsell's avatar
gsell committed
371 372 373 374 375
    {}


    template <class T> inline
    PtrToArray<T>::PtrToArray(const PtrToArray &rhs):
376
        OwnPtr<OArray<T> >(rhs)
gsell's avatar
gsell committed
377 378 379 380
    {}


    template <class T> inline
381 382
    PtrToArray<T>::PtrToArray(OArray<T> *rhs):
        OwnPtr<OArray<T> >(rhs)
gsell's avatar
gsell committed
383 384 385 386 387 388 389 390 391 392 393 394 395
    {}


    template <class T> inline
    PtrToArray<T>::~PtrToArray()
    {}


    // End of namespace Expressions.
    // ======================================================================
};

#endif // OPAL_Expressions_HH
396 397 398 399 400 401 402 403

// 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: