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-2025 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 "functionName.H"
45 #include "variable.H"
46 #include "keyType.H"
47 #include "verbatimString.H"
48 #include "InfoProxy.H"
49 #include "refCount.H"
50 #include "typeInfo.H"
51 
52 #define NoHashTableC
53 #include "runTimeSelectionTables.H"
54 
55 #include <iostream>
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward declaration of friend functions and operators
63 
64 class token;
65 
66 Istream& operator>>(Istream&, token&);
67 Ostream& operator<<(Ostream&, const token&);
68 
69 
70 /*---------------------------------------------------------------------------*\
71  Class token Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 class token
75 {
76 
77 public:
78 
79  //- Enumeration defining the types of token
80  enum tokenType : char
81  {
82  UNDEFINED = 0,
83 
84  PUNCTUATION = char(128),
97  COMPOUND,
98 
100  };
101 
102  //- Standard punctuation tokens
103  enum punctuationToken : char
104  {
105  NULL_TOKEN = '\0',
106  SPACE = ' ',
107  TAB = '\t',
108  NL = '\n',
109 
111  BEGIN_LIST = '(',
112  END_LIST = ')',
113  BEGIN_SQR = '[',
114  END_SQR = ']',
115  BEGIN_BLOCK = '{',
116  END_BLOCK = '}',
117  COLON = ':',
118  COMMA = ',',
119  HASH = '#',
120 
123 
124  ASSIGN = '=',
125  ADD = '+',
126  SUBTRACT = '-',
127  MULTIPLY = '*',
128  DIVIDE = '/'
129  };
130 
131  //- Abstract base class for complex tokens
132  class compound
133  :
134  public refCount
135  {
136  // Private Data
137 
138  bool empty_;
139 
140 
141  public:
142 
143  //- Runtime type information
144  TypeName("compound");
145 
146 
147  //- Declare run-time constructor selection table
149  (
150  autoPtr,
151  compound,
152  Istream,
153  (Istream& is),
154  (is)
155  );
156 
157 
158  // Constructors
159 
160  //- Construct null
161  compound()
162  :
163  empty_(false)
164  {}
165 
166  //- Disallow default bitwise copy construction
167  compound(const compound&) = delete;
168 
169 
170  // Selectors
171 
172  //- Select null constructed
173  static autoPtr<compound> New(const word& type, Istream&);
174 
175 
176  //- Destructor
177  virtual ~compound();
178 
179 
180  // Member Functions
181 
182  //- Return true if name is a compound type
183  static bool isCompound(const word& name);
184 
185  bool empty() const
186  {
187  return empty_;
188  }
189 
190  bool& empty()
191  {
192  return empty_;
193  }
194 
195  virtual label size() const = 0;
196 
197  virtual void write(Ostream&) const = 0;
198 
199 
200  // Member Operators
201 
202  //- Disallow default bitwise assignment
203  void operator=(const compound&) = delete;
204 
205 
206  // IOstream Operators
207 
208  friend Ostream& operator<<(Ostream&, const compound&);
209  };
210 
211 
212  //- A templated class for holding compound tokens
213  template<class T>
214  class Compound
215  :
216  public token::compound,
217  public T
218  {
219  public:
220 
221  //- Runtime type information
222  TypeName("Compound<T>");
223 
224  Compound(Istream& is)
225  :
226  T(is)
227  {}
228 
229  label size() const
230  {
231  return T::size();
232  }
233 
234  void write(Ostream& os) const
235  {
236  operator<<(os, static_cast<const T&>(*this));
237  }
238  };
239 
240 
241  //- Static undefined token
242  static token undefinedToken;
243 
244 
245 private:
246 
247  // Private Data
248 
249  //- List of token type names
250  static const word typeNames_[];
251 
252  //- The token type
253  tokenType type_;
254 
255  //- Anonymous Union of token types
256  union
257  {
271  mutable compound* compoundTokenPtr_;
272  };
273 
274  //- Line number in the file this token was read from
275  label lineNumber_;
276 
277 
278  // Private Member Functions
279 
280  //- Clear any allocated storage (word or string)
281  inline void clear();
282 
283  // Parse error, expected 'expected', found ...
284  void parseError(const char* expected) const;
285 
286 
287 public:
288 
289  // Constructors
290 
291  //- Construct null
292  inline token();
293 
294  //- Copy constructor
295  inline token(const token&);
296 
297  //- Construct punctuation character token
299 
300  //- Construct word token
301  inline token(const word&, label lineNumber=0);
302 
303  //- Construct string token
304  inline token(const string&, label lineNumber=0);
305 
306  //- Construct keyType token
307  inline token(const keyType&, label lineNumber=0);
308 
309  //- Construct verbatimString token
310  inline token(const verbatimString&, label lineNumber=0);
311 
312  //- Construct 32-bit integer token
313  inline token(const int32_t, label lineNumber=0);
314 
315  //- Construct 64-bit integer token
316  inline token(const int64_t, label lineNumber=0);
317 
318  //- Construct unsigned 32-bit integer token
319  inline token(const uint32_t, label lineNumber=0);
320 
321  //- Construct unsigned 64-bit integer token
322  inline token(const uint64_t, label lineNumber=0);
323 
324  //- Construct floatScalar token
325  inline token(const floatScalar, label lineNumber=0);
326 
327  //- Construct doubleScalar token
328  inline token(const doubleScalar, label lineNumber=0);
329 
330  //- Construct longDoubleScalar token
331  inline token(const longDoubleScalar, label lineNumber=0);
332 
333  //- Construct from Istream
334  token(Istream&);
335 
336 
337  //- Destructor
338  inline ~token();
339 
340 
341  // Member Functions
342 
343  // Access
344 
345  inline tokenType type() const;
346  inline tokenType& type();
347 
348  inline bool good() const;
349  inline bool undefined() const;
350  inline bool error() const;
351 
352  //- Return the type name of the token
353  const word& typeName() const;
354 
355  inline bool isPunctuation() const;
356  inline punctuationToken pToken() const;
357 
358  inline bool isWord() const;
359  inline const word& wordToken() const;
360 
361  inline bool isFunctionName() const;
362  inline const functionName& functionNameToken() const;
363 
364  inline bool isVariable() const;
365  inline const variable& variableToken() const;
366 
367  inline bool isString() const;
368  inline const string& stringToken() const;
369 
370  inline bool isVerbatimString() const;
371  inline const verbatimString& verbatimStringToken() const;
372 
373  inline bool isAnyString() const;
374  inline const string& anyStringToken() const;
375 
376  inline bool isInteger32() const;
377  inline int32_t integer32Token() const;
378 
379  inline bool isInteger64() const;
380  inline int64_t integer64Token() const;
381 
382  inline bool isUnsignedInteger32() const;
383  inline uint32_t unsignedInteger32Token() const;
384 
385  inline bool isUnsignedInteger64() const;
386  inline uint64_t unsignedInteger64Token() const;
387 
388  inline bool isLabel() const;
389  inline label labelToken() const;
390 
391  inline bool isULabel() const;
392  inline uLabel uLabelToken() const;
393 
394  inline bool isFloatScalar() const;
395  inline floatScalar floatScalarToken() const;
396 
397  inline bool isDoubleScalar() const;
398  inline doubleScalar doubleScalarToken() const;
399 
400  inline bool isLongDoubleScalar() const;
402 
403  inline bool isScalar() const;
404  inline scalar scalarToken() const;
405 
406  inline bool isNumber() const;
407  inline scalar number() const;
408 
409  inline bool isCompound() const;
410  inline const compound& compoundToken() const;
411  compound& transferCompoundToken(const Istream& is);
412 
413  inline label lineNumber() const;
414  inline label& lineNumber();
415 
416 
417  // Edit
418 
419  //- Set bad
420  inline void setBad();
421 
422 
423  // Info
424 
425  //- Return info proxy.
426  // Used to print token information to a stream
427  InfoProxy<token> info() const
428  {
429  return *this;
430  }
431 
432 
433  // Member Operators
434 
435  // Assignment
436 
437  inline void operator=(const token&);
438 
439  inline void operator=(const punctuationToken);
440 
441  inline void operator=(word*);
442  inline void operator=(const word&);
443 
444  inline void operator=(functionName*);
445  inline void operator=(const functionName&);
446 
447  inline void operator=(variable*);
448  inline void operator=(const variable&);
449 
450  inline void operator=(string*);
451  inline void operator=(const string&);
452 
453  inline void operator=(verbatimString*);
454  inline void operator=(const verbatimString&);
455 
456  inline void operator=(const int32_t);
457  inline void operator=(const int64_t);
458  inline void operator=(const uint32_t);
459  inline void operator=(const uint64_t);
460  inline void operator=(const floatScalar);
461  inline void operator=(const doubleScalar);
462  inline void operator=(const longDoubleScalar);
463 
464  inline void operator=(compound*);
465 
466 
467  // Equality
468 
469  inline bool operator==(const token&) const;
470  inline bool operator==(const punctuationToken) const;
471  inline bool operator==(const word&) const;
472  inline bool operator==(const functionName&) const;
473  inline bool operator==(const variable&) const;
474  inline bool operator==(const string&) const;
475  inline bool operator==(const verbatimString&) const;
476  inline bool operator==(const int32_t) const;
477  inline bool operator==(const int64_t) const;
478  inline bool operator==(const uint32_t) const;
479  inline bool operator==(const uint64_t) const;
480  inline bool operator==(const floatScalar) const;
481  inline bool operator==(const doubleScalar) const;
482  inline bool operator==(const longDoubleScalar) const;
483 
484 
485  // Inequality
486 
487  inline bool operator!=(const token&) const;
488  inline bool operator!=(const punctuationToken) const;
489  inline bool operator!=(const word&) const;
490  inline bool operator!=(const functionName&) const;
491  inline bool operator!=(const variable&) const;
492  inline bool operator!=(const string&) const;
493  inline bool operator!=(const verbatimString&) const;
494  inline bool operator!=(const int32_t) const;
495  inline bool operator!=(const int64_t) const;
496  inline bool operator!=(const uint32_t) const;
497  inline bool operator!=(const uint64_t) const;
498  inline bool operator!=(const floatScalar) const;
499  inline bool operator!=(const doubleScalar) const;
500  inline bool operator!=(const longDoubleScalar) const;
501 
502 
503  // IOstream Operators
504 
506  friend Ostream& operator<<(Ostream&, const token&);
507 
509  friend ostream& operator<<(ostream&, const punctuationToken&);
510 
511  friend ostream& operator<<(ostream&, const InfoProxy<token>&);
512 };
513 
514 
516 ostream& operator<<(ostream&, const token::punctuationToken&);
518 
519 ostream& operator<<(ostream&, const InfoProxy<token>&);
520 
521 template<>
522 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
523 
524 #define defineCompoundTypeName(Type, Name) \
525  defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);
526 
527 #define addCompoundToRunTimeSelectionTable(Type, Name) \
528  token::compound::addIstreamConstructorToTable<token::Compound<Type>> \
529  add##Name##IstreamConstructorToTable_;
530 
531 
532 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
533 
534 } // End namespace Foam
535 
536 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
537 
538 #include "tokenI.H"
539 #include "Istream.H"
540 
541 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
542 
543 #endif
544 
545 // ************************************************************************* //
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
The TAB Method for Numerical Calculation of Spray Droplet Breakup.
Definition: TAB.H:63
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A functionName is a word starting with '#'.
Definition: functionName.H:60
A class for handling keywords in dictionaries.
Definition: keyType.H:69
Reference counter for various OpenFOAM components.
Definition: refCount.H:50
A templated class for holding compound tokens.
Definition: token.H:217
Compound(Istream &is)
Definition: token.H:223
void write(Ostream &os) const
Definition: token.H:233
label size() const
Definition: token.H:228
TypeName("Compound<T>")
Runtime type information.
Abstract base class for complex tokens.
Definition: token.H:134
virtual void write(Ostream &) const =0
TypeName("compound")
Runtime type information.
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:78
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:118
bool empty() const
Definition: token.H:184
compound()
Construct null.
Definition: token.H:160
friend Ostream & operator<<(Ostream &, const compound &)
void operator=(const compound &)=delete
Disallow default bitwise assignment.
virtual label size() const =0
virtual ~compound()
Destructor.
Definition: token.C:71
declareRunTimeSelectionTable(autoPtr, compound, Istream,(Istream &is),(is))
Declare run-time constructor selection table.
A token holds items read from Istream.
Definition: token.H:74
bool isLabel() const
Definition: tokenI.H:615
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:746
const variable & variableToken() const
Definition: tokenI.H:383
compound & transferCompoundToken(const Istream &is)
Definition: token.C:128
friend Ostream & operator<<(Ostream &, const token &)
bool isUnsignedInteger32() const
Definition: tokenI.H:544
bool isNumber() const
Definition: tokenI.H:789
bool isPunctuation() const
Definition: tokenI.H:324
punctuationToken punctuationToken_
Definition: token.H:257
bool isDoubleScalar() const
Definition: tokenI.H:722
tokenType
Enumeration defining the types of token.
Definition: token.H:80
@ ERROR
Definition: token.H:98
@ VARIABLE
Definition: token.H:86
@ WORD
Definition: token.H:84
@ UNSIGNED_INTEGER_32
Definition: token.H:91
@ UNDEFINED
Definition: token.H:81
@ COMPOUND
Definition: token.H:96
@ FLOAT_SCALAR
Definition: token.H:93
@ INTEGER_64
Definition: token.H:90
@ DOUBLE_SCALAR
Definition: token.H:94
@ LONG_DOUBLE_SCALAR
Definition: token.H:95
@ VERBATIMSTRING
Definition: token.H:88
@ FUNCTIONNAME
Definition: token.H:85
@ UNSIGNED_INTEGER_64
Definition: token.H:92
@ INTEGER_32
Definition: token.H:89
@ STRING
Definition: token.H:87
@ PUNCTUATION
Definition: token.H:83
bool isLongDoubleScalar() const
Definition: tokenI.H:741
verbatimString * verbatimStringTokenPtr_
Definition: token.H:262
int32_t integer32Token() const
Definition: tokenI.H:485
compound * compoundTokenPtr_
Definition: token.H:270
bool isVerbatimString() const
Definition: tokenI.H:414
functionName * functionNameTokenPtr_
Definition: token.H:259
floatScalar floatScalarToken_
Definition: token.H:267
const functionName & functionNameToken() const
Definition: tokenI.H:365
variable * variableTokenPtr_
Definition: token.H:260
floatScalar floatScalarToken() const
Definition: tokenI.H:708
punctuationToken
Standard punctuation tokens.
Definition: token.H:103
@ BEGIN_STRING
Definition: token.H:120
@ DIVIDE
Definition: token.H:127
@ BEGIN_BLOCK
Definition: token.H:114
@ BEGIN_SQR
Definition: token.H:112
@ END_BLOCK
Definition: token.H:115
@ END_STRING
Definition: token.H:121
@ ASSIGN
Definition: token.H:123
@ END_STATEMENT
Definition: token.H:109
@ NULL_TOKEN
Definition: token.H:104
@ BEGIN_LIST
Definition: token.H:110
@ SUBTRACT
Definition: token.H:125
@ END_LIST
Definition: token.H:111
@ END_SQR
Definition: token.H:113
@ MULTIPLY
Definition: token.H:126
bool isULabel() const
Definition: tokenI.H:659
const string & stringToken() const
Definition: tokenI.H:401
bool isAnyString() const
Definition: tokenI.H:432
punctuationToken pToken() const
Definition: tokenI.H:329
void setBad()
Set bad.
Definition: tokenI.H:858
bool isUnsignedInteger64() const
Definition: tokenI.H:581
label labelToken() const
Definition: tokenI.H:634
uint32_t unsignedInteger32Token() const
Definition: tokenI.H:556
bool isVariable() const
Definition: tokenI.H:378
friend ostream & operator<<(ostream &, const punctuationToken &)
bool isInteger64() const
Definition: tokenI.H:510
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:426
void operator=(const token &)
Definition: tokenI.H:867
int64_t integer64Token() const
Definition: tokenI.H:519
string * stringTokenPtr_
Definition: token.H:261
bool isScalar() const
Definition: tokenI.H:760
tokenType type() const
Definition: tokenI.H:299
friend Istream & operator>>(Istream &, token &)
const string & anyStringToken() const
Definition: tokenI.H:444
bool isFunctionName() const
Definition: tokenI.H:360
bool isInteger32() const
Definition: tokenI.H:473
int64_t integer64Token_
Definition: token.H:264
bool isCompound() const
Definition: tokenI.H:828
bool error() const
Definition: tokenI.H:319
int32_t integer32Token_
Definition: token.H:263
const compound & compoundToken() const
Definition: tokenI.H:833
bool isFloatScalar() const
Definition: tokenI.H:703
bool undefined() const
Definition: tokenI.H:314
uLabel uLabelToken() const
Definition: tokenI.H:678
bool operator!=(const token &) const
Definition: tokenI.H:1200
const word & typeName() const
Return the type name of the token.
Definition: token.C:101
uint64_t unsignedInteger64Token() const
Definition: tokenI.H:590
doubleScalar doubleScalarToken() const
Definition: tokenI.H:727
bool isString() const
Definition: tokenI.H:396
bool good() const
Definition: tokenI.H:309
doubleScalar doubleScalarToken_
Definition: token.H:268
bool isWord() const
Definition: tokenI.H:342
word * wordTokenPtr_
Definition: token.H:258
token()
Construct null.
Definition: tokenI.H:75
const word & wordToken() const
Definition: tokenI.H:347
scalar scalarToken() const
Definition: tokenI.H:768
const verbatimString & verbatimStringToken() const
Definition: tokenI.H:419
~token()
Destructor.
Definition: tokenI.H:291
uint32_t unsignedInteger32Token_
Definition: token.H:265
bool operator==(const token &) const
Definition: tokenI.H:1067
uint64_t unsignedInteger64Token_
Definition: token.H:266
static token undefinedToken
Static undefined token.
Definition: token.H:241
label lineNumber() const
Definition: tokenI.H:847
scalar number() const
Definition: tokenI.H:799
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:269
A variable is a word with support for additional characters, in particular '$' and '/'.
Definition: variable.H:61
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:63
Namespace for OpenFOAM.
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
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
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
uintWM_LABEL_SIZE_t uLabel
A uLabel is an uint32_t or uint64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: uLabel.H:59
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)
long double longDoubleScalar
Lang double precision floating point scalar type.
Macros to ease declaration of run-time selection tables.
Basic run-time type information using word as the type's name. Used to enhance the standard RTTI to c...