Commit 990d00d4 authored by Mark Olesen's avatar Mark Olesen
Browse files

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.
parent fdf19d67
......@@ -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\<label\> etc.
COMPOUND, //!< Compound type such as \c List\<label\> 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;
......
......@@ -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<label>::typeName);
parseError("label");
return 0;
}
......@@ -441,8 +486,8 @@ inline Foam::floatScalar Foam::token::floatScalarToken() const
return data_.floatVal;
}
parseError("floatScalar");
return 0.0;
parseError("float");
return 0;
}
......@@ -459,8 +504,8 @@ inline Foam::doubleScalar Foam::token::doubleScalarToken() const
return data_.doubleVal;
}
parseError("doubleScalar");
return 0.0;
parseError("double");
return 0;
}
......@@ -485,8 +530,8 @@ inline Foam::scalar Foam::token::scalarToken() const
return data_.doubleVal;
}
parseError(pTraits<scalar>::typeName);
return 0.0;
parseError("scalar");
return 0;
}
......@@ -508,7 +553,7 @@ inline Foam::scalar Foam::token::number() const
}
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;
}
parseError(word::typeName);
parseError("word");
return word::null;
}
......@@ -559,7 +604,7 @@ inline const Foam::string& Foam::token::stringToken() const
return *data_.stringPtr;
}
parseError(string::typeName);
parseError("string");
return string::null;
}
......@@ -742,6 +787,9 @@ inline bool Foam::token::operator==(const token& tok) const
case tokenType::UNDEFINED:
return true;
case tokenType::BOOL:
return data_.labelVal == tok.data_.labelVal;
case tokenType::FLAG:
return data_.flagVal == tok.data_.flagVal;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,6 +43,10 @@ static OS& printTokenInfo(OS& os, const token& tok)
os << "undefined token";
break;
case token::tokenType::BOOL:
os << "bool '" << (tok.boolToken() ? "true" : "false") << '\'';
break;
case token::tokenType::FLAG:
os << "flag '" << int(tok.flagToken()) << '\'';
break;
......@@ -121,6 +125,7 @@ Foam::word Foam::token::name() const
switch (type_)
{
case token::tokenType::UNDEFINED: return "undefined";
case token::tokenType::BOOL: return "bool";
case token::tokenType::FLAG: return "flag";
case token::tokenType::PUNCTUATION: return "punctuation";
case token::tokenType::LABEL: return "label";
......@@ -160,6 +165,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
<< "Undefined token" << endl;
break;
case token::tokenType::BOOL:
os << tok.data_.labelVal;
break;
case token::tokenType::FLAG:
// Swallow the flag
break;
......@@ -207,8 +216,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
default:
os << "UNKNOWN";
SeriousErrorInFunction
<< "Unknown token"
<< endl;
<< "Unknown token" << endl;
}
os.check(FUNCTION_NAME);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment