# PhasicFlow: src/phasicFlow/streams/token/token.hpp Source File

token.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
// based on OpenFOAM stream, with some modifications/simplifications
// to be tailored to our needs
#ifndef __token_hpp__
#define __token_hpp__
#include "bTypes.hpp"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace pFlow
{
// Forward Declarations
class token;
class iIstream;
class iOstream;
iOstream& operator<<(iOstream& os, const token& tok);
class token
{
public:
//- Enumeration defining the types of token.
// Since these values are also used to tag content in Pstream,
// the maximum number of types is limited to 30.
enum tokenType
{
UNDEFINED = 0,
// Fundamental types
FLAG,
PUNCTUATION,
BOOL,
INT64,
FLOAT,
DOUBLE,
// Pointer types
WORD,
STRING,
DIRECTIVE,
VARIABLE,
ERROR,
};
//**************- Stream or output control flags (1-byte width)
enum flagType
{
NO_FLAG = 0,
ASCII = 1,
BINARY = 2
};
//- Standard punctuation tokens (a character)
enum punctuationToken : char
{
NULL_TOKEN = '\0',
SPACE = ' ',
TAB = '\t',
NL = '\n',
END_STATEMENT = ';',
BEGIN_LIST = '(',
END_LIST = ')',
BEGIN_SQR = '[',
END_SQR = ']',
BEGIN_BLOCK = '{',
END_BLOCK = '}',
COLON = ':',
COMMA = ',',
DOLLAR = '$',
SQUOTE = '\'',
DQUOTE = '"',
SUBTRACT = '-',
DIVIDE = '/',
BEGIN_STRING = DQUOTE,
END_STRING = DQUOTE
};
//- An undefined token
static const inline token undefinedToken();
static token endList()
{
return token(punctuationToken::END_LIST);
}
static token beginList()
{
return token(punctuationToken::BEGIN_LIST);
}
static token endStatement()
{
return token(punctuationToken::END_STATEMENT);
}
static token beginBlock()
{
return token(punctuationToken::BEGIN_BLOCK);
}
static token endBlocK()
{
return token(punctuationToken::END_BLOCK);
}
static token beginSquare()
{
return token(punctuationToken::BEGIN_SQR);
}
static token endSquare()
{
return token(punctuationToken::END_SQR);
}
static token space()
{
return token(punctuationToken::SPACE);
}
static token newLine()
{
return token(punctuationToken::NL);
}
private:
//- A %union of token types
union content
{
// Fundamental values. Largest first for any {} initialization.
int64_t int64Val;
int flagVal; // bitmask - stored as int, not enum
punctuationToken punctuationVal;
float floatVal;
double doubleVal;
// Pointers
word* wordPtr;
word* stringPtr;
};
// Private Data
//- The data content (as a union).
// For memory alignment this should appear as the first member.
content data_;
//- The token type
tokenType type_;
//- Line number in the file the token was read from
int32 lineNumber_;
// Private Member Functions
//- Set as UNDEFINED and zero the union content without any checking
inline void setUndefined();
// Parse error, expected 'expected', found ...
void parseError(const char* expected) const;
public:
// Static Data Members
// Constructors
//- Default construct, initialized to an UNDEFINED token.
inline constexpr token() noexcept;
//- Copy construct
inline token(const token& t);
//- Move construct. The original token is left as UNDEFINED.
inline token(token&& t);
//- Construct punctuation character token
inline explicit token(punctuationToken p, int32 lineNumber=0);
//- Construct label token
inline explicit token(const label val, int32 lineNumber=0);
//- Construct uint32 token
inline explicit token(const uint32 val, int32 lineNumber=0);
//- Construct int64 token
inline explicit token(const int64 val, int32 lineNumber=0);
//- Construct int64 token
inline explicit token(const int32 val, int32 lineNumber=0);
//- Construct float token
inline explicit token(const float val, int32 lineNumber=0);
//- Construct double token
inline explicit token(const double val, int32 lineNumber=0);
//- Copy construct word & string token
inline explicit token(const word& w, int32 lineNumber=0, bool isString = false);
//- Move construct word & string token
inline explicit token(word&& w, int32 lineNumber=0, bool isString = false);
//- Construct from iIstream
explicit token(iIstream& is);
//- Destructor
inline ~token();
// Static Functions
//- Create a bool token.
inline static token boolean(bool on);
//- Create a token with stream flags, no sanity check
//
// \param bitmask the flags to set
inline static token flag(int bitmask);
//- True if the character is a punctuation separator (eg, in ISstream).
// Since it could also start a number, SUBTRACT is not included as
// a separator.
//
// \param c the character to test, passed as int for consistency with
// isdigit, isspace etc.
inline static bool isseparator(int c);
// Member Functions
// Status
//- Return the name of the token type
word name() const;
//- Return the token type
inline tokenType type() const;
//- Change the token type, for similar types.
// This can be used to change between string-like variants
// (eg, STRING, VARIABLE, etc)
// To change types entirely (eg, STRING to DOUBLE),
// use the corresponding assignment operator.
//
// \return true if the change was successful or no change was required
inline bool setType(const tokenType tokType);
//- The line number for the token
inline int32 lineNumber() const;
//- The line number for the token
inline int32& lineNumber();
//- True if token is not UNDEFINED or ERROR
inline bool good() const;
//- Token is UNDEFINED
inline bool undefined() const;
//- Token is ERROR
inline bool error() const;
//- Token is BOOL
inline bool isBool() const;
//- Token is FLAG
inline bool isFlag() const;
//- Token is PUNCTUATION
inline bool isPunctuation() const;
//- Token is PUNCTUATION and isseparator
inline bool isSeparator() const;
//- Tolen is end statement
inline bool isEndStatement() const;
inline bool isEndBlock()const;
//- Token is INT64
inline bool isInt64() const;
//- Token is INT32
inline bool isInt32() const;
//- Token is FLOAT
inline bool isFloat() const;
//- Token is DOUBLE
inline bool isDouble() const;
//- Token is FLOAT or DOUBLE
inline bool isReal() const;
//- Token is INT64, FLOAT or DOUBLE
inline bool isNumber() const;
//- Token is WORD or DIRECTIVE word
inline bool isWord() const;
//- Token is DIRECTIVE (word variant)
inline bool isDirective() const;
//- Token is STRING, VARIABLE or VERBATIM string
inline bool isString() const;
//- Token is VARIABLE (string variant)
inline bool isVariable() const;
//- Token is WORD, DIRECTIVE, STRING, VARIABLE or VERBATIM
inline bool isStringType() const;
// Access
//- Return boolean token value.
// Report FatalIOError and return false if token is not BOOL or INT64
inline bool boolToken() const;
//- Return flag bitmask value.
// Report FatalIOError and return NO_FLAG if token is not FLAG
inline int flagToken() const;
//- Return punctuation character.
// Report FatalIOError and return \b \\0 if token is not PUNCTUATION
inline punctuationToken pToken() const;
//- Return int64 value.
// Report FatalIOError and return \b 0 if token is not INT64
inline int64 int64Token() const;
//- Return int32 value.
// Report FatalIOError and return \b 0 if token is not INT64
inline int32 int32Token() const;
//- Return float value.
// Report FatalIOError and return \b 0 if token is not FLOAT
inline float floatToken() const;
//- Return double value.
// Report FatalIOError and return \b 0 if token is not DOUBLE
inline double doubleToken() const;
//- Return float or double value.
// Report FatalIOError and return \b 0 if token is not a
// FLOAT or DOUBLE
inline real realToken() const;
//- Return int64, float or double value.
// Report FatalIOError and return \b 0 if token is not a
// INT64, FLOAT or DOUBLE
inline real number() const;
//- Return const reference to the word contents.
// Report FatalIOError and return \b "" if token is not a
// WORD or DIRECTIVE
inline const word& wordToken() const;
//- Return const reference to the string contents.
// Report FatalIOError and return \b "" if token is not a
// STRING, VARIABLE, VERBATIM or an upcast WORD or DIRECTIVE
inline const word& stringToken() const;
// Edit
//- Reset token to UNDEFINED and clear any allocated storage
inline void reset();
//- Clear token and set to be ERROR.
inline void setBad();
//- Swap token contents: type, data, line-number
inline void swap(token& tok);
// Assignment
//- Copy assign
inline void operator=(const token& tok);
//- Move assign
inline void operator=(token&& tok);
//- Copy assign from punctuation
inline void operator=(const punctuationToken p);
//- Copy assign from int64
inline void operator=(const int64 val);
//- Copy assign from int32
inline void operator=(const int32 val);
//- Copy assign from float
inline void operator=(const float val);
//- Copy assign from double
inline void operator=(const double val);
//- Copy assign from word
inline void operator=(const word& w);
//- Move assign from word
inline void operator=(word&& w);
// Equality
inline bool operator==(const token& tok) const;
inline bool operator==(const punctuationToken p) const;
inline bool operator==(const int64 val) const;
inline bool operator==(const int32 val) const;
inline bool operator==(const float val) const;
inline bool operator==(const double val) const;
inline bool operator==(const word& w) const;
// Inequality
inline bool operator!=(const token& tok) const;
inline bool operator!=(const punctuationToken p) const;
inline bool operator!=(const int64 val) const;
inline bool operator!=(const int32 val) const;
inline bool operator!=(const float val) const;
inline bool operator!=(const double val) const;
inline bool operator!=(const word& w) const;
iOstream& printInfo(iOstream& os)const;
std::ostream& printInfo(std::ostream& os)const;
// IOstream Operators
friend iOstream& operator<<(iOstream& os, const token& tok);
friend iOstream& operator<<(iOstream& os, const punctuationToken& pt);
// mostly used for debuging and developement
friend std::ostream& operator<<(std::ostream& os, const token& tok);
friend std::ostream& operator<<(std::ostream& os, const punctuationToken& pt);
void operator=(word*) = delete;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// IOstream Operators
iIstream& operator>>(iIstream& is, token& tok);
iOstream& operator<<(iOstream& os, const token::punctuationToken& pt);
std::ostream& operator<<(std::ostream& os, const token::punctuationToken& pt);
std::ostream& operator<<(std::ostream& os, const token& tok);
inline token endListToken()
{
return token::endList();
}
inline token beginListToken()
{
return token::beginList();
}
inline token endStatementToken()
{
return token::endStatement();
}
inline token beginBlockToken()
{
return token::beginBlock();
}
inline token endBlocKToken()
{
return token::endBlocK();
}
inline token spaceToken()
{
return token::space();
}
inline token newLineToken()
{
return token::newLine();
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace pFlow
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "tokenI.hpp"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
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