ENH: additional boolToken token type

- not used by the ISstream parser, but suitable for other parsing
  methods where true/false concept should be distinguishable from
  integer values.

  Only constructed via the token::boolean() static method, not
  directly assignable.
  This avoids any potential ambiguities with label.
This commit is contained in:
Mark Olesen
2018-10-12 09:09:36 +02:00
parent fdf19d67a4
commit 990d00d40d
3 changed files with 97 additions and 28 deletions

View File

@ -79,6 +79,7 @@ public:
UNDEFINED = 0, //!< An undefined token-type UNDEFINED = 0, //!< An undefined token-type
// Fundamental types // Fundamental types
BOOL, //!< boolean type
FLAG, //!< stream flag (1-byte bitmask) FLAG, //!< stream flag (1-byte bitmask)
PUNCTUATION, //!< single character punctuation PUNCTUATION, //!< single character punctuation
LABEL, //!< label (integer) type LABEL, //!< label (integer) type
@ -92,7 +93,7 @@ public:
//!< dictionary \c $variable expansion //!< dictionary \c $variable expansion
VERBATIMSTRING, //!< Contents are a Foam::string representing verbatim VERBATIMSTRING, //!< Contents are a Foam::string representing verbatim
//!< content //!< content
COMPOUND, //!< Compound type such as List\<label\> etc. COMPOUND, //!< Compound type such as \c List\<label\> etc.
ERROR //!< A token error encountered ERROR //!< A token error encountered
}; };
@ -115,20 +116,22 @@ public:
TAB = '\t', TAB = '\t',
NL = '\n', NL = '\n',
END_STATEMENT = ';', //!< End entry [#isseparator] END_STATEMENT = ';', //!< End entry [#isseparator]
BEGIN_LIST = '(', //!< Begin list [#isseparator] BEGIN_LIST = '(', //!< Begin list [#isseparator]
END_LIST = ')', //!< End list [#isseparator] END_LIST = ')', //!< End list [#isseparator]
BEGIN_SQR = '[', //!< Begin dimensions [#isseparator] BEGIN_SQR = '[', //!< Begin dimensions [#isseparator]
END_SQR = ']', //!< End dimensions [#isseparator] END_SQR = ']', //!< End dimensions [#isseparator]
BEGIN_BLOCK = '{', //!< Begin block [#isseparator] BEGIN_BLOCK = '{', //!< Begin block [#isseparator]
END_BLOCK = '}', //!< End block [#isseparator] END_BLOCK = '}', //!< End block [#isseparator]
COLON = ':', //!< Colon [#isseparator] COLON = ':', //!< Colon [#isseparator]
COMMA = ',', //!< Comma [#isseparator] COMMA = ',', //!< Comma [#isseparator]
HASH = '#', HASH = '#',
ATSYM = '@', ATSYM = '@',
SQUOTE = '\'', //!< Single quote
DQUOTE = '"', //!< Double quote
BEGIN_STRING = '"', BEGIN_STRING = DQUOTE, //!< Double quote for begin string
END_STRING = BEGIN_STRING, END_STRING = DQUOTE, //!< Double quote for end string
ASSIGN = '=', //!< Assignment/equals [#isseparator] ASSIGN = '=', //!< Assignment/equals [#isseparator]
ADD = '+', //!< Addition [#isseparator] ADD = '+', //!< Addition [#isseparator]
@ -358,6 +361,9 @@ public:
// Static Member Functions // Static Member Functions
//- Create a bool token.
inline static token boolean(bool on);
//- Create a token with stream flags, no sanity check //- Create a token with stream flags, no sanity check
// //
// \param bitmask the flags to set // \param bitmask the flags to set
@ -406,6 +412,9 @@ public:
//- True if token is ERROR //- True if token is ERROR
inline bool error() const; inline bool error() const;
//- True if token is BOOL
inline bool isBool() const;
//- True if token is FLAG //- True if token is FLAG
inline bool isFlag() const; inline bool isFlag() const;
@ -445,7 +454,11 @@ public:
// Access // Access
//- Return flag bitmask //- Return boolean token value.
// Report FatalIOError and return false if token is not BOOL
inline bool boolToken() const;
//- Return flag bitmask value.
// Report FatalIOError and return NO_FLAG if token is not FLAG // Report FatalIOError and return NO_FLAG if token is not FLAG
inline int flagToken() const; inline int flagToken() const;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,6 +27,16 @@ License
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
inline Foam::token Foam::token::boolean(bool on)
{
token tok;
tok.type_ = tokenType::BOOL;
tok.data_.labelVal = on;
return tok;
}
inline Foam::token Foam::token::flag(int bitmask) inline Foam::token Foam::token::flag(int bitmask)
{ {
token tok; token tok;
@ -306,6 +316,23 @@ inline bool Foam::token::setType(token::tokenType variant)
switch (variant) switch (variant)
{ {
case tokenType::BOOL:
case tokenType::LABEL:
{
switch (type_)
{
case tokenType::BOOL:
case tokenType::LABEL:
type_ = variant;
return true;
break;
default:
break;
}
}
break;
case tokenType::STRING: case tokenType::STRING:
case tokenType::VARIABLE: case tokenType::VARIABLE:
case tokenType::VERBATIMSTRING: case tokenType::VERBATIMSTRING:
@ -321,10 +348,10 @@ inline bool Foam::token::setType(token::tokenType variant)
break; break;
default: default:
return false;
break; break;
} }
} }
break;
default: default:
break; break;
@ -364,6 +391,24 @@ inline bool Foam::token::error() const
} }
inline bool Foam::token::isBool() const
{
return (type_ == tokenType::BOOL);
}
inline bool Foam::token::boolToken() const
{
if (type_ == tokenType::BOOL)
{
return data_.labelVal;
}
parseError("bool");
return false;
}
inline bool Foam::token::isFlag() const inline bool Foam::token::isFlag() const
{ {
return (type_ == tokenType::FLAG); return (type_ == tokenType::FLAG);
@ -423,7 +468,7 @@ inline Foam::label Foam::token::labelToken() const
return data_.labelVal; return data_.labelVal;
} }
parseError(pTraits<label>::typeName); parseError("label");
return 0; return 0;
} }
@ -441,8 +486,8 @@ inline Foam::floatScalar Foam::token::floatScalarToken() const
return data_.floatVal; return data_.floatVal;
} }
parseError("floatScalar"); parseError("float");
return 0.0; return 0;
} }
@ -459,8 +504,8 @@ inline Foam::doubleScalar Foam::token::doubleScalarToken() const
return data_.doubleVal; return data_.doubleVal;
} }
parseError("doubleScalar"); parseError("double");
return 0.0; return 0;
} }
@ -485,8 +530,8 @@ inline Foam::scalar Foam::token::scalarToken() const
return data_.doubleVal; return data_.doubleVal;
} }
parseError(pTraits<scalar>::typeName); parseError("scalar");
return 0.0; return 0;
} }
@ -508,7 +553,7 @@ inline Foam::scalar Foam::token::number() const
} }
parseError("number (label or scalar)"); parseError("number (label or scalar)");
return 0.0; return 0;
} }
@ -525,7 +570,7 @@ inline const Foam::word& Foam::token::wordToken() const
return *data_.wordPtr; return *data_.wordPtr;
} }
parseError(word::typeName); parseError("word");
return word::null; return word::null;
} }
@ -559,7 +604,7 @@ inline const Foam::string& Foam::token::stringToken() const
return *data_.stringPtr; return *data_.stringPtr;
} }
parseError(string::typeName); parseError("string");
return string::null; return string::null;
} }
@ -742,6 +787,9 @@ inline bool Foam::token::operator==(const token& tok) const
case tokenType::UNDEFINED: case tokenType::UNDEFINED:
return true; return true;
case tokenType::BOOL:
return data_.labelVal == tok.data_.labelVal;
case tokenType::FLAG: case tokenType::FLAG:
return data_.flagVal == tok.data_.flagVal; return data_.flagVal == tok.data_.flagVal;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -43,6 +43,10 @@ static OS& printTokenInfo(OS& os, const token& tok)
os << "undefined token"; os << "undefined token";
break; break;
case token::tokenType::BOOL:
os << "bool '" << (tok.boolToken() ? "true" : "false") << '\'';
break;
case token::tokenType::FLAG: case token::tokenType::FLAG:
os << "flag '" << int(tok.flagToken()) << '\''; os << "flag '" << int(tok.flagToken()) << '\'';
break; break;
@ -121,6 +125,7 @@ Foam::word Foam::token::name() const
switch (type_) switch (type_)
{ {
case token::tokenType::UNDEFINED: return "undefined"; case token::tokenType::UNDEFINED: return "undefined";
case token::tokenType::BOOL: return "bool";
case token::tokenType::FLAG: return "flag"; case token::tokenType::FLAG: return "flag";
case token::tokenType::PUNCTUATION: return "punctuation"; case token::tokenType::PUNCTUATION: return "punctuation";
case token::tokenType::LABEL: return "label"; case token::tokenType::LABEL: return "label";
@ -160,6 +165,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
<< "Undefined token" << endl; << "Undefined token" << endl;
break; break;
case token::tokenType::BOOL:
os << tok.data_.labelVal;
break;
case token::tokenType::FLAG: case token::tokenType::FLAG:
// Swallow the flag // Swallow the flag
break; break;
@ -207,8 +216,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
default: default:
os << "UNKNOWN"; os << "UNKNOWN";
SeriousErrorInFunction SeriousErrorInFunction
<< "Unknown token" << "Unknown token" << endl;
<< endl;
} }
os.check(FUNCTION_NAME); os.check(FUNCTION_NAME);