/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
\*---------------------------------------------------------------------------*/
#include
#include "token.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline void Foam::token::clear()
{
if (type_ == WORD)
{
delete wordTokenPtr_;
}
else if (type_ == FUNCTIONNAME)
{
delete functionNameTokenPtr_;
}
else if (type_ == VARIABLE)
{
delete variableTokenPtr_;
}
else if (type_ == STRING)
{
delete stringTokenPtr_;
}
else if (type_ == VERBATIMSTRING)
{
delete verbatimStringTokenPtr_;
}
else if (type_ == LONG_DOUBLE_SCALAR)
{
delete longDoubleScalarTokenPtr_;
}
else if (type_ == COMPOUND)
{
if (compoundTokenPtr_->unique())
{
delete compoundTokenPtr_;
}
else
{
compoundTokenPtr_->refCount::operator--();
}
}
type_ = UNDEFINED;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::token::token()
:
type_(UNDEFINED),
lineNumber_(0)
{}
inline Foam::token::token(const token& t)
:
type_(t.type_),
lineNumber_(t.lineNumber_)
{
switch (type_)
{
case token::UNDEFINED:
break;
case PUNCTUATION:
punctuationToken_ = t.punctuationToken_;
break;
case WORD:
wordTokenPtr_ = new word(*t.wordTokenPtr_);
break;
case FUNCTIONNAME:
functionNameTokenPtr_ = new functionName(*t.functionNameTokenPtr_);
break;
case VARIABLE:
variableTokenPtr_ = new variable(*t.variableTokenPtr_);
break;
case STRING:
stringTokenPtr_ = new string(*t.stringTokenPtr_);
break;
case VERBATIMSTRING:
verbatimStringTokenPtr_ =
new verbatimString(*t.verbatimStringTokenPtr_);
break;
case INTEGER_32:
integer32Token_ = t.integer32Token_;
break;
case INTEGER_64:
integer64Token_ = t.integer64Token_;
break;
case UNSIGNED_INTEGER_32:
unsignedInteger32Token_ = t.unsignedInteger32Token_;
break;
case UNSIGNED_INTEGER_64:
unsignedInteger64Token_ = t.unsignedInteger64Token_;
break;
case FLOAT_SCALAR:
floatScalarToken_ = t.floatScalarToken_;
break;
case DOUBLE_SCALAR:
doubleScalarToken_ = t.doubleScalarToken_;
break;
case LONG_DOUBLE_SCALAR:
longDoubleScalarTokenPtr_ =
new longDoubleScalar(*t.longDoubleScalarTokenPtr_);
break;
case COMPOUND:
compoundTokenPtr_ = t.compoundTokenPtr_;
compoundTokenPtr_->refCount::operator++();
break;
case token::ERROR:
break;
}
}
inline Foam::token::token(punctuationToken p, label lineNumber)
:
type_(PUNCTUATION),
punctuationToken_(p),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const word& w, label lineNumber)
:
type_(WORD),
wordTokenPtr_(new word(w)),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const string& s, label lineNumber)
:
type_(STRING),
stringTokenPtr_(new string(s)),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const keyType& kt, label lineNumber)
:
type_(UNDEFINED),
lineNumber_(lineNumber)
{
switch (kt.Type())
{
case keyType::UNDEFINED:
{
type_ = UNDEFINED;
break;
}
case keyType::WORD:
{
type_ = WORD;
wordTokenPtr_ = new word(kt);
break;
}
case keyType::FUNCTIONNAME:
{
type_ = FUNCTIONNAME;
functionNameTokenPtr_ = new functionName(kt);
break;
}
case keyType::VARIABLE:
{
type_ = VARIABLE;
variableTokenPtr_ = new variable(kt);
break;
}
case keyType::PATTERN:
{
type_ = STRING;
stringTokenPtr_ = new string(kt);
break;
}
}
}
inline Foam::token::token(const verbatimString& vs, label lineNumber)
:
type_(VERBATIMSTRING),
verbatimStringTokenPtr_(new verbatimString(vs)),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const int32_t l, label lineNumber)
:
type_(INTEGER_32),
integer32Token_(l),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const int64_t l, label lineNumber)
:
type_(INTEGER_64),
integer64Token_(l),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const uint32_t l, label lineNumber)
:
type_(UNSIGNED_INTEGER_32),
unsignedInteger32Token_(l),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const uint64_t l, label lineNumber)
:
type_(UNSIGNED_INTEGER_64),
unsignedInteger64Token_(l),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const floatScalar s, label lineNumber)
:
type_(FLOAT_SCALAR),
floatScalarToken_(s),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const doubleScalar s, label lineNumber)
:
type_(DOUBLE_SCALAR),
doubleScalarToken_(s),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const longDoubleScalar s, label lineNumber)
:
type_(LONG_DOUBLE_SCALAR),
longDoubleScalarTokenPtr_(new longDoubleScalar(s)),
lineNumber_(lineNumber)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
inline Foam::token::~token()
{
clear();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline Foam::token::tokenType Foam::token::type() const
{
return type_;
}
inline Foam::token::tokenType& Foam::token::type()
{
return type_;
}
inline bool Foam::token::good() const
{
return (type_ != ERROR && type_ != UNDEFINED);
}
inline bool Foam::token::undefined() const
{
return (type_ == UNDEFINED);
}
inline bool Foam::token::error() const
{
return (type_ == ERROR);
}
inline bool Foam::token::isPunctuation() const
{
return (type_ == PUNCTUATION);
}
inline Foam::token::punctuationToken Foam::token::pToken() const
{
if (type_ == PUNCTUATION)
{
return punctuationToken_;
}
else
{
parseError("punctuation character");
return NULL_TOKEN;
}
}
inline bool Foam::token::isWord() const
{
return (type_ == WORD);
}
inline const Foam::word& Foam::token::wordToken() const
{
if (type_ == WORD)
{
return *wordTokenPtr_;
}
else
{
parseError(word::typeName);
return word::null;
}
}
inline bool Foam::token::isFunctionName() const
{
return (type_ == FUNCTIONNAME);
}
inline const Foam::functionName& Foam::token::functionNameToken() const
{
if (type_ == FUNCTIONNAME)
{
return *functionNameTokenPtr_;
}
else
{
parseError(functionName::typeName);
return functionName::null;
}
}
inline bool Foam::token::isVariable() const
{
return (type_ == VARIABLE);
}
inline const Foam::variable& Foam::token::variableToken() const
{
if (type_ == VARIABLE)
{
return *variableTokenPtr_;
}
else
{
parseError(variable::typeName);
return variable::null;
}
}
inline bool Foam::token::isString() const
{
return (type_ == STRING);
}
inline const Foam::string& Foam::token::stringToken() const
{
if (type_ == STRING)
{
return *stringTokenPtr_;
}
else
{
parseError(string::typeName);
return string::null;
}
}
inline bool Foam::token::isVerbatimString() const
{
return (type_ == VERBATIMSTRING);
}
inline const Foam::verbatimString& Foam::token::verbatimStringToken() const
{
if (type_ == VERBATIMSTRING)
{
return *verbatimStringTokenPtr_;
}
else
{
parseError(verbatimString::typeName);
return verbatimString::null;
}
}
inline bool Foam::token::isAnyString() const
{
return
(
type_ == WORD
|| type_ == FUNCTIONNAME
|| type_ == VARIABLE
|| type_ == STRING
|| type_ == VERBATIMSTRING
);
}
inline const Foam::string& Foam::token::anyStringToken() const
{
if (type_ == WORD)
{
return *wordTokenPtr_;
}
else if (type_ == FUNCTIONNAME)
{
return *functionNameTokenPtr_;
}
else if (type_ == VARIABLE)
{
return *variableTokenPtr_;
}
else if (type_ == STRING)
{
return *stringTokenPtr_;
}
else if (type_ == VERBATIMSTRING)
{
return *verbatimStringTokenPtr_;
}
else
{
parseError(string::typeName);
return string::null;
}
}
inline bool Foam::token::isInteger32() const
{
return
type_ == INTEGER_32
|| (
type_ == INTEGER_64
&& (integer64Token_ >= INT32_MIN) && (integer64Token_ <= INT32_MAX)
)
|| (type_ == UNSIGNED_INTEGER_32 && unsignedInteger32Token_ <= INT32_MAX)
|| (type_ == UNSIGNED_INTEGER_64 && unsignedInteger64Token_ <= INT32_MAX);
}
inline int32_t Foam::token::integer32Token() const
{
if (type_ == INTEGER_32)
{
return integer32Token_;
}
else if (type_ == INTEGER_64)
{
return integer64Token_;
}
if (type_ == UNSIGNED_INTEGER_32)
{
return unsignedInteger32Token_;
}
else if (type_ == UNSIGNED_INTEGER_64)
{
return unsignedInteger64Token_;
}
else
{
parseError(pTraits::typeName);
return 0;
}
}
inline bool Foam::token::isInteger64() const
{
return
type_ == INTEGER_32
|| type_ == INTEGER_64
|| type_ == UNSIGNED_INTEGER_32
|| (type_ == UNSIGNED_INTEGER_64 && unsignedInteger64Token_ <= INT64_MAX);
}
inline int64_t Foam::token::integer64Token() const
{
if (type_ == INTEGER_32)
{
return integer32Token_;
}
else if (type_ == INTEGER_64)
{
return integer64Token_;
}
if (type_ == UNSIGNED_INTEGER_32)
{
return unsignedInteger32Token_;
}
else if (type_ == UNSIGNED_INTEGER_64)
{
return unsignedInteger64Token_;
}
else
{
parseError(pTraits::typeName);
return 0;
}
}
inline bool Foam::token::isUnsignedInteger32() const
{
return
(type_ == INTEGER_32 && integer32Token_ >= 0)
|| (
type_ == INTEGER_64
&& (integer64Token_ >= 0) && (integer64Token_ <= UINT32_MAX)
)
|| type_ == UNSIGNED_INTEGER_32
|| (type_ == UNSIGNED_INTEGER_64 && unsignedInteger64Token_ <= UINT32_MAX);
}
inline uint32_t Foam::token::unsignedInteger32Token() const
{
if (type_ == INTEGER_32)
{
return integer32Token_;
}
else if (type_ == INTEGER_64)
{
return integer64Token_;
}
if (type_ == UNSIGNED_INTEGER_32)
{
return unsignedInteger32Token_;
}
else if (type_ == UNSIGNED_INTEGER_64)
{
return unsignedInteger64Token_;
}
else
{
parseError(pTraits::typeName);
return 0;
}
}
inline bool Foam::token::isUnsignedInteger64() const
{
return
(type_ == INTEGER_32 && integer32Token_ >= 0)
|| (type_ == INTEGER_64 && integer64Token_ >= 0)
|| type_ == UNSIGNED_INTEGER_32
|| type_ == UNSIGNED_INTEGER_64;
}
inline uint64_t Foam::token::unsignedInteger64Token() const
{
if (type_ == INTEGER_32)
{
return integer32Token_;
}
else if (type_ == INTEGER_64)
{
return integer64Token_;
}
if (type_ == UNSIGNED_INTEGER_32)
{
return unsignedInteger32Token_;
}
else if (type_ == UNSIGNED_INTEGER_64)
{
return unsignedInteger64Token_;
}
else
{
parseError(pTraits::typeName);
return 0;
}
}
inline bool Foam::token::isLabel() const
{
return
type_ == INTEGER_32
|| (
type_ == INTEGER_64
&& integer64Token_ >= int64_t(labelMin)
&& integer64Token_ <= int64_t(labelMax)
)
|| (
type_ == UNSIGNED_INTEGER_32
&& uint64_t(unsignedInteger32Token_) <= uint64_t(labelMax)
)
|| (
type_ == UNSIGNED_INTEGER_64
&& unsignedInteger64Token_ <= uint64_t(labelMax)
);
}
inline Foam::label Foam::token::labelToken() const
{
if (type_ == INTEGER_32)
{
return integer32Token_;
}
else if (type_ == INTEGER_64)
{
return integer64Token_;
}
if (type_ == UNSIGNED_INTEGER_32)
{
return unsignedInteger32Token_;
}
else if (type_ == UNSIGNED_INTEGER_64)
{
return unsignedInteger64Token_;
}
else
{
parseError(pTraits