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