From 990d00d40d2e850c8a9652daf9dcedca59039da9 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 12 Oct 2018 09:09:36 +0200 Subject: [PATCH] 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. --- src/OpenFOAM/db/IOstreams/token/token.H | 39 ++++++++---- src/OpenFOAM/db/IOstreams/token/tokenI.H | 72 +++++++++++++++++++---- src/OpenFOAM/db/IOstreams/token/tokenIO.C | 14 ++++- 3 files changed, 97 insertions(+), 28 deletions(-) diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index 48d5cde872..eb2b19a1d0 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -79,6 +79,7 @@ public: UNDEFINED = 0, //!< An undefined token-type // Fundamental types + BOOL, //!< boolean type FLAG, //!< stream flag (1-byte bitmask) PUNCTUATION, //!< single character punctuation LABEL, //!< label (integer) type @@ -92,7 +93,7 @@ public: //!< dictionary \c $variable expansion VERBATIMSTRING, //!< Contents are a Foam::string representing verbatim //!< content - COMPOUND, //!< Compound type such as List\ etc. + COMPOUND, //!< Compound type such as \c List\ etc. ERROR //!< A token error encountered }; @@ -115,20 +116,22 @@ public: TAB = '\t', NL = '\n', - END_STATEMENT = ';', //!< End entry [#isseparator] - BEGIN_LIST = '(', //!< Begin list [#isseparator] - END_LIST = ')', //!< End list [#isseparator] - BEGIN_SQR = '[', //!< Begin dimensions [#isseparator] - END_SQR = ']', //!< End dimensions [#isseparator] - BEGIN_BLOCK = '{', //!< Begin block [#isseparator] - END_BLOCK = '}', //!< End block [#isseparator] - COLON = ':', //!< Colon [#isseparator] - COMMA = ',', //!< Comma [#isseparator] + END_STATEMENT = ';', //!< End entry [#isseparator] + BEGIN_LIST = '(', //!< Begin list [#isseparator] + END_LIST = ')', //!< End list [#isseparator] + BEGIN_SQR = '[', //!< Begin dimensions [#isseparator] + END_SQR = ']', //!< End dimensions [#isseparator] + BEGIN_BLOCK = '{', //!< Begin block [#isseparator] + END_BLOCK = '}', //!< End block [#isseparator] + COLON = ':', //!< Colon [#isseparator] + COMMA = ',', //!< Comma [#isseparator] HASH = '#', ATSYM = '@', + SQUOTE = '\'', //!< Single quote + DQUOTE = '"', //!< Double quote - BEGIN_STRING = '"', - END_STRING = BEGIN_STRING, + BEGIN_STRING = DQUOTE, //!< Double quote for begin string + END_STRING = DQUOTE, //!< Double quote for end string ASSIGN = '=', //!< Assignment/equals [#isseparator] ADD = '+', //!< Addition [#isseparator] @@ -358,6 +361,9 @@ public: // Static Member 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 @@ -406,6 +412,9 @@ public: //- True if token is ERROR inline bool error() const; + //- True if token is BOOL + inline bool isBool() const; + //- True if token is FLAG inline bool isFlag() const; @@ -445,7 +454,11 @@ public: // 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 inline int flagToken() const; diff --git a/src/OpenFOAM/db/IOstreams/token/tokenI.H b/src/OpenFOAM/db/IOstreams/token/tokenI.H index da3f0abcdf..6c37a3c7b1 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenI.H +++ b/src/OpenFOAM/db/IOstreams/token/tokenI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,6 +27,16 @@ License // * * * * * * * * * * * * * 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) { token tok; @@ -306,6 +316,23 @@ inline bool Foam::token::setType(token::tokenType 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::VARIABLE: case tokenType::VERBATIMSTRING: @@ -321,10 +348,10 @@ inline bool Foam::token::setType(token::tokenType variant) break; default: - return false; break; } } + break; default: 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 { return (type_ == tokenType::FLAG); @@ -423,7 +468,7 @@ inline Foam::label Foam::token::labelToken() const return data_.labelVal; } - parseError(pTraits