OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Expressions.h
Go to the documentation of this file.
1 //
2 // Namespace Expressions
3 // This namespace contains the representations for all OPAL expressions
4 // (scalar and array). It also defines the entire user interface for
5 // the expression parser. All classes derived from these classes are
6 // declared in this same namespace in the module "Expressions".
7 //
8 // The expression parser is a recursive-descent parser. When a parse
9 // routine recognizes an expression, it returns its internal represenation
10 // and skips the corresponding input tokens. In case of error, a
11 // ParseError exception is thrown.
12 //
13 // Template class: Expression::Scalar<T>
14 // This abstract base class defines the interface for OPAL scalar
15 // expressions whose values have type T.
16 //
17 // Template class: Expression::Array<T>
18 // This abstract base class defines the interface for OPAL array
19 // expressions whose components have type T.
20 //
21 // Template class: Expression::PtrToScalar<T>
22 // This class implements a pointer which owns a scalar expression
23 // of type Expression::Scalar<T>.
24 //
25 // Template class: Expression::PtrToArray<T>
26 // This class implements a pointer which owns an array expression
27 // of type Expression::Array<T>.
28 //
29 // Template class: Expression::ArrayOfPtrs<T>
30 // This class implements an array of pointers, each of which owns
31 // an expression of type Expression::Scalar<T>.
32 //
33 // Declared functions:
34 // A collection of functions implementing a recursive descent parser.
35 //
36 //
37 // Copyright (c) 2008-2020
38 // Paul Scherrer Institut, Villigen PSI, Switzerland
39 // All rights reserved.
40 //
41 // OPAL is licensed under GNU GPL version 3.
42 //
43 
44 #ifndef OPAL_Expressions_HH
45 #define OPAL_Expressions_HH 1
46 
48 #include "Parser/Token.h"
49 #include <list>
50 #include <iosfwd>
51 #include <string>
52 #include <vector>
53 
54 class PlaceRep;
55 class RangeRep;
56 class SFunction;
57 class Statement;
58 class Table;
59 class TableRowRep;
60 
61 // ========================================================================
63 
64 namespace Expressions {
65 
66 
67  // Template class Expression::Scalar<T>.
68  // ----------------------------------------------------------------------
70 
71  template <class T> class Scalar {
72 
73  public:
74 
75  Scalar();
76  Scalar(const Scalar &);
77  virtual ~Scalar();
78 
80  // Deep copy of the expression.
81  virtual Scalar<T> *clone() const = 0;
82 
84  // Recursively evaluate the expression and return the result.
85  // Any non-deferred values are cached.
86  virtual T evaluate() const = 0;
87 
89  // Default action assumes non-constant.
90  virtual bool isConstant() const;
91 
93  // Print the expression on the specified stream.
94  // The [b]precedence[/b] parameter controls printing of parentheses.
95  virtual void print(std::ostream &, int precedence = 99) const = 0;
96 
97  private:
98 
99  // Not implemented.
100  void operator=(const Scalar &);
101  };
102 
103 
104  // Template class Expression::PtrToScalar<T>.
105  // ----------------------------------------------------------------------
107 
108  template <class T> class PtrToScalar:
109  public OwnPtr<Scalar<T> > {
110 
111  public:
112 
115 
119  PtrToScalar& operator=(const PtrToScalar&) = default;
120  };
121 
122 
123  // Template class Expression::ArrayOfPtrs<T>.
124  // ------------------------------------------------------------------------
126 
127  template <class T> class ArrayOfPtrs: public std::vector<PtrToScalar<T> > {
128 
129  public:
130 
131  ArrayOfPtrs();
132  ArrayOfPtrs(int);
133  ArrayOfPtrs(const std::vector<PtrToScalar<T> > &rhs);
134  ~ArrayOfPtrs();
135  };
136 
137 
138  // Template class Expression::Array<T>.
139  // ----------------------------------------------------------------------
141 
142  template <class T> class OArray {
143 
144  public:
145 
147  OArray(const OArray &);
148  virtual ~OArray();
149 
151  // Deep copy.
152  virtual OArray<T> *clone() const = 0;
153 
155  // Recursively evaluate the expression and return the result.
156  // Any non-deferred expression is cached.
157  virtual std::vector<T> evaluate() const = 0;
158 
160  // Default action assumes non-constant.
161  virtual bool isConstant() const;
162 
164  // Print the expression on the specified stream.
165  // The [b]precedence[/b] parameter controls printing of parentheses.
166  virtual void print(std::ostream &, int precedence = 99) const = 0;
167 
168  private:
169 
170  // Not implemented.
171  void operator=(const OArray &);
172  };
173 
174 
175  // Template class Expression::PtrToArray<T>.
176  // ----------------------------------------------------------------------
178 
179  template <class T> class PtrToArray:
180  public OwnPtr<OArray<T> > {
181 
182  public:
183 
186 
188  PtrToArray(const PtrToArray &rhs);
190  PtrToArray& operator=(const PtrToArray<T>&) = default;
191  };
192 
193 
194  // ----------------------------------------------------------------------
195  // SCALAR EXPRESSION PARSERS.
196 
199 
202 
204  extern double parseRealConst(Statement &);
205 
207  // When no string is seen, a ParseError is thrown with the message
208  // given as the second argument.
209  extern std::string parseString(Statement &, const char msg[]);
210  extern std::string parseStringValue(Statement &, const char msg[]);
211 
212  // ARRAY EXPRESSION PARSERS.
213 
216 
219 
222 
224  extern std::vector<std::string> parseStringArray(Statement &);
225 
226 
227  // SPECIAL VALUE PARSERS.
228 
230  extern void parseDelimiter(Statement &stat, char delim);
231 
233  extern void parseDelimiter(Statement &stat, const char delim[2]);
234 
236  extern PlaceRep parsePlace(Statement &);
237 
239  extern RangeRep parseRange(Statement &);
240 
241  // Forward declaration.
242  template <class T> class SRefAttr;
243 
246 
249 
251  extern std::list<Token> parseTokenList(Statement &);
252 
254  extern std::vector<std::list<Token> > parseTokenListArray(Statement &);
255 
256 
257  // SPECIAL PARSER.
258 
261 
262 
263  // Implementation of class Expression::Scalar<T>.
264  // ----------------------------------------------------------------------
265 
266  template <class T> inline
268  {}
269 
270 
271  template <class T> inline
273  {}
274 
275 
276  template <class T> inline
278  {}
279 
280 
281  template <class T> inline
282  bool Scalar<T>::isConstant() const {
283  return false;
284  }
285 
286 
287  // Implementation of class Expression::OArray<T>.
288  // ----------------------------------------------------------------------
289 
290  template <class T> inline
292  {}
293 
294 
295  template <class T> inline
297  {}
298 
299 
300  template <class T> inline
302  {}
303 
304 
305  template <class T> inline
306  bool OArray<T>::isConstant() const {
307  return false;
308  }
309 
310 
311  // Implementation of class Expression::PtrToScalar<T>.
312  // ----------------------------------------------------------------------
313 
314 
315  template <class T> inline
317  OwnPtr<Scalar<T> >()
318  {}
319 
320 
321  template <class T> inline
323  OwnPtr<Scalar<T> >(rhs)
324  {}
325 
326 
327  template <class T> inline
329  OwnPtr<Scalar<T> >(rhs)
330  {}
331 
332 
333  template <class T> inline
335  {}
336 
337 
338  // Implementation of class Expression::ArrayOfPtrs<T>.
339  // ----------------------------------------------------------------------
340 
341  template <class T> inline
343  std::vector<PtrToScalar<T> >()
344  {}
345 
346 
347  template <class T> inline
349  std::vector<PtrToScalar<T> >(n, PtrToScalar<T>())
350  {}
351 
352 
353  template <class T> inline
354  ArrayOfPtrs<T>::ArrayOfPtrs(const std::vector<PtrToScalar<T> > &rhs):
355  std::vector<PtrToScalar<T> >(rhs)
356  {}
357 
358 
359  template <class T> inline
361  {}
362 
363 
364  // Implementation of class Expression::PtrToArray<T>.
365  // ----------------------------------------------------------------------
366 
367 
368  template <class T> inline
370  OwnPtr<OArray<T> >()
371  {}
372 
373 
374  template <class T> inline
376  OwnPtr<OArray<T> >(rhs)
377  {}
378 
379 
380  template <class T> inline
382  OwnPtr<OArray<T> >(rhs)
383  {}
384 
385 
386  template <class T> inline
388  {}
389 
390 
391  // End of namespace Expressions.
392  // ======================================================================
393 };
394 
395 #endif // OPAL_Expressions_HH
Representation objects and parsers for attribute expressions.
Definition: Expressions.h:64
std::string parseStringValue(Statement &, const char msg[])
PtrToScalar< bool > parseBool(Statement &)
Parse boolean expression.
PtrToScalar< double > parseReal(Statement &)
Parse real expression.
PtrToArray< double > parseRealConstArray(Statement &)
Parse real array constant.
std::vector< std::string > parseStringArray(Statement &)
Parse string array.
RangeRep parseRange(Statement &)
Parse range specification.
PlaceRep parsePlace(Statement &)
Parse place specification.
SRefAttr< double > * parseReference(Statement &)
Parse variable reference.
std::vector< std::list< Token > > parseTokenListArray(Statement &)
Parse a token list array (for LIST commands).
PtrToArray< double > parseRealArray(Statement &)
Parse real array expression.
PtrToScalar< double > parseTableExpression(Statement &, const Table *)
Parse table expression (depends on a table's rows).
std::list< Token > parseTokenList(Statement &)
Parse a token list (for macro argument and the like).
TableRowRep parseTableRow(Statement &)
Parse a token list (for macro argument and the like).
std::string parseString(Statement &, const char msg[])
Parse string value.
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
double parseRealConst(Statement &)
Parse real constant.
PtrToArray< bool > parseBoolArray(Statement &)
Parse boolean array expression.
A scalar expression.
Definition: Expressions.h:71
void operator=(const Scalar &)
virtual T evaluate() const =0
Evaluate.
virtual Scalar< T > * clone() const =0
Copy scalar expression.
Scalar(const Scalar &)
Definition: Expressions.h:272
virtual bool isConstant() const
Test for constant.
Definition: Expressions.h:282
virtual void print(std::ostream &, int precedence=99) const =0
Print expression.
A pointer to a scalar expression.
Definition: Expressions.h:109
PtrToScalar & operator=(const PtrToScalar &)=default
PtrToScalar(Scalar< T > *rhs)
Constructor from an object just created.
Definition: Expressions.h:328
PtrToScalar(const PtrToScalar &rhs)
Definition: Expressions.h:322
An array of pointers to scalar expressions.
Definition: Expressions.h:127
An array expression.
Definition: Expressions.h:142
virtual bool isConstant() const
Test for constant.
Definition: Expressions.h:306
virtual std::vector< T > evaluate() const =0
Evaluate.
virtual void print(std::ostream &, int precedence=99) const =0
Print expression.
OArray(const OArray &)
Definition: Expressions.h:296
virtual OArray< T > * clone() const =0
Copy expression.
void operator=(const OArray &)
A pointer to an array expression.
Definition: Expressions.h:180
PtrToArray(OArray< T > *rhs)
Constructor from object just created.
Definition: Expressions.h:381
PtrToArray & operator=(const PtrToArray< T > &)=default
PtrToArray(const PtrToArray &rhs)
Definition: Expressions.h:375
An attribute defined as a reference to a scalar.
Definition: SRefAttr.h:48
Representation of a place within a beam line or sequence.
Definition: PlaceRep.h:41
Representation of a range within a beam line or sequence.
Definition: RangeRep.h:34
The base class for all OPAL tables.
Definition: Table.h:42
Representation of a table row reference.
Definition: TableRowRep.h:36
A pointer which owns the object pointed at.
Definition: OwnPtr.h:31
Interface for statements.
Definition: Statement.h:38
Functions of arc length.
Definition: SFunction.h:31