token.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 // based on OpenFOAM stream, with some modifications/simplifications
21 // to be tailored to our needs
22 
23 
24 #ifndef __token_hpp__
25 #define __token_hpp__
26 
27 
28 #include "bTypes.hpp"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace pFlow
33 {
34 
35 // Forward Declarations
36 class token;
37 class iIstream;
38 class iOstream;
39 iOstream& operator<<(iOstream& os, const token& tok);
40 
41 
42 class token
43 {
44 public:
45 
46  //- Enumeration defining the types of token.
47  // Since these values are also used to tag content in Pstream,
48  // the maximum number of types is limited to 30.
49  enum tokenType
50  {
51  UNDEFINED = 0,
52 
53  // Fundamental types
54  FLAG,
56  BOOL,
60 
61  // Pointer types
62  WORD,
66 
68  };
69 
70 
71  //**************- Stream or output control flags (1-byte width)
72  enum flagType
73  {
74  NO_FLAG = 0,
75  ASCII = 1,
76  BINARY = 2
77  };
78 
79 
80  //- Standard punctuation tokens (a character)
81  enum punctuationToken : char
82  {
83  NULL_TOKEN = '\0',
84  SPACE = ' ',
85  TAB = '\t',
86  NL = '\n',
87 
88  END_STATEMENT = ';',
89  BEGIN_LIST = '(',
90  END_LIST = ')',
91  BEGIN_SQR = '[',
92  END_SQR = ']',
93  BEGIN_BLOCK = '{',
94  END_BLOCK = '}',
95  COLON = ':',
96  COMMA = ',',
97  DOLLAR = '$',
98  SQUOTE = '\'',
99  DQUOTE = '"',
100 
101  SUBTRACT = '-',
102  DIVIDE = '/',
103 
106  };
107 
108  //- An undefined token
109  static const inline token undefinedToken();
110 
111  static token endList()
112  {
113  return token(punctuationToken::END_LIST);
114  }
115 
116  static token beginList()
117  {
118  return token(punctuationToken::BEGIN_LIST);
119  }
120 
122  {
123  return token(punctuationToken::END_STATEMENT);
124  }
125 
126  static token beginBlock()
127  {
128  return token(punctuationToken::BEGIN_BLOCK);
129  }
130 
131  static token endBlocK()
132  {
133  return token(punctuationToken::END_BLOCK);
134  }
135 
137  {
138  return token(punctuationToken::BEGIN_SQR);
139  }
140 
141  static token endSquare()
142  {
143  return token(punctuationToken::END_SQR);
144  }
145 
146  static token space()
147  {
148  return token(punctuationToken::SPACE);
149  }
150 
151  static token newLine()
152  {
153  return token(punctuationToken::NL);
154  }
155 
156 private:
157 
158  //- A %union of token types
159  union content
160  {
161  // Fundamental values. Largest first for any {} initialization.
162  int64_t int64Val;
163 
164  int flagVal; // bitmask - stored as int, not enum
166  float floatVal;
167  double doubleVal;
168 
169  // Pointers
172  };
173 
174 
175  // Private Data
176 
177  //- The data content (as a union).
178  // For memory alignment this should appear as the first member.
180 
181  //- The token type
183 
184  //- Line number in the file the token was read from
186 
187 
188  // Private Member Functions
189 
190  //- Set as UNDEFINED and zero the union content without any checking
191  inline void setUndefined();
192 
193  // Parse error, expected 'expected', found ...
194  void parseError(const char* expected) const;
195 
196 
197 public:
198 
199  // Static Data Members
200 
201  // Constructors
202 
203  //- Default construct, initialized to an UNDEFINED token.
204  inline constexpr token() noexcept;
205 
206  //- Copy construct
207  inline token(const token& t);
208 
209  //- Move construct. The original token is left as UNDEFINED.
210  inline token(token&& t);
211 
212  //- Construct punctuation character token
213  inline explicit token(punctuationToken p, int32 lineNumber=0);
214 
215  //- Construct label token
216  inline explicit token(const label val, int32 lineNumber=0);
217 
218  //- Construct uint32 token
219  inline explicit token(const uint32 val, int32 lineNumber=0);
220 
221  //- Construct int64 token
222  inline explicit token(const int64 val, int32 lineNumber=0);
223 
224  //- Construct int64 token
225  inline explicit token(const int32 val, int32 lineNumber=0);
226 
227  //- Construct float token
228  inline explicit token(const float val, int32 lineNumber=0);
229 
230  //- Construct double token
231  inline explicit token(const double val, int32 lineNumber=0);
232 
233  //- Copy construct word & string token
234  inline explicit token(const word& w, int32 lineNumber=0, bool isString = false);
235 
236 
237  //- Move construct word & string token
238  inline explicit token(word&& w, int32 lineNumber=0, bool isString = false);
239 
240  //- Construct from iIstream
241  explicit token(iIstream& is);
242 
243 
244  //- Destructor
245  inline ~token();
246 
247 
248  // Static Functions
249 
250  //- Create a bool token.
251  inline static token boolean(bool on);
252 
253  //- Create a token with stream flags, no sanity check
254  //
255  // \param bitmask the flags to set
256  inline static token flag(int bitmask);
257 
258  //- True if the character is a punctuation separator (eg, in ISstream).
259  // Since it could also start a number, SUBTRACT is not included as
260  // a separator.
261  //
262  // \param c the character to test, passed as int for consistency with
263  // isdigit, isspace etc.
264  inline static bool isseparator(int c);
265 
266 
267  // Member Functions
268 
269  // Status
270 
271  //- Return the name of the token type
272  word name() const;
273 
274  //- Return the token type
275  inline tokenType type() const;
276 
277  //- Change the token type, for similar types.
278  // This can be used to change between string-like variants
279  // (eg, STRING, VARIABLE, etc)
280  // To change types entirely (eg, STRING to DOUBLE),
281  // use the corresponding assignment operator.
282  //
283  // \return true if the change was successful or no change was required
284  inline bool setType(const tokenType tokType);
285 
286  //- The line number for the token
287  inline int32 lineNumber() const;
288 
289  //- The line number for the token
290  inline int32& lineNumber();
291 
292  //- True if token is not UNDEFINED or ERROR
293  inline bool good() const;
294 
295  //- Token is UNDEFINED
296  inline bool undefined() const;
297 
298  //- Token is ERROR
299  inline bool error() const;
300 
301  //- Token is BOOL
302  inline bool isBool() const;
303 
304  //- Token is FLAG
305  inline bool isFlag() const;
306 
307  //- Token is PUNCTUATION
308  inline bool isPunctuation() const;
309 
310  //- Token is PUNCTUATION and isseparator
311  inline bool isSeparator() const;
312 
313  //- Tolen is end statement
314  inline bool isEndStatement() const;
315 
316  inline bool isEndBlock()const;
317 
318  //- Token is INT64
319  inline bool isInt64() const;
320 
321  //- Token is INT32
322  inline bool isInt32() const;
323 
324  //- Token is FLOAT
325  inline bool isFloat() const;
326 
327  //- Token is DOUBLE
328  inline bool isDouble() const;
329 
330  //- Token is FLOAT or DOUBLE
331  inline bool isReal() const;
332 
333  //- Token is INT64, FLOAT or DOUBLE
334  inline bool isNumber() const;
335 
336  //- Token is WORD or DIRECTIVE word
337  inline bool isWord() const;
338 
339  //- Token is DIRECTIVE (word variant)
340  inline bool isDirective() const;
341 
342  //- Token is STRING, VARIABLE or VERBATIM string
343  inline bool isString() const;
344 
345  //- Token is VARIABLE (string variant)
346  inline bool isVariable() const;
347 
348  //- Token is WORD, DIRECTIVE, STRING, VARIABLE or VERBATIM
349  inline bool isStringType() const;
350 
351 
352  // Access
353 
354  //- Return boolean token value.
355  // Report FatalIOError and return false if token is not BOOL or INT64
356  inline bool boolToken() const;
357 
358  //- Return flag bitmask value.
359  // Report FatalIOError and return NO_FLAG if token is not FLAG
360  inline int flagToken() const;
361 
362  //- Return punctuation character.
363  // Report FatalIOError and return \b \\0 if token is not PUNCTUATION
364  inline punctuationToken pToken() const;
365 
366  //- Return int64 value.
367  // Report FatalIOError and return \b 0 if token is not INT64
368  inline int64 int64Token() const;
369 
370  //- Return int32 value.
371  // Report FatalIOError and return \b 0 if token is not INT64
372  inline int32 int32Token() const;
373 
374  //- Return float value.
375  // Report FatalIOError and return \b 0 if token is not FLOAT
376  inline float floatToken() const;
377 
378  //- Return double value.
379  // Report FatalIOError and return \b 0 if token is not DOUBLE
380  inline double doubleToken() const;
381 
382  //- Return float or double value.
383  // Report FatalIOError and return \b 0 if token is not a
384  // FLOAT or DOUBLE
385  inline real realToken() const;
386 
387  //- Return int64, float or double value.
388  // Report FatalIOError and return \b 0 if token is not a
389  // INT64, FLOAT or DOUBLE
390  inline real number() const;
391 
392  //- Return const reference to the word contents.
393  // Report FatalIOError and return \b "" if token is not a
394  // WORD or DIRECTIVE
395  inline const word& wordToken() const;
396 
397  //- Return const reference to the string contents.
398  // Report FatalIOError and return \b "" if token is not a
399  // STRING, VARIABLE, VERBATIM or an upcast WORD or DIRECTIVE
400  inline const word& stringToken() const;
401 
402 
403  // Edit
404 
405  //- Reset token to UNDEFINED and clear any allocated storage
406  inline void reset();
407 
408  //- Clear token and set to be ERROR.
409  inline void setBad();
410 
411  //- Swap token contents: type, data, line-number
412  inline void swap(token& tok);
413 
414 
415  // Assignment
416 
417  //- Copy assign
418  inline void operator=(const token& tok);
419 
420  //- Move assign
421  inline void operator=(token&& tok);
422 
423  //- Copy assign from punctuation
424  inline void operator=(const punctuationToken p);
425 
426  //- Copy assign from int64
427  inline void operator=(const int64 val);
428 
429  //- Copy assign from int32
430  inline void operator=(const int32 val);
431 
432  //- Copy assign from float
433  inline void operator=(const float val);
434 
435  //- Copy assign from double
436  inline void operator=(const double val);
437 
438  //- Copy assign from word
439  inline void operator=(const word& w);
440 
441 
442 
443  //- Move assign from word
444  inline void operator=(word&& w);
445 
446 
447  // Equality
448 
449  inline bool operator==(const token& tok) const;
450  inline bool operator==(const punctuationToken p) const;
451  inline bool operator==(const int64 val) const;
452  inline bool operator==(const int32 val) const;
453  inline bool operator==(const float val) const;
454  inline bool operator==(const double val) const;
455  inline bool operator==(const word& w) const;
456 
457 
458  // Inequality
459 
460  inline bool operator!=(const token& tok) const;
461  inline bool operator!=(const punctuationToken p) const;
462  inline bool operator!=(const int64 val) const;
463  inline bool operator!=(const int32 val) const;
464  inline bool operator!=(const float val) const;
465  inline bool operator!=(const double val) const;
466  inline bool operator!=(const word& w) const;
467 
468  iOstream& printInfo(iOstream& os)const;
469 
470  std::ostream& printInfo(std::ostream& os)const;
471  // IOstream Operators
472 
473  friend iOstream& operator<<(iOstream& os, const token& tok);
474  friend iOstream& operator<<(iOstream& os, const punctuationToken& pt);
475 
476  // mostly used for debuging and developement
477  friend std::ostream& operator<<(std::ostream& os, const token& tok);
478  friend std::ostream& operator<<(std::ostream& os, const punctuationToken& pt);
479 
480  void operator=(word*) = delete;
481 
482 };
483 
484 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
485 
486 // IOstream Operators
487 
488 iIstream& operator>>(iIstream& is, token& tok);
489 iOstream& operator<<(iOstream& os, const token::punctuationToken& pt);
490 std::ostream& operator<<(std::ostream& os, const token::punctuationToken& pt);
491 std::ostream& operator<<(std::ostream& os, const token& tok);
492 
493 
495 {
496  return token::endList();
497 }
498 
500 {
501  return token::beginList();
502 }
503 
505 {
506  return token::endStatement();
507 }
508 
510 {
511  return token::beginBlock();
512 }
513 
515 {
516  return token::endBlocK();
517 }
518 
520 {
521  return token::space();
522 }
523 
525 {
526  return token::newLine();
527 }
528 
529 
530 
531 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
532 
533 } // End namespace pFlow
534 
535 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
536 
537 #include "tokenI.hpp"
538 
539 
540 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
541 
542 #endif
543 
544 // ************************************************************************* //
pFlow::token::beginBlock
static token beginBlock()
Definition: token.hpp:126
pFlow::token::parseError
void parseError(const char *expected) const
Definition: token.cpp:30
pFlow::token::setType
bool setType(const tokenType tokType)
Definition: tokenI.hpp:290
pFlow::token::PUNCTUATION
@ PUNCTUATION
single character punctuation
Definition: token.hpp:55
pFlow::token::isBool
bool isBool() const
Definition: tokenI.hpp:390
pFlow::token::type
tokenType type() const
Definition: tokenI.hpp:284
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::token
Definition: token.hpp:42
pFlow::token::undefined
bool undefined() const
Definition: tokenI.hpp:378
pFlow::token::data_
content data_
Definition: token.hpp:179
pFlow::token::pToken
punctuationToken pToken() const
Definition: tokenI.hpp:452
pFlow::token::isFloat
bool isFloat() const
Definition: tokenI.hpp:500
pFlow::endStatementToken
token endStatementToken()
Definition: token.hpp:504
pFlow::token::punctuationToken
punctuationToken
Definition: token.hpp:81
pFlow::token::content::stringPtr
word * stringPtr
Definition: token.hpp:171
pFlow::token::isEndBlock
bool isEndBlock() const
Definition: tokenI.hpp:442
pFlow::token::isPunctuation
bool isPunctuation() const
Definition: tokenI.hpp:426
pFlow::token::stringToken
const word & stringToken() const
Definition: tokenI.hpp:624
pFlow::token::END_BLOCK
@ END_BLOCK
End block [isseparator].
Definition: token.hpp:94
pFlow::token::error
bool error() const
Definition: tokenI.hpp:384
pFlow::token::INT64
@ INT64
int64 (integer) type
Definition: token.hpp:57
pFlow::token::endList
static token endList()
Definition: token.hpp:111
pFlow::token::good
bool good() const
Definition: tokenI.hpp:372
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::token::FLOAT
@ FLOAT
float (single-precision) type
Definition: token.hpp:58
pFlow::token::isDouble
bool isDouble() const
Definition: tokenI.hpp:518
tokenI.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::token::number
real number() const
Definition: tokenI.hpp:568
pFlow::token::NL
@ NL
Newline [isspace].
Definition: token.hpp:86
pFlow::token::isVariable
bool isVariable() const
Definition: tokenI.hpp:648
pFlow::token::STRING
@ STRING
A string whth double quuote.
Definition: token.hpp:63
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::token::DOUBLE
@ DOUBLE
double (double-precision) type
Definition: token.hpp:59
pFlow::token::int64Token
int64 int64Token() const
Definition: tokenI.hpp:484
pFlow::token::isSeparator
bool isSeparator() const
Definition: tokenI.hpp:464
pFlow::token::flagType
flagType
Definition: token.hpp:72
pFlow::token::content::punctuationVal
punctuationToken punctuationVal
Definition: token.hpp:165
pFlow::token::NULL_TOKEN
@ NULL_TOKEN
Nul character.
Definition: token.hpp:83
pFlow::token::boolToken
bool boolToken() const
Definition: tokenI.hpp:396
pFlow::token::isStringType
bool isStringType() const
Definition: tokenI.hpp:653
pFlow::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.hpp:93
pFlow::token::ERROR
@ ERROR
A token error encountered.
Definition: token.hpp:67
pFlow::endBlocKToken
token endBlocKToken()
Definition: token.hpp:514
pFlow::newLineToken
token newLineToken()
Definition: token.hpp:524
pFlow::token::SPACE
@ SPACE
Space [isspace].
Definition: token.hpp:84
pFlow::token::SQUOTE
@ SQUOTE
Single quote.
Definition: token.hpp:98
bTypes.hpp
pFlow::token::setUndefined
void setUndefined()
Definition: tokenI.hpp:79
pFlow
Definition: demComponent.hpp:28
pFlow::token::endBlocK
static token endBlocK()
Definition: token.hpp:131
pFlow::token::BOOL
@ BOOL
boolean type
Definition: token.hpp:56
pFlow::token::beginSquare
static token beginSquare()
Definition: token.hpp:136
pFlow::token::flagToken
int flagToken() const
Definition: tokenI.hpp:414
pFlow::token::beginList
static token beginList()
Definition: token.hpp:116
pFlow::token::reset
void reset()
Definition: tokenI.hpp:245
pFlow::token::token
constexpr token() noexcept
Definition: tokenI.hpp:89
pFlow::token::space
static token space()
Definition: token.hpp:146
pFlow::token::doubleToken
double doubleToken() const
Definition: tokenI.hpp:524
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::token::content
Definition: token.hpp:159
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::token::newLine
static token newLine()
Definition: token.hpp:151
pFlow::token::realToken
real realToken() const
Definition: tokenI.hpp:546
pFlow::token::isseparator
static bool isseparator(int c)
Definition: tokenI.hpp:48
pFlow::token::lineNumber_
int32 lineNumber_
Definition: token.hpp:185
pFlow::token::WORD
@ WORD
A pFlow::word.
Definition: token.hpp:62
pFlow::token::UNDEFINED
@ UNDEFINED
An undefined token-type.
Definition: token.hpp:51
pFlow::token::printInfo
iOstream & printInfo(iOstream &os) const
Definition: tokenIO.cpp:224
pFlow::token::flag
static token flag(int bitmask)
Definition: tokenI.hpp:38
pFlow::token::FLAG
@ FLAG
stream flag (1-byte bitmask)
Definition: token.hpp:54
pFlow::token::content::flagVal
int flagVal
Definition: token.hpp:164
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Begin string with double quote.
Definition: token.hpp:104
pFlow::token::VARIABLE
@ VARIABLE
A dictionary $variable (string variant)
Definition: token.hpp:65
pFlow::token::END_LIST
@ END_LIST
End list [isseparator].
Definition: token.hpp:90
pFlow::token::DOLLAR
@ DOLLAR
Dollar - start variable.
Definition: token.hpp:97
pFlow::beginListToken
token beginListToken()
Definition: token.hpp:499
pFlow::token::floatToken
float floatToken() const
Definition: tokenI.hpp:506
pFlow::token::content::wordPtr
word * wordPtr
Definition: token.hpp:170
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::token::isInt64
bool isInt64() const
Definition: tokenI.hpp:474
pFlow::token::COLON
@ COLON
Colon [isseparator].
Definition: token.hpp:95
pFlow::token::isInt32
bool isInt32() const
Definition: tokenI.hpp:479
pFlow::token::END_STATEMENT
@ END_STATEMENT
End entry [isseparator].
Definition: token.hpp:88
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:519
pFlow::token::isFlag
bool isFlag() const
Definition: tokenI.hpp:408
pFlow::token::content::floatVal
float floatVal
Definition: token.hpp:166
pFlow::token::undefinedToken
static const token undefinedToken()
pFlow::beginBlockToken
token beginBlockToken()
Definition: token.hpp:509
pFlow::token::tokenType
tokenType
Definition: token.hpp:49
pFlow::token::content::int64Val
int64_t int64Val
Definition: token.hpp:162
pFlow::token::content::doubleVal
double doubleVal
Definition: token.hpp:167
pFlow::token::SUBTRACT
@ SUBTRACT
Subtract or start of negative number.
Definition: token.hpp:101
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.hpp:89
pFlow::token::swap
void swap(token &tok)
Definition: tokenI.hpp:271
pFlow::token::BEGIN_SQR
@ BEGIN_SQR
Begin dimensions [isseparator].
Definition: token.hpp:91
pFlow::token::NO_FLAG
@ NO_FLAG
No flags.
Definition: token.hpp:74
pFlow::endListToken
token endListToken()
Definition: token.hpp:494
pFlow::token::DIVIDE
@ DIVIDE
Divide [isseparator].
Definition: token.hpp:102
pFlow::token::DQUOTE
@ DQUOTE
Double quote.
Definition: token.hpp:99
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::token::COMMA
@ COMMA
Comma [isseparator].
Definition: token.hpp:96
pFlow::token::TAB
@ TAB
Tab [isspace].
Definition: token.hpp:85
pFlow::token::name
word name() const
Definition: tokenIO.cpp:110
pFlow::token::END_STRING
@ END_STRING
End string with double quote.
Definition: token.hpp:105
pFlow::token::endStatement
static token endStatement()
Definition: token.hpp:121
pFlow::token::type_
tokenType type_
Definition: token.hpp:182
pFlow::token::isNumber
bool isNumber() const
Definition: tokenI.hpp:562
pFlow::token::endSquare
static token endSquare()
Definition: token.hpp:141
pFlow::token::isEndStatement
bool isEndStatement() const
Definition: tokenI.hpp:431
pFlow::token::isDirective
bool isDirective() const
Definition: tokenI.hpp:594
pFlow::token::int32Token
int32 int32Token() const
Definition: tokenI.hpp:495
pFlow::token::DIRECTIVE
@ DIRECTIVE
A dictionary #directive (word variant)
Definition: token.hpp:64
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::token::END_SQR
@ END_SQR
End dimensions [isseparator].
Definition: token.hpp:92
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::token::setBad
void setBad()
Definition: tokenI.hpp:658
pFlow::token::isString
bool isString() const
Definition: tokenI.hpp:615
pFlow::token::lineNumber
int32 lineNumber() const
Definition: tokenI.hpp:360
pFlow::token::BINARY
@ BINARY
BINARY-mode stream.
Definition: token.hpp:76
pFlow::token::isReal
bool isReal() const
Definition: tokenI.hpp:536
pFlow::token::isWord
bool isWord() const
Definition: tokenI.hpp:584
pFlow::token::ASCII
@ ASCII
ASCII-mode stream.
Definition: token.hpp:75