www.cemf.ir
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 #ifndef __token_hpp__
21 #define __token_hpp__
22 
23 
24 #include "bTypes.hpp"
25 
26 
27 
28 namespace pFlow
29 {
30 
32 class token;
33 class iIstream;
34 class iOstream;
35 
36 iOstream& operator<<(iOstream& os, const token& tok);
37 
38 
44 class token
45 {
46 public:
47 
51  enum tokenType
52  {
53  UNDEFINED = 0,
54 
55  // Fundamental types
56  FLAG,
58  BOOL,
62 
63  // Pointer types
64  WORD,
68 
70  };
71 
72 
74  enum flagType
75  {
76  NO_FLAG = 0,
77  ASCII = 1,
78  BINARY = 2
79  };
80 
81 
83  enum punctuationToken : char
84  {
85  NULL_TOKEN = '\0',
86  SPACE = ' ',
87  TAB = '\t',
88  NL = '\n',
89 
90  END_STATEMENT = ';',
91  BEGIN_LIST = '(',
92  END_LIST = ')',
93  BEGIN_SQR = '[',
94  END_SQR = ']',
95  BEGIN_BLOCK = '{',
96  END_BLOCK = '}',
97  COLON = ':',
98  COMMA = ',',
99  DOLLAR = '$',
100  SQUOTE = '\'',
101  DQUOTE = '"',
102 
103  SUBTRACT = '-',
104  DIVIDE = '/',
105 
108  };
109 
111  static const inline token undefinedToken();
112 
113  static token endList()
114  {
115  return token(punctuationToken::END_LIST);
116  }
117 
118  static token beginList()
119  {
120  return token(punctuationToken::BEGIN_LIST);
121  }
122 
124  {
125  return token(punctuationToken::END_STATEMENT);
126  }
127 
128  static token beginBlock()
129  {
130  return token(punctuationToken::BEGIN_BLOCK);
131  }
132 
133  static token endBlocK()
134  {
135  return token(punctuationToken::END_BLOCK);
136  }
137 
139  {
140  return token(punctuationToken::BEGIN_SQR);
141  }
142 
143  static token endSquare()
144  {
145  return token(punctuationToken::END_SQR);
146  }
147 
148  static token space()
149  {
150  return token(punctuationToken::SPACE);
151  }
152 
153  static token newLine()
154  {
155  return token(punctuationToken::NL);
156  }
157 
158 private:
159 
161  union content
162  {
163  // Fundamental values. Largest first for any {} initialization.
164  int64_t int64Val;
165 
166  int flagVal; // bitmask - stored as int, not enum
168  float floatVal;
169  double doubleVal;
170 
171  // Pointers
174  };
175 
176 
178 
180  // For memory alignment this should appear as the first member.
182 
185 
188 
189 
190  // Private Member Functions
191 
193  inline void setUndefined();
194 
196  void parseError(const char* expected) const;
197 
198 
199 public:
200 
201 
203 
205  inline constexpr token() noexcept;
206 
208  inline token(const token& t);
209 
211  inline token(token&& t);
212 
214  inline explicit token(punctuationToken p, int32 lineNumber=0);
215 
217  inline explicit token(const uint64 val, int32 lineNumber=0);
218 
220  inline explicit token(const uint32 val, int32 lineNumber=0);
221 
223  inline explicit token(const uint8 val, int32 lineNumber=0);
224 
226  inline explicit token(const int64 val, int32 lineNumber=0);
227 
229  inline explicit token(const int32 val, int32 lineNumber=0);
230 
232  inline explicit token(const int8 val, int32 lineNumber=0);
233 
235  inline explicit token(const float val, int32 lineNumber=0);
236 
238  inline explicit token(const double val, int32 lineNumber=0);
239 
241  inline explicit token(const word& w, int32 lineNumber=0, bool isString = false);
242 
243 
245  inline explicit token(word&& w, int32 lineNumber=0, bool isString = false);
246 
248  explicit token(iIstream& is);
249 
250 
252  inline ~token();
253 
254 
256 
258  inline static token boolean(bool on);
259 
263  inline static token flag(int bitmask);
264 
271  inline static bool isseparator(int c);
272 
273 
275 
276  // - Status
277 
279  word name() const;
280 
282  inline tokenType type() const;
283 
291  inline bool setType(const tokenType tokType);
292 
294  inline int32 lineNumber() const;
295 
297  inline int32& lineNumber();
298 
300  inline bool good() const;
301 
303  inline bool undefined() const;
304 
306  inline bool error() const;
307 
309  inline bool isBool() const;
310 
312  inline bool isFlag() const;
313 
315  inline bool isPunctuation() const;
316 
318  inline bool isSeparator() const;
319 
321  inline bool isEndStatement() const;
322 
324  inline bool isEndBlock()const;
325 
327  inline bool isInt64() const;
328 
330  inline bool isInt32() const;
331 
333  inline bool isFloat() const;
334 
336  inline bool isDouble() const;
337 
339  inline bool isReal() const;
340 
342  inline bool isNumber() const;
343 
345  inline bool isWord() const;
346 
348  inline bool isDirective() const;
349 
351  inline bool isString() const;
352 
354  inline bool isVariable() const;
355 
357  inline bool isStringType() const;
358 
359 
360  //- Access
361 
364  inline bool boolToken() const;
365 
368  inline int flagToken() const;
369 
372  inline punctuationToken pToken() const;
373 
375  // Report FatalIOError and return \b 0 if token is not INT64
376  inline int64 int64Token() const;
377 
380  inline int32 int32Token() const;
381 
384  inline float floatToken() const;
385 
388  inline double doubleToken() const;
389 
393  inline real realToken() const;
394 
398  inline real number() const;
399 
403  inline const word& wordToken() const;
404 
408  inline const word& stringToken() const;
409 
410 
411  //- Edit
412 
414  inline void reset();
415 
417  inline void setBad();
418 
420  inline void swap(token& tok);
421 
422 
423  //- Assignment
424 
426  inline void operator=(const token& tok);
427 
429  inline void operator=(token&& tok);
430 
432  inline void operator=(const punctuationToken p);
433 
435  inline void operator=(const int64 val);
436 
438  inline void operator=(const int32 val);
439 
441  inline void operator=(const float val);
442 
444  inline void operator=(const double val);
445 
447  inline void operator=(const word& w);
448 
449 
450 
452  inline void operator=(word&& w);
453 
454 
455  //- Equality
456 
457  inline bool operator==(const token& tok) const;
458  inline bool operator==(const punctuationToken p) const;
459  inline bool operator==(const int64 val) const;
460  inline bool operator==(const int32 val) const;
461  inline bool operator==(const float val) const;
462  inline bool operator==(const double val) const;
463  inline bool operator==(const word& w) const;
464 
465 
466  //- Inequality
467 
468  inline bool operator!=(const token& tok) const;
469  inline bool operator!=(const punctuationToken p) const;
470  inline bool operator!=(const int64 val) const;
471  inline bool operator!=(const int32 val) const;
472  inline bool operator!=(const float val) const;
473  inline bool operator!=(const double val) const;
474  inline bool operator!=(const word& w) const;
475 
476  iOstream& printInfo(iOstream& os)const;
477 
478  std::ostream& printInfo(std::ostream& os)const;
479 
480  //- IOstream Operators
481 
482  friend iOstream& operator<<(iOstream& os, const token& tok);
483  friend iOstream& operator<<(iOstream& os, const punctuationToken& pt);
484 
485  // mostly used for debuging and developement
486  friend std::ostream& operator<<(std::ostream& os, const token& tok);
487  friend std::ostream& operator<<(std::ostream& os, const punctuationToken& pt);
488 
489  void operator=(word*) = delete;
490 
491 };
492 
493 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
494 
495 // IOstream Operators
496 
497 iIstream& operator>>(iIstream& is, token& tok);
498 iOstream& operator<<(iOstream& os, const token::punctuationToken& pt);
499 std::ostream& operator<<(std::ostream& os, const token::punctuationToken& pt);
500 std::ostream& operator<<(std::ostream& os, const token& tok);
501 
502 
504 {
505  return token::endList();
506 }
507 
509 {
510  return token::beginList();
511 }
512 
514 {
515  return token::endStatement();
516 }
517 
519 {
520  return token::beginBlock();
521 }
522 
524 {
525  return token::endBlocK();
526 }
527 
529 {
530  return token::space();
531 }
532 
534 {
535  return token::newLine();
536 }
537 
538 
539 } // End namespace pFlow
540 
541 
542 #include "tokenI.hpp"
543 
544 
545 #endif
pFlow::token::beginBlock
static token beginBlock()
Definition: token.hpp:128
pFlow::token::parseError
void parseError(const char *expected) const
Parse error, expected 'expected', found ...
Definition: token.cpp:30
pFlow::token::setType
bool setType(const tokenType tokType)
Change the token type, for similar types.
Definition: tokenI.hpp:308
pFlow::token::PUNCTUATION
@ PUNCTUATION
stream flag (1-byte bitmask)
Definition: token.hpp:57
pFlow::token::isBool
bool isBool() const
Token is BOOL.
Definition: tokenI.hpp:408
pFlow::token::type
tokenType type() const
Return the token type.
Definition: tokenI.hpp:302
pFlow::real
float real
Definition: builtinTypes.hpp:45
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::token::undefined
bool undefined() const
Token is UNDEFINED.
Definition: tokenI.hpp:396
pFlow::token::data_
content data_
The data content (as a union).
Definition: token.hpp:181
pFlow::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.hpp:470
pFlow::token::isFloat
bool isFloat() const
Token is float.
Definition: tokenI.hpp:518
pFlow::endStatementToken
token endStatementToken()
Definition: token.hpp:513
pFlow::token::punctuationToken
punctuationToken
Standard punctuation tokens (a character)
Definition: token.hpp:83
pFlow::token::content::stringPtr
word * stringPtr
Definition: token.hpp:173
pFlow::token::isEndBlock
bool isEndBlock() const
Token is end endBlock.
Definition: tokenI.hpp:460
pFlow::token::isPunctuation
bool isPunctuation() const
Token is PUNCTUATION.
Definition: tokenI.hpp:444
pFlow::token::stringToken
const word & stringToken() const
Return const reference to the string contents.
Definition: tokenI.hpp:642
pFlow::token::END_BLOCK
@ END_BLOCK
Begin block [isseparator].
Definition: token.hpp:96
pFlow::token::error
bool error() const
Token is ERROR.
Definition: tokenI.hpp:402
pFlow::token::INT64
@ INT64
boolean type
Definition: token.hpp:59
pFlow::token::endList
static token endList()
Definition: token.hpp:113
pFlow::token::good
bool good() const
True if token is not UNDEFINED or ERROR.
Definition: tokenI.hpp:390
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::token::FLOAT
@ FLOAT
int64 (integer) type
Definition: token.hpp:60
pFlow::token::isDouble
bool isDouble() const
Token is double.
Definition: tokenI.hpp:536
tokenI.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::token::number
real number() const
Return int64, float or double value.
Definition: tokenI.hpp:586
pFlow::token::NL
@ NL
Tab [isspace].
Definition: token.hpp:88
pFlow::token::isVariable
bool isVariable() const
Token is VARIABLE (string variant)
Definition: tokenI.hpp:666
pFlow::token::STRING
@ STRING
A pFlow::word.
Definition: token.hpp:65
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::token::DOUBLE
@ DOUBLE
float (single-precision) type
Definition: token.hpp:61
pFlow::token::int64Token
int64 int64Token() const
Return int64 value.
Definition: tokenI.hpp:502
pFlow::token::isSeparator
bool isSeparator() const
Token is PUNCTUATION and isseparator.
Definition: tokenI.hpp:482
pFlow::token::flagType
flagType
Stream or output control flags (1-byte width)
Definition: token.hpp:74
pFlow::token::content::punctuationVal
punctuationToken punctuationVal
Definition: token.hpp:167
pFlow::token::NULL_TOKEN
@ NULL_TOKEN
Definition: token.hpp:85
pFlow::token::boolToken
bool boolToken() const
Return boolean token value.
Definition: tokenI.hpp:414
pFlow::token::isStringType
bool isStringType() const
Token is WORD, DIRECTIVE, STRING, VARIABLE or VERBATIM.
Definition: tokenI.hpp:671
pFlow::token::BEGIN_BLOCK
@ BEGIN_BLOCK
End dimensions [isseparator].
Definition: token.hpp:95
pFlow::token::ERROR
@ ERROR
A dictionary $variable (string variant)
Definition: token.hpp:69
pFlow::endBlocKToken
token endBlocKToken()
Definition: token.hpp:523
pFlow::newLineToken
token newLineToken()
Definition: token.hpp:533
pFlow::token::SPACE
@ SPACE
Nul character.
Definition: token.hpp:86
pFlow::token::SQUOTE
@ SQUOTE
Dollar - start variable.
Definition: token.hpp:100
bTypes.hpp
pFlow::token::setUndefined
void setUndefined()
Set as UNDEFINED and zero the union content without any checking.
Definition: tokenI.hpp:79
pFlow
Definition: demGeometry.hpp:27
pFlow::token::endBlocK
static token endBlocK()
Definition: token.hpp:133
pFlow::token::BOOL
@ BOOL
single character punctuation
Definition: token.hpp:58
pFlow::token::beginSquare
static token beginSquare()
Definition: token.hpp:138
pFlow::token::flagToken
int flagToken() const
Return flag bitmask value.
Definition: tokenI.hpp:432
pFlow::token::beginList
static token beginList()
Definition: token.hpp:118
pFlow::token::reset
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.hpp:263
pFlow::token::token
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.hpp:89
pFlow::token::space
static token space()
Definition: token.hpp:148
pFlow::token::doubleToken
double doubleToken() const
Return double value.
Definition: tokenI.hpp:542
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::token::content
A union of token types.
Definition: token.hpp:161
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::token::newLine
static token newLine()
Definition: token.hpp:153
pFlow::token::realToken
real realToken() const
Return float or double value.
Definition: tokenI.hpp:564
pFlow::token::isseparator
static bool isseparator(int c)
True if the character is a punctuation separator (eg, in ISstream).
Definition: tokenI.hpp:48
pFlow::token::lineNumber_
int32 lineNumber_
Line number in the file the token was read from.
Definition: token.hpp:187
pFlow::token::WORD
@ WORD
double (double-precision) type
Definition: token.hpp:64
pFlow::token::UNDEFINED
@ UNDEFINED
An undefined token-type.
Definition: token.hpp:53
pFlow::token::printInfo
iOstream & printInfo(iOstream &os) const
Definition: tokenIO.cpp:224
pFlow::token::flag
static token flag(int bitmask)
Create a token with stream flags, no sanity check.
Definition: tokenI.hpp:38
pFlow::token::FLAG
@ FLAG
Definition: token.hpp:56
pFlow::token::content::flagVal
int flagVal
Definition: token.hpp:166
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Divide [isseparator].
Definition: token.hpp:106
pFlow::token::VARIABLE
@ VARIABLE
A dictionary #directive (word variant)
Definition: token.hpp:67
pFlow::token::END_LIST
@ END_LIST
Begin list [isseparator].
Definition: token.hpp:92
pFlow::token::DOLLAR
@ DOLLAR
Comma [isseparator].
Definition: token.hpp:99
pFlow::beginListToken
token beginListToken()
Definition: token.hpp:508
pFlow::token::floatToken
float floatToken() const
Return float value.
Definition: tokenI.hpp:524
pFlow::token::content::wordPtr
word * wordPtr
Definition: token.hpp:172
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::token::isInt64
bool isInt64() const
Token is int64.
Definition: tokenI.hpp:492
pFlow::token::COLON
@ COLON
End block [isseparator].
Definition: token.hpp:97
pFlow::token::isInt32
bool isInt32() const
Token is int32.
Definition: tokenI.hpp:497
pFlow::token::END_STATEMENT
@ END_STATEMENT
Newline [isspace].
Definition: token.hpp:90
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:528
pFlow::token::isFlag
bool isFlag() const
Token is FLAG.
Definition: tokenI.hpp:426
pFlow::token::content::floatVal
float floatVal
Definition: token.hpp:168
pFlow::token::undefinedToken
static const token undefinedToken()
An undefined token.
pFlow::beginBlockToken
token beginBlockToken()
Definition: token.hpp:518
pFlow::token::tokenType
tokenType
Enumeration defining the types of token.
Definition: token.hpp:51
pFlow::token::content::int64Val
int64_t int64Val
Definition: token.hpp:164
pFlow::token::content::doubleVal
double doubleVal
Definition: token.hpp:169
pFlow::token::SUBTRACT
@ SUBTRACT
Double quote.
Definition: token.hpp:103
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
End entry [isseparator].
Definition: token.hpp:91
pFlow::token::swap
void swap(token &tok)
Swap token contents: type, data, line-number.
Definition: tokenI.hpp:289
pFlow::token::BEGIN_SQR
@ BEGIN_SQR
End list [isseparator].
Definition: token.hpp:93
pFlow::token::NO_FLAG
@ NO_FLAG
Definition: token.hpp:76
pFlow::endListToken
token endListToken()
Definition: token.hpp:503
pFlow::token::DIVIDE
@ DIVIDE
Subtract or start of negative number.
Definition: token.hpp:104
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::token::DQUOTE
@ DQUOTE
Single quote.
Definition: token.hpp:101
pFlow::token::COMMA
@ COMMA
Colon [isseparator].
Definition: token.hpp:98
pFlow::token::TAB
@ TAB
Space [isspace].
Definition: token.hpp:87
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::token::name
word name() const
Return the name of the token type.
Definition: tokenIO.cpp:110
pFlow::token::END_STRING
@ END_STRING
Begin string with double quote.
Definition: token.hpp:107
pFlow::token::endStatement
static token endStatement()
Definition: token.hpp:123
pFlow::token::type_
tokenType type_
The token type.
Definition: token.hpp:184
pFlow::token::isNumber
bool isNumber() const
Token is int, float or duble.
Definition: tokenI.hpp:580
pFlow::token::endSquare
static token endSquare()
Definition: token.hpp:143
pFlow::token::isEndStatement
bool isEndStatement() const
Token is end statement.
Definition: tokenI.hpp:449
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::token::isDirective
bool isDirective() const
Token is DIRECTIVE (word variant)
Definition: tokenI.hpp:612
pFlow::token::int32Token
int32 int32Token() const
Return int32 value.
Definition: tokenI.hpp:513
pFlow::token::DIRECTIVE
@ DIRECTIVE
A string whth double quuote.
Definition: token.hpp:66
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::token::END_SQR
@ END_SQR
Begin dimensions [isseparator].
Definition: token.hpp:94
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
pFlow::token::setBad
void setBad()
Clear token and set to be ERROR.
Definition: tokenI.hpp:676
pFlow::token::isString
bool isString() const
Token is STRING, VARIABLE or VERBATIM string.
Definition: tokenI.hpp:633
pFlow::token::lineNumber
int32 lineNumber() const
The line number for the token.
Definition: tokenI.hpp:378
pFlow::token::BINARY
@ BINARY
ASCII-mode stream.
Definition: token.hpp:78
pFlow::token::isReal
bool isReal() const
Token is float or double.
Definition: tokenI.hpp:554
pFlow::token::isWord
bool isWord() const
Token is word or DIRECTIVE word.
Definition: tokenI.hpp:602
pFlow::token::ASCII
@ ASCII
No flags.
Definition: token.hpp:77