token.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::token
26 
27 Description
28  A token holds items read from Istream.
29 
30 SourceFiles
31  tokenI.H
32  token.C
33  tokenIO.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef token_H
38 #define token_H
39 
40 #include "label.H"
41 #include "uLabel.H"
42 #include "scalar.H"
43 #include "word.H"
44 #include "InfoProxy.H"
45 #include "refCount.H"
46 #include "typeInfo.H"
47 
48 #define NoHashTableC
49 #include "runTimeSelectionTables.H"
50 
51 #include <iostream>
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward declaration of friend functions and operators
59 
60 class token;
61 
62 Istream& operator>>(Istream&, token&);
63 Ostream& operator<<(Ostream&, const token&);
64 
65 
66 /*---------------------------------------------------------------------------*\
67  Class token Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class token
71 {
72 
73 public:
74 
75  //- Enumeration defining the types of token
76  enum tokenType : char
77  {
78  UNDEFINED = 0,
79 
80  PUNCTUATION = char(128),
89  COMPOUND,
90 
91  ERROR
92  };
93 
94  //- Standard punctuation tokens
95  enum punctuationToken : char
96  {
97  NULL_TOKEN = '\0',
98  SPACE = ' ',
99  TAB = '\t',
100  NL = '\n',
103  BEGIN_LIST = '(',
104  END_LIST = ')',
105  BEGIN_SQR = '[',
106  END_SQR = ']',
107  BEGIN_BLOCK = '{',
108  END_BLOCK = '}',
109  COLON = ':',
110  COMMA = ',',
111  HASH = '#',
116  ASSIGN = '=',
117  ADD = '+',
118  SUBTRACT = '-',
119  MULTIPLY = '*',
120  DIVIDE = '/'
121  };
122 
123  //- Abstract base class for complex tokens
124  class compound
125  :
126  public refCount
127  {
128  // Private Data
129 
130  bool empty_;
131 
132 
133  public:
134 
135  //- Runtime type information
136  TypeName("compound");
137 
138 
139  //- Declare run-time constructor selection table
141  (
142  autoPtr,
143  compound,
144  Istream,
145  (Istream& is),
146  (is)
147  );
148 
149 
150  // Constructors
151 
152  //- Construct null
153  compound()
154  :
155  empty_(false)
156  {}
157 
158  //- Disallow default bitwise copy construction
159  compound(const compound&) = delete;
160 
161 
162  // Selectors
163 
164  //- Select null constructed
165  static autoPtr<compound> New(const word& type, Istream&);
166 
167 
168  //- Destructor
169  virtual ~compound();
170 
171 
172  // Member Functions
173 
174  //- Return true if name is a compound type
175  static bool isCompound(const word& name);
177  bool empty() const
178  {
179  return empty_;
180  }
182  bool& empty()
183  {
184  return empty_;
185  }
186 
187  virtual label size() const = 0;
188 
189  virtual void write(Ostream&) const = 0;
190 
191 
192  // Member Operators
193 
194  //- Disallow default bitwise assignment
195  void operator=(const compound&) = delete;
196 
197 
198  // IOstream Operators
199 
200  friend Ostream& operator<<(Ostream&, const compound&);
201  };
202 
203 
204  //- A templated class for holding compound tokens
205  template<class T>
206  class Compound
207  :
208  public token::compound,
209  public T
210  {
211  public:
212 
213  //- Runtime type information
214  TypeName("Compound<T>");
216  Compound(Istream& is)
217  :
218  T(is)
219  {}
221  label size() const
222  {
223  return T::size();
224  }
226  void write(Ostream& os) const
227  {
228  operator<<(os, static_cast<const T&>(*this));
229  }
230  };
231 
232 
233  //- Static undefined token
234  static token undefinedToken;
235 
236 
237 private:
238 
239  // Private Data
240 
241  //- The token type
242  tokenType type_;
243 
244  //- Anonymous Union of token types
245  union
246  {
254  mutable compound* compoundTokenPtr_;
255  };
256 
257  //- Line number in the file this token was read from
258  label lineNumber_;
259 
260 
261  // Private Member Functions
262 
263  //- Clear any allocated storage (word or string)
264  inline void clear();
265 
266  // Parse error, expected 'expected', found ...
267  void parseError(const char* expected) const;
268 
269 
270 public:
271 
272  // Static Data Members
274  static const char* const typeName;
275 
276 
277  // Constructors
278 
279  //- Construct null
280  inline token();
281 
282  //- Copy constructor
283  inline token(const token&);
284 
285  //- Construct punctuation character token
287 
288  //- Construct word token
289  inline token(const word&, label lineNumber=0);
290 
291  //- Construct string token
292  inline token(const string&, label lineNumber=0);
293 
294  //- Construct label token
295  inline token(const label, label lineNumber=0);
296 
297  //- Construct floatScalar token
298  inline token(const floatScalar, label lineNumber=0);
299 
300  //- Construct doubleScalar token
301  inline token(const doubleScalar, label lineNumber=0);
302 
303  //- Construct longDoubleScalar token
304  inline token(const longDoubleScalar, label lineNumber=0);
305 
306  //- Construct from Istream
307  token(Istream&);
308 
309 
310  //- Destructor
311  inline ~token();
312 
313 
314  // Member Functions
315 
316  // Access
317 
318  inline tokenType type() const;
319  inline tokenType& type();
320 
321  inline bool good() const;
322  inline bool undefined() const;
323  inline bool error() const;
324 
325  inline bool isPunctuation() const;
326  inline punctuationToken pToken() const;
327 
328  inline bool isWord() const;
329  inline const word& wordToken() const;
330 
331  inline bool isVariable() const;
332 
333  inline bool isString() const;
334  inline const string& stringToken() const;
335 
336  inline bool isLabel() const;
337  inline label labelToken() const;
338 
339  inline bool isFloatScalar() const;
340  inline floatScalar floatScalarToken() const;
341 
342  inline bool isDoubleScalar() const;
343  inline doubleScalar doubleScalarToken() const;
344 
345  inline bool isLongDoubleScalar() const;
347 
348  inline bool isScalar() const;
349  inline scalar scalarToken() const;
350 
351  inline bool isNumber() const;
352  inline scalar number() const;
353 
354  inline bool isCompound() const;
355  inline const compound& compoundToken() const;
357 
358  inline label lineNumber() const;
359  inline label& lineNumber();
360 
361 
362  // Edit
363 
364  //- Set bad
365  inline void setBad();
366 
367 
368  // Info
369 
370  //- Return info proxy.
371  // Used to print token information to a stream
372  InfoProxy<token> info() const
373  {
374  return *this;
375  }
376 
377 
378  // Member Operators
379 
380  // Assignment
381 
382  inline void operator=(const token&);
383 
384  inline void operator=(const punctuationToken);
385 
386  inline void operator=(word*);
387  inline void operator=(const word&);
388 
389  inline void operator=(string*);
390  inline void operator=(const string&);
391 
392  inline void operator=(const label);
393  inline void operator=(const floatScalar);
394  inline void operator=(const doubleScalar);
395  inline void operator=(const longDoubleScalar);
396 
397  inline void operator=(compound*);
398 
399 
400  // Equality
401 
402  inline bool operator==(const token&) const;
403  inline bool operator==(const punctuationToken) const;
404  inline bool operator==(const word&) const;
405  inline bool operator==(const string&) const;
406  inline bool operator==(const label) const;
407  inline bool operator==(const floatScalar) const;
408  inline bool operator==(const doubleScalar) const;
409  inline bool operator==(const longDoubleScalar) const;
410 
411 
412  // Inequality
413 
414  inline bool operator!=(const token&) const;
415  inline bool operator!=(const punctuationToken) const;
416  inline bool operator!=(const word&) const;
417  inline bool operator!=(const string&) const;
418  inline bool operator!=(const label) const;
419  inline bool operator!=(const floatScalar) const;
420  inline bool operator!=(const doubleScalar) const;
421  inline bool operator!=(const longDoubleScalar) const;
422 
423 
424  // IOstream Operators
425 
426  friend Istream& operator>>(Istream&, token&);
427  friend Ostream& operator<<(Ostream&, const token&);
428 
429  friend Ostream& operator<<(Ostream&, const punctuationToken&);
430  friend ostream& operator<<(ostream&, const punctuationToken&);
431 
432  friend ostream& operator<<(ostream&, const InfoProxy<token>&);
433 };
434 
435 
437 ostream& operator<<(ostream&, const token::punctuationToken&);
439 
440 ostream& operator<<(ostream&, const InfoProxy<token>&);
441 
442 template<>
443 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
445 #define defineCompoundTypeName(Type, Name) \
446  defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);
448 #define addCompoundToRunTimeSelectionTable(Type, Name) \
449  token::compound::addIstreamConstructorToTable<token::Compound<Type>> \
450  add##Name##IstreamConstructorToTable_;
451 
452 
453 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
454 
455 } // End namespace Foam
456 
457 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
458 
459 #include "tokenI.H"
460 #include "Istream.H"
461 
462 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
463 
464 #endif
465 
466 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
doubleScalar doubleScalarToken_
Definition: token.H:251
~token()
Destructor.
Definition: tokenI.H:179
virtual ~compound()
Destructor.
Definition: token.C:53
bool isWord() const
Definition: tokenI.H:230
punctuationToken pToken() const
Definition: tokenI.H:217
bool operator!=(const token &) const
Definition: tokenI.H:649
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
compound & transferCompoundToken(const Istream &is)
Definition: token.C:93
bool isLongDoubleScalar() const
Definition: tokenI.H:327
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:252
compound * compoundTokenPtr_
Definition: token.H:253
Reference counter for various OpenFOAM components.
Definition: refCount.H:49
tUEqn clear()
scalar number() const
Definition: tokenI.H:382
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:371
label labelToken_
Definition: token.H:249
const word & wordToken() const
Definition: tokenI.H:235
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
friend Ostream & operator<<(Ostream &, const compound &)
declareRunTimeSelectionTable(autoPtr, compound, Istream,(Istream &is),(is))
Declare run-time constructor selection table.
A token holds items read from Istream.
Definition: token.H:69
tokenType
Enumeration defining the types of token.
Definition: token.H:75
string * stringTokenPtr_
Definition: token.H:248
TypeName("compound")
Runtime type information.
bool operator==(const token &) const
Definition: tokenI.H:558
scalar scalarToken() const
Definition: tokenI.H:356
friend Istream & operator>>(Istream &, token &)
word * wordTokenPtr_
Definition: token.H:247
bool isNumber() const
Definition: tokenI.H:377
Abstract base class for complex tokens.
Definition: token.H:123
A templated class for holding compound tokens.
Definition: token.H:205
floatScalar floatScalarToken() const
Definition: tokenI.H:294
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
static token undefinedToken
Static undefined token.
Definition: token.H:233
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
void setBad()
Set bad.
Definition: tokenI.H:429
punctuationToken
Standard punctuation tokens.
Definition: token.H:94
bool good() const
Definition: tokenI.H:197
bool isScalar() const
Definition: tokenI.H:346
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
label lineNumber() const
Definition: tokenI.H:418
const compound & compoundToken() const
Definition: tokenI.H:404
bool isVariable() const
Definition: tokenI.H:248
bool undefined() const
Definition: tokenI.H:202
bool empty() const
Definition: token.H:176
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual void write(Ostream &) const =0
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
The TAB Method for Numerical Calculation of Spray Droplet Breakup.
Definition: TAB.H:60
bool isFloatScalar() const
Definition: tokenI.H:289
bool isDoubleScalar() const
Definition: tokenI.H:308
const string & stringToken() const
Definition: tokenI.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:332
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void operator=(const compound &)=delete
Disallow default bitwise assignment.
bool isString() const
Definition: tokenI.H:253
long double longDoubleScalar
Lang double precision floating point scalar type.
bool error() const
Definition: tokenI.H:207
virtual label size() const =0
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
floatScalar floatScalarToken_
Definition: token.H:250
tokenType type() const
Definition: tokenI.H:187
punctuationToken punctuationToken_
Definition: token.H:246
Ostream & operator<<(Ostream &, const ensightPart &)
label labelToken() const
Definition: tokenI.H:276
token()
Construct null.
Definition: tokenI.H:62
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Macros to ease declaration of run-time selection tables.
static const char *const typeName
Definition: token.H:273
doubleScalar doubleScalarToken() const
Definition: tokenI.H:313
compound()
Construct null.
Definition: token.H:152
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
bool isPunctuation() const
Definition: tokenI.H:212
Namespace for OpenFOAM.