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-2020 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),
93  COMPOUND,
94 
95  ERROR
96  };
97 
98  //- Standard punctuation tokens
99  enum punctuationToken : char
100  {
101  NULL_TOKEN = '\0',
102  SPACE = ' ',
103  TAB = '\t',
104  NL = '\n',
107  BEGIN_LIST = '(',
108  END_LIST = ')',
109  BEGIN_SQR = '[',
110  END_SQR = ']',
111  BEGIN_BLOCK = '{',
112  END_BLOCK = '}',
113  COLON = ':',
114  COMMA = ',',
115  HASH = '#',
120  ASSIGN = '=',
121  ADD = '+',
122  SUBTRACT = '-',
123  MULTIPLY = '*',
124  DIVIDE = '/'
125  };
126 
127  //- Abstract base class for complex tokens
128  class compound
129  :
130  public refCount
131  {
132  // Private Data
133 
134  bool empty_;
135 
136 
137  public:
138 
139  //- Runtime type information
140  TypeName("compound");
141 
142 
143  //- Declare run-time constructor selection table
145  (
146  autoPtr,
147  compound,
148  Istream,
149  (Istream& is),
150  (is)
151  );
152 
153 
154  // Constructors
155 
156  //- Construct null
157  compound()
158  :
159  empty_(false)
160  {}
161 
162  //- Disallow default bitwise copy construction
163  compound(const compound&) = delete;
164 
165 
166  // Selectors
167 
168  //- Select null constructed
169  static autoPtr<compound> New(const word& type, Istream&);
170 
171 
172  //- Destructor
173  virtual ~compound();
174 
175 
176  // Member Functions
177 
178  //- Return true if name is a compound type
179  static bool isCompound(const word& name);
181  bool empty() const
182  {
183  return empty_;
184  }
186  bool& empty()
187  {
188  return empty_;
189  }
190 
191  virtual label size() const = 0;
192 
193  virtual void write(Ostream&) const = 0;
194 
195 
196  // Member Operators
197 
198  //- Disallow default bitwise assignment
199  void operator=(const compound&) = delete;
200 
201 
202  // IOstream Operators
203 
204  friend Ostream& operator<<(Ostream&, const compound&);
205  };
206 
207 
208  //- A templated class for holding compound tokens
209  template<class T>
210  class Compound
211  :
212  public token::compound,
213  public T
214  {
215  public:
216 
217  //- Runtime type information
218  TypeName("Compound<T>");
220  Compound(Istream& is)
221  :
222  T(is)
223  {}
225  label size() const
226  {
227  return T::size();
228  }
230  void write(Ostream& os) const
231  {
232  operator<<(os, static_cast<const T&>(*this));
233  }
234  };
235 
236 
237  //- Static undefined token
238  static token undefinedToken;
239 
240 
241 private:
242 
243  // Private Data
244 
245  //- The token type
246  tokenType type_;
247 
248  //- Anonymous Union of token types
249  union
250  {
261  mutable compound* compoundTokenPtr_;
262  };
263 
264  //- Line number in the file this token was read from
265  label lineNumber_;
266 
267 
268  // Private Member Functions
269 
270  //- Clear any allocated storage (word or string)
271  inline void clear();
272 
273  // Parse error, expected 'expected', found ...
274  void parseError(const char* expected) const;
275 
276 
277 public:
278 
279  // Static Data Members
281  static const char* const typeName;
282 
283 
284  // Constructors
285 
286  //- Construct null
287  inline token();
288 
289  //- Copy constructor
290  inline token(const token&);
291 
292  //- Construct punctuation character token
294 
295  //- Construct word token
296  inline token(const word&, label lineNumber=0);
297 
298  //- Construct string token
299  inline token(const string&, label lineNumber=0);
300 
301  //- Construct verbatimString token
302  inline token(const verbatimString&, label lineNumber=0);
303 
304  //- Construct label token
305  inline token(const label, label lineNumber=0);
306 
307  //- Construct floatScalar token
308  inline token(const floatScalar, label lineNumber=0);
309 
310  //- Construct doubleScalar token
311  inline token(const doubleScalar, label lineNumber=0);
312 
313  //- Construct longDoubleScalar token
314  inline token(const longDoubleScalar, label lineNumber=0);
315 
316  //- Construct from Istream
317  token(Istream&);
318 
319 
320  //- Destructor
321  inline ~token();
322 
323 
324  // Member Functions
325 
326  // Access
327 
328  inline tokenType type() const;
329  inline tokenType& type();
330 
331  inline bool good() const;
332  inline bool undefined() const;
333  inline bool error() const;
334 
335  inline bool isPunctuation() const;
336  inline punctuationToken pToken() const;
337 
338  inline bool isWord() const;
339  inline const word& wordToken() const;
340 
341  inline bool isFunctionName() const;
342  inline const functionName& functionNameToken() const;
343 
344  inline bool isVariable() const;
345  inline const variable& variableToken() const;
346 
347  inline bool isString() const;
348  inline const string& stringToken() const;
349 
350  inline bool isVerbatimString() const;
351  inline const verbatimString& verbatimStringToken() const;
352 
353  inline bool isAnyString() const;
354  inline const string& anyStringToken() const;
355 
356  inline bool isLabel() const;
357  inline label labelToken() const;
358 
359  inline bool isFloatScalar() const;
360  inline floatScalar floatScalarToken() const;
361 
362  inline bool isDoubleScalar() const;
363  inline doubleScalar doubleScalarToken() const;
364 
365  inline bool isLongDoubleScalar() const;
367 
368  inline bool isScalar() const;
369  inline scalar scalarToken() const;
370 
371  inline bool isNumber() const;
372  inline scalar number() const;
373 
374  inline bool isCompound() const;
375  inline const compound& compoundToken() const;
377 
378  inline label lineNumber() const;
379  inline label& lineNumber();
380 
381 
382  // Edit
383 
384  //- Set bad
385  inline void setBad();
386 
387 
388  // Info
389 
390  //- Return info proxy.
391  // Used to print token information to a stream
392  InfoProxy<token> info() const
393  {
394  return *this;
395  }
396 
397 
398  // Member Operators
399 
400  // Assignment
401 
402  inline void operator=(const token&);
403 
404  inline void operator=(const punctuationToken);
405 
406  inline void operator=(word*);
407  inline void operator=(const word&);
408 
409  inline void operator=(functionName*);
410  inline void operator=(const functionName&);
411 
412  inline void operator=(variable*);
413  inline void operator=(const variable&);
414 
415  inline void operator=(string*);
416  inline void operator=(const string&);
417 
418  inline void operator=(verbatimString*);
419  inline void operator=(const verbatimString&);
420 
421  inline void operator=(const label);
422  inline void operator=(const floatScalar);
423  inline void operator=(const doubleScalar);
424  inline void operator=(const longDoubleScalar);
425 
426  inline void operator=(compound*);
427 
428 
429  // Equality
430 
431  inline bool operator==(const token&) const;
432  inline bool operator==(const punctuationToken) const;
433  inline bool operator==(const word&) const;
434  inline bool operator==(const functionName&) const;
435  inline bool operator==(const variable&) const;
436  inline bool operator==(const string&) const;
437  inline bool operator==(const verbatimString&) const;
438  inline bool operator==(const label) const;
439  inline bool operator==(const floatScalar) const;
440  inline bool operator==(const doubleScalar) const;
441  inline bool operator==(const longDoubleScalar) const;
442 
443 
444  // Inequality
445 
446  inline bool operator!=(const token&) const;
447  inline bool operator!=(const punctuationToken) const;
448  inline bool operator!=(const word&) const;
449  inline bool operator!=(const functionName&) const;
450  inline bool operator!=(const variable&) const;
451  inline bool operator!=(const string&) const;
452  inline bool operator!=(const verbatimString&) const;
453  inline bool operator!=(const label) const;
454  inline bool operator!=(const floatScalar) const;
455  inline bool operator!=(const doubleScalar) const;
456  inline bool operator!=(const longDoubleScalar) const;
457 
458 
459  // IOstream Operators
460 
461  friend Istream& operator>>(Istream&, token&);
462  friend Ostream& operator<<(Ostream&, const token&);
463 
464  friend Ostream& operator<<(Ostream&, const punctuationToken&);
465  friend ostream& operator<<(ostream&, const punctuationToken&);
466 
467  friend ostream& operator<<(ostream&, const InfoProxy<token>&);
468 };
469 
470 
472 ostream& operator<<(ostream&, const token::punctuationToken&);
474 
475 ostream& operator<<(ostream&, const InfoProxy<token>&);
476 
477 template<>
478 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
480 #define defineCompoundTypeName(Type, Name) \
481  defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);
483 #define addCompoundToRunTimeSelectionTable(Type, Name) \
484  token::compound::addIstreamConstructorToTable<token::Compound<Type>> \
485  add##Name##IstreamConstructorToTable_;
486 
487 
488 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
489 
490 } // End namespace Foam
491 
492 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
493 
494 #include "tokenI.H"
495 #include "Istream.H"
496 
497 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
498 
499 #endif
500 
501 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
A class for handling verbatimStrings, derived from string.
const variable & variableToken() const
Definition: tokenI.H:302
doubleScalar doubleScalarToken_
Definition: token.H:258
const functionName & functionNameToken() const
Definition: tokenI.H:284
~token()
Destructor.
Definition: tokenI.H:210
virtual ~compound()
Destructor.
Definition: token.C:53
bool isWord() const
Definition: tokenI.H:261
punctuationToken pToken() const
Definition: tokenI.H:248
bool operator!=(const token &) const
Definition: tokenI.H:835
tUEqn clear()
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:448
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:259
compound * compoundTokenPtr_
Definition: token.H:260
Reference counter for various OpenFOAM components.
Definition: refCount.H:49
scalar number() const
Definition: tokenI.H:503
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:391
label labelToken_
Definition: token.H:256
const word & wordToken() const
Definition: tokenI.H:266
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:72
tokenType
Enumeration defining the types of token.
Definition: token.H:78
string * stringTokenPtr_
Definition: token.H:254
const verbatimString & verbatimStringToken() const
Definition: tokenI.H:338
TypeName("compound")
Runtime type information.
bool operator==(const token &) const
Definition: tokenI.H:726
scalar scalarToken() const
Definition: tokenI.H:477
bool isVerbatimString() const
Definition: tokenI.H:333
friend Istream & operator>>(Istream &, token &)
functionName * functionNameTokenPtr_
Definition: token.H:252
word * wordTokenPtr_
Definition: token.H:251
bool isNumber() const
Definition: tokenI.H:498
Abstract base class for complex tokens.
Definition: token.H:127
A templated class for holding compound tokens.
Definition: token.H:209
floatScalar floatScalarToken() const
Definition: tokenI.H:415
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
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:237
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
verbatimString * verbatimStringTokenPtr_
Definition: token.H:255
void setBad()
Set bad.
Definition: tokenI.H:550
punctuationToken
Standard punctuation tokens.
Definition: token.H:98
bool good() const
Definition: tokenI.H:228
bool isScalar() const
Definition: tokenI.H:467
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
label lineNumber() const
Definition: tokenI.H:539
const compound & compoundToken() const
Definition: tokenI.H:525
bool isVariable() const
Definition: tokenI.H:297
const string & anyStringToken() const
Definition: tokenI.H:363
bool undefined() const
Definition: tokenI.H:233
bool empty() const
Definition: token.H:180
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
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:410
bool isDoubleScalar() const
Definition: tokenI.H:429
const string & stringToken() const
Definition: tokenI.H:320
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:453
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:315
long double longDoubleScalar
Lang double precision floating point scalar type.
bool error() const
Definition: tokenI.H:238
bool isAnyString() const
Definition: tokenI.H:351
virtual label size() const =0
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
floatScalar floatScalarToken_
Definition: token.H:257
tokenType type() const
Definition: tokenI.H:218
punctuationToken punctuationToken_
Definition: token.H:250
Ostream & operator<<(Ostream &, const ensightPart &)
label labelToken() const
Definition: tokenI.H:397
token()
Construct null.
Definition: tokenI.H:74
bool isFunctionName() const
Definition: tokenI.H:279
variable * variableTokenPtr_
Definition: token.H:253
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:280
doubleScalar doubleScalarToken() const
Definition: tokenI.H:434
compound()
Construct null.
Definition: token.H:156
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
A variable is a word with support for additional characters, in particular &#39;$&#39; and &#39;/&#39;...
Definition: variable.H:58
bool isPunctuation() const
Definition: tokenI.H:243
Namespace for OpenFOAM.